[kaffe] CVS kaffe (guilhem): Fixes for preventing integer overflows in newArrayChecked + max heap size fixlet

Kaffe CVS cvs-commits at kaffe.org
Sun Oct 2 07:27:44 PDT 2005


PatchSet 6941 
Date: 2005/10/02 14:20:02
Author: guilhem
Branch: HEAD
Tag: (none) 
Log:
Fixes for preventing integer overflows in newArrayChecked + max heap size fixlet.

        * kaffe/kaffevm/gc.h
        (KGC_MAX_MALLOC_TYPE): Alias for SIZE_MAX.

        * kaffe/kaffevm/object.c
        (newArrayChecked): Check for integer overflows before summing
        some maybe big values.

        * kaffe/kaffevm/boehm-gc/gc2.c
        (KaffeGC_realloc, KaffeGC_malloc): Added more assertion safeguards.
        (KaffeGC_init): Initialized the max heap size correctly.
        (KGC_max_heap_size): New static global variable.
        (KaffeGC_HeapLimit): It now returns a valid value now.

        * kaffe/kaffevm/kaffe-gc/gc-incremental.c
        (gcMalloc): Added more assertion safeguards.

        * libraries/clib/native/Runtime.c
        (maxMemory): Check if the heap is unlimited using the good
        constant.

Members: 
	ChangeLog:1.4463->1.4464 
	kaffe/kaffevm/gc.h:1.35->1.36 
	kaffe/kaffevm/object.c:1.36->1.37 
	kaffe/kaffevm/boehm-gc/gc2.c:1.13->1.14 
	kaffe/kaffevm/kaffe-gc/gc-incremental.c:1.33->1.34 
	libraries/clib/native/Runtime.c:1.38->1.39 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.4463 kaffe/ChangeLog:1.4464
--- kaffe/ChangeLog:1.4463	Sun Oct  2 13:04:12 2005
+++ kaffe/ChangeLog	Sun Oct  2 14:20:02 2005
@@ -1,3 +1,25 @@
+2005-10-02  Guilhem Lavaux  <guilhem at kaffe.org>
+
+	* kaffe/kaffevm/gc.h
+	(KGC_MAX_MALLOC_TYPE): Alias for SIZE_MAX.
+
+	* kaffe/kaffevm/object.c
+	(newArrayChecked): Check for integer overflows before summing
+	some maybe big values.
+
+	* kaffe/kaffevm/boehm-gc/gc2.c
+	(KaffeGC_realloc, KaffeGC_malloc): Added more assertion safeguards.
+	(KaffeGC_init): Initialized the max heap size correctly.
+	(KGC_max_heap_size): New static global variable.
+	(KaffeGC_HeapLimit): It now returns a valid value now.
+	
+	* kaffe/kaffevm/kaffe-gc/gc-incremental.c
+	(gcMalloc): Added more assertion safeguards.
+
+	* libraries/clib/native/Runtime.c
+	(maxMemory): Check if the heap is unlimited using the good
+	constant.
+	
 2005-10-02  Dalibor Topic  <robilad at kaffe.org>
 
 	* kaffe/kaffevm/stats.c: Added guards for sys/resource.h include.
Index: kaffe/kaffe/kaffevm/gc.h
diff -u kaffe/kaffe/kaffevm/gc.h:1.35 kaffe/kaffe/kaffevm/gc.h:1.36
--- kaffe/kaffe/kaffevm/gc.h:1.35	Sat Aug 20 22:34:02 2005
+++ kaffe/kaffe/kaffevm/gc.h	Sun Oct  2 14:20:08 2005
@@ -30,6 +30,12 @@
 #define	MAX_HEAPSIZE	(UNLIMITED_HEAP)
 #define	ALLOC_HEAPSIZE	(1024*1024)
 
+/*
+ * This macro sets the maximal value you can allocate in one chunk of memory. The type
+ * signed so we substract one bit.
+ */
+#define KGC_MAX_MALLOC_TYPE SIZE_MAX
+
 /* 
  * We do not support incremental collection as this time.
  */
