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
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
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/>. */
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
);
40 __mpx_wrapper_mmap (void *addr
, size_t length
, int prot
, int flags
,
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
);
49 __mpx_wrapper_realloc (void *ptr
, size_t n
)
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
);
60 return __bnd_null_ptr_bounds (ptr
);
62 return __bnd_set_ptr_bounds (ptr
, n
);
66 __mpx_wrapper_calloc (size_t n_elements
, size_t element_size
)
68 void *p
= calloc (n_elements
, element_size
);
70 return __bnd_null_ptr_bounds (p
);
71 return __bnd_set_ptr_bounds (p
, n_elements
* element_size
);
75 __mpx_wrapper_memset (void *dstpp
, int c
, size_t len
)
79 __bnd_chk_ptr_bounds (dstpp
, len
);
80 memset (dstpp
, c
, len
);
86 __mpx_wrapper_bzero (void *dst
, size_t len
)
88 __mpx_wrapper_memset (dst
, 0, len
);
92 __mpx_wrapper_memmove (void *dst
, const void *src
, size_t n
)
94 const char *s
= (const char*)src
;
97 size_t offset_src
= ((size_t) s
) & (sizeof (void *) - 1);
98 size_t offset_dst
= ((size_t) d
) & (sizeof (void *) - 1);
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
);
118 offset_src
= (offset_src
+ n
) & (sizeof (void *) -1);
119 while (n
-- && offset_src
--)
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 *);
139 offset_src
= sizeof (void *) - offset_src
;
140 while (n
-- && offset_src
--)
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 *);
164 __mpx_wrapper_memcpy (void *dst
, const void *src
, size_t n
)
166 return __mpx_wrapper_memmove (dst
, src
, n
);
170 __mpx_wrapper_mempcpy (void *dst
, const void *src
, size_t n
)
172 return (char *)__mpx_wrapper_memcpy (dst
, src
, n
) + n
;
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);
183 __bnd_chk_ptr_bounds (src
, src_size
+ 1);
185 __bnd_chk_ptr_bounds (src
, src_size
);
187 strncat (dst
, src
, n
);
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);
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
;
220 __mpx_wrapper_stpncpy (char *dst
, const char *src
, size_t n
)
222 size_t src_size
= strnlen (src
, n
);
225 __bnd_chk_ptr_bounds (dst
, n
);
228 __bnd_chk_ptr_bounds (src
, src_size
+ 1);
229 res
= dst
+ src_size
;
233 __bnd_chk_ptr_bounds (src
, src_size
);
237 memcpy (dst
, src
, src_size
);
239 memset (dst
+ src_size
, 0, n
- src_size
);
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);
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
);
264 __bnd_chk_ptr_bounds (src
, src_size
+ 1);
266 __bnd_chk_ptr_bounds (src
, src_size
);
268 memcpy (dst
, src
, src_size
);
270 memset (dst
+ src_size
, 0, n
- src_size
);
276 __mpx_wrapper_strlen (const char *s
)
278 size_t length
= strlen (s
);
279 __bnd_chk_ptr_bounds (s
, length
+ 1);