blob: f4ec7f57048c509c7c90098089df7f6a1d463d74 [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
Elliott Hughes18181e62019-01-30 13:52:36 -080017#include <sys/auxv.h>
18
Daniel Micayee7649c2015-03-15 21:39:25 -040019#include <errno.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080020#include <sys/cdefs.h>
Elliott Hughes900a4dc2016-03-28 11:53:12 -070021#include <sys/utsname.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080022#include <gtest/gtest.h>
23
Nick Kralevich2c5153b2013-01-11 14:43:05 -080024TEST(getauxval, expected_values) {
Elliott Hughesebb48952016-03-18 18:36:04 -070025 ASSERT_EQ(0UL, getauxval(AT_SECURE));
Nick Kralevich2c5153b2013-01-11 14:43:05 -080026 ASSERT_EQ(getuid(), getauxval(AT_UID));
27 ASSERT_EQ(geteuid(), getauxval(AT_EUID));
28 ASSERT_EQ(getgid(), getauxval(AT_GID));
29 ASSERT_EQ(getegid(), getauxval(AT_EGID));
Elliott Hughesebb48952016-03-18 18:36:04 -070030 ASSERT_EQ(static_cast<unsigned long>(getpagesize()), getauxval(AT_PAGESZ));
Nick Kralevich2c5153b2013-01-11 14:43:05 -080031
Elliott Hughesebb48952016-03-18 18:36:04 -070032 ASSERT_NE(0UL, getauxval(AT_PHDR));
33 ASSERT_NE(0UL, getauxval(AT_PHNUM));
34 ASSERT_NE(0UL, getauxval(AT_ENTRY));
35 ASSERT_NE(0UL, getauxval(AT_PAGESZ));
Nick Kralevich2c5153b2013-01-11 14:43:05 -080036}
37
38TEST(getauxval, unexpected_values) {
Daniel Micayee7649c2015-03-15 21:39:25 -040039 errno = 0;
Elliott Hughesebb48952016-03-18 18:36:04 -070040 ASSERT_EQ(0UL, getauxval(0xdeadbeef));
Daniel Micayee7649c2015-03-15 21:39:25 -040041 ASSERT_EQ(ENOENT, errno);
Elliott Hughesebb48952016-03-18 18:36:04 -070042}
43
44TEST(getauxval, arm_has_AT_HWCAP2) {
45#if defined(__arm__)
Elliott Hughes900a4dc2016-03-28 11:53:12 -070046 // There are no known 32-bit processors that implement any of these instructions, so rather
47 // than require that OEMs backport kernel patches, let's just ignore old hardware. Strictly
48 // speaking this would be fooled by someone choosing to ship a 32-bit kernel on 64-bit hardware,
49 // but that doesn't seem very likely in 2016.
50 utsname u;
51 ASSERT_EQ(0, uname(&u));
Yabin Cui78f5eb02016-03-29 15:41:49 -070052 if (strcmp(u.machine, "aarch64") == 0) {
53 // If this test fails, apps that use getauxval to decide at runtime whether crypto hardware is
54 // available will incorrectly assume that it isn't, and will have really bad performance.
55 // If this test fails, ensure that you've enabled COMPAT_BINFMT_ELF in your kernel configuration.
56 // Note that 0 ("I don't support any of these things") is a legitimate response --- we need
57 // to check errno to see whether we got a "true" 0 or a "not found" 0.
58 errno = 0;
59 getauxval(AT_HWCAP2);
60 ASSERT_EQ(0, errno) << "64-bit kernel not reporting AT_HWCAP2 to 32-bit ARM process";
Elliott Hughes900a4dc2016-03-28 11:53:12 -070061 return;
62 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -080063#endif
Elliott Hughesbcaa4542019-03-08 15:20:23 -080064 GTEST_SKIP() << "This test is only meaningful for 32-bit ARM code on 64-bit devices";
Nick Kralevich2c5153b2013-01-11 14:43:05 -080065}