-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_test.go
62 lines (58 loc) · 2.05 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cedar
import (
"context"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"testing"
)
func Test_Allocation(t *testing.T) {
r := wazero.NewRuntime(context.Background())
module, err := r.Instantiate(context.Background(), cedarWasm)
if err != nil {
t.Fatal(err)
}
t.Run("two concurrent allocation must return different ptr", func(t *testing.T) {
twoConcurrentAllocationMustReturnDifferentPtr(t, module)
})
t.Run("a huge allocation must return an error", func(t *testing.T) {
aHugeAllocationMustReturnAPtr(t, module)
})
}
// aHugeAllocationMustReturnAPtr tests that a huge allocation mustn't return an error.
// As we use Wasm to run the Engine, we need to ensure that even if the user tries to allocate a huge amount of memory,
// memory will be allocated without crashing the process.
func aHugeAllocationMustReturnAPtr(t *testing.T, module api.Module) {
exportedFuncs := exportFuncs(module)
entitiesSize := uint64(1000000000) // 1GB allocation as we allocate u8 (1 byte) * size inside runtime.
entitiesPtr, err := exportedFuncs[string(allocate)].Call(context.Background(), entitiesSize)
if err != nil {
t.Fatal(err)
}
_, err = exportedFuncs[string(deallocate)].Call(context.Background(), entitiesPtr[0], entitiesSize)
if err != nil {
t.Fatal("expected an error")
}
}
func twoConcurrentAllocationMustReturnDifferentPtr(t *testing.T, module api.Module) {
exportedFuncs := exportFuncs(module)
entitiesSize := uint64(10)
entitiesPtr1, err := exportedFuncs[string(allocate)].Call(context.Background(), entitiesSize)
if err != nil {
t.Fatal(err)
}
entitiesPtr2, err := exportedFuncs[string(allocate)].Call(context.Background(), entitiesSize)
if err != nil {
t.Fatal(err)
}
if entitiesPtr1[0] == entitiesPtr2[0] {
t.Fatal("expected different pointers")
}
_, err = exportedFuncs[string(deallocate)].Call(context.Background(), entitiesPtr1[0], entitiesSize)
if err != nil {
t.Fatal(err)
}
_, err = exportedFuncs[string(deallocate)].Call(context.Background(), entitiesPtr2[0], entitiesSize)
if err != nil {
t.Fatal(err)
}
}