github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/engine/wazevo/wazevoapi/offsetdata_test.go (about) 1 package wazevoapi 2 3 import ( 4 "testing" 5 6 "github.com/tetratelabs/wazero/internal/testing/require" 7 "github.com/tetratelabs/wazero/internal/wasm" 8 ) 9 10 func TestNewModuleContextOffsetData(t *testing.T) { 11 for _, tc := range []struct { 12 name string 13 m *wasm.Module 14 withListener bool 15 exp ModuleContextOffsetData 16 }{ 17 { 18 name: "empty", 19 m: &wasm.Module{}, 20 exp: ModuleContextOffsetData{ 21 LocalMemoryBegin: -1, 22 ImportedMemoryBegin: -1, 23 ImportedFunctionsBegin: -1, 24 GlobalsBegin: -1, 25 TypeIDs1stElement: -1, 26 TablesBegin: -1, 27 BeforeListenerTrampolines1stElement: -1, 28 AfterListenerTrampolines1stElement: -1, 29 DataInstances1stElement: 8, 30 ElementInstances1stElement: 16, 31 TotalSize: 32, // 16 byte alignment. 32 }, 33 }, 34 { 35 name: "local mem", 36 m: &wasm.Module{MemorySection: &wasm.Memory{}}, 37 exp: ModuleContextOffsetData{ 38 LocalMemoryBegin: 8, 39 ImportedMemoryBegin: -1, 40 ImportedFunctionsBegin: -1, 41 GlobalsBegin: -1, 42 TypeIDs1stElement: -1, 43 TablesBegin: -1, 44 BeforeListenerTrampolines1stElement: -1, 45 AfterListenerTrampolines1stElement: -1, 46 DataInstances1stElement: 24, 47 ElementInstances1stElement: 32, 48 TotalSize: 48, // 16 byte alignment. 49 }, 50 }, 51 { 52 name: "imported mem", 53 m: &wasm.Module{ImportMemoryCount: 1}, 54 exp: ModuleContextOffsetData{ 55 LocalMemoryBegin: -1, 56 ImportedMemoryBegin: 8, 57 ImportedFunctionsBegin: -1, 58 GlobalsBegin: -1, 59 TypeIDs1stElement: -1, 60 TablesBegin: -1, 61 BeforeListenerTrampolines1stElement: -1, 62 AfterListenerTrampolines1stElement: -1, 63 DataInstances1stElement: 24, 64 ElementInstances1stElement: 32, 65 TotalSize: 48, // 16 byte alignment. 66 }, 67 }, 68 { 69 name: "imported func", 70 m: &wasm.Module{ImportFunctionCount: 10}, 71 exp: ModuleContextOffsetData{ 72 LocalMemoryBegin: -1, 73 ImportedMemoryBegin: -1, 74 ImportedFunctionsBegin: 8, 75 GlobalsBegin: -1, 76 TypeIDs1stElement: -1, 77 TablesBegin: -1, 78 BeforeListenerTrampolines1stElement: -1, 79 AfterListenerTrampolines1stElement: -1, 80 DataInstances1stElement: 10*FunctionInstanceSize + 8, 81 ElementInstances1stElement: 10*FunctionInstanceSize + 16, 82 TotalSize: int(align16(Offset(10*FunctionInstanceSize + 24))), 83 }, 84 }, 85 { 86 name: "imported func/mem", 87 m: &wasm.Module{ImportMemoryCount: 1, ImportFunctionCount: 10}, 88 exp: ModuleContextOffsetData{ 89 LocalMemoryBegin: -1, 90 ImportedMemoryBegin: 8, 91 ImportedFunctionsBegin: 24, 92 GlobalsBegin: -1, 93 TypeIDs1stElement: -1, 94 TablesBegin: -1, 95 BeforeListenerTrampolines1stElement: -1, 96 AfterListenerTrampolines1stElement: -1, 97 DataInstances1stElement: 10*FunctionInstanceSize + 24, 98 ElementInstances1stElement: 10*FunctionInstanceSize + 32, 99 TotalSize: int(align16(Offset(10*FunctionInstanceSize + 40))), 100 }, 101 }, 102 { 103 name: "local mem / imported func / globals / tables", 104 m: &wasm.Module{ 105 ImportGlobalCount: 10, 106 ImportFunctionCount: 10, 107 ImportTableCount: 5, 108 TableSection: make([]wasm.Table, 10), 109 MemorySection: &wasm.Memory{}, 110 GlobalSection: make([]wasm.Global, 20), 111 }, 112 exp: ModuleContextOffsetData{ 113 LocalMemoryBegin: 8, 114 ImportedMemoryBegin: -1, 115 ImportedFunctionsBegin: 24, 116 // Align to 16 bytes for globals. 117 GlobalsBegin: 32 + 10*FunctionInstanceSize, 118 TypeIDs1stElement: 32 + 10*FunctionInstanceSize + 16*30, 119 TablesBegin: 32 + 10*FunctionInstanceSize + 16*30 + 8, 120 BeforeListenerTrampolines1stElement: -1, 121 AfterListenerTrampolines1stElement: -1, 122 DataInstances1stElement: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15, 123 ElementInstances1stElement: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15 + 8, 124 TotalSize: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15 + 16, 125 }, 126 }, 127 { 128 name: "local mem / imported func / globals / tables / listener", 129 m: &wasm.Module{ 130 ImportGlobalCount: 10, 131 ImportFunctionCount: 10, 132 ImportTableCount: 5, 133 TableSection: make([]wasm.Table, 10), 134 MemorySection: &wasm.Memory{}, 135 GlobalSection: make([]wasm.Global, 20), 136 }, 137 withListener: true, 138 exp: ModuleContextOffsetData{ 139 LocalMemoryBegin: 8, 140 ImportedMemoryBegin: -1, 141 ImportedFunctionsBegin: 24, 142 // Align to 16 bytes for globals. 143 GlobalsBegin: 32 + 10*FunctionInstanceSize, 144 TypeIDs1stElement: 32 + 10*FunctionInstanceSize + 16*30, 145 TablesBegin: 32 + 10*FunctionInstanceSize + 16*30 + 8, 146 BeforeListenerTrampolines1stElement: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15, 147 AfterListenerTrampolines1stElement: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15 + 8, 148 DataInstances1stElement: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15 + 16, 149 ElementInstances1stElement: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15 + 24, 150 TotalSize: 32 + 10*FunctionInstanceSize + 16*30 + 8 + 8*15 + 32, 151 }, 152 }, 153 } { 154 t.Run(tc.name, func(t *testing.T) { 155 got := NewModuleContextOffsetData(tc.m, tc.withListener) 156 require.Equal(t, tc.exp, got) 157 }) 158 } 159 }