blob: 1fdbf1a288ab0b5c05d67808d4f4faaba7a0a146 [file] [log] [blame]
Peter Collingbourne7a0f04c2019-01-23 17:56:24 -08001/*
2 * Copyright (C) 2019 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 <gtest/gtest.h>
18
Peter Collingbournee9491952019-10-28 10:57:26 -070019#include <sys/auxv.h>
20#if defined(__BIONIC__)
21#include <sys/ifunc.h>
22#endif
23
24typedef int (*fn_ptr_t)();
25
Peter Collingbourne7a0f04c2019-01-23 17:56:24 -080026int ret42() {
27 return 42;
28}
29
Peter Collingbournee9491952019-10-28 10:57:26 -070030extern "C" fn_ptr_t resolver() {
31 return ret42;
Peter Collingbourne7a0f04c2019-01-23 17:56:24 -080032}
33
34int ifunc() __attribute__((ifunc("resolver")));
35
36TEST(ifunc, function) {
37 ASSERT_EQ(42, ifunc());
38}
Peter Collingbournee9491952019-10-28 10:57:26 -070039
40#if defined(__BIONIC__)
41
42#if defined(__aarch64__)
43
44static uint64_t g_hwcap;
45static __ifunc_arg_t g_arg;
46
Elliott Hughes19b2ce82020-04-13 14:09:26 -070047extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __ifunc_arg_t* arg)
48 __attribute__((no_sanitize("hwaddress"))) {
Peter Collingbournee9491952019-10-28 10:57:26 -070049 g_hwcap = hwcap;
50 g_arg = *arg;
51 return ret42;
52}
53
54#elif defined(__arm__)
55
56static unsigned long g_hwcap;
57
58extern "C" fn_ptr_t hwcap_resolver(unsigned long hwcap) {
59 g_hwcap = hwcap;
60 return ret42;
61}
62
Elliott Hughes620a7222023-08-07 12:12:36 -070063#elif defined(__riscv)
64
65#include <sys/hwprobe.h>
66
67static uint64_t g_hwcap;
68
69static riscv_hwprobe g_hwprobes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}};
70
71extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, void* null) {
72 // Check hwcap like arm32/arm64.
73 g_hwcap = hwcap;
74
75 // For now, the pointer argument is reserved for future expansion.
76 if (null != NULL) abort();
77
78 // Ensure that __riscv_hwprobe() can be called from an ifunc.
79 if (__riscv_hwprobe(g_hwprobes, 1, 0, nullptr, 0) != 0) return nullptr;
80 return ret42;
81}
82
Peter Collingbournee9491952019-10-28 10:57:26 -070083#else
84
85extern "C" fn_ptr_t hwcap_resolver() {
86 return ret42;
87}
88
89#endif
90
91int hwcap() __attribute__((ifunc("hwcap_resolver")));
92
93TEST(ifunc, hwcap) {
94 ASSERT_EQ(42, hwcap());
95
96#if defined(__aarch64__)
97 EXPECT_EQ(getauxval(AT_HWCAP) | _IFUNC_ARG_HWCAP, g_hwcap);
98
99 EXPECT_EQ(sizeof(__ifunc_arg_t), g_arg._size);
100 EXPECT_EQ(getauxval(AT_HWCAP), g_arg._hwcap);
101 EXPECT_EQ(getauxval(AT_HWCAP2), g_arg._hwcap2);
102#elif defined(__arm__)
103 EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap);
Elliott Hughes620a7222023-08-07 12:12:36 -0700104#elif defined(__riscv)
105 EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap);
106
107 riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}};
108 ASSERT_EQ(0, __riscv_hwprobe(probes, 1, 0, nullptr, 0));
109 EXPECT_EQ(probes[0].value, g_hwprobes[0].value);
Peter Collingbournee9491952019-10-28 10:57:26 -0700110#endif
111}
112
113#endif // defined(__BIONIC__)