• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <sys/auxv.h>
20 #if defined(__BIONIC__)
21 #include <sys/ifunc.h>
22 #endif
23 
24 typedef int (*fn_ptr_t)();
25 
ret42()26 int ret42() {
27   return 42;
28 }
29 
resolver()30 extern "C" fn_ptr_t resolver() {
31   return ret42;
32 }
33 
34 int ifunc() __attribute__((ifunc("resolver")));
35 
TEST(ifunc,function)36 TEST(ifunc, function) {
37   ASSERT_EQ(42, ifunc());
38 }
39 
40 #if defined(__BIONIC__)
41 
42 #if defined(__aarch64__)
43 
44 static uint64_t g_hwcap;
45 static __ifunc_arg_t g_arg;
46 
hwcap_resolver(uint64_t hwcap,__ifunc_arg_t * arg)47 extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __ifunc_arg_t* arg)
48     __attribute__((no_sanitize("hwaddress"))) {
49   g_hwcap = hwcap;
50   g_arg = *arg;
51   return ret42;
52 }
53 
54 #elif defined(__arm__)
55 
56 static unsigned long g_hwcap;
57 
hwcap_resolver(unsigned long hwcap)58 extern "C" fn_ptr_t hwcap_resolver(unsigned long hwcap) {
59   g_hwcap = hwcap;
60   return ret42;
61 }
62 
63 #elif defined(__riscv)
64 
65 #include <sys/hwprobe.h>
66 
67 static uint64_t g_hwcap;
68 static __riscv_hwprobe_t g_hwprobe_ptr;
69 static void* g_null;
70 
71 static riscv_hwprobe g_hwprobes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}};
72 
hwcap_resolver(uint64_t hwcap,__riscv_hwprobe_t hwprobe_ptr,void * null)73 extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __riscv_hwprobe_t hwprobe_ptr, void* null) {
74   g_hwcap = hwcap;
75   g_hwprobe_ptr = hwprobe_ptr;
76   g_null = null;
77 
78   // Ensure that __riscv_hwprobe() can be called from an ifunc.
79   if ((*hwprobe_ptr)(g_hwprobes, 1, 0, nullptr, 0) != 0) return nullptr;
80   return ret42;
81 }
82 
83 #else
84 
hwcap_resolver()85 extern "C" fn_ptr_t hwcap_resolver() {
86   return ret42;
87 }
88 
89 #endif
90 
91 int hwcap() __attribute__((ifunc("hwcap_resolver")));
92 
TEST(ifunc,hwcap)93 TEST(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);
104 #elif defined(__riscv)
105   printf("hwcap=%lx hwprobe_ptr=%p (__riscv_hwprobe=%p) null=%p\n", g_hwcap, g_hwprobe_ptr,
106          __riscv_hwprobe, g_null);
107 
108   EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap);
109   EXPECT_EQ(nullptr, g_null);
110 
111   riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}};
112   ASSERT_EQ(0, __riscv_hwprobe(probes, 1, 0, nullptr, 0));
113   EXPECT_EQ(probes[0].value, g_hwprobes[0].value);
114 #endif
115 }
116 
117 #endif  // defined(__BIONIC__)
118