Skip to content

Commit 2e1fea5

Browse files
committed
Omit field receiver type check.
1 parent 3128c97 commit 2e1fea5

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/FieldInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import java.lang.invoke.VarHandle;
4444
import java.lang.reflect.Field;
45+
import java.util.Objects;
4546

4647
import com.oracle.truffle.api.CompilerAsserts;
4748
import com.oracle.truffle.api.CompilerDirectives;
@@ -158,4 +159,10 @@ private IllegalArgumentException illegalReceiver(DynamicObject store) {
158159
CompilerAsserts.neverPartOfCompilation();
159160
return new IllegalArgumentException("Invalid receiver type (expected " + getDeclaringClass() + ", was " + (store == null ? null : store.getClass()) + ")");
160161
}
162+
163+
void unsafeReceiverCast(DynamicObject store) {
164+
if (ObjectStorageOptions.ReceiverCast) {
165+
Objects.requireNonNull(tclass.cast(store));
166+
}
167+
}
161168
}

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Location.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ final Object getInternal(DynamicObject store, Shape expectedShape, boolean guard
209209
base = getObjectArray(store, guard);
210210
offset = getObjectArrayOffset();
211211
} else {
212-
field.receiverCheck(store);
212+
field.unsafeReceiverCast(store);
213213
base = store;
214214
offset = getFieldOffset();
215215
}
@@ -229,7 +229,7 @@ final Object getInternal(DynamicObject store, Shape expectedShape, boolean guard
229229
return ((ConstantLocation) this).get(store, guard);
230230
}
231231
} else {
232-
field.receiverCheck(store);
232+
field.unsafeReceiverCast(store);
233233
long longValue = UnsafeAccess.unsafeGetLong(store, getFieldOffset(), guard, this);
234234
if (this instanceof IntLocation) {
235235
return (int) longValue;
@@ -253,7 +253,7 @@ final int getIntInternal(DynamicObject store, Shape expectedShape, boolean guard
253253
long offset = getPrimitiveArrayOffset();
254254
return UnsafeAccess.unsafeGetInt(array, offset, guard, this);
255255
} else {
256-
field.receiverCheck(store);
256+
field.unsafeReceiverCast(store);
257257
long longValue = UnsafeAccess.unsafeGetLong(store, getFieldOffset(), guard, this);
258258
return (int) longValue;
259259
}
@@ -280,7 +280,7 @@ final long getLongInternal(DynamicObject store, Shape expectedShape, boolean gua
280280
long offset = getPrimitiveArrayOffset();
281281
return UnsafeAccess.unsafeGetLong(array, offset, guard, this);
282282
} else {
283-
field.receiverCheck(store);
283+
field.unsafeReceiverCast(store);
284284
return UnsafeAccess.unsafeGetLong(store, getFieldOffset(), guard, this);
285285
}
286286
}
@@ -306,7 +306,7 @@ final double getDoubleInternal(DynamicObject store, Shape expectedShape, boolean
306306
long offset = getPrimitiveArrayOffset();
307307
return UnsafeAccess.unsafeGetDouble(array, offset, guard, this);
308308
} else {
309-
field.receiverCheck(store);
309+
field.unsafeReceiverCast(store);
310310
long longValue = UnsafeAccess.unsafeGetLong(store, getFieldOffset(), guard, this);
311311
return Double.longBitsToDouble(longValue);
312312
}
@@ -462,7 +462,7 @@ final void setInternal(DynamicObject receiver, Object value, boolean guard, Shap
462462
base = getObjectArray(receiver, guard);
463463
offset = getObjectArrayOffset();
464464
} else {
465-
field.receiverCheck(receiver);
465+
field.unsafeReceiverCast(receiver);
466466
base = receiver;
467467
offset = getFieldOffset();
468468
}
@@ -514,7 +514,7 @@ final void setInternal(DynamicObject receiver, Object value, boolean guard, Shap
514514
assert isConstantLocation() : this;
515515
return;
516516
}
517-
field.receiverCheck(receiver);
517+
field.unsafeReceiverCast(receiver);
518518
long offset = getFieldOffset();
519519
UnsafeAccess.unsafePutLong(receiver, offset, longValue, this);
520520
}

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObjectStorageOptions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ final class ObjectStorageOptions {
4747
private ObjectStorageOptions() {
4848
}
4949

50+
static final boolean ASSERTIONS_ENABLED = assertionsEnabled();
51+
5052
static final boolean PrimitiveLocations = booleanOption(OPTION_PREFIX + "PrimitiveLocations", true);
5153
static final boolean IntegerLocations = booleanOption(OPTION_PREFIX + "IntegerLocations", true);
5254
static final boolean DoubleLocations = booleanOption(OPTION_PREFIX + "DoubleLocations", true);
@@ -61,6 +63,10 @@ private ObjectStorageOptions() {
6163
* If set to true, use VarHandle rather than Unsafe to implement field accesses.
6264
*/
6365
static final boolean UseVarHandle = booleanOption(OPTION_PREFIX + "UseVarHandle", false);
66+
/**
67+
* Enforce receiver type checks for unsafe field accesses.
68+
*/
69+
static final boolean ReceiverCast = booleanOption(OPTION_PREFIX + "ReceiverCast", false) || ASSERTIONS_ENABLED;
6470

6571
static final boolean TriePropertyMap = booleanOption(OPTION_PREFIX + "TriePropertyMap", true);
6672
static final boolean TrieTransitionMap = booleanOption(OPTION_PREFIX + "TrieTransitionMap", true);
@@ -94,4 +100,11 @@ static boolean booleanOption(String name, boolean defaultValue) {
94100
String value = System.getProperty(name);
95101
return value == null ? defaultValue : value.equalsIgnoreCase("true");
96102
}
103+
104+
@SuppressWarnings("all")
105+
private static boolean assertionsEnabled() {
106+
boolean enabled = false;
107+
assert (enabled = true) == true;
108+
return enabled;
109+
}
97110
}

0 commit comments

Comments
 (0)