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 #include <platform/bionic/mte_kernel.h>
30 #include <private/bionic_ifuncs.h>
31 #include <stddef.h>
32 #include <sys/auxv.h>
33
34 extern "C" {
35
supports_mte(unsigned long hwcap2)36 static bool supports_mte(unsigned long hwcap2) {
37 #ifdef ANDROID_EXPERIMENTAL_MTE
38 return hwcap2 & HWCAP2_MTE;
39 #else
40 (void)hwcap2;
41 return false;
42 #endif
43 }
44
45 typedef void* memchr_func(const void*, int, size_t);
DEFINE_IFUNC_FOR(memchr)46 DEFINE_IFUNC_FOR(memchr) {
47 if (supports_mte(arg->_hwcap2)) {
48 RETURN_FUNC(memchr_func, memchr_mte);
49 } else {
50 RETURN_FUNC(memchr_func, memchr_default);
51 }
52 }
53
54 typedef char* strchr_func(const char*, int);
DEFINE_IFUNC_FOR(strchr)55 DEFINE_IFUNC_FOR(strchr) {
56 if (supports_mte(arg->_hwcap2)) {
57 RETURN_FUNC(strchr_func, strchr_mte);
58 } else {
59 RETURN_FUNC(strchr_func, strchr_default);
60 }
61 }
62
63 typedef int strcmp_func(const char*, const char*);
DEFINE_IFUNC_FOR(strcmp)64 DEFINE_IFUNC_FOR(strcmp) {
65 if (supports_mte(arg->_hwcap2)) {
66 RETURN_FUNC(strcmp_func, strcmp_mte);
67 } else {
68 RETURN_FUNC(strcmp_func, strcmp_default);
69 }
70 }
71
72 typedef size_t strlen_func(const char*);
DEFINE_IFUNC_FOR(strlen)73 DEFINE_IFUNC_FOR(strlen) {
74 if (supports_mte(arg->_hwcap2)) {
75 RETURN_FUNC(strlen_func, strlen_mte);
76 } else {
77 RETURN_FUNC(strlen_func, strlen_default);
78 }
79 }
80
81 typedef int strncmp_func(const char*, const char*, int);
DEFINE_IFUNC_FOR(strncmp)82 DEFINE_IFUNC_FOR(strncmp) {
83 if (supports_mte(arg->_hwcap2)) {
84 RETURN_FUNC(strncmp_func, strncmp_mte);
85 } else {
86 RETURN_FUNC(strncmp_func, strncmp_default);
87 }
88 }
89
90 typedef size_t strnlen_func(const char*, int);
DEFINE_IFUNC_FOR(strnlen)91 DEFINE_IFUNC_FOR(strnlen) {
92 if (supports_mte(arg->_hwcap2)) {
93 RETURN_FUNC(strnlen_func, strnlen_mte);
94 } else {
95 RETURN_FUNC(strnlen_func, strnlen_default);
96 }
97 }
98
99 } // extern "C"
100