Skip to content

Commit

Permalink
Removing conditional multi-memory support, but making Spec.Test fail …
Browse files Browse the repository at this point in the history
…multiple memory validation.
  • Loading branch information
kelnishi committed Nov 19, 2024
1 parent 35e8766 commit b830e9c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
4 changes: 4 additions & 0 deletions Spec.Test/WastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using Wacs.Core;
using Wacs.Core.Runtime;
using Wacs.Core.Validation;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -38,6 +39,9 @@ public void RunWast(WastJson.WastJson file)
SpecTestEnv env = new SpecTestEnv();
WasmRuntime runtime = new();
env.BindToRuntime(runtime);

//Make multiple memories fail validation
ModuleValidator.ValidateMultipleMemories = false;

Module? module = null;
foreach (var command in file.Commands)
Expand Down
28 changes: 1 addition & 27 deletions Wacs.Core/Instructions/MemoryBulk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ public override void Execute(ExecContext context)
public override IInstruction Parse(BinaryReader reader)
{
M = (MemIdx)reader.ReadByte();
#if !MULTI_MEMORY
if (M != 0x00)
throw new FormatException(
$"Invalid memory.size. Multiple memories are not yet supported. memidx:{M}");
#endif
return this;
}
}
Expand Down Expand Up @@ -126,11 +121,6 @@ public override void Execute(ExecContext context)
public override IInstruction Parse(BinaryReader reader)
{
M = (MemIdx)reader.ReadByte();
#if !MULTI_MEMORY
if (M != 0x00)
throw new FormatException(
$"Invalid memory.grow. Multiple memories are not yet supported. memidx:{M}");
#endif
return this;
}
}
Expand Down Expand Up @@ -239,12 +229,7 @@ public override IInstruction Parse(BinaryReader reader)
{
X = (DataIdx)reader.ReadLeb128_u32();
Y = (MemIdx)reader.ReadByte();

#if !MULTI_MEMORY
if (Y != 0x00)
throw new FormatException(
$"Invalid memory.init. Multiple memories are not yet supported. memidx:{Y}");
#endif

return this;
}

Expand Down Expand Up @@ -422,13 +407,6 @@ public override IInstruction Parse(BinaryReader reader)
{
SrcX = (MemIdx)reader.ReadByte();
DstY = (MemIdx)reader.ReadByte();
#if !MULTI_MEMORY
if (SrcX != 0x00 || DstY != 0x00)
{
throw new FormatException(
$"Invalid memory.copy. Multiple memories are not yet supported. {SrcX} -> {DstY}");
}
#endif
return this;
}
}
Expand Down Expand Up @@ -513,10 +491,6 @@ public override void Execute(ExecContext context)
public override IInstruction Parse(BinaryReader reader)
{
X = (MemIdx)reader.ReadByte();
#if !MULTI_MEMORY
if (X.Value != 0x00)
throw new FormatException($"Invalid memory.fill. Multiple memories are not yet supported. {X}");
#endif
return this;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Wacs.Core/Types/MemArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public static MemArg Parse(BinaryReader reader)
throw new InvalidDataException($"Memory alignment exceeds page size");

MemIdx idx = default;
#if MULTI_MEMORY

if (align.HasFlag(Alignment.MemIdxSet))
{
idx = (MemIdx)reader.ReadLeb128_s32();
}
#endif

uint offset = reader.ReadLeb128_u32();
return new MemArg(align, offset, idx);
}
Expand Down
13 changes: 8 additions & 5 deletions Wacs.Core/Validation/Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Wacs.Core.Validation
/// </summary>
public class ModuleValidator : AbstractValidator<Module>
{
public static bool ValidateMultipleMemories = true;

public ModuleValidator()
{
//Set the validation context
Expand All @@ -39,11 +41,12 @@ public ModuleValidator()
.SetValidator(new Module.Function.Validator()).OverridePropertyName("Function");
RuleForEach(module => module.Tables).SetValidator(new TableType.Validator());
RuleForEach(module => module.Memories).SetValidator(new MemoryType.Validator());
#if !MULTI_MEMORY
RuleFor(module => module.MemoryCount)
.LessThan(2)
.WithMessage("Multiple memories are not supported.");
#endif
if (!ValidateMultipleMemories)
{
RuleFor(module => module.MemoryCount)
.LessThan(2)
.WithMessage("Multiple memories are not supported.");
}
RuleForEach(module => module.Globals).SetValidator(new Module.Global.Validator());
RuleForEach(module => module.Exports).SetValidator(new Module.Export.Validator());
RuleForEach(module => module.Elements).SetValidator(new Module.ElementSegment.Validator());
Expand Down
4 changes: 2 additions & 2 deletions Wacs.Core/Wacs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE;MULTI_MEMORY</DefineConstants>
<DefineConstants>TRACE;</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<DefineConstants>TRACE;MULTI_MEMORY</DefineConstants>
<DefineConstants>TRACE;</DefineConstants>
</PropertyGroup>


Expand Down

0 comments on commit b830e9c

Please sign in to comment.