| Peter Collingbourne | 7a0f04c | 2019-01-23 17:56:24 -0800 | [diff] [blame] | 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 | |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 19 | #include <sys/auxv.h> |
| 20 | #if defined(__BIONIC__) |
| 21 | #include <sys/ifunc.h> |
| 22 | #endif |
| 23 | |
| 24 | typedef int (*fn_ptr_t)(); |
| 25 | |
| Peter Collingbourne | 7a0f04c | 2019-01-23 17:56:24 -0800 | [diff] [blame] | 26 | int ret42() { |
| 27 | return 42; |
| 28 | } |
| 29 | |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 30 | extern "C" fn_ptr_t resolver() { |
| 31 | return ret42; |
| Peter Collingbourne | 7a0f04c | 2019-01-23 17:56:24 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | int ifunc() __attribute__((ifunc("resolver"))); |
| 35 | |
| 36 | TEST(ifunc, function) { |
| 37 | ASSERT_EQ(42, ifunc()); |
| 38 | } |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 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 | |
| Elliott Hughes | 19b2ce8 | 2020-04-13 14:09:26 -0700 | [diff] [blame] | 47 | extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __ifunc_arg_t* arg) |
| 48 | __attribute__((no_sanitize("hwaddress"))) { |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 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 | |
| 58 | extern "C" fn_ptr_t hwcap_resolver(unsigned long hwcap) { |
| 59 | g_hwcap = hwcap; |
| 60 | return ret42; |
| 61 | } |
| 62 | |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 63 | #elif defined(__riscv) |
| 64 | |
| 65 | #include <sys/hwprobe.h> |
| 66 | |
| 67 | static uint64_t g_hwcap; |
| Elliott Hughes | 561e804 | 2023-12-05 16:23:53 -0800 | [diff] [blame] | 68 | static __riscv_hwprobe_t g_hwprobe_ptr; |
| 69 | static void* g_null; |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 70 | |
| 71 | static riscv_hwprobe g_hwprobes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}}; |
| 72 | |
| Elliott Hughes | 561e804 | 2023-12-05 16:23:53 -0800 | [diff] [blame] | 73 | extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __riscv_hwprobe_t hwprobe_ptr, void* null) { |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 74 | g_hwcap = hwcap; |
| Elliott Hughes | 561e804 | 2023-12-05 16:23:53 -0800 | [diff] [blame] | 75 | g_hwprobe_ptr = hwprobe_ptr; |
| 76 | g_null = null; |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 77 | |
| 78 | // Ensure that __riscv_hwprobe() can be called from an ifunc. |
| Elliott Hughes | 561e804 | 2023-12-05 16:23:53 -0800 | [diff] [blame] | 79 | if ((*hwprobe_ptr)(g_hwprobes, 1, 0, nullptr, 0) != 0) return nullptr; |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 80 | return ret42; |
| 81 | } |
| 82 | |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 83 | #else |
| 84 | |
| 85 | extern "C" fn_ptr_t hwcap_resolver() { |
| 86 | return ret42; |
| 87 | } |
| 88 | |
| 89 | #endif |
| 90 | |
| 91 | int hwcap() __attribute__((ifunc("hwcap_resolver"))); |
| 92 | |
| 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); |
| Elliott Hughes | 6b5b792 | 2025-08-12 13:39:47 -0700 | [diff] [blame] | 100 | EXPECT_EQ(5 * sizeof(uint64_t), g_arg._size); |
| 101 | |
| 102 | // Test the fields. |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 103 | EXPECT_EQ(getauxval(AT_HWCAP), g_arg._hwcap); |
| 104 | EXPECT_EQ(getauxval(AT_HWCAP2), g_arg._hwcap2); |
| Elliott Hughes | 6b5b792 | 2025-08-12 13:39:47 -0700 | [diff] [blame] | 105 | EXPECT_EQ(getauxval(AT_HWCAP3), g_arg._hwcap3); |
| 106 | EXPECT_EQ(getauxval(AT_HWCAP4), g_arg._hwcap4); |
| 107 | |
| 108 | // (Real users should just declare their ifunc resolver with the appropriate |
| 109 | // type, but we're deliberately trying to test all the options.) |
| 110 | uint64_t* raw_arg = reinterpret_cast<uint64_t*>(&g_arg); |
| 111 | |
| 112 | // Test the helper function. |
| 113 | EXPECT_EQ(getauxval(AT_HWCAP), __ifunc_hwcap(1, g_hwcap, raw_arg)); |
| 114 | EXPECT_EQ(getauxval(AT_HWCAP2), __ifunc_hwcap(2, g_hwcap, raw_arg)); |
| 115 | EXPECT_EQ(getauxval(AT_HWCAP3), __ifunc_hwcap(3, g_hwcap, raw_arg)); |
| 116 | EXPECT_EQ(getauxval(AT_HWCAP4), __ifunc_hwcap(4, g_hwcap, raw_arg)); |
| 117 | |
| 118 | // Test helper function edge cases. |
| 119 | EXPECT_EQ(0u, __ifunc_hwcap(-1, g_hwcap, raw_arg)); |
| 120 | EXPECT_EQ(0u, __ifunc_hwcap(5, g_hwcap, raw_arg)); |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 121 | #elif defined(__arm__) |
| 122 | EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap); |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 123 | #elif defined(__riscv) |
| Elliott Hughes | 561e804 | 2023-12-05 16:23:53 -0800 | [diff] [blame] | 124 | printf("hwcap=%lx hwprobe_ptr=%p (__riscv_hwprobe=%p) null=%p\n", g_hwcap, g_hwprobe_ptr, |
| 125 | __riscv_hwprobe, g_null); |
| 126 | |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 127 | EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap); |
| Elliott Hughes | 561e804 | 2023-12-05 16:23:53 -0800 | [diff] [blame] | 128 | EXPECT_EQ(nullptr, g_null); |
| Elliott Hughes | 620a722 | 2023-08-07 12:12:36 -0700 | [diff] [blame] | 129 | |
| 130 | riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}}; |
| 131 | ASSERT_EQ(0, __riscv_hwprobe(probes, 1, 0, nullptr, 0)); |
| 132 | EXPECT_EQ(probes[0].value, g_hwprobes[0].value); |
| Peter Collingbourne | e949195 | 2019-10-28 10:57:26 -0700 | [diff] [blame] | 133 | #endif |
| 134 | } |
| 135 | |
| 136 | #endif // defined(__BIONIC__) |