1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/auxv.h>
18
19 #include <errno.h>
20 #include <sys/cdefs.h>
21 #include <sys/utsname.h>
22 #include <gtest/gtest.h>
23
24 #include "utils.h"
25
TEST(getauxval,expected_values)26 TEST(getauxval, expected_values) {
27 ASSERT_EQ(0UL, getauxval(AT_SECURE));
28 ASSERT_EQ(getuid(), getauxval(AT_UID));
29 ASSERT_EQ(geteuid(), getauxval(AT_EUID));
30 ASSERT_EQ(getgid(), getauxval(AT_GID));
31 ASSERT_EQ(getegid(), getauxval(AT_EGID));
32 ASSERT_EQ(static_cast<unsigned long>(getpagesize()), getauxval(AT_PAGESZ));
33
34 ASSERT_NE(0UL, getauxval(AT_PHDR));
35 ASSERT_NE(0UL, getauxval(AT_PHNUM));
36 ASSERT_NE(0UL, getauxval(AT_ENTRY));
37 ASSERT_NE(0UL, getauxval(AT_PAGESZ));
38 }
39
TEST(getauxval,unexpected_values)40 TEST(getauxval, unexpected_values) {
41 errno = 0;
42 ASSERT_EQ(0UL, getauxval(0xdeadbeef));
43 ASSERT_ERRNO(ENOENT);
44 }
45
TEST(getauxval,arm_has_AT_HWCAP2)46 TEST(getauxval, arm_has_AT_HWCAP2) {
47 #if defined(__arm__)
48 // There are no known 32-bit processors that implement any of these instructions, so rather
49 // than require that OEMs backport kernel patches, let's just ignore old hardware. Strictly
50 // speaking this would be fooled by someone choosing to ship a 32-bit kernel on 64-bit hardware,
51 // but that doesn't seem very likely in 2016.
52 utsname u;
53 ASSERT_EQ(0, uname(&u));
54 if (strcmp(u.machine, "aarch64") == 0) {
55 // If this test fails, apps that use getauxval to decide at runtime whether crypto hardware is
56 // available will incorrectly assume that it isn't, and will have really bad performance.
57 // If this test fails, ensure that you've enabled COMPAT_BINFMT_ELF in your kernel configuration.
58 // Note that 0 ("I don't support any of these things") is a legitimate response --- we need
59 // to check errno to see whether we got a "true" 0 or a "not found" 0.
60 errno = 0;
61 getauxval(AT_HWCAP2);
62 ASSERT_ERRNO(0) << "64-bit kernel not reporting AT_HWCAP2 to 32-bit ARM process";
63 return;
64 }
65 #endif
66 GTEST_SKIP() << "This test is only meaningful for 32-bit ARM code on 64-bit devices";
67 }
68