Skip to content

Commit

Permalink
Painless: Fix Bug with Duplicate PainlessClasses (#32110)
Browse files Browse the repository at this point in the history
When building the PainlessMethods and PainlessFields they stored a reference to a 
PainlessClass. This reference was prior to "freezing" the PainlessClass so the data was 
both incomplete and mutable. This has been replaced with a target java class instead 
since the PainlessClass is accessible through a java class now and it requires no special 
modifications to get around a chicken and egg issue.
  • Loading branch information
jdconrad committed Jul 17, 2018
1 parent 9cdbd1b commit 1c63eb1
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ static MethodHandle lookupReference(PainlessLookup painlessLookup, MethodHandles
}
int arity = interfaceMethod.arguments.size();
PainlessMethod implMethod = lookupMethodInternal(painlessLookup, receiverClass, name, arity);
return lookupReferenceInternal(painlessLookup, methodHandlesLookup, interfaceType, implMethod.owner.name,
implMethod.name, receiverClass);
return lookupReferenceInternal(painlessLookup, methodHandlesLookup, interfaceType,
PainlessLookupUtility.anyTypeToPainlessTypeName(implMethod.target), implMethod.name, receiverClass);
}

/** Returns a method handle to an implementation of clazz, given method reference signature. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,22 @@ public FunctionRef(Class<?> expected, PainlessMethod interfaceMethod, PainlessMe
interfaceMethodType = interfaceMethod.getMethodType().dropParameterTypes(0, 1);

// the Painless$Script class can be inferred if owner is null
if (delegateMethod.owner == null) {
if (delegateMethod.target == null) {
delegateClassName = CLASS_NAME;
isDelegateInterface = false;
} else if (delegateMethod.augmentation != null) {
delegateClassName = delegateMethod.augmentation.getName();
isDelegateInterface = delegateMethod.augmentation.isInterface();
} else {
delegateClassName = delegateMethod.owner.clazz.getName();
isDelegateInterface = delegateMethod.owner.clazz.isInterface();
delegateClassName = delegateMethod.target.getName();
isDelegateInterface = delegateMethod.target.isInterface();
}

if ("<init>".equals(delegateMethod.name)) {
delegateInvokeType = H_NEWINVOKESPECIAL;
} else if (Modifier.isStatic(delegateMethod.modifiers)) {
delegateInvokeType = H_INVOKESTATIC;
} else if (delegateMethod.owner.clazz.isInterface()) {
} else if (delegateMethod.target.isInterface()) {
delegateInvokeType = H_INVOKEINTERFACE;
} else {
delegateInvokeType = H_INVOKEVIRTUAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

public final class PainlessField {
public final String name;
public final PainlessClass owner;
public final Class<?> target;
public final Class<?> clazz;
public final String javaName;
public final int modifiers;
public final MethodHandle getter;
public final MethodHandle setter;

PainlessField(String name, String javaName, PainlessClass owner, Class<?> clazz, int modifiers,
PainlessField(String name, String javaName, Class<?> target, Class<?> clazz, int modifiers,
MethodHandle getter, MethodHandle setter) {
this.name = name;
this.javaName = javaName;
this.owner = owner;
this.target = target;
this.clazz = clazz;
this.modifiers = modifiers;
this.getter = getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private void addConstructor(String ownerStructName, WhitelistConstructor whiteli
}

painlessConstructor = methodCache.computeIfAbsent(buildMethodCacheKey(ownerStruct.name, "<init>", painlessParametersTypes),
key -> new PainlessMethod("<init>", ownerStruct, null, void.class, painlessParametersTypes,
key -> new PainlessMethod("<init>", ownerStruct.clazz, null, void.class, painlessParametersTypes,
asmConstructor, javaConstructor.getModifiers(), javaHandle));
ownerStruct.constructors.put(painlessMethodKey, painlessConstructor);
} else if (painlessConstructor.arguments.equals(painlessParametersTypes) == false){
Expand Down Expand Up @@ -419,7 +419,7 @@ private void addMethod(ClassLoader whitelistClassLoader, String ownerStructName,

painlessMethod = methodCache.computeIfAbsent(
buildMethodCacheKey(ownerStruct.name, whitelistMethod.javaMethodName, painlessParametersTypes),
key -> new PainlessMethod(whitelistMethod.javaMethodName, ownerStruct, null, painlessReturnClass,
key -> new PainlessMethod(whitelistMethod.javaMethodName, ownerStruct.clazz, null, painlessReturnClass,
painlessParametersTypes, asmMethod, javaMethod.getModifiers(), javaMethodHandle));
ownerStruct.staticMethods.put(painlessMethodKey, painlessMethod);
} else if ((painlessMethod.name.equals(whitelistMethod.javaMethodName) && painlessMethod.rtn == painlessReturnClass &&
Expand All @@ -445,7 +445,7 @@ private void addMethod(ClassLoader whitelistClassLoader, String ownerStructName,

painlessMethod = methodCache.computeIfAbsent(
buildMethodCacheKey(ownerStruct.name, whitelistMethod.javaMethodName, painlessParametersTypes),
key -> new PainlessMethod(whitelistMethod.javaMethodName, ownerStruct, javaAugmentedClass, painlessReturnClass,
key -> new PainlessMethod(whitelistMethod.javaMethodName, ownerStruct.clazz, javaAugmentedClass, painlessReturnClass,
painlessParametersTypes, asmMethod, javaMethod.getModifiers(), javaMethodHandle));
ownerStruct.methods.put(painlessMethodKey, painlessMethod);
} else if ((painlessMethod.name.equals(whitelistMethod.javaMethodName) && painlessMethod.rtn.equals(painlessReturnClass) &&
Expand Down Expand Up @@ -501,7 +501,7 @@ private void addField(String ownerStructName, WhitelistField whitelistField) {
painlessField = fieldCache.computeIfAbsent(
buildFieldCacheKey(ownerStruct.name, whitelistField.javaFieldName, painlessFieldClass.getName()),
key -> new PainlessField(whitelistField.javaFieldName, javaField.getName(),
ownerStruct, painlessFieldClass, javaField.getModifiers(), null, null));
ownerStruct.clazz, painlessFieldClass, javaField.getModifiers(), null, null));
ownerStruct.staticMembers.put(whitelistField.javaFieldName, painlessField);
} else if (painlessField.clazz != painlessFieldClass) {
throw new IllegalArgumentException("illegal duplicate static fields [" + whitelistField.javaFieldName + "] " +
Expand Down Expand Up @@ -530,7 +530,7 @@ private void addField(String ownerStructName, WhitelistField whitelistField) {
painlessField = fieldCache.computeIfAbsent(
buildFieldCacheKey(ownerStruct.name, whitelistField.javaFieldName, painlessFieldClass.getName()),
key -> new PainlessField(whitelistField.javaFieldName, javaField.getName(),
ownerStruct, painlessFieldClass, javaField.getModifiers(), javaMethodHandleGetter, javaMethodHandleSetter));
ownerStruct.clazz, painlessFieldClass, javaField.getModifiers(), javaMethodHandleGetter, javaMethodHandleSetter));
ownerStruct.members.put(whitelistField.javaFieldName, painlessField);
} else if (painlessField.clazz != painlessFieldClass) {
throw new IllegalArgumentException("illegal duplicate member fields [" + whitelistField.javaFieldName + "] " +
Expand Down Expand Up @@ -615,8 +615,8 @@ private void copyStruct(String struct, List<String> children) {

for (PainlessField field : child.members.values()) {
if (owner.members.get(field.name) == null) {
owner.members.put(field.name,
new PainlessField(field.name, field.javaName, owner, field.clazz, field.modifiers, field.getter, field.setter));
owner.members.put(field.name, new PainlessField(
field.name, field.javaName, owner.clazz, field.clazz, field.modifiers, field.getter, field.setter));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static Class<?> painlessTypeNameToPainlessType(String painlessTypeName, M
painlessTypeName.charAt(arrayIndex++) == ']') {
++arrayDimensions;
} else {
throw new IllegalArgumentException("invalid painless type [" + painlessTypeName + "].");
throw new IllegalArgumentException("painless type [" + painlessTypeName + "] not found");
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public static Class<?> painlessTypeNameToPainlessType(String painlessTypeName, M
try {
return Class.forName(javaDescriptor);
} catch (ClassNotFoundException cnfe) {
throw new IllegalStateException("painless type [" + painlessTypeName + "] not found", cnfe);
throw new IllegalArgumentException("painless type [" + painlessTypeName + "] not found", cnfe);
}
}

Expand All @@ -207,7 +207,7 @@ public static void validatePainlessType(Class<?> painlessType, Collection<Class<
}

if (javaClasses.contains(painlessType) == false) {
throw new IllegalStateException("painless type [" + painlessTypeName + "] not found");
throw new IllegalArgumentException("painless type [" + painlessTypeName + "] not found");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.painless.MethodWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
Expand All @@ -30,19 +31,19 @@

public class PainlessMethod {
public final String name;
public final PainlessClass owner;
public final Class<?> target;
public final Class<?> augmentation;
public final Class<?> rtn;
public final List<Class<?>> arguments;
public final org.objectweb.asm.commons.Method method;
public final int modifiers;
public final MethodHandle handle;

public PainlessMethod(String name, PainlessClass owner, Class<?> augmentation, Class<?> rtn, List<Class<?>> arguments,
public PainlessMethod(String name, Class<?> target, Class<?> augmentation, Class<?> rtn, List<Class<?>> arguments,
org.objectweb.asm.commons.Method method, int modifiers, MethodHandle handle) {
this.name = name;
this.augmentation = augmentation;
this.owner = owner;
this.target = target;
this.rtn = rtn;
this.arguments = Collections.unmodifiableList(arguments);
this.method = method;
Expand Down Expand Up @@ -85,11 +86,11 @@ public MethodType getMethodType() {
for (int i = 0; i < arguments.size(); i++) {
params[i] = PainlessLookupUtility.painlessDefTypeToJavaObjectType(arguments.get(i));
}
returnValue = owner.clazz;
returnValue = target;
} else {
// virtual/interface method: add receiver class
params = new Class<?>[1 + arguments.size()];
params[0] = owner.clazz;
params[0] = target;
for (int i = 0; i < arguments.size(); i++) {
params[i + 1] = PainlessLookupUtility.painlessDefTypeToJavaObjectType(arguments.get(i));
}
Expand All @@ -106,8 +107,8 @@ public void write(MethodWriter writer) {
clazz = augmentation;
type = org.objectweb.asm.Type.getType(augmentation);
} else {
clazz = owner.clazz;
type = owner.type;
clazz = target;
type = Type.getType(target);
}

if (Modifier.isStatic(modifiers)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessMethodKey;
import org.elasticsearch.painless.lookup.def;
import org.objectweb.asm.Type;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -90,7 +91,7 @@ void write(MethodWriter writer, Globals globals) {

writer.newInstance(MethodWriter.getType(actual));
writer.dup();
writer.invokeConstructor(constructor.owner.type, constructor.method);
writer.invokeConstructor(Type.getType(constructor.target), constructor.method);

for (AExpression value : values) {
writer.dup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessMethodKey;
import org.elasticsearch.painless.lookup.def;
import org.objectweb.asm.Type;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -109,7 +110,7 @@ void write(MethodWriter writer, Globals globals) {

writer.newInstance(MethodWriter.getType(actual));
writer.dup();
writer.invokeConstructor(constructor.owner.type, constructor.method);
writer.invokeConstructor(Type.getType(constructor.target), constructor.method);

for (int index = 0; index < keys.size(); ++index) {
AExpression key = keys.get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.painless.lookup.PainlessClass;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessMethodKey;
import org.objectweb.asm.Type;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -104,7 +105,7 @@ void write(MethodWriter writer, Globals globals) {
argument.write(writer, globals);
}

writer.invokeConstructor(constructor.owner.type, constructor.method);
writer.invokeConstructor(Type.getType(constructor.target), constructor.method);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.painless.MethodWriter;
import org.elasticsearch.painless.lookup.PainlessField;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.objectweb.asm.Type;

import java.lang.reflect.Modifier;
import java.util.Objects;
Expand Down Expand Up @@ -63,9 +64,9 @@ void write(MethodWriter writer, Globals globals) {
writer.writeDebugInfo(location);

if (java.lang.reflect.Modifier.isStatic(field.modifiers)) {
writer.getStatic(field.owner.type, field.javaName, MethodWriter.getType(field.clazz));
writer.getStatic(Type.getType(field.target), field.javaName, MethodWriter.getType(field.clazz));
} else {
writer.getField(field.owner.type, field.javaName, MethodWriter.getType(field.clazz));
writer.getField(Type.getType(field.target), field.javaName, MethodWriter.getType(field.clazz));
}
}

Expand Down Expand Up @@ -94,9 +95,9 @@ void load(MethodWriter writer, Globals globals) {
writer.writeDebugInfo(location);

if (java.lang.reflect.Modifier.isStatic(field.modifiers)) {
writer.getStatic(field.owner.type, field.javaName, MethodWriter.getType(field.clazz));
writer.getStatic(Type.getType(field.target), field.javaName, MethodWriter.getType(field.clazz));
} else {
writer.getField(field.owner.type, field.javaName, MethodWriter.getType(field.clazz));
writer.getField(Type.getType(field.target), field.javaName, MethodWriter.getType(field.clazz));
}
}

Expand All @@ -105,9 +106,9 @@ void store(MethodWriter writer, Globals globals) {
writer.writeDebugInfo(location);

if (java.lang.reflect.Modifier.isStatic(field.modifiers)) {
writer.putStatic(field.owner.type, field.javaName, MethodWriter.getType(field.clazz));
writer.putStatic(Type.getType(field.target), field.javaName, MethodWriter.getType(field.clazz));
} else {
writer.putField(field.owner.type, field.javaName, MethodWriter.getType(field.clazz));
writer.putField(Type.getType(field.target), field.javaName, MethodWriter.getType(field.clazz));
}
}

Expand Down
Loading

0 comments on commit 1c63eb1

Please sign in to comment.