Skip to content

Commit 164f56f

Browse files
Fix access checks in JVMCI
Some methods such as EspressoJVMCIRuntime.lookupType and EspressoResolvedJavaField.getType shouldn't perform access checks. The methods in EspressoConstantPool should continue performing access checks.
1 parent e448ee3 commit 164f56f

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/impl/ClassRegistry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,11 @@ public static void classInModuleOfLoader(ClassLoadingEnv env, Klass klass, boole
689689
sb.append(loaderDesc(env, meta, klass.getDefiningClassLoader()));
690690
}
691691

692+
public static String loaderDesc(ObjectKlass accessingKlass) {
693+
EspressoContext context = accessingKlass.getContext();
694+
return loaderDesc(context.getClassLoadingEnv(), context.getMeta(), accessingKlass.getDefiningClassLoader());
695+
}
696+
692697
private static String loaderDesc(ClassLoadingEnv env, Meta meta, StaticObject loader) {
693698
if (env.loaderIsBoot(loader)) {
694699
return "bootstrap";

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.truffle.espresso.classfile.descriptors.TypeSymbols;
3232
import com.oracle.truffle.espresso.constantpool.ResolvedConstant;
3333
import com.oracle.truffle.espresso.constantpool.RuntimeConstantPool;
34+
import com.oracle.truffle.espresso.impl.ClassRegistry;
3435
import com.oracle.truffle.espresso.impl.Klass;
3536
import com.oracle.truffle.espresso.impl.ObjectKlass;
3637
import com.oracle.truffle.espresso.meta.Meta;
@@ -43,44 +44,45 @@ private JVMCIUtils() {
4344
}
4445

4546
@TruffleBoundary
46-
public static ObjectKlass findInstanceType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, Meta meta) {
47+
public static ObjectKlass findInstanceType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, boolean checkAccess, Meta meta) {
4748
assert !TypeSymbols.isArray(symbol);
4849
StaticObject loader = accessingKlass.getDefiningClassLoader();
4950
ObjectKlass klass;
5051
if (resolve) {
5152
klass = (ObjectKlass) meta.loadKlassOrFail(symbol, loader, accessingKlass.protectionDomain());
53+
assert klass != null : symbol + " in " + ClassRegistry.loaderDesc(accessingKlass);
5254
} else {
5355
klass = (ObjectKlass) meta.getRegistries().findLoadedClass(symbol, loader);
5456
}
55-
if (klass != null && !Klass.checkAccess(klass, accessingKlass)) {
57+
if (checkAccess && klass != null && !Klass.checkAccess(klass, accessingKlass)) {
5658
return null;
5759
}
5860
return klass;
5961
}
6062

6163
@TruffleBoundary
62-
public static Klass findType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, Meta meta) {
64+
public static Klass findType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, boolean checkAccess, Meta meta) {
6365
if (TypeSymbols.isPrimitive(symbol)) {
6466
return meta.resolvePrimitive(symbol);
6567
} else {
66-
return findObjectType(symbol, accessingKlass, resolve, meta);
68+
return findObjectType(symbol, accessingKlass, resolve, checkAccess, meta);
6769
}
6870
}
6971

7072
@TruffleBoundary
71-
public static Klass findObjectType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, Meta meta) {
73+
public static Klass findObjectType(Symbol<Type> symbol, ObjectKlass accessingKlass, boolean resolve, boolean checkAccess, Meta meta) {
7274
if (TypeSymbols.isArray(symbol)) {
73-
Klass elemental = findType(meta.getTypes().getElementalType(symbol), accessingKlass, resolve, meta);
75+
Klass elemental = findType(meta.getTypes().getElementalType(symbol), accessingKlass, resolve, checkAccess, meta);
7476
if (elemental == null) {
7577
return null;
7678
}
7779
return elemental.getArrayKlass(TypeSymbols.getArrayDimensions(symbol));
7880
} else {
79-
return findInstanceType(symbol, accessingKlass, resolve, meta);
81+
return findInstanceType(symbol, accessingKlass, resolve, checkAccess, meta);
8082
}
8183
}
8284

83-
public static Klass findObjectType(int classIndex, RuntimeConstantPool pool, boolean resolve, Meta meta) {
85+
public static Klass findObjectType(int classIndex, RuntimeConstantPool pool, boolean resolve, boolean checkAccess, Meta meta) {
8486
ResolvedConstant resolvedConstant = pool.peekResolvedOrNull(classIndex, meta);
8587
if (resolvedConstant != null) {
8688
if (!resolve && !resolvedConstant.isSuccess()) {
@@ -98,6 +100,6 @@ public static Klass findObjectType(int classIndex, RuntimeConstantPool pool, boo
98100
if (type == null || TypeSymbols.isPrimitive(type)) {
99101
return null;
100102
}
101-
return findObjectType(type, pool.getHolder(), resolve, meta);
103+
return findObjectType(type, pool.getHolder(), resolve, checkAccess, meta);
102104
}
103105
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private static StaticObject lookupNonPrimitiveType(ByteSequence typeDescriptor,
130130
return toJVMCIUnresolvedType(typeDescriptor, createUnresolved, meta);
131131
}
132132
}
133-
Klass result = findObjectType(symbol, accessingKlass, resolve, meta);
133+
Klass result = findObjectType(symbol, accessingKlass, resolve, false, meta);
134134
if (result == null) {
135135
assert !resolve;
136136
return toJVMCIUnresolvedType(symbol, createUnresolved, meta);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private static StaticObject resolvedConstantToJVMCIObjectType(ResolvedConstant r
296296

297297
private static Klass lookupSymbolicHolder(int cpi, RuntimeConstantPool constantPool, Meta meta) {
298298
int holderClassIndex = constantPool.memberClassIndex(cpi);
299-
return findObjectType(holderClassIndex, constantPool, false, meta);
299+
return findObjectType(holderClassIndex, constantPool, false, true, meta);
300300
}
301301

302302
private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, RuntimeConstantPool constantPool, Meta meta) {
@@ -394,7 +394,7 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
394394
}
395395
Klass klass;
396396
try {
397-
klass = findObjectType(classCpi, constantPool, false, meta);
397+
klass = findObjectType(classCpi, constantPool, false, true, meta);
398398
} catch (EspressoException e) {
399399
throw EspressoError.shouldNotReachHere("findObjectType with resolve=false should never throw", e);
400400
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static StaticObject doDefault(StaticObject self, StaticObject unresolved,
110110
assert context.getLanguage().isInternalJVMCIEnabled();
111111
Meta meta = context.getMeta();
112112
Field field = (Field) meta.jvmci.HIDDEN_FIELD_MIRROR.getHiddenObject(self);
113-
Klass klass = findType(field.getType(), field.getDeclaringKlass(), false, meta);
113+
Klass klass = findType(field.getType(), field.getDeclaringKlass(), false, false, meta);
114114
if (klass != null) {
115115
LOGGER.finer(() -> "ERJF.getType0 found " + klass);
116116
return toJVMCIType(klass, objectTypeConstructor, arrayTypeConstructor, forBasicType, context, meta);

0 commit comments

Comments
 (0)