annotate libmpx/mpxwrap/mpx_wrappers.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* MPX Wrappers Library
kono
parents:
diff changeset
2 Copyright (C) 2014 Free Software Foundation, Inc.
kono
parents:
diff changeset
3 Contributed by Ilya Enkovich (ilya.enkovich@intel.com)
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #include "stdlib.h"
kono
parents:
diff changeset
27 #include "string.h"
kono
parents:
diff changeset
28 #include <sys/mman.h>
kono
parents:
diff changeset
29 #include <stdint.h>
kono
parents:
diff changeset
30 #include <assert.h>
kono
parents:
diff changeset
31 #include "mpxrt/mpxrt.h"
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 /* Since internal MPX wrapper calls must avoid PLT which will clear bound
kono
parents:
diff changeset
34 registers, we make them static with an external alias. */
kono
parents:
diff changeset
35 #define EXTERN_ALIAS(f) \
kono
parents:
diff changeset
36 __typeof (f) __##f __attribute__((alias(#f)));
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 static void *
kono
parents:
diff changeset
39 mpx_wrapper_malloc (size_t size)
kono
parents:
diff changeset
40 {
kono
parents:
diff changeset
41 void *p = (void *)malloc (size);
kono
parents:
diff changeset
42 if (!p) return __bnd_null_ptr_bounds (p);
kono
parents:
diff changeset
43 return __bnd_set_ptr_bounds (p, size);
kono
parents:
diff changeset
44 }
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 EXTERN_ALIAS (mpx_wrapper_malloc)
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 void *
kono
parents:
diff changeset
49 __mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags,
kono
parents:
diff changeset
50 int fd, off_t offset)
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 void *p = mmap (addr, length, prot, flags, fd, offset);
kono
parents:
diff changeset
53 if (!p) return __bnd_null_ptr_bounds (p);
kono
parents:
diff changeset
54 return __bnd_set_ptr_bounds (p, length);
kono
parents:
diff changeset
55 }
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 void *
kono
parents:
diff changeset
58 __mpx_wrapper_realloc (void *ptr, size_t n)
kono
parents:
diff changeset
59 {
kono
parents:
diff changeset
60 if (!ptr)
kono
parents:
diff changeset
61 return mpx_wrapper_malloc (n);
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 /* We don't kwnow how much data is copied by realloc
kono
parents:
diff changeset
64 and therefore may check only lower bounds. */
kono
parents:
diff changeset
65 __bnd_chk_ptr_lbounds (ptr);
kono
parents:
diff changeset
66 ptr = realloc (ptr, n);
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 if (!ptr)
kono
parents:
diff changeset
69 return __bnd_null_ptr_bounds (ptr);
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 return __bnd_set_ptr_bounds (ptr, n);
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 void *
kono
parents:
diff changeset
75 __mpx_wrapper_calloc (size_t n_elements, size_t element_size)
kono
parents:
diff changeset
76 {
kono
parents:
diff changeset
77 void *p = calloc (n_elements, element_size);
kono
parents:
diff changeset
78 if (!p)
kono
parents:
diff changeset
79 return __bnd_null_ptr_bounds (p);
kono
parents:
diff changeset
80 return __bnd_set_ptr_bounds (p, n_elements * element_size);
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 static void *
kono
parents:
diff changeset
84 mpx_wrapper_memset (void *dstpp, int c, size_t len)
kono
parents:
diff changeset
85 {
kono
parents:
diff changeset
86 if (len > 0)
kono
parents:
diff changeset
87 {
kono
parents:
diff changeset
88 __bnd_chk_ptr_bounds (dstpp, len);
kono
parents:
diff changeset
89 memset (dstpp, c, len);
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91 return dstpp;
kono
parents:
diff changeset
92 }
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 EXTERN_ALIAS (mpx_wrapper_memset)
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 void
kono
parents:
diff changeset
97 __mpx_wrapper_bzero (void *dst, size_t len)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 mpx_wrapper_memset (dst, 0, len);
kono
parents:
diff changeset
100 }
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 /* The mpx_pointer type is used for getting bits
kono
parents:
diff changeset
103 for bt_index (index in bounds table) and
kono
parents:
diff changeset
104 bd_index (index in bounds directory). */
kono
parents:
diff changeset
105 typedef union
kono
parents:
diff changeset
106 {
kono
parents:
diff changeset
107 struct
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 unsigned long ignored:NUM_IGN_BITS;
kono
parents:
diff changeset
110 unsigned long l2entry:NUM_L2_BITS;
kono
parents:
diff changeset
111 unsigned long l1index:NUM_L1_BITS;
kono
parents:
diff changeset
112 };
kono
parents:
diff changeset
113 void *pointer;
kono
parents:
diff changeset
114 } mpx_pointer;
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 /* The mpx_bt_entry struct represents a cell in bounds table.
kono
parents:
diff changeset
117 lb is the lower bound, ub is the upper bound,
kono
parents:
diff changeset
118 p is the stored pointer. */
kono
parents:
diff changeset
119 struct mpx_bt_entry
kono
parents:
diff changeset
120 {
kono
parents:
diff changeset
121 void *lb;
kono
parents:
diff changeset
122 void *ub;
kono
parents:
diff changeset
123 void *p;
kono
parents:
diff changeset
124 void *reserved;
kono
parents:
diff changeset
125 };
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 /* A special type for bd is needed because bt addresses can be modified. */
kono
parents:
diff changeset
128 typedef struct mpx_bt_entry * volatile * bd_type;
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 /* Function alloc_bt is used for allocating bounds table
kono
parents:
diff changeset
131 for the destination pointers if we don't have one.
kono
parents:
diff changeset
132 We generate a bounds store for some pointer belonging
kono
parents:
diff changeset
133 to that table and kernel allocates the table for us. */
kono
parents:
diff changeset
134 static inline void __attribute__ ((bnd_legacy))
kono
parents:
diff changeset
135 alloc_bt (void *ptr)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 __asm__ __volatile__ ("bndstx %%bnd0, (%0,%0)"::"r" (ptr):"%bnd0");
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* get_bt returns address of bounds table that should
kono
parents:
diff changeset
141 exist at BD[BD_INDEX]. If there is no address or the address is not valid,
kono
parents:
diff changeset
142 we try to allocate a valid table.
kono
parents:
diff changeset
143 If we succeed in getting bt, its address will be returned.
kono
parents:
diff changeset
144 If we can't get a valid bt, NULL will be returned. */
kono
parents:
diff changeset
145 __attribute__ ((bnd_legacy)) static inline struct mpx_bt_entry *
kono
parents:
diff changeset
146 get_bt (unsigned bd_index, bd_type bd)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 struct mpx_bt_entry *bt = (struct mpx_bt_entry *) ((uintptr_t) bd[bd_index]
kono
parents:
diff changeset
149 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
150 if (!(bt) || !((uintptr_t) bd[bd_index] & MPX_L2_VALID_MASK))
kono
parents:
diff changeset
151 {
kono
parents:
diff changeset
152 mpx_pointer ptr;
kono
parents:
diff changeset
153 ptr.l1index = bd_index;
kono
parents:
diff changeset
154 /* If we don't have BT, allocate it. */
kono
parents:
diff changeset
155 alloc_bt (ptr.pointer);
kono
parents:
diff changeset
156 bt = (struct mpx_bt_entry *) ((uintptr_t) bd[bd_index]
kono
parents:
diff changeset
157 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
158 if (!(bt) || !((uintptr_t) bd[bd_index] & MPX_L2_VALID_MASK))
kono
parents:
diff changeset
159 return NULL;
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161 return bt;
kono
parents:
diff changeset
162 }
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 /* Function copy_if_possible moves elements from *FROM to *TO.
kono
parents:
diff changeset
165 If ELEMS is less then the ELEMS_TO_COPY (elements we can copy),
kono
parents:
diff changeset
166 it copies ELEMS elements and returns 0.
kono
parents:
diff changeset
167 Otherwise, it copies ELEMS_TO_COPY elements and returns 1. */
kono
parents:
diff changeset
168 __attribute__ ((bnd_legacy)) static inline int
kono
parents:
diff changeset
169 copy_if_possible (int elems, int elems_to_copy, struct mpx_bt_entry *from,
kono
parents:
diff changeset
170 struct mpx_bt_entry *to)
kono
parents:
diff changeset
171 {
kono
parents:
diff changeset
172 if (elems < elems_to_copy)
kono
parents:
diff changeset
173 memmove (to, from, elems * sizeof (struct mpx_bt_entry));
kono
parents:
diff changeset
174 else
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 memmove (to, from, elems_to_copy * sizeof (struct mpx_bt_entry));
kono
parents:
diff changeset
177 return 1;
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179 return 0;
kono
parents:
diff changeset
180 }
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 /* Function copy_if_possible_from_end moves elements ending at *SRC_END
kono
parents:
diff changeset
183 to the place where they will end at *DST_END.
kono
parents:
diff changeset
184 If ELEMS is less then the ELEMS_TO_COPY (elements we can copy),
kono
parents:
diff changeset
185 function copies ELEMS elements and returns 0.
kono
parents:
diff changeset
186 Otherwise, it copies ELEMS_TO_COPY elements and returns 1. */
kono
parents:
diff changeset
187 __attribute__ ((bnd_legacy)) static inline int
kono
parents:
diff changeset
188 copy_if_possible_from_end (int elems, int elems_to_copy, struct mpx_bt_entry
kono
parents:
diff changeset
189 *src_end, struct mpx_bt_entry *dst_end)
kono
parents:
diff changeset
190 {
kono
parents:
diff changeset
191 if (elems < elems_to_copy)
kono
parents:
diff changeset
192 memmove (dst_end - elems, src_end - elems,
kono
parents:
diff changeset
193 elems * sizeof (struct mpx_bt_entry));
kono
parents:
diff changeset
194 else
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 memmove (dst_end - elems_to_copy,
kono
parents:
diff changeset
197 src_end - elems_to_copy,
kono
parents:
diff changeset
198 elems_to_copy * sizeof (struct mpx_bt_entry));
kono
parents:
diff changeset
199 return 1;
kono
parents:
diff changeset
200 }
kono
parents:
diff changeset
201 return 0;
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 /* move_bounds function copies bounds for N bytes from bt of SRC to bt of DST.
kono
parents:
diff changeset
205 It also copies bounds for all pointers inside.
kono
parents:
diff changeset
206 There are 3 parts of the algorithm:
kono
parents:
diff changeset
207 1) We copy everything till the end of the first bounds table of SRC
kono
parents:
diff changeset
208 2) In loop we copy whole bound tables till the second-last one
kono
parents:
diff changeset
209 3) Data in the last bounds table is copied separately, after the loop.
kono
parents:
diff changeset
210 If one of bound tables in SRC doesn't exist,
kono
parents:
diff changeset
211 we skip it because there are no pointers.
kono
parents:
diff changeset
212 Depending on the arrangement of SRC and DST we copy from the beginning
kono
parents:
diff changeset
213 or from the end. */
kono
parents:
diff changeset
214 __attribute__ ((bnd_legacy)) static void
kono
parents:
diff changeset
215 move_bounds (void *dst, const void *src, size_t n)
kono
parents:
diff changeset
216 {
kono
parents:
diff changeset
217 bd_type bd = (bd_type)get_bd ();
kono
parents:
diff changeset
218 if (!(bd))
kono
parents:
diff changeset
219 return;
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 /* We get indexes for all tables and number of elements for BT. */
kono
parents:
diff changeset
222 unsigned long bt_num_of_elems = (1UL << NUM_L2_BITS);
kono
parents:
diff changeset
223 mpx_pointer addr_src, addr_dst, addr_src_end, addr_dst_end;
kono
parents:
diff changeset
224 addr_src.pointer = (char *) src;
kono
parents:
diff changeset
225 addr_dst.pointer = (char *) dst;
kono
parents:
diff changeset
226 addr_src_end.pointer = (char *) src + n - 1;
kono
parents:
diff changeset
227 addr_dst_end.pointer = (char *) dst + n - 1;
kono
parents:
diff changeset
228 unsigned dst_bd_index = addr_dst.l1index;
kono
parents:
diff changeset
229 unsigned src_bd_index = addr_src.l1index;
kono
parents:
diff changeset
230 unsigned dst_bt_index = addr_dst.l2entry;
kono
parents:
diff changeset
231 unsigned src_bt_index = addr_src.l2entry;
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 unsigned dst_bd_index_end = addr_dst_end.l1index;
kono
parents:
diff changeset
234 unsigned src_bd_index_end = addr_src_end.l1index;
kono
parents:
diff changeset
235 unsigned dst_bt_index_end = addr_dst_end.l2entry;
kono
parents:
diff changeset
236 unsigned src_bt_index_end = addr_src_end.l2entry;
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 int elems_to_copy = src_bt_index_end - src_bt_index + 1 + (src_bd_index_end
kono
parents:
diff changeset
239 - src_bd_index) * bt_num_of_elems;
kono
parents:
diff changeset
240 struct mpx_bt_entry *bt_src, *bt_dst;
kono
parents:
diff changeset
241 uintptr_t bt_valid;
kono
parents:
diff changeset
242 /* size1 and size2 will be used to find out what portions
kono
parents:
diff changeset
243 can be used to copy data. */
kono
parents:
diff changeset
244 int size1_elem, size2_elem, size1_bytes, size2_bytes;
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 /* Copy from the beginning. */
kono
parents:
diff changeset
247 if (((char *) src - (char *) dst) > 0)
kono
parents:
diff changeset
248 {
kono
parents:
diff changeset
249 /* Copy everything till the end of the first bounds table (src) */
kono
parents:
diff changeset
250 bt_src = (struct mpx_bt_entry *) ((uintptr_t) bd[src_bd_index]
kono
parents:
diff changeset
251 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
252 bt_valid = (uintptr_t) bd[src_bd_index] & MPX_L2_VALID_MASK;
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 /* We can copy the whole preliminary piece of data. */
kono
parents:
diff changeset
255 if (src_bt_index > dst_bt_index)
kono
parents:
diff changeset
256 {
kono
parents:
diff changeset
257 size1_elem = src_bt_index - dst_bt_index;
kono
parents:
diff changeset
258 size2_elem = bt_num_of_elems - size1_elem;
kono
parents:
diff changeset
259 size1_bytes = size1_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
260 size2_bytes = size2_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 /* Check we have bounds to copy. */
kono
parents:
diff changeset
263 if (bt_src && bt_valid)
kono
parents:
diff changeset
264 {
kono
parents:
diff changeset
265 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
266 if (!bt_dst)
kono
parents:
diff changeset
267 return;
kono
parents:
diff changeset
268 if (copy_if_possible (bt_num_of_elems - src_bt_index,
kono
parents:
diff changeset
269 elems_to_copy, &(bt_src[src_bt_index]),
kono
parents:
diff changeset
270 &(bt_dst[dst_bt_index])))
kono
parents:
diff changeset
271 return;
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273 elems_to_copy -= bt_num_of_elems - src_bt_index;
kono
parents:
diff changeset
274 }
kono
parents:
diff changeset
275 /* We have to copy preliminary data in two parts. */
kono
parents:
diff changeset
276 else
kono
parents:
diff changeset
277 {
kono
parents:
diff changeset
278 size2_elem = dst_bt_index - src_bt_index;
kono
parents:
diff changeset
279 size1_elem = bt_num_of_elems - size2_elem;
kono
parents:
diff changeset
280 size1_bytes = size1_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
281 size2_bytes = size2_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 /* Check we have bounds to copy. */
kono
parents:
diff changeset
284 if (bt_src && bt_valid)
kono
parents:
diff changeset
285 {
kono
parents:
diff changeset
286 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
287 if (!bt_dst)
kono
parents:
diff changeset
288 return;
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 if (copy_if_possible (bt_num_of_elems - dst_bt_index,
kono
parents:
diff changeset
291 elems_to_copy, &(bt_src[src_bt_index]),
kono
parents:
diff changeset
292 &(bt_dst[dst_bt_index])))
kono
parents:
diff changeset
293 return;
kono
parents:
diff changeset
294 elems_to_copy -= bt_num_of_elems - dst_bt_index;
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 dst_bd_index++;
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
299 if (!bt_dst)
kono
parents:
diff changeset
300 return;
kono
parents:
diff changeset
301 if (copy_if_possible (size2_elem, elems_to_copy,
kono
parents:
diff changeset
302 &(bt_src[size1_elem]), &(bt_dst[0])))
kono
parents:
diff changeset
303 return;
kono
parents:
diff changeset
304 elems_to_copy -= size2_elem;
kono
parents:
diff changeset
305 }
kono
parents:
diff changeset
306 else
kono
parents:
diff changeset
307 elems_to_copy -= bt_num_of_elems - src_bt_index;
kono
parents:
diff changeset
308 }
kono
parents:
diff changeset
309 src_bd_index++;
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 /* For each bounds table check if it's valid and move it. */
kono
parents:
diff changeset
312 for (; src_bd_index < src_bd_index_end; src_bd_index++)
kono
parents:
diff changeset
313 {
kono
parents:
diff changeset
314 bt_src = (struct mpx_bt_entry *) ((uintptr_t) bd[src_bd_index]
kono
parents:
diff changeset
315 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
316 bt_valid = (uintptr_t) bd[src_bd_index] & MPX_L2_VALID_MASK;
kono
parents:
diff changeset
317
kono
parents:
diff changeset
318 /* Check we have bounds to copy. */
kono
parents:
diff changeset
319 if (!bt_src || !bt_valid)
kono
parents:
diff changeset
320 dst_bd_index++;
kono
parents:
diff changeset
321 else
kono
parents:
diff changeset
322 {
kono
parents:
diff changeset
323 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
324 if (!bt_dst)
kono
parents:
diff changeset
325 return;
kono
parents:
diff changeset
326 memmove (&(bt_dst[size2_elem]), &(bt_src[0]), size1_bytes);
kono
parents:
diff changeset
327 dst_bd_index++;
kono
parents:
diff changeset
328 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
329 if (!bt_dst)
kono
parents:
diff changeset
330 return;
kono
parents:
diff changeset
331 memmove (&(bt_dst[0]), &(bt_src[size1_elem]), size2_bytes);
kono
parents:
diff changeset
332 }
kono
parents:
diff changeset
333 elems_to_copy -= bt_num_of_elems;
kono
parents:
diff changeset
334 }
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 /* Now we have the last page that may be not full
kono
parents:
diff changeset
337 we copy it separately. */
kono
parents:
diff changeset
338 if (elems_to_copy > 0)
kono
parents:
diff changeset
339 {
kono
parents:
diff changeset
340 bt_src = (struct mpx_bt_entry *) ((uintptr_t) bd[src_bd_index]
kono
parents:
diff changeset
341 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
342 bt_valid = (uintptr_t) bd[src_bd_index] & MPX_L2_VALID_MASK;
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 /* Check we have bounds to copy. */
kono
parents:
diff changeset
345 if (bt_src && bt_valid)
kono
parents:
diff changeset
346 {
kono
parents:
diff changeset
347 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
348 if (!bt_dst)
kono
parents:
diff changeset
349 return;
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 if (copy_if_possible (size1_elem, elems_to_copy, &(bt_src[0]),
kono
parents:
diff changeset
352 &(bt_dst[size2_elem])))
kono
parents:
diff changeset
353 return;
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 elems_to_copy -= size1_elem;
kono
parents:
diff changeset
356 dst_bd_index++;
kono
parents:
diff changeset
357 bt_dst = get_bt (dst_bd_index, bd);
kono
parents:
diff changeset
358 if (!bt_dst)
kono
parents:
diff changeset
359 return;
kono
parents:
diff changeset
360 memmove (&(bt_dst[0]), &(bt_src[size1_elem]),
kono
parents:
diff changeset
361 elems_to_copy * sizeof (struct mpx_bt_entry));
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 }
kono
parents:
diff changeset
364 }
kono
parents:
diff changeset
365 }
kono
parents:
diff changeset
366 /* Copy from the end. */
kono
parents:
diff changeset
367 else
kono
parents:
diff changeset
368 {
kono
parents:
diff changeset
369 /* Copy everything till the end of the first bounds table (src) */
kono
parents:
diff changeset
370 bt_src = (struct mpx_bt_entry *) ((uintptr_t) bd[src_bd_index_end]
kono
parents:
diff changeset
371 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
372 bt_valid = (uintptr_t) bd[src_bd_index_end] & MPX_L2_VALID_MASK;
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 if (src_bt_index_end <= dst_bt_index_end)
kono
parents:
diff changeset
375 /* We can copy the whole preliminary piece of data. */
kono
parents:
diff changeset
376 {
kono
parents:
diff changeset
377 size2_elem = dst_bt_index_end - src_bt_index_end;
kono
parents:
diff changeset
378 size1_elem = bt_num_of_elems - size2_elem;
kono
parents:
diff changeset
379 size1_bytes = size1_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
380 size2_bytes = size2_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 /* Check we have bounds to copy. */
kono
parents:
diff changeset
383 if (bt_src && bt_valid)
kono
parents:
diff changeset
384 {
kono
parents:
diff changeset
385 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
386 if (!bt_dst)
kono
parents:
diff changeset
387 return;
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 if (copy_if_possible_from_end (src_bt_index_end + 1,
kono
parents:
diff changeset
390 elems_to_copy, &(bt_src[src_bt_index_end + 1]),
kono
parents:
diff changeset
391 &(bt_dst[dst_bt_index_end + 1])))
kono
parents:
diff changeset
392 return;
kono
parents:
diff changeset
393 }
kono
parents:
diff changeset
394 elems_to_copy -= src_bt_index_end + 1;
kono
parents:
diff changeset
395 }
kono
parents:
diff changeset
396 /* We have to copy preliminary data in two parts. */
kono
parents:
diff changeset
397 else
kono
parents:
diff changeset
398 {
kono
parents:
diff changeset
399 size1_elem = src_bt_index_end - dst_bt_index_end;
kono
parents:
diff changeset
400 size2_elem = bt_num_of_elems - size1_elem;
kono
parents:
diff changeset
401 size1_bytes = size1_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
402 size2_bytes = size2_elem * sizeof (struct mpx_bt_entry);
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 /* Check we have bounds to copy. */
kono
parents:
diff changeset
405 if (bt_src && bt_valid)
kono
parents:
diff changeset
406 {
kono
parents:
diff changeset
407 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
408 if (!bt_dst)
kono
parents:
diff changeset
409 return;
kono
parents:
diff changeset
410 if (copy_if_possible_from_end (dst_bt_index_end + 1,
kono
parents:
diff changeset
411 elems_to_copy, &(bt_src[src_bt_index_end + 1]),
kono
parents:
diff changeset
412 &(bt_dst[dst_bt_index_end + 1])))
kono
parents:
diff changeset
413 return;
kono
parents:
diff changeset
414 elems_to_copy -= dst_bt_index_end + 1;
kono
parents:
diff changeset
415
kono
parents:
diff changeset
416 dst_bd_index_end--;
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
419 if (!bt_dst)
kono
parents:
diff changeset
420 return;
kono
parents:
diff changeset
421 if (copy_if_possible_from_end (size1_elem, elems_to_copy,
kono
parents:
diff changeset
422 &(bt_src[size1_elem]), &(bt_dst[bt_num_of_elems])))
kono
parents:
diff changeset
423 return;
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 elems_to_copy -= size1_elem;
kono
parents:
diff changeset
426 }
kono
parents:
diff changeset
427 else
kono
parents:
diff changeset
428 elems_to_copy -= src_bt_index_end + 1;
kono
parents:
diff changeset
429 }
kono
parents:
diff changeset
430 /* Go to previous table but beware of overflow.
kono
parents:
diff changeset
431 We should have copied all required element
kono
parents:
diff changeset
432 in case src_bd_index_end is 0. */
kono
parents:
diff changeset
433 if (src_bd_index_end)
kono
parents:
diff changeset
434 src_bd_index_end--;
kono
parents:
diff changeset
435 else
kono
parents:
diff changeset
436 {
kono
parents:
diff changeset
437 assert (!elems_to_copy);
kono
parents:
diff changeset
438 return;
kono
parents:
diff changeset
439 }
kono
parents:
diff changeset
440 /* For each bounds table we check if there are valid pointers inside.
kono
parents:
diff changeset
441 If there are some, we copy table in pre-counted portions. */
kono
parents:
diff changeset
442 for (; src_bd_index_end > src_bd_index; src_bd_index_end--)
kono
parents:
diff changeset
443 {
kono
parents:
diff changeset
444 bt_src = (struct mpx_bt_entry *) ((uintptr_t) bd[src_bd_index_end]
kono
parents:
diff changeset
445 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
446 bt_valid = (uintptr_t) bd[src_bd_index_end] & MPX_L2_VALID_MASK;
kono
parents:
diff changeset
447 /* Check we have bounds to copy. */
kono
parents:
diff changeset
448 if (!bt_src || !bt_valid)
kono
parents:
diff changeset
449 dst_bd_index_end--;
kono
parents:
diff changeset
450 else
kono
parents:
diff changeset
451 {
kono
parents:
diff changeset
452 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
453 if (!bt_dst)
kono
parents:
diff changeset
454 return;
kono
parents:
diff changeset
455 memmove (&(bt_dst[0]), &(bt_src[size1_elem]), size2_bytes);
kono
parents:
diff changeset
456 dst_bd_index_end--;
kono
parents:
diff changeset
457 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
458 if (!bt_dst)
kono
parents:
diff changeset
459 return;
kono
parents:
diff changeset
460 memmove (&(bt_dst[size2_elem]), &(bt_src[0]), size1_bytes);
kono
parents:
diff changeset
461 }
kono
parents:
diff changeset
462 elems_to_copy -= bt_num_of_elems;
kono
parents:
diff changeset
463 }
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 /* Now we have the last page that may be not full
kono
parents:
diff changeset
466 we copy it separately. */
kono
parents:
diff changeset
467 if (elems_to_copy > 0)
kono
parents:
diff changeset
468 {
kono
parents:
diff changeset
469 bt_src = (struct mpx_bt_entry *) ((uintptr_t) bd[src_bd_index_end]
kono
parents:
diff changeset
470 & MPX_L2_ADDR_MASK);
kono
parents:
diff changeset
471 bt_valid = (uintptr_t) bd[src_bd_index_end] & MPX_L2_VALID_MASK;
kono
parents:
diff changeset
472 /* Check we have bounds to copy. */
kono
parents:
diff changeset
473 if (bt_src && bt_valid)
kono
parents:
diff changeset
474 {
kono
parents:
diff changeset
475 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
476 if (!bt_dst)
kono
parents:
diff changeset
477 return;
kono
parents:
diff changeset
478 if (copy_if_possible_from_end (size2_elem, elems_to_copy,
kono
parents:
diff changeset
479 &(bt_src[bt_num_of_elems]), &(bt_dst[size2_elem])))
kono
parents:
diff changeset
480 return;
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 elems_to_copy -= size2_elem;
kono
parents:
diff changeset
483 dst_bd_index_end--;
kono
parents:
diff changeset
484 bt_dst = get_bt (dst_bd_index_end, bd);
kono
parents:
diff changeset
485 if (!bt_dst)
kono
parents:
diff changeset
486 return;
kono
parents:
diff changeset
487 memmove (&(bt_dst[dst_bt_index]), &(bt_src[src_bt_index]),
kono
parents:
diff changeset
488 elems_to_copy * sizeof (struct mpx_bt_entry));
kono
parents:
diff changeset
489 }
kono
parents:
diff changeset
490 }
kono
parents:
diff changeset
491 }
kono
parents:
diff changeset
492 return;
kono
parents:
diff changeset
493 }
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 static void *
kono
parents:
diff changeset
496 mpx_wrapper_memmove (void *dst, const void *src, size_t n)
kono
parents:
diff changeset
497 {
kono
parents:
diff changeset
498 if (n == 0)
kono
parents:
diff changeset
499 return dst;
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 __bnd_chk_ptr_bounds (dst, n);
kono
parents:
diff changeset
502 __bnd_chk_ptr_bounds (src, n);
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504 /* When we copy exactly one pointer it is faster to
kono
parents:
diff changeset
505 just use bndldx + bndstx. */
kono
parents:
diff changeset
506 if (n == sizeof (void *))
kono
parents:
diff changeset
507 {
kono
parents:
diff changeset
508 void *const *s = (void *const *) src;
kono
parents:
diff changeset
509 void **d = (void **) dst;
kono
parents:
diff changeset
510 *d = *s;
kono
parents:
diff changeset
511 return dst;
kono
parents:
diff changeset
512 }
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 memmove (dst, src, n);
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 /* Not necessary to copy bounds if size is less then size of pointer
kono
parents:
diff changeset
517 or SRC==DST. */
kono
parents:
diff changeset
518 if ((n >= sizeof (void *)) && (src != dst))
kono
parents:
diff changeset
519 move_bounds (dst, src, n);
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 return dst;
kono
parents:
diff changeset
522 }
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 EXTERN_ALIAS (mpx_wrapper_memmove)
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 static void *
kono
parents:
diff changeset
527 mpx_wrapper_memcpy (void *dst, const void *src, size_t n)
kono
parents:
diff changeset
528 {
kono
parents:
diff changeset
529 return mpx_wrapper_memmove (dst, src, n);
kono
parents:
diff changeset
530 }
kono
parents:
diff changeset
531
kono
parents:
diff changeset
532 EXTERN_ALIAS (mpx_wrapper_memcpy)
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 void *
kono
parents:
diff changeset
535 __mpx_wrapper_mempcpy (void *dst, const void *src, size_t n)
kono
parents:
diff changeset
536 {
kono
parents:
diff changeset
537 return (char *)mpx_wrapper_memcpy (dst, src, n) + n;
kono
parents:
diff changeset
538 }
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 char *
kono
parents:
diff changeset
541 __mpx_wrapper_strncat (char *dst, const char *src, size_t n)
kono
parents:
diff changeset
542 {
kono
parents:
diff changeset
543 size_t dst_size = strlen (dst);
kono
parents:
diff changeset
544 size_t src_size = strnlen (src, n);
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1);
kono
parents:
diff changeset
547 if (src_size < n)
kono
parents:
diff changeset
548 __bnd_chk_ptr_bounds (src, src_size + 1);
kono
parents:
diff changeset
549 else
kono
parents:
diff changeset
550 __bnd_chk_ptr_bounds (src, src_size);
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 strncat (dst, src, n);
kono
parents:
diff changeset
553
kono
parents:
diff changeset
554 return dst;
kono
parents:
diff changeset
555 }
kono
parents:
diff changeset
556
kono
parents:
diff changeset
557 char *
kono
parents:
diff changeset
558 __mpx_wrapper_strcat (char *dst, const char *src)
kono
parents:
diff changeset
559 {
kono
parents:
diff changeset
560 size_t dst_size = strlen (dst);
kono
parents:
diff changeset
561 size_t src_size = strlen (src);
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1);
kono
parents:
diff changeset
564 __bnd_chk_ptr_bounds (src, src_size + 1);
kono
parents:
diff changeset
565
kono
parents:
diff changeset
566 strcat (dst, src);
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 return dst;
kono
parents:
diff changeset
569 }
kono
parents:
diff changeset
570
kono
parents:
diff changeset
571 char *
kono
parents:
diff changeset
572 __mpx_wrapper_stpcpy (char *dst, const char *src)
kono
parents:
diff changeset
573 {
kono
parents:
diff changeset
574 size_t src_size = strlen (src);
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 __bnd_chk_ptr_bounds (dst, src_size + 1);
kono
parents:
diff changeset
577 __bnd_chk_ptr_bounds (src, src_size + 1);
kono
parents:
diff changeset
578
kono
parents:
diff changeset
579 memcpy (dst, src, src_size + 1);
kono
parents:
diff changeset
580
kono
parents:
diff changeset
581 return dst + src_size;
kono
parents:
diff changeset
582 }
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 char *
kono
parents:
diff changeset
585 __mpx_wrapper_stpncpy (char *dst, const char *src, size_t n)
kono
parents:
diff changeset
586 {
kono
parents:
diff changeset
587 size_t src_size = strnlen (src, n);
kono
parents:
diff changeset
588 char *res;
kono
parents:
diff changeset
589
kono
parents:
diff changeset
590 __bnd_chk_ptr_bounds (dst, n);
kono
parents:
diff changeset
591 if (src_size < n)
kono
parents:
diff changeset
592 {
kono
parents:
diff changeset
593 __bnd_chk_ptr_bounds (src, src_size + 1);
kono
parents:
diff changeset
594 res = dst + src_size;
kono
parents:
diff changeset
595 }
kono
parents:
diff changeset
596 else
kono
parents:
diff changeset
597 {
kono
parents:
diff changeset
598 __bnd_chk_ptr_bounds (src, src_size);
kono
parents:
diff changeset
599 res = dst + n;
kono
parents:
diff changeset
600 }
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 memcpy (dst, src, src_size);
kono
parents:
diff changeset
603 if (n > src_size)
kono
parents:
diff changeset
604 memset (dst + src_size, 0, n - src_size);
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 return res;
kono
parents:
diff changeset
607 }
kono
parents:
diff changeset
608
kono
parents:
diff changeset
609 char *
kono
parents:
diff changeset
610 __mpx_wrapper_strcpy (char *dst, const char *src)
kono
parents:
diff changeset
611 {
kono
parents:
diff changeset
612 size_t src_size = strlen (src);
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 __bnd_chk_ptr_bounds (dst, src_size + 1);
kono
parents:
diff changeset
615 __bnd_chk_ptr_bounds (src, src_size + 1);
kono
parents:
diff changeset
616
kono
parents:
diff changeset
617 memcpy (dst, src, src_size + 1);
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 return dst;
kono
parents:
diff changeset
620 }
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622 char *
kono
parents:
diff changeset
623 __mpx_wrapper_strncpy (char *dst, const char *src, size_t n)
kono
parents:
diff changeset
624 {
kono
parents:
diff changeset
625 size_t src_size = strnlen (src, n);
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 __bnd_chk_ptr_bounds (dst, n);
kono
parents:
diff changeset
628 if (src_size < n)
kono
parents:
diff changeset
629 __bnd_chk_ptr_bounds (src, src_size + 1);
kono
parents:
diff changeset
630 else
kono
parents:
diff changeset
631 __bnd_chk_ptr_bounds (src, src_size);
kono
parents:
diff changeset
632
kono
parents:
diff changeset
633 memcpy (dst, src, src_size);
kono
parents:
diff changeset
634 if (n > src_size)
kono
parents:
diff changeset
635 memset (dst + src_size, 0, n - src_size);
kono
parents:
diff changeset
636
kono
parents:
diff changeset
637 return dst;
kono
parents:
diff changeset
638 }
kono
parents:
diff changeset
639
kono
parents:
diff changeset
640 size_t
kono
parents:
diff changeset
641 __mpx_wrapper_strlen (const char *s)
kono
parents:
diff changeset
642 {
kono
parents:
diff changeset
643 size_t length = strlen (s);
kono
parents:
diff changeset
644 __bnd_chk_ptr_bounds (s, length + 1);
kono
parents:
diff changeset
645 return length;
kono
parents:
diff changeset
646 }