Skip to content

Commit

Permalink
Fixing broken tests shared stack concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
kelnishi committed Dec 4, 2024
1 parent fce7cc4 commit fb4f9cf
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
38 changes: 15 additions & 23 deletions Wacs.Core/Instructions/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public override void Validate(IWasmValidationContext context)
context.Assert(funcType, "Invalid BlockType: {0}",Block.Type);

//Check the parameters [t1*] and discard
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(funcType.ParameterTypes);

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(BlockOp, funcType);
Expand Down Expand Up @@ -164,8 +163,7 @@ public override void Validate(IWasmValidationContext context)
context.Assert(funcType, "Invalid BlockType: {0}",Block.Type);

//Check the parameters [t1*] and discard
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(funcType.ParameterTypes);

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(LoopOp, funcType);
Expand Down Expand Up @@ -247,8 +245,7 @@ public override void Validate(IWasmValidationContext context)
context.OpStack.PopI32();

//Check the parameters [t1*] and discard
context.OpStack.PopValues(ifType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(ifType.ParameterTypes);

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(IfOp, ifType);
Expand Down Expand Up @@ -444,8 +441,7 @@ public override void Validate(IWasmValidationContext context)
var nthFrame = context.ControlStack.PeekAt((int)L.Value);

//Validate results, but leave them on the stack
context.OpStack.PopValues(nthFrame.LabelTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(nthFrame.LabelTypes);

// if (!context.Unreachable)
// nthFrame.ConditionallyReachable = true;
Expand Down Expand Up @@ -544,7 +540,7 @@ public override void Validate(IWasmValidationContext context)
// nthFrame.ConditionallyReachable = true;

//Pop values like we branch
context.OpStack.PopValues(nthFrame.LabelTypes, ref _aside);
context.OpStack.DiscardValues(nthFrame.LabelTypes);
//But actually, we don't, so push them back on.
context.OpStack.PushResult(nthFrame.LabelTypes);
}
Expand Down Expand Up @@ -610,7 +606,8 @@ public override void Validate(IWasmValidationContext context)

// if (!context.Unreachable)
// mthFrame.ConditionallyReachable = true;


Stack<Value> aside = new();
foreach (var lidx in Ls)
{
context.Assert(context.ContainsLabel(lidx.Value),
Expand All @@ -619,16 +616,12 @@ public override void Validate(IWasmValidationContext context)
var nthFrame = context.ControlStack.PeekAt((int)lidx.Value);
context.Assert(nthFrame.LabelTypes.Arity == arity,
"Instruction br_table invalid. Label {0} had different arity {1} =/= {2}", lidx, nthFrame.LabelTypes.Arity,arity);

// if (!context.Unreachable)
// nthFrame.ConditionallyReachable = true;

context.OpStack.PopValues(nthFrame.LabelTypes, ref _aside);
context.OpStack.PushValues(_aside);
context.OpStack.PopValues(nthFrame.LabelTypes, ref aside);
context.OpStack.PushValues(aside);
}

context.OpStack.PopValues(mthFrame.LabelTypes, ref _aside);
_aside.Clear();
context.OpStack.PopValues(mthFrame.LabelTypes, ref aside);
context.SetUnreachable();
}

Expand Down Expand Up @@ -723,8 +716,9 @@ public sealed class InstReturn : InstructionBase
public override void Validate(IWasmValidationContext context)
{
//keep the results for the block or function to validate
context.OpStack.PopValues(context.ReturnType, ref _aside);
context.OpStack.PushValues(_aside);
Stack<Value> aside = new();
context.OpStack.PopValues(context.ReturnType, ref aside);
context.OpStack.PushValues(aside);
context.SetUnreachable();
}

Expand Down Expand Up @@ -770,8 +764,7 @@ public override void Validate(IWasmValidationContext context)
"Instruction call was invalid. Function {0} was not in the Context.",X);
var func = context.Funcs[X];
var type = context.Types[func.TypeIndex];
context.OpStack.PopValues(type.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(type.ParameterTypes);
context.OpStack.PushResult(type.ResultType);
}

Expand Down Expand Up @@ -891,8 +884,7 @@ public override void Validate(IWasmValidationContext context)
var funcType = context.Types[Y];

