Skip to content

Commit b0c5b43

Browse files
committed
fix rebase issues, disallow branches out of a finally handler
1 parent 026778b commit b0c5b43

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

truffle/src/com.oracle.truffle.api.operation.test/src/com/oracle/truffle/api/operation/test/TestOperations.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import com.oracle.truffle.api.dsl.Bind;
5050
import com.oracle.truffle.api.dsl.Cached;
5151
import com.oracle.truffle.api.dsl.GenerateAOT;
52-
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5352
import com.oracle.truffle.api.dsl.NeverDefault;
5453
import com.oracle.truffle.api.dsl.Specialization;
5554
import com.oracle.truffle.api.frame.FrameDescriptor;
@@ -63,12 +62,10 @@
6362
import com.oracle.truffle.api.operation.LocalSetter;
6463
import com.oracle.truffle.api.operation.LocalSetterRange;
6564
import com.oracle.truffle.api.operation.Operation;
66-
import com.oracle.truffle.api.operation.OperationProxy;
6765
import com.oracle.truffle.api.operation.OperationRootNode;
6866
import com.oracle.truffle.api.operation.ShortCircuitOperation;
6967
import com.oracle.truffle.api.operation.Variadic;
7068
import com.oracle.truffle.api.operation.test.GenerateOperationsTestVariants.Variant;
71-
import com.oracle.truffle.api.operation.tracing.TracingMetadata.SpecializationNames;
7269

