| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 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 |  | 
| Daniel Micay | ee7649c | 2015-03-15 21:39:25 -0400 | [diff] [blame] | 17 | #include <errno.h> | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 18 | #include <sys/cdefs.h> | 
| Elliott Hughes | 900a4dc | 2016-03-28 11:53:12 -0700 | [diff] [blame] | 19 | #include <sys/utsname.h> | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 20 | #include <gtest/gtest.h> | 
|  | 21 |  | 
|  | 22 | // getauxval() was only added as of glibc version 2.16. | 
|  | 23 | // See: http://lwn.net/Articles/519085/ | 
|  | 24 | // Don't try to compile this code on older glibc versions. | 
|  | 25 |  | 
|  | 26 | #if defined(__BIONIC__) | 
|  | 27 | #define GETAUXVAL_CAN_COMPILE 1 | 
|  | 28 | #elif defined(__GLIBC_PREREQ) | 
|  | 29 | #if __GLIBC_PREREQ(2, 16) | 
|  | 30 | #define GETAUXVAL_CAN_COMPILE 1 | 
|  | 31 | #endif | 
|  | 32 | #endif | 
|  | 33 |  | 
|  | 34 | #if defined(GETAUXVAL_CAN_COMPILE) | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 35 | #include <sys/auxv.h> | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 36 | #endif | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | TEST(getauxval, expected_values) { | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 39 | #if defined(GETAUXVAL_CAN_COMPILE) | 
| Elliott Hughes | ebb4895 | 2016-03-18 18:36:04 -0700 | [diff] [blame] | 40 | ASSERT_EQ(0UL, getauxval(AT_SECURE)); | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 41 | ASSERT_EQ(getuid(), getauxval(AT_UID)); | 
|  | 42 | ASSERT_EQ(geteuid(), getauxval(AT_EUID)); | 
|  | 43 | ASSERT_EQ(getgid(), getauxval(AT_GID)); | 
|  | 44 | ASSERT_EQ(getegid(), getauxval(AT_EGID)); | 
| Elliott Hughes | ebb4895 | 2016-03-18 18:36:04 -0700 | [diff] [blame] | 45 | ASSERT_EQ(static_cast<unsigned long>(getpagesize()), getauxval(AT_PAGESZ)); | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 46 |  | 
| Elliott Hughes | ebb4895 | 2016-03-18 18:36:04 -0700 | [diff] [blame] | 47 | ASSERT_NE(0UL, getauxval(AT_PHDR)); | 
|  | 48 | ASSERT_NE(0UL, getauxval(AT_PHNUM)); | 
|  | 49 | ASSERT_NE(0UL, getauxval(AT_ENTRY)); | 
|  | 50 | ASSERT_NE(0UL, getauxval(AT_PAGESZ)); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 51 | #else | 
| Elliott Hughes | ebb4895 | 2016-03-18 18:36:04 -0700 | [diff] [blame] | 52 | GTEST_LOG_(INFO) << "This test requires a C library with getauxval.\n"; | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 53 | #endif | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | TEST(getauxval, unexpected_values) { | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 57 | #if defined(GETAUXVAL_CAN_COMPILE) | 
| Daniel Micay | ee7649c | 2015-03-15 21:39:25 -0400 | [diff] [blame] | 58 | errno = 0; | 
| Elliott Hughes | ebb4895 | 2016-03-18 18:36:04 -0700 | [diff] [blame] | 59 | ASSERT_EQ(0UL, getauxval(0xdeadbeef)); | 
| Daniel Micay | ee7649c | 2015-03-15 21:39:25 -0400 | [diff] [blame] | 60 | ASSERT_EQ(ENOENT, errno); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 61 | #else | 
| Elliott Hughes | ebb4895 | 2016-03-18 18:36:04 -0700 | [diff] [blame] | 62 | GTEST_LOG_(INFO) << "This test requires a C library with getauxval.\n"; | 
|  | 63 | #endif | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | TEST(getauxval, arm_has_AT_HWCAP2) { | 
|  | 67 | #if defined(__arm__) | 
| Elliott Hughes | 900a4dc | 2016-03-28 11:53:12 -0700 | [diff] [blame] | 68 | // There are no known 32-bit processors that implement any of these instructions, so rather | 
|  | 69 | // than require that OEMs backport kernel patches, let's just ignore old hardware. Strictly | 
|  | 70 | // speaking this would be fooled by someone choosing to ship a 32-bit kernel on 64-bit hardware, | 
|  | 71 | // but that doesn't seem very likely in 2016. | 
|  | 72 | utsname u; | 
|  | 73 | ASSERT_EQ(0, uname(&u)); | 
| Yabin Cui | 78f5eb0 | 2016-03-29 15:41:49 -0700 | [diff] [blame] | 74 | if (strcmp(u.machine, "aarch64") == 0) { | 
|  | 75 | // If this test fails, apps that use getauxval to decide at runtime whether crypto hardware is | 
|  | 76 | // available will incorrectly assume that it isn't, and will have really bad performance. | 
|  | 77 | // If this test fails, ensure that you've enabled COMPAT_BINFMT_ELF in your kernel configuration. | 
|  | 78 | // Note that 0 ("I don't support any of these things") is a legitimate response --- we need | 
|  | 79 | // to check errno to see whether we got a "true" 0 or a "not found" 0. | 
|  | 80 | errno = 0; | 
|  | 81 | getauxval(AT_HWCAP2); | 
|  | 82 | ASSERT_EQ(0, errno) << "64-bit kernel not reporting AT_HWCAP2 to 32-bit ARM process"; | 
| Elliott Hughes | 900a4dc | 2016-03-28 11:53:12 -0700 | [diff] [blame] | 83 | return; | 
|  | 84 | } | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 85 | #endif | 
| Yabin Cui | 78f5eb0 | 2016-03-29 15:41:49 -0700 | [diff] [blame] | 86 | GTEST_LOG_(INFO) << "This test is only meaningful for 32-bit ARM code on 64-bit devices.\n"; | 
| Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 87 | } |