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 <private/bionic_ifuncs.h>
30 #include <stddef.h>
31 #include <sys/auxv.h>
32 
33 extern "C" {
34 
35 typedef void* memchr_func(const void*, int, size_t);
DEFINE_IFUNC_FOR(memchr)36 DEFINE_IFUNC_FOR(memchr) {
37     if (arg->_hwcap2 & HWCAP2_MTE) {
38         RETURN_FUNC(memchr_func, __memchr_aarch64_mte);
39     } else {
40         RETURN_FUNC(memchr_func, __memchr_aarch64);
41     }
42 }
43 
44 typedef int stpcpy_func(char*, const char*);
DEFINE_IFUNC_FOR(stpcpy)45 DEFINE_IFUNC_FOR(stpcpy) {
46     if (arg->_hwcap2 & HWCAP2_MTE) {
47         RETURN_FUNC(stpcpy_func, __stpcpy_aarch64_mte);
48     } else {
49         RETURN_FUNC(stpcpy_func, __stpcpy_aarch64);
50     }
51 }
52 
53 typedef char* strchr_func(const char*, int);
DEFINE_IFUNC_FOR(strchr)54 DEFINE_IFUNC_FOR(strchr) {
55     if (arg->_hwcap2 & HWCAP2_MTE) {
56         RETURN_FUNC(strchr_func, __strchr_aarch64_mte);
57     } else {
58         RETURN_FUNC(strchr_func, __strchr_aarch64);
59     }
60 }
61 
62 typedef char* strchrnul_func(const char*, int);
DEFINE_IFUNC_FOR(strchrnul)63 DEFINE_IFUNC_FOR(strchrnul) {
64     if (arg->_hwcap2 & HWCAP2_MTE) {
65         RETURN_FUNC(strchrnul_func, __strchrnul_aarch64_mte);
66     } else {
67         RETURN_FUNC(strchrnul_func, __strchrnul_aarch64);
68     }
69 }
70 
71 typedef int strcmp_func(const char*, const char*);
DEFINE_IFUNC_FOR(strcmp)72 DEFINE_IFUNC_FOR(strcmp) {
73     if (arg->_hwcap2 & HWCAP2_MTE) {
74         RETURN_FUNC(strcmp_func, __strcmp_aarch64_mte);
75     } else {
76         RETURN_FUNC(strcmp_func, __strcmp_aarch64);
77     }
78 }
79 
80 typedef int strcpy_func(char*, const char*);
DEFINE_IFUNC_FOR(strcpy)81 DEFINE_IFUNC_FOR(strcpy) {
82     if (arg->_hwcap2 & HWCAP2_MTE) {
83         RETURN_FUNC(strcpy_func, __strcpy_aarch64_mte);
84     } else {
85         RETURN_FUNC(strcpy_func, __strcpy_aarch64);
86     }
87 }
88 
89 typedef size_t strlen_func(const char*);
DEFINE_IFUNC_FOR(strlen)90 DEFINE_IFUNC_FOR(strlen) {
91     if (arg->_hwcap2 & HWCAP2_MTE) {
92         RETURN_FUNC(strlen_func, __strlen_aarch64_mte);
93     } else {
94         RETURN_FUNC(strlen_func, __strlen_aarch64);
95     }
96 }
97 
98 typedef int strncmp_func(const char*, const char*, int);
DEFINE_IFUNC_FOR(strncmp)99 DEFINE_IFUNC_FOR(strncmp) {
100     if (arg->_hwcap2 & HWCAP2_MTE) {
101         RETURN_FUNC(strncmp_func, __strncmp_aarch64_mte);
102     } else {
103         RETURN_FUNC(strncmp_func, __strncmp_aarch64);
104     }
105 }
106 
107 typedef char* strrchr_func(const char*, int);
DEFINE_IFUNC_FOR(strrchr)108 DEFINE_IFUNC_FOR(strrchr) {
109     if (arg->_hwcap2 & HWCAP2_MTE) {
110         RETURN_FUNC(strrchr_func, __strrchr_aarch64_mte);
111     } else {
112         RETURN_FUNC(strrchr_func, __strrchr_aarch64);
113     }
114 }
115 
116 }  // extern "C"
117