changeset 19788:94d87f6324a3

ensure an offset into a HotSpot metaspace object is valid before reading it
author Doug Simon <doug.simon@oracle.com>
date Wed, 11 Mar 2015 15:25:34 +0100
parents 7117697d11e1
children a72945780580
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMemoryAccessProviderImpl.java
diffstat 1 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMemoryAccessProviderImpl.java	Tue Mar 10 21:59:29 2015 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMemoryAccessProviderImpl.java	Wed Mar 11 15:25:34 2015 +0100
@@ -48,6 +48,24 @@
         }
     }
 
+    private boolean isValidObjectFieldDisplacement(Constant base, long displacement) {
+        if (base instanceof HotSpotMetaspaceConstant) {
+            Object metaspaceObject = HotSpotMetaspaceConstantImpl.getMetaspaceObject(base);
+            if (metaspaceObject instanceof HotSpotResolvedObjectTypeImpl) {
+                if (displacement == runtime.getConfig().classMirrorOffset) {
+                    // Klass::_java_mirror is valid for all Klass* values
+                    return true;
+                } else if (displacement == runtime.getConfig().arrayKlassComponentMirrorOffset) {
+                    // ArrayKlass::_component_mirror is only valid for all ArrayKlass* values
+                    return ((HotSpotResolvedObjectTypeImpl) metaspaceObject).mirror().isArray();
+                }
+            } else {
+                throw GraalInternalError.shouldNotReachHere();
+            }
+        }
+        return false;
+    }
+
     private static long asRawPointer(Constant base) {
         if (base instanceof HotSpotMetaspaceConstant) {
             return ((HotSpotMetaspaceConstant) base).rawValue();
@@ -119,7 +137,6 @@
         if (base == null) {
             displacement += asRawPointer(baseConstant);
         }
-
         Object ret = runtime.getCompilerToVM().readUnsafeOop(base, displacement, compressed);
         assert verifyReadRawObject(ret, baseConstant, initialDisplacement, compressed);
 
@@ -167,6 +184,9 @@
 
     @Override
     public JavaConstant readObjectConstant(Constant base, long displacement) {
+        if (!isValidObjectFieldDisplacement(base, displacement)) {
+            return null;
+        }
         return HotSpotObjectConstantImpl.forObject(readRawObject(base, displacement, false));
     }