@@ -159,18 +159,21 @@ type TempMap = Map<TypeRef,LocalIndex>;
159159
160160/** Attempts to match the `__tostack(value)` pattern. Returns `value` if a match, otherwise `0`. */
161161function matchPattern ( module : Module , expr : ExpressionRef ) : ExpressionRef {
162- if (
162+ let isFound = false ;
163+ while (
163164 _BinaryenExpressionGetId ( expr ) == ExpressionId . Call &&
164165 module . readStringCached ( _BinaryenCallGetTarget ( expr ) ) == BuiltinNames . tostack
165166 ) {
166167 assert ( _BinaryenCallGetNumOperands ( expr ) == 1 ) ;
167- return _BinaryenCallGetOperandAt ( expr , 0 ) ;
168+ expr = _BinaryenCallGetOperandAt ( expr , 0 ) ;
169+ isFound = true ;
168170 }
169- return 0 ;
171+ if ( ! isFound ) return 0 ;
172+ return expr ;
170173}
171174
172175/** Tests whether a `value` matched by `matchTostack` needs a slot. */
173- function needsSlot ( module : Module , value : ExpressionRef ) : bool {
176+ function needsSlot ( value : ExpressionRef ) : bool {
174177 switch ( _BinaryenExpressionGetId ( value ) ) {
175178 // no need to stack null pointers
176179 case ExpressionId . Const : return ! isConstZero ( value ) ;
@@ -344,7 +347,7 @@ export class ShadowStackPass extends Pass {
344347 let operand = operands [ i ] ;
345348 let match = matchPattern ( module , operand ) ;
346349 if ( ! match ) continue ;
347- if ( ! needsSlot ( module , match ) ) {
350+ if ( ! needsSlot ( match ) ) {
348351 operands [ i ] = match ;
349352 continue ;
350353 }
@@ -434,7 +437,7 @@ export class ShadowStackPass extends Pass {
434437 let value = _BinaryenLocalSetGetValue ( localSet ) ;
435438 let match = matchPattern ( module , value ) ;
436439 if ( ! match ) return ;
437- if ( ! needsSlot ( module , match ) ) {
440+ if ( ! needsSlot ( match ) ) {
438441 _BinaryenLocalSetSetValue ( localSet , match ) ;
439442 return ;
440443 }
0 commit comments