blob: ce6c028660e7472915932bdcc307edc38099608a [file] [log] [blame]
Elliott Hughesaefe9992023-11-08 12:04:41 -08001/*
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 Hughesadc41712024-08-16 13:08:11 +000030#include <private/bionic_ifuncs.h>
Elliott Hughesaefe9992023-11-08 12:04:41 -080031#include <stddef.h>
32#include <sys/syscall.h>
33#include <unistd.h>
34
Elliott Hughesaefe9992023-11-08 12:04:41 -080035extern "C" {
36
Nick Desaulniersc574f792024-04-17 09:49:59 -070037static inline __always_inline int ifunc_faccessat(int dir_fd, const char* path, int mode) {
Elliott Hughesaefe9992023-11-08 12:04:41 -080038 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
46static 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 Gong066f4b72023-11-23 11:11:22 +080050 return ifunc_faccessat(AT_FDCWD, "/dev/hvc0", F_OK) != 0;
Elliott Hughesaefe9992023-11-08 12:04:41 -080051 }();
52 return result;
53}
54
Elliott Hughesaefe9992023-11-08 12:04:41 -080055DEFINE_IFUNC_FOR(memchr) {
Elliott Hughesadc41712024-08-16 13:08:11 +000056 if (have_fast_v()) RETURN_FUNC(memchr_func_t, memchr_v);
57 RETURN_FUNC(memchr_func_t, memchr_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080058}
Elliott Hughesadc41712024-08-16 13:08:11 +000059MEMCHR_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080060
Elliott Hughesaefe9992023-11-08 12:04:41 -080061DEFINE_IFUNC_FOR(memcmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +000062 if (have_fast_v()) RETURN_FUNC(memcmp_func_t, memcmp_v);
63 RETURN_FUNC(memcmp_func_t, memcmp_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080064}
Elliott Hughesadc41712024-08-16 13:08:11 +000065MEMCMP_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080066
Elliott Hughesaefe9992023-11-08 12:04:41 -080067DEFINE_IFUNC_FOR(memcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +000068 if (have_fast_v()) RETURN_FUNC(memcpy_func_t, memcpy_v);
69 RETURN_FUNC(memcpy_func_t, memcpy_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080070}
Elliott Hughesadc41712024-08-16 13:08:11 +000071MEMCPY_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080072
Elliott Hughesaefe9992023-11-08 12:04:41 -080073DEFINE_IFUNC_FOR(memmove) {
Elliott Hughesadc41712024-08-16 13:08:11 +000074 if (have_fast_v()) RETURN_FUNC(memmove_func_t, memmove_v);
75 RETURN_FUNC(memmove_func_t, memmove_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080076}
Elliott Hughesadc41712024-08-16 13:08:11 +000077MEMMOVE_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080078
Elliott Hughesaefe9992023-11-08 12:04:41 -080079DEFINE_IFUNC_FOR(memset) {
Elliott Hughesadc41712024-08-16 13:08:11 +000080 if (have_fast_v()) RETURN_FUNC(memset_func_t, memset_v);
81 RETURN_FUNC(memset_func_t, memset_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080082}
Elliott Hughesadc41712024-08-16 13:08:11 +000083MEMSET_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080084
Elliott Hughesaefe9992023-11-08 12:04:41 -080085DEFINE_IFUNC_FOR(stpcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +000086 if (have_fast_v()) RETURN_FUNC(stpcpy_func_t, stpcpy_v);
87 RETURN_FUNC(stpcpy_func_t, stpcpy_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080088}
Elliott Hughesadc41712024-08-16 13:08:11 +000089STPCPY_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080090
Elliott Hughesaefe9992023-11-08 12:04:41 -080091DEFINE_IFUNC_FOR(strcat) {
Elliott Hughesadc41712024-08-16 13:08:11 +000092 if (have_fast_v()) RETURN_FUNC(strcat_func_t, strcat_v);
93 RETURN_FUNC(strcat_func_t, strcat_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -080094}
Elliott Hughesadc41712024-08-16 13:08:11 +000095STRCAT_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -080096
Elliott Hughesaefe9992023-11-08 12:04:41 -080097DEFINE_IFUNC_FOR(strchr) {
Elliott Hughesadc41712024-08-16 13:08:11 +000098 if (have_fast_v()) RETURN_FUNC(strchr_func_t, strchr_v);
99 RETURN_FUNC(strchr_func_t, strchr_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800100}
Elliott Hughesadc41712024-08-16 13:08:11 +0000101STRCHR_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800102
Elliott Hughesaefe9992023-11-08 12:04:41 -0800103DEFINE_IFUNC_FOR(strcmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000104 if (have_fast_v()) RETURN_FUNC(strcmp_func_t, strcmp_v);
105 RETURN_FUNC(strcmp_func_t, strcmp_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800106}
Elliott Hughesadc41712024-08-16 13:08:11 +0000107STRCMP_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800108
Elliott Hughesaefe9992023-11-08 12:04:41 -0800109DEFINE_IFUNC_FOR(strcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000110 if (have_fast_v()) RETURN_FUNC(strcpy_func_t, strcpy_v);
111 RETURN_FUNC(strcpy_func_t, strcpy_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800112}
Elliott Hughesadc41712024-08-16 13:08:11 +0000113STRCPY_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800114
Elliott Hughesaefe9992023-11-08 12:04:41 -0800115DEFINE_IFUNC_FOR(strlen) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000116 if (have_fast_v()) RETURN_FUNC(strlen_func_t, strlen_v);
117 RETURN_FUNC(strlen_func_t, strlen_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800118}
Elliott Hughesadc41712024-08-16 13:08:11 +0000119STRLEN_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800120
Elliott Hughesaefe9992023-11-08 12:04:41 -0800121DEFINE_IFUNC_FOR(strncat) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000122 if (have_fast_v()) RETURN_FUNC(strncat_func_t, strncat_v);
123 RETURN_FUNC(strncat_func_t, strncat_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800124}
Elliott Hughesadc41712024-08-16 13:08:11 +0000125STRNCAT_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800126
Elliott Hughesaefe9992023-11-08 12:04:41 -0800127DEFINE_IFUNC_FOR(strncmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000128 if (have_fast_v()) RETURN_FUNC(strncmp_func_t, strncmp_v);
129 RETURN_FUNC(strncmp_func_t, strncmp_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800130}
Elliott Hughesadc41712024-08-16 13:08:11 +0000131STRNCMP_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800132
Elliott Hughesaefe9992023-11-08 12:04:41 -0800133DEFINE_IFUNC_FOR(strncpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000134 if (have_fast_v()) RETURN_FUNC(strncpy_func_t, strncpy_v);
135 RETURN_FUNC(strncpy_func_t, strncpy_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800136}
Elliott Hughesadc41712024-08-16 13:08:11 +0000137STRNCPY_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800138
Elliott Hughesaefe9992023-11-08 12:04:41 -0800139DEFINE_IFUNC_FOR(strnlen) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000140 if (have_fast_v()) RETURN_FUNC(strnlen_func_t, strnlen_v);
141 RETURN_FUNC(strnlen_func_t, strnlen_gc);
Elliott Hughesaefe9992023-11-08 12:04:41 -0800142}
Elliott Hughesadc41712024-08-16 13:08:11 +0000143STRNLEN_SHIM()
Elliott Hughesaefe9992023-11-08 12:04:41 -0800144
145} // extern "C"