blob: 5c6d4edef0930416f89b101373ff6431e8638428 [file] [log] [blame]
Peter Collingbourne900d07d2019-10-28 13:11:00 -07001/*
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
34extern "C" {
35
36static 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
45typedef void* memchr_func(const void*, int, size_t);
46DEFINE_IFUNC_FOR(memchr) {
47 if (supports_mte(arg->_hwcap2)) {
48 RETURN_FUNC(memchr_func, memchr_mte);
49 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -080050 RETURN_FUNC(memchr_func, __memchr_aarch64);
51 }
52}
53
54typedef int stpcpy_func(char*, const char*);
55DEFINE_IFUNC_FOR(stpcpy) {
56 if (supports_mte(arg->_hwcap2)) {
57 RETURN_FUNC(stpcpy_func, stpcpy_mte);
58 } else {
59 RETURN_FUNC(stpcpy_func, __stpcpy_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -070060 }
61}
62
63typedef char* strchr_func(const char*, int);
64DEFINE_IFUNC_FOR(strchr) {
65 if (supports_mte(arg->_hwcap2)) {
Peter Collingbourne337a5b32020-02-21 12:11:02 -080066 RETURN_FUNC(strchr_func, __strchr_aarch64_mte);
Peter Collingbourne900d07d2019-10-28 13:11:00 -070067 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -080068 RETURN_FUNC(strchr_func, __strchr_aarch64);
69 }
70}
71
72typedef char* strchrnul_func(const char*, int);
73DEFINE_IFUNC_FOR(strchrnul) {
74 if (supports_mte(arg->_hwcap2)) {
75 RETURN_FUNC(strchrnul_func, strchrnul_mte);
76 } else {
77 RETURN_FUNC(strchrnul_func, __strchrnul_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -070078 }
79}
80
81typedef int strcmp_func(const char*, const char*);
82DEFINE_IFUNC_FOR(strcmp) {
83 if (supports_mte(arg->_hwcap2)) {
84 RETURN_FUNC(strcmp_func, strcmp_mte);
85 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -080086 RETURN_FUNC(strcmp_func, __strcmp_aarch64);
87 }
88}
89
90typedef int strcpy_func(char*, const char*);
91DEFINE_IFUNC_FOR(strcpy) {
92 if (supports_mte(arg->_hwcap2)) {
93 RETURN_FUNC(strcpy_func, strcpy_mte);
94 } else {
95 RETURN_FUNC(strcpy_func, __strcpy_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -070096 }
97}
98
99typedef size_t strlen_func(const char*);
100DEFINE_IFUNC_FOR(strlen) {
101 if (supports_mte(arg->_hwcap2)) {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800102 RETURN_FUNC(strlen_func, __strlen_aarch64_mte);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700103 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800104 RETURN_FUNC(strlen_func, __strlen_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700105 }
106}
107
108typedef int strncmp_func(const char*, const char*, int);
109DEFINE_IFUNC_FOR(strncmp) {
110 if (supports_mte(arg->_hwcap2)) {
111 RETURN_FUNC(strncmp_func, strncmp_mte);
112 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800113 RETURN_FUNC(strncmp_func, __strncmp_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700114 }
115}
116
117typedef size_t strnlen_func(const char*, int);
118DEFINE_IFUNC_FOR(strnlen) {
119 if (supports_mte(arg->_hwcap2)) {
120 RETURN_FUNC(strnlen_func, strnlen_mte);
121 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800122 RETURN_FUNC(strnlen_func, __strnlen_aarch64);
123 }
124}
125
126typedef char* strrchr_func(const char*, int);
127DEFINE_IFUNC_FOR(strrchr) {
128 if (supports_mte(arg->_hwcap2)) {
129 RETURN_FUNC(strrchr_func, strrchr_mte);
130 } else {
131 RETURN_FUNC(strrchr_func, __strrchr_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700132 }
133}
134
135} // extern "C"