changeset 22196:fec4d8428ace

Fill uninitialized memory with garbage when asserts are enabled
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Fri, 10 Jul 2015 11:40:58 -0700
parents 6cd6d5e670ad
children cbb24d44d09b
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/NewObjectSnippets.java
diffstat 1 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/NewObjectSnippets.java	Thu Jul 09 22:05:11 2015 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/NewObjectSnippets.java	Fri Jul 10 11:40:58 2015 -0700
@@ -310,10 +310,14 @@
      * @param manualUnroll maximally unroll zeroing
      */
     private static void zeroMemory(int size, Word memory, boolean constantSize, int startOffset, boolean manualUnroll, boolean useSnippetCounters) {
+        fillMemory(0, size, memory, constantSize, startOffset, manualUnroll, useSnippetCounters);
+    }
+
+    private static void fillMemory(long value, int size, Word memory, boolean constantSize, int startOffset, boolean manualUnroll, boolean useSnippetCounters) {
         ReplacementsUtil.runtimeAssert((size & 0x7) == 0, "unaligned object size");
         int offset = startOffset;
         if ((offset & 0x7) != 0) {
-            memory.writeInt(offset, 0, INIT_LOCATION);
+            memory.writeInt(offset, (int) value, INIT_LOCATION);
             offset += 4;
         }
         ReplacementsUtil.runtimeAssert((offset & 0x7) == 0, "unaligned offset");
@@ -330,7 +334,7 @@
                 if (offset == size) {
                     break;
                 }
-                memory.initializeLong(offset, 0, INIT_LOCATION);
+                memory.initializeLong(offset, value, INIT_LOCATION);
             }
         } else {
             // Use Word instead of int to avoid extension to long in generated code
@@ -346,12 +350,27 @@
                 }
             }
             for (; off.rawValue() < size; off = off.add(8)) {
-                memory.initializeLong(off, 0, INIT_LOCATION);
+                memory.initializeLong(off, value, INIT_LOCATION);
             }
         }
     }
 
     /**
+     * Full uninitialized memory with garbage value in a newly allocated object, unrolling as
+     * necessary and ensuring that stores are aligned.
+     *
+     * @param size number of bytes to zero
+     * @param memory beginning of object which is being zeroed
+     * @param constantSize is @ size} known to be constant in the snippet
+     * @param startOffset offset to begin zeroing. May not be word aligned.
+     * @param manualUnroll maximally unroll zeroing
+     */
+    private static boolean fillWithGarbage(int size, Word memory, boolean constantSize, int startOffset, boolean manualUnroll, boolean useSnippetCounters) {
+        fillMemory(0xfefefefefefefefeL, size, memory, constantSize, startOffset, manualUnroll, useSnippetCounters);
+        return true;
+    }
+
+    /**
      * Formats some allocated memory with an object header and zeroes out the rest. Disables asserts
      * since they can't be compiled in stubs.
      */
@@ -367,6 +386,8 @@
         initializeObjectHeader(memory, prototypeMarkWord, hub);
         if (fillContents) {
             zeroMemory(size, memory, constantSize, instanceHeaderSize(), false, useSnippetCounters);
+        } else {
+            ReplacementsUtil.runtimeAssert(fillWithGarbage(size, memory, constantSize, instanceHeaderSize(), false, useSnippetCounters), "");
         }
         return memory.toObject();
     }
@@ -396,6 +417,8 @@
         initializeObjectHeader(memory, prototypeMarkWord, hub);
         if (fillContents) {
             zeroMemory(allocationSize, memory, false, headerSize, maybeUnroll, useSnippetCounters);
+        } else {
+            ReplacementsUtil.runtimeAssert(fillWithGarbage(allocationSize, memory, false, headerSize, maybeUnroll, useSnippetCounters), "");
         }
         return memory.toObject();
     }