blob: bb2ba5101df7674aade3f17c5d945213e921a886 [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>
30#include <stddef.h>
31#include <sys/syscall.h>
32#include <unistd.h>
33
34#include <private/bionic_ifuncs.h>
35
36extern "C" {
37
Nick Desaulniersc574f792024-04-17 09:49:59 -070038static inline __always_inline int ifunc_faccessat(int dir_fd, const char* path, int mode) {
Elliott Hughesaefe9992023-11-08 12:04:41 -080039 register long a0 __asm__("a0") = dir_fd;
40 register long a1 __asm__("a1") = reinterpret_cast<long>(path);
41 register long a2 __asm__("a2") = mode;
42 register long a7 __asm__("a7") = __NR_faccessat;
43 __asm__("ecall" : "=r"(a0) : "r"(a0), "r"(a1), "r"(a2), "r"(a7) : "memory");
44 return a0;
45}
46
47static bool have_fast_v() {
48 static bool result = []() {
49 // We don't want to do a full "bogomips" test, so just check for the
50 // presence of a file that would indicate that we're running in qemu.
Kenny Gong066f4b72023-11-23 11:11:22 +080051 return ifunc_faccessat(AT_FDCWD, "/dev/hvc0", F_OK) != 0;
Elliott Hughesaefe9992023-11-08 12:04:41 -080052 }();
53 return result;
54}
55
56typedef void* memchr_func(const void*, int, size_t);
57DEFINE_IFUNC_FOR(memchr) {
58 if (have_fast_v()) RETURN_FUNC(memchr_func, memchr_v);
59 RETURN_FUNC(memchr_func, memchr_gc);
60}
61
62typedef int memcmp_func(const void*, const void*, size_t);
63DEFINE_IFUNC_FOR(memcmp) {
64 if (have_fast_v()) RETURN_FUNC(memcmp_func, memcmp_v);
65 RETURN_FUNC(memcmp_func, memcmp_gc);
66}
67
68typedef void* memcpy_func(void*, const void*, size_t);
69DEFINE_IFUNC_FOR(memcpy) {
70 if (have_fast_v()) RETURN_FUNC(memcpy_func, memcpy_v);
71 RETURN_FUNC(memcpy_func, memcpy_gc);
72}
73
74typedef void* memmove_func(void*, const void*, size_t);
75DEFINE_IFUNC_FOR(memmove) {
76 if (have_fast_v()) RETURN_FUNC(memmove_func, memmove_v);
77 RETURN_FUNC(memmove_func, memmove_gc);
78}
79
80typedef void* memset_func(void*, int, size_t);
81DEFINE_IFUNC_FOR(memset) {
82 if (have_fast_v()) RETURN_FUNC(memset_func, memset_v);
83 RETURN_FUNC(memset_func, memset_gc);
84}
85
86typedef char* stpcpy_func(char*, const char*);
87DEFINE_IFUNC_FOR(stpcpy) {
88 if (have_fast_v()) RETURN_FUNC(stpcpy_func, stpcpy_v);
89 RETURN_FUNC(stpcpy_func, stpcpy_gc);
90}
91
92typedef char* strcat_func(char*, const char*);
93DEFINE_IFUNC_FOR(strcat) {
94 if (have_fast_v()) RETURN_FUNC(strcat_func, strcat_v);
95 RETURN_FUNC(strcat_func, strcat_gc);
96}
97
98typedef char* strchr_func(const char*, int);
99DEFINE_IFUNC_FOR(strchr) {
100 if (have_fast_v()) RETURN_FUNC(strchr_func, strchr_v);
101 RETURN_FUNC(strchr_func, strchr_gc);
102}
103
104typedef int strcmp_func(const char*, const char*);
105DEFINE_IFUNC_FOR(strcmp) {
106 if (have_fast_v()) RETURN_FUNC(strcmp_func, strcmp_v);
107 RETURN_FUNC(strcmp_func, strcmp_gc);
108}
109
110typedef char* strcpy_func(char*, const char*);
111DEFINE_IFUNC_FOR(strcpy) {
112 if (have_fast_v()) RETURN_FUNC(strcpy_func, strcpy_v);
113 RETURN_FUNC(strcpy_func, strcpy_gc);
114}
115
116typedef size_t strlen_func(const char*);
117DEFINE_IFUNC_FOR(strlen) {
118 if (have_fast_v()) RETURN_FUNC(strlen_func, strlen_v);
119 RETURN_FUNC(strlen_func, strlen_gc);
120}
121
122typedef char* strncat_func(char*, const char*, size_t);
123DEFINE_IFUNC_FOR(strncat) {
124 if (have_fast_v()) RETURN_FUNC(strncat_func, strncat_v);
125 RETURN_FUNC(strncat_func, strncat_gc);
126}
127
128typedef int strncmp_func(const char*, const char*, size_t);
129DEFINE_IFUNC_FOR(strncmp) {
130 if (have_fast_v()) RETURN_FUNC(strncmp_func, strncmp_v);
131 RETURN_FUNC(strncmp_func, strncmp_gc);
132}
133
134typedef char* strncpy_func(char*, const char*, size_t);
135DEFINE_IFUNC_FOR(strncpy) {
136 if (have_fast_v()) RETURN_FUNC(strncpy_func, strncpy_v);
137 RETURN_FUNC(strncpy_func, strncpy_gc);
138}
139
140typedef size_t strnlen_func(const char*, size_t);
141DEFINE_IFUNC_FOR(strnlen) {
142 if (have_fast_v()) RETURN_FUNC(strnlen_func, strnlen_v);
143 RETURN_FUNC(strnlen_func, strnlen_gc);
144}
145
146} // extern "C"