| 1 |
package wazevoapi |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/tetratelabs/wazero/internal/wasm" |
| 5 |
) |
| 6 |
|
| 7 |
const ( |
| 8 |
// FunctionInstanceSize is the size of wazevo.functionInstance. |
| 9 |
FunctionInstanceSize = 24 |
| 10 |
// FunctionInstanceExecutableOffset is an offset of `executable` field in wazevo.functionInstance |
| 11 |
FunctionInstanceExecutableOffset = 0 |
| 12 |
// FunctionInstanceModuleContextOpaquePtrOffset is an offset of `moduleContextOpaquePtr` field in wazevo.functionInstance |
| 13 |
FunctionInstanceModuleContextOpaquePtrOffset = 8 |
| 14 |
// FunctionInstanceTypeIDOffset is an offset of `typeID` field in wazevo.functionInstance |
| 15 |
FunctionInstanceTypeIDOffset = 16 |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
// ExecutionContextOffsetExitCodeOffset is an offset of `exitCode` field in wazevo.executionContext |
| 20 |
ExecutionContextOffsetExitCodeOffset Offset = 0 |
| 21 |
// ExecutionContextOffsetCallerModuleContextPtr is an offset of `callerModuleContextPtr` field in wazevo.executionContext |
| 22 |
ExecutionContextOffsetCallerModuleContextPtr Offset = 8 |
| 23 |
// ExecutionContextOffsetOriginalFramePointer is an offset of `originalFramePointer` field in wazevo.executionContext |
| 24 |
ExecutionContextOffsetOriginalFramePointer Offset = 16 |
| 25 |
// ExecutionContextOffsetOriginalStackPointer is an offset of `originalStackPointer` field in wazevo.executionContext |
| 26 |
ExecutionContextOffsetOriginalStackPointer Offset = 24 |
| 27 |
// ExecutionContextOffsetGoReturnAddress is an offset of `goReturnAddress` field in wazevo.executionContext |
| 28 |
ExecutionContextOffsetGoReturnAddress Offset = 32 |
| 29 |
// ExecutionContextOffsetStackBottomPtr is an offset of `stackBottomPtr` field in wazevo.executionContext |
| 30 |
ExecutionContextOffsetStackBottomPtr Offset = 40 |
| 31 |
// ExecutionContextOffsetGoCallReturnAddress is an offset of `goCallReturnAddress` field in wazevo.executionContext |
| 32 |
ExecutionContextOffsetGoCallReturnAddress Offset = 48 |
| 33 |
// ExecutionContextOffsetStackPointerBeforeGoCall is an offset of `StackPointerBeforeGoCall` field in wazevo.executionContext |
| 34 |
ExecutionContextOffsetStackPointerBeforeGoCall Offset = 56 |
| 35 |
// ExecutionContextOffsetStackGrowRequiredSize is an offset of `stackGrowRequiredSize` field in wazevo.executionContext |
| 36 |
ExecutionContextOffsetStackGrowRequiredSize Offset = 64 |
| 37 |
// ExecutionContextOffsetMemoryGrowTrampolineAddress is an offset of `memoryGrowTrampolineAddress` field in wazevo.executionContext |
| 38 |
ExecutionContextOffsetMemoryGrowTrampolineAddress Offset = 72 |
| 39 |
// ExecutionContextOffsetStackGrowCallTrampolineAddress is an offset of `stackGrowCallTrampolineAddress` field in wazevo.executionContext. |
| 40 |
ExecutionContextOffsetStackGrowCallTrampolineAddress Offset = 80 |
| 41 |
// ExecutionContextOffsetCheckModuleExitCodeTrampolineAddress is an offset of `checkModuleExitCodeTrampolineAddress` field in wazevo.executionContext. |
| 42 |
ExecutionContextOffsetCheckModuleExitCodeTrampolineAddress Offset = 88 |
| 43 |
// ExecutionContextOffsetSavedRegistersBegin is an offset of the first element of `savedRegisters` field in wazevo.executionContext |
| 44 |
ExecutionContextOffsetSavedRegistersBegin Offset = 96 |
| 45 |
// ExecutionContextOffsetGoFunctionCallCalleeModuleContextOpaque is an offset of `goFunctionCallCalleeModuleContextOpaque` field in wazevo.executionContext |
| 46 |
ExecutionContextOffsetGoFunctionCallCalleeModuleContextOpaque Offset = 1120 |
| 47 |
// ExecutionContextOffsetTableGrowTrampolineAddress is an offset of `tableGrowTrampolineAddress` field in wazevo.executionContext |
| 48 |
ExecutionContextOffsetTableGrowTrampolineAddress Offset = 1128 |
| 49 |
// ExecutionContextOffsetRefFuncTrampolineAddress is an offset of `refFuncTrampolineAddress` field in wazevo.executionContext |
| 50 |
ExecutionContextOffsetRefFuncTrampolineAddress Offset = 1136 |
| 51 |
ExecutionContextOffsetMemmoveAddress Offset = 1144 |
| 52 |
ExecutionContextOffsetFramePointerBeforeGoCall Offset = 1152 |
| 53 |
ExecutionContextOffsetMemoryWait32TrampolineAddress Offset = 1160 |
| 54 |
ExecutionContextOffsetMemoryWait64TrampolineAddress Offset = 1168 |
| 55 |
ExecutionContextOffsetMemoryNotifyTrampolineAddress Offset = 1176 |
| 56 |
// ExecutionContextOffsetThrowAllocTrampolineAddress is the address of the |
| 57 |
// throw-alloc trampoline, which allocates the Exception heap object, |
| 58 |
// sets exceptionParamsPtr, and returns the exnref. |
| 59 |
ExecutionContextOffsetThrowAllocTrampolineAddress Offset = 1184 |
| 60 |
ExecutionContextOffsetThrowTrampolineAddress Offset = 1192 |
| 61 |
ExecutionContextOffsetTryTableEnterTrampolineAddress Offset = 1200 |
| 62 |
ExecutionContextOffsetTryTableLeaveTrampolineAddress Offset = 1208 |
| 63 |
// ExecutionContextOffsetExceptionPtr holds the pointer to the Exception struct, |
| 64 |
// used on the throw side and by catch_ref/catch_all_ref handlers. |
| 65 |
ExecutionContextOffsetExceptionPtr Offset = 1216 |
| 66 |
// ExecutionContextOffsetExceptionParamsPtr points into the Exception's |
| 67 |
// Params slice backing array. Used by both throw (store params) and |
| 68 |
// catch (load params) sides. |
| 69 |
ExecutionContextOffsetExceptionParamsPtr Offset = 1224 |
| 70 |
// ExecutionContextOffsetCaughtExceptionClauseIdx is the matched catch clause index |
| 71 |
// written by handleException and read by compiled handler dispatch code. |
| 72 |
ExecutionContextOffsetCaughtExceptionClauseIdx Offset = 1232 |
| 73 |
) |
| 74 |
|
| 75 |
// ModuleContextOffsetData allows the compilers to get the information about offsets to the fields of wazevo.moduleContextOpaque, |
| 76 |
// This is unique per module. |
| 77 |
type ModuleContextOffsetData struct { |
| 78 |
TotalSize int |
| 79 |
ModuleInstanceOffset, |
| 80 |
LocalMemoryBegin, |
| 81 |
ImportedMemoryBegin, |
| 82 |
ImportedFunctionsBegin, |
| 83 |
GlobalsBegin, |
| 84 |
TypeIDs1stElement, |
| 85 |
TablesBegin, |
| 86 |
TagsBegin, |
| 87 |
BeforeListenerTrampolines1stElement, |
| 88 |
AfterListenerTrampolines1stElement, |
| 89 |
DataInstances1stElement, |
| 90 |
ElementInstances1stElement Offset |
| 91 |
} |
| 92 |
|
| 93 |
// ImportedFunctionOffset returns an offset of the i-th imported function. |
| 94 |
// Each item is stored as wazevo.functionInstance whose size matches FunctionInstanceSize. |
| 95 |
func (m *ModuleContextOffsetData) ImportedFunctionOffset(i wasm.Index) ( |
| 96 |
executableOffset, moduleCtxOffset, typeIDOffset Offset, |
| 97 |
) { |
| 98 |
base := m.ImportedFunctionsBegin + Offset(i)*FunctionInstanceSize |
| 99 |
return base, base + 8, base + 16 |
| 100 |
} |
| 101 |
|
| 102 |
// GlobalInstanceOffset returns an offset of the i-th global instance. |
| 103 |
func (m *ModuleContextOffsetData) GlobalInstanceOffset(i wasm.Index) Offset { |
| 104 |
return m.GlobalsBegin + Offset(i)*16 |
| 105 |
} |
| 106 |
|
| 107 |
// Offset represents an offset of a field of a struct. |
| 108 |
type Offset int32 |
| 109 |
|
| 110 |
// U32 encodes an Offset as uint32 for convenience. |
| 111 |
func (o Offset) U32() uint32 { |
| 112 |
return uint32(o) |
| 113 |
} |
| 114 |
|
| 115 |
// I64 encodes an Offset as int64 for convenience. |
| 116 |
func (o Offset) I64() int64 { |
| 117 |
return int64(o) |
| 118 |
} |
| 119 |
|
| 120 |
// U64 encodes an Offset as int64 for convenience. |
| 121 |
func (o Offset) U64() uint64 { |
| 122 |
return uint64(o) |
| 123 |
} |
| 124 |
|
| 125 |
// LocalMemoryBase returns an offset of the first byte of the local memory. |
| 126 |
func (m *ModuleContextOffsetData) LocalMemoryBase() Offset { |
| 127 |
return m.LocalMemoryBegin |
| 128 |
} |
| 129 |
|
| 130 |
// LocalMemoryLen returns an offset of the length of the local memory buffer. |
| 131 |
func (m *ModuleContextOffsetData) LocalMemoryLen() Offset { |
| 132 |
if l := m.LocalMemoryBegin; l >= 0 { |
| 133 |
return l + 8 |
| 134 |
} |
| 135 |
return -1 |
| 136 |
} |
| 137 |
|
| 138 |
// TableOffset returns an offset of the i-th table instance. |
| 139 |
func (m *ModuleContextOffsetData) TableOffset(tableIndex int) Offset { |
| 140 |
return m.TablesBegin + Offset(tableIndex)*8 |
| 141 |
} |
| 142 |
|
| 143 |
// TagOffset returns an offset of the i-th tag instance pointer. |
| 144 |
func (m *ModuleContextOffsetData) TagOffset(tagIndex int) Offset { |
| 145 |
return m.TagsBegin + Offset(tagIndex)*8 |
| 146 |
} |
| 147 |
|
| 148 |
// NewModuleContextOffsetData creates a ModuleContextOffsetData determining the structure of moduleContextOpaque for the given Module. |
| 149 |
// The structure is described in the comment of wazevo.moduleContextOpaque. |
| 150 |
func NewModuleContextOffsetData(m *wasm.Module, withListener bool) ModuleContextOffsetData { |
| 151 |
ret := ModuleContextOffsetData{} |
| 152 |
var offset Offset |
| 153 |
|
| 154 |
ret.ModuleInstanceOffset = 0 |
| 155 |
offset += 8 |
| 156 |
|
| 157 |
if m.MemorySection != nil { |
| 158 |
ret.LocalMemoryBegin = offset |
| 159 |
// buffer base + memory size. |
| 160 |
const localMemorySizeInOpaqueModuleContext = 16 |
| 161 |
offset += localMemorySizeInOpaqueModuleContext |
| 162 |
} else { |
| 163 |
// Indicates that there's no local memory |
| 164 |
ret.LocalMemoryBegin = -1 |
| 165 |
} |
| 166 |
|
| 167 |
if m.ImportMemoryCount > 0 { |
| 168 |
offset = align8(offset) |
| 169 |
// *wasm.MemoryInstance + imported memory's owner (moduleContextOpaque) |
| 170 |
const importedMemorySizeInOpaqueModuleContext = 16 |
| 171 |
ret.ImportedMemoryBegin = offset |
| 172 |
offset += importedMemorySizeInOpaqueModuleContext |
| 173 |
} else { |
| 174 |
// Indicates that there's no imported memory |
| 175 |
ret.ImportedMemoryBegin = -1 |
| 176 |
} |
| 177 |
|
| 178 |
if m.ImportFunctionCount > 0 { |
| 179 |
offset = align8(offset) |
| 180 |
ret.ImportedFunctionsBegin = offset |
| 181 |
// Each function is stored wazevo.functionInstance. |
| 182 |
size := int(m.ImportFunctionCount) * FunctionInstanceSize |
| 183 |
offset += Offset(size) |
| 184 |
} else { |
| 185 |
ret.ImportedFunctionsBegin = -1 |
| 186 |
} |
| 187 |
|
| 188 |
if globals := int(m.ImportGlobalCount) + len(m.GlobalSection); globals > 0 { |
| 189 |
// Align to 16 bytes for globals, as f32/f64/v128 might be loaded via SIMD instructions. |
| 190 |
offset = align16(offset) |
| 191 |
ret.GlobalsBegin = offset |
| 192 |
// Pointers to *wasm.GlobalInstance. |
| 193 |
offset += Offset(globals) * 16 |
| 194 |
} else { |
| 195 |
ret.GlobalsBegin = -1 |
| 196 |
} |
| 197 |
|
| 198 |
if tables := len(m.TableSection) + int(m.ImportTableCount); tables > 0 { |
| 199 |
offset = align8(offset) |
| 200 |
ret.TypeIDs1stElement = offset |
| 201 |
offset += 8 // First element of TypeIDs. |
| 202 |
|
| 203 |
ret.TablesBegin = offset |
| 204 |
// Pointers to *wasm.TableInstance. |
| 205 |
offset += Offset(tables) * 8 |
| 206 |
} else { |
| 207 |
ret.TypeIDs1stElement = -1 |
| 208 |
ret.TablesBegin = -1 |
| 209 |
} |
| 210 |
|
| 211 |
if tags := int(m.ImportTagCount) + len(m.TagSection); tags > 0 { |
| 212 |
offset = align8(offset) |
| 213 |
ret.TagsBegin = offset |
| 214 |
// Pointers to *wasm.TagInstance. |
| 215 |
offset += Offset(tags) * 8 |
| 216 |
} else { |
| 217 |
ret.TagsBegin = -1 |
| 218 |
} |
| 219 |
|
| 220 |
if withListener { |
| 221 |
offset = align8(offset) |
| 222 |
ret.BeforeListenerTrampolines1stElement = offset |
| 223 |
offset += 8 // First element of BeforeListenerTrampolines. |
| 224 |
|
| 225 |
ret.AfterListenerTrampolines1stElement = offset |
| 226 |
offset += 8 // First element of AfterListenerTrampolines. |
| 227 |
} else { |
| 228 |
ret.BeforeListenerTrampolines1stElement = -1 |
| 229 |
ret.AfterListenerTrampolines1stElement = -1 |
| 230 |
} |
| 231 |
|
| 232 |
ret.DataInstances1stElement = offset |
| 233 |
offset += 8 // First element of DataInstances. |
| 234 |
|
| 235 |
ret.ElementInstances1stElement = offset |
| 236 |
offset += 8 // First element of ElementInstances. |
| 237 |
|
| 238 |
ret.TotalSize = int(align16(offset)) |
| 239 |
return ret |
| 240 |
} |
| 241 |
|
| 242 |
func align16(o Offset) Offset { |
| 243 |
return (o + 15) &^ 15 |
| 244 |
} |
| 245 |
|
| 246 |
func align8(o Offset) Offset { |
| 247 |
return (o + 7) &^ 7 |
| 248 |
} |