ptr_chck.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. */
  13. /*
  14. * These are checking routines calls to which could be inserted by a
  15. * preprocessor to validate C pointer arithmetic.
  16. */
  17. #include "private/gc_pmark.h"
  18. #ifdef __STDC__
  19. void GC_default_same_obj_print_proc(GC_PTR p, GC_PTR q)
  20. #else
  21. void GC_default_same_obj_print_proc (p, q)
  22. GC_PTR p, q;
  23. #endif
  24. {
  25. GC_err_printf2("0x%lx and 0x%lx are not in the same object\n",
  26. (unsigned long)p, (unsigned long)q);
  27. ABORT("GC_same_obj test failed");
  28. }
  29. void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR, GC_PTR))
  30. = GC_default_same_obj_print_proc;
  31. /* Check that p and q point to the same object. Call */
  32. /* *GC_same_obj_print_proc if they don't. */
  33. /* Returns the first argument. (Return value may be hard */
  34. /* to use,due to typing issues. But if we had a suitable */
  35. /* preprocessor ...) */
  36. /* Succeeds if neither p nor q points to the heap. */
  37. /* We assume this is performance critical. (It shouldn't */
  38. /* be called by production code, but this can easily make */
  39. /* debugging intolerably slow.) */
  40. #ifdef __STDC__
  41. GC_PTR GC_same_obj(register void *p, register void *q)
  42. #else
  43. GC_PTR GC_same_obj(p, q)
  44. register char *p, *q;
  45. #endif
  46. {
  47. register struct hblk *h;
  48. register hdr *hhdr;
  49. register ptr_t base, limit;
  50. register word sz;
  51. if (!GC_is_initialized) GC_init();
  52. hhdr = HDR((word)p);
  53. if (hhdr == 0) {
  54. if (divHBLKSZ((word)p) != divHBLKSZ((word)q)
  55. && HDR((word)q) != 0) {
  56. goto fail;
  57. }
  58. return(p);
  59. }
  60. /* If it's a pointer to the middle of a large object, move it */
  61. /* to the beginning. */
  62. if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
  63. h = HBLKPTR(p) - (word)hhdr;
  64. hhdr = HDR(h);
  65. while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
  66. h = FORWARDED_ADDR(h, hhdr);
  67. hhdr = HDR(h);
  68. }
  69. limit = (ptr_t)((word *)h + hhdr -> hb_sz);
  70. if ((ptr_t)p >= limit || (ptr_t)q >= limit || (ptr_t)q < (ptr_t)h ) {
  71. goto fail;
  72. }
  73. return(p);
  74. }
  75. sz = WORDS_TO_BYTES(hhdr -> hb_sz);
  76. if (sz > MAXOBJBYTES) {
  77. base = (ptr_t)HBLKPTR(p);
  78. limit = base + sz;
  79. if ((ptr_t)p >= limit) {
  80. goto fail;
  81. }
  82. } else {
  83. register int map_entry;
  84. register int pdispl = HBLKDISPL(p);
  85. map_entry = MAP_ENTRY((hhdr -> hb_map), pdispl);
  86. if (map_entry > CPP_MAX_OFFSET) {
  87. map_entry = BYTES_TO_WORDS(pdispl) % BYTES_TO_WORDS(sz);
  88. if (HBLKPTR(p) != HBLKPTR(q)) goto fail;
  89. /* W/o this check, we might miss an error if */
  90. /* q points to the first object on a page, and */
  91. /* points just before the page. */
  92. }
  93. base = (char *)((word)p & ~(WORDS_TO_BYTES(1) - 1));
  94. base -= WORDS_TO_BYTES(map_entry);
  95. limit = base + sz;
  96. }
  97. /* [base, limit) delimits the object containing p, if any. */
  98. /* If p is not inside a valid object, then either q is */
  99. /* also outside any valid object, or it is outside */
  100. /* [base, limit). */
  101. if ((ptr_t)q >= limit || (ptr_t)q < base) {
  102. goto fail;
  103. }
  104. return(p);
  105. fail:
  106. (*GC_same_obj_print_proc)((ptr_t)p, (ptr_t)q);
  107. return(p);
  108. }
  109. #ifdef __STDC__
  110. void GC_default_is_valid_displacement_print_proc (GC_PTR p)
  111. #else
  112. void GC_default_is_valid_displacement_print_proc (p)
  113. GC_PTR p;
  114. #endif
  115. {
  116. GC_err_printf1("0x%lx does not point to valid object displacement\n",
  117. (unsigned long)p);
  118. ABORT("GC_is_valid_displacement test failed");
  119. }
  120. void (*GC_is_valid_displacement_print_proc) GC_PROTO((GC_PTR)) =
  121. GC_default_is_valid_displacement_print_proc;
  122. /* Check that if p is a pointer to a heap page, then it points to */
  123. /* a valid displacement within a heap object. */
  124. /* Uninteresting with GC_all_interior_pointers. */
  125. /* Always returns its argument. */
  126. /* Note that we don't lock, since nothing relevant about the header */
  127. /* should change while we have a valid object pointer to the block. */
  128. #ifdef __STDC__
  129. void * GC_is_valid_displacement(void *p)
  130. #else
  131. char *GC_is_valid_displacement(p)
  132. char *p;
  133. #endif
  134. {
  135. register hdr *hhdr;
  136. register word pdispl;
  137. register struct hblk *h;
  138. register map_entry_type map_entry;
  139. register word sz;
  140. if (!GC_is_initialized) GC_init();
  141. hhdr = HDR((word)p);
  142. if (hhdr == 0) return(p);
  143. h = HBLKPTR(p);
  144. if (GC_all_interior_pointers) {
  145. while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
  146. h = FORWARDED_ADDR(h, hhdr);
  147. hhdr = HDR(h);
  148. }
  149. }
  150. if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
  151. goto fail;
  152. }
  153. sz = WORDS_TO_BYTES(hhdr -> hb_sz);
  154. pdispl = HBLKDISPL(p);
  155. map_entry = MAP_ENTRY((hhdr -> hb_map), pdispl);
  156. if (map_entry == OBJ_INVALID
  157. || sz > MAXOBJBYTES && (ptr_t)p >= (ptr_t)h + sz) {
  158. goto fail;
  159. }
  160. return(p);
  161. fail:
  162. (*GC_is_valid_displacement_print_proc)((ptr_t)p);
  163. return(p);
  164. }
  165. #ifdef __STDC__
  166. void GC_default_is_visible_print_proc(GC_PTR p)
  167. #else
  168. void GC_default_is_visible_print_proc(p)
  169. GC_PTR p;
  170. #endif
  171. {
  172. GC_err_printf1("0x%lx is not a GC visible pointer location\n",
  173. (unsigned long)p);
  174. ABORT("GC_is_visible test failed");
  175. }
  176. void (*GC_is_visible_print_proc) GC_PROTO((GC_PTR p)) =
  177. GC_default_is_visible_print_proc;
  178. /* Could p be a stack address? */
  179. GC_bool GC_on_stack(p)
  180. ptr_t p;
  181. {
  182. # ifdef THREADS
  183. return(TRUE);
  184. # else
  185. int dummy;
  186. # ifdef STACK_GROWS_DOWN
  187. if ((ptr_t)p >= (ptr_t)(&dummy) && (ptr_t)p < GC_stackbottom ) {
  188. return(TRUE);
  189. }
  190. # else
  191. if ((ptr_t)p <= (ptr_t)(&dummy) && (ptr_t)p > GC_stackbottom ) {
  192. return(TRUE);
  193. }
  194. # endif
  195. return(FALSE);
  196. # endif
  197. }
  198. /* Check that p is visible */
  199. /* to the collector as a possibly pointer containing location. */
  200. /* If it isn't invoke *GC_is_visible_print_proc. */
  201. /* Returns the argument in all cases. May erroneously succeed */
  202. /* in hard cases. (This is intended for debugging use with */
  203. /* untyped allocations. The idea is that it should be possible, though */
  204. /* slow, to add such a call to all indirect pointer stores.) */
  205. /* Currently useless for multithreaded worlds. */
  206. #ifdef __STDC__
  207. void * GC_is_visible(void *p)
  208. #else
  209. char *GC_is_visible(p)
  210. char *p;
  211. #endif
  212. {
  213. register hdr *hhdr;
  214. if ((word)p & (ALIGNMENT - 1)) goto fail;
  215. if (!GC_is_initialized) GC_init();
  216. # ifdef THREADS
  217. hhdr = HDR((word)p);
  218. if (hhdr != 0 && GC_base(p) == 0) {
  219. goto fail;
  220. } else {
  221. /* May be inside thread stack. We can't do much. */
  222. return(p);
  223. }
  224. # else
  225. /* Check stack first: */
  226. if (GC_on_stack(p)) return(p);
  227. hhdr = HDR((word)p);
  228. if (hhdr == 0) {
  229. GC_bool result;
  230. if (GC_is_static_root(p)) return(p);
  231. /* Else do it again correctly: */
  232. # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || \
  233. defined(MSWINCE) || defined (CYGWIN32) || defined(PCR)) \
  234. && !defined(SRC_M3)
  235. DISABLE_SIGNALS();
  236. GC_register_dynamic_libraries();
  237. result = GC_is_static_root(p);
  238. ENABLE_SIGNALS();
  239. if (result) return(p);
  240. # endif
  241. goto fail;
  242. } else {
  243. /* p points to the heap. */
  244. word descr;
  245. ptr_t base = GC_base(p); /* Should be manually inlined? */
  246. if (base == 0) goto fail;
  247. if (HBLKPTR(base) != HBLKPTR(p)) hhdr = HDR((word)p);
  248. descr = hhdr -> hb_descr;
  249. retry:
  250. switch(descr & GC_DS_TAGS) {
  251. case GC_DS_LENGTH:
  252. if ((word)((ptr_t)p - (ptr_t)base) > (word)descr) goto fail;
  253. break;
  254. case GC_DS_BITMAP:
  255. if ((ptr_t)p - (ptr_t)base
  256. >= WORDS_TO_BYTES(BITMAP_BITS)
  257. || ((word)p & (sizeof(word) - 1))) goto fail;
  258. if (!((1 << (WORDSZ - ((ptr_t)p - (ptr_t)base) - 1))
  259. & descr)) goto fail;
  260. break;
  261. case GC_DS_PROC:
  262. /* We could try to decipher this partially. */
  263. /* For now we just punt. */
  264. break;
  265. case GC_DS_PER_OBJECT:
  266. if ((signed_word)descr >= 0) {
  267. descr = *(word *)((ptr_t)base + (descr & ~GC_DS_TAGS));
  268. } else {
  269. ptr_t type_descr = *(ptr_t *)base;
  270. descr = *(word *)(type_descr
  271. - (descr - (GC_DS_PER_OBJECT
  272. - GC_INDIR_PER_OBJ_BIAS)));
  273. }
  274. goto retry;
  275. }
  276. return(p);
  277. }
  278. # endif
  279. fail:
  280. (*GC_is_visible_print_proc)((ptr_t)p);
  281. return(p);
  282. }
  283. GC_PTR GC_pre_incr (p, how_much)
  284. GC_PTR *p;
  285. size_t how_much;
  286. {
  287. GC_PTR initial = *p;
  288. GC_PTR result = GC_same_obj((GC_PTR)((word)initial + how_much), initial);
  289. if (!GC_all_interior_pointers) {
  290. (void) GC_is_valid_displacement(result);
  291. }
  292. return (*p = result);
  293. }
  294. GC_PTR GC_post_incr (p, how_much)
  295. GC_PTR *p;
  296. size_t how_much;
  297. {
  298. GC_PTR initial = *p;
  299. GC_PTR result = GC_same_obj((GC_PTR)((word)initial + how_much), initial);
  300. if (!GC_all_interior_pointers) {
  301. (void) GC_is_valid_displacement(result);
  302. }
  303. *p = result;
  304. return(initial);
  305. }