blob: 6499f892692584a320f02723baee2885af94c94b [file] [log] [blame]
Nick Kralevich2c5153b2013-01-11 14:43:05 -08001/*
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 Micayee7649c2015-03-15 21:39:25 -040017#include <errno.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080018#include <sys/cdefs.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080019#include <gtest/gtest.h>
20
21// getauxval() was only added as of glibc version 2.16.
22// See: http://lwn.net/Articles/519085/
23// Don't try to compile this code on older glibc versions.
24
25#if defined(__BIONIC__)
26 #define GETAUXVAL_CAN_COMPILE 1
27#elif defined(__GLIBC_PREREQ)
28 #if __GLIBC_PREREQ(2, 16)
29 #define GETAUXVAL_CAN_COMPILE 1
30 #endif
31#endif
32
33#if defined(GETAUXVAL_CAN_COMPILE)
Nick Kralevich2c5153b2013-01-11 14:43:05 -080034#include <sys/auxv.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080035#endif
Nick Kralevich2c5153b2013-01-11 14:43:05 -080036
37TEST(getauxval, expected_values) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080038#if defined(GETAUXVAL_CAN_COMPILE)
Elliott Hughesebb48952016-03-18 18:36:04 -070039 ASSERT_EQ(0UL, getauxval(AT_SECURE));
Nick Kralevich2c5153b2013-01-11 14:43:05 -080040 ASSERT_EQ(getuid(), getauxval(AT_UID));
41 ASSERT_EQ(geteuid(), getauxval(AT_EUID));
42 ASSERT_EQ(getgid(), getauxval(AT_GID));
43 ASSERT_EQ(getegid(), getauxval(AT_EGID));
Elliott Hughesebb48952016-03-18 18:36:04 -070044 ASSERT_EQ(static_cast<unsigned long>(getpagesize()), getauxval(AT_PAGESZ));
Nick Kralevich2c5153b2013-01-11 14:43:05 -080045
Elliott Hughesebb48952016-03-18 18:36:04 -070046 ASSERT_NE(0UL, getauxval(AT_PHDR));
47 ASSERT_NE(0UL, getauxval(AT_PHNUM));
48 ASSERT_NE(0UL, getauxval(AT_ENTRY));
49 ASSERT_NE(0UL, getauxval(AT_PAGESZ));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080050#else
Elliott Hughesebb48952016-03-18 18:36:04 -070051 GTEST_LOG_(INFO) << "This test requires a C library with getauxval.\n";
Christopher Ferrisf04935c2013-12-20 18:43:21 -080052#endif
Nick Kralevich2c5153b2013-01-11 14:43:05 -080053}
54
55TEST(getauxval, unexpected_values) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080056#if defined(GETAUXVAL_CAN_COMPILE)
Daniel Micayee7649c2015-03-15 21:39:25 -040057 errno = 0;
Elliott Hughesebb48952016-03-18 18:36:04 -070058 ASSERT_EQ(0UL, getauxval(0xdeadbeef));
Daniel Micayee7649c2015-03-15 21:39:25 -040059 ASSERT_EQ(ENOENT, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -080060#else
Elliott Hughesebb48952016-03-18 18:36:04 -070061 GTEST_LOG_(INFO) << "This test requires a C library with getauxval.\n";
62#endif
63}
64
65TEST(getauxval, arm_has_AT_HWCAP2) {
66#if defined(__arm__)
67 // If this test fails, apps that use getauxval to decide at runtime whether crypto hardware is
68 // available will incorrectly assume that it isn't, and will have really bad performance.
69 // If this test fails, ensure that you've enabled COMPAT_BINFMT_ELF in your kernel configuration.
70 ASSERT_NE(0UL, getauxval(AT_HWCAP2)) << "kernel not reporting AT_HWCAP2 to 32-bit ARM process";
71#else
72 GTEST_LOG_(INFO) << "This test is only meaningful for 32-bit ARM code.\n";
Christopher Ferrisf04935c2013-12-20 18:43:21 -080073#endif
Nick Kralevich2c5153b2013-01-11 14:43:05 -080074}