Skip to content

Commit

Permalink
use -toolexec to avoid manual preprocessing (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: lance6716 <lance6716@gmail.com>
  • Loading branch information
lance6716 committed Apr 11, 2024
1 parent 2eaa328 commit 411cc9a
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/suite.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: setup go
uses: actions/setup-go@v2
with:
go-version: 1.13
go-version: 1.22
- name: validation
run: make build test
- name: codecov
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Expand Up @@ -7,6 +7,7 @@ LDFLAGS += -X "github.com/pingcap/failpoint/failpoint-ctl/version.gitBranch=$(sh
LDFLAGS += -X "github.com/pingcap/failpoint/failpoint-ctl/version.goVersion=$(shell go version)"

FAILPOINT_CTL_BIN := bin/failpoint-ctl
FAILPOINT_TOOLEXEC_BIN := bin/failpoint-toolexec

path_to_add := $(addsuffix /bin,$(subst :,/bin:,$(GOPATH)))
export PATH := $(path_to_add):$(PATH):$(shell pwd)/tools/bin
Expand All @@ -31,12 +32,17 @@ default: build checksuccess

build:
$(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS)' -o $(FAILPOINT_CTL_BIN) failpoint-ctl/main.go
$(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS)' -o $(FAILPOINT_TOOLEXEC_BIN) failpoint-toolexec/main.go

checksuccess:
@if [ -f $(FAILPOINT_CTL_BIN) ]; \
then \
echo "failpoint-ctl build successfully :-) !" ; \
fi
@if [ -f $(FAILPOINT_TOOLEXEC_BIN) ]; \
then \
echo "failpoint-toolexec build successfully :-) !" ; \
fi

test: gotest check-static

Expand Down
43 changes: 42 additions & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ An implementation of [failpoints][failpoint] for Golang. Fail points are used to

[failpoint]: http://www.freebsd.org/cgi/man.cgi?query=fail

## Quick Start
## Quick Start (use `failpoint-ctl`)

1. Build `failpoint-ctl` from source

Expand Down Expand Up @@ -51,6 +51,47 @@ An implementation of [failpoints][failpoint] for Golang. Fail points are used to
GO_FAILPOINTS="main/testPanic=return(true)" go run your-program.go binding__failpoint_binding__.go
```

## Quick Start (use `failpoint-toolexec`)

1. Build `failpoint-toolexec` from source

``` bash
git clone https://github.com/pingcap/failpoint.git
cd failpoint
make
ls bin/failpoint-toolexec
```

2. Inject failpoints to your program, eg:

``` go
package main

import "github.com/pingcap/failpoint"

func main() {
failpoint.Inject("testPanic", func() {
panic("failpoint triggerd")
})
}
```

3. Use a separate build cache to avoid mixing caches without `failpoint-toolexec`, and build

`GOCACHE=/tmp/failpoint-cache go build -toolexec path/to/failpoint-toolexec`

4. Enable failpoints with `GO_FAILPOINTS` environment variable

``` bash
GO_FAILPOINTS="main/testPanic=return(true)" ./your-program
```

5. You can also use `go run` or `go test`, like:

```bash
GOCACHE=/tmp/failpoint-cache GO_FAILPOINTS="main/testPanic=return(true)" go run -toolexec path/to/failpoint-toolexec your-program.go
```

## Design principles

- Define failpoint in valid Golang code, not comments or anything else
Expand Down
4 changes: 2 additions & 2 deletions code/expr_rewriter.go
Expand Up @@ -66,7 +66,7 @@ func (r *Rewriter) rewriteInject(call *ast.CallExpr) (bool, ast.Stmt, error) {
}

fpnameExtendCall := &ast.CallExpr{
Fun: ast.NewIdent(extendPkgName),
Fun: ast.NewIdent(ExtendPkgName),
Args: []ast.Expr{fpname},
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func (r *Rewriter) rewriteInjectContext(call *ast.CallExpr) (bool, ast.Stmt, err
}

fpnameExtendCall := &ast.CallExpr{
Fun: ast.NewIdent(extendPkgName),
Fun: ast.NewIdent(ExtendPkgName),
Args: []ast.Expr{fpname},
}

Expand Down
4 changes: 3 additions & 1 deletion code/restorer.go
Expand Up @@ -32,6 +32,7 @@ const (

// Restorer represents a manager to restore currentFile tree which has been modified by
// `failpoint-ctl enable`, e.g:
/*
// ├── foo
// │ ├── foo.go
// │   └── foo.go__failpoint_stash__
Expand All @@ -48,6 +49,7 @@ const (
// │   └── bar.go <- bar.go__failpoint_stash__
// └── foobar
//    └── foobar.go <- foobar.go__failpoint_stash__
*/
type Restorer struct {
path string
}
Expand Down Expand Up @@ -154,6 +156,6 @@ func init() {
func %s(name string) string {
return __failpointBindingCache.pkgpath + "/" + name
}
`, pak, extendPkgName)
`, pak, ExtendPkgName)
return ioutil.WriteFile(bindingFile, []byte(bindingContent), 0644)
}
33 changes: 26 additions & 7 deletions code/rewriter.go
Expand Up @@ -32,7 +32,7 @@ const (
packageName = "failpoint"
evalFunction = "Eval"
evalCtxFunction = "EvalContext"
extendPkgName = "_curpkg_"
ExtendPkgName = "_curpkg_"
// It is an indicator to indicate the label is converted from `failpoint.Label("...")`
// We use an illegal suffix to avoid conflict with the user's code
// So `failpoint.Label("label1")` will be converted to `label1-tmp-marker:` in expression
Expand All @@ -44,12 +44,13 @@ const (
// corresponding statements in Golang. It will traverse the specified path and filter
// out files which do not have failpoint injection sites, and rewrite the remain files.
type Rewriter struct {
rewriteDir string
currentPath string
currentFile *ast.File
currsetFset *token.FileSet
failpointName string
rewritten bool
rewriteDir string
currentPath string
currentFile *ast.File
currsetFset *token.FileSet
failpointName string
allowNotChecked bool
rewritten bool

output io.Writer
}
Expand All @@ -66,6 +67,21 @@ func (r *Rewriter) SetOutput(out io.Writer) {
r.output = out
}

// SetAllowNotChecked sets whether the rewriter allows the file which does not import failpoint package.
func (r *Rewriter) SetAllowNotChecked(b bool) {
r.allowNotChecked = b
}

// GetRewritten returns whether the rewriter has rewritten the file in a RewriteFile call.
func (r *Rewriter) GetRewritten() bool {
return r.rewritten
}

// GetCurrentFile returns the current file which is being rewritten
func (r *Rewriter) GetCurrentFile() *ast.File {
return r.currentFile
}

func (r *Rewriter) pos(pos token.Pos) string {
p := r.currsetFset.Position(pos)
return fmt.Sprintf("%s:%d", p.Filename, p.Line)
Expand Down Expand Up @@ -603,6 +619,9 @@ func (r *Rewriter) RewriteFile(path string) (err error) {
}
}
if failpointImport == nil {
if r.allowNotChecked {
return nil
}
panic("import path should be check before rewrite")
}
if failpointImport.Name != nil {
Expand Down
190 changes: 190 additions & 0 deletions failpoint-toolexec/main.go
@@ -0,0 +1,190 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint/code"
"golang.org/x/mod/modfile"
)

var logger = log.New(os.Stderr, "[failpoint-toolexec]", log.LstdFlags)

func main() {
if len(os.Args) < 2 {
return
}
goCmd, buildArgs := os.Args[1], os.Args[2:]
goCmdBase := filepath.Base(goCmd)
if runtime.GOOS == "windows" {
goCmdBase = strings.TrimSuffix(goCmd, ".exe")
}

if strings.ToLower(goCmdBase) == "compile" {
if err := injectFailpoint(&buildArgs); err != nil {
logger.Println("failed to inject failpoint", err)
}
}

cmd := exec.Command(goCmd, buildArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
logger.Println("failed to run command", err)
}
}

func injectFailpoint(argsP *[]string) error {
callersModule, err := findCallersModule()
if err != nil {
return err
}

// ref https://pkg.go.dev/cmd/compile#hdr-Command_Line
var module string
args := *argsP
for i, arg := range args {
if arg == "-p" {
if i+1 < len(args) {
module = args[i+1]
}
break
}
}
if !strings.HasPrefix(module, callersModule) && module != "main" {
return nil
}

fileIndices := make([]int, 0, len(args))
for i, arg := range args {
// find the golang source files of the caller's package
if strings.HasSuffix(arg, ".go") && !inSDKOrMod(arg) {
fileIndices = append(fileIndices, i)
}
}

needExtraFile := false
writer := &code.Rewriter{}
writer.SetAllowNotChecked(true)
for _, idx := range fileIndices {
needExtraFile = needExtraFile || injectFailpointForFile(writer, &args[idx], module)
}
if needExtraFile {
newFile := filepath.Join(tmpFolder, module, "failpoint_toolexec_extra.go")
if err := writeExtraFile(newFile, writer.GetCurrentFile().Name.Name, module); err != nil {
return err
}
*argsP = append(args, newFile)
}
return nil
}

// ref https://github.com/golang/go/blob/bdd27c4debfb51fe42df0c0532c1c747777b7a32/src/cmd/go/internal/modload/init.go#L1511
func findCallersModule() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
dir := filepath.Clean(cwd)

// Look for enclosing go.mod.
for {
goModPath := filepath.Join(dir, "go.mod")
if fi, err := os.Stat(goModPath); err == nil && !fi.IsDir() {
data, err := os.ReadFile(goModPath)
if err != nil {
return "", err
}
f, err := modfile.ParseLax(goModPath, data, nil)
if err != nil {
return "", err
}
return f.Module.Mod.Path, err
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}
return "", errors.New("go.mod file not found")
}

var goModCache = os.Getenv("GOMODCACHE")
var goRoot = runtime.GOROOT()

func inSDKOrMod(path string) bool {
absPath, err := filepath.Abs(path)
if err != nil {
logger.Println("failed to get absolute path", err)
return false
}

if goModCache != "" && strings.HasPrefix(absPath, goModCache) {
return true
}
if strings.HasPrefix(absPath, goRoot) {
return true
}
return false
}

var tmpFolder = filepath.Join(os.TempDir(), "failpoint-toolexec")

func injectFailpointForFile(w *code.Rewriter, file *string, module string) bool {
newFile := filepath.Join(tmpFolder, module, filepath.Base(*file))
newFileDir := filepath.Dir(newFile)
if err := os.MkdirAll(newFileDir, 0700); err != nil {
logger.Println("failed to create temp folder", err)
return false
}
f, err := os.OpenFile(newFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
logger.Println("failed to open temp file", err)
return false
}
defer f.Close()
w.SetOutput(f)

if err := w.RewriteFile(*file); err != nil {
logger.Println("failed to rewrite file", err)
return false
}
if !w.GetRewritten() {
return false
}
*file = newFile
return true
}

func writeExtraFile(filePath, packageName, module string) error {
bindingContent := fmt.Sprintf(`
package %s
func %s(name string) string {
return "%s/" + name
}
`, packageName, code.ExtendPkgName, module)
return os.WriteFile(filePath, []byte(bindingContent), 0644)
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -6,6 +6,7 @@ require (
github.com/sergi/go-diff v1.1.0
github.com/stretchr/testify v1.7.0
go.uber.org/goleak v1.1.10
golang.org/x/mod v0.17.0
)

go 1.13

0 comments on commit 411cc9a

Please sign in to comment.