439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
# File 'lib/wardite.rb', line 439
def eval_insn(frame, insn)
namespace, code, operand, else_pos, end_pos = *insn
case namespace
when :convert
return Evaluator.convert_eval_insn(self, frame, code, operand)
when :i32
return Evaluator.i32_eval_insn(self, frame, code, operand)
when :i64
return Evaluator.i64_eval_insn(self, frame, code, operand)
when :f32
return Evaluator.f32_eval_insn(self, frame, code, operand)
when :f64
return Evaluator.f64_eval_insn(self, frame, code, operand)
end
case code
when :unreachable
raise Unreachable, "unreachable op"
when :nop
return
when :br
level = operand[0]
raise EvalError, "br op without level" if !level.is_a?(Integer)
pc = do_branch(frame.labels, stack, level)
frame.pc = pc
when :br_if
level = operand[0]
raise EvalError, "br op without level" if !level.is_a?(Integer)
cond = stack.pop
raise EvalError, "cond not found" if !cond.is_a?(I32)
if cond.value.zero?
return
end
pc = do_branch(frame.labels, stack, level)
frame.pc = pc
when :br_table
level_vec = operand[0]
raise EvalError, "no level vector" if !level_vec.is_a?(Array)
default = operand[1]
raise EvalError, "no default specified" if !default.is_a?(Integer)
idx = stack.pop
raise EvalError, "idx not found" if !idx.is_a?(I32)
level = if idx.value_s < 0 || idx.value_s >= level_vec.size
default
else
level_vec[idx.value_s]
end
pc = do_branch(frame.labels, stack, level)
frame.pc = pc
when :block
block = operand[0]
raise EvalError, "block op without block" if !block.is_a?(Block)
next_pc = end_pos || -1
label = Label.new(:block, next_pc, stack.size, block.result_size)
frame.labels.push(label)
when :loop
block = operand[0]
raise EvalError, "loop op without block" if !block.is_a?(Block)
start = frame.pc
next_pc = end_pos || -1
label = Label.new(:loop, next_pc, stack.size, block.result_size, start)
frame.labels.push(label)
when :if
block = operand[0]
raise EvalError, "if op without block" if !block.is_a?(Block)
cond = stack.pop
raise EvalError, "cond not found" if !cond.is_a?(I32)
next_pc = end_pos || -1
if cond.value.zero?
frame.pc = else_pos || -1
end
if frame.pc == next_pc
return
end
label = Label.new(:if, next_pc, stack.size, block.result_size)
frame.labels.push(label)
when :else
if old_label = frame.labels.pop
frame.pc = old_label.pc
stack_unwind(old_label.sp, old_label.arity)
else
raise EvalError, "else should be in if block"
end
when :call
idx = operand[0]
raise EvalError, "[BUG] local operand not found" if !idx.is_a?(Integer)
fn = self.instance.store.funcs[idx]
case fn
when WasmFunction
push_frame(fn)
when ExternalFunction
ret = invoke_external(fn)
self.stack.push ret if ret
else
raise GenericError, "got a non-function pointer"
end
when :call_indirect
table = self.instance.store.tables[0]
raise EvalError, "table required but not found" if !table
type_idx = operand[0]
raise EvalError, "[BUG] index operand invalid" if !type_idx.is_a?(Integer)
nullbyte = operand[1]
raise EvalError, "[BUG] invalid bytearray of call_indirect" if nullbyte != 0x0
table_idx = stack.pop
raise EvalError, "[BUG] index stack invalid" if !table_idx.is_a?(I32)
fntype = self.instance.types[type_idx]
if !fntype
raise EvalError, "undefined type index: idx=#{type_idx}"
end
refs = self.instance.store.tables[0]&.refs
if !refs
raise EvalError, "uninitialized element idx:#{table_idx}"
end
fn = refs[table_idx.value]
case fn
when WasmFunction
if table.type != :funcref
raise EvalError, "invalid type of elem; expected: #{table.type}"
end
fn = fn.clone(override_type: fntype)
push_frame(fn)
when ExternalFunction
if table.type != :externref
raise EvalError, "invalid type of elem; expected: #{table.type}"
end
fn = fn.clone(override_type: fntype)
ret = invoke_external(fn)
self.stack.push ret if ret
when nil
raise EvalError, "uninitialized element idx:#{table_idx.value}"
else
raise EvalError, "[BUG] unknwon function type #{fn}"
end
when :return
old_frame = call_stack.pop
if !old_frame
raise EvalError, "maybe empty call stack"
end
stack_unwind(old_frame.sp, old_frame.arity)
when :end
if old_label = frame.labels.pop
frame.pc = old_label.pc
stack_unwind(old_label.sp, old_label.arity)
else
old_frame = call_stack.pop
if !old_frame
raise EvalError, "maybe empty call stack"
end
stack_unwind(old_frame.sp, old_frame.arity)
end
when :drop
stack.pop
when :select
cond, right, left = stack.pop, stack.pop, stack.pop
if !cond.is_a?(I32)
raise EvalError, "invalid stack for select"
end
if !right || !left
raise EvalError, "stack too short"
end
stack.push(cond.value != 0 ? left : right)
when :local_get
idx = operand[0]
if !idx.is_a?(Integer)
raise EvalError, "[BUG] invalid type of operand"
end
local = frame.locals[idx]
if !local
raise EvalError, "local not found, idx = #{idx}"
end
stack.push(local)
when :local_set
idx = operand[0]
if !idx.is_a?(Integer)
raise EvalError, "[BUG] invalid type of operand"
end
value = stack.pop
if !value
raise EvalError, "value should be pushed"
end
frame.locals[idx] = value
when :local_tee
idx = operand[0]
if !idx.is_a?(Integer)
raise EvalError, "[BUG] invalid type of operand"
end
value = stack.pop
if !value
raise EvalError, "value should be pushed"
end
frame.locals[idx] = value
stack.push value
when :global_get
idx = operand[0]
if !idx.is_a?(Integer)
raise EvalError, "[BUG] invalid type of operand"
end
global = instance.store.globals[idx]
if !global
raise EvalError, "global not found"
end
stack.push(global.value)
when :global_set
idx = operand[0]
if !idx.is_a?(Integer)
raise EvalError, "[BUG] invalid type of operand"
end
current_global = instance.store.globals[idx]
if !current_global
raise EvalError, "global index not valid"
end
if !current_global.mutable?
raise EvalError, "global not mutable"
end
value = stack.pop
if !value
raise EvalError, "value should be pushed"
end
current_global.value = value
instance.store.globals[idx] = current_global
when :memory_size
memory = instance.store.memories[0] || raise("[BUG] no memory")
stack.push(I32(memory.current))
when :memory_grow
delta = stack.pop
if !delta.is_a?(I32)
raise EvalError, "maybe stack too short"
end
memory = instance.store.memories[0] || raise("[BUG] no memory")
stack.push(I32(memory.grow(delta.value)))
when :memory_init
idx = operand[0]
if !idx.is_a?(Integer)
raise EvalError, "[BUG] invalid type of operand"
end
if operand[1] != 0x0
$stderr.puts "warning: :memory_init is not ending with 0x00"
end
data_sec = instance.data_section
if !data_sec
raise EvalError, "data segment out of range"
end
data_seg = data_sec.segments[idx]
if !data_seg
raise EvalError, "data segment out of range"
end
memory = instance.store.memories[0] || raise("[BUG] no memory")
length, src_offset, dest_offset = stack.pop, stack.pop, stack.pop
if !length.is_a?(I32) || !src_offset.is_a?(I32) || !dest_offset.is_a?(I32)
raise EvalError, "invalid stack values"
end
source = data_seg.data[src_offset.value...(src_offset.value+length.value)]
raise EvalError, "invalid source range" if !source
memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
when :memory_copy
if operand[0] != 0x0 || operand[1] != 0x0
$stderr.puts "warning: :memory_copy is not ending with 0x00"
end
length, src_offset, dest_offset = stack.pop, stack.pop, stack.pop
if !length.is_a?(I32) || !src_offset.is_a?(I32) || !dest_offset.is_a?(I32)
raise EvalError, "invalid stack values"
end
memory = instance.store.memories[0] || raise("[BUG] no memory")
source = memory.data[src_offset.value...(src_offset.value+length.value)]
raise EvalError, "invalid source range" if !source
memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
when :memory_fill
if operand[0] != 0x0
$stderr.puts "warning: :memory_fill is not ending with 0x00"
end
length, byte, dest_offset = stack.pop, stack.pop, stack.pop
if !length.is_a?(I32) || !byte.is_a?(I32) || !dest_offset.is_a?(I32)
raise EvalError, "invalid stack values"
end
memory = instance.store.memories[0] || raise("[BUG] no memory")
source = byte.value.chr * length.value
memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
else
raise "TODO! unsupported #{insn.inspect}"
end
rescue => e
if ENV["DEBUG"]
require "pp"
$stderr.puts "instance:::\n#{self.instance.pretty_inspect}"
$stderr.puts "frame:::\n#{frame.pretty_inspect}"
$stderr.puts "stack:::\n#{stack.pretty_inspect}"
end
raise e
end
|