forked from go-interpreter/ssainterp
/
interp.go
904 lines (812 loc) · 24.6 KB
/
interp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package interp defines an interpreter for the SSA
// representation of Go programs.
//
// This interpreter is provided as an adjunct for testing the SSA
// construction algorithm. Its purpose is to provide a minimal
// metacircular implementation of the dynamic semantics of each SSA
// instruction. It is not, and will never be, a production-quality Go
// interpreter.
//
// The following is a partial list of Go features that are currently
// unsupported or incomplete in the interpreter.
//
// * Unsafe operations, including all uses of unsafe.Pointer, are
// impossible to support given the "boxed" Ivalue representation we
// have chosen.
//
// * The reflect package is only partially implemented.
//
// * "sync/atomic" operations are not currently atomic due to the
// "boxed" Ivalue representation: it is not possible to read, modify
// and write an interface Ivalue atomically. As a consequence, Mutexes
// are currently broken. TODO(adonovan): provide a metacircular
// implementation of Mutex avoiding the broken atomic primitives.
//
// * recover is only partially implemented. Also, the interpreter
// makes no attempt to distinguish target panics from interpreter
// crashes.
//
// * map iteration is asymptotically inefficient.
//
// * the sizes of the int, uint and uintptr types in the target
// program are assumed to be the same as those of the interpreter
// itself.
//
// * all Ivalues occupy space, even those of types defined by the spec
// to have zero size, e.g. struct{}. This can cause asymptotic
// performance degradation.
//
// * os.Exit is implemented using panic, causing deferred functions to
// run.
package interp
// was: import "golang.org/x/tools/go/ssa/interp"
import (
"fmt"
"go/token"
"io"
"os"
"reflect"
"runtime"
"sync"
"sync/atomic"
"go/types"
"golang.org/x/tools/go/ssa"
)
type continuation int
const (
kNext continuation = iota
kReturn
kJump
)
// Mode is a bitmask of options affecting the interpreter.
type Mode uint
const (
DisableRecover Mode = 1 << iota // Disable recover() in target programs; show interpreter crash instead.
EnableTracing // Print a trace of all instructions as they are interpreted.
)
type methodSet map[string]*ssa.Function
// State shared between all interpreted goroutines.
type interpreter struct {
osArgs []Ivalue // the Ivalue of os.Args
prog *ssa.Program // the SSA program
globals map[ssa.Value]*Ivalue // addresses of global variables (immutable)
mode Mode // interpreter options
reflectPackage *ssa.Package // the fake reflect package
errorMethods methodSet // the method set of reflect.error, which implements the error interface.
rtypeMethods methodSet // the method set of rtype, which implements the reflect.Type interface.
runtimeErrorString types.Type // the runtime.errorString type
sizes types.Sizes // the effective type-sizing function
goroutines int32 // atomically updated
externals *Externals // per-interpreter externals (immutable)
/*
constants map[*ssa.Const]Ivalue // constant cache, set during preprocess()
constantsRWMutex sync.RWMutex
zeroes typeutil.Map // zero value cache, early tests show no benefit, TODO retest
zeroesRWMutex sync.RWMutex
*/
functions map[*ssa.Function]functionInfo
functionsRWMutex sync.RWMutex
panicSource *frame
runtimeFunc map[uintptr]*ssa.Function // required to avoid using the unsafe package
callTargetCache map[string]*ssa.Function // speed up 2nd calls to specific funcs
// CapturedOutput is non-nil, all writes by the interpreted program
// to file descriptors 1 and 2 will also be written to CapturedOutput.
//
// (The $GOROOT/test system requires that the test be considered a
// failure if "BUG" appears in the combined stdout/stderr output, even
// if it exits zero.
//
CapturedOutput io.Writer
capturedOutputMu sync.Mutex
}
type deferred struct {
fn Ivalue
args []Ivalue
instr *ssa.Defer
tail *deferred
}
type frame struct {
i *interpreter
caller *frame
fn *ssa.Function
block, prevBlock *ssa.BasicBlock
iNum int // the latest instruction number in the block
//env map[ssa.Value]Ivalue // dynamic Ivalues of SSA variables
env []Ivalue // dynamic Ivalues of SSA variables
locals []Ivalue
defers *deferred
result Ivalue
panicking bool
panic interface{}
}
func (fr *frame) get(key ssa.Value) Ivalue {
switch key := key.(type) {
case nil:
// Hack; simplifies handling of optional attributes
// such as ssa.Slice.{Low,High}.
return nil
case *ssa.Function, *ssa.Builtin:
return key
case *ssa.Const:
return fr.i.constIvalue(key)
case *ssa.Global:
if r, ok := fr.i.globals[key]; ok {
return r
}
}
return fr.env[envEnt(fr, key)]
/*
if r, ok := fr.env[key]; ok {
return r
}
panic(fmt.Sprintf("get: no Ivalue for %T: %v", key, key.Name()))
*/
}
// runDefer runs a deferred call d.
// It always returns normally, but may set or clear fr.panic.
//
func (fr *frame) runDefer(d *deferred) {
if fr.i.mode&EnableTracing != 0 {
fmt.Fprintf(os.Stderr, "%s: invoking deferred function call\n",
fr.i.prog.Fset.Position(d.instr.Pos()))
}
var ok bool
defer func() {
if !ok {
// Deferred call created a new state of panic.
fr.panicking = true
fr.panic = recover()
if fr.i.panicSource == nil {
fr = fr.i.panicSource
}
}
}()
call(fr.i, fr, d.instr.Pos(), d.fn, d.args)
ok = true
}
// runDefers executes fr's deferred function calls in LIFO order.
//
// On entry, fr.panicking indicates a state of panic; if
// true, fr.panic contains the panic Ivalue.
//
// On completion, if a deferred call started a panic, or if no
// deferred call recovered from a previous state of panic, then
// runDefers itself panics after the last deferred call has run.
//
// If there was no initial state of panic, or it was recovered from,
// runDefers returns normally.
//
func (fr *frame) runDefers() {
for d := fr.defers; d != nil; d = d.tail {
fr.runDefer(d)
}
fr.defers = nil
if fr.panicking {
if fr.i.panicSource == nil {
fr.i.panicSource = fr
}
panic(fr.panic) // new panic, or still panicking
}
}
// lookupMethod returns the method set for type typ, which may be one
// of the interpreter's fake types.
func lookupMethod(i *interpreter, typ types.Type, meth *types.Func) *ssa.Function {
switch typ {
case rtypeType:
return i.rtypeMethods[meth.Id()]
case errorType:
return i.errorMethods[meth.Id()]
}
return i.prog.LookupMethod(typ, meth.Pkg(), meth.Name())
}
// visitInstr interprets a single ssa.Instruction within the activation
// record frame. It returns a continuation Ivalue indicating where to
// read the next instruction from.
func visitInstr(fr *frame, instr ssa.Instruction) continuation {
if debugStats {
debugMapMutex.Lock()
debugMap[fmt.Sprintf("%T", instr)]++
debugMapMutex.Unlock()
}
switch instr := instr.(type) {
case *ssa.DebugRef:
// no-op
case *ssa.UnOp:
fr.env[envEnt(fr, instr)] = fr.i.unop(instr, fr.get(instr.X))
case *ssa.BinOp:
fr.env[envEnt(fr, instr)] = binop(instr.Op, instr.X.Type(), fr.get(instr.X), fr.get(instr.Y))
case *ssa.Call:
fn, args := prepareCall(fr, &instr.Call)
fr.env[envEnt(fr, instr)] = call(fr.i, fr, instr.Pos(), fn, args)
case *ssa.ChangeInterface:
fr.env[envEnt(fr, instr)] = fr.get(instr.X)
case *ssa.ChangeType:
fr.env[envEnt(fr, instr)] = fr.get(instr.X) // (can't fail)
case *ssa.Convert:
fr.env[envEnt(fr, instr)] = fr.i.conv(instr.Type(), instr.X.Type(), fr.get(instr.X))
case *ssa.MakeInterface:
fr.env[envEnt(fr, instr)] = iface{t: instr.X.Type(), v: fr.get(instr.X)}
case *ssa.Extract:
fr.env[envEnt(fr, instr)] = fr.get(instr.Tuple).(tuple)[instr.Index]
case *ssa.Slice:
fr.env[envEnt(fr, instr)] = slice(fr.get(instr.X), fr.get(instr.Low), fr.get(instr.High), fr.get(instr.Max))
case *ssa.Return:
switch len(instr.Results) {
case 0:
case 1:
fr.result = fr.get(instr.Results[0])
default:
var res []Ivalue
for _, r := range instr.Results {
res = append(res, fr.get(r))
}
fr.result = tuple(res)
}
fr.block = nil
return kReturn
case *ssa.RunDefers:
fr.runDefers()
case *ssa.Panic:
if fr.i.panicSource == nil {
fr.i.panicSource = fr
}
panic(targetPanic{fr.get(instr.X)})
case *ssa.Send:
fr.get(instr.Chan).(chan Ivalue) <- fr.get(instr.X)
case *ssa.Store:
store(deref(instr.Addr.Type()), fr.get(instr.Addr).(*Ivalue), fr.get(instr.Val))
case *ssa.If:
succ := 1
if fr.get(instr.Cond).(bool) {
succ = 0
}
fr.prevBlock, fr.block = fr.block, fr.block.Succs[succ]
return kJump
case *ssa.Jump:
fr.prevBlock, fr.block = fr.block, fr.block.Succs[0]
return kJump
case *ssa.Defer:
fn, args := prepareCall(fr, &instr.Call)
fr.defers = &deferred{
fn: fn,
args: args,
instr: instr,
tail: fr.defers,
}
case *ssa.Go:
fn, args := prepareCall(fr, &instr.Call)
atomic.AddInt32(&fr.i.goroutines, 1)
go func() {
defer func() {
fr.i.userStackIfPanic()
}()
call(fr.i, nil, instr.Pos(), fn, args)
atomic.AddInt32(&fr.i.goroutines, -1)
}()
case *ssa.MakeChan:
fr.env[envEnt(fr, instr)] = make(chan Ivalue, asInt(fr.get(instr.Size)))
case *ssa.Alloc:
var addr *Ivalue
if instr.Heap {
// new
addr = new(Ivalue)
fr.env[envEnt(fr, instr)] = addr
} else {
// local
addr = fr.env[envEnt(fr, instr)].(*Ivalue)
}
*addr = fr.i.zero(deref(instr.Type()))
case *ssa.MakeSlice:
slice := make([]Ivalue, asInt(fr.get(instr.Cap)))
tElt := instr.Type().Underlying().(*types.Slice).Elem()
for i := range slice {
slice[i] = fr.i.zero(tElt)
}
fr.env[envEnt(fr, instr)] = slice[:asInt(fr.get(instr.Len))]
case *ssa.MakeMap:
reserve := 0
if instr.Reserve != nil {
reserve = asInt(fr.get(instr.Reserve))
}
fr.env[envEnt(fr, instr)] = makeMap(instr.Type().Underlying().(*types.Map).Key(), reserve)
case *ssa.Range:
fr.env[envEnt(fr, instr)] = rangeIter(fr.get(instr.X), instr.X.Type())
case *ssa.Next:
fr.env[envEnt(fr, instr)] = fr.get(instr.Iter).(iter).next()
case *ssa.FieldAddr:
x := fr.get(instr.X)
// FIXME wrong! &global.f must not change if we do *global = zero!
fr.env[envEnt(fr, instr)] = &(*x.(*Ivalue)).(structure)[instr.Field]
case *ssa.Field:
fr.env[envEnt(fr, instr)] = fr.get(instr.X).(structure)[instr.Field]
case *ssa.IndexAddr:
x := fr.get(instr.X)
idx := fr.get(instr.Index)
switch x := x.(type) {
case []Ivalue:
fr.env[envEnt(fr, instr)] = &x[asInt(idx)]
case *Ivalue: // *array
fr.env[envEnt(fr, instr)] = &(*x).(array)[asInt(idx)]
default:
panic(fmt.Sprintf("unexpected x type in IndexAddr: %T", x))
}
case *ssa.Index:
fr.env[envEnt(fr, instr)] = fr.get(instr.X).(array)[asInt(fr.get(instr.Index))]
case *ssa.Lookup:
fr.env[envEnt(fr, instr)] = fr.i.lookup(instr, fr.get(instr.X), fr.get(instr.Index))
case *ssa.MapUpdate:
m := fr.get(instr.Map)
key := fr.get(instr.Key)
v := fr.get(instr.Value)
switch m := m.(type) {
case map[Ivalue]Ivalue:
m[key] = v
case *hashmap:
m.insert(key.(hashable), v)
default:
panic(fmt.Sprintf("illegal map type: %T", m))
}
case *ssa.TypeAssert:
fr.env[envEnt(fr, instr)] = typeAssert(fr.i, instr, fr.get(instr.X).(iface))
case *ssa.MakeClosure:
var bindings []Ivalue
for _, binding := range instr.Bindings {
bindings = append(bindings, fr.get(binding))
}
fr.env[envEnt(fr, instr)] = &closure{instr.Fn.(*ssa.Function), bindings}
case *ssa.Phi:
for i, pred := range instr.Block().Preds {
if fr.prevBlock == pred {
fr.env[envEnt(fr, instr)] = fr.get(instr.Edges[i])
break
}
}
case *ssa.Select:
var cases []reflect.SelectCase
if !instr.Blocking {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectDefault,
})
}
for _, state := range instr.States {
var dir reflect.SelectDir
if state.Dir == types.RecvOnly {
dir = reflect.SelectRecv
} else {
dir = reflect.SelectSend
}
var send reflect.Value
if state.Send != nil {
send = reflect.ValueOf(fr.get(state.Send))
}
cases = append(cases, reflect.SelectCase{
Dir: dir,
Chan: reflect.ValueOf(fr.get(state.Chan)),
Send: send,
})
}
chosen, recv, recvOk := reflect.Select(cases)
if !instr.Blocking {
chosen-- // default case should have index -1.
}
r := tuple{chosen, recvOk}
for i, st := range instr.States {
if st.Dir == types.RecvOnly {
var v Ivalue
if i == chosen && recvOk {
// No need to copy since send makes an unaliased copy.
v = recv.Interface().(Ivalue)
} else {
v = fr.i.zero(st.Chan.Type().Underlying().(*types.Chan).Elem())
}
r = append(r, v)
}
}
fr.env[envEnt(fr, instr)] = r
default:
panic(fmt.Sprintf("unexpected instruction: %T", instr))
}
// if val, ok := instr.(ssa.Value); ok {
// fmt.Println(toString(fr.env[val])) // debugging
// }
return kNext
}
// prepareCall determines the function Ivalue and argument Ivalues for a
// function call in a Call, Go or Defer instruction, performing
// interface method lookup if needed.
//
func prepareCall(fr *frame, call *ssa.CallCommon) (fn Ivalue, args []Ivalue) {
v := fr.get(call.Value)
if call.Method == nil {
// Function call.
fn = v
} else {
// Interface method invocation.
recv := v.(iface)
if recv.t == nil {
panic("method invoked on nil interface")
}
if f := lookupMethod(fr.i, recv.t, call.Method); f == nil {
// Unreachable in well-typed programs.
panic(fmt.Sprintf("method set for dynamic type %v does not contain %s", recv.t, call.Method))
} else {
fn = f
}
args = append(args, recv.v)
}
for _, arg := range call.Args {
args = append(args, fr.get(arg))
}
return
}
// call interprets a call to a function (function, builtin or closure)
// fn with arguments args, returning its result.
// callpos is the position of the callsite.
//
func call(i *interpreter, caller *frame, callpos token.Pos, fn Ivalue, args []Ivalue) Ivalue {
switch fn := fn.(type) {
case *ssa.Function:
if fn == nil {
panic("call of nil function") // nil of func type
}
return callSSA(i, caller, callpos, fn, args, nil)
case *closure:
return callSSA(i, caller, callpos, fn.Fn, args, fn.Env)
case *ssa.Builtin:
return callBuiltin(caller, callpos, fn, args)
}
panic(fmt.Sprintf("cannot call %T", fn))
}
func loc(fset *token.FileSet, pos token.Pos) string {
if pos == token.NoPos {
return ""
}
return " at " + fset.Position(pos).String()
}
// callSSA interprets a call to function fn with arguments args,
// and lexical environment env, returning its result.
// callpos is the position of the callsite.
//
func callSSA(i *interpreter, caller *frame, callpos token.Pos, fn *ssa.Function, args []Ivalue, env []Ivalue) Ivalue {
if i.mode&EnableTracing != 0 {
fset := fn.Prog.Fset
// TODO(adonovan): fix: loc() lies for external functions.
fmt.Fprintf(os.Stderr, "Entering %s%s.\n", fn, loc(fset, fn.Pos()))
suffix := ""
if caller != nil {
suffix = ", resuming " + caller.fn.String() + loc(fset, callpos)
}
defer fmt.Fprintf(os.Stderr, "Leaving %s%s.\n", fn, suffix)
}
fr := &frame{
i: i,
caller: caller, // for panic/recover
fn: fn,
}
fr.getFnInf() // pre-process the function if required
if fn.Parent() == nil {
name := fn.String()
if ext := i.externals.funcs[name]; ext != nil {
if i.mode&EnableTracing != 0 {
fmt.Fprintln(os.Stderr, "\t(user-defined external)")
}
return ext(fr, args)
}
if ext := externals[name]; ext != nil {
if i.mode&EnableTracing != 0 {
fmt.Fprintln(os.Stderr, "\t(interpreter external)")
}
return ext(fr, args)
}
if fn.Blocks == nil {
fr.i.panicSource = fr
panic("no code for function: " + name)
}
}
fr.env = make([]Ivalue, len(i.functions[fr.fn].envEntries)) // make(map[ssa.Value]Ivalue)
fr.block = fn.Blocks[0]
fr.locals = make([]Ivalue, len(fn.Locals))
for i, l := range fn.Locals { // allocate space for locals (used in alloc)
fr.locals[i] = fr.i.zero(deref(l.Type()))
fr.env[envEnt(fr, l)] = &fr.locals[i]
}
for i, p := range fn.Params {
fr.env[envEnt(fr, p)] = args[i]
}
for i, fv := range fn.FreeVars {
fr.env[envEnt(fr, fv)] = env[i]
}
for fr.block != nil {
runFrame(fr)
}
// Destroy the locals to avoid accidental use after return.
for i := range fn.Locals {
fr.locals[i] = bad{}
}
return fr.result
}
// runFrame executes SSA instructions starting at fr.block and
// continuing until a return, a panic, or a recovered panic.
//
// After a panic, runFrame panics.
//
// After a normal return, fr.result contains the result of the call
// and fr.block is nil.
//
// A recovered panic in a function without named return parameters
// (NRPs) becomes a normal return of the zero Ivalue of the function's
// result type.
//
// After a recovered panic in a function with NRPs, fr.result is
// undefined and fr.block contains the block at which to resume
// control.
//
func runFrame(fr *frame) {
defer func() {
if fr.block == nil {
return // normal return
}
if fr.i.mode&DisableRecover != 0 {
return // let interpreter crash
}
fr.panicking = true
fr.panic = recover()
if fr.i.panicSource == nil {
fr.i.panicSource = fr
}
if fr.i.mode&EnableTracing != 0 {
fmt.Fprintf(os.Stderr, "Panicking: %T %v.\n", fr.panic, fr.panic)
}
fr.runDefers()
fr.block = fr.fn.Recover
}()
fnInf := fr.getFnInf()
fnInstrs := fnInf.instrs
blkInstrs := fnInstrs[fr.block.Index]
trace := fr.i.mode&EnableTracing != 0
var instr ssa.Instruction
for {
if trace {
fmt.Fprintf(os.Stderr, ".%s:\n", fr.block)
}
block:
for fr.iNum, instr = range fr.block.Instrs {
if trace {
if v, ok := instr.(ssa.Value); ok {
fmt.Fprintln(os.Stderr, "\t", v.Name(), "=", instr)
} else {
fmt.Fprintln(os.Stderr, "\t", instr)
}
}
if fun := blkInstrs[fr.iNum]; fun != nil {
fun(fr)
} else {
switch visitInstr(fr, instr) {
case kReturn:
return
case kNext:
// no-op
case kJump:
blkInstrs = fnInstrs[fr.block.Index]
break block
}
}
}
}
}
// doRecover implements the recover() built-in.
func doRecover(caller *frame) Ivalue {
// recover() must be exactly one level beneath the deferred
// function (two levels beneath the panicking function) to
// have any effect. Thus we ignore both "defer recover()" and
// "defer f() -> g() -> recover()".
if caller.i.mode&DisableRecover == 0 &&
caller != nil && !caller.panicking &&
caller.caller != nil && caller.caller.panicking {
caller.caller.panicking = false
caller.i.panicSource = caller.caller // Elliott added
p := caller.caller.panic
caller.caller.panic = nil
switch p := p.(type) {
case targetPanic:
// The target program explicitly called panic().
return p.v
case runtime.Error:
// The interpreter encountered a runtime error.
return iface{caller.i.runtimeErrorString, p.Error()}
case string:
// The interpreter explicitly called panic().
return iface{caller.i.runtimeErrorString, p}
default:
panic(fmt.Sprintf("unexpected panic type %T in target call to recover()", p))
}
}
return iface{}
}
// setGlobal sets the Ivalue of a system-initialized global variable.
func setGlobal(i *interpreter, pkg *ssa.Package, name string, v Ivalue) {
if g, ok := i.globals[pkg.Var(name)]; ok {
*g = v
return
}
panic("no global variable: " + pkg.Pkg.Path() + "." + name)
}
var environ []Ivalue
func init() {
for _, s := range os.Environ() {
environ = append(environ, s)
}
environ = append(environ, "GOSSAINTERP=1")
environ = append(environ, "GOARCH="+runtime.GOARCH)
}
// deleteBodies delete the bodies of all standalone functions except the
// specified ones. A missing intrinsic leads to a clear runtime error.
func deleteBodies(pkg *ssa.Package, except ...string) {
keep := make(map[string]bool)
for _, e := range except {
keep[e] = true
}
for _, mem := range pkg.Members {
if fn, ok := mem.(*ssa.Function); ok && !keep[fn.Name()] {
fn.Blocks = nil
}
}
}
// Context provides the execution context for subsequent calls
type Context struct {
Interp *interpreter
}
// Interpret interprets the Go program whose main package is mainpkg.
// mode specifies various interpreter options. filename and args are
// the initial Ivalues of os.Args for the target program. sizes is the
// effective type-sizing function for this program.
//
// Interpret returns the exit code of the program: 2 for panic (like
// gc does), or the argument to os.Exit for normal termination.
//
// The SSA program must include the "runtime" package.
//
func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename string, args []string, ext *Externals, output io.Writer) (context *Context, exitCode int) {
if ext == nil {
ext = NewExternals()
}
i := &interpreter{
prog: mainpkg.Prog,
globals: make(map[ssa.Value]*Ivalue),
mode: mode,
sizes: sizes,
goroutines: 1,
//constants: make(map[*ssa.Const]Ivalue),
externals: ext,
functions: make(map[*ssa.Function]functionInfo),
runtimeFunc: make(map[uintptr]*ssa.Function),
CapturedOutput: output,
}
i.preprocess()
context = &Context{
Interp: i,
}
runtimePkg := i.prog.ImportedPackage("runtime")
if runtimePkg == nil {
panic("ssa.Program doesn't include runtime package")
}
i.runtimeErrorString = runtimePkg.Type("errorString").Object().Type()
initReflect(i)
i.osArgs = append(i.osArgs, filename)
for _, arg := range args {
i.osArgs = append(i.osArgs, arg)
}
for _, pkg := range i.prog.AllPackages() {
// Initialize global storage.
for _, m := range pkg.Members {
switch v := m.(type) {
case *ssa.Global:
//if gp, ok := i.externals.globVals[v]; ok {
// i.globals[v] = gp.addr
//} else {
cell := i.zero(deref(v.Type()))
i.globals[v] = &cell
//}
}
}
// Ad-hoc initialization for magic system variables.
switch pkg.Pkg.Path() {
case "syscall":
setGlobal(i, pkg, "envs", environ)
case "reflect":
deleteBodies(pkg, "DeepEqual", "deepValueEqual")
case "runtime":
sz := sizes.Sizeof(pkg.Pkg.Scope().Lookup("MemStats").Type())
setGlobal(i, pkg, "sizeof_C_MStats", uintptr(sz))
deleteBodies(pkg, "GOROOT", "gogetenv")
}
}
// Top-level error handler.
exitCode = 2
defer func() {
if exitCode != 2 || i.mode&DisableRecover != 0 {
return
}
rec := recover()
switch p := rec.(type) {
case exitPanic:
exitCode = int(p)
return
case targetPanic:
fmt.Fprintln(os.Stderr, "panic:", toString(p.v))
case runtime.Error:
fmt.Fprintln(os.Stderr, "panic:", p.Error())
case string:
fmt.Fprintln(os.Stderr, "panic:", p)
default:
fmt.Fprintf(os.Stderr, "panic: unexpected type: %T: %v\n", p, p)
}
i.userStackIfPanic()
fmt.Fprintln(os.Stderr, "INTERPRETER STACK:")
buf := make([]byte, 0x10000)
runtime.Stack(buf, false)
fmt.Fprintln(os.Stderr, string(buf))
panic(rec) // to report the panic back
}()
// Run!
call(i, nil, token.NoPos, mainpkg.Func("init"), nil)
if mainFn := mainpkg.Func("main"); mainFn != nil {
call(i, nil, token.NoPos, mainFn, nil)
exitCode = 0
} else {
fmt.Fprintln(os.Stderr, "No main function.")
exitCode = 1
}
return
}
func (i *interpreter) userStackIfPanic() {
if i.panicSource != nil {
fmt.Fprintln(os.Stderr, "USER STACK:")
fr := i.panicSource
for fr != nil {
inst := fr.block.Instrs[fr.iNum].(ssa.Instruction)
where := "-"
for ii := fr.iNum; ii >= 0 && where == "-"; ii-- {
where =
fr.i.prog.Fset.Position(fr.block.Instrs[ii].(ssa.Instruction).Pos()).String()
}
fmt.Fprintln(os.Stderr, fr.fn.String(), where, inst.String())
fr = fr.caller
}
}
}
// Call a function in the interpreter's execution context
func (cont *Context) Call(name string, args []Ivalue) Ivalue {
fr := &frame{i: cont.Interp}
if cont.Interp.callTargetCache == nil {
cont.Interp.callTargetCache = make(map[string]*ssa.Function)
pkgs := cont.Interp.prog.AllPackages()
for _, pkg := range pkgs {
for _, mem := range pkg.Members {
targetFn, isFn := mem.(*ssa.Function)
if isFn {
sname := targetFn.String()
if sname == name {
cont.Interp.callTargetCache[name] = targetFn
}
}
}
}
}
targetFn := cont.Interp.callTargetCache[name]
if targetFn == nil {
panic("No interpreter function: " + name)
}
return call(cont.Interp, fr, token.NoPos, targetFn, args)
}
// deref returns a pointer's element type; otherwise it returns typ.
// TODO(adonovan): Import from ssa?
func deref(typ types.Type) types.Type {
if p, ok := typ.Underlying().(*types.Pointer); ok {
return p.Elem()
}
return typ
}