github.com/tetratelabs/wazero@v1.2.1/internal/wasm/table.go (about) 1 package wasm 2 3 import ( 4 "fmt" 5 "math" 6 "sync" 7 8 "github.com/tetratelabs/wazero/api" 9 "github.com/tetratelabs/wazero/internal/leb128" 10 ) 11 12 // Table describes the limits of elements and its type in a table. 13 type Table struct { 14 Min uint32 15 Max *uint32 16 Type RefType 17 } 18 19 // RefType is either RefTypeFuncref or RefTypeExternref as of WebAssembly core 2.0. 20 type RefType = byte 21 22 const ( 23 // RefTypeFuncref represents a reference to a function. 24 RefTypeFuncref = ValueTypeFuncref 25 // RefTypeExternref represents a reference to a host object, which is not currently supported in wazero. 26 RefTypeExternref = ValueTypeExternref 27 ) 28 29 func RefTypeName(t RefType) (ret string) { 30 switch t { 31 case RefTypeFuncref: 32 ret = "funcref" 33 case RefTypeExternref: 34 ret = "externref" 35 default: 36 ret = fmt.Sprintf("unknown(0x%x)", t) 37 } 38 return 39 } 40 41 // ElementMode represents a mode of element segment which is either active, passive or declarative. 42 // 43 // https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/syntax/modules.html#element-segments 44 type ElementMode = byte 45 46 const ( 47 // ElementModeActive is the mode which requires the runtime to initialize table with the contents in .Init field combined with OffsetExpr. 48 ElementModeActive ElementMode = iota 49 // ElementModePassive is the mode which doesn't require the runtime to initialize table, and only used with OpcodeTableInitName. 50 ElementModePassive 51 // ElementModeDeclarative is introduced in reference-types proposal which can be used to declare function indexes used by OpcodeRefFunc. 52 ElementModeDeclarative 53 ) 54 55 // ElementSegment are initialization instructions for a TableInstance 56 // 57 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#syntax-elem 58 type ElementSegment struct { 59 // OffsetExpr returns the table element offset to apply to Init indices. 60 // Note: This can be validated prior to instantiation unless it includes OpcodeGlobalGet (an imported global). 61 OffsetExpr ConstantExpression 62 63 // TableIndex is the table's index to which this element segment is applied. 64 // Note: This is used if and only if the Mode is active. 65 TableIndex Index 66 67 // Followings are set/used regardless of the Mode. 68 69 // Init indices are (nullable) table elements where each index is the function index by which the module initialize the table. 70 Init []Index 71 72 // Type holds the type of this element segment, which is the RefType in WebAssembly 2.0. 73 Type RefType 74 75 // Mode is the mode of this element segment. 76 Mode ElementMode 77 } 78 79 const ( 80 // ElementInitNullReference represents the null reference in ElementSegment's Init. 81 // In Wasm spec, an init item represents either Function's Index or null reference, 82 // and in wazero, we limit the maximum number of functions available in a module to 83 // MaximumFunctionIndex. Therefore, it is safe to use 1 << 31 to represent the null 84 // reference in Element segments. 85 ElementInitNullReference Index = 1 << 31 86 // ElementInitImportedGlobalFunctionReference represents an init item which is resolved via an imported global constexpr. 87 // The actual function reference stored at Global is only known at instantiation-time, so we set this flag 88 // to items of ElementSegment.Init at binary decoding, and unwrap this flag at instantiation to resolve the value. 89 // 90 // This might collide the init element resolved via ref.func instruction which is resolved with the func index at decoding, 91 // but in practice, that is not allowed in wazero thanks to our limit MaximumFunctionIndex. Thus, it is safe to set this flag 92 // in init element to indicate as such. 93 ElementInitImportedGlobalFunctionReference Index = 1 << 30 94 ) 95 96 // unwrapElementInitGlobalReference takes an item of the init vector of an ElementSegment, 97 // and returns the Global index if it is supposed to get generated from a global. 98 // ok is true if the given init item is as such. 99 func unwrapElementInitGlobalReference(init Index) (_ Index, ok bool) { 100 if init&ElementInitImportedGlobalFunctionReference == ElementInitImportedGlobalFunctionReference { 101 return init &^ ElementInitImportedGlobalFunctionReference, true 102 } 103 return init, false 104 } 105 106 // IsActive returns true if the element segment is "active" mode which requires the runtime to initialize table 107 // with the contents in .Init field. 108 func (e *ElementSegment) IsActive() bool { 109 return e.Mode == ElementModeActive 110 } 111 112 // TableInstance represents a table of (RefTypeFuncref) elements in a module. 113 // 114 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#table-instances%E2%91%A0 115 type TableInstance struct { 116 // References holds references whose type is either RefTypeFuncref or RefTypeExternref (unsupported). 117 // 118 // Currently, only function references are supported. 119 References []Reference 120 121 // Min is the minimum (function) elements in this table and cannot grow to accommodate ElementSegment. 122 Min uint32 123 124 // Max if present is the maximum (function) elements in this table, or nil if unbounded. 125 Max *uint32 126 127 // Type is either RefTypeFuncref or RefTypeExternRef. 128 Type RefType 129 130 // mux is used to prevent overlapping calls to Grow. 131 mux sync.RWMutex 132 } 133 134 // ElementInstance represents an element instance in a module. 135 // 136 // See https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/runtime.html#element-instances 137 type ElementInstance struct { 138 // References holds references whose type is either RefTypeFuncref or RefTypeExternref (unsupported). 139 References []Reference 140 // Type is the RefType of the references in this instance's References. 141 Type RefType 142 } 143 144 // Reference is the runtime representation of RefType which is either RefTypeFuncref or RefTypeExternref. 145 type Reference = uintptr 146 147 // validateTable ensures any ElementSegment is valid. This caches results via Module.validatedActiveElementSegments. 148 // Note: limitsType are validated by decoders, so not re-validated here. 149 func (m *Module) validateTable(enabledFeatures api.CoreFeatures, tables []Table, maximumTableIndex uint32) error { 150 if len(tables) > int(maximumTableIndex) { 151 return fmt.Errorf("too many tables in a module: %d given with limit %d", len(tables), maximumTableIndex) 152 } 153 154 importedTableCount := m.ImportTableCount 155 156 // Create bounds checks as these can err prior to instantiation 157 funcCount := m.ImportFunctionCount + m.SectionElementCount(SectionIDFunction) 158 globalsCount := m.ImportGlobalCount + m.SectionElementCount(SectionIDGlobal) 159 160 // Now, we have to figure out which table elements can be resolved before instantiation and also fail early if there 161 // are any imported globals that are known to be invalid by their declarations. 162 for i := range m.ElementSection { 163 elem := &m.ElementSection[i] 164 idx := Index(i) 165 initCount := uint32(len(elem.Init)) 166 167 if elem.Type == RefTypeFuncref { 168 // Any offset applied is to the element, not the function index: validate here if the funcidx is sound. 169 for ei, init := range elem.Init { 170 if init == ElementInitNullReference { 171 continue 172 } 173 index, ok := unwrapElementInitGlobalReference(init) 174 if ok { 175 if index >= globalsCount { 176 return fmt.Errorf("%s[%d].init[%d] globalidx %d out of range", SectionIDName(SectionIDElement), idx, ei, index) 177 } 178 } else { 179 if index >= funcCount { 180 return fmt.Errorf("%s[%d].init[%d] funcidx %d out of range", SectionIDName(SectionIDElement), idx, ei, index) 181 } 182 } 183 } 184 } else { 185 for j, elem := range elem.Init { 186 if elem != ElementInitNullReference { 187 return fmt.Errorf("%s[%d].init[%d] must be ref.null but was %v", SectionIDName(SectionIDElement), idx, j, elem) 188 } 189 } 190 } 191 192 if elem.IsActive() { 193 if len(tables) <= int(elem.TableIndex) { 194 return fmt.Errorf("unknown table %d as active element target", elem.TableIndex) 195 } 196 197 t := tables[elem.TableIndex] 198 if t.Type != elem.Type { 199 return fmt.Errorf("element type mismatch: table has %s but element has %s", 200 RefTypeName(t.Type), RefTypeName(elem.Type), 201 ) 202 } 203 204 // global.get needs to be discovered during initialization 205 oc := elem.OffsetExpr.Opcode 206 if oc == OpcodeGlobalGet { 207 globalIdx, _, err := leb128.LoadUint32(elem.OffsetExpr.Data) 208 if err != nil { 209 return fmt.Errorf("%s[%d] couldn't read global.get parameter: %w", SectionIDName(SectionIDElement), idx, err) 210 } else if err = m.verifyImportGlobalI32(SectionIDElement, idx, globalIdx); err != nil { 211 return err 212 } 213 } else if oc == OpcodeI32Const { 214 // Per https://github.com/WebAssembly/spec/blob/wg-1.0/test/core/elem.wast#L117 we must pass if imported 215 // table has set its min=0. Per https://github.com/WebAssembly/spec/blob/wg-1.0/test/core/elem.wast#L142, we 216 // have to do fail if module-defined min=0. 217 if !enabledFeatures.IsEnabled(api.CoreFeatureReferenceTypes) && elem.TableIndex >= importedTableCount { 218 // Treat constants as signed as their interpretation is not yet known per /RATIONALE.md 219 o, _, err := leb128.LoadInt32(elem.OffsetExpr.Data) 220 if err != nil { 221 return fmt.Errorf("%s[%d] couldn't read i32.const parameter: %w", SectionIDName(SectionIDElement), idx, err) 222 } 223 offset := Index(o) 224 if err = checkSegmentBounds(t.Min, uint64(initCount)+uint64(offset), idx); err != nil { 225 return err 226 } 227 } 228 } else { 229 return fmt.Errorf("%s[%d] has an invalid const expression: %s", SectionIDName(SectionIDElement), idx, InstructionName(oc)) 230 } 231 } 232 } 233 return nil 234 } 235 236 // buildTable returns TableInstances if the module defines or imports a table. 237 // - importedTables: returned as `tables` unmodified. 238 // - importedGlobals: include all instantiated, imported globals. 239 // 240 // If the result `init` is non-nil, it is the `tableInit` parameter of Engine.NewModuleEngine. 241 // 242 // Note: An error is only possible when an ElementSegment.OffsetExpr is out of range of the TableInstance.Min. 243 func (m *ModuleInstance) buildTables(module *Module, skipBoundCheck bool) (err error) { 244 idx := module.ImportTableCount 245 for i := range module.TableSection { 246 tsec := &module.TableSection[i] 247 // The module defining the table is the one that sets its Min/Max etc. 248 m.Tables[idx] = &TableInstance{ 249 References: make([]Reference, tsec.Min), Min: tsec.Min, Max: tsec.Max, 250 Type: tsec.Type, 251 } 252 idx++ 253 } 254 255 if !skipBoundCheck { 256 for elemI := range module.ElementSection { // Do not loop over the value since elementSegments is a slice of value. 257 elem := &module.ElementSection[elemI] 258 table := m.Tables[elem.TableIndex] 259 var offset uint32 260 if elem.OffsetExpr.Opcode == OpcodeGlobalGet { 261 // Ignore error as it's already validated. 262 globalIdx, _, _ := leb128.LoadUint32(elem.OffsetExpr.Data) 263 global := m.Globals[globalIdx] 264 offset = uint32(global.Val) 265 } else { // i32.const 266 // Ignore error as it's already validated. 267 o, _, _ := leb128.LoadInt32(elem.OffsetExpr.Data) 268 offset = uint32(o) 269 } 270 271 // Check to see if we are out-of-bounds 272 initCount := uint64(len(elem.Init)) 273 if err = checkSegmentBounds(table.Min, uint64(offset)+initCount, Index(elemI)); err != nil { 274 return 275 } 276 } 277 } 278 return 279 } 280 281 // checkSegmentBounds fails if the capacity needed for an ElementSegment.Init is larger than limitsType.Min 282 // 283 // WebAssembly 1.0 (20191205) doesn't forbid growing to accommodate element segments, and spectests are inconsistent. 284 // For example, the spectests enforce elements within Table limitsType.Min, but ignore Import.DescTable min. What this 285 // means is we have to delay offset checks on imported tables until we link to them. 286 // e.g. https://github.com/WebAssembly/spec/blob/wg-1.0/test/core/elem.wast#L117 wants pass on min=0 for import 287 // e.g. https://github.com/WebAssembly/spec/blob/wg-1.0/test/core/elem.wast#L142 wants fail on min=0 module-defined 288 func checkSegmentBounds(min uint32, requireMin uint64, idx Index) error { // uint64 in case offset was set to -1 289 if requireMin > uint64(min) { 290 return fmt.Errorf("%s[%d].init exceeds min table size", SectionIDName(SectionIDElement), idx) 291 } 292 return nil 293 } 294 295 func (m *Module) verifyImportGlobalI32(sectionID SectionID, sectionIdx Index, idx uint32) error { 296 ig := uint32(math.MaxUint32) // +1 == 0 297 for i := range m.ImportSection { 298 imp := &m.ImportSection[i] 299 if imp.Type == ExternTypeGlobal { 300 ig++ 301 if ig == idx { 302 if imp.DescGlobal.ValType != ValueTypeI32 { 303 return fmt.Errorf("%s[%d] (global.get %d): import[%d].global.ValType != i32", SectionIDName(sectionID), sectionIdx, idx, i) 304 } 305 return nil 306 } 307 } 308 } 309 return fmt.Errorf("%s[%d] (global.get %d): out of range of imported globals", SectionIDName(sectionID), sectionIdx, idx) 310 } 311 312 // Grow appends the `initialRef` by `delta` times into the References slice. 313 // Returns -1 if the operation is not valid, otherwise the old length of the table. 314 // 315 // https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/instructions.html#xref-syntax-instructions-syntax-instr-table-mathsf-table-grow-x 316 func (t *TableInstance) Grow(delta uint32, initialRef Reference) (currentLen uint32) { 317 // We take write-lock here as the following might result in a new slice 318 t.mux.Lock() 319 defer t.mux.Unlock() 320 321 currentLen = uint32(len(t.References)) 322 if delta == 0 { 323 return 324 } 325 326 if newLen := int64(currentLen) + int64(delta); // adding as 64bit ints to avoid overflow. 327 newLen >= math.MaxUint32 || (t.Max != nil && newLen > int64(*t.Max)) { 328 return 0xffffffff // = -1 in signed 32-bit integer. 329 } 330 t.References = append(t.References, make([]uintptr, delta)...) 331 332 // Uses the copy trick for faster filling the new region with the initial value. 333 // https://gist.github.com/taylorza/df2f89d5f9ab3ffd06865062a4cf015d 334 newRegion := t.References[currentLen:] 335 newRegion[0] = initialRef 336 for i := 1; i < len(newRegion); i *= 2 { 337 copy(newRegion[i:], newRegion[:i]) 338 } 339 return 340 }