1 /* This file is part of Netwib. Read and honor its license.
2 */
3 
4 /*-------------------------------------------------------------*/
5 /* characters in uninit_pattern */
6 #define NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C0  0x10
7 #define NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C1  'E'
8 #define NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C2  0xFF
9 /* characters in outer_pattern */
10 #define NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C0   0xA4
11 #define NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C1   '_'
12 #define NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C2   0x03
13 /* characters in free_pattern */
14 #define NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C0    0xD9
15 #define NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C1    'Z'
16 #define NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C2    0x81
17 /* chunk size for allocation of stored data */
18 #define NETWIB_DEBUG_MEMCORRUPT_ALLOCCHUNKS       1000
19 /* size of separator patterns */
20 #define MEMCORRUPT_SMALLPATTERN_SIZE 8
21 /* 1/x times to check outer_pattern and free_pattern */
22 #define NETWIB_DEBUG_MEMCORRUPT_CHECK_FREQUENCY 22
23 /* to speed up checks (ignore middle : just look at edges) */
24 #define NETWIB_DEBUG_MEMCORRUPT_SPEEDUP           1
25 
26 /*-------------------------------------------------------------*/
27 #if defined NETWIBDEF_SYSNAME_Unix
28 #define NETWIB_DEBUG_CAST_FREE
29 #elif defined NETWIBDEF_SYSNAME_Windows
30  /* We should not need it, but system doesn't accept "free((void*)ptr)".
31     If you know a better solution, contact me. */
32 #define NETWIB_DEBUG_CAST_FREE (void*)
33 #endif
34 
35 /*-------------------------------------------------------------*/
36 #define netwib_debug_mem_data_init_uint32(x,pc) { *(pc++)=netwib_c2_uint32_0(x); *(pc++)=netwib_c2_uint32_1(x); *(pc++)=netwib_c2_uint32_2(x); *(pc++)=netwib_c2_uint32_3(x); }
37 #define netwib_debug_mem_uint32_init_data(pc,x) { x=(*(pc++))<<24; x|=(*(pc++))<<16; x|=(*(pc++))<<8; x|=(*(pc++)); }
38 
39 /*-------------------------------------------------------------*/
40 #define NETWIB_DEBUG_MEMCORRUPT_ALIGNPAD(s) ( ( sizeof(void*) - ( s & (sizeof(void*) - 1) ) ) & (sizeof(void*) - 1) )
41 #define NETWIB_DEBUG_MEMCORRUPT_ALIGN(s) ((s + sizeof(void*) - 1) & ~((netwib_uintptr)(sizeof(void*) - 1)))
42 
43 
44 /*-------------------------------------------------------------*/
45 /*-------------------------------------------------------------*/
46 /* Those do not start with "netwib_debug" because it's too long */
47 typedef enum {
48   MEMCORRUPT_PATTERNTYPE_UNINIT = 1,
49   MEMCORRUPT_PATTERNTYPE_OUTER,
50   MEMCORRUPT_PATTERNTYPE_FREE,
51   MEMCORRUPT_PATTERNTYPE_PROTECTED
52 } memcorrupt_patterntype;
memcorrupt_pattern_init(netwib_data * ppc,netwib_uint32 patternsize,memcorrupt_patterntype patterntype)53 static netwib_err memcorrupt_pattern_init(netwib_data *ppc,
54                                           netwib_uint32 patternsize,
55                                           memcorrupt_patterntype patterntype)
56 {
57   netwib_byte c0, c1, c2;
58   netwib_uint32 i;
59   netwib_int32 j;
60   netwib_data pc;
61 
62 #if NETWIB_DEBUG_MEMCORRUPT_SPEEDUP == 1
63   if (patternsize > 64) {
64     netwib_uint32 skipsize, lastsize;
65     skipsize = patternsize - 64;
66     skipsize = 3*(skipsize/3);
67     netwib_er(memcorrupt_pattern_init(ppc, 30, patterntype));
68     *ppc += skipsize;
69     lastsize = patternsize - 30 - skipsize;
70     netwib_er(memcorrupt_pattern_init(ppc, lastsize, patterntype));
71     return(NETWIB_ERR_OK);
72   }
73 #endif
74 
75   c0 = c1 = c2 = 0;
76   switch (patterntype) {
77     case MEMCORRUPT_PATTERNTYPE_UNINIT :
78       c0 = NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C0;
79       c1 = NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C1;
80       c2 = NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C2;
81       break;
82     case MEMCORRUPT_PATTERNTYPE_OUTER :
83       c0 = NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C0;
84       c1 = NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C1;
85       c2 = NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C2;
86       break;
87     case MEMCORRUPT_PATTERNTYPE_FREE :
88       c0 = NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C0;
89       c1 = NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C1;
90       c2 = NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C2;
91       break;
92     case MEMCORRUPT_PATTERNTYPE_PROTECTED :
93       c0 = 0xCC;
94       c1 = 0xCC;
95       c2 = 0xCC;
96       break;
97   }
98 
99   pc = *ppc;
100   for (i = 0, j = 0; i < patternsize; i++, j++) {
101     if (j == 0)
102       *(pc++) = c0;
103     else if (j == 1)
104       *(pc++) = c1;
105     else if (j == 2) {
106       *(pc++) = c2;
107       j = -1;
108     }
109   }
110   *ppc = pc;
111 
112   return(NETWIB_ERR_OK);
113 }
114 
115 /*-------------------------------------------------------------*/
memcorrupt_pattern_check(netwib_conststring patternname,netwib_constdata * ppc,netwib_uint32 patternsize,memcorrupt_patterntype patterntype)116 static netwib_err memcorrupt_pattern_check(netwib_conststring patternname,
117                                            netwib_constdata *ppc,
118                                            netwib_uint32 patternsize,
119                                            memcorrupt_patterntype patterntype)
120 {
121   netwib_byte c0, c1, c2, c;
122   netwib_uint32 i;
123   netwib_int32 j;
124   netwib_constdata pc;
125 
126 #if NETWIB_DEBUG_MEMCORRUPT_SPEEDUP == 1
127   if (patternsize > 64) {
128     netwib_uint32 skipsize, lastsize;
129     skipsize = patternsize - 64;
130     skipsize = 3*(skipsize/3);
131     netwib_er(memcorrupt_pattern_check(patternname, ppc, 30, patterntype));
132     *ppc += skipsize;
133     lastsize = patternsize - 30 - skipsize;
134     netwib_er(memcorrupt_pattern_check(patternname, ppc, lastsize,patterntype));
135     return(NETWIB_ERR_OK);
136   }
137 #endif
138 
139   c0 = c1 = c2 = 0;
140   switch (patterntype) {
141     case MEMCORRUPT_PATTERNTYPE_UNINIT:
142       c0 = NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C0;
143       c1 = NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C1;
144       c2 = NETWIB_DEBUG_MEMCORRUPT_UNINITPATTERN_C2;
145       break;
146     case MEMCORRUPT_PATTERNTYPE_OUTER:
147       c0 = NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C0;
148       c1 = NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C1;
149       c2 = NETWIB_DEBUG_MEMCORRUPT_OUTERPATTERN_C2;
150       break;
151     case MEMCORRUPT_PATTERNTYPE_FREE:
152       c0 = NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C0;
153       c1 = NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C1;
154       c2 = NETWIB_DEBUG_MEMCORRUPT_FREEPATTERN_C2;
155       break;
156     case MEMCORRUPT_PATTERNTYPE_PROTECTED :
157       netwib_er(netwib_priv_errmsg_func_string("memcorrupt_pattern_check",
158                                              "unknown value for patterntype"));
159       return(NETWIB_ERR_LOINTERNALERROR);
160   }
161 
162   c = 0; /* for the compiler warning */
163   pc = *ppc;
164   for (i = 0, j = 0; i < patternsize; i++, j++) {
165     if (j == 0) {
166       if (*pc++ != c0) {
167         break;
168       }
169     } else if (j == 1) {
170       if (*pc++ != c1) {
171         break;
172       }
173     } else if (j == 2) {
174       if (*pc++ != c2) {
175         break;
176       }
177       j = -1;
178     }
179   }
180   if (i != patternsize) {
181     pc--;
182     netwib_er(netwib_debug_disp_fmt("%s : byte %{uint32} of pattern ",
183                                   patternname, i));
184     netwib_er(netwib_debug_disp_fmt("at %p has changed.\n", *ppc));
185     netwib_er(netwib_debug_disp_fmt("We found %{byte:02X} instead of ", *pc));
186     netwib_er(netwib_debug_disp_fmt("%{byte:02X} (pattern", c));
187     netwib_er(netwib_debug_disp_fmt("%{byte:02X}%{byte:02X}%{byte:02X}).\n",
188                                   c0, c1, c2));
189     netwib_er(netwib_debug_disp_string("The bad pattern is :\n"));
190     netwib_er(netwib_debug_disp_dump(*ppc, patternsize));
191     return(NETWIB_ERR_LOOBJUSECORRUPT);
192   }
193   *ppc = pc;
194 
195   return(NETWIB_ERR_OK);
196 }
197 
198 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_al_add2(netwib_constdata ptr)199 static netwib_err netwib_debug_memcorrupt_al_add2(netwib_constdata ptr)
200 {
201   netwib_uintptr alloclistsize;
202   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
203 
204   /* add in list */
205   alloclistsize = (netwib_uintptr) pglo->corrupt_alloclist[0];
206   if (alloclistsize == pglo->corrupt_alloclist_maxitems) {
207     pglo->corrupt_alloclist_maxitems += NETWIB_DEBUG_MEMCORRUPT_ALLOCCHUNKS;
208     pglo->corrupt_alloclist =
209       (const void **)realloc(NETWIB_DEBUG_CAST_FREE(pglo->corrupt_alloclist),
210                              (pglo->corrupt_alloclist_maxitems + 1) *
211                              sizeof(void *));
212     if (pglo->corrupt_alloclist == NULL) {
213       return(NETWIB_ERR_FUREALLOC);
214     }
215   }
216   alloclistsize++;
217   pglo->corrupt_alloclist[alloclistsize] = (const void *)ptr;
218   pglo->corrupt_alloclist[0] = (const void *)alloclistsize;
219   /*netwib_er(netwib_debug_disp_fmt("Set %p\n", ptr)); */
220   return(NETWIB_ERR_OK);
221 }
netwib_debug_memcorrupt_al_add(netwib_constdata ptr)222 static netwib_err netwib_debug_memcorrupt_al_add(netwib_constdata ptr)
223 {
224   netwib_err ret;
225   netwib_er(netwib_debug_glovars_wrlock());
226   ret = netwib_debug_memcorrupt_al_add2(ptr);
227   netwib_er(netwib_debug_glovars_wrunlock());
228   return(ret);
229 }
230 
231 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_al_del2(netwib_constdata ptr)232 static netwib_err netwib_debug_memcorrupt_al_del2(netwib_constdata ptr)
233 {
234   netwib_uintptr alloclistsize, i;
235   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
236 
237   alloclistsize = (netwib_uintptr) pglo->corrupt_alloclist[0];
238   if (alloclistsize == 0) {
239     return(NETWIB_ERR_LOOBJCLOSENOTINITIALIZED);
240   }
241   /*netwib_er(netwib_debug_disp_fmt("Del %p\n", ptr)); */
242 
243   for (i = 1; i <= alloclistsize; i++) {
244     if (ptr == pglo->corrupt_alloclist[i]) {
245       if (alloclistsize != 1) {
246         pglo->corrupt_alloclist[i] = pglo->corrupt_alloclist[alloclistsize];
247         pglo->corrupt_alloclist[alloclistsize] = NULL;
248       }
249       alloclistsize--;
250       pglo->corrupt_alloclist[0] = (const void *)alloclistsize;
251       return(NETWIB_ERR_OK);
252     }
253   }
254 
255   return(NETWIB_ERR_LOOBJCLOSENOTINITIALIZED);
256 }
257 /*
258 Not needed for the moment
259 static netwib_err netwib_debug_memcorrupt_al_del(netwib_constdata ptr);
260 static netwib_err netwib_debug_memcorrupt_al_del(netwib_constdata ptr)
261 {
262   netwib_err ret;
263   netwib_er(netwib_debug_glovars_wrlock());
264   ret = netwib_debug_memcorrupt_al_del2(ptr);
265   netwib_er(netwib_debug_glovars_wrunlock());
266   return(ret);
267 }
268 */
269 
270 /*-------------------------------------------------------------*/
271 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_init(void)272 netwib_err netwib_debug_memcorrupt_init(void)
273 {
274   int i;
275   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
276 
277   netwib_er(netwib_debug_glovars_wrlock());
278 
279   /* initialize for alloc */
280   pglo->corrupt_alloclist_maxitems = NETWIB_DEBUG_MEMCORRUPT_ALLOCCHUNKS;
281   pglo->corrupt_alloclist =
282     (const void **)malloc((pglo->corrupt_alloclist_maxitems + 1) *
283                           sizeof(void *));
284   if (pglo->corrupt_alloclist == NULL) {
285     netwib_er(netwib_debug_glovars_wrunlock());
286     return(NETWIB_ERR_FUMALLOC);
287   }
288   pglo->corrupt_alloclist[0] = (const void *)0;
289 
290   /* initialize for free */
291   pglo->corrupt_freelist_nextpost = 0;
292   for (i = 0; i < NETWIB_DEBUG_MEMCORRUPT_FREELIST_SIZE; i++) {
293     pglo->corrupt_freelist_ptr[i] = NULL;
294   }
295 
296   netwib_er(netwib_debug_glovars_wrunlock());
297   return(NETWIB_ERR_OK);
298 }
299 
300 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_close(void)301 netwib_err netwib_debug_memcorrupt_close(void)
302 {
303   netwib_data ptr;
304   netwib_uint32 ptrsize;
305   int i;
306   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
307 
308   netwib_er(netwib_debug_glovars_wrlock());
309 
310   /* close alloc */
311   free(NETWIB_DEBUG_CAST_FREE(pglo->corrupt_alloclist));
312   pglo->corrupt_alloclist = NULL;
313   pglo->corrupt_alloclist_maxitems = 0;
314 
315   /* close free */
316   for (i = 0; i < NETWIB_DEBUG_MEMCORRUPT_FREELIST_SIZE; i++) {
317     ptr = (netwib_data) pglo->corrupt_freelist_ptr[i];
318     ptrsize = pglo->corrupt_freelist_size[i];
319     if (ptr != NULL) {
320       /*netwib_er(netwib_debug_disp_fmt("Free %p\n", ptr)); */
321 #if NETWIB_DEBUG_MEM_CORRUPT==2 || NETWIB_DEBUG_MEM_CORRUPT==3
322       {
323         netwib_err ret;
324         ret = netwib_debug_memprotect_protect(ptr, ptrsize, NETWIB_TRUE);
325         if (ret != NETWIB_ERR_OK) {
326           netwib_er(netwib_debug_glovars_wrunlock());
327           return(ret);
328         }
329         ret = netwib_debug_memprotect_free(ptr, ptrsize);
330         if (ret != NETWIB_ERR_OK) {
331           netwib_er(netwib_debug_glovars_wrunlock());
332           return(ret);
333         }
334       }
335 #else
336       free(ptr);
337 #endif
338       pglo->corrupt_freelist_ptr[i] = NULL;
339     }
340   }
341   pglo->corrupt_freelist_nextpost = 0;
342 
343   netwib_er(netwib_debug_glovars_wrunlock());
344   return(NETWIB_ERR_OK);
345 }
346 
347 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_check(netwib_constptr ptr)348 netwib_err netwib_debug_memcorrupt_check(netwib_constptr ptr)
349 {
350   return(netwib_debug_memcorrupt_alloc_check(ptr, NULL, NULL, NULL, NULL));
351 }
352 
353 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_check_all(void)354 netwib_err netwib_debug_memcorrupt_check_all(void)
355 {
356   netwib_uintptr alloclistsize, i;
357   netwib_uint32 size;
358   const void *ptr;
359   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
360   netwib_err ret;
361 
362   netwib_er(netwib_debug_glovars_rdlock());
363 
364   /* check allocated data */
365   alloclistsize = (netwib_uintptr) pglo->corrupt_alloclist[0];
366   for (i = 1; i <= alloclistsize; i++) {
367     ptr = pglo->corrupt_alloclist[i];
368     /*netwib_er(netwib_debug_disp_fmt("CheckA %p\n", ptr)); */
369     ret = netwib_debug_memcorrupt_check((netwib_constdata) ptr);
370     if (ret != NETWIB_ERR_OK) {
371       netwib_er(netwib_debug_glovars_rdunlock());
372       return(ret);
373     }
374   }
375 
376   /* check freed data */
377   for (i = 0; i < NETWIB_DEBUG_MEMCORRUPT_FREELIST_SIZE; i++) {
378     ptr = pglo->corrupt_freelist_ptr[i];
379     size = pglo->corrupt_freelist_size[i];
380     /*netwib_er(netwib_debug_disp_fmt("CheckF %p %{uint32}\n", ptr, size)); */
381     if (ptr != NULL) {
382       ret = netwib_debug_memcorrupt_free_check((netwib_constdata)ptr, size);
383       if (ret != NETWIB_ERR_OK) {
384         netwib_er(netwib_debug_glovars_rdunlock());
385         return(ret);
386       }
387     }
388   }
389 
390   netwib_er(netwib_debug_glovars_rdunlock());
391   return(NETWIB_ERR_OK);
392 }
393 
394 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_check_periodic(void)395 netwib_err netwib_debug_memcorrupt_check_periodic(void)
396 {
397   static int i = 1;
398 
399   if (NETWIB_DEBUG_MEMCORRUPT_CHECK_FREQUENCY == 0)
400     return(NETWIB_ERR_OK);
401 
402   i++;
403   if (i > NETWIB_DEBUG_MEMCORRUPT_CHECK_FREQUENCY) {
404     i = 1;
405     return(netwib_debug_memcorrupt_check_all());
406   }
407 
408   return(NETWIB_ERR_OK);
409 }
410 
411 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_realloc(netwib_uint32 newallocsize,netwib_ptr * pptr)412 netwib_err netwib_debug_memcorrupt_realloc(netwib_uint32 newallocsize,
413                                            netwib_ptr *pptr)
414 {
415   netwib_ptr ptr;
416   netwib_uint32 allocsize;
417 
418   if (*pptr == NULL) {
419     return(netwib_debug_memcorrupt_alloc(newallocsize, pptr));
420   }
421 
422   netwib_er(netwib_debug_memcorrupt_alloc_check(*pptr, &allocsize,
423                                                 NULL, NULL, NULL));
424   netwib_er(netwib_debug_memcorrupt_alloc(newallocsize, &ptr));
425   if (newallocsize <= allocsize) {
426     netwib_c_memcpy(ptr, *pptr, newallocsize);
427   } else {
428     netwib_c_memcpy(ptr, *pptr, allocsize);
429     netwib_c_memset((netwib_data) ptr + allocsize, 0,
430                     newallocsize - allocsize);
431   }
432   netwib_er(netwib_debug_memcorrupt_free(pptr));
433 
434   if (pptr != NULL)
435     *pptr = ptr;
436   return(NETWIB_ERR_OK);
437 }
438 
439 /*-------------------------------------------------------------*/
netwib_debug_memcorrupt_add(netwib_ptr ptr)440 netwib_err netwib_debug_memcorrupt_add(netwib_ptr ptr)
441 {
442   netwib_er(netwib_debug_memcorrupt_al_add((netwib_constdata)ptr));
443   return(NETWIB_ERR_OK);
444 }
445 
446 
447 /*-------------------------------------------------------------*/
448 /*-------------------------------------------------------------*/
449 /*-------------------------------------------------------------*/
450 /*-------------------------------------------------------------*/
451 #if NETWIB_DEBUG_MEM_CORRUPT==1
452 /* ptroffset
453    pattern
454    allocsize smallpattern
455    allocsizexor smallpattern
456    data
457    smallpattern allocsize
458    pattern
459  Which is :
460    sizeof(netwib_uint32)+
461    patternsize+
462    sizeof(netwib_uint32)+MEMCORRUPT_SMALLPATTERN_SIZE+
463    sizeof(netwib_uint32)+MEMCORRUPT_SMALLPATTERN_SIZE+
464    allocsize+
465    MEMCORRUPT_SMALLPATTERN_SIZE+sizeof(netwib_uint32)+
466    patternsize;
467  */
netwib_debug_memcorrupt_alloc(netwib_uint32 allocsize,netwib_ptr * pptr)468 netwib_err netwib_debug_memcorrupt_alloc(netwib_uint32 allocsize,
469                                          netwib_ptr *pptr)
470 {
471   netwib_uint32 patternsize, ptroffset;
472   netwib_uint32 trueallocsize;
473   netwib_data trueptr, ptr, pc;
474 
475   /* compute ptroffset */
476   patternsize = allocsize + 2; /* this value migth increase if aligned */
477   ptroffset = patternsize + 3*sizeof(netwib_uint32) +
478     2*MEMCORRUPT_SMALLPATTERN_SIZE;
479 #if NETWIBDEF_ARCH_ALIGN==1
480   ptroffset = NETWIB_DEBUG_MEMCORRUPT_ALIGN(ptroffset);
481   patternsize = ptroffset -
482     (3*sizeof(netwib_uint32) + 2*MEMCORRUPT_SMALLPATTERN_SIZE);
483 #endif
484 
485   /* compute trueallocsize */
486   trueallocsize = ptroffset + allocsize + sizeof(netwib_uint32) +
487     MEMCORRUPT_SMALLPATTERN_SIZE + patternsize;
488 
489   /* allocate data */
490   trueptr = (netwib_data) malloc(trueallocsize);
491   if (trueptr == NULL)
492     return(NETWIB_ERR_FUMALLOC);
493 
494   /* set ptr */
495   ptr = trueptr + ptroffset;
496 
497   /* now, initialize trueptr */
498   pc = trueptr;
499   netwib_debug_mem_data_init_uint32(ptroffset, pc);
500   netwib_er(memcorrupt_pattern_init(&pc, patternsize,
501                                     MEMCORRUPT_PATTERNTYPE_OUTER));
502   netwib_debug_mem_data_init_uint32(allocsize, pc);
503   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
504                                     MEMCORRUPT_PATTERNTYPE_OUTER));
505   netwib_debug_mem_data_init_uint32(0xFFFFFFFFu ^ allocsize, pc);
506   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
507                                     MEMCORRUPT_PATTERNTYPE_OUTER));
508   netwib_er(memcorrupt_pattern_init(&pc, allocsize,
509                                     MEMCORRUPT_PATTERNTYPE_UNINIT));
510   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
511                                     MEMCORRUPT_PATTERNTYPE_OUTER));
512   netwib_debug_mem_data_init_uint32(allocsize, pc);
513   netwib_er(memcorrupt_pattern_init(&pc, patternsize,
514                                     MEMCORRUPT_PATTERNTYPE_OUTER));
515 
516   /* display */
517   /*netwib_er(netwib_debug_disp_dump(trueptr, trueallocsize)); */
518 
519   /* add in list */
520   netwib_er(netwib_debug_memcorrupt_al_add(ptr));
521 
522   /* set output */
523   *pptr = ptr;
524   return(NETWIB_ERR_OK);
525 }
526 #endif
527 
528 /*-------------------------------------------------------------*/
529 #if NETWIB_DEBUG_MEM_CORRUPT==1
netwib_debug_memcorrupt_free2(netwib_ptr * pptr)530 static netwib_err netwib_debug_memcorrupt_free2(netwib_ptr *pptr)
531 {
532   netwib_data trueptr, pc, ptr;
533   netwib_uint32 trueallocsize, ptrsize, nextpost;
534   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
535 
536   /* check it was not corrupted */
537   netwib_er(netwib_debug_memcorrupt_alloc_check(*pptr, NULL, NULL,
538                                                 (netwib_ptr *) &trueptr,
539                                                 &trueallocsize));
540 
541   /* remove it from the list */
542   netwib_er(netwib_debug_memcorrupt_al_del2((netwib_data)*pptr));
543 
544   /* empty data zone */
545   pc = trueptr;
546   netwib_er(memcorrupt_pattern_init(&pc, trueallocsize,
547                                     MEMCORRUPT_PATTERNTYPE_FREE));
548 
549   /* deal with saved freed data */
550   nextpost = pglo->corrupt_freelist_nextpost;
551   ptr = (netwib_data) pglo->corrupt_freelist_ptr[nextpost];
552   if (ptr != NULL) {
553     ptrsize = pglo->corrupt_freelist_size[nextpost];
554     netwib_er(netwib_debug_memcorrupt_free_check(ptr, ptrsize));
555     /*netwib_er(netwib_debug_disp_fmt("Free_ %p\n", ptr)); */
556     free(ptr);
557   }
558   pglo->corrupt_freelist_ptr[nextpost] = trueptr;
559   pglo->corrupt_freelist_size[nextpost] = trueallocsize;
560   nextpost++;
561   if (nextpost >= NETWIB_DEBUG_MEMCORRUPT_FREELIST_SIZE)
562     nextpost = 0;
563   pglo->corrupt_freelist_nextpost = nextpost;
564 
565   return(NETWIB_ERR_OK);
566 }
netwib_debug_memcorrupt_free(netwib_ptr * pptr)567 netwib_err netwib_debug_memcorrupt_free(netwib_ptr *pptr)
568 {
569   netwib_err ret;
570 
571   netwib_er(netwib_debug_glovars_wrlock());
572   ret = netwib_debug_memcorrupt_free2(pptr);
573   netwib_er(netwib_debug_glovars_wrunlock());
574 
575   return(ret);
576 }
577 #endif
578 
579 /*-------------------------------------------------------------*/
580 #if NETWIB_DEBUG_MEM_CORRUPT==1
netwib_debug_memcorrupt_alloc_check(netwib_constptr ptr,netwib_uint32 * pallocsize,netwib_uint32 * pptroffset,netwib_ptr * ptrueptr,netwib_uint32 * ptrueallocsize)581 netwib_err netwib_debug_memcorrupt_alloc_check(netwib_constptr ptr,
582                                                netwib_uint32 *pallocsize,
583                                                netwib_uint32 *pptroffset,
584                                                netwib_ptr *ptrueptr,
585                                                netwib_uint32 *ptrueallocsize)
586 {
587   netwib_uint32 patternsize, offset, readuint32;
588   netwib_uint32 ptroffset, allocsize;
589   netwib_constdata pc;
590 
591   /* obtain allocsize from bottom data */
592   offset = 2*sizeof(netwib_uint32) + 2*MEMCORRUPT_SMALLPATTERN_SIZE;
593   pc = (netwib_constdata) ptr - offset;
594   netwib_debug_mem_uint32_init_data(pc, allocsize);
595   netwib_er(memcorrupt_pattern_check("Outer_pattern -2", &pc,
596                                      MEMCORRUPT_SMALLPATTERN_SIZE,
597                                      MEMCORRUPT_PATTERNTYPE_OUTER));
598   netwib_debug_mem_uint32_init_data(pc, readuint32);
599   netwib_er(memcorrupt_pattern_check("Outer_pattern -1", &pc,
600                                      MEMCORRUPT_SMALLPATTERN_SIZE,
601                                      MEMCORRUPT_PATTERNTYPE_OUTER));
602   if (allocsize != (readuint32 ^ 0xFFFFFFFFu)) {
603     netwib_er(netwib_debug_disp_fmt("Allocsize(%{uint32:08X}) and allocsize",
604                                     allocsize));
605     netwib_er(netwib_debug_disp_fmt("(xor(%{uint32:08X})=%{uint32:08X}) ",
606                                     readuint32, readuint32 ^ 0xFFFFFFFFu));
607     netwib_er(netwib_debug_disp_fmt("are different.\nHere is allocsize+outer"));
608     netwib_er(netwib_debug_disp_fmt("pattern-2+allocsizexor+"));
609     netwib_er(netwib_debug_disp_fmt("outerpattern-1 :\n"));
610     netwib_er(netwib_debug_disp_dump((netwib_constdata) ptr - offset, offset));
611     netwib_er(netwib_debug_disp_fmt("Here is concerned memory (ptr=%p ", ptr));
612     netwib_er(netwib_debug_disp_fmt("of size %{uint32}) :\n", allocsize));
613     netwib_er(netwib_debug_disp_dump((netwib_constdata)ptr, allocsize));
614     return(NETWIB_ERR_LOOBJUSECORRUPT);
615   }
616 
617   /* check it corresponds with top data */
618   pc = (netwib_constdata) ptr + allocsize;
619   netwib_er(memcorrupt_pattern_check("Outer_pattern +1", &pc,
620                                      MEMCORRUPT_SMALLPATTERN_SIZE,
621                                      MEMCORRUPT_PATTERNTYPE_OUTER));
622   netwib_debug_mem_uint32_init_data(pc, readuint32);
623   if (allocsize != readuint32) {
624     netwib_er(netwib_debug_disp_fmt("Bottom allocsize(%{uint32:08X}) and top ",
625                                     allocsize));
626     netwib_er(netwib_debug_disp_fmt("allocsize(%{uint32:08X}) are different.\n",
627                                     readuint32));
628     netwib_er(netwib_debug_disp_fmt("Here is outerpattern+1+allocsize :\n"));
629     netwib_er(netwib_debug_disp_dump((netwib_constdata) ptr + allocsize +
630                                      MEMCORRUPT_SMALLPATTERN_SIZE, offset));
631     netwib_er(netwib_debug_disp_fmt("Here is concerned memory (ptr=%p ", ptr));
632     netwib_er(netwib_debug_disp_fmt("of size %{uint32}) :\n", allocsize));
633     netwib_er(netwib_debug_disp_dump((netwib_constdata)ptr, allocsize));
634     return(NETWIB_ERR_LOOBJUSECORRUPT);
635   }
636 
637   /* now check bottom patternsize */
638   patternsize = allocsize + 2;
639   offset = patternsize + 3*sizeof(netwib_uint32) +
640     2*MEMCORRUPT_SMALLPATTERN_SIZE;
641 #if NETWIBDEF_ARCH_ALIGN==1
642   offset = NETWIB_DEBUG_MEMCORRUPT_ALIGN(offset);
643   patternsize = offset - 28;
644 #endif
645   pc = (netwib_constdata) ptr - offset;
646   netwib_debug_mem_uint32_init_data(pc, ptroffset);
647   netwib_er(memcorrupt_pattern_check("Outer_pattern -3", &pc,
648                                      patternsize,
649                                      MEMCORRUPT_PATTERNTYPE_OUTER));
650   if (ptroffset != offset) {
651     netwib_er(netwib_debug_disp_fmt("Ptroffset(%{uint32:08X}) and computer ",
652                                     ptroffset));
653     netwib_er(netwib_debug_disp_fmt("offset(%{uint32:08X}) are different.\n",
654                                     offset));
655     netwib_er(netwib_debug_disp_fmt("Here is concerned memory (ptr=%p ", ptr));
656     netwib_er(netwib_debug_disp_fmt("of size %{uint32}) :\n", allocsize));
657     netwib_er(netwib_debug_disp_dump((netwib_constdata)ptr, allocsize));
658     return(NETWIB_ERR_LOOBJUSECORRUPT);
659   }
660 
661   /* now check top patternsize */
662   pc = (netwib_constdata) ptr + allocsize + sizeof(netwib_uint32) +
663     MEMCORRUPT_SMALLPATTERN_SIZE;
664   netwib_er(memcorrupt_pattern_check("Outer_pattern +2", &pc,
665                                      patternsize,
666                                      MEMCORRUPT_PATTERNTYPE_OUTER));
667 
668   /* set output */
669   if (pallocsize != NULL)
670     *pallocsize = allocsize;
671   if (pptroffset != NULL)
672     *pptroffset = ptroffset;
673   if (ptrueptr != NULL) {
674     *ptrueptr = (netwib_data) ((netwib_uintptr)ptr - ptroffset);
675   }
676   if (ptrueallocsize != NULL) {
677     *ptrueallocsize = ptroffset + allocsize + sizeof(netwib_uint32) +
678       MEMCORRUPT_SMALLPATTERN_SIZE + patternsize;
679   }
680   return(NETWIB_ERR_OK);
681 }
682 #endif
683 
684 /*-------------------------------------------------------------*/
685 #if NETWIB_DEBUG_MEM_CORRUPT==1
netwib_debug_memcorrupt_free_check(netwib_constptr trueptr,netwib_uint32 trueallocsize)686 netwib_err netwib_debug_memcorrupt_free_check(netwib_constptr trueptr,
687                                               netwib_uint32 trueallocsize)
688 {
689   netwib_constdata pc;
690 
691   pc = (netwib_constdata)trueptr;
692   netwib_er(memcorrupt_pattern_check("Free", &pc, trueallocsize,
693                                      MEMCORRUPT_PATTERNTYPE_FREE));
694 
695   return(NETWIB_ERR_OK);
696 }
697 #endif
698 
699 /*-------------------------------------------------------------*/
700 /*-------------------------------------------------------------*/
701 /*-------------------------------------------------------------*/
702 /*-------------------------------------------------------------*/
703 #if NETWIB_DEBUG_MEM_CORRUPT==2 || NETWIB_DEBUG_MEM_CORRUPT==3
704 /* bottom_pattern
705    protected_memory
706    middle_pattern
707    bottom_pattern_size
708    protected_memory_size
709    middle_pattern_size
710    top_pattern_size
711    allocsize smallpattern
712    allocsizexor smallpattern
713    data
714    protected_memory
715    top_pattern
716 */
717 /* bottom_pattern
718    protected_memory
719    allocsize smallpattern
720    allocsizexor smallpattern
721    data
722    smallpattern
723    bottom_pattern_size
724    protected_memory_size
725    middle_pattern_size
726    top_pattern_size
727    middle_pattern
728    protected_memory
729    top_pattern
730 */
netwib_debug_memcorrupt_alloc(netwib_uint32 allocsize,netwib_ptr * pptr)731 netwib_err netwib_debug_memcorrupt_alloc(netwib_uint32 allocsize,
732                                          netwib_ptr *pptr)
733 {
734   netwib_uint32 protectedpages, protectedsize, checkzonesize;
735   netwib_uint32 middlepages, middlesize, middlepatternsize, middlefixedsize;
736   netwib_uint32 bottompatternsize, toppatternsize, tmpvar, trueallocsize;
737   netwib_uint32 alignpad;
738   netwib_uintptr tmpvarptr;
739   netwib_data trueptr, ptr, pc;
740 
741   /* compute protectedsize */
742   protectedsize = allocsize + 2;
743   /* number of memory pages is a multiple of NETWIB_PRIV_PAGESIZE */
744   protectedpages = 1 + (protectedsize - 1) / NETWIB_PRIV_PAGESIZE;
745   protectedsize = protectedpages * NETWIB_PRIV_PAGESIZE;
746 
747   /* compute the necessary alignment pad */
748 #if NETWIB_DEBUG_MEM_CORRUPT==2 && NETWIBDEF_ARCH_ALIGN==1
749   /* note : if NETWIB_DEBUG_MEM_CORRUPT==3, it is aligned, so alignpad=0 */
750   alignpad = NETWIB_DEBUG_MEMCORRUPT_ALIGNPAD(allocsize);
751 #else
752   alignpad = 0;
753 #endif
754 
755   /* middle pages must be aligned on a NETWIB_PRIV_PAGESIZE boundary */
756   checkzonesize = 4 * sizeof(netwib_uint32) +
757     2*sizeof(netwib_uint32) + 2*MEMCORRUPT_SMALLPATTERN_SIZE;
758 #if NETWIB_DEBUG_MEM_CORRUPT==3
759   checkzonesize += MEMCORRUPT_SMALLPATTERN_SIZE;
760 #endif
761   middlefixedsize = allocsize + alignpad + checkzonesize;
762   tmpvar = middlefixedsize % NETWIB_PRIV_PAGESIZE;
763   middlepatternsize = (tmpvar == 0) ? 0 : NETWIB_PRIV_PAGESIZE - tmpvar;
764   middlesize = middlepatternsize + middlefixedsize;
765   middlepages = middlesize / NETWIB_PRIV_PAGESIZE;
766 
767   /* compute trueallocsize */
768   tmpvar = 2 * protectedpages + middlepages;
769 #if defined NETWIBDEF_SYSNAME_Unix
770   trueallocsize = tmpvar * NETWIB_PRIV_PAGESIZE + NETWIB_PRIV_PAGESIZE - 1;
771 #elif defined NETWIBDEF_SYSNAME_Windows
772   trueallocsize = tmpvar * NETWIB_PRIV_PAGESIZE;
773 #endif
774 
775   /* allocate data */
776   netwib_er(netwib_debug_memprotect_alloc(trueallocsize,
777                                           (netwib_ptr *)&trueptr));
778 
779   /* compute bottom pattern size (aligned) */
780   tmpvarptr = NETWIB_PRIV_PAGESIZE - 1;
781   pc = (netwib_data) (((netwib_uintptr)trueptr + tmpvarptr) & (~tmpvarptr));
782   bottompatternsize = pc - trueptr;
783 
784   /* compute top pattern size */
785   tmpvar = trueallocsize % NETWIB_PRIV_PAGESIZE;
786   toppatternsize = tmpvar - bottompatternsize;
787 
788   /* set ptr */
789 #if NETWIB_DEBUG_MEM_CORRUPT==2
790   ptr = pc + protectedsize + middlepatternsize + checkzonesize;
791 #elif NETWIB_DEBUG_MEM_CORRUPT==3
792   ptr = pc + protectedsize +
793     2 * (sizeof(netwib_uint32) + MEMCORRUPT_SMALLPATTERN_SIZE);
794 #endif
795 
796   /* now, initialize trueptr */
797   pc = trueptr;
798   netwib_er(memcorrupt_pattern_init(&pc, bottompatternsize,
799                                     MEMCORRUPT_PATTERNTYPE_OUTER));
800   netwib_er(netwib_debug_memprotect_protect(pc,
801                                             protectedpages*NETWIB_PRIV_PAGESIZE,
802                                             NETWIB_FALSE));
803   pc += protectedpages * NETWIB_PRIV_PAGESIZE;
804 #if NETWIB_DEBUG_MEM_CORRUPT==2
805   netwib_er(memcorrupt_pattern_init(&pc, middlepatternsize,
806                                     MEMCORRUPT_PATTERNTYPE_OUTER));
807   netwib_debug_mem_data_init_uint32(bottompatternsize, pc);
808   netwib_debug_mem_data_init_uint32(protectedsize, pc);
809   netwib_debug_mem_data_init_uint32(middlepatternsize, pc);
810   netwib_debug_mem_data_init_uint32(toppatternsize, pc);
811   netwib_debug_mem_data_init_uint32(allocsize, pc);
812   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
813                                     MEMCORRUPT_PATTERNTYPE_OUTER));
814   netwib_debug_mem_data_init_uint32(0xFFFFFFFFu ^ allocsize, pc);
815   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
816                                     MEMCORRUPT_PATTERNTYPE_OUTER));
817   netwib_er(memcorrupt_pattern_init(&pc, allocsize,
818                                     MEMCORRUPT_PATTERNTYPE_UNINIT));
819   netwib_er(memcorrupt_pattern_init(&pc, alignpad,
820                                     MEMCORRUPT_PATTERNTYPE_OUTER));
821 #elif NETWIB_DEBUG_MEM_CORRUPT==3
822   netwib_debug_mem_data_init_uint32(allocsize, pc);
823   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
824                                     MEMCORRUPT_PATTERNTYPE_OUTER));
825   netwib_debug_mem_data_init_uint32(0xFFFFFFFFu ^ allocsize, pc);
826   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
827                                     MEMCORRUPT_PATTERNTYPE_OUTER));
828   netwib_er(memcorrupt_pattern_init(&pc, allocsize,
829                                     MEMCORRUPT_PATTERNTYPE_UNINIT));
830   netwib_er(memcorrupt_pattern_init(&pc, MEMCORRUPT_SMALLPATTERN_SIZE,
831                                     MEMCORRUPT_PATTERNTYPE_OUTER));
832   netwib_debug_mem_data_init_uint32(bottompatternsize, pc);
833   netwib_debug_mem_data_init_uint32(protectedsize, pc);
834   netwib_debug_mem_data_init_uint32(middlepatternsize, pc);
835   netwib_debug_mem_data_init_uint32(toppatternsize, pc);
836   netwib_er(memcorrupt_pattern_init(&pc, middlepatternsize,
837                                     MEMCORRUPT_PATTERNTYPE_OUTER));
838 #endif
839   netwib_er(netwib_debug_memprotect_protect(pc,
840                                             protectedpages*NETWIB_PRIV_PAGESIZE,
841                                             NETWIB_FALSE));
842   pc += protectedpages * NETWIB_PRIV_PAGESIZE;
843   netwib_er(memcorrupt_pattern_init(&pc, toppatternsize,
844                                     MEMCORRUPT_PATTERNTYPE_OUTER));
845 
846   /* display */
847   /*netwib_er(netwib_debug_disp_dump(trueptr, trueallocsize)); */
848 
849   /* add in list */
850   netwib_er(netwib_debug_memcorrupt_al_add(ptr));
851 
852   /* set output */
853   *pptr = ptr;
854   return(NETWIB_ERR_OK);
855 }
856 #endif
857 
858 /*-------------------------------------------------------------*/
859 #if NETWIB_DEBUG_MEM_CORRUPT==2 || NETWIB_DEBUG_MEM_CORRUPT==3
netwib_debug_memcorrupt_free2(netwib_ptr * pptr)860 static netwib_err netwib_debug_memcorrupt_free2(netwib_ptr *pptr)
861 {
862   netwib_ptr trueptr, ptr;
863   netwib_uint32 trueallocsize, ptrsize, nextpost;
864   netwib_debug_glovars_t *pglo = &netwib_debug_glovars;
865 
866   /* check it was not corrupted */
867   netwib_er(netwib_debug_memcorrupt_alloc_check(*pptr, NULL, NULL,
868                                                 &trueptr, &trueallocsize));
869 
870   /* remove it from the list */
871   netwib_er(netwib_debug_memcorrupt_al_del2((netwib_constdata)*pptr));
872 
873   /* set all pages as protected */
874   netwib_er(netwib_debug_memprotect_protect(trueptr, trueallocsize,
875                                             NETWIB_FALSE));
876 
877   /* deal with saved freed data */
878   nextpost = pglo->corrupt_freelist_nextpost;
879   ptr = (netwib_data) pglo->corrupt_freelist_ptr[nextpost];
880   if (ptr != NULL) {
881     ptrsize = pglo->corrupt_freelist_size[nextpost];
882     netwib_er(netwib_debug_memcorrupt_free_check(ptr, ptrsize));
883     netwib_er(netwib_debug_memprotect_protect(ptr, ptrsize, NETWIB_TRUE));
884     netwib_er(netwib_debug_memprotect_free(ptr, ptrsize));
885   }
886   pglo->corrupt_freelist_ptr[nextpost] = trueptr;
887   pglo->corrupt_freelist_size[nextpost] = trueallocsize;
888   nextpost++;
889   if (nextpost >= NETWIB_DEBUG_MEMCORRUPT_FREELIST_SIZE)
890     nextpost = 0;
891   pglo->corrupt_freelist_nextpost = nextpost;
892 
893   return(NETWIB_ERR_OK);
894 }
netwib_debug_memcorrupt_free(netwib_ptr * pptr)895 netwib_err netwib_debug_memcorrupt_free(netwib_ptr *pptr)
896 {
897   netwib_err ret;
898 
899   netwib_er(netwib_debug_glovars_wrlock());
900   ret = netwib_debug_memcorrupt_free2(pptr);
901   netwib_er(netwib_debug_glovars_wrunlock());
902 
903   return(ret);
904 }
905 #endif
906 
907 /*-------------------------------------------------------------*/
908 #if NETWIB_DEBUG_MEM_CORRUPT==2 || NETWIB_DEBUG_MEM_CORRUPT==3
netwib_debug_memcorrupt_alloc_check(netwib_constptr ptr,netwib_uint32 * pallocsize,netwib_uint32 * pptroffset,netwib_ptr * ptrueptr,netwib_uint32 * ptrueallocsize)909 netwib_err netwib_debug_memcorrupt_alloc_check(netwib_constptr ptr,
910                                                netwib_uint32 *pallocsize,
911                                                netwib_uint32 *pptroffset,
912                                                netwib_ptr *ptrueptr,
913                                                netwib_uint32 *ptrueallocsize)
914 {
915   netwib_uint32 readuint32, checkzonesize2, checkzonesize4;
916   netwib_uint32 ptroffset, allocsize, bottompatternsize, trueallocsize;
917   netwib_uint32 protectedsize, middlepatternsize, toppatternsize;
918   netwib_uint32 alignpad;
919   netwib_constdata pc;
920 
921   /* obtain allocsize from bottom data */
922   checkzonesize2 = 2 * (sizeof(netwib_uint32) +
923                         MEMCORRUPT_SMALLPATTERN_SIZE);
924   checkzonesize4 = 4 * sizeof(netwib_uint32);
925 #if NETWIB_DEBUG_MEM_CORRUPT==3
926   checkzonesize4 += MEMCORRUPT_SMALLPATTERN_SIZE;
927 #endif
928   pc = (netwib_constdata) ptr - checkzonesize2;
929   netwib_debug_mem_uint32_init_data(pc, allocsize);
930   netwib_er(memcorrupt_pattern_check("Outer_pattern -2", &pc,
931                                      MEMCORRUPT_SMALLPATTERN_SIZE,
932                                      MEMCORRUPT_PATTERNTYPE_OUTER));
933   netwib_debug_mem_uint32_init_data(pc, readuint32);
934   netwib_er(memcorrupt_pattern_check("Outer_pattern -1", &pc,
935                                      MEMCORRUPT_SMALLPATTERN_SIZE,
936                                      MEMCORRUPT_PATTERNTYPE_OUTER));
937   if (allocsize != (readuint32 ^ 0xFFFFFFFFu)) {
938     netwib_er(netwib_debug_disp_fmt("Allocsize(%{uint32:08X}) and allocsize(",
939                                     allocsize));
940     netwib_er(netwib_debug_disp_fmt("xor(%{uint32:08X})=%{uint32:08X}) are ",
941                                     readuint32));
942     netwib_er(netwib_debug_disp_fmt("different.\n", readuint32 ^ 0xFFFFFFFFu));
943     netwib_er(netwib_debug_disp_fmt("Here is allocsize+outerpattern-2+alloc"));
944     netwib_er(netwib_debug_disp_fmt("sizexor+outerpattern-1 :\n"));
945     netwib_er(netwib_debug_disp_dump((netwib_constdata) ptr - checkzonesize2,
946                                      checkzonesize2));
947     return(NETWIB_ERR_LOOBJUSECORRUPT);
948   }
949 
950   /* compute the necessary alignment pad */
951 #if NETWIB_DEBUG_MEM_CORRUPT==2 && NETWIBDEF_ARCH_ALIGN==1
952   /* note : if NETWIB_DEBUG_MEM_CORRUPT==3, it is aligned, so alignpad=0 */
953   alignpad = NETWIB_DEBUG_MEMCORRUPT_ALIGNPAD(allocsize);
954 #else
955   alignpad = 0;
956 #endif
957 
958   /* obtain other values */
959 #if NETWIB_DEBUG_MEM_CORRUPT==2
960   pc = (netwib_constdata) ptr - checkzonesize2 - checkzonesize4;
961 #elif NETWIB_DEBUG_MEM_CORRUPT==3
962   pc = (netwib_constdata) ptr + allocsize;
963   netwib_er(memcorrupt_pattern_check("Outer_pattern +1", &pc,
964                                      MEMCORRUPT_SMALLPATTERN_SIZE,
965                                      MEMCORRUPT_PATTERNTYPE_OUTER));
966 #endif
967   netwib_debug_mem_uint32_init_data(pc, bottompatternsize);
968   netwib_debug_mem_uint32_init_data(pc, protectedsize);
969   netwib_debug_mem_uint32_init_data(pc, middlepatternsize);
970   netwib_debug_mem_uint32_init_data(pc, toppatternsize);
971   /* compute those values */
972 #if NETWIB_DEBUG_MEM_CORRUPT==2
973   ptroffset = bottompatternsize + protectedsize + middlepatternsize +
974               checkzonesize4 + checkzonesize2;
975   trueallocsize = ptroffset + allocsize + alignpad + protectedsize +
976                   toppatternsize;
977 #elif NETWIB_DEBUG_MEM_CORRUPT==3
978   ptroffset = bottompatternsize + protectedsize + checkzonesize2;
979   trueallocsize = ptroffset + allocsize + checkzonesize4 +
980                   middlepatternsize + protectedsize + toppatternsize;
981 #endif
982 
983   /* check memory */
984   pc = (netwib_constdata) ptr - ptroffset;
985 #if NETWIB_DEBUG_MEM_CORRUPT==2
986   netwib_er(memcorrupt_pattern_check("Outer_pattern -4", &pc,
987                                      bottompatternsize,
988                                      MEMCORRUPT_PATTERNTYPE_OUTER));
989   pc += protectedsize;
990   netwib_er(memcorrupt_pattern_check("Outer_pattern -3", &pc,
991                                      middlepatternsize,
992                                      MEMCORRUPT_PATTERNTYPE_OUTER));
993   pc += checkzonesize4 + checkzonesize2 + allocsize;
994   netwib_er(memcorrupt_pattern_check("Outer_pattern +1b", &pc,
995                                      alignpad,
996                                      MEMCORRUPT_PATTERNTYPE_OUTER));
997   pc += protectedsize;
998   netwib_er(memcorrupt_pattern_check("Outer_pattern +1", &pc,
999                                      toppatternsize,
1000                                      MEMCORRUPT_PATTERNTYPE_OUTER));
1001 #elif NETWIB_DEBUG_MEM_CORRUPT==3
1002   netwib_er(memcorrupt_pattern_check("Outer_pattern -3", &pc,
1003                                      bottompatternsize,
1004                                      MEMCORRUPT_PATTERNTYPE_OUTER));
1005   pc += protectedsize + checkzonesize2 + allocsize + checkzonesize4;
1006   netwib_er(memcorrupt_pattern_check("Outer_pattern +2", &pc,
1007                                      middlepatternsize,
1008                                      MEMCORRUPT_PATTERNTYPE_OUTER));
1009   pc += protectedsize;
1010   netwib_er(memcorrupt_pattern_check("Outer_pattern +3", &pc,
1011                                      toppatternsize,
1012                                      MEMCORRUPT_PATTERNTYPE_OUTER));
1013 #endif
1014 
1015   /* set output */
1016   if (pallocsize != NULL)
1017     *pallocsize = allocsize;
1018   if (pptroffset != NULL)
1019     *pptroffset = ptroffset;
1020   if (ptrueptr != NULL)
1021     *ptrueptr = (netwib_data) ((netwib_uintptr) ptr - ptroffset);
1022   if (ptrueallocsize != NULL)
1023     *ptrueallocsize = trueallocsize;
1024   return(NETWIB_ERR_OK);
1025 }
1026 #endif
1027 
1028 /*-------------------------------------------------------------*/
1029 #if NETWIB_DEBUG_MEM_CORRUPT==2 || NETWIB_DEBUG_MEM_CORRUPT==3
netwib_debug_memcorrupt_free_check(netwib_constptr trueptr,netwib_uint32 trueallocsize)1030 netwib_err netwib_debug_memcorrupt_free_check(netwib_constptr trueptr,
1031                                               netwib_uint32 trueallocsize)
1032 {
1033   /* there is no check in this case */
1034   trueptr = trueptr; /* for compiler warning */
1035   trueallocsize = trueallocsize; /* for compiler warning */
1036   return(NETWIB_ERR_OK);
1037 }
1038 #endif
1039