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_aarch64_mte);
49     } else {
50         RETURN_FUNC(memchr_func, __memchr_aarch64);
51     }
52 }
53 
54 typedef int stpcpy_func(char*, const char*);
DEFINE_IFUNC_FOR(stpcpy)55 DEFINE_IFUNC_FOR(stpcpy) {
56     if (supports_mte(arg->_hwcap2)) {
57         RETURN_FUNC(stpcpy_func, __stpcpy_aarch64_mte);
58     } else {
59         RETURN_FUNC(stpcpy_func, __stpcpy_aarch64);
60     }
61 }
62 
63 typedef char* strchr_func(const char*, int);
DEFINE_IFUNC_FOR(strchr)64 DEFINE_IFUNC_FOR(strchr) {
65     if (supports_mte(arg->_hwcap2)) {
66         RETURN_FUNC(strchr_func, __strchr_aarch64_mte);
67     } else {
68         RETURN_FUNC(strchr_func, __strchr_aarch64);
69     }
70 }
71 
72 typedef char* strchrnul_func(const char*, int);
DEFINE_IFUNC_FOR(strchrnul)73 DEFINE_IFUNC_FOR(strchrnul) {
74     if (supports_mte(arg->_hwcap2)) {
75         RETURN_FUNC(strchrnul_func, __strchrnul_aarch64_mte);
76     } else {
77         RETURN_FUNC(strchrnul_func, __strchrnul_aarch64);
78     }
79 }
80 
81 typedef int strcmp_func(const char*, const char*);
DEFINE_IFUNC_FOR(strcmp)82 DEFINE_IFUNC_FOR(strcmp) {
83     if (supports_mte(arg->_hwcap2)) {
84         RETURN_FUNC(strcmp_func, __strcmp_aarch64_mte);
85     } else {
86         RETURN_FUNC(strcmp_func, __strcmp_aarch64);
87     }
88 }
89 
90 typedef int strcpy_func(char*, const char*);
DEFINE_IFUNC_FOR(strcpy)91 DEFINE_IFUNC_FOR(strcpy) {
92     if (supports_mte(arg->_hwcap2)) {
93         RETURN_FUNC(strcpy_func, __strcpy_aarch64_mte);
94     } else {
95         RETURN_FUNC(strcpy_func, __strcpy_aarch64);
96     }
97 }
98 
99 typedef size_t strlen_func(const char*);
DEFINE_IFUNC_FOR(strlen)100 DEFINE_IFUNC_FOR(strlen) {
101     if (supports_mte(arg->_hwcap2)) {
102         RETURN_FUNC(strlen_func, __strlen_aarch64_mte);
103     } else {
104         RETURN_FUNC(strlen_func, __strlen_aarch64);
105     }
106 }
107 
108 typedef int strncmp_func(const char*, const char*, int);
DEFINE_IFUNC_FOR(strncmp)109 DEFINE_IFUNC_FOR(strncmp) {
110     if (supports_mte(arg->_hwcap2)) {
111         RETURN_FUNC(strncmp_func, __strncmp_aarch64_mte);
112     } else {
113         RETURN_FUNC(strncmp_func, __strncmp_aarch64);
114     }
115 }
116 
117 typedef char* strrchr_func(const char*, int);
DEFINE_IFUNC_FOR(strrchr)118 DEFINE_IFUNC_FOR(strrchr) {
119     if (supports_mte(arg->_hwcap2)) {
120         RETURN_FUNC(strrchr_func, __strrchr_aarch64_mte);
121     } else {
122         RETURN_FUNC(strrchr_func, __strrchr_aarch64);
123     }
124 }
125 
126 }  // extern "C"
127