From b830e9c4971023982d6a86b85d5322931968538e Mon Sep 17 00:00:00 2001 From: Kelvin Nishikawa Date: Mon, 18 Nov 2024 23:21:01 -0800 Subject: [PATCH] Removing conditional multi-memory support, but making Spec.Test fail multiple memory validation. --- Spec.Test/WastTests.cs | 4 ++++ Wacs.Core/Instructions/MemoryBulk.cs | 28 +--------------------------- Wacs.Core/Types/MemArg.cs | 4 ++-- Wacs.Core/Validation/Validation.cs | 13 ++++++++----- Wacs.Core/Wacs.Core.csproj | 4 ++-- 5 files changed, 17 insertions(+), 36 deletions(-) diff --git a/Spec.Test/WastTests.cs b/Spec.Test/WastTests.cs index 78b594d..f3af9f5 100644 --- a/Spec.Test/WastTests.cs +++ b/Spec.Test/WastTests.cs @@ -16,6 +16,7 @@ using Wacs.Core; using Wacs.Core.Runtime; +using Wacs.Core.Validation; using Xunit; using Xunit.Abstractions; @@ -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) diff --git a/Wacs.Core/Instructions/MemoryBulk.cs b/Wacs.Core/Instructions/MemoryBulk.cs index 438bd2f..386cae1 100644 --- a/Wacs.Core/Instructions/MemoryBulk.cs +++ b/Wacs.Core/Instructions/MemoryBulk.cs @@ -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; } } @@ -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; } } @@ -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; } @@ -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; } } @@ -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; } } diff --git a/Wacs.Core/Types/MemArg.cs b/Wacs.Core/Types/MemArg.cs index 4e05c56..3d8e880 100644 --- a/Wacs.Core/Types/MemArg.cs +++ b/Wacs.Core/Types/MemArg.cs @@ -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); } diff --git a/Wacs.Core/Validation/Validation.cs b/Wacs.Core/Validation/Validation.cs index 64b4b4c..0f70b49 100644 --- a/Wacs.Core/Validation/Validation.cs +++ b/Wacs.Core/Validation/Validation.cs @@ -24,6 +24,8 @@ namespace Wacs.Core.Validation /// public class ModuleValidator : AbstractValidator { + public static bool ValidateMultipleMemories = true; + public ModuleValidator() { //Set the validation context @@ -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()); diff --git a/Wacs.Core/Wacs.Core.csproj b/Wacs.Core/Wacs.Core.csproj index 9c10c32..d3cb041 100644 --- a/Wacs.Core/Wacs.Core.csproj +++ b/Wacs.Core/Wacs.Core.csproj @@ -36,12 +36,12 @@ - TRACE;MULTI_MEMORY + TRACE; true - TRACE;MULTI_MEMORY + TRACE;