Index: kaffe/kaffe/kaffevm/object.c
diff -u kaffe/kaffe/kaffevm/object.c:1.36 kaffe/kaffe/kaffevm/object.c:1.37
--- kaffe/kaffe/kaffevm/object.c:1.36	Mon Jun 13 15:44:44 2005
+++ kaffe/kaffe/kaffevm/object.c	Sun Oct  2 14:20:08 2005
@@ -139,19 +139,34 @@
 
 	if ((class = lookupArray(elclass, info)) != NULL) {
 		size_t total_count;
-		
+
 		if (CLASS_IS_PRIMITIVE(elclass) || elclass == PtrClass) {
-			total_count = (TYPE_SIZE(elclass) * count) +
-				ARRAY_DATA_OFFSET;
-			obj = gc_malloc(total_count,
-					KGC_ALLOC_PRIMARRAY);
+
+			if (((KGC_MAX_MALLOC_TYPE - ARRAY_DATA_OFFSET) / TYPE_SIZE(elclass)) < count)
+			  {
+			    postOutOfMemory(info);
+			  }
+			else
+			  {
+			    total_count = (TYPE_SIZE(elclass) * count) +
+			      ARRAY_DATA_OFFSET;
+			    obj = gc_malloc(total_count,
+					    KGC_ALLOC_PRIMARRAY);
+			  }
 		}
 		else {
-			total_count = (PTR_TYPE_SIZE * count) +
-				ARRAY_DATA_OFFSET;
-
-			obj = gc_malloc(total_count,
-					KGC_ALLOC_REFARRAY);
+			if (((KGC_MAX_MALLOC_TYPE - ARRAY_DATA_OFFSET) / PTR_TYPE_SIZE) < count)
+			  {
+			    postOutOfMemory(info);
+			  }
+			else
+			  {
+			    total_count = (PTR_TYPE_SIZE * count) +
+			      ARRAY_DATA_OFFSET;
+			    
+			    obj = gc_malloc(total_count,
+					    KGC_ALLOC_REFARRAY);
+			  }
 		}
 		if (obj != NULL) {
 		        KaffeVM_setFinalizer(obj, KGC_DEFAULT_FINALIZER);
Index: kaffe/kaffe/kaffevm/boehm-gc/gc2.c
diff -u kaffe/kaffe/kaffevm/boehm-gc/gc2.c:1.13 kaffe/kaffe/kaffevm/boehm-gc/gc2.c:1.14
--- kaffe/kaffe/kaffevm/boehm-gc/gc2.c:1.13	Sat Oct  1 18:25:04 2005
+++ kaffe/kaffe/kaffevm/boehm-gc/gc2.c	Sun Oct  2 14:20:08 2005
@@ -53,6 +53,8 @@
 static Hjava_lang_Thread* finalman;
 static Hjava_lang_Thread* garbageman;
 
+static ssize_t KGC_max_heap_size;
+
 #define GCSTACKSIZE             (1024*1024)
 #define FINALIZERSTACKSIZE      (128*1024)
 
@@ -283,7 +285,9 @@
 
   if (mem == NULL)
     return KGC_malloc(gcif, sz, type);
-    
+
+  assert(sz > 0);
+  
   new_ptr = GC_realloc ( ALIGN_BACKWARD(mem), (size_t)SYSTEM_SIZE(sz));
   if (new_ptr) {
     MemDescriptor *desc = (MemDescriptor *)new_ptr;
@@ -322,6 +326,7 @@
 
   assert(gcFunctions[type].description != NULL);
   assert(sz != 0);
+  assert(sz > 0);
 
   desc.memtype = type;
   desc.memsize = sz;
@@ -434,7 +439,7 @@
 static uintp
 KaffeGC_HeapLimit(Collector *gcif UNUSED)
 {
-  return 0;
+  return KGC_max_heap_size;
 }
 
 static uintp
@@ -515,7 +520,12 @@
   GC_finalize_on_demand = 1;
   GC_set_warn_proc(KaffeGC_warnproc);
   GC_init();
-  GC_set_max_heap_size((size_t)Kaffe_JavaVMArgs.maxHeapSize);
+  if (Kaffe_JavaVMArgs.maxHeapSize == UNLIMITED_HEAP)
+    GC_set_max_heap_size(0);
+  else
+    GC_set_max_heap_size((size_t)Kaffe_JavaVMArgs.maxHeapSize);
+
+  KGC_max_heap_size = Kaffe_JavaVMArgs.maxHeapSize;
 
   if (GC_get_heap_size() < (size_t)Kaffe_JavaVMArgs.minHeapSize)
     GC_expand_hp( Kaffe_JavaVMArgs.minHeapSize - GC_get_heap_size());
Index: kaffe/kaffe/kaffevm/kaffe-gc/gc-incremental.c
diff -u kaffe/kaffe/kaffevm/kaffe-gc/gc-incremental.c:1.33 kaffe/kaffe/kaffevm/kaffe-gc/gc-incremental.c:1.34
--- kaffe/kaffe/kaffevm/kaffe-gc/gc-incremental.c:1.33	Thu Sep 22 20:20:48 2005
+++ kaffe/kaffe/kaffevm/kaffe-gc/gc-incremental.c	Sun Oct  2 14:20:08 2005
@@ -1067,6 +1067,7 @@
 	assert(gc_init != 0);
 	assert(gcFunctions[fidx].description != NULL);
 	assert(size != 0);
+	assert(size > 0);
 
 	size += sizeof(gc_unit);
 
Index: kaffe/libraries/clib/native/Runtime.c
diff -u kaffe/libraries/clib/native/Runtime.c:1.38 kaffe/libraries/clib/native/Runtime.c:1.39
--- kaffe/libraries/clib/native/Runtime.c:1.38	Thu Sep 15 03:35:49 2005
+++ kaffe/libraries/clib/native/Runtime.c	Sun Oct  2 14:20:09 2005
@@ -61,7 +61,7 @@
 {
 	jlong max = KGC_getHeapLimit(main_collector);
 
-	if (max <= 0) {
+	if (max == UNLIMITED_HEAP) {
 		return 0x7fffffffffffffffLL;
 	}
 	else {




More information about the kaffe mailing list