]> git.saurik.com Git - apple/xnu.git/blame - san/kasan-memintrinsics.c
xnu-4570.31.3.tar.gz
[apple/xnu.git] / san / kasan-memintrinsics.c
CommitLineData
5ba3f43e
A
1/*
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <string.h>
30#include <mach/boolean.h>
31
32#include <mach/boolean.h>
33#include <machine/limits.h>
34#include <kern/debug.h>
35
36#include <kasan_internal.h>
37#include <memintrinsics.h>
38
39#if MEMINTRINSICS
40static bool check_intrinsics = true;
41#else
42static bool check_intrinsics = false;
43#endif
44
45void
46__asan_bcopy(const void *src, void *dst, size_t sz)
47{
48 if (check_intrinsics) {
49 kasan_check_range(src, sz, TYPE_MEMLD);
50 kasan_check_range(dst, sz, TYPE_MEMSTR);
51 }
52 __nosan_bcopy(src, dst, sz);
53}
54
55void *
56__asan_memmove(void *src, const void *dst, size_t sz)
57{
58 if (check_intrinsics) {
59 kasan_check_range(src, sz, TYPE_MEMLD);
60 kasan_check_range(dst, sz, TYPE_MEMSTR);
61 }
62 return __nosan_memmove(src, dst, sz);
63}
64
65void *
66__asan_memcpy(void *dst, const void *src, size_t sz)
67{
68 if (check_intrinsics) {
69 kasan_check_range(src, sz, TYPE_MEMLD);
70 kasan_check_range(dst, sz, TYPE_MEMSTR);
71 }
72 return __nosan_memcpy(dst, src, sz);
73}
74
75void *
76__asan_memset(void *dst, int c, size_t sz)
77{
78 if (check_intrinsics) {
79 kasan_check_range(dst, sz, TYPE_MEMSTR);
80 }
81 return __nosan_memset(dst, c, sz);
82}
83
84void
85__asan_bzero(void *dst, size_t sz)
86{
87 if (check_intrinsics) {
88 kasan_check_range(dst, sz, TYPE_MEMSTR);
89 }
90 __nosan_bzero(dst, sz);
91}
92
93int
94__asan_bcmp(const void *a, const void *b, size_t len)
95{
96 if (check_intrinsics) {
97 kasan_check_range(a, len, TYPE_MEMLD);
98 kasan_check_range(b, len, TYPE_MEMLD);
99 }
100 return __nosan_bcmp(a, b, len);
101}
102
103int
104__asan_memcmp(const void *a, const void *b, size_t n)
105{
106 if (check_intrinsics) {
107 kasan_check_range(a, n, TYPE_MEMLD);
108 kasan_check_range(b, n, TYPE_MEMLD);
109 }
110 return __nosan_memcmp(a, b, n);
111}
112
113size_t
114__asan_strlcpy(char *dst, const char *src, size_t sz)
115{
116 if (check_intrinsics) {
117 kasan_check_range(dst, sz, TYPE_STRINGSTR);
118 }
119 return __nosan_strlcpy(dst, src, sz);
120}
121
122size_t
123__asan_strlcat(char *dst, const char *src, size_t sz)
124{
125 if (check_intrinsics) {
126 kasan_check_range(dst, sz, TYPE_STRINGSTR);
127 }
128 return __nosan_strlcat(dst, src, sz);
129}
130
131char *
132__asan_strncpy(char *dst, const char *src, size_t sz)
133{
134 if (check_intrinsics) {
135 kasan_check_range(dst, sz, TYPE_STRINGSTR);
136 }
137 return __nosan_strncpy(dst, src, sz);
138}
139
140char *
141__asan_strncat(char *dst, const char *src, size_t sz)
142{
143 if (check_intrinsics) {
144 kasan_check_range(dst, strlen(dst) + sz + 1, TYPE_STRINGSTR);
145 }
146 return __nosan_strncat(dst, src, sz);
147}
148
149size_t
150__asan_strnlen(const char *src, size_t sz)
151{
152 if (check_intrinsics) {
153 kasan_check_range(src, sz, TYPE_STRINGLD);
154 }
155
156 return __nosan_strnlen(src, sz);
157}
158
159size_t
160__asan_strlen(const char *src)
161{
162 size_t sz = __nosan_strlen(src);
163 if (check_intrinsics) {
164 kasan_check_range(src, sz + 1, TYPE_STRINGLD);
165 }
166 return sz;
167}