wardite 1.0.0 → 1.2.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/Makefile +9 -1
- data/README.md +65 -7
- data/Steepfile +1 -0
- data/assets/wardite.svg +12 -0
- data/benchmark/count_ruby_wasm_functions.rb +37 -0
- data/benchmark/measure_mir_cache_restore.rb +59 -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-mir2-function-restore.json +480 -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 +152 -0
- data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
- data/docs/efflorescence-spinel-linear-memory.md +74 -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 +58 -6
- data/lib/wardite/efflorescence/ir.rb +26 -4
- data/lib/wardite/efflorescence/mir_cache.rb +749 -0
- data/lib/wardite/efflorescence/register_validation.rb +128 -56
- data/lib/wardite/efflorescence/snapshot.rb +232 -0
- data/lib/wardite/efflorescence/store_access.rb +103 -8
- data/lib/wardite/efflorescence/translator.rb +10 -3
- data/lib/wardite/efflorescence/vm.rb +203 -38
- data/lib/wardite/efflorescence/wasi.rb +0 -56
- data/lib/wardite/efflorescence.rb +6 -0
- data/lib/wardite/load.rb +109 -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/build_spinel_zig.rb +121 -0
- 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 +12 -2
- data/sig/generated/wardite/efflorescence/mir_cache.rbs +263 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +31 -3
- data/sig/generated/wardite/efflorescence/snapshot.rbs +84 -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 +33 -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/io_buffer.rbs +7 -0
- data/sig/mir_cache_stdlib.rbs +5 -0
- metadata +32 -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_functions(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_ir(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,53 @@ 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
|
+
@mir_cache&.restore_all
|
|
174
|
+
linked = IR.new([], entries: @ir.entries, hosts: @ir.hosts)
|
|
175
|
+
build_mir(Translator.new(@instance), linked, false)
|
|
176
|
+
end
|
|
177
|
+
|
|
98
178
|
# @rbs name: String|Symbol
|
|
99
179
|
# @rbs return: Wardite::Efflorescence::FunctionEntry
|
|
100
180
|
def export_entry(name)
|
|
@@ -288,12 +368,42 @@ module Wardite
|
|
|
288
368
|
pools[index] || raise(Wardite::EvalError, "missing MIR frame pool")
|
|
289
369
|
end
|
|
290
370
|
|
|
371
|
+
# Update in place so a suspended execute_flat retains the same array
|
|
372
|
+
# across a host call, including a reentrant call on this VM.
|
|
373
|
+
# @rbs return: void
|
|
374
|
+
def read_globals
|
|
375
|
+
@instance.store.globals.each_with_index do |global, index|
|
|
376
|
+
@global_bits[index] = @store_access.global_bits_at(index, global.type)
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
# @rbs return: void
|
|
381
|
+
def write_globals
|
|
382
|
+
@instance.store.globals.each_with_index do |global, index|
|
|
383
|
+
@store_access.write_global_bits(index, global.type, @global_bits.fetch(index))
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
|
|
291
387
|
# @rbs host_index: Integer
|
|
292
388
|
# @rbs arguments: Array[Integer]
|
|
293
389
|
# @rbs metrics: Wardite::Efflorescence::Metrics
|
|
390
|
+
# @rbs sources: Array[Integer]?
|
|
294
391
|
# @rbs return: Integer?
|
|
392
|
+
def invoke_host_with_globals(host_index, arguments, metrics, sources)
|
|
393
|
+
write_globals
|
|
394
|
+
begin
|
|
395
|
+
invoke_host(host_index, arguments, metrics, sources)
|
|
396
|
+
ensure
|
|
397
|
+
read_globals
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# @rbs host_index: Integer
|
|
402
|
+
# @rbs arguments: Array[Integer]
|
|
403
|
+
# @rbs metrics: Wardite::Efflorescence::Metrics
|
|
295
404
|
# @rbs sources: Array[Integer]?
|
|
296
|
-
|
|
405
|
+
# @rbs return: Integer?
|
|
406
|
+
def invoke_host(host_index, arguments, metrics, sources)
|
|
297
407
|
host = @ir.hosts[host_index] || raise(Wardite::EvalError, "missing MIR host function")
|
|
298
408
|
metrics.host_calls += 1 if @profiling
|
|
299
409
|
if RUBY_ENGINE != "spinel"
|
|
@@ -348,25 +458,55 @@ module Wardite
|
|
|
348
458
|
raise Wardite::EvalError, "host ABI result mismatch for #{host.external.name}"
|
|
349
459
|
end
|
|
350
460
|
|
|
351
|
-
# Each public entry owns its frames, pools and counters
|
|
352
|
-
#
|
|
461
|
+
# Each public or start entry owns its frames, pools and counters. Host
|
|
462
|
+
# calls synchronize globals before a callback can re-enter the VM.
|
|
353
463
|
# @rbs entry: Wardite::Efflorescence::FunctionEntry
|
|
354
464
|
# @rbs arguments: Array[Integer]
|
|
355
465
|
# @rbs return: Integer?
|
|
356
466
|
def execute(entry, arguments)
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
467
|
+
if entry.kind == :host
|
|
468
|
+
read_globals if @has_globals
|
|
469
|
+
metrics = @profiling ? Metrics.new : @metrics
|
|
470
|
+
begin
|
|
471
|
+
return @has_globals ? invoke_host_with_globals(entry.host_index, arguments, metrics, nil) :
|
|
472
|
+
invoke_host(entry.host_index, arguments, metrics, nil)
|
|
473
|
+
ensure
|
|
474
|
+
@metrics = metrics
|
|
475
|
+
write_globals if @has_globals
|
|
476
|
+
end
|
|
477
|
+
end
|
|
361
478
|
|
|
362
|
-
|
|
479
|
+
pools = new_pools
|
|
363
480
|
frame = pool_at(pools, entry.defined_index).acquire(-1)
|
|
364
|
-
active_frames << frame
|
|
365
|
-
metrics.root_defined_entries = 1 if @profiling
|
|
366
481
|
arguments.each_with_index { |value, i| frame.registers[i] = value }
|
|
367
|
-
result =
|
|
482
|
+
result = run_frames(pools, [frame], 1)
|
|
368
483
|
entry.result_types.empty? ? nil : result
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
# @rbs return: Array[Wardite::Efflorescence::FramePool]
|
|
487
|
+
def new_pools
|
|
488
|
+
pools = [] #: Array[Wardite::Efflorescence::FramePool]
|
|
489
|
+
@ir.functions.each { |function| pools << FramePool.new(function, @reuse_frames, @mir_cache) }
|
|
490
|
+
pools
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
# Globals live in @global_bits while frames run. A restored snapshot
|
|
494
|
+
# writes the Store first, so both public calls and resume read it here.
|
|
495
|
+
# @rbs pools: Array[Wardite::Efflorescence::FramePool]
|
|
496
|
+
# @rbs active_frames: Array[Wardite::Efflorescence::Frame]
|
|
497
|
+
# @rbs root_entries: Integer
|
|
498
|
+
# @rbs return: Integer
|
|
499
|
+
def run_frames(pools, active_frames, root_entries)
|
|
500
|
+
read_globals if @has_globals
|
|
501
|
+
metrics = @profiling ? Metrics.new : @metrics
|
|
502
|
+
metrics.root_defined_entries = root_entries if @profiling
|
|
503
|
+
outer = @active_frames
|
|
504
|
+
@active_frames = active_frames
|
|
505
|
+
@depth += 1
|
|
506
|
+
execute_flat(current_frame(active_frames), pools, active_frames, metrics)
|
|
369
507
|
ensure
|
|
508
|
+
@depth -= 1
|
|
509
|
+
@active_frames = outer
|
|
370
510
|
active_frames.each { |active| pool_at(pools, active.pool_index).release(active) }
|
|
371
511
|
if @profiling
|
|
372
512
|
created = 0
|
|
@@ -374,6 +514,7 @@ module Wardite
|
|
|
374
514
|
metrics.frames_created = created
|
|
375
515
|
@metrics = metrics
|
|
376
516
|
end
|
|
517
|
+
write_globals if @has_globals
|
|
377
518
|
end
|
|
378
519
|
|
|
379
520
|
# @rbs frame: Wardite::Efflorescence::Frame
|
|
@@ -384,7 +525,23 @@ module Wardite
|
|
|
384
525
|
# @rbs return: void
|
|
385
526
|
def execute_host_site(frame, site, host_index, destination, metrics)
|
|
386
527
|
registers = frame.registers
|
|
387
|
-
|
|
528
|
+
recorder = @snapshot
|
|
529
|
+
if recorder
|
|
530
|
+
host = @ir.hosts[host_index] || raise(Wardite::EvalError, "missing MIR host function")
|
|
531
|
+
if recorder.checkpoint?(host, registers, site.arguments)
|
|
532
|
+
@snapshot = nil
|
|
533
|
+
if @depth == 1
|
|
534
|
+
# The snapshot reads globals from the Store.
|
|
535
|
+
write_globals if @has_globals
|
|
536
|
+
recorder.save(@instance, @active_frames)
|
|
537
|
+
end
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
result = unless @has_globals
|
|
541
|
+
invoke_host(host_index, registers, metrics, site.arguments)
|
|
542
|
+
else
|
|
543
|
+
invoke_host_with_globals(host_index, registers, metrics, site.arguments)
|
|
544
|
+
end
|
|
388
545
|
registers[destination] = result if destination >= 0 && result
|
|
389
546
|
end
|
|
390
547
|
|
|
@@ -434,8 +591,10 @@ module Wardite
|
|
|
434
591
|
entry = indirect_entry(site, registers[selector])
|
|
435
592
|
if entry.kind == :host
|
|
436
593
|
execute_host_site(frame, site, entry.host_index, destination, metrics)
|
|
594
|
+
frame.pc += Lowerer::WIDTH
|
|
437
595
|
frame
|
|
438
596
|
else
|
|
597
|
+
frame.pc += Lowerer::WIDTH
|
|
439
598
|
next_frame = pool_at(pools, entry.defined_index).acquire(destination)
|
|
440
599
|
frames << next_frame
|
|
441
600
|
target = next_frame.registers
|
|
@@ -477,14 +636,6 @@ module Wardite
|
|
|
477
636
|
when Opcode::F64_STORE
|
|
478
637
|
offset = instructions.fetch(pc + 3)
|
|
479
638
|
@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
639
|
else
|
|
489
640
|
raise Wardite::EvalError, "Efflorescence: invalid MIR opcode #{opcode}"
|
|
490
641
|
end
|
|
@@ -499,8 +650,9 @@ module Wardite
|
|
|
499
650
|
def execute_flat(frame, pools, active_frames, metrics)
|
|
500
651
|
frames = active_frames
|
|
501
652
|
registers = frame.registers
|
|
653
|
+
global_bits = @global_bits
|
|
502
654
|
instructions = frame.function.bytecode
|
|
503
|
-
pc =
|
|
655
|
+
pc = Integer(frame.pc)
|
|
504
656
|
# Avoid Kernel#loop's StopIteration handler in the native hot loop.
|
|
505
657
|
while true
|
|
506
658
|
opcode = instructions.fetch(pc)
|
|
@@ -619,11 +771,12 @@ module Wardite
|
|
|
619
771
|
pc = 0
|
|
620
772
|
next
|
|
621
773
|
when 67 # Opcode::CALL_HOST
|
|
774
|
+
frame.pc = pc
|
|
622
775
|
execute_direct_host(frame, instructions.fetch(pc + 2), a, metrics)
|
|
623
776
|
when 68 # Opcode::CALL_INDIRECT
|
|
624
777
|
b = instructions.fetch(pc + 2)
|
|
625
778
|
c = instructions.fetch(pc + 3)
|
|
626
|
-
frame.pc = pc
|
|
779
|
+
frame.pc = pc
|
|
627
780
|
frame = execute_indirect(frame, pools, frames, a, b, c, metrics)
|
|
628
781
|
registers = frame.registers
|
|
629
782
|
instructions = frame.function.bytecode
|
|
@@ -762,10 +915,10 @@ module Wardite
|
|
|
762
915
|
next
|
|
763
916
|
when 52 # Opcode::GLOBAL_GET
|
|
764
917
|
b = instructions.fetch(pc + 2)
|
|
765
|
-
registers[a] =
|
|
918
|
+
registers[a] = global_bits[b]
|
|
766
919
|
when 53 # Opcode::GLOBAL_SET
|
|
767
920
|
b = instructions.fetch(pc + 2)
|
|
768
|
-
|
|
921
|
+
global_bits[a] = registers[b]
|
|
769
922
|
when 54 # Opcode::I32_LOAD
|
|
770
923
|
b = instructions.fetch(pc + 2)
|
|
771
924
|
c = instructions.fetch(pc + 3)
|
|
@@ -985,10 +1138,22 @@ module Wardite
|
|
|
985
1138
|
@store_access.store(registers[b], c, 4, registers[a])
|
|
986
1139
|
when 115 # Opcode::I64_GLOBAL_GET
|
|
987
1140
|
b = instructions.fetch(pc + 2)
|
|
988
|
-
registers[a] =
|
|
1141
|
+
registers[a] = global_bits[b]
|
|
989
1142
|
when 116 # Opcode::I64_GLOBAL_SET
|
|
990
1143
|
b = instructions.fetch(pc + 2)
|
|
991
|
-
|
|
1144
|
+
global_bits[a] = registers[b]
|
|
1145
|
+
when 191 # Opcode::F32_GLOBAL_GET
|
|
1146
|
+
b = instructions.fetch(pc + 2)
|
|
1147
|
+
registers[a] = global_bits[b]
|
|
1148
|
+
when 192 # Opcode::F64_GLOBAL_GET
|
|
1149
|
+
b = instructions.fetch(pc + 2)
|
|
1150
|
+
registers[a] = global_bits[b]
|
|
1151
|
+
when 193 # Opcode::F32_GLOBAL_SET
|
|
1152
|
+
b = instructions.fetch(pc + 2)
|
|
1153
|
+
global_bits[a] = registers[b]
|
|
1154
|
+
when 194 # Opcode::F64_GLOBAL_SET
|
|
1155
|
+
b = instructions.fetch(pc + 2)
|
|
1156
|
+
global_bits[a] = registers[b]
|
|
992
1157
|
when 22 # Opcode::PROFILE
|
|
993
1158
|
b = instructions.fetch(pc + 2)
|
|
994
1159
|
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,58 @@ 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
|
|
334
|
+
# @rbs mir_cache_eager: bool
|
|
274
335
|
# @rbs return: Instance
|
|
275
|
-
def self.load_from_buffer(buf, import_object: {}, enable_wasi: true, engine: :stack, disabled_optimizations: [], mir_profile: false)
|
|
336
|
+
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, mir_cache_eager: false)
|
|
276
337
|
start = Time.now.to_f #: Float
|
|
338
|
+
unless mir_cache_verify == :checksum || mir_cache_verify == :sha256
|
|
339
|
+
raise ArgumentError, "MIR cache verification must be checksum or sha256"
|
|
340
|
+
end
|
|
277
341
|
@buf = buf
|
|
342
|
+
cache = nil #: Efflorescence::MIRCache?
|
|
343
|
+
if mir_cache
|
|
344
|
+
raise ArgumentError, "MIR cache requires Efflorescence" unless engine == :efflorescence
|
|
345
|
+
unless mir_profile
|
|
346
|
+
require "wardite/efflorescence/mir_cache" unless RUBY_ENGINE == "spinel"
|
|
347
|
+
# Hash the same bytes that the decoder will see, even if the file changes.
|
|
348
|
+
bytes = buf.read || "".b
|
|
349
|
+
configuration = Efflorescence::Configuration.new(disabled_optimizations)
|
|
350
|
+
disabled_optimizations = configuration.disabled
|
|
351
|
+
cache = Efflorescence::MIRCache.new(mir_cache, bytes, configuration, verify: mir_cache_verify, eager: mir_cache_eager)
|
|
352
|
+
@buf = StringIO.new(bytes)
|
|
353
|
+
end
|
|
354
|
+
end
|
|
278
355
|
|
|
279
356
|
version = preamble
|
|
280
|
-
sections_ = sections
|
|
357
|
+
sections_ = sections(!cache.nil?)
|
|
358
|
+
@buf = buf if cache
|
|
281
359
|
|
|
282
360
|
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
|
|
361
|
+
if enable_wasi && sections_.any? { |section| section.is_a?(ImportSection) &&
|
|
362
|
+
section.imports.any? { |import| import.module_name == "wasi_snapshot_preview1" } }
|
|
363
|
+
wasi_env = if RUBY_ENGINE == "spinel"
|
|
364
|
+
require "wardite/wasi"
|
|
365
|
+
Wardite::WasiSnapshotPreview1.new
|
|
366
|
+
elsif engine == :efflorescence
|
|
367
|
+
require "wardite/efflorescence/wasi"
|
|
368
|
+
Wardite::Efflorescence::WasiSnapshotPreview1.new
|
|
369
|
+
else
|
|
370
|
+
require "wardite/wasi"
|
|
371
|
+
Wardite::WasiSnapshotPreview1.new
|
|
296
372
|
end
|
|
373
|
+
import_object[:wasi_snapshot_preview1] = wasi_env
|
|
297
374
|
end
|
|
298
375
|
|
|
299
|
-
return Instance.new(import_object, engine: engine, disabled_optimizations: disabled_optimizations,
|
|
376
|
+
return Instance.new(import_object, engine: engine, disabled_optimizations: disabled_optimizations,
|
|
377
|
+
mir_profile: mir_profile, mir_cache: cache) do |i|
|
|
300
378
|
i.version = version
|
|
301
379
|
i.sections = sections_
|
|
302
380
|
i.wasi = wasi_env
|
|
303
381
|
end
|
|
304
382
|
ensure
|
|
305
|
-
$stderr.puts "load_from_buffer: #{Time.now.to_f - start}" if ENV['
|
|
383
|
+
$stderr.puts "load_from_buffer: #{Time.now.to_f - start}" if ENV['WARDITE_TRACE']
|
|
306
384
|
end
|
|
307
385
|
|
|
308
386
|
# @rbs return: Integer
|
|
@@ -326,8 +404,9 @@ module Wardite
|
|
|
326
404
|
version
|
|
327
405
|
end
|
|
328
406
|
|
|
407
|
+
# @rbs defer_code: bool
|
|
329
408
|
# @rbs return: Array[Section]
|
|
330
|
-
def self.sections
|
|
409
|
+
def self.sections(defer_code = false)
|
|
331
410
|
sections = [] #: Array[Section]
|
|
332
411
|
|
|
333
412
|
loop do
|
|
@@ -359,7 +438,7 @@ module Wardite
|
|
|
359
438
|
when Wardite::SectionElement
|
|
360
439
|
elem_section
|
|
361
440
|
when Wardite::SectionCode
|
|
362
|
-
code_section
|
|
441
|
+
code_section(defer_code)
|
|
363
442
|
when Wardite::SectionData
|
|
364
443
|
data_section
|
|
365
444
|
when Wardite::Const::SectionDataCount
|
|
@@ -630,21 +709,23 @@ module Wardite
|
|
|
630
709
|
dest
|
|
631
710
|
end
|
|
632
711
|
|
|
712
|
+
# @rbs defer_code: bool
|
|
633
713
|
# @rbs return: CodeSection
|
|
634
|
-
def self.code_section
|
|
714
|
+
def self.code_section(defer_code = false)
|
|
635
715
|
dest = CodeSection.new
|
|
636
716
|
size = fetch_uleb128(@buf)
|
|
637
717
|
dest.size = size
|
|
638
718
|
sbuf = StringIO.new(assert_read(@buf, size))
|
|
639
719
|
|
|
640
720
|
len = fetch_uleb128(sbuf)
|
|
641
|
-
len.times { dest.func_codes << read_function_body(sbuf) }
|
|
721
|
+
len.times { dest.func_codes << read_function_body(sbuf, defer_code) }
|
|
642
722
|
dest
|
|
643
723
|
end
|
|
644
724
|
|
|
645
725
|
# @rbs sbuf: StringIO
|
|
726
|
+
# @rbs defer_code: bool
|
|
646
727
|
# @rbs return: CodeSection::CodeBody
|
|
647
|
-
def self.read_function_body(sbuf)
|
|
728
|
+
def self.read_function_body(sbuf, defer_code = false)
|
|
648
729
|
ilen = fetch_uleb128(sbuf)
|
|
649
730
|
code = assert_read(sbuf, ilen)
|
|
650
731
|
last_code = code[-1]
|
|
@@ -662,7 +743,11 @@ module Wardite
|
|
|
662
743
|
body.locals_type << Op.i2type(assert_read(cbuf, 1).ord)
|
|
663
744
|
locals_len -= 1
|
|
664
745
|
end
|
|
665
|
-
|
|
746
|
+
if defer_code
|
|
747
|
+
body.instruction_bytes = (cbuf.read || "".b).freeze
|
|
748
|
+
else
|
|
749
|
+
body.body = code_body(cbuf)
|
|
750
|
+
end
|
|
666
751
|
end
|
|
667
752
|
end
|
|
668
753
|
|
data/lib/wardite/version.rb
CHANGED