[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / libmpx / mpxwrap / mpx_wrappers.c
blob58670aae4819e21027dfda5fee9c601ddb611af9
1 /* MPX Wrappers Library
2 Copyright (C) 2014 Free Software Foundation, Inc.
3 Contributed by Ilya Enkovich (ilya.enkovich@intel.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "stdlib.h"
27 #include "string.h"
28 #include <sys/mman.h>
30 void *
31 __mpx_wrapper_malloc (size_t size)
33 void *p = (void *)malloc (size);
34 if (!p) return __bnd_null_ptr_bounds (p);
35 return __bnd_set_ptr_bounds (p, size);
39 void *
40 __mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags,
41 int fd, off_t offset)
43 void *p = mmap (addr, length, prot, flags, fd, offset);
44 if (!p) return __bnd_null_ptr_bounds (p);
45 return __bnd_set_ptr_bounds (p, length);
48 void *
49 __mpx_wrapper_realloc (void *ptr, size_t n)
51 if (!ptr)
52 return __mpx_wrapper_malloc (n);
54 /* We don't kwnow how much data is copied by realloc
55 and therefore may check only lower bounds. */
56 __bnd_chk_ptr_lbounds (ptr);
57 ptr = realloc (ptr, n);
59 if (!ptr)
60 return __bnd_null_ptr_bounds (ptr);
62 return __bnd_set_ptr_bounds (ptr, n);
65 void *
66 __mpx_wrapper_calloc (size_t n_elements, size_t element_size)
68 void *p = calloc (n_elements, element_size);
69 if (!p)
70 return __bnd_null_ptr_bounds (p);
71 return __bnd_set_ptr_bounds (p, n_elements * element_size);
74 void *
75 __mpx_wrapper_memset (void *dstpp, int c, size_t len)
77 if (len > 0)
79 __bnd_chk_ptr_bounds (dstpp, len);
80 memset (dstpp, c, len);
82 return dstpp;
85 void
86 __mpx_wrapper_bzero (void *dst, size_t len)
88 __mpx_wrapper_memset (dst, 0, len);
91 void *
92 __mpx_wrapper_memmove (void *dst, const void *src, size_t n)
94 const char *s = (const char*)src;
95 char *d = (char*)dst;
96 void *ret = dst;
97 size_t offset_src = ((size_t) s) & (sizeof (void *) - 1);
98 size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1);
100 if (n == 0)
101 return ret;
103 __bnd_chk_ptr_bounds (dst, n);
104 __bnd_chk_ptr_bounds (src, n);
106 /* Different alignment means that even if
107 pointers exist in memory, we don't how
108 pointers are aligned and therefore cann't
109 copy bounds anyway. */
110 if (offset_src != offset_dst)
111 memmove (dst, src, n);
112 else
114 if (s < d)
116 d += n;
117 s += n;
118 offset_src = (offset_src + n) & (sizeof (void *) -1);
119 while (n-- && offset_src--)
120 *--d = *--s;
121 n++;
122 if (!n)
123 return ret;
124 void **d1 = (void **)d;
125 void **s1 = (void **)s;
126 /* This loop will also copy bounds. */
127 while (n >= sizeof (void *))
129 n -= sizeof (void *);
130 *--d1 = *--s1;
132 s = (char *)s1;
133 d = (char *)d1;
134 while (n--)
135 *--d = *--s;
137 else
139 offset_src = sizeof (void *) - offset_src;
140 while (n-- && offset_src--)
141 *d++ = *s++;
142 n++;
143 if (!n)
144 return ret;
145 void **d1 = (void **)d;
146 void **s1 = (void **)s;
147 /* This loop will also copy bounds. */
148 while (n >= sizeof (void *))
150 n -= sizeof (void *);
151 *d1++ = *s1++;
153 s = (char *)s1;
154 d = (char *)d1;
155 while (n--)
156 *d++ = *s++;
159 return ret;
163 void *
164 __mpx_wrapper_memcpy (void *dst, const void *src, size_t n)
166 return __mpx_wrapper_memmove (dst, src, n);
169 void *
170 __mpx_wrapper_mempcpy (void *dst, const void *src, size_t n)
172 return (char *)__mpx_wrapper_memcpy (dst, src, n) + n;
175 char *
176 __mpx_wrapper_strncat (char *dst, const char *src, size_t n)
178 size_t dst_size = strlen (dst);
179 size_t src_size = strnlen (src, n);
181 __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1);
182 if (src_size < n)
183 __bnd_chk_ptr_bounds (src, src_size + 1);
184 else
185 __bnd_chk_ptr_bounds (src, src_size);
187 strncat (dst, src, n);
189 return dst;
192 char *
193 __mpx_wrapper_strcat (char *dst, const char *src)
195 size_t dst_size = strlen (dst);
196 size_t src_size = strlen (src);
198 __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1);
199 __bnd_chk_ptr_bounds (src, src_size + 1);
201 strcat (dst, src);
203 return dst;
206 char *
207 __mpx_wrapper_stpcpy (char *dst, const char *src)
209 size_t src_size = strlen (src);
211 __bnd_chk_ptr_bounds (dst, src_size + 1);
212 __bnd_chk_ptr_bounds (src, src_size + 1);
214 memcpy (dst, src, src_size + 1);
216 return dst + src_size;
219 char *
220 __mpx_wrapper_stpncpy (char *dst, const char *src, size_t n)
222 size_t src_size = strnlen (src, n);
223 char *res;
225 __bnd_chk_ptr_bounds (dst, n);
226 if (src_size < n)
228 __bnd_chk_ptr_bounds (src, src_size + 1);
229 res = dst + src_size;
231 else
233 __bnd_chk_ptr_bounds (src, src_size);
234 res = dst + n;
237 memcpy (dst, src, src_size);
238 if (n > src_size)
239 memset (dst + src_size, 0, n - src_size);
241 return res;
244 char *
245 __mpx_wrapper_strcpy (char *dst, const char *src)
247 size_t src_size = strlen (src);
249 __bnd_chk_ptr_bounds (dst, src_size + 1);
250 __bnd_chk_ptr_bounds (src, src_size + 1);
252 memcpy (dst, src, src_size + 1);
254 return dst;
257 char *
258 __mpx_wrapper_strncpy (char *dst, const char *src, size_t n)
260 size_t src_size = strnlen (src, n);
262 __bnd_chk_ptr_bounds (dst, n);
263 if (src_size < n)
264 __bnd_chk_ptr_bounds (src, src_size + 1);
265 else
266 __bnd_chk_ptr_bounds (src, src_size);
268 memcpy (dst, src, src_size);
269 if (n > src_size)
270 memset (dst + src_size, 0, n - src_size);
272 return dst;
275 size_t
276 __mpx_wrapper_strlen (const char *s)
278 size_t length = strlen (s);
279 __bnd_chk_ptr_bounds (s, length + 1);
280 return length;