context.OpStack.PopI32();
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(funcType.ParameterTypes);
context.OpStack.PushResult(funcType.ResultType);
}

Expand Down
2 changes: 0 additions & 2 deletions Wacs.Core/Instructions/InstructionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ namespace Wacs.Core.Instructions
/// </summary>
public abstract class InstructionBase
{
protected static Stack<Value> _aside = new();

public bool IsAsync = false;
public int Size = 1;

Expand Down
18 changes: 10 additions & 8 deletions Wacs.Core/Instructions/TailCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ public bool IsBound(ExecContext context)
public override void Validate(IWasmValidationContext context)
{
//Return
context.OpStack.PopValues(context.ReturnType, ref _aside);
context.OpStack.PushValues(_aside);
Stack<Value> aside = new();
context.OpStack.PopValues(context.ReturnType, ref aside);
context.OpStack.PushValues(aside);
context.SetUnreachable();

//Call
context.Assert(context.Funcs.Contains(X),
"Instruction call was invalid. Function {0} was not in the Context.",X);
var func = context.Funcs[X];
var type = context.Types[func.TypeIndex];
context.OpStack.PopValues(type.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.PopValues(type.ParameterTypes, ref aside);
aside.Clear();
context.OpStack.PushResult(type.ResultType);
}

Expand Down Expand Up @@ -203,8 +204,9 @@ public bool IsBound(ExecContext context)
public override void Validate(IWasmValidationContext context)
{
//Return
context.OpStack.PopValues(context.ReturnType, ref _aside);
context.OpStack.PushValues(_aside);
Stack<Value> aside = new();
context.OpStack.PopValues(context.ReturnType, ref aside);
context.OpStack.PushValues(aside);
context.SetUnreachable();

//Call Indirect
Expand All @@ -218,8 +220,8 @@ public override void Validate(IWasmValidationContext context)
var funcType = context.Types[Y];

context.OpStack.PopI32();
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.PopValues(funcType.ParameterTypes, ref aside);
aside.Clear();
context.OpStack.PushResult(funcType.ResultType);
}

Expand Down
3 changes: 1 addition & 2 deletions Wacs.Core/Instructions/Transpiler/InstCompoundIf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public override void Validate(IWasmValidationContext context)
// context.OpStack.PopI32();

//Check the parameters [t1*] and discard
context.OpStack.PopValues(ifType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.DiscardValues(ifType.ParameterTypes);

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(IfOp, ifType);
Expand Down
4 changes: 4 additions & 0 deletions Wacs.Core/Modules/Sections/StackCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void PushResult(ResultType types)
public void PushValues(Stack<Value> vals) {
while (vals.Count > 0) _context.Push(vals.Pop().Type);
}

public void DiscardValues(ResultType types) {
foreach (var type in types.Types.Reverse()) PopType(type);
}

public Value PopI32() => _context.Pop(ValType.I32);
public Value PopI64() => _context.Pop(ValType.I64);
Expand Down
4 changes: 4 additions & 0 deletions Wacs.Core/Modules/Sections/StackRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void PushResult(ResultType types)
public void PushValues(Stack<Value> vals) {
while (vals.Count > 0) _context.Push(vals.Pop().Type);
}

public void DiscardValues(ResultType types) {
foreach (var type in types.Types.Reverse()) PopType(type);
}

public Value PopI32() => _context.Pop(ValType.I32);
public Value PopI64() => _context.Pop(ValType.I64);
Expand Down
10 changes: 10 additions & 0 deletions Wacs.Core/Validation/ValidationOpStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public interface IValidationOpStack
Value PopAny();

void PopValues(ResultType types, ref Stack<Value> aside);
void DiscardValues(ResultType types);

public void ReturnResults(ResultType type);
}

Expand Down Expand Up @@ -258,6 +260,14 @@ public void PopValues(ResultType types, ref Stack<Value> aside)
aside.Push(PopType(type));
}
}

public void DiscardValues(ResultType types)
{
foreach (var type in types.Types.Reverse())
{
PopType(type);
}
}

public void ReturnResults(ResultType types)
{
Expand Down

0 comments on commit fb4f9cf

Please sign in to comment.