Skip to content

Commit

Permalink
fix: otel_setup_inst.go executes after RawFunc causing HookFunc to be…
Browse files Browse the repository at this point in the history
… nil resulting in panic (#22)
  • Loading branch information
y1yang0 committed Aug 9, 2024
1 parent 67e3528 commit 43bed91
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 13 deletions.
49 changes: 49 additions & 0 deletions test/errors-test/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 h1:j9+03ymgYhPKmeXGk5Zu+cIZOlVzd9Zv7QIiyItjFBU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0/go.mod h1:Y5+XiUG4Emn1hTfciPzGPJaSI+RpDts6BnCIir0SLqk=
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 18 additions & 8 deletions tool/instrument/trampoline.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,15 @@ func (rp *RuleProcessor) callOnEnterHook(t *api.InstFuncRule, traits []ParamTrai
}
}
}
// Generate onEnter call
onEnterCall := shared.CallTo(makeOnXName(t, true), args)
insertAtBody(rp.onEnterHookFunc, shared.ExprStmt(onEnterCall),
len(rp.onEnterHookFunc.Body.List)-1 /*before return*/)
// Call onEnter if it exists
fnName := makeOnXName(t, true)
call := shared.ExprStmt(shared.CallTo(fnName, args))
iff := shared.IfNotNilStmt(
dst.NewIdent(fnName),
shared.Block(call),
nil,
)
insertAtBody(rp.onEnterHookFunc, iff, len(rp.onEnterHookFunc.Body.List)-1)
return nil
}

Expand All @@ -219,10 +224,15 @@ func (rp *RuleProcessor) callOnExitHook(t *api.InstFuncRule, traits []ParamTrait
}
}
}
// Generate onExit call
onExitCall := shared.CallTo(makeOnXName(t, false), args)
insertAtBody(rp.onExitHookFunc, shared.ExprStmt(onExitCall),
len(rp.onExitHookFunc.Body.List))
// Call onExit if it exists
fnName := makeOnXName(t, false)
call := shared.ExprStmt(shared.CallTo(fnName, args))
iff := shared.IfNotNilStmt(
dst.NewIdent(fnName),
shared.Block(call),
nil,
)
insertAtBody(rp.onExitHookFunc, iff, len(rp.onExitHookFunc.Body.List))
return nil
}

Expand Down
35 changes: 30 additions & 5 deletions tool/shared/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/dave/dst/decorator"
)

const (
IdentNil = "nil"
IdentTrue = "true"
IdentFalse = "false"
IdentIgnore = "_"
)

// AST Construction
func AddressOf(expr dst.Expr) *dst.UnaryExpr {
return &dst.UnaryExpr{Op: token.AND, X: dst.Clone(expr).(dst.Expr)}
Expand All @@ -25,12 +32,12 @@ func CallTo(name string, args []dst.Expr) *dst.CallExpr {
}

func MakeUnusedIdent(ident *dst.Ident) *dst.Ident {
ident.Name = "_"
ident.Name = IdentIgnore
return ident
}

func IsUnusedIdent(ident *dst.Ident) bool {
return ident.Name == "_"
return ident.Name == IdentIgnore
}

func Ident(name string) *dst.Ident {
Expand Down Expand Up @@ -111,11 +118,11 @@ func NewField(name string, typ dst.Expr) *dst.Field {
}

func BoolTrue() *dst.BasicLit {
return &dst.BasicLit{Value: "true"}
return &dst.BasicLit{Value: IdentTrue}
}

func BoolFalse() *dst.BasicLit {
return &dst.BasicLit{Value: "false"}
return &dst.BasicLit{Value: IdentFalse}
}

func IsInterfaceType(typ dst.Expr) bool {
Expand Down Expand Up @@ -145,6 +152,24 @@ func IfStmt(init dst.Stmt, cond dst.Expr, body, elseBody *dst.BlockStmt) *dst.If
}
}

func IfNotNilStmt(cond dst.Expr, body, elseBody *dst.BlockStmt) *dst.IfStmt {
var elseB dst.Stmt
if elseBody == nil {
elseB = nil
} else {
elseB = dst.Clone(elseBody).(dst.Stmt)
}
return &dst.IfStmt{
Cond: &dst.BinaryExpr{
X: dst.Clone(cond).(dst.Expr),
Op: token.NEQ,
Y: &dst.Ident{Name: IdentNil},
},
Body: dst.Clone(body).(*dst.BlockStmt),
Else: elseB,
}
}

func EmptyStmt() *dst.EmptyStmt {
return &dst.EmptyStmt{}
}
Expand Down Expand Up @@ -199,7 +224,7 @@ func AddImportForcely(root *dst.File, path string) {
Tok: token.IMPORT,
Specs: []dst.Spec{
&dst.ImportSpec{
Name: &dst.Ident{Name: "_"},
Name: &dst.Ident{Name: IdentIgnore},
Path: &dst.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf("\"%s\"", path),
Expand Down

0 comments on commit 43bed91

Please sign in to comment.