Skip to content

Commit 8c87d18

Browse files
Crema: fix invokedynamic patching the wrong cpi
1 parent 1006af4 commit 8c87d18

File tree

4 files changed

+13
-27
lines changed

4 files changed

+13
-27
lines changed

substratevm/src/com.oracle.svm.interpreter.metadata/src/com/oracle/svm/interpreter/metadata/BytecodeStream.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,6 @@ public static char readCPI2(byte[] code, int curBCI) {
260260
return (char) ByteUtils.beU2(code, curBCI + 1);
261261
}
262262

263-
/**
264-
* Reads a 2-byte constant pool index for the current instruction, reading each byte with
265-
* volatile semantics. Note that this does not read the 2 bytes atomically.
266-
*
267-
* @return the constant pool index
268-
*/
269-
public static char readCPI2Volatile(byte[] code, int curBCI) {
270-
return (char) (((ByteUtils.volatileBeU1(code, curBCI + 1) & 0xff) << 8) | (ByteUtils.volatileBeU1(code, curBCI + 1 + 1) & 0xff));
271-
}
272-
273263
/**
274264
* Reads a constant pool index for the current instruction.
275265
*
@@ -371,25 +361,20 @@ private static String unknownVariableLengthBytecodeMessage(int opcode) {
371361
return "unknown variable-length bytecode: " + opcode;
372362
}
373363

374-
@Platforms(Platform.HOSTED_ONLY.class)
375-
public static void patchAppendixCPI(byte[] code, int curBCI, int appendixCPI) {
376-
int opcode = opcode(code, curBCI);
377-
switch (opcode) {
378-
case INVOKEDYNAMIC:
379-
code[curBCI + 3] = (byte) ((appendixCPI >> 8) & 0xFF);
380-
code[curBCI + 4] = (byte) (appendixCPI & 0xFF);
381-
break;
382-
default:
383-
throw VMError.shouldNotReachHereAtRuntime();
384-
}
364+
/**
365+
* Reads the 2-byte extra CPI for the current invokedynamic instruction, reading each byte with
366+
* volatile semantics. Note that this does not read the 2 bytes atomically.
367+
*/
368+
public static char readIndyExtraCPIVolatile(byte[] code, int curBCI) {
369+
return (char) ((ByteUtils.volatileBeU1(code, curBCI + 3) << 8) | ByteUtils.volatileBeU1(code, curBCI + 4));
385370
}
386371

387372
public static void patchIndyExtraCPI(byte[] code, int curBCI, int extraCPI) {
388373
int opcode = opcode(code, curBCI);
389374
switch (opcode) {
390375
case INVOKEDYNAMIC:
391-
code[curBCI + 1] = (byte) ((extraCPI >> 8) & 0xFF);
392-
code[curBCI + 2] = (byte) (extraCPI & 0xFF);
376+
code[curBCI + 3] = (byte) ((extraCPI >> 8) & 0xFF);
377+
code[curBCI + 4] = (byte) (extraCPI & 0xFF);
393378
break;
394379
default:
395380
throw VMError.shouldNotReachHereAtRuntime();

substratevm/src/com.oracle.svm.interpreter/src/com/oracle/svm/interpreter/BuildTimeConstantPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public void hydrate(InterpreterResolvedObjectType type) {
448448
// in the CP.
449449
newAppendixCPI = appendixConstant(JavaConstant.NULL_POINTER);
450450
}
451-
BytecodeStream.patchAppendixCPI(code, bci, newAppendixCPI);
451+
BytecodeStream.patchIndyExtraCPI(code, bci, newAppendixCPI);
452452
}
453453

454454
BytecodeStream.patchCPI(code, bci, newCPI);

substratevm/src/com.oracle.svm.interpreter/src/com/oracle/svm/interpreter/Interpreter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,8 @@ private static int invoke(InterpreterFrame callerFrame, InterpreterResolvedJavaM
14011401
throw SemanticJavaException.raise(e);
14021402
}
14031403
BytecodeStream.patchIndyExtraCPI(code, curBCI, extraCPI);
1404-
assert BytecodeStream.readCPI2Volatile(code, curBCI) == extraCPI;
1404+
assert BytecodeStream.readIndyExtraCPIVolatile(code, curBCI) == extraCPI;
1405+
assert BytecodeStream.readCPI2(code, curBCI) == indyCPI;
14051406
}
14061407
CallSiteLink link = invokeDynamicConstant.getCallSiteLink(extraCPI);
14071408
while (!link.matchesCallSite(method, curBCI)) {
@@ -1411,7 +1412,7 @@ private static int invoke(InterpreterFrame callerFrame, InterpreterResolvedJavaM
14111412
* still safe to use in `getCallSiteLink`. `matchesCallSite` ensures we have the
14121413
* full extraCPI.
14131414
*/
1414-
extraCPI = BytecodeStream.readCPI2Volatile(code, curBCI);
1415+
extraCPI = BytecodeStream.readIndyExtraCPIVolatile(code, curBCI);
14151416
link = invokeDynamicConstant.getCallSiteLink(extraCPI);
14161417
}
14171418
if (link instanceof SuccessfulCallSiteLink successfulCallSiteLink) {

substratevm/src/com.oracle.svm.interpreter/src/com/oracle/svm/interpreter/classfile/ClassFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ private byte[] recomputeConstantPoolIndices(ResolvedJavaMethod method) {
10001000
// The VM already provides the resolved bootstrap method and appendix.
10011001
// Investigate how to persist the appendix (arbitrary object) on the constant pool.
10021002
BytecodeStream.patchCPI(code, bci, 0);
1003-
BytecodeStream.patchAppendixCPI(code, bci, 0);
1003+
BytecodeStream.patchIndyExtraCPI(code, bci, 0);
10041004
break;
10051005
}
10061006
// @formatter:on

0 commit comments

Comments
 (0)