[pypy-svn] r28891 - in pypy/dist/pypy: rpython rpython/memory translator/c/test

ac at codespeak.net ac at codespeak.net
Fri Jun 16 17:28:56 CEST 2006


Author: ac
Date: Fri Jun 16 17:28:55 2006
New Revision: 28891

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/memory/gc.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/translator/c/test/test_newgc.py
Log:
(pedronis, arre) Check for overflowing calculations in malloc + a skipped test.



Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Fri Jun 16 17:28:55 2006
@@ -973,6 +973,14 @@
         else:
             return self.original_int_add(x, y)
 
+    original_int_add_ovf = op_int_add_ovf
+
+    def op_int_add_ovf(self, x, y):
+        if isinstance(x, llmemory.AddressOffset) or isinstance(y, llmemory.AddressOffset) :
+            return x + y
+        else:
+            return self.original_int_add_ovf(x, y)
+
     original_int_mul = op_int_mul
 
     def op_int_mul(self, x, y):

Modified: pypy/dist/pypy/rpython/memory/gc.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gc.py	(original)
+++ pypy/dist/pypy/rpython/memory/gc.py	Fri Jun 16 17:28:55 2006
@@ -6,9 +6,9 @@
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rpython.objectmodel import free_non_gc_object
 from pypy.rpython.lltypesystem.lloperation import llop
-from pypy.rpython import rarithmetic
+from pypy.rpython.rarithmetic import ovfcheck
 
-import sys
+import sys, os
 
 int_size = lltypesimulation.sizeof(lltype.Signed)
 gc_header_two_ints = 2*int_size
@@ -110,7 +110,7 @@
     init_gc_object_immortal = init_gc_object
 
 DEBUG_PRINT = True
-
+memoryError = MemoryError()
 class MarkSweepGC(GCBase):
     _alloc_flavor_ = "raw"
 
@@ -172,7 +172,14 @@
         if can_collect and self.bytes_malloced > self.bytes_malloced_threshold:
             self.collect()
         size_gc_header = self.gcheaderbuilder.size_gc_header
-        result = raw_malloc(size_gc_header + size)
+        try:
+            tot_size = ovfcheck(size_gc_header + size)
+            usage = raw_malloc_usage(tot_size)
+            bytes_malloced = ovfcheck(self.bytes_malloced+usage)
+            ovfcheck(self.heap_usage + bytes_malloced)
+        except OverflowError:
+            raise memoryError
+        result = raw_malloc(tot_size)
         hdr = llmemory.cast_adr_to_ptr(result, self.HDRPTR)
         hdr.typeid = typeid << 1
         if not self.getfinalizer(typeid):
@@ -181,7 +188,7 @@
         else:
             hdr.next = self.malloced_objects_with_finalizer
             self.malloced_objects_with_finalizer = hdr
-        self.bytes_malloced += raw_malloc_usage(size + size_gc_header)
+        self.bytes_malloced = bytes_malloced
         result += size_gc_header
         return llmemory.cast_adr_to_ptr(result, llmemory.GCREF)
 
@@ -189,14 +196,17 @@
                        can_collect):
         if can_collect and self.bytes_malloced > self.bytes_malloced_threshold:
             self.collect()
+        size_gc_header = self.gcheaderbuilder.size_gc_header
         try:
-            varsize = rarithmetic.ovfcheck(itemsize * length)
+            varsize = ovfcheck(itemsize * length)
+            size = ovfcheck(size + varsize)
+            tot_size = ovfcheck(size_gc_header + size)
+            usage = raw_malloc_usage(tot_size)
+            bytes_malloced = ovfcheck(self.bytes_malloced+usage)
+            ovfcheck(self.heap_usage + bytes_malloced)
         except OverflowError:
