github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/engine/wazevo/wazevoapi/offsetdata_test.go (about) 1 package wazevoapi 2 3 import ( 4 "testing" 5 6 "github.com/wasilibs/wazerox/internal/testing/require" 7 "github.com/wasilibs/wazerox/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: 24, 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: 40, 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: 40, 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: 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: 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 GlobalsBegin: 24 + 10*FunctionInstanceSize, 117 TypeIDs1stElement: 24 + 10*FunctionInstanceSize + 8*30, 118 TablesBegin: 24 + 10*FunctionInstanceSize + 8*30 + 8, 119 BeforeListenerTrampolines1stElement: -1, 120 AfterListenerTrampolines1stElement: -1, 121 DataInstances1stElement: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15, 122 ElementInstances1stElement: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15 + 8, 123 TotalSize: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15 + 16, 124 }, 125 }, 126 { 127 name: "local mem / imported func / globals / tables / listener", 128 m: &wasm.Module{ 129 ImportGlobalCount: 10, 130 ImportFunctionCount: 10, 131 ImportTableCount: 5, 132 TableSection: make([]wasm.Table, 10), 133 MemorySection: &wasm.Memory{}, 134 GlobalSection: make([]wasm.Global, 20), 135 }, 136 withListener: true, 137 exp: ModuleContextOffsetData{ 138 LocalMemoryBegin: 8, 139 ImportedMemoryBegin: -1, 140 ImportedFunctionsBegin: 24, 141 GlobalsBegin: 24 + 10*FunctionInstanceSize, 142 TypeIDs1stElement: 24 + 10*FunctionInstanceSize + 8*30, 143 TablesBegin: 24 + 10*FunctionInstanceSize + 8*30 + 8, 144 BeforeListenerTrampolines1stElement: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15, 145 AfterListenerTrampolines1stElement: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15 + 8, 146 DataInstances1stElement: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15 + 16, 147 ElementInstances1stElement: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15 + 24, 148 TotalSize: 24 + 10*FunctionInstanceSize + 8*30 + 8 + 8*15 + 32, 149 }, 150 }, 151 } { 152 t.Run(tc.name, func(t *testing.T) { 153 got := NewModuleContextOffsetData(tc.m, tc.withListener) 154 require.Equal(t, tc.exp, got) 155 }) 156 } 157 }