Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
Signed-off-by: lance6716 <[email protected]>
  • Loading branch information
lance6716 committed Apr 11, 2024
1 parent 2eaa328 commit 842695b
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 14 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions code/expr_rewriter.go
Original file line number Diff line number Diff line change
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
11 changes: 7 additions & 4 deletions code/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ const (
// │ ├── bar.go
// │   └── bar.go__failpoint_stash__
// └── foobar
// ├── foobar.go
//    └── foobar.go__failpoint_stash__
//
// ├── foobar.go
//    └── foobar.go__failpoint_stash__
//
// Which will be restored as below:
// ├── foo
// │   └── foo.go <- foo.go__failpoint_stash__
// ├── bar
// │   └── bar.go <- bar.go__failpoint_stash__
// └── foobar
//    └── foobar.go <- foobar.go__failpoint_stash__
//
//    └── foobar.go <- foobar.go__failpoint_stash__
type Restorer struct {
path string
}
Expand Down Expand Up @@ -154,6 +157,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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) {
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)

Check failure on line 115 in failpoint-toolexec/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: os.ReadFile
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)

Check failure on line 189 in failpoint-toolexec/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: os.WriteFile
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
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
42 changes: 41 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,59 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11 h1:Yq9t9jnGoR+dBuitxdo9l6Q7xh/zOyNnYUtDKaQ3x0E=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 842695b

Please sign in to comment.