[Gc] GC: New assertions suggested

Ivan Maidanski ivmai at mail.ru
Tue Oct 14 09:15:11 PDT 2008


Hi!

The attached patch does the following suggested improvements:
- adds runtime assertions for non-null "hhdr" in GC_realloc(), GC_size() and GC_is_marked() (this helps against invalid arg passing during debugging);
- fixes assertion in GC_free() (assertion for non-null "hhdr" is placed before its dereferencing);
- treats null "link" arg in GC_general_register_disappearing_link() the same way as for mis-aligned "link" value (i.e., causing abort with an appropriate message);
- and, besides, changes the declarator for GC_register_finalizer_inner() and GC_..._finalize_mark_proc() from GC_API to STATIC (since these funcs are not exported and are used only locally in "finalize" module).

Bye.

-------------- next part --------------
diff -ru bdwgc/finalize.c updated/bdwgc/finalize.c
--- bdwgc/finalize.c	2008-10-11 15:04:26.000000000 +0400
+++ updated/bdwgc/finalize.c	2008-10-14 17:45:15.739037300 +0400
@@ -154,7 +154,7 @@
     struct disappearing_link * new_dl;
     DCL_LOCK_STATE;
     
-    if ((word)link & (ALIGNMENT-1))
+    if (((word)link & (ALIGNMENT-1)) || link == NULL)
     	ABORT("Bad arg to GC_general_register_disappearing_link");
 #   ifdef THREADS
     	LOCK();
@@ -242,7 +242,7 @@
 
 /* Possible finalization_marker procedures.  Note that mark stack	*/
 /* overflow is handled by the caller, and is not a disaster.		*/
-GC_API void GC_normal_finalize_mark_proc(ptr_t p)
+STATIC void GC_normal_finalize_mark_proc(ptr_t p)
 {
     hdr * hhdr = HDR(p);
     
@@ -253,7 +253,7 @@
 /* This only pays very partial attention to the mark descriptor.	*/
 /* It does the right thing for normal and atomic objects, and treats	*/
 /* most others as normal.						*/
-GC_API void GC_ignore_self_finalize_mark_proc(ptr_t p)
+STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
 {
     hdr * hhdr = HDR(p);
     word descr = hhdr -> hb_descr;
@@ -275,7 +275,7 @@
 }
 
 /*ARGSUSED*/
-GC_API void GC_null_finalize_mark_proc(ptr_t p)
+STATIC void GC_null_finalize_mark_proc(ptr_t p)
 {
 }
 
@@ -287,7 +287,7 @@
 /* behavior.  Objects registered in this way are not finalized		*/
 /* if they are reachable by other finalizable objects, eve if those	*/
 /* other objects specify no ordering.					*/
-GC_API void GC_unreachable_finalize_mark_proc(ptr_t p)
+STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
 {
     GC_normal_finalize_mark_proc(p);
 }
@@ -302,7 +302,7 @@
 /* marking for finalization ordering.  Any objects marked	*/
 /* by that procedure will be guaranteed to not have been	*/
 /* finalized when this finalizer is invoked.			*/
-GC_API void GC_register_finalizer_inner(void * obj,
+STATIC void GC_register_finalizer_inner(void * obj,
 					GC_finalization_proc fn, void *cd,
 					GC_finalization_proc *ofn, void **ocd,
 					finalization_mark_proc mp)
diff -ru bdwgc/malloc.c updated/bdwgc/malloc.c
--- bdwgc/malloc.c	2008-07-26 04:51:34.000000000 +0400
+++ updated/bdwgc/malloc.c	2008-10-14 17:37:54.254662300 +0400
@@ -404,8 +404,6 @@
 #   endif
     h = HBLKPTR(p);
     hhdr = HDR(h);
-    sz = hhdr -> hb_sz;
-    ngranules = BYTES_TO_GRANULES(sz);
 #   if defined(REDIRECT_MALLOC) && \
 	(defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
 	 || defined(MSWIN32))
@@ -416,6 +414,8 @@
 	if (0 == hhdr) return;
 #   endif
     GC_ASSERT(GC_base(p) == p);
+    sz = hhdr -> hb_sz;
+    ngranules = BYTES_TO_GRANULES(sz);
     knd = hhdr -> hb_obj_kind;
     ok = &GC_obj_kinds[knd];
     if (EXPECT((ngranules <= MAXOBJGRANULES), 1)) {
diff -ru bdwgc/mallocx.c updated/bdwgc/mallocx.c
--- bdwgc/mallocx.c	2008-09-26 12:59:02.000000000 +0400
+++ updated/bdwgc/mallocx.c	2008-10-14 17:38:48.614037300 +0400
@@ -75,6 +75,7 @@
     if (p == 0) return(GC_malloc(lb));	/* Required by ANSI */
     h = HBLKPTR(p);
     hhdr = HDR(h);
+    GC_ASSERT(hhdr != NULL);
     sz = hhdr -> hb_sz;
     obj_kind = hhdr -> hb_obj_kind;
     orig_sz = sz;
diff -ru bdwgc/mark.c updated/bdwgc/mark.c
--- bdwgc/mark.c	2008-10-11 15:18:34.000000000 +0400
+++ updated/bdwgc/mark.c	2008-10-14 17:39:42.989037300 +0400
@@ -230,7 +230,9 @@
 {
     struct hblk *h = HBLKPTR(p);
     hdr * hhdr = HDR(h);
-    word bit_no = MARK_BIT_NO(p - (ptr_t)h, hhdr -> hb_sz);
+    word bit_no;
+    GC_ASSERT(hhdr != NULL);
+    bit_no = MARK_BIT_NO(p - (ptr_t)h, hhdr -> hb_sz);
     
     return((GC_bool)mark_bit_from_hdr(hhdr, bit_no));
 }
diff -ru bdwgc/misc.c updated/bdwgc/misc.c
--- bdwgc/misc.c	2008-10-11 13:23:28.000000000 +0400
+++ updated/bdwgc/misc.c	2008-10-14 17:42:02.614037300 +0400
@@ -370,7 +370,7 @@
 GC_API size_t GC_size(void * p)
 {
     hdr * hhdr = HDR(p);
-    
+    GC_ASSERT(hhdr != NULL);
     return hhdr -> hb_sz;
 }
 


More information about the Gc mailing list