Elliott Hughes | a4c7876 | 2019-09-17 09:53:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 31 | #include <stdint.h> |
| 32 | #include <sys/ifunc.h> |
| 33 | |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 34 | #include <private/bionic_call_ifunc_resolver.h> |
| 35 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 36 | #if defined(__aarch64__) |
| 37 | #define IFUNC_ARGS (uint64_t hwcap __attribute__((unused)), \ |
| 38 | __ifunc_arg_t* arg __attribute__((unused))) |
| 39 | #elif defined(__arm__) |
| 40 | #define IFUNC_ARGS (unsigned long hwcap __attribute__((unused))) |
| 41 | #else |
| 42 | #define IFUNC_ARGS () |
| 43 | #endif |
| 44 | |
Elliott Hughes | a4c7876 | 2019-09-17 09:53:14 -0700 | [diff] [blame] | 45 | #define DECLARE_FUNC(type, name) \ |
| 46 | __attribute__((visibility("hidden"))) \ |
| 47 | type name |
| 48 | |
| 49 | #define RETURN_FUNC(type, name) { \ |
| 50 | DECLARE_FUNC(type, name); \ |
| 51 | return name; \ |
| 52 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 53 | |
| 54 | #if defined(BIONIC_DYNAMIC_DISPATCH) |
| 55 | |
| 56 | // We can't have HWASAN enabled in resolvers because they may be called before |
| 57 | // HWASAN is initialized. |
| 58 | #define DEFINE_IFUNC_FOR(name) \ |
| 59 | name##_func_t name __attribute__((ifunc(#name "_resolver"))); \ |
| 60 | __attribute__((visibility("hidden"))) \ |
| 61 | __attribute__((no_sanitize("hwaddress"))) name##_func_t* name##_resolver IFUNC_ARGS |
| 62 | |
| 63 | #define DEFINE_STATIC_SHIM(x) |
| 64 | |
| 65 | #elif defined(BIONIC_STATIC_DISPATCH) |
| 66 | |
| 67 | #define DEFINE_IFUNC_FOR(name) \ |
| 68 | name##_func_t* name##_resolver IFUNC_ARGS; \ |
| 69 | __attribute__((visibility("hidden"))) \ |
| 70 | __attribute__((no_sanitize("hwaddress"))) name##_func_t* name##_resolver IFUNC_ARGS |
| 71 | |
| 72 | #define DEFINE_STATIC_SHIM(x) x |
| 73 | |
| 74 | #define FORWARD(name) \ |
| 75 | static name##_func_t* fn = reinterpret_cast<name##_func_t*>( \ |
| 76 | __bionic_call_ifunc_resolver(reinterpret_cast<ElfW(Addr)>(name##_resolver))); \ |
| 77 | return fn |
| 78 | |
| 79 | #else |
| 80 | #error neither dynamic nor static dispatch?! |
| 81 | #endif |
| 82 | |
| 83 | typedef void* memchr_func_t(const void*, int, size_t); |
| 84 | #define MEMCHR_SHIM() \ |
| 85 | DEFINE_STATIC_SHIM(void* memchr(const void* src, int ch, size_t n) { \ |
| 86 | FORWARD(memchr)(src, ch, n); \ |
| 87 | }) |
| 88 | |
| 89 | typedef int memcmp_func_t(const void*, const void*, size_t); |
| 90 | #define MEMCMP_SHIM() \ |
| 91 | DEFINE_STATIC_SHIM(int memcmp(const void* lhs, const void* rhs, size_t n) { \ |
| 92 | FORWARD(memcmp)(lhs, rhs, n); \ |
| 93 | }) |
| 94 | |
| 95 | typedef void* memcpy_func_t(void*, const void*, size_t); |
| 96 | #define MEMCPY_SHIM() \ |
| 97 | DEFINE_STATIC_SHIM(void* memcpy(void* dst, const void* src, size_t n) { \ |
| 98 | FORWARD(memcpy)(dst, src, n); \ |
| 99 | }) |
| 100 | |
| 101 | typedef void* memmove_func_t(void*, const void*, size_t); |
| 102 | #define MEMMOVE_SHIM() \ |
| 103 | DEFINE_STATIC_SHIM(void* memmove(void* dst, const void* src, size_t n) { \ |
| 104 | FORWARD(memmove)(dst, src, n); \ |
| 105 | }) |
| 106 | |
| 107 | typedef int memrchr_func_t(const void*, int, size_t); |
| 108 | #define MEMRCHR_SHIM() \ |
| 109 | DEFINE_STATIC_SHIM(int memrchr(const void* src, int ch, size_t n) { \ |
| 110 | FORWARD(memrchr)(src, ch, n); \ |
| 111 | }) |
| 112 | |
| 113 | typedef void* memset_func_t(void*, int, size_t); |
| 114 | #define MEMSET_SHIM() \ |
| 115 | DEFINE_STATIC_SHIM(void* memset(void* dst, int ch, size_t n) { FORWARD(memset)(dst, ch, n); }) |
| 116 | |
| 117 | typedef void* __memset_chk_func_t(void*, int, size_t, size_t); |
| 118 | #define __MEMSET_CHK_SHIM() \ |
| 119 | DEFINE_STATIC_SHIM(void* __memset_chk(void* dst, int ch, size_t n, size_t n2) { \ |
| 120 | FORWARD(__memset_chk)(dst, ch, n, n2); \ |
| 121 | }) |
| 122 | |
| 123 | typedef char* stpcpy_func_t(char*, const char*); |
| 124 | #define STPCPY_SHIM() \ |
| 125 | DEFINE_STATIC_SHIM(char* stpcpy(char* dst, const char* src) { FORWARD(stpcpy)(dst, src); }) |
| 126 | |
| 127 | typedef char* strcat_func_t(char*, const char*); |
| 128 | #define STRCAT_SHIM() \ |
| 129 | DEFINE_STATIC_SHIM(char* strcat(char* dst, const char* src) { FORWARD(strcat)(dst, src); }) |
| 130 | |
| 131 | typedef char* __strcat_chk_func_t(char*, const char*, size_t); |
| 132 | #define __STRCAT_CHK_SHIM() \ |
| 133 | DEFINE_STATIC_SHIM(char* __strcat_chk(char* dst, const char* src, size_t dst_buf_size) { \ |
| 134 | FORWARD(__strcat_chk)(dst, src, dst_buf_size); \ |
| 135 | }) |
| 136 | |
| 137 | typedef char* strchr_func_t(const char*, int); |
| 138 | #define STRCHR_SHIM() \ |
| 139 | DEFINE_STATIC_SHIM(char* strchr(const char* src, int ch) { FORWARD(strchr)(src, ch); }) |
| 140 | |
| 141 | typedef char* strchrnul_func_t(const char*, int); |
| 142 | #define STRCHRNUL_SHIM() \ |
| 143 | DEFINE_STATIC_SHIM(char* strchrnul(const char* src, int ch) { FORWARD(strchrnul)(src, ch); }) |
| 144 | |
| 145 | typedef int strcmp_func_t(const char*, const char*); |
| 146 | #define STRCMP_SHIM() \ |
| 147 | DEFINE_STATIC_SHIM(int strcmp(char* lhs, const char* rhs) { FORWARD(strcmp)(lhs, rhs); }) |
| 148 | |
| 149 | typedef char* strcpy_func_t(char*, const char*); |
| 150 | #define STRCPY_SHIM() \ |
| 151 | DEFINE_STATIC_SHIM(char* strcpy(char* dst, const char* src) { FORWARD(strcpy)(dst, src); }) |
| 152 | |
| 153 | typedef char* __strcpy_chk_func_t(char*, const char*, size_t); |
| 154 | #define __STRCPY_CHK_SHIM() \ |
| 155 | DEFINE_STATIC_SHIM(char* __strcpy_chk(char* dst, const char* src, size_t dst_len) { \ |
| 156 | FORWARD(__strcpy_chk)(dst, src, dst_len); \ |
| 157 | }) |
| 158 | |
| 159 | typedef size_t strlen_func_t(const char*); |
| 160 | #define STRLEN_SHIM() DEFINE_STATIC_SHIM(size_t strlen(const char* s) { FORWARD(strlen)(s); }) |
| 161 | |
| 162 | typedef char* strncat_func_t(char*, const char*, size_t); |
| 163 | #define STRNCAT_SHIM() \ |
| 164 | DEFINE_STATIC_SHIM(char* strncat(char* dst, const char* src, size_t n) { \ |
| 165 | FORWARD(strncat)(dst, src, n); \ |
| 166 | }) |
| 167 | |
| 168 | typedef int strncmp_func_t(const char*, const char*, size_t); |
| 169 | #define STRNCMP_SHIM() \ |
| 170 | DEFINE_STATIC_SHIM(int strncmp(const char* lhs, const char* rhs, size_t n) { \ |
| 171 | FORWARD(strncmp)(lhs, rhs, n); \ |
| 172 | }) |
| 173 | |
| 174 | typedef char* strncpy_func_t(char*, const char*, size_t); |
| 175 | #define STRNCPY_SHIM() \ |
| 176 | DEFINE_STATIC_SHIM(char* strncpy(char* dst, const char* src, size_t n) { \ |
| 177 | FORWARD(strncpy)(dst, src, n); \ |
| 178 | }) |
| 179 | |
| 180 | typedef size_t strnlen_func_t(const char*, size_t); |
| 181 | #define STRNLEN_SHIM() \ |
| 182 | DEFINE_STATIC_SHIM(size_t strnlen(const char* s, size_t n) { FORWARD(strnlen)(s, n); }) |
| 183 | |
| 184 | typedef char* strrchr_func_t(const char*, int); |
| 185 | #define STRRCHR_SHIM() \ |
| 186 | DEFINE_STATIC_SHIM(char* strrchr(const char* src, int ch) { FORWARD(strrchr)(src, ch); }) |