4
4
package engine
5
5
6
6
import (
7
- "fmt"
8
- "math"
9
-
10
- "github.com/ava-labs/avalanchego/utils/wrappers"
11
-
12
7
"github.com/bytecodealliance/wasmtime-go/v14"
13
8
)
14
9
@@ -21,281 +16,146 @@ const (
21
16
PrecompiledWasm
22
17
)
23
18
24
- const (
25
- defaultMaxWasmStack = 256 * 1024 * 1024 // 256 MiB
19
+ var (
20
+ DefaultMaxWasmStack = 256 * 1024 * 1024 // 256 MiB
21
+ DefaultLimitMaxMemory = int64 (18 * 64 * 1024 ) // 18 pages
22
+ DefaultSIMD = false
23
+ DefaultEnableReferenceTypes = false
24
+ DefaultEnableBulkMemory = false
25
+ DefaultProfilingStrategy = wasmtime .ProfilingStrategyNone
26
+ DefaultMultiValue = false
27
+ DefaultCompileStrategy = CompileWasm
28
+
26
29
defaultWasmThreads = false
27
30
defaultFuelMetering = true
28
31
defaultWasmMultiMemory = false
29
32
defaultWasmMemory64 = false
30
- defaultLimitMaxMemory = 18 * 64 * 1024 // 18 pages
31
- defaultSIMD = false
32
33
defaultCompilerStrategy = wasmtime .StrategyCranelift
33
34
defaultEpochInterruption = true
34
35
defaultNaNCanonicalization = "true"
35
36
defaultCraneliftOptLevel = wasmtime .OptLevelSpeed
36
- defaultEnableReferenceTypes = false
37
- defaultEnableBulkMemory = false
38
- defaultProfilingStrategy = wasmtime .ProfilingStrategyNone
39
- defaultMultiValue = false
40
37
defaultEnableCraneliftDebugVerifier = false
41
38
defaultEnableDebugInfo = false
42
- defaultCompileStrategy = CompileWasm
43
-
44
- defaultLimitMaxTableElements = 4096
45
- defaultLimitMaxTables = 1
46
- defaultLimitMaxInstances = 32
47
- defaultLimitMaxMemories = 1
48
39
)
49
40
50
- // NewConfigBuilder returns a new runtime configuration builder with default settings.
51
- // All instances of ConfigBuilder should be created with this constructor.
52
- func NewConfigBuilder () * ConfigBuilder {
53
- return & ConfigBuilder {
54
- EnableBulkMemory : defaultEnableBulkMemory ,
55
- EnableWasmMultiValue : defaultMultiValue ,
56
- EnableWasmReferenceTypes : defaultEnableReferenceTypes ,
57
- EnableWasmSIMD : defaultSIMD ,
58
- MaxWasmStack : defaultMaxWasmStack ,
59
- LimitMaxMemory : defaultLimitMaxMemory ,
60
- ProfilingStrategy : defaultProfilingStrategy ,
61
- CompileStrategy : defaultCompileStrategy ,
62
- EnableDefaultCache : false ,
63
- EnableDebugMode : false ,
41
+ // NewConfig creates a new engine config with default settings
42
+ func NewConfig () * Config {
43
+ return & Config {
44
+ inner : DefaultWasmtimeConfig (),
64
45
}
65
46
}
66
47
67
- type ConfigBuilder struct {
68
- // Configures whether the WebAssembly bulk memory operations proposal will
69
- // be enabled for compilation. This feature gates items such as the
70
- // memory.copy instruction, passive data/table segments, etc, being in a
71
- // module.
72
- // This is false by default.
73
- EnableBulkMemory bool `yaml:"enable_bulk_memory,omitempty" json:"enableBulkMemory,omitempty"`
74
- // Configures whether the WebAssembly multi-value proposal will be enabled for compilation.
75
- // This feature gates functions and blocks returning multiple values in a module, for example.
76
- // This is false by default.
77
- EnableWasmMultiValue bool `yaml:"enable_wasm_multi_value,omitempty" json:"enableWasmMultiValue,omitempty"`
78
- // Configures whether the WebAssembly reference types proposal will be
79
- // enabled for compilation. This feature gates items such as the externref
80
- // and funcref types as well as allowing a module to define multiple tables.
81
- // Note that the reference types proposal depends on the bulk memory
82
- // proposal.
83
- // This is false by default.
84
- EnableWasmReferenceTypes bool `yaml:"enable_wasm_reference_types,omitempty" json:"enableWasmReferenceTypes,omitempty"`
85
- // Configures whether the WebAssembly SIMD proposal will be enabled for
86
- // compilation. The WebAssembly SIMD proposal. This feature gates items
87
- // such as the v128 type and all of its operators being in a module. Note
88
- // that this does not enable the relaxed simd proposal.
89
- // This is false by default.
90
- EnableWasmSIMD bool `yaml:"enable_wasm_simd,omitempty" json:"enableWasmSIMD,omitempty"`
91
- // EnableDefaultCache enables compiled code caching for this `Config` using the default settings
92
- // configuration can be found.
93
- //
94
- // For more information about caching see
95
- // https://bytecodealliance.github.io/wasmtime/cli-cache.html
96
- // This is false by default.
97
- EnableDefaultCache bool `yaml:"enable_default_cache,omitempty" json:"enableDefaultCache,omitempty"`
98
- // EnableDebugMode is a test mode which provides access to non production debugging features.
99
- // This should not be set for a live system as it has both performance and security considerations.
100
- // This is false by default.
101
- EnableDebugMode bool `yaml:"enable_testing_only_mode,omitempty" json:"enableTestingOnlyMode,omitempty"`
102
- // SetMaxWasmStack configures the maximum stack size, in bytes, that JIT code can use.
103
- // The amount of stack space that wasm takes is always relative to the first invocation of wasm on the stack.
104
- // Recursive calls with host frames in the middle will all need to fit within this setting.
105
- // Note that this setting is not interpreted with 100% precision.
106
- // This is 256 MiB by default.
107
- MaxWasmStack int `yaml:"max_wasm_stack,omitempty" json:"maxWasmStack,omitempty"`
108
- // LimitMaxMemory defines the maximum number of pages of memory that can be used.
109
- // Each page represents 64KiB of memory.
110
- // This is 18 pages by default.
111
- LimitMaxMemory int64 `yaml:"limit_max_memory,omitempty" json:"limitMaxMemory,omitempty"`
112
- // ProfilingStrategy decides what sort of profiling to enable, if any.
113
- // Default is `wasmtime.ProfilingStrategyNone`.
114
- ProfilingStrategy wasmtime.ProfilingStrategy
115
- // CompileStrategy helps the engine to understand if the files has been precompiled.
116
- CompileStrategy CompileStrategy
117
-
118
- err wrappers.Errs
48
+ // Config is wrapper for wasmtime.Config
49
+ type Config struct {
50
+ inner * wasmtime.Config
119
51
}
120
52
121
- // WithCompileStrategy defines the EngineCompileStrategy.
122
- //
123
- // Default is CompileWasm.
124
- func (c * ConfigBuilder ) WithCompileStrategy (strategy CompileStrategy ) * ConfigBuilder {
125
- c .CompileStrategy = strategy
126
- return c
53
+ // CacheConfigLoad enables compiled code caching for this `Config` using the
54
+ // settings specified in the configuration file `path`.
55
+ func (c * Config ) CacheConfigLoad (path string ) error {
56
+ return c .inner .CacheConfigLoad (path )
127
57
}
128
58
129
- // WithMaxWasmStack defines the maximum amount of stack space available for
130
- // executing WebAssembly code.
131
- //
132
- // Default is 256 MiB.
133
- func (c * ConfigBuilder ) WithMaxWasmStack (max int ) * ConfigBuilder {
134
- c .MaxWasmStack = max
135
- return c
59
+ // CacheConfigLoadDefault enables compiled code caching for this `Config` using
60
+ // the default settings configuration can be found.
61
+ func (c * Config ) CacheConfigLoadDefault () error {
62
+ return c .inner .CacheConfigLoadDefault ()
136
63
}
137
64
138
- // WithMultiValue enables modules that can return multiple values.
139
- // ref. https://github.com/webassembly/multi-value
140
- //
141
- // Default is false.
142
- func (c * ConfigBuilder ) WithMultiValue (enable bool ) * ConfigBuilder {
143
- c .EnableWasmMultiValue = enable
144
- return c
65
+ // EnableCraneliftFlag enables a target-specific flag in Cranelift.
66
+ func (c * Config ) EnableCraneliftFlag (flag string ) {
67
+ c .inner .EnableCraneliftFlag (flag )
145
68
}
146
69
147
- // WithBulkMemory enables`memory.copy` instruction, tables and passive data.
148
- // ref. https://github.com/WebAssembly/bulk-memory-operations
149
- //
150
- // Default is false.
151
- func (c * ConfigBuilder ) WithBulkMemory (enable bool ) * ConfigBuilder {
152
- c .EnableBulkMemory = enable
153
- return c
70
+ // SetConsumFuel configures whether fuel is enabled.
71
+ func (c * Config ) SetConsumeFuel (enabled bool ) {
72
+ c .inner .SetConsumeFuel (enabled )
154
73
}
155
74
156
- // WithReferenceTypes Enables the `externref` and `funcref` types as well as
157
- // allowing a module to define multiple tables.
158
- // ref. https://github.com/webassembly/reference-types
159
- //
160
- // Note: depends on bulk memory being enabled.
161
- //
162
- // Default is false.
163
- func (c * ConfigBuilder ) WithReferenceTypes (enable bool ) * ConfigBuilder {
164
- c .EnableWasmReferenceTypes = enable
165
- return c
75
+ // SetCraneliftDebugVerifier configures whether the cranelift debug verifier
76
+ // will be active when cranelift is used to compile wasm code.
77
+ func (c * Config ) SetCraneliftDebugVerifier (enabled bool ) {
78
+ c .inner .SetCraneliftDebugVerifier (enabled )
166
79
}
167
80
168
- // WithSIMD enables SIMD instructions including v128.
169
- // ref. https://github.com/webassembly/simd
170
- //
171
- // Default is false.
172
- func (c * ConfigBuilder ) WithSIMD (enable bool ) * ConfigBuilder {
173
- c .EnableWasmSIMD = enable
174
- return c
81
+ // SetCraneliftFlag sets a target-specific flag in Cranelift to the specified value.
82
+ func (c * Config ) SetCraneliftFlag (name string , value string ) {
83
+ c .inner .SetCraneliftFlag (name , value )
175
84
}
176
85
177
- // WithProfilingStrategy defines the profiling strategy used for defining the
178
- // default profiler.
179
- //
180
- // Default is `wasmtime.ProfilingStrategyNone`.
181
- func (c * ConfigBuilder ) WithProfilingStrategy (strategy wasmtime.ProfilingStrategy ) * ConfigBuilder {
182
- c .ProfilingStrategy = strategy
183
- return c
86
+ // SetCraneliftOptLevel configures the cranelift optimization level for generated code.
87
+ func (c * Config ) SetCraneliftOptLevel (level wasmtime.OptLevel ) {
88
+ c .inner .SetCraneliftOptLevel (level )
184
89
}
185
90
186
- // WithLimitMaxMemory defines the maximum number of pages of memory that can be used.
187
- // Each page represents 64KiB of memory.
188
- //
189
- // Default is 18 pages.
190
- func (c * ConfigBuilder ) WithLimitMaxMemory (max uint64 ) * ConfigBuilder {
191
- if max > math .MaxInt64 {
192
- c .err .Add (fmt .Errorf ("max memory %d is greater than max int64 %d" , max , math .MaxInt64 ))
193
- } else {
194
- c .LimitMaxMemory = int64 (max )
195
- }
196
- return c
91
+ // SetDebugInfo configures whether dwarf debug information for JIT code is enabled
92
+ func (c * Config ) SetDebugInfo (enabled bool ) {
93
+ c .inner .SetDebugInfo (enabled )
197
94
}
198
95
199
- // WithDefaultCache enables the default caching strategy.
200
- //
201
- // Default is false.
202
- func (c * ConfigBuilder ) WithDefaultCache (enabled bool ) * ConfigBuilder {
203
- c .EnableDefaultCache = enabled
204
- return c
96
+ // SetEpochInterruption enables epoch-based instrumentation of generated code to
97
+ // interrupt WebAssembly execution when the current engine epoch exceeds a
98
+ // defined threshold.
99
+ func (c * Config ) SetEpochInterruption (enable bool ) {
100
+ c .inner .SetEpochInterruption (enable )
205
101
}
206
102
207
- // WithDebugMode enables debug mode which provides access to
208
- // useful debugging information. If [enabled] is true we set
209
- // EnableBulkMemory to support useful IO operations during debugging.
210
- //
211
- // This should not be set for a live system as it has
212
- // both performance and security considerations.
213
- //
214
- // Note: This requires Rust programs to be compiled with the wasm32-wasi target.
215
- //
216
- // Default is false.
217
- func (c * ConfigBuilder ) WithDebugMode (enabled bool ) * ConfigBuilder {
218
- c .EnableDebugMode = enabled
219
- if enabled {
220
- c .EnableBulkMemory = true
221
- }
222
-
223
- return c
103
+ // SetMaxWasmStack configures the maximum stack size, in bytes, that JIT code can use.
104
+ func (c * Config ) SetMaxWasmStack (size int ) {
105
+ c .inner .SetMaxWasmStack (size )
224
106
}
225
107
226
- // build is private to ensure that the config id built
227
- func (c * ConfigBuilder ) Build () (* Config , error ) {
228
- // validate inputs
229
- if c .err .Errored () {
230
- return nil , c .err .Err
231
- }
232
-
233
- return & Config {
234
- // engine config
235
- enableBulkMemory : c .EnableBulkMemory ,
236
- enableWasmMultiValue : c .EnableWasmMultiValue ,
237
- enableWasmReferenceTypes : c .EnableWasmReferenceTypes ,
238
- enableWasmSIMD : c .EnableWasmSIMD ,
239
- enableDefaultCache : c .EnableDefaultCache ,
240
- maxWasmStack : c .MaxWasmStack ,
241
- profilingStrategy : c .ProfilingStrategy ,
108
+ // SetProfiler configures what profiler strategy to use for generated code.
109
+ func (c * Config ) SetProfiler (profiler wasmtime.ProfilingStrategy ) {
110
+ c .inner .SetProfiler (profiler )
111
+ }
242
112
243
- // limits
244
- limitMaxMemory : c .LimitMaxMemory ,
245
- limitMaxTableElements : defaultLimitMaxTableElements ,
246
- limitMaxTables : defaultLimitMaxTables ,
247
- limitMaxInstances : defaultLimitMaxInstances ,
248
- limitMaxMemories : defaultLimitMaxMemories ,
113
+ // SetStrategy configures what compilation strategy is used to compile wasm code.
114
+ func (c * Config ) SetStrategy (strat wasmtime.Strategy ) {
115
+ c .inner .SetStrategy (strat )
116
+ }
249
117
250
- // runtime config
251
- CompileStrategy : c .CompileStrategy ,
252
- DebugMode : c .EnableDebugMode ,
253
- }, nil
118
+ // SetTarget configures the target triple that this configuration will produce machine code for.
119
+ func (c * Config ) SetTarget (target string ) error {
120
+ return c .inner .SetTarget (target )
254
121
}
255
122
256
- // Engine returns a new wasmtime engine config. A wasmtime config can only be used once.
257
- func (c * Config ) Engine () (* wasmtime.Config , error ) {
258
- cfg := defaultWasmtimeConfig ()
259
- if c .enableDefaultCache {
260
- err := cfg .CacheConfigLoadDefault ()
261
- if err != nil {
262
- return nil , err
263
- }
264
- }
123
+ // SetWasmBulkMemory configures whether the wasm bulk memory proposal is enabled.
124
+ func (c * Config ) SetWasmBulkMemory (enabled bool ) {
125
+ c .inner .SetWasmBulkMemory (enabled )
126
+ }
265
127
266
- cfg .SetWasmBulkMemory (c .enableBulkMemory )
267
- cfg .SetWasmMultiValue (c .enableWasmMultiValue )
268
- cfg .SetWasmReferenceTypes (c .enableWasmReferenceTypes )
269
- cfg .SetWasmSIMD (c .enableWasmSIMD )
270
- cfg .SetProfiler (c .profilingStrategy )
271
- cfg .SetMaxWasmStack (c .maxWasmStack )
128
+ // SetWasmMemory64 configures whether the wasm memory64 proposal is enabled.
129
+ func (c * Config ) SetWasmMemory64 (enabled bool ) {
130
+ c .inner .SetWasmMemory64 (enabled )
131
+ }
272
132
273
- return cfg , nil
133
+ // SetWasmMultiMemory configures whether the wasm multi memory proposal is enabled.
134
+ func (c * Config ) SetWasmMultiMemory (enabled bool ) {
135
+ c .inner .SetWasmMultiMemory (enabled )
274
136
}
275
137
276
- type Config struct {
277
- enableBulkMemory bool
278
- enableWasmMultiValue bool
279
- enableWasmReferenceTypes bool
280
- enableWasmSIMD bool
281
- enableDefaultCache bool
282
- maxWasmStack int
283
- profilingStrategy wasmtime.ProfilingStrategy
138
+ // SetWasmMultiValue configures whether the wasm multi value proposal is enabled.
139
+ func (c * Config ) SetWasmMultiValue (enabled bool ) {
140
+ c .inner .SetWasmMultiValue (enabled )
141
+ }
284
142
285
- // store limits
286
- limitMaxMemory int64
287
- limitMaxTableElements int64
288
- limitMaxTables int64
289
- // number of instances of this module can be instantiated in parallel
290
- limitMaxInstances int64
291
- limitMaxMemories int64
143
+ // SetWasmReferenceTypes configures whether the wasm reference types proposal is enabled.
144
+ func (c * Config ) SetWasmReferenceTypes (enabled bool ) {
145
+ c .inner .SetWasmReferenceTypes (enabled )
146
+ }
292
147
293
- DebugMode bool
148
+ // SetWasmSIMD configures whether the wasm SIMD proposal is enabled.
149
+ func (c * Config ) SetWasmSIMD (enabled bool ) {
150
+ c .inner .SetWasmSIMD (enabled )
151
+ }
294
152
295
- CompileStrategy CompileStrategy
153
+ // SetWasmThreads configures whether the wasm threads proposal is enabled.
154
+ func (c * Config ) SetWasmThreads (enabled bool ) {
155
+ c .inner .SetWasmThreads (enabled )
296
156
}
297
157
298
- func defaultWasmtimeConfig () * wasmtime.Config {
158
+ func DefaultWasmtimeConfig () * wasmtime.Config {
299
159
cfg := wasmtime .NewConfig ()
300
160
301
161
// non configurable defaults
0 commit comments