Skip to content

Commit 1b199cf

Browse files
author
Håvar Nøvik
committed
Added out of bounds memory access tests.
1 parent 08e5484 commit 1b199cf

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(import "" "mem" (memory $m 1 2))
3+
(data (i32.const 65535) "\01")
4+
(data (i32.const 65536) "\02")
5+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
using FluentAssertions;
4+
5+
using Xunit;
6+
7+
namespace Wasmtime.Tests
8+
{
9+
public class OutOfBoundsMemoryAccessFixture : ModuleFixture
10+
{
11+
protected override string ModuleFileName => "OutOfBoundsMemoryAccess.wat";
12+
}
13+
14+
public class OutOfBoundsMemoryAccessTests : IClassFixture<OutOfBoundsMemoryAccessFixture>
15+
{
16+
public OutOfBoundsMemoryAccessTests(OutOfBoundsMemoryAccessFixture fixture)
17+
{
18+
19+
Fixture = fixture;
20+
21+
Fixture.Host.ClearDefinitions();
22+
}
23+
24+
private OutOfBoundsMemoryAccessFixture Fixture { get; set; }
25+
26+
[Fact]
27+
public void ItDoesNotThrowsOnOutOfBoundsMemoryAccess()
28+
{
29+
var mem = Fixture.Host.DefineMemory("", "mem", minimum: 2, maximum: 2);
30+
31+
using var instance = Fixture.Host.Instantiate(Fixture.Module);
32+
33+
mem.ReadByte(65535).Should().Be(1);
34+
mem.ReadByte(65536).Should().Be(2);
35+
}
36+
37+
[Fact]
38+
public void ItThrowsOnOutOfBoundsMemoryAccess()
39+
{
40+
var mem = Fixture.Host.DefineMemory("", "mem", minimum: 1, maximum: 1);
41+
42+
Action action = () => { using var instance = Fixture.Host.Instantiate(Fixture.Module); };
43+
44+
action
45+
.Should()
46+
.Throw<TrapException>()
47+
.WithMessage("wasm trap: out of bounds memory access, source location: @-");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)