|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using FluentAssertions; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Wasmtime.Tests |
| 7 | +{ |
| 8 | + public class Memory64AccessFixture : ModuleFixture |
| 9 | + { |
| 10 | + protected override string ModuleFileName => "Memory64Access.wat"; |
| 11 | + } |
| 12 | + |
| 13 | + public class Memory64AccessTests : IClassFixture<Memory64AccessFixture>, IDisposable |
| 14 | + { |
| 15 | + private Memory64AccessFixture Fixture { get; set; } |
| 16 | + |
| 17 | + private Store Store { get; set; } |
| 18 | + |
| 19 | + private Linker Linker { get; set; } |
| 20 | + |
| 21 | + public Memory64AccessTests(Memory64AccessFixture fixture) |
| 22 | + { |
| 23 | + Fixture = fixture; |
| 24 | + Store = new Store(Fixture.Engine); |
| 25 | + Linker = new Linker(Fixture.Engine); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public unsafe void ItCanAccessMemoryWith65537Pages() |
| 30 | + { |
| 31 | + var instance = Linker.Instantiate(Store, Fixture.Module); |
| 32 | + var memory = instance.GetMemory("mem"); |
| 33 | + |
| 34 | + memory.GetLength().Should().Be(0x100010000); |
| 35 | + |
| 36 | + memory.ReadInt32(0).Should().Be(0); |
| 37 | + |
| 38 | + memory.WriteInt64(100, 1234); |
| 39 | + memory.ReadInt64(100).Should().Be(1234); |
| 40 | + |
| 41 | + memory.ReadByte(0x10000FFFF).Should().Be(0x63); |
| 42 | + memory.ReadInt16(0x10000FFFE).Should().Be(0x6364); |
| 43 | + memory.ReadInt32(0x10000FFFC).Should().Be(0x63646500); |
| 44 | + |
| 45 | + memory.ReadSingle(0x10000FFFC).Should().Be(4.2131355E+21F); |
| 46 | + |
| 47 | + var span = memory.GetSpan(0x10000FFFE, 2); |
| 48 | + span.SequenceEqual(new byte[] { 0x64, 0x63 }).Should().BeTrue(); |
| 49 | + |
| 50 | + var int16Span = memory.GetSpan<short>(0x10000, int.MaxValue); |
| 51 | + int16Span[0x7FFFFFFE].Should().Be(0x6500); |
| 52 | + |
| 53 | + int16Span = memory.GetSpan<short>(0x10002); |
| 54 | + int16Span[0x7FFFFFFE].Should().Be(0x6364); |
| 55 | + |
| 56 | + byte* ptr = (byte*)memory.GetPointer(); |
| 57 | + ptr += 0x10000FFFF; |
| 58 | + (*ptr).Should().Be(0x63); |
| 59 | + |
| 60 | + string str1 = "Hello World"; |
| 61 | + memory.WriteString(0x10000FFFF - str1.Length, str1); |
| 62 | + memory.ReadString(0x10000FFFF - str1.Length, str1.Length).Should().Be(str1); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void ItThrowsForOutOfBoundsAccess() |
| 67 | + { |
| 68 | + var instance = Linker.Instantiate(Store, Fixture.Module); |
| 69 | + var memory = instance.GetMemory("mem"); |
| 70 | + |
| 71 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 72 | + Action action = () => memory.GetSpan(); |
| 73 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 74 | + action.Should().Throw<OverflowException>(); |
| 75 | + |
| 76 | + action = () => memory.GetSpan<short>(0x10000); |
| 77 | + action.Should().Throw<OverflowException>(); |
| 78 | + |
| 79 | + action = () => memory.GetSpan(-1L, 0); |
| 80 | + action.Should().Throw<ArgumentOutOfRangeException>(); |
| 81 | + |
| 82 | + action = () => memory.GetSpan(0L, -1); |
| 83 | + action.Should().Throw<ArgumentOutOfRangeException>(); |
| 84 | + |
| 85 | + action = () => memory.ReadInt16(0x10000FFFF); |
| 86 | + action.Should().Throw<ArgumentException>(); |
| 87 | + |
| 88 | + action = () => memory.GetSpan<short>(0x10000FFFF, 1); |
| 89 | + action.Should().Throw<ArgumentException>(); |
| 90 | + } |
| 91 | + |
| 92 | + public void Dispose() |
| 93 | + { |
| 94 | + Store.Dispose(); |
| 95 | + Linker.Dispose(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments