wardite 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +33 -7
- data/Steepfile +1 -0
- data/assets/wardite.svg +12 -0
- data/benchmark/measure_mir_cache_verify.rb +58 -0
- data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
- data/benchmark/measure_ruby_wasm_startup.rb +43 -0
- data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
- data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
- data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
- data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
- data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
- data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
- data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
- data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
- data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
- data/docs/efflorescence-generated-c-guards.md +5 -0
- data/docs/efflorescence-mir2-cache.md +123 -0
- data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
- data/docs/efflorescence-spinel-linear-memory.md +72 -0
- data/docs/efflorescence-startup-snapshot.md +60 -0
- data/docs/efflorescence-unboxed-globals.md +61 -0
- data/lib/wardite/alu_f32.generated.rb +10 -2
- data/lib/wardite/alu_f64.generated.rb +10 -2
- data/lib/wardite/alu_i32.generated.rb +40 -8
- data/lib/wardite/alu_i64.generated.rb +55 -11
- data/lib/wardite/cli/cli.rb +56 -6
- data/lib/wardite/efflorescence/ir.rb +14 -1
- data/lib/wardite/efflorescence/mir_cache.rb +657 -0
- data/lib/wardite/efflorescence/register_validation.rb +128 -56
- data/lib/wardite/efflorescence/snapshot.rb +229 -0
- data/lib/wardite/efflorescence/store_access.rb +103 -8
- data/lib/wardite/efflorescence/translator.rb +10 -3
- data/lib/wardite/efflorescence/vm.rb +202 -38
- data/lib/wardite/efflorescence/wasi.rb +0 -56
- data/lib/wardite/efflorescence.rb +6 -0
- data/lib/wardite/load.rb +108 -24
- data/lib/wardite/version.rb +1 -1
- data/lib/wardite/wasi/spinel.rb +41 -0
- data/lib/wardite/wasi.rb +483 -268
- data/lib/wardite.rb +218 -70
- data/scripts/gen_alu.rb +55 -11
- data/scripts/verify_ruby_wasm.rb +70 -0
- data/sig/generated/wardite/cli/cli.rbs +15 -0
- data/sig/generated/wardite/efflorescence/ir.rbs +7 -0
- data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +31 -3
- data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +13 -2
- data/sig/generated/wardite/efflorescence/translator.rbs +5 -1
- data/sig/generated/wardite/efflorescence/vm.rbs +68 -7
- data/sig/generated/wardite/efflorescence/wasi.rbs +0 -13
- data/sig/generated/wardite/load.rbs +32 -5
- data/sig/generated/wardite/wasi/spinel.rbs +15 -0
- data/sig/generated/wardite/wasi.rbs +111 -4
- data/sig/generated/wardite.rbs +67 -3
- data/sig/mir_cache_stdlib.rbs +5 -0
- data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
- data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
- metadata +29 -1
|
@@ -6,16 +6,27 @@ module Wardite
|
|
|
6
6
|
class VM
|
|
7
7
|
attr_reader :instance #: Wardite::Instance
|
|
8
8
|
attr_reader :ir #: Wardite::Efflorescence::IR
|
|
9
|
-
attr_reader :semantic_ir #: Wardite::Efflorescence::IR
|
|
10
|
-
attr_reader :optimized_ir #: Wardite::Efflorescence::IR
|
|
11
9
|
attr_reader :metrics #: Wardite::Efflorescence::Metrics
|
|
10
|
+
attr_reader :mir_cache_hit #: bool
|
|
11
|
+
attr_reader :mir_cache #: Wardite::Efflorescence::MIRCache?
|
|
12
|
+
# Records a startup snapshot before the first WASI call outside its allowlist.
|
|
13
|
+
attr_accessor :snapshot #: Wardite::Efflorescence::Snapshot?
|
|
14
|
+
|
|
15
|
+
# @rbs @semantic_ir: Wardite::Efflorescence::IR?
|
|
16
|
+
# @rbs @optimized_ir: Wardite::Efflorescence::IR?
|
|
17
|
+
# @rbs @global_bits: Array[Integer]
|
|
12
18
|
|
|
13
19
|
# @rbs instance: Wardite::Instance
|
|
14
20
|
# @rbs disabled_optimizations: Array[Symbol]
|
|
15
21
|
# @rbs profile: bool
|
|
16
|
-
|
|
22
|
+
# @rbs mir_cache: Wardite::Efflorescence::MIRCache?
|
|
23
|
+
# @rbs cached_functions: Array[Function]?
|
|
24
|
+
# @rbs cache_checked: bool
|
|
25
|
+
def initialize(instance, disabled_optimizations: [], profile: false, mir_cache: nil, cached_functions: nil, cache_checked: false)
|
|
17
26
|
@instance = instance
|
|
18
27
|
@store_access = StoreAccess.new(instance.store)
|
|
28
|
+
@global_bits = []
|
|
29
|
+
@has_globals = !instance.store.globals.empty?
|
|
19
30
|
@function_indices = {} #: Hash[Integer, Integer]
|
|
20
31
|
# Retain the original objects so IDs stay valid even if a host edits
|
|
21
32
|
# Store.funcs. Spinel cannot use an identity Hash for these objects.
|
|
@@ -24,15 +35,29 @@ module Wardite
|
|
|
24
35
|
@indirect_functions = instance.store.funcs.dup
|
|
25
36
|
@indirect_functions.each_with_index { |function, index| @function_indices[function.object_id] = index }
|
|
26
37
|
end
|
|
27
|
-
configuration = Configuration.new(disabled_optimizations)
|
|
28
|
-
@reuse_frames = configuration.enabled?(:frames)
|
|
38
|
+
@configuration = Configuration.new(disabled_optimizations)
|
|
39
|
+
@reuse_frames = @configuration.enabled?(:frames)
|
|
29
40
|
@profiling = profile
|
|
30
41
|
@metrics = Metrics.new
|
|
31
|
-
@semantic_ir =
|
|
32
|
-
@
|
|
33
|
-
@
|
|
34
|
-
@
|
|
35
|
-
|
|
42
|
+
@semantic_ir = @optimized_ir = nil
|
|
43
|
+
@mir_cache = mir_cache
|
|
44
|
+
@snapshot = nil
|
|
45
|
+
@active_frames = [] #: Array[Wardite::Efflorescence::Frame]
|
|
46
|
+
@depth = 0
|
|
47
|
+
translator = Translator.new(instance)
|
|
48
|
+
linked = translator.link
|
|
49
|
+
if mir_cache && !profile && !cache_checked
|
|
50
|
+
cached_functions = mir_cache.read(instance)
|
|
51
|
+
end
|
|
52
|
+
@mir_cache_hit = !cached_functions.nil?
|
|
53
|
+
@ir = if cached_functions
|
|
54
|
+
IR.new(cached_functions, entries: linked.entries, hosts: linked.hosts)
|
|
55
|
+
else
|
|
56
|
+
build_mir(translator, linked, profile)
|
|
57
|
+
end
|
|
58
|
+
if mir_cache && !profile && !@mir_cache_hit
|
|
59
|
+
mir_cache.write(instance, @ir)
|
|
60
|
+
end
|
|
36
61
|
start = instance.start_section
|
|
37
62
|
if start
|
|
38
63
|
entry = @ir.entry_at(start.func_index)
|
|
@@ -43,6 +68,20 @@ module Wardite
|
|
|
43
68
|
end
|
|
44
69
|
end
|
|
45
70
|
|
|
71
|
+
# A caller explicitly inspecting a cache hit can still request the
|
|
72
|
+
# intermediate forms. Execution continues to use the restored MIR2.
|
|
73
|
+
# @rbs return: Wardite::Efflorescence::IR
|
|
74
|
+
def semantic_ir
|
|
75
|
+
restore_inspection_ir unless @semantic_ir
|
|
76
|
+
@semantic_ir || raise(Wardite::EvalError, "missing semantic MIR")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# @rbs return: Wardite::Efflorescence::IR
|
|
80
|
+
def optimized_ir
|
|
81
|
+
restore_inspection_ir unless @optimized_ir
|
|
82
|
+
@optimized_ir || raise(Wardite::EvalError, "missing optimized MIR")
|
|
83
|
+
end
|
|
84
|
+
|
|
46
85
|
# @rbs name: String|Symbol
|
|
47
86
|
# @rbs return: bool
|
|
48
87
|
def callable?(name)
|
|
@@ -89,12 +128,52 @@ module Wardite
|
|
|
89
128
|
# @rbs return: String
|
|
90
129
|
def dump_mir
|
|
91
130
|
inspection = Inspection.new
|
|
92
|
-
"Semantic MIR (#{inspection.statistics(
|
|
93
|
-
"Optimized MIR (#{inspection.statistics(
|
|
131
|
+
"Semantic MIR (#{inspection.statistics(semantic_ir)})\n#{inspection.disassemble(semantic_ir)}\n" \
|
|
132
|
+
"Optimized MIR (#{inspection.statistics(optimized_ir)})\n#{inspection.disassemble(optimized_ir)}"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Continue a public call restored from a startup snapshot. Each frame
|
|
136
|
+
# resumes at its saved pc: the host call for the top, a return for others.
|
|
137
|
+
# @rbs saved: Array[Wardite::Efflorescence::SavedFrame]
|
|
138
|
+
# @rbs return: void
|
|
139
|
+
def resume(saved)
|
|
140
|
+
pools = new_pools
|
|
141
|
+
frames = [] #: Array[Wardite::Efflorescence::Frame]
|
|
142
|
+
saved.each do |state|
|
|
143
|
+
frame = pool_at(pools, state.pool_index).acquire(state.return_register)
|
|
144
|
+
registers = frame.registers
|
|
145
|
+
state.registers.each_with_index { |value, i| registers[i] = value }
|
|
146
|
+
state.handlers.each { |handler| frame.handlers << handler }
|
|
147
|
+
frame.pc = state.pc
|
|
148
|
+
frames << frame
|
|
149
|
+
end
|
|
150
|
+
run_frames(pools, frames, 0)
|
|
151
|
+
nil
|
|
94
152
|
end
|
|
95
153
|
|
|
96
154
|
private
|
|
97
155
|
|
|
156
|
+
# @rbs translator: Wardite::Efflorescence::Translator
|
|
157
|
+
# @rbs linked: Wardite::Efflorescence::IR
|
|
158
|
+
# @rbs profile: bool
|
|
159
|
+
# @rbs return: Wardite::Efflorescence::IR
|
|
160
|
+
def build_mir(translator, linked, profile)
|
|
161
|
+
semantic = translator.translate(linked)
|
|
162
|
+
optimized = Optimizer.new(@configuration).optimize(semantic)
|
|
163
|
+
optimized.functions.each { |function| RegisterValidation.new.validate(function, optimized, tags: @instance.tags) }
|
|
164
|
+
ir = profile ? Instrumentation.new.instrument(optimized) : optimized
|
|
165
|
+
Lowerer.new.compile(ir)
|
|
166
|
+
@semantic_ir = semantic
|
|
167
|
+
@optimized_ir = optimized
|
|
168
|
+
ir
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# @rbs return: void
|
|
172
|
+
def restore_inspection_ir
|
|
173
|
+
linked = IR.new([], entries: @ir.entries, hosts: @ir.hosts)
|
|
174
|
+
build_mir(Translator.new(@instance), linked, false)
|
|
175
|
+
end
|
|
176
|
+
|
|
98
177
|
# @rbs name: String|Symbol
|
|
99
178
|
# @rbs return: Wardite::Efflorescence::FunctionEntry
|
|
100
179
|
def export_entry(name)
|
|
@@ -288,12 +367,42 @@ module Wardite
|
|
|
288
367
|
pools[index] || raise(Wardite::EvalError, "missing MIR frame pool")
|
|
289
368
|
end
|
|
290
369
|
|
|
370
|
+
# Update in place so a suspended execute_flat retains the same array
|
|
371
|
+
# across a host call, including a reentrant call on this VM.
|
|
372
|
+
# @rbs return: void
|
|
373
|
+
def read_globals
|
|
374
|
+
@instance.store.globals.each_with_index do |global, index|
|
|
375
|
+
@global_bits[index] = @store_access.global_bits_at(index, global.type)
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
# @rbs return: void
|
|
380
|
+
def write_globals
|
|
381
|
+
@instance.store.globals.each_with_index do |global, index|
|
|
382
|
+
@store_access.write_global_bits(index, global.type, @global_bits.fetch(index))
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
291
386
|
# @rbs host_index: Integer
|
|
292
387
|
# @rbs arguments: Array[Integer]
|
|
293
388
|
# @rbs metrics: Wardite::Efflorescence::Metrics
|
|
389
|
+
# @rbs sources: Array[Integer]?
|
|
294
390
|
# @rbs return: Integer?
|
|
391
|
+
def invoke_host_with_globals(host_index, arguments, metrics, sources)
|
|
392
|
+
write_globals
|
|
393
|
+
begin
|
|
394
|
+
invoke_host(host_index, arguments, metrics, sources)
|
|
395
|
+
ensure
|
|
396
|
+
read_globals
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# @rbs host_index: Integer
|
|
401
|
+
# @rbs arguments: Array[Integer]
|
|
402
|
+
# @rbs metrics: Wardite::Efflorescence::Metrics
|
|
295
403
|
# @rbs sources: Array[Integer]?
|
|
296
|
-
|
|
404
|
+
# @rbs return: Integer?
|
|
405
|
+
def invoke_host(host_index, arguments, metrics, sources)
|
|
297
406
|
host = @ir.hosts[host_index] || raise(Wardite::EvalError, "missing MIR host function")
|
|
298
407
|
metrics.host_calls += 1 if @profiling
|
|
299
408
|
if RUBY_ENGINE != "spinel"
|
|
@@ -348,25 +457,55 @@ module Wardite
|
|
|
348
457
|
raise Wardite::EvalError, "host ABI result mismatch for #{host.external.name}"
|
|
349
458
|
end
|
|
350
459
|
|
|
351
|
-
# Each public entry owns its frames, pools and counters
|
|
352
|
-
#
|
|
460
|
+
# Each public or start entry owns its frames, pools and counters. Host
|
|
461
|
+
# calls synchronize globals before a callback can re-enter the VM.
|
|
353
462
|
# @rbs entry: Wardite::Efflorescence::FunctionEntry
|
|
354
463
|
# @rbs arguments: Array[Integer]
|
|
355
464
|
# @rbs return: Integer?
|
|
356
465
|
def execute(entry, arguments)
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
466
|
+
if entry.kind == :host
|
|
467
|
+
read_globals if @has_globals
|
|
468
|
+
metrics = @profiling ? Metrics.new : @metrics
|
|
469
|
+
begin
|
|
470
|
+
return @has_globals ? invoke_host_with_globals(entry.host_index, arguments, metrics, nil) :
|
|
471
|
+
invoke_host(entry.host_index, arguments, metrics, nil)
|
|
472
|
+
ensure
|
|
473
|
+
@metrics = metrics
|
|
474
|
+
write_globals if @has_globals
|
|
475
|
+
end
|
|
476
|
+
end
|
|
361
477
|
|
|
362
|
-
|
|
478
|
+
pools = new_pools
|
|
363
479
|
frame = pool_at(pools, entry.defined_index).acquire(-1)
|
|
364
|
-
active_frames << frame
|
|
365
|
-
metrics.root_defined_entries = 1 if @profiling
|
|
366
480
|
arguments.each_with_index { |value, i| frame.registers[i] = value }
|
|
367
|
-
result =
|
|
481
|
+
result = run_frames(pools, [frame], 1)
|
|
368
482
|
entry.result_types.empty? ? nil : result
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
# @rbs return: Array[Wardite::Efflorescence::FramePool]
|
|
486
|
+
def new_pools
|
|
487
|
+
pools = [] #: Array[Wardite::Efflorescence::FramePool]
|
|
488
|
+
@ir.functions.each { |function| pools << FramePool.new(function, @reuse_frames) }
|
|
489
|
+
pools
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
# Globals live in @global_bits while frames run. A restored snapshot
|
|
493
|
+
# writes the Store first, so both public calls and resume read it here.
|
|
494
|
+
# @rbs pools: Array[Wardite::Efflorescence::FramePool]
|
|
495
|
+
# @rbs active_frames: Array[Wardite::Efflorescence::Frame]
|
|
496
|
+
# @rbs root_entries: Integer
|
|
497
|
+
# @rbs return: Integer
|
|
498
|
+
def run_frames(pools, active_frames, root_entries)
|
|
499
|
+
read_globals if @has_globals
|
|
500
|
+
metrics = @profiling ? Metrics.new : @metrics
|
|
501
|
+
metrics.root_defined_entries = root_entries if @profiling
|
|
502
|
+
outer = @active_frames
|
|
503
|
+
@active_frames = active_frames
|
|
504
|
+
@depth += 1
|
|
505
|
+
execute_flat(current_frame(active_frames), pools, active_frames, metrics)
|
|
369
506
|
ensure
|
|
507
|
+
@depth -= 1
|
|
508
|
+
@active_frames = outer
|
|
370
509
|
active_frames.each { |active| pool_at(pools, active.pool_index).release(active) }
|
|
371
510
|
if @profiling
|
|
372
511
|
created = 0
|
|
@@ -374,6 +513,7 @@ module Wardite
|
|
|
374
513
|
metrics.frames_created = created
|
|
375
514
|
@metrics = metrics
|
|
376
515
|
end
|
|
516
|
+
write_globals if @has_globals
|
|
377
517
|
end
|
|
378
518
|
|
|
379
519
|
# @rbs frame: Wardite::Efflorescence::Frame
|
|
@@ -384,7 +524,23 @@ module Wardite
|
|
|
384
524
|
# @rbs return: void
|
|
385
525
|
def execute_host_site(frame, site, host_index, destination, metrics)
|
|
386
526
|
registers = frame.registers
|
|
387
|
-
|
|
527
|
+
recorder = @snapshot
|
|
528
|
+
if recorder
|
|
529
|
+
host = @ir.hosts[host_index] || raise(Wardite::EvalError, "missing MIR host function")
|
|
530
|
+
if recorder.checkpoint?(host, registers, site.arguments)
|
|
531
|
+
@snapshot = nil
|
|
532
|
+
if @depth == 1
|
|
533
|
+
# The snapshot reads globals from the Store.
|
|
534
|
+
write_globals if @has_globals
|
|
535
|
+
recorder.save(@instance, @active_frames)
|
|
536
|
+
end
|
|
537
|
+
end
|
|
538
|
+
end
|
|
539
|
+
result = unless @has_globals
|
|
540
|
+
invoke_host(host_index, registers, metrics, site.arguments)
|
|
541
|
+
else
|
|
542
|
+
invoke_host_with_globals(host_index, registers, metrics, site.arguments)
|
|
543
|
+
end
|
|
388
544
|
registers[destination] = result if destination >= 0 && result
|
|
389
545
|
end
|
|
390
546
|
|
|
@@ -434,8 +590,10 @@ module Wardite
|
|
|
434
590
|
entry = indirect_entry(site, registers[selector])
|
|
435
591
|
if entry.kind == :host
|
|
436
592
|
execute_host_site(frame, site, entry.host_index, destination, metrics)
|
|
593
|
+
frame.pc += Lowerer::WIDTH
|
|
437
594
|
frame
|
|
438
595
|
else
|
|
596
|
+
frame.pc += Lowerer::WIDTH
|
|
439
597
|
next_frame = pool_at(pools, entry.defined_index).acquire(destination)
|
|
440
598
|
frames << next_frame
|
|
441
599
|
target = next_frame.registers
|
|
@@ -477,14 +635,6 @@ module Wardite
|
|
|
477
635
|
when Opcode::F64_STORE
|
|
478
636
|
offset = instructions.fetch(pc + 3)
|
|
479
637
|
@store_access.store(registers[b], offset, 8, registers[a])
|
|
480
|
-
when Opcode::F32_GLOBAL_GET
|
|
481
|
-
registers[a] = @store_access.global_get_float(b, :f32)
|
|
482
|
-
when Opcode::F64_GLOBAL_GET
|
|
483
|
-
registers[a] = @store_access.global_get_float(b, :f64)
|
|
484
|
-
when Opcode::F32_GLOBAL_SET
|
|
485
|
-
@store_access.global_set_float(a, :f32, registers[b])
|
|
486
|
-
when Opcode::F64_GLOBAL_SET
|
|
487
|
-
@store_access.global_set_float(a, :f64, registers[b])
|
|
488
638
|
else
|
|
489
639
|
raise Wardite::EvalError, "Efflorescence: invalid MIR opcode #{opcode}"
|
|
490
640
|
end
|
|
@@ -499,8 +649,9 @@ module Wardite
|
|
|
499
649
|
def execute_flat(frame, pools, active_frames, metrics)
|
|
500
650
|
frames = active_frames
|
|
501
651
|
registers = frame.registers
|
|
652
|
+
global_bits = @global_bits
|
|
502
653
|
instructions = frame.function.bytecode
|
|
503
|
-
pc =
|
|
654
|
+
pc = Integer(frame.pc)
|
|
504
655
|
# Avoid Kernel#loop's StopIteration handler in the native hot loop.
|
|
505
656
|
while true
|
|
506
657
|
opcode = instructions.fetch(pc)
|
|
@@ -619,11 +770,12 @@ module Wardite
|
|
|
619
770
|
pc = 0
|
|
620
771
|
next
|
|
621
772
|
when 67 # Opcode::CALL_HOST
|
|
773
|
+
frame.pc = pc
|
|
622
774
|
execute_direct_host(frame, instructions.fetch(pc + 2), a, metrics)
|
|
623
775
|
when 68 # Opcode::CALL_INDIRECT
|
|
624
776
|
b = instructions.fetch(pc + 2)
|
|
625
777
|
c = instructions.fetch(pc + 3)
|
|
626
|
-
frame.pc = pc
|
|
778
|
+
frame.pc = pc
|
|
627
779
|
frame = execute_indirect(frame, pools, frames, a, b, c, metrics)
|
|
628
780
|
registers = frame.registers
|
|
629
781
|
instructions = frame.function.bytecode
|
|
@@ -762,10 +914,10 @@ module Wardite
|
|
|
762
914
|
next
|
|
763
915
|
when 52 # Opcode::GLOBAL_GET
|
|
764
916
|
b = instructions.fetch(pc + 2)
|
|
765
|
-
registers[a] =
|
|
917
|
+
registers[a] = global_bits[b]
|
|
766
918
|
when 53 # Opcode::GLOBAL_SET
|
|
767
919
|
b = instructions.fetch(pc + 2)
|
|
768
|
-
|
|
920
|
+
global_bits[a] = registers[b]
|
|
769
921
|
when 54 # Opcode::I32_LOAD
|
|
770
922
|
b = instructions.fetch(pc + 2)
|
|
771
923
|
c = instructions.fetch(pc + 3)
|
|
@@ -985,10 +1137,22 @@ module Wardite
|
|
|
985
1137
|
@store_access.store(registers[b], c, 4, registers[a])
|
|
986
1138
|
when 115 # Opcode::I64_GLOBAL_GET
|
|
987
1139
|
b = instructions.fetch(pc + 2)
|
|
988
|
-
registers[a] =
|
|
1140
|
+
registers[a] = global_bits[b]
|
|
989
1141
|
when 116 # Opcode::I64_GLOBAL_SET
|
|
990
1142
|
b = instructions.fetch(pc + 2)
|
|
991
|
-
|
|
1143
|
+
global_bits[a] = registers[b]
|
|
1144
|
+
when 191 # Opcode::F32_GLOBAL_GET
|
|
1145
|
+
b = instructions.fetch(pc + 2)
|
|
1146
|
+
registers[a] = global_bits[b]
|
|
1147
|
+
when 192 # Opcode::F64_GLOBAL_GET
|
|
1148
|
+
b = instructions.fetch(pc + 2)
|
|
1149
|
+
registers[a] = global_bits[b]
|
|
1150
|
+
when 193 # Opcode::F32_GLOBAL_SET
|
|
1151
|
+
b = instructions.fetch(pc + 2)
|
|
1152
|
+
global_bits[a] = registers[b]
|
|
1153
|
+
when 194 # Opcode::F64_GLOBAL_SET
|
|
1154
|
+
b = instructions.fetch(pc + 2)
|
|
1155
|
+
global_bits[a] = registers[b]
|
|
992
1156
|
when 22 # Opcode::PROFILE
|
|
993
1157
|
b = instructions.fetch(pc + 2)
|
|
994
1158
|
c = instructions.fetch(pc + 3)
|
|
@@ -21,14 +21,8 @@ module Wardite
|
|
|
21
21
|
def self.unboxed_callable(environment, function)
|
|
22
22
|
case function.name
|
|
23
23
|
when :fd_write
|
|
24
|
-
unless function.callsig == [:i32, :i32, :i32, :i32] && function.retsig == [:i32]
|
|
25
|
-
raise Wardite::LoadError, "invalid WASI signature for fd_write"
|
|
26
|
-
end
|
|
27
24
|
environment.method(:fd_write_from_registers)
|
|
28
25
|
when :proc_exit
|
|
29
|
-
unless function.callsig == [:i32] && function.retsig.empty?
|
|
30
|
-
raise Wardite::LoadError, "invalid WASI signature for proc_exit"
|
|
31
|
-
end
|
|
32
26
|
environment.method(:proc_exit_from_registers)
|
|
33
27
|
end
|
|
34
28
|
end
|
|
@@ -53,56 +47,6 @@ module Wardite
|
|
|
53
47
|
exit(values.fetch((sources || PROC_EXIT_ARGUMENTS).fetch(0)))
|
|
54
48
|
end
|
|
55
49
|
|
|
56
|
-
# @rbs store: Wardite::Store
|
|
57
|
-
# @rbs fd: Integer
|
|
58
|
-
# @rbs iovs: Integer
|
|
59
|
-
# @rbs count: Integer
|
|
60
|
-
# @rbs written_at: Integer
|
|
61
|
-
# @rbs return: Integer
|
|
62
|
-
def fd_write_i32(store, fd, iovs, count, written_at)
|
|
63
|
-
file = fd_table[fd]
|
|
64
|
-
return Wasi::EBADF if !file || file.is_a?(Wasi::PreopenedDir)
|
|
65
|
-
|
|
66
|
-
data = store.memories[0]&.data
|
|
67
|
-
return Wasi::EFAULT unless data && written_at + 4 <= data.bytesize && iovs + count * 8 <= data.bytesize
|
|
68
|
-
|
|
69
|
-
if count == 1
|
|
70
|
-
payload = iovec_payload(data, iovs)
|
|
71
|
-
return Wasi::EFAULT unless payload
|
|
72
|
-
written = file.write(payload)
|
|
73
|
-
else
|
|
74
|
-
# Validate every guest buffer before any write to the descriptor.
|
|
75
|
-
payloads = [] #: Array[String]
|
|
76
|
-
total = 0
|
|
77
|
-
count.times do |index|
|
|
78
|
-
payload = iovec_payload(data, iovs + index * 8)
|
|
79
|
-
return Wasi::EFAULT unless payload
|
|
80
|
-
total += payload.bytesize
|
|
81
|
-
return Wasi::EOVERFLOW if total > 0xffffffff
|
|
82
|
-
payloads << payload
|
|
83
|
-
end
|
|
84
|
-
written = 0
|
|
85
|
-
payloads.each do |payload|
|
|
86
|
-
bytes = file.write(payload)
|
|
87
|
-
written += bytes
|
|
88
|
-
break if bytes < payload.bytesize
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
data[written_at, 4] = [written].pack("V")
|
|
92
|
-
0
|
|
93
|
-
rescue Errno::EBADF
|
|
94
|
-
Wasi::EBADF
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
# @rbs data: String
|
|
98
|
-
# @rbs iov: Integer
|
|
99
|
-
# @rbs return: String?
|
|
100
|
-
def iovec_payload(data, iov)
|
|
101
|
-
offset = Integer(data.unpack1("V", offset: iov))
|
|
102
|
-
length = Integer(data.unpack1("V", offset: iov + 4))
|
|
103
|
-
return nil if offset + length > data.bytesize
|
|
104
|
-
data.byteslice(offset, length)
|
|
105
|
-
end
|
|
106
50
|
end
|
|
107
51
|
end
|
|
108
52
|
end
|
|
@@ -9,3 +9,9 @@ require_relative "efflorescence/optimizer"
|
|
|
9
9
|
require_relative "efflorescence/register_validation"
|
|
10
10
|
require_relative "efflorescence/inspection"
|
|
11
11
|
require_relative "efflorescence/vm"
|
|
12
|
+
|
|
13
|
+
# Spinel initializes the cache tables at startup, after Opcode is defined.
|
|
14
|
+
if RUBY_ENGINE == "spinel"
|
|
15
|
+
require_relative "efflorescence/mir_cache"
|
|
16
|
+
require_relative "efflorescence/snapshot"
|
|
17
|
+
end
|
data/lib/wardite/load.rb
CHANGED
|
@@ -142,15 +142,73 @@ module Wardite
|
|
|
142
142
|
|
|
143
143
|
attr_accessor :locals_type #: Array[Symbol]
|
|
144
144
|
|
|
145
|
-
|
|
145
|
+
attr_writer :instruction_bytes #: String?
|
|
146
|
+
|
|
147
|
+
attr_writer :type_section #: TypeSection?
|
|
146
148
|
|
|
147
149
|
# @rbs &blk: (CodeBody) -> void
|
|
148
150
|
# @rbs return: void
|
|
149
151
|
def initialize(&blk)
|
|
150
152
|
@locals_count = []
|
|
151
153
|
@locals_type = []
|
|
154
|
+
@body = [] #: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
155
|
+
@instruction_bytes = nil
|
|
156
|
+
@type_section = nil
|
|
152
157
|
blk.call(self)
|
|
153
158
|
end
|
|
159
|
+
|
|
160
|
+
# @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
161
|
+
def body
|
|
162
|
+
check_features(@type_section, false) if @instruction_bytes
|
|
163
|
+
@body
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# @rbs instructions: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
167
|
+
# @rbs return: void
|
|
168
|
+
def body=(instructions)
|
|
169
|
+
@body = instructions
|
|
170
|
+
@instruction_bytes = nil
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# @rbs types: TypeSection?
|
|
174
|
+
# @rbs stack: bool
|
|
175
|
+
# @rbs return: void
|
|
176
|
+
def check_features(types, stack)
|
|
177
|
+
if stack && locals_type.include?(:exnref)
|
|
178
|
+
raise LoadError, "stack engine does not support exnref"
|
|
179
|
+
end
|
|
180
|
+
bytes = @instruction_bytes
|
|
181
|
+
instructions = bytes ? BinaryLoader.code_body(StringIO.new(bytes)) : @body
|
|
182
|
+
check_instructions(instructions, types, stack)
|
|
183
|
+
self.body = instructions if bytes
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
private
|
|
187
|
+
|
|
188
|
+
# @rbs instructions: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
189
|
+
# @rbs types: TypeSection?
|
|
190
|
+
# @rbs stack: bool
|
|
191
|
+
# @rbs return: void
|
|
192
|
+
def check_instructions(instructions, types, stack)
|
|
193
|
+
instructions.each do |instruction|
|
|
194
|
+
if stack && [:try_table, :throw, :throw_ref].include?(instruction[1])
|
|
195
|
+
raise LoadError, "stack engine does not support EH"
|
|
196
|
+
end
|
|
197
|
+
instruction[2].each do |operand|
|
|
198
|
+
next unless operand.is_a?(Block)
|
|
199
|
+
index = operand.type_index
|
|
200
|
+
if index >= 0
|
|
201
|
+
params = types&.defined_types&.[](index)
|
|
202
|
+
results = types&.defined_results&.[](index)
|
|
203
|
+
raise LoadError, "invalid block type index #{index}" unless params && results
|
|
204
|
+
operand.resolve(params, results)
|
|
205
|
+
end
|
|
206
|
+
if stack && (index >= 0 || operand.result_types.include?(:exnref))
|
|
207
|
+
raise LoadError, "stack engine does not support indexed/reference block signatures"
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
154
212
|
end
|
|
155
213
|
|
|
156
214
|
attr_accessor :func_codes #:Array[CodeBody]
|
|
@@ -271,38 +329,57 @@ module Wardite
|
|
|
271
329
|
# @rbs engine: Symbol
|
|
272
330
|
# @rbs disabled_optimizations: Array[Symbol]
|
|
273
331
|
# @rbs mir_profile: bool
|
|
332
|
+
# @rbs mir_cache: String?
|
|
333
|
+
# @rbs mir_cache_verify: Symbol
|
|
274
334
|
# @rbs return: Instance
|
|
275
|
-
def self.load_from_buffer(buf, import_object: {}, enable_wasi: true, engine: :stack, disabled_optimizations: [], mir_profile: false)
|
|
335
|
+
def self.load_from_buffer(buf, import_object: {}, enable_wasi: true, engine: :stack, disabled_optimizations: [], mir_profile: false, mir_cache: nil, mir_cache_verify: :checksum)
|
|
276
336
|
start = Time.now.to_f #: Float
|
|
337
|
+
unless mir_cache_verify == :checksum || mir_cache_verify == :sha256
|
|
338
|
+
raise ArgumentError, "MIR cache verification must be checksum or sha256"
|
|
339
|
+
end
|
|
277
340
|
@buf = buf
|
|
341
|
+
cache = nil #: Efflorescence::MIRCache?
|
|
342
|
+
if mir_cache
|
|
343
|
+
raise ArgumentError, "MIR cache requires Efflorescence" unless engine == :efflorescence
|
|
344
|
+
unless mir_profile
|
|
345
|
+
require "wardite/efflorescence/mir_cache" unless RUBY_ENGINE == "spinel"
|
|
346
|
+
# Hash the same bytes that the decoder will see, even if the file changes.
|
|
347
|
+
bytes = buf.read || "".b
|
|
348
|
+
configuration = Efflorescence::Configuration.new(disabled_optimizations)
|
|
349
|
+
disabled_optimizations = configuration.disabled
|
|
350
|
+
cache = Efflorescence::MIRCache.new(mir_cache, bytes, configuration, verify: mir_cache_verify)
|
|
351
|
+
@buf = StringIO.new(bytes)
|
|
352
|
+
end
|
|
353
|
+
end
|
|
278
354
|
|
|
279
355
|
version = preamble
|
|
280
|
-
sections_ = sections
|
|
356
|
+
sections_ = sections(!cache.nil?)
|
|
357
|
+
@buf = buf if cache
|
|
281
358
|
|
|
282
359
|
wasi_env = nil
|
|
283
|
-
if enable_wasi
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
end
|
|
295
|
-
import_object[:wasi_snapshot_preview1] = wasi_env
|
|
360
|
+
if enable_wasi && sections_.any? { |section| section.is_a?(ImportSection) &&
|
|
361
|
+
section.imports.any? { |import| import.module_name == "wasi_snapshot_preview1" } }
|
|
362
|
+
wasi_env = if RUBY_ENGINE == "spinel"
|
|
363
|
+
require "wardite/wasi"
|
|
364
|
+
Wardite::WasiSnapshotPreview1.new
|
|
365
|
+
elsif engine == :efflorescence
|
|
366
|
+
require "wardite/efflorescence/wasi"
|
|
367
|
+
Wardite::Efflorescence::WasiSnapshotPreview1.new
|
|
368
|
+
else
|
|
369
|
+
require "wardite/wasi"
|
|
370
|
+
Wardite::WasiSnapshotPreview1.new
|
|
296
371
|
end
|
|
372
|
+
import_object[:wasi_snapshot_preview1] = wasi_env
|
|
297
373
|
end
|
|
298
374
|
|
|
299
|
-
return Instance.new(import_object, engine: engine, disabled_optimizations: disabled_optimizations,
|
|
375
|
+
return Instance.new(import_object, engine: engine, disabled_optimizations: disabled_optimizations,
|
|
376
|
+
mir_profile: mir_profile, mir_cache: cache) do |i|
|
|
300
377
|
i.version = version
|
|
301
378
|
i.sections = sections_
|
|
302
379
|
i.wasi = wasi_env
|
|
303
380
|
end
|
|
304
381
|
ensure
|
|
305
|
-
$stderr.puts "load_from_buffer: #{Time.now.to_f - start}" if ENV['
|
|
382
|
+
$stderr.puts "load_from_buffer: #{Time.now.to_f - start}" if ENV['WARDITE_TRACE']
|
|
306
383
|
end
|
|
307
384
|
|
|
308
385
|
# @rbs return: Integer
|
|
@@ -326,8 +403,9 @@ module Wardite
|
|
|
326
403
|
version
|
|
327
404
|
end
|
|
328
405
|
|
|
406
|
+
# @rbs defer_code: bool
|
|
329
407
|
# @rbs return: Array[Section]
|
|
330
|
-
def self.sections
|
|
408
|
+
def self.sections(defer_code = false)
|
|
331
409
|
sections = [] #: Array[Section]
|
|
332
410
|
|
|
333
411
|
loop do
|
|
@@ -359,7 +437,7 @@ module Wardite
|
|
|
359
437
|
when Wardite::SectionElement
|
|
360
438
|
elem_section
|
|
361
439
|
when Wardite::SectionCode
|
|
362
|
-
code_section
|
|
440
|
+
code_section(defer_code)
|
|
363
441
|
when Wardite::SectionData
|
|
364
442
|
data_section
|
|
365
443
|
when Wardite::Const::SectionDataCount
|
|
@@ -630,21 +708,23 @@ module Wardite
|
|
|
630
708
|
dest
|
|
631
709
|
end
|
|
632
710
|
|
|
711
|
+
# @rbs defer_code: bool
|
|
633
712
|
# @rbs return: CodeSection
|
|
634
|
-
def self.code_section
|
|
713
|
+
def self.code_section(defer_code = false)
|
|
635
714
|
dest = CodeSection.new
|
|
636
715
|
size = fetch_uleb128(@buf)
|
|
637
716
|
dest.size = size
|
|
638
717
|
sbuf = StringIO.new(assert_read(@buf, size))
|
|
639
718
|
|
|
640
719
|
len = fetch_uleb128(sbuf)
|
|
641
|
-
len.times { dest.func_codes << read_function_body(sbuf) }
|
|
720
|
+
len.times { dest.func_codes << read_function_body(sbuf, defer_code) }
|
|
642
721
|
dest
|
|
643
722
|
end
|
|
644
723
|
|
|
645
724
|
# @rbs sbuf: StringIO
|
|
725
|
+
# @rbs defer_code: bool
|
|
646
726
|
# @rbs return: CodeSection::CodeBody
|
|
647
|
-
def self.read_function_body(sbuf)
|
|
727
|
+
def self.read_function_body(sbuf, defer_code = false)
|
|
648
728
|
ilen = fetch_uleb128(sbuf)
|
|
649
729
|
code = assert_read(sbuf, ilen)
|
|
650
730
|
last_code = code[-1]
|
|
@@ -662,7 +742,11 @@ module Wardite
|
|
|
662
742
|
body.locals_type << Op.i2type(assert_read(cbuf, 1).ord)
|
|
663
743
|
locals_len -= 1
|
|
664
744
|
end
|
|
665
|
-
|
|
745
|
+
if defer_code
|
|
746
|
+
body.instruction_bytes = (cbuf.read || "".b).freeze
|
|
747
|
+
else
|
|
748
|
+
body.body = code_body(cbuf)
|
|
749
|
+
end
|
|
666
750
|
end
|
|
667
751
|
end
|
|
668
752
|
|
data/lib/wardite/version.rb
CHANGED