2013-08-27 16:49:13 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
// +build !windows,!plan9
|
|
|
|
|
|
|
|
package interp_test
|
|
|
|
|
|
|
|
import (
|
2013-10-08 12:35:39 -06:00
|
|
|
"bytes"
|
2013-05-17 14:20:39 -06:00
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-07-18 14:59:06 -06:00
|
|
|
"time"
|
2013-05-17 14:20:39 -06:00
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
"code.google.com/p/go.tools/importer"
|
2013-05-17 14:20:39 -06:00
|
|
|
"code.google.com/p/go.tools/ssa"
|
|
|
|
"code.google.com/p/go.tools/ssa/interp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Each line contains a space-separated list of $GOROOT/test/
|
|
|
|
// filenames comprising the main package of a program.
|
|
|
|
// They are ordered quickest-first, roughly.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): integrate into the $GOROOT/test driver scripts,
|
|
|
|
// golden file checking, etc.
|
2013-08-19 13:00:25 -06:00
|
|
|
var gorootTestTests = []string{
|
2013-05-17 14:20:39 -06:00
|
|
|
"235.go",
|
|
|
|
"alias1.go",
|
|
|
|
"chancap.go",
|
|
|
|
"func5.go",
|
|
|
|
"func6.go",
|
|
|
|
"func7.go",
|
|
|
|
"func8.go",
|
|
|
|
"helloworld.go",
|
|
|
|
"varinit.go",
|
|
|
|
"escape3.go",
|
|
|
|
"initcomma.go",
|
2013-07-16 10:23:55 -06:00
|
|
|
"cmp.go",
|
2013-05-17 14:20:39 -06:00
|
|
|
"compos.go",
|
|
|
|
"turing.go",
|
|
|
|
"indirect.go",
|
|
|
|
"complit.go",
|
|
|
|
"for.go",
|
|
|
|
"struct0.go",
|
|
|
|
"intcvt.go",
|
|
|
|
"printbig.go",
|
|
|
|
"deferprint.go",
|
|
|
|
"escape.go",
|
|
|
|
"range.go",
|
|
|
|
"const4.go",
|
|
|
|
"float_lit.go",
|
|
|
|
"bigalg.go",
|
|
|
|
"decl.go",
|
|
|
|
"if.go",
|
|
|
|
"named.go",
|
|
|
|
"bigmap.go",
|
|
|
|
"func.go",
|
|
|
|
"reorder2.go",
|
|
|
|
"closure.go",
|
|
|
|
"gc.go",
|
|
|
|
"simassign.go",
|
|
|
|
"iota.go",
|
2013-08-19 15:51:33 -06:00
|
|
|
"nilptr2.go",
|
2013-10-08 12:35:39 -06:00
|
|
|
"goprint.go", // doesn't actually assert anything (cmpout)
|
2013-05-17 14:20:39 -06:00
|
|
|
"utf.go",
|
|
|
|
"method.go",
|
|
|
|
"char_lit.go",
|
|
|
|
"env.go",
|
|
|
|
"int_lit.go",
|
|
|
|
"string_lit.go",
|
|
|
|
"defer.go",
|
|
|
|
"typeswitch.go",
|
|
|
|
"stringrange.go",
|
|
|
|
"reorder.go",
|
2013-07-16 10:23:55 -06:00
|
|
|
"method3.go",
|
2013-05-17 14:20:39 -06:00
|
|
|
"literal.go",
|
2013-10-08 12:35:39 -06:00
|
|
|
"nul1.go", // doesn't actually assert anything (errorcheckoutput)
|
2013-05-17 14:20:39 -06:00
|
|
|
"zerodivide.go",
|
|
|
|
"convert.go",
|
|
|
|
"convT2X.go",
|
|
|
|
"initialize.go",
|
|
|
|
"ddd.go",
|
2013-10-08 12:35:39 -06:00
|
|
|
"blank.go", // partly disabled
|
2013-05-17 14:20:39 -06:00
|
|
|
"map.go",
|
|
|
|
"closedchan.go",
|
|
|
|
"divide.go",
|
|
|
|
"rename.go",
|
|
|
|
"const3.go",
|
|
|
|
"nil.go",
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
"recover.go", // reflection parts disabled
|
|
|
|
"recover1.go",
|
|
|
|
"recover2.go",
|
|
|
|
"recover3.go",
|
2013-05-17 14:20:39 -06:00
|
|
|
"typeswitch1.go",
|
|
|
|
"floatcmp.go",
|
2013-10-08 12:35:39 -06:00
|
|
|
"crlf.go", // doesn't actually assert anything (runoutput)
|
2013-07-18 14:59:06 -06:00
|
|
|
// Slow tests follow.
|
|
|
|
"bom.go", // ~1.7s
|
|
|
|
"gc1.go", // ~1.7s
|
|
|
|
"cmplxdivide.go cmplxdivide1.go", // ~2.4s
|
2013-05-17 14:20:39 -06:00
|
|
|
|
|
|
|
// Working, but not worth enabling:
|
2013-07-18 14:59:06 -06:00
|
|
|
// "append.go", // works, but slow (15s).
|
2013-05-17 14:20:39 -06:00
|
|
|
// "gc2.go", // works, but slow, and cheats on the memory check.
|
|
|
|
// "sigchld.go", // works, but only on POSIX.
|
|
|
|
// "peano.go", // works only up to n=9, and slow even then.
|
|
|
|
// "stack.go", // works, but too slow (~30s) by default.
|
|
|
|
// "solitaire.go", // works, but too slow (~30s).
|
|
|
|
// "const.go", // works but for but one bug: constant folder doesn't consider representations.
|
|
|
|
// "init1.go", // too slow (80s) and not that interesting. Cheats on ReadMemStats check too.
|
2013-07-16 10:23:55 -06:00
|
|
|
// "rotate.go rotate0.go", // emits source for a test
|
|
|
|
// "rotate.go rotate1.go", // emits source for a test
|
|
|
|
// "rotate.go rotate2.go", // emits source for a test
|
|
|
|
// "rotate.go rotate3.go", // emits source for a test
|
|
|
|
// "64bit.go", // emits source for a test
|
|
|
|
// "run.go", // test driver, not a test.
|
2013-05-17 14:20:39 -06:00
|
|
|
|
|
|
|
// Typechecker failures:
|
2013-07-16 10:23:55 -06:00
|
|
|
// "switch.go", // https://code.google.com/p/go/issues/detail?id=5505
|
2013-05-17 14:20:39 -06:00
|
|
|
|
|
|
|
// Broken. TODO(adonovan): fix.
|
|
|
|
// copy.go // very slow; but with N=4 quickly crashes, slice index out of range.
|
|
|
|
// nilptr.go // interp: V > uintptr not implemented. Slow test, lots of mem
|
|
|
|
// args.go // works, but requires specific os.Args from the driver.
|
|
|
|
// index.go // a template, not a real test.
|
|
|
|
// mallocfin.go // SetFinalizer not implemented.
|
|
|
|
|
|
|
|
// TODO(adonovan): add tests from $GOROOT/test/* subtrees:
|
|
|
|
// bench chan bugs fixedbugs interface ken.
|
|
|
|
}
|
|
|
|
|
2013-05-28 13:28:46 -06:00
|
|
|
// These are files in go.tools/ssa/interp/testdata/.
|
2013-05-17 14:20:39 -06:00
|
|
|
var testdataTests = []string{
|
2013-05-22 15:56:18 -06:00
|
|
|
"boundmeth.go",
|
2013-07-26 19:49:27 -06:00
|
|
|
"coverage.go",
|
2013-07-01 13:17:36 -06:00
|
|
|
"fieldprom.go",
|
2013-07-26 19:49:27 -06:00
|
|
|
"ifaceconv.go",
|
|
|
|
"ifaceprom.go",
|
2013-07-29 12:24:09 -06:00
|
|
|
"initorder.go",
|
2013-07-26 19:49:27 -06:00
|
|
|
"methprom.go",
|
|
|
|
"mrvchain.go",
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
"recover.go",
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
2013-08-19 13:00:25 -06:00
|
|
|
// These are files in $GOROOT/src/pkg/.
|
|
|
|
// These tests exercise the "testing" package.
|
|
|
|
var gorootSrcPkgTests = []string{
|
|
|
|
"unicode/script_test.go",
|
|
|
|
"unicode/digit_test.go",
|
|
|
|
"hash/crc32/crc32.go hash/crc32/crc32_generic.go hash/crc32/crc32_test.go",
|
|
|
|
"path/path.go path/path_test.go",
|
|
|
|
// TODO(adonovan): figure out the package loading error here:
|
|
|
|
// "strings.go strings/search.go strings/search_test.go",
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
func run(t *testing.T, dir, input string) bool {
|
|
|
|
fmt.Printf("Input: %s\n", input)
|
|
|
|
|
2013-07-18 14:59:06 -06:00
|
|
|
start := time.Now()
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
var inputs []string
|
|
|
|
for _, i := range strings.Split(input, " ") {
|
|
|
|
inputs = append(inputs, dir+i)
|
|
|
|
}
|
|
|
|
|
2013-09-04 11:15:49 -06:00
|
|
|
imp := importer.New(&importer.Config{Build: &build.Default})
|
2013-09-06 16:13:57 -06:00
|
|
|
// TODO(adonovan): use LoadInitialPackages, then un-export ParseFiles.
|
2013-05-31 14:14:13 -06:00
|
|
|
files, err := importer.ParseFiles(imp.Fset, ".", inputs...)
|
2013-05-17 14:20:39 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ssa.ParseFiles(%s) failed: %s", inputs, err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print a helpful hint if we don't make it to the end.
|
|
|
|
var hint string
|
|
|
|
defer func() {
|
|
|
|
if hint != "" {
|
|
|
|
fmt.Println("FAIL")
|
|
|
|
fmt.Println(hint)
|
|
|
|
} else {
|
|
|
|
fmt.Println("PASS")
|
|
|
|
}
|
2013-10-08 12:35:39 -06:00
|
|
|
|
|
|
|
interp.CapturedOutput = nil
|
2013-05-17 14:20:39 -06:00
|
|
|
}()
|
|
|
|
|
2013-09-16 13:22:19 -06:00
|
|
|
hint = fmt.Sprintf("To dump SSA representation, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=CFP %s\n", input)
|
2013-10-10 10:37:49 -06:00
|
|
|
mainInfo := imp.CreatePackage("main", files...)
|
2013-05-17 14:20:39 -06:00
|
|
|
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
if _, err := imp.LoadPackage("runtime"); err != nil {
|
|
|
|
t.Errorf("LoadPackage(runtime) failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-06-03 14:46:57 -06:00
|
|
|
prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions)
|
2013-09-06 16:13:57 -06:00
|
|
|
if err := prog.CreatePackages(imp); err != nil {
|
|
|
|
t.Errorf("CreatePackages failed: %s", err)
|
|
|
|
return false
|
2013-07-18 14:59:06 -06:00
|
|
|
}
|
2013-06-03 14:46:57 -06:00
|
|
|
prog.BuildAll()
|
2013-05-17 14:20:39 -06:00
|
|
|
|
2013-09-06 16:13:57 -06:00
|
|
|
mainPkg := prog.Package(mainInfo.Pkg)
|
2013-08-19 13:00:25 -06:00
|
|
|
mainPkg.CreateTestMainFunction() // (no-op if main already exists)
|
|
|
|
|
2013-10-08 12:35:39 -06:00
|
|
|
var out bytes.Buffer
|
|
|
|
interp.CapturedOutput = &out
|
|
|
|
|
2013-09-16 13:22:19 -06:00
|
|
|
hint = fmt.Sprintf("To trace execution, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=C -run --interp=T %s\n", input)
|
2013-08-19 13:00:25 -06:00
|
|
|
if exitCode := interp.Interpret(mainPkg, 0, inputs[0], []string{}); exitCode != 0 {
|
2013-05-17 14:20:39 -06:00
|
|
|
t.Errorf("interp.Interpret(%s) exited with code %d, want zero", inputs, exitCode)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-10-08 12:35:39 -06:00
|
|
|
// $GOROOT/tests are considered a failure if they print "BUG".
|
|
|
|
if strings.Contains(out.String(), "BUG") {
|
|
|
|
t.Errorf("interp.Interpret(%s) exited zero but output contained 'BUG'", inputs)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
hint = "" // call off the hounds
|
2013-07-18 14:59:06 -06:00
|
|
|
|
|
|
|
if false {
|
|
|
|
fmt.Println(input, time.Since(start)) // test profiling
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const slash = string(os.PathSeparator)
|
|
|
|
|
|
|
|
// TestInterp runs the interpreter on a selection of small Go programs.
|
|
|
|
func TestInterp(t *testing.T) {
|
|
|
|
var failures []string
|
|
|
|
|
|
|
|
for _, input := range testdataTests {
|
|
|
|
if !run(t, "testdata"+slash, input) {
|
|
|
|
failures = append(failures, input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testing.Short() {
|
2013-08-19 13:00:25 -06:00
|
|
|
for _, input := range gorootTestTests {
|
2013-05-17 14:20:39 -06:00
|
|
|
if !run(t, filepath.Join(build.Default.GOROOT, "test")+slash, input) {
|
|
|
|
failures = append(failures, input)
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 13:00:25 -06:00
|
|
|
|
|
|
|
for _, input := range gorootSrcPkgTests {
|
|
|
|
if !run(t, filepath.Join(build.Default.GOROOT, "src/pkg")+slash, input) {
|
|
|
|
failures = append(failures, input)
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if failures != nil {
|
|
|
|
fmt.Println("The following tests failed:")
|
|
|
|
for _, f := range failures {
|
|
|
|
fmt.Printf("\t%s\n", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|