1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* MPX Wrappers Library
Copyright (C) 2014 Free Software Foundation, Inc.
Contributed by Ilya Enkovich (ilya.enkovich@intel.com)
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include "stdlib.h"
#include "string.h"
#include <sys/mman.h>
void *
__mpx_wrapper_malloc (size_t size)
{
void *p = (void *)malloc (size);
if (!p) return __bnd_null_ptr_bounds (p);
return __bnd_set_ptr_bounds (p, size);
}
void *
__mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
void *p = mmap (addr, length, prot, flags, fd, offset);
if (!p) return __bnd_null_ptr_bounds (p);
return __bnd_set_ptr_bounds (p, length);
}
void *
__mpx_wrapper_realloc (void *ptr, size_t n)
{
if (!ptr)
return __mpx_wrapper_malloc (n);
/* We don't kwnow how much data is copied by realloc
and therefore may check only lower bounds. */
__bnd_chk_ptr_lbounds (ptr);
ptr = realloc (ptr, n);
if (!ptr)
return __bnd_null_ptr_bounds (ptr);
return __bnd_set_ptr_bounds (ptr, n);
}
void *
__mpx_wrapper_calloc (size_t n_elements, size_t element_size)
{
void *p = calloc (n_elements, element_size);
if (!p)
return __bnd_null_ptr_bounds (p);
return __bnd_set_ptr_bounds (p, n_elements * element_size);
}
void *
__mpx_wrapper_memset (void *dstpp, int c, size_t len)
{
if (len > 0)
{
__bnd_chk_ptr_bounds (dstpp, len);
memset (dstpp, c, len);
}
return dstpp;
}
void
__mpx_wrapper_bzero (void *dst, size_t len)
{
__mpx_wrapper_memset (dst, 0, len);
}
void *
__mpx_wrapper_memmove (void *dst, const void *src, size_t n)
{
const char *s = (const char*)src;
char *d = (char*)dst;
void *ret = dst;
size_t offset_src = ((size_t) s) & (sizeof (void *) - 1);
size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1);
if (n == 0)
return ret;
__bnd_chk_ptr_bounds (dst, n);
__bnd_chk_ptr_bounds (src, n);
/* Different alignment means that even if
pointers exist in memory, we don't how
pointers are aligned and therefore cann't
copy bounds anyway. */
if (offset_src != offset_dst)
memmove (dst, src, n);
else
{
if (s < d)
{
d += n;
s += n;
offset_src = (offset_src + n) & (sizeof (void *) -1);
while (n-- && offset_src--)
*--d = *--s;
n++;
if (!n)
return ret;
void **d1 = (void **)d;
void **s1 = (void **)s;
/* This loop will also copy bounds. */
while (n >= sizeof (void *))
{
n -= sizeof (void *);
*--d1 = *--s1;
}
s = (char *)s1;
d = (char *)d1;
while (n--)
*--d = *--s;
}
else
{
offset_src = sizeof (void *) - offset_src;
while (n-- && offset_src--)
*d++ = *s++;
n++;
if (!n)
return ret;
void **d1 = (void **)d;
void **s1 = (void **)s;
/* This loop will also copy bounds. */
while (n >= sizeof (void *))
{
n -= sizeof (void *);
*d1++ = *s1++;
}
s = (char *)s1;
d = (char *)d1;
while (n--)
*d++ = *s++;
}
}
return ret;
}
void *
__mpx_wrapper_memcpy (void *dst, const void *src, size_t n)
{
return __mpx_wrapper_memmove (dst, src, n);
}
void *
__mpx_wrapper_mempcpy (void *dst, const void *src, size_t n)
{
return (char *)__mpx_wrapper_memcpy (dst, src, n) + n;
}
char *
__mpx_wrapper_strncat (char *dst, const char *src, size_t n)
{
size_t dst_size = strlen (dst);
size_t src_size = strnlen (src, n);
__bnd_chk_ptr_bounds (dst, dst_size + src_size + 1);
if (src_size < n)
__bnd_chk_ptr_bounds (src, src_size + 1);
else
__bnd_chk_ptr_bounds (src, src_size);
strncat (dst, src, n);
return dst;
}
char *
__mpx_wrapper_strcat (char *dst, const char *src)
{
size_t dst_size = strlen (dst);
size_t src_size = strlen (src);
__bnd_chk_ptr_bounds (dst, dst_size + src_size + 1);
__bnd_chk_ptr_bounds (src, src_size + 1);
strcat (dst, src);
return dst;
}
char *
__mpx_wrapper_stpcpy (char *dst, const char *src)
{
size_t src_size = strlen (src);
__bnd_chk_ptr_bounds (dst, src_size + 1);
__bnd_chk_ptr_bounds (src, src_size + 1);
memcpy (dst, src, src_size + 1);
return dst + src_size;
}
char *
__mpx_wrapper_stpncpy (char *dst, const char *src, size_t n)
{
size_t src_size = strnlen (src, n);
char *res;
__bnd_chk_ptr_bounds (dst, n);
if (src_size < n)
{
__bnd_chk_ptr_bounds (src, src_size + 1);
res = dst + src_size;
}
else
{
__bnd_chk_ptr_bounds (src, src_size);
res = dst + n;
}
memcpy (dst, src, src_size);
if (n > src_size)
memset (dst + src_size, 0, n - src_size);
return res;
}
char *
__mpx_wrapper_strcpy (char *dst, const char *src)
{
size_t src_size = strlen (src);
__bnd_chk_ptr_bounds (dst, src_size + 1);
__bnd_chk_ptr_bounds (src, src_size + 1);
memcpy (dst, src, src_size + 1);
return dst;
}
char *
__mpx_wrapper_strncpy (char *dst, const char *src, size_t n)
{
size_t src_size = strnlen (src, n);
__bnd_chk_ptr_bounds (dst, n);
if (src_size < n)
__bnd_chk_ptr_bounds (src, src_size + 1);
else
__bnd_chk_ptr_bounds (src, src_size);
memcpy (dst, src, src_size);
if (n > src_size)
memset (dst + src_size, 0, n - src_size);
return dst;
}
size_t
__mpx_wrapper_strlen (const char *s)
{
size_t length = strlen (s);
__bnd_chk_ptr_bounds (s, length + 1);
return length;
}
|