A middle block should not start at beginning of a CATCH block

This commit is contained in:
Volker Berlin 2022-07-16 15:17:59 +02:00
parent e512ee4e65
commit 0ff60a2809
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB
2 changed files with 35 additions and 8 deletions

View File

@ -1507,7 +1507,23 @@ class BranchManager {
* @return the new node * @return the new node
*/ */
private BranchNode addMiddleNode( BranchNode parent, int startPos, int endPos ) { private BranchNode addMiddleNode( BranchNode parent, int startPos, int endPos ) {
BranchNode middleNode = new BranchNode( startPos, endPos, WasmBlockOperator.BLOCK, WasmBlockOperator.END ); Object data = null;
// use same input parameter if parent or child start on same position
if( parent.startPos == startPos ) {
WasmBlockOperator startOp = parent.startOp;
if( startOp != null ) {
switch( parent.startOp ) {
case BLOCK:
data = parent.data;
break;
case CATCH:
// first instruction of a CATCH block ever saved the exception from the stack to a local variable that we start the middle block an instruction later
startPos++;
}
}
}
BranchNode middleNode = new BranchNode( startPos, endPos, WasmBlockOperator.BLOCK, WasmBlockOperator.END, data );
int idx = 0; int idx = 0;
for( Iterator<BranchNode> it = parent.iterator(); it.hasNext(); ) { for( Iterator<BranchNode> it = parent.iterator(); it.hasNext(); ) {
BranchNode child = it.next(); BranchNode child = it.next();
@ -1520,18 +1536,13 @@ class BranchManager {
} }
middleNode.add( child ); middleNode.add( child );
it.remove(); it.remove();
}
// use same input parameter if parent or child start on same position if( child.startPos == startPos && child.startOp == WasmBlockOperator.BLOCK ) {
if( parent.startPos == startPos ) {
middleNode.data = parent.data;
} else if( middleNode.size() > 0 ) {
BranchNode child = middleNode.get( 0 );
if( child.startPos == startPos ) {
middleNode.data = child.data; middleNode.data = child.data;
} }
} }
parent.add( idx, middleNode ); parent.add( idx, middleNode );
patchBrDeep( middleNode ); patchBrDeep( middleNode );
return middleNode; return middleNode;

View File

@ -59,6 +59,7 @@ public class Exceptions extends AbstractBaseTest {
addParam( list, script, "serialCatch" ); addParam( list, script, "serialCatch" );
addParam( list, script, "tryReturn" ); addParam( list, script, "tryReturn" );
addParam( list, script, "whileTrueTryFinally" ); addParam( list, script, "whileTrueTryFinally" );
addParam( list, script, "ifMultipleInFinally" );
} }
rule.setTestParameters( list ); rule.setTestParameters( list );
rule.setProperty( JWebAssembly.WASM_USE_EH, "true" ); rule.setProperty( JWebAssembly.WASM_USE_EH, "true" );
@ -276,6 +277,21 @@ public class Exceptions extends AbstractBaseTest {
return sw; return sw;
} }
@Export
private static int ifMultipleInFinally() throws Throwable {
int pos = 0;
try {
pos = 13;
} catch( Throwable ex ) {
pos = 42;
} finally {
if( pos < -13 || pos == 0 ) {
return 17;
}
}
return pos;
}
// @Export // @Export
// static int npe() { // static int npe() {
// Object obj = new NullPointerException(); // Object obj = new NullPointerException();