Skip to content

Commit 0324103

Browse files
authored
Painless: Fix bug for static method calls on interfaces (#31348)
Static method calls on interfaces were not being called correctly which was causing JVM crashes. This change fixes the issue.
1 parent d6d0727 commit 0324103

File tree

10 files changed

+51
-11
lines changed

10 files changed

+51
-11
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ private static MethodHandle lookupReferenceInternal(Definition definition, Looku
376376
ref.delegateClassName,
377377
ref.delegateInvokeType,
378378
ref.delegateMethodName,
379-
ref.delegateMethodType
379+
ref.delegateMethodType,
380+
ref.isDelegateInterface ? 1 : 0
380381
);
381382
return callSite.dynamicInvoker().asType(MethodType.methodType(clazz.clazz, captures));
382383
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.painless;
2121

2222
import org.elasticsearch.painless.spi.Whitelist;
23+
import org.objectweb.asm.Opcodes;
2324

2425
import java.lang.invoke.MethodHandle;
2526
import java.lang.invoke.MethodHandles;
@@ -202,16 +203,28 @@ public MethodType getMethodType() {
202203

203204
public void write(MethodWriter writer) {
204205
final org.objectweb.asm.Type type;
206+
final Class<?> clazz;
205207
if (augmentation != null) {
206208
assert java.lang.reflect.Modifier.isStatic(modifiers);
209+
clazz = augmentation;
207210
type = org.objectweb.asm.Type.getType(augmentation);
208211
} else {
212+
clazz = owner.clazz;
209213
type = owner.type;
210214
}
211215

212216
if (java.lang.reflect.Modifier.isStatic(modifiers)) {
213-
writer.invokeStatic(type, method);
214-
} else if (java.lang.reflect.Modifier.isInterface(owner.clazz.getModifiers())) {
217+
// invokeStatic assumes that the owner class is not an interface, so this is a
218+
// special case for interfaces where the interface method boolean needs to be set to
219+
// true to reference the appropriate class constant when calling a static interface
220+
// method since java 8 did not check, but java 9 and 10 do
221+
if (java.lang.reflect.Modifier.isInterface(clazz.getModifiers())) {
222+
writer.visitMethodInsn(Opcodes.INVOKESTATIC,
223+
type.getInternalName(), name, getMethodType().toMethodDescriptorString(), true);
224+
} else {
225+
writer.invokeStatic(type, method);
226+
}
227+
} else if (java.lang.reflect.Modifier.isInterface(clazz.getModifiers())) {
215228
writer.invokeInterface(type, method);
216229
} else {
217230
writer.invokeVirtual(type, method);

modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class FunctionRef {
6666
/** delegate method type method as type */
6767
public final Type delegateType;
6868

69+
/** whether a call is made on a delegate interface */
70+
public final boolean isDelegateInterface;
71+
6972
/**
7073
* Creates a new FunctionRef, which will resolve {@code type::call} from the whitelist.
7174
* @param definition the whitelist against which this script is being compiled
@@ -97,10 +100,13 @@ public FunctionRef(Class<?> expected, Method interfaceMethod, Method delegateMet
97100
// the Painless$Script class can be inferred if owner is null
98101
if (delegateMethod.owner == null) {
99102
delegateClassName = CLASS_NAME;
103+
isDelegateInterface = false;
100104
} else if (delegateMethod.augmentation != null) {
101105
delegateClassName = delegateMethod.augmentation.getName();
106+
isDelegateInterface = delegateMethod.augmentation.isInterface();
102107
} else {
103108
delegateClassName = delegateMethod.owner.clazz.getName();
109+
isDelegateInterface = delegateMethod.owner.clazz.isInterface();
104110
}
105111

106112
if ("<init>".equals(delegateMethod.name)) {
@@ -139,6 +145,7 @@ public FunctionRef(Class<?> expected,
139145
delegateInvokeType = H_INVOKESTATIC;
140146
this.delegateMethodName = delegateMethodName;
141147
this.delegateMethodType = delegateMethodType.dropParameterTypes(0, numCaptures);
148+
isDelegateInterface = false;
142149

143150
this.interfaceMethod = null;
144151
delegateMethod = null;

modules/lang-painless/src/main/java/org/elasticsearch/painless/LambdaBootstrap.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ private Capture(int count, Class<?> type) {
188188
* @param delegateMethodName The name of the method to be called in the Painless script class
189189
* @param delegateMethodType The type of method call in the Painless script class without
190190
* the captured types
191+
* @param isDelegateInterface If the method to be called is owned by an interface where
192+
* if the value is '1' if the delegate is an interface and '0'
193+
* otherwise; note this is an int because the bootstrap method
194+
* cannot convert constants to boolean
191195
* @return A {@link CallSite} linked to a factory method for creating a lambda class
192196
* that implements the expected functional interface
193197
* @throws LambdaConversionException Thrown when an illegal type conversion occurs at link time
@@ -200,7 +204,8 @@ public static CallSite lambdaBootstrap(
200204
String delegateClassName,
201205
int delegateInvokeType,
202206
String delegateMethodName,
203-
MethodType delegateMethodType)
207+
MethodType delegateMethodType,
208+
int isDelegateInterface)
204209
throws LambdaConversionException {
205210
Loader loader = (Loader)lookup.lookupClass().getClassLoader();
206211
String lambdaClassName = Type.getInternalName(lookup.lookupClass()) + "$$Lambda" + loader.newLambdaIdentifier();
@@ -225,7 +230,7 @@ public static CallSite lambdaBootstrap(
225230

226231
generateInterfaceMethod(cw, factoryMethodType, lambdaClassType, interfaceMethodName,
227232
interfaceMethodType, delegateClassType, delegateInvokeType,
228-
delegateMethodName, delegateMethodType, captures);
233+
delegateMethodName, delegateMethodType, isDelegateInterface == 1, captures);
229234

230235
endLambdaClass(cw);
231236

@@ -369,6 +374,7 @@ private static void generateInterfaceMethod(
369374
int delegateInvokeType,
370375
String delegateMethodName,
371376
MethodType delegateMethodType,
377+
boolean isDelegateInterface,
372378
Capture[] captures)
373379
throws LambdaConversionException {
374380

@@ -434,7 +440,7 @@ private static void generateInterfaceMethod(
434440
Handle delegateHandle =
435441
new Handle(delegateInvokeType, delegateClassType.getInternalName(),
436442
delegateMethodName, delegateMethodType.toMethodDescriptorString(),
437-
delegateInvokeType == H_INVOKEINTERFACE);
443+
isDelegateInterface);
438444
iface.invokeDynamic(delegateMethodName, Type.getMethodType(interfaceMethodType
439445
.toMethodDescriptorString()).getDescriptor(), DELEGATE_BOOTSTRAP_HANDLE,
440446
delegateHandle);

modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public final class WriterConstants {
141141

142142
/** invokedynamic bootstrap for lambda expression/method references */
143143
public static final MethodType LAMBDA_BOOTSTRAP_TYPE =
144-
MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
145-
MethodType.class, MethodType.class, String.class, int.class, String.class, MethodType.class);
144+
MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class,
145+
MethodType.class, String.class, int.class, String.class, MethodType.class, int.class);
146146
public static final Handle LAMBDA_BOOTSTRAP_HANDLE =
147147
new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(LambdaBootstrap.class),
148148
"lambdaBootstrap", LAMBDA_BOOTSTRAP_TYPE.toMethodDescriptorString(), false);

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECapturingFunctionRef.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ void write(MethodWriter writer, Globals globals) {
121121
ref.delegateClassName,
122122
ref.delegateInvokeType,
123123
ref.delegateMethodName,
124-
ref.delegateType
124+
ref.delegateType,
125+
ref.isDelegateInterface ? 1 : 0
125126
);
126127
}
127128
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EFunctionRef.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ void write(MethodWriter writer, Globals globals) {
112112
ref.delegateClassName,
113113
ref.delegateInvokeType,
114114
ref.delegateMethodName,
115-
ref.delegateType
115+
ref.delegateType,
116+
ref.isDelegateInterface ? 1 : 0
116117
);
117118
} else {
118119
// TODO: don't do this: its just to cutover :)

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ void write(MethodWriter writer, Globals globals) {
222222
ref.delegateClassName,
223223
ref.delegateInvokeType,
224224
ref.delegateMethodName,
225-
ref.delegateType
225+
ref.delegateType,
226+
ref.isDelegateInterface ? 1 : 0
226227
);
227228
} else {
228229
// placeholder

modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ public void testNullSafeDeref() {
264264
// assertEquals(null, exec("def a = ['thing': 'bar']; a.other?.cat?.dog = 'wombat'; return a.other?.cat?.dog"));
265265
}
266266

267+
// test to ensure static interface methods are called correctly
268+
public void testStaticInterfaceMethod() {
269+
assertEquals(4, exec("def values = [1, 4, 3, 2]; values.sort(Comparator.comparing(p -> p)); return values[3]"));
270+
}
271+
267272
private void assertMustBeNullable(String script) {
268273
Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> exec(script));
269274
assertEquals("Result of null safe operator must be nullable", e.getMessage());

modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionRefTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ public void testInterfaceDefaultMethodDef() {
184184
"def map = new HashMap(); f(map::getOrDefault)"));
185185
}
186186

187+
public void testInterfaceStaticMethod() {
188+
assertEquals(-1, exec("Supplier get(Supplier supplier) { return supplier }" +
189+
"Supplier s = get(Comparator::naturalOrder); s.get().compare(1, 2)"));
190+
}
191+
187192
public void testMethodMissing() {
188193
Exception e = expectScriptThrows(IllegalArgumentException.class, () -> {
189194
exec("List l = [2, 1]; l.sort(Integer::bogus); return l.get(0);");

0 commit comments

Comments
 (0)