Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #include <fcntl.h> |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 30 | #include <private/bionic_ifuncs.h> |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 31 | #include <stddef.h> |
| 32 | #include <sys/syscall.h> |
| 33 | #include <unistd.h> |
| 34 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 35 | extern "C" { |
| 36 | |
Nick Desaulniers | c574f79 | 2024-04-17 09:49:59 -0700 | [diff] [blame] | 37 | static inline __always_inline int ifunc_faccessat(int dir_fd, const char* path, int mode) { |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 38 | register long a0 __asm__("a0") = dir_fd; |
| 39 | register long a1 __asm__("a1") = reinterpret_cast<long>(path); |
| 40 | register long a2 __asm__("a2") = mode; |
| 41 | register long a7 __asm__("a7") = __NR_faccessat; |
| 42 | __asm__("ecall" : "=r"(a0) : "r"(a0), "r"(a1), "r"(a2), "r"(a7) : "memory"); |
| 43 | return a0; |
| 44 | } |
| 45 | |
| 46 | static bool have_fast_v() { |
| 47 | static bool result = []() { |
| 48 | // We don't want to do a full "bogomips" test, so just check for the |
| 49 | // presence of a file that would indicate that we're running in qemu. |
Kenny Gong | 066f4b7 | 2023-11-23 11:11:22 +0800 | [diff] [blame] | 50 | return ifunc_faccessat(AT_FDCWD, "/dev/hvc0", F_OK) != 0; |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 51 | }(); |
| 52 | return result; |
| 53 | } |
| 54 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 55 | DEFINE_IFUNC_FOR(memchr) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 56 | if (have_fast_v()) RETURN_FUNC(memchr_func_t, memchr_v); |
| 57 | RETURN_FUNC(memchr_func_t, memchr_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 58 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 59 | MEMCHR_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 61 | DEFINE_IFUNC_FOR(memcmp) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 62 | if (have_fast_v()) RETURN_FUNC(memcmp_func_t, memcmp_v); |
| 63 | RETURN_FUNC(memcmp_func_t, memcmp_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 64 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 65 | MEMCMP_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 66 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 67 | DEFINE_IFUNC_FOR(memcpy) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 68 | if (have_fast_v()) RETURN_FUNC(memcpy_func_t, memcpy_v); |
| 69 | RETURN_FUNC(memcpy_func_t, memcpy_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 70 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 71 | MEMCPY_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 73 | DEFINE_IFUNC_FOR(memmove) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 74 | if (have_fast_v()) RETURN_FUNC(memmove_func_t, memmove_v); |
| 75 | RETURN_FUNC(memmove_func_t, memmove_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 76 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 77 | MEMMOVE_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 79 | DEFINE_IFUNC_FOR(memset) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 80 | if (have_fast_v()) RETURN_FUNC(memset_func_t, memset_v); |
| 81 | RETURN_FUNC(memset_func_t, memset_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 82 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 83 | MEMSET_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 84 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 85 | DEFINE_IFUNC_FOR(stpcpy) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 86 | if (have_fast_v()) RETURN_FUNC(stpcpy_func_t, stpcpy_v); |
| 87 | RETURN_FUNC(stpcpy_func_t, stpcpy_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 88 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 89 | STPCPY_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 90 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 91 | DEFINE_IFUNC_FOR(strcat) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 92 | if (have_fast_v()) RETURN_FUNC(strcat_func_t, strcat_v); |
| 93 | RETURN_FUNC(strcat_func_t, strcat_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 94 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 95 | STRCAT_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 96 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 97 | DEFINE_IFUNC_FOR(strchr) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 98 | if (have_fast_v()) RETURN_FUNC(strchr_func_t, strchr_v); |
| 99 | RETURN_FUNC(strchr_func_t, strchr_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 100 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 101 | STRCHR_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 102 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 103 | DEFINE_IFUNC_FOR(strcmp) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 104 | if (have_fast_v()) RETURN_FUNC(strcmp_func_t, strcmp_v); |
| 105 | RETURN_FUNC(strcmp_func_t, strcmp_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 106 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 107 | STRCMP_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 108 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 109 | DEFINE_IFUNC_FOR(strcpy) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 110 | if (have_fast_v()) RETURN_FUNC(strcpy_func_t, strcpy_v); |
| 111 | RETURN_FUNC(strcpy_func_t, strcpy_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 112 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 113 | STRCPY_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 114 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 115 | DEFINE_IFUNC_FOR(strlen) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 116 | if (have_fast_v()) RETURN_FUNC(strlen_func_t, strlen_v); |
| 117 | RETURN_FUNC(strlen_func_t, strlen_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 118 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 119 | STRLEN_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 120 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 121 | DEFINE_IFUNC_FOR(strncat) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 122 | if (have_fast_v()) RETURN_FUNC(strncat_func_t, strncat_v); |
| 123 | RETURN_FUNC(strncat_func_t, strncat_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 124 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 125 | STRNCAT_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 126 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 127 | DEFINE_IFUNC_FOR(strncmp) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 128 | if (have_fast_v()) RETURN_FUNC(strncmp_func_t, strncmp_v); |
| 129 | RETURN_FUNC(strncmp_func_t, strncmp_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 130 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 131 | STRNCMP_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 132 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 133 | DEFINE_IFUNC_FOR(strncpy) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 134 | if (have_fast_v()) RETURN_FUNC(strncpy_func_t, strncpy_v); |
| 135 | RETURN_FUNC(strncpy_func_t, strncpy_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 136 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 137 | STRNCPY_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 139 | DEFINE_IFUNC_FOR(strnlen) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 140 | if (have_fast_v()) RETURN_FUNC(strnlen_func_t, strnlen_v); |
| 141 | RETURN_FUNC(strnlen_func_t, strnlen_gc); |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 142 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 143 | STRNLEN_SHIM() |
Elliott Hughes | aefe999 | 2023-11-08 12:04:41 -0800 | [diff] [blame] | 144 | |
| 145 | } // extern "C" |