-            raise MemoryError
-        # XXX also check for overflow on the various '+' below!
-        size += varsize
-        size_gc_header = self.gcheaderbuilder.size_gc_header
-        result = raw_malloc(size_gc_header + size)
+            raise memoryError
+        result = raw_malloc(tot_size)
         (result + size_gc_header + offset_to_length).signed[0] = length
         hdr = llmemory.cast_adr_to_ptr(result, self.HDRPTR)
         hdr.typeid = typeid << 1
@@ -206,7 +216,8 @@
         else:
             hdr.next = self.malloced_objects_with_finalizer
             self.malloced_objects_with_finalizer = hdr
-        self.bytes_malloced += raw_malloc_usage(size + size_gc_header)
+        self.bytes_malloced = bytes_malloced
+            
         result += size_gc_header
         return llmemory.cast_adr_to_ptr(result, llmemory.GCREF)
 
@@ -838,7 +849,7 @@
             #XXX need to increase the space size if the object is too big
             #for bonus points do big objects differently
             if self.free + totalsize > self.top_of_space:
-                raise MemoryError
+                raise memoryError
         result = self.free
         self.init_gc_object(result, typeid)
         self.free += totalsize
@@ -847,9 +858,9 @@
     def malloc_varsize(self, typeid, length, size, itemsize, offset_to_length,
                        can_collect):
         try:
-            varsize = rarithmetic.ovfcheck(itemsize * length)
+            varsize = ovfcheck(itemsize * length)
         except OverflowError:
-            raise MemoryError
+            raise memoryError
         # XXX also check for overflow on the various '+' below!
         size += varsize
         size_gc_header = self.gcheaderbuilder.size_gc_header
@@ -859,7 +870,7 @@
             #XXX need to increase the space size if the object is too big
             #for bonus points do big objects differently
             if self.free + totalsize > self.top_of_space:
-                raise MemoryError
+                raise memoryError
         result = self.free
         self.init_gc_object(result, typeid)
         (result + size_gc_header + offset_to_length).signed[0] = length

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Fri Jun 16 17:28:55 2006
@@ -540,6 +540,7 @@
 
 def rtype_raw_malloc_usage(hop):
     v_size, = hop.inputargs(lltype.Signed)
+    hop.exception_cannot_occur()
     return hop.genop('raw_malloc_usage', [v_size], resulttype=lltype.Signed)
 
 def rtype_raw_free(hop):

Modified: pypy/dist/pypy/translator/c/test/test_newgc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_newgc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_newgc.py	Fri Jun 16 17:28:55 2006
@@ -753,6 +753,57 @@
         res = fn()
         assert res == 42
 
+    def test_memory_error_varsize(self):
+        py.test.skip("Needs lots (>2GB) of memory.")
+        import gc
+        import pypy.rpython.rgc
+        from pypy.rpython.lltypesystem import lltype
+        N = sys.maxint / 4 + 4
+        A = lltype.GcArray(lltype.Signed)
+        def alloc(n):
+            return lltype.malloc(A, n)
+        def f():
+            try:
+                try:
+                    x = alloc(N)
+                except MemoryError:
+                    y = alloc(10)
+                    return len(y)
+                return -1
+            finally:
+                gc.collect()
+                
+        fn = self.getcompiled(f)
+        res = fn()
+        assert res == 10
+        N = sys.maxint / 4
+        fn = self.getcompiled(f)
+        res = fn()
+        assert res == 10
+
+        N = sys.maxint / 4 - 1
+        fn = self.getcompiled(f)
+        res = fn()
+        assert res == 10
+
+        N = sys.maxint / 8 + 1000
+        def f():
+            try:
+                x0 = alloc(N)
+                try:
+                    x1 = alloc(N)
+                    return len(x0) + len(x1)
+                except MemoryError:
+                    y = alloc(10)
+                    return len(y)
+                return -1
+            finally:
+                gc.collect()
+
+        fn = self.getcompiled(f)
+        res = fn()
+        assert res == 10
+
 class TestUsingStacklessFramework(TestUsingFramework):
     from pypy.translator.c.gc import StacklessFrameworkGcPolicy as gcpolicy
 



More information about the Pypy-commit mailing list