Skip to content

Commit 4844266

Browse files
Improve EspressoConstantPool.lookupReferencedType
* `JVMCIUtils.findInstanceType` should check access. * `JVMCIUtils.findObjectType` should only throw saved errors when resolve=true. * Add `resolvedConstantToJVMCIObjectType` helper. * In `lookupReferencedType` try to `findObjectType` for all entry types. * Merge `METHODHANDLE` and `METHODTYPE` in `lookupConstant
1 parent f16b721 commit 4844266

File tree

2 files changed

+46
-54
lines changed

2 files changed

+46
-54
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/jvmci/JVMCIUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ private JVMCIUtils() {
4646
public static ObjectKlass findInstanceType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, Meta meta) {
4747
assert !TypeSymbols.isArray(symbol);
4848
StaticObject loader = accessingKlass.getDefiningClassLoader();
49+
ObjectKlass klass;
4950
if (resolve) {
50-
return (ObjectKlass) meta.loadKlassOrFail(symbol, loader, accessingKlass.protectionDomain());
51+
klass = (ObjectKlass) meta.loadKlassOrFail(symbol, loader, accessingKlass.protectionDomain());
5152
} else {
52-
return (ObjectKlass) meta.getRegistries().findLoadedClass(symbol, loader);
53+
klass = (ObjectKlass) meta.getRegistries().findLoadedClass(symbol, loader);
5354
}
55+
if (klass != null && !Klass.checkAccess(klass, accessingKlass)) {
56+
return null;
57+
}
58+
return klass;
5459
}
5560

5661
@TruffleBoundary
@@ -78,6 +83,9 @@ public static Klass findObjectType(Symbol<Type> symbol, ObjectKlass accessingKla
7883
public static Klass findObjectType(int classIndex, RuntimeConstantPool pool, boolean resolve, Meta meta) {
7984
ResolvedConstant resolvedConstant = pool.peekResolvedOrNull(classIndex, meta);
8085
if (resolvedConstant != null) {
86+
if (!resolve && !resolvedConstant.isSuccess()) {
87+
return null;
88+
}
8189
return (Klass) resolvedConstant.value();
8290
}
8391
Symbol<Name> name = pool.className(classIndex);

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/substitutions/jvmci/Target_com_oracle_truffle_espresso_jvmci_meta_EspressoConstantPool.java

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
import static com.oracle.truffle.espresso.substitutions.jvmci.Target_com_oracle_truffle_espresso_jvmci_meta_EspressoResolvedInstanceType.toJVMCIField;
5151
import static com.oracle.truffle.espresso.substitutions.jvmci.Target_com_oracle_truffle_espresso_jvmci_meta_EspressoResolvedInstanceType.toJVMCIMethod;
5252

53-
import java.util.logging.Level;
54-
5553
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5654
import com.oracle.truffle.espresso.classfile.ConstantPool;
5755
import com.oracle.truffle.espresso.classfile.JavaKind;
@@ -212,19 +210,22 @@ private static Field tryResolveField(int fieldIndex, Klass symbolicHolder, Runti
212210
RuntimeConstantPool constantPool = getRuntimeConstantPool(self, meta);
213211
if (safeTagAt(constantPool, cpi, meta) == ConstantPool.Tag.CLASS) {
214212
ResolvedConstant resolvedConstant = constantPool.peekResolvedOrNull(cpi, meta);
215-
if (resolvedConstant == null || !resolvedConstant.isSuccess()) {
216-
return toJVMCIUnresolvedType(TypeSymbols.nameToType(constantPool.className(cpi)), meta);
217-
}
218-
Klass klass = (Klass) resolvedConstant.value();
219-
LOGGER.finer(() -> "ECP.lookupType found " + klass);
220-
return toJVMCIObjectType(klass, meta);
213+
return resolvedConstantToJVMCIObjectType(resolvedConstant, constantPool, cpi, meta);
221214
}
222215
if (safeTagAt(constantPool, cpi, meta) == ConstantPool.Tag.UTF8) {
223216
return toJVMCIUnresolvedType(TypeSymbols.nameToType(constantPool.utf8At(cpi)), meta);
224217
}
225218
throw meta.throwIllegalArgumentExceptionBoundary();
226219
}
227220

221+
private static StaticObject resolvedConstantToJVMCIObjectType(ResolvedConstant resolvedConstant, RuntimeConstantPool constantPool, int cpi, Meta meta) {
222+
if (resolvedConstant == null || !resolvedConstant.isSuccess()) {
223+
return toJVMCIUnresolvedType(TypeSymbols.nameToType(constantPool.className(cpi)), meta);
224+
}
225+
Klass klass = (Klass) resolvedConstant.value();
226+
return toJVMCIObjectType(klass, meta);
227+
}
228+
228229
@Substitution(hasReceiver = true)
229230
@TruffleBoundary
230231
public static @JavaType(internalName = "Lcom/oracle/truffle/espresso/jvmci/meta/EspressoResolvedJavaMethod;") StaticObject lookupResolvedMethod(StaticObject self, int cpi, int opcode,
@@ -358,6 +359,7 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
358359
StaticObject cpHolder = meta.jvmci.EspressoConstantPool_holder.getObject(self);
359360
ObjectKlass cpHolderKlass = (ObjectKlass) meta.jvmci.HIDDEN_OBJECTKLASS_MIRROR.getHiddenObject(cpHolder);
360361
RuntimeConstantPool constantPool = cpHolderKlass.getConstantPool();
362+
int classCpi;
361363
switch (opcode) {
362364
case CHECKCAST:
363365
case INSTANCEOF:
@@ -367,15 +369,10 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
367369
case LDC:
368370
case LDC_W:
369371
case LDC2_W:
370-
if (safeTagAt(constantPool, cpi, meta) == ConstantPool.Tag.CLASS) {
371-
ResolvedConstant resolvedConstant = constantPool.peekResolvedOrNull(cpi, meta);
372-
if (resolvedConstant == null || !resolvedConstant.isSuccess()) {
373-
return toJVMCIUnresolvedType(TypeSymbols.nameToType(constantPool.className(cpi)), meta);
374-
}
375-
Klass klass = (Klass) resolvedConstant.value();
376-
LOGGER.finer(() -> "ECP.lookupReferencedType found " + klass);
377-
return toJVMCIObjectType(klass, meta);
372+
if (safeTagAt(constantPool, cpi, meta) != ConstantPool.Tag.CLASS) {
373+
throw meta.throwIllegalArgumentExceptionBoundary("Opcode and constant pool entry types mismatch");
378374
}
375+
classCpi = cpi;
379376
break;
380377
case GETSTATIC:
381378
case PUTSTATIC:
@@ -385,27 +382,28 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
385382
case INVOKESPECIAL:
386383
case INVOKESTATIC:
387384
case INVOKEINTERFACE:
388-
if (safeTagAt(constantPool, cpi, meta).isMember()) {
389-
int holderClassIndex = constantPool.memberClassIndex(cpi);
390-
Klass holderKlass;
391-
try {
392-
holderKlass = findObjectType(holderClassIndex, constantPool, false, meta);
393-
} catch (EspressoException e) {
394-
LOGGER.log(Level.FINE, "ECP.lookupReferencedType exception during lookup", e);
395-
holderKlass = null;
396-
}
397-
if (holderKlass == null) {
398-
Symbol<Name> holderName = constantPool.memberClassName(cpi);
399-
return toJVMCIUnresolvedType(TypeSymbols.nameToType(holderName), meta);
400-
}
401-
Klass finalHolderKlass = holderKlass;
402-
LOGGER.finer(() -> "ECP.lookupReferencedType found " + finalHolderKlass);
403-
return toJVMCIObjectType(holderKlass, meta);
385+
if (!safeTagAt(constantPool, cpi, meta).isMember()) {
386+
throw meta.throwIllegalArgumentExceptionBoundary("Opcode and constant pool entry types mismatch");
404387
}
388+
classCpi = constantPool.memberClassIndex(cpi);
405389
break;
406-
}
407-
LOGGER.warning(() -> "Unsupported CP entry type for lookupReferencedType: " + safeTagAt(constantPool, cpi, meta) + " " + constantPool.toString(cpi) + " for " + Bytecodes.nameOf(opcode));
408-
throw meta.throwIllegalArgumentExceptionBoundary();
390+
default:
391+
LOGGER.warning(() -> "Unsupported CP entry type for lookupReferencedType: " + safeTagAt(constantPool, cpi, meta) + " " + constantPool.toString(cpi) + " for " +
392+
Bytecodes.nameOf(opcode));
393+
throw meta.throwIllegalArgumentExceptionBoundary("Unsupported CP entry type");
394+
}
395+
Klass klass;
396+
try {
397+
klass = findObjectType(classCpi, constantPool, false, meta);
398+
} catch (EspressoException e) {
399+
throw EspressoError.shouldNotReachHere("findObjectType with resolve=false should never throw", e);
400+
}
401+
if (klass == null) {
402+
Symbol<Name> className = constantPool.className(classCpi);
403+
return toJVMCIUnresolvedType(TypeSymbols.nameToType(className), meta);
404+
}
405+
LOGGER.finer(() -> "ECP.lookupReferencedType found " + klass);
406+
return toJVMCIObjectType(klass, meta);
409407
}
410408

411409
@Substitution(hasReceiver = true)
@@ -506,27 +504,13 @@ public static boolean loadReferencedType0(StaticObject self, int cpi, int opcode
506504
}
507505
switch (tag) {
508506
case CLASS -> {
509-
if (resolvedConstantOrNull == null || !resolvedConstantOrNull.isSuccess()) {
510-
EspressoError.guarantee(!resolve || resolvedConstantOrNull != null, "Should have been resolved");
511-
return toJVMCIUnresolvedType(TypeSymbols.nameToType(constantPool.className(cpi)), meta);
512-
}
513-
Klass klass = (Klass) resolvedConstantOrNull.value();
514-
LOGGER.finer(() -> "ECP.lookupConstant found " + klass);
515-
return toJVMCIObjectType(klass, meta);
507+
EspressoError.guarantee(!resolve || resolvedConstantOrNull != null, "Should have been resolved");
508+
return resolvedConstantToJVMCIObjectType(resolvedConstantOrNull, constantPool, cpi, meta);
516509
}
517510
case STRING -> {
518511
return wrapEspressoObjectConstant(constantPool.resolvedStringAt(cpi), meta);
519512
}
520-
case METHODHANDLE -> {
521-
if (resolvedConstantOrNull != null) {
522-
return wrapEspressoObjectConstant((StaticObject) resolvedConstantOrNull.value(), meta);
523-
} else if (resolve) {
524-
throw EspressoError.shouldNotReachHere();
525-
} else {
526-
return StaticObject.NULL;
527-
}
528-
}
529-
case METHODTYPE -> {
513+
case METHODHANDLE, METHODTYPE -> {
530514
if (resolvedConstantOrNull != null) {
531515
return wrapEspressoObjectConstant((StaticObject) resolvedConstantOrNull.value(), meta);
532516
} else if (resolve) {

0 commit comments

Comments
 (0)