Skip to content

Commit

Permalink
go/ssa/interp: make tests fast and robust
Browse files Browse the repository at this point in the history
The go/ssa/interp tests have been a maintenance nightmare for years
because the interpreter requires intrinsics for all low-level or
non-Go code functions, and the set of such functions in the standard
library naturally changes from day to day.

This CL finally drops support for interpreting real packages (which
has anyway been broken for ages) and restricts the test suite to small
programs that use a handful of simple functions in packages bytes,
strings, errors, runtime, reflect, and unicode. These functions are
declared in a tiny fake standard libary in testdata/src, and the
implementations of these functions are provided by interpreter
intrinsics that delegate to the real Go implementation---all their
parameters and results are basic datatypes.

The test suite is now very fast and should be easy to maintain going
forward. It is still possible that a change to some file in
$GOROOT/test/*.go adds a dependency to a symbol not present in our
standard library, but this is rare. I will either delete the test or
add the intrinsic on a case-by-case basis.

We no longer attempt to interpret major functionality like
fmt.Sprintf or "testing".

The interpreter always pretends to be in linux/amd64 mode.

Happy Christmas, Brad. ;)

Fixes golang/go#27292

Change-Id: I715cf63e3534e2e0dab4666a5d7c669bf1d92674
Reviewed-on: https://go-review.googlesource.com/c/tools/+/168898
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
adonovan committed Mar 22, 2019
1 parent cc8e56e commit e779aa4
Show file tree
Hide file tree
Showing 24 changed files with 304 additions and 986 deletions.
534 changes: 131 additions & 403 deletions go/ssa/interp/external.go

Large diffs are not rendered by default.

35 changes: 0 additions & 35 deletions go/ssa/interp/external_darwin.go

This file was deleted.

256 changes: 0 additions & 256 deletions go/ssa/interp/external_unix.go

This file was deleted.

43 changes: 0 additions & 43 deletions go/ssa/interp/interp.go
Expand Up @@ -630,30 +630,6 @@ func setGlobal(i *interpreter, pkg *ssa.Package, name string, v value) {
panic("no global variable: " + pkg.Pkg.Path() + "." + name)
}

var environ []value

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
}
}
}

// Interpret interprets the Go program whose main package is mainpkg.
// mode specifies various interpreter options. filename and args are
// the initial values of os.Args for the target program. sizes is the
Expand All @@ -665,11 +641,6 @@ func deleteBodies(pkg *ssa.Package, except ...string) {
// The SSA program must include the "runtime" package.
//
func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename string, args []string) (exitCode int) {
if syswrite == nil {
fmt.Fprintln(os.Stderr, "Interpret: unsupported platform.")
return 1
}

i := &interpreter{
prog: mainpkg.Prog,
globals: make(map[ssa.Value]*value),
Expand Down Expand Up @@ -699,20 +670,6 @@ func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename stri
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.
Expand Down

0 comments on commit e779aa4

Please sign in to comment.