7370
@GenerateOperationsTestVariants({
7471
@Variant(suffix = "Base", configuration = @GenerateOperations(languageClass = TestOperationsLanguage.class, enableYield = true, enableSerialization = true)),

truffle/src/com.oracle.truffle.api.operation.test/src/com/oracle/truffle/api/operation/test/TestOperationsFinallyTryTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ public void testFinallyTryBranchForwardOutOfHandler() {
180180
// lbl:
181181
// arg0.append(4);
182182

183-
RootCallTarget root = parse("finallyTryBranchForwardOutOfHandler", b -> {
183+
thrown.expect(IllegalStateException.class);
184+
thrown.expectMessage("Branches inside finally handlers can only target labels defined in the same handler.");
185+
parse("finallyTryBranchForwardOutOfHandler", b -> {
184186
b.beginRoot(LANGUAGE);
185187
OperationLabel lbl = b.createLabel();
186188

@@ -203,8 +205,6 @@ public void testFinallyTryBranchForwardOutOfHandler() {
203205

204206
b.endRoot();
205207
});
206-
207-
testOrdering(false, root, 1L, 2L, 4L);
208208
}
209209

210210
@Test
@@ -851,7 +851,9 @@ public void testFinallyTryBranchIntoOuterFinally() {
851851
// return 0;
852852
// }
853853

854-
RootCallTarget root = parse("finallyTryBranchIntoOuterFinally", b -> {
854+
thrown.expect(IllegalStateException.class);
855+
thrown.expectMessage("Branches inside finally handlers can only target labels defined in the same handler.");
856+
parse("finallyTryBranchIntoOuterFinally", b -> {
855857
b.beginRoot(LANGUAGE);
856858

857859
b.beginFinallyTry();
@@ -887,8 +889,6 @@ public void testFinallyTryBranchIntoOuterFinally() {
887889
b.endFinallyTry();
888890
b.endRoot();
889891
});
890-
891-
testOrdering(false, root, 1L, 3L, 5L, 8L);
892892
}
893893

894894
@Test
@@ -921,7 +921,9 @@ public void testFinallyTryBranchIntoOuterFinallyNestedInAnotherFinally() {
921921
// return 0;
922922
// }
923923

924-
RootCallTarget root = parse("finallyTryBranchIntoOuterFinallyNestedInAnotherFinally", b -> {
924+
thrown.expect(IllegalStateException.class);
925+
thrown.expectMessage("Branches inside finally handlers can only target labels defined in the same handler.");
926+
parse("finallyTryBranchIntoOuterFinallyNestedInAnotherFinally", b -> {
925927
b.beginRoot(LANGUAGE);
926928

927929
b.beginFinallyTry(); // a
@@ -970,8 +972,6 @@ public void testFinallyTryBranchIntoOuterFinallyNestedInAnotherFinally() {
970972
b.endFinallyTry();
971973
b.endRoot();
972974
});
973-
974-
testOrdering(false, root, 1L, 3L, 5L, 6L, 8L, 11L);
975975
}
976976

977977
@Test

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/operations/generator/OperationsNodeFactory.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ private CodeTypeElement create() {
16571657
builder.add(createDoEmitLeaves());
16581658
builder.add(createAllocateNode());
16591659
builder.add(createInFinallyTryHandler());
1660+
builder.add(createGetFinallyTryHandlerSequenceNumber());
16601661
if (model.enableSerialization) {
16611662
builder.add(createSerialize());
16621663
builder.add(createDeserialize());
@@ -2024,7 +2025,7 @@ private CodeExecutableElement createCreateBranchStackHeightMapping() {
20242025
b.statement("HashMap<Integer, Integer> result = new HashMap<>()");
20252026
b.startFor().string("OperationLabel lbl : unresolvedLabels.keySet()").end().startBlock();
20262027
b.startFor().startGroup().type(bytecodeLocation.asType()).string(" site : unresolvedLabels.get(lbl)").end(2).startBlock();
2027-
b.statement("assert !result.containsKey(site)");
2028+
b.statement("assert !result.containsKey(site.bci)");
20282029
b.statement("result.put(site.bci, site.sp)");
20292030
b.end(2);
20302031
b.startReturn().string("result").end();
@@ -2627,19 +2628,18 @@ private void buildEmitOperationInstruction(CodeTreeBuilder b, OperationModel ope
26272628
b.string("curStack");
26282629
b.end(2);
26292630
b.newLine();
2630-
b.lineComment("We need to track branch targets inside finally handlers so that they can be adjusted each time the handler is emitted.");
2631-
b.startIf().string("label.finallyTryOp != " + UNINIT).end().startBlock();
2632-
// An earlier step has validated that the label is defined by an operation on
2633-
// the stack. We should be able to find the defining FinallyTry context without
2634-
// hitting an NPE.
2635-
b.statement("FinallyTryContext ctx = finallyTryContext");
2636-
b.startWhile().string("ctx.finallyTrySequenceNumber != label.finallyTryOp").end().startBlock();
2637-
b.statement("ctx = ctx.parentContext");
2631+
2632+
// Branches inside finally handlers can only be relative to the handler,
2633+
// otherwise a finally handler emitted before a "return" could branch out of the
2634+
// handler and circumvent the return.
2635+
b.startIf().string("inFinallyTryHandler(finallyTryContext)").end().startBlock();
2636+
2637+
b.startIf().string("label.finallyTryOp != finallyTryContext.finallyTrySequenceNumber").end().startBlock();
2638+
buildThrowIllegalStateException(b, "\"Branches inside finally handlers can only target labels defined in the same handler.\"");
26382639
b.end();
26392640

2640-
b.startIf().string("inFinallyTryHandler(ctx)").end().startBlock();
2641+
b.lineComment("We need to track branch targets inside finally handlers so that they can be adjusted each time the handler is emitted.");
26412642
b.statement("finallyTryContext.finallyRelativeBranches.add(bci + 1)");
2642-
b.end();
26432643

26442644
b.end();
26452645
yield new String[]{UNINIT};
@@ -3486,6 +3486,18 @@ private CodeExecutableElement createInFinallyTryHandler() {
34863486
return ex;
34873487
}
34883488

3489+
private CodeExecutableElement createGetFinallyTryHandlerSequenceNumber() {
3490+
CodeExecutableElement ex = new CodeExecutableElement(Set.of(PRIVATE), context.getType(int.class), "getFinallyTryHandlerSequenceNumber");
3491+
ex.addParameter(new CodeVariableElement(finallyTryContext.asType(), "context"));
3492+
CodeTreeBuilder b = ex.createBuilder();
3493+
3494+
b.startReturn();
3495+
b.string("inFinallyTryHandler(context) ? context.finallyTrySequenceNumber : -1");
3496+
b.end();
3497+
3498+
return ex;
3499+
}
3500+
34893501
private static String writeBc(String index, String value) {
34903502
return String.format("ACCESS.shortArrayWrite(bc, %s, %s)", index, value);
34913503
}

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/operations/model/InstructionModel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import com.oracle.truffle.dsl.processor.ProcessorContext;
5050
import com.oracle.truffle.dsl.processor.java.model.CodeTypeElement;
5151
import com.oracle.truffle.dsl.processor.model.NodeData;
52-
import com.oracle.truffle.dsl.processor.operations.model.InstructionModel.Signature;
5352

5453
public class InstructionModel implements InfoDumpable {
5554
public enum InstructionKind {

truffle/src/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLFunctionLiteralNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
@NodeChild("functionName")
6565
public abstract class SLFunctionLiteralNode extends SLExpressionNode {
6666

67-
@SuppressWarnings("unused")
67+
@SuppressWarnings({"unused", "truffle-neverdefault"})
6868
@Specialization
6969
public static SLFunction perform(
7070
TruffleString functionName,

0 commit comments

Comments
 (0)