@@ -13,9 +13,9 @@ package luajit // import "go.opentelemetry.io/ebpf-profiler/interpreter/luajit"
1313
1414import (
1515 "errors"
16- "slices"
1716
1817 "go.opentelemetry.io/ebpf-profiler/libpf/pfelf"
18+ xh "go.opentelemetry.io/ebpf-profiler/x86helpers"
1919 "golang.org/x/arch/x86/x86asm"
2020)
2121
@@ -50,7 +50,7 @@ which is a dynamic public symbol that should be in all binaries of LuaJIT includ
5050*/
5151//nolint:nonamedreturns
5252func (x * x86Extractor ) findOffsetsFromLuaClose (b []byte ) (glref , curL uint64 , err error ) {
53- b , _ = skipEndBranch (b )
53+ b , _ = xh . SkipEndBranch (b )
5454 var greg x86asm.Reg
5555 for len (b ) > 0 {
5656 var i x86asm.Inst
@@ -101,7 +101,7 @@ func (x *x86Extractor) findOffsetsFromLuaClose(b []byte) (glref, curL uint64, er
101101// 0xfa8 is the g to dispatch offset.
102102// https://github.com/openresty/luajit2/blob/7952882d/src/lj_dispatch.c#L122
103103func (x * x86Extractor ) findG2DispatchOffsetFromLjDispatchUpdate (b []byte ) (uint64 , error ) {
104- b , _ = skipEndBranch (b )
104+ b , _ = xh . SkipEndBranch (b )
105105 var greg x86asm.Reg
106106 for len (b ) > 0 {
107107 i , err := x86asm .Decode (b , 64 )
@@ -159,7 +159,7 @@ func (x *x86Extractor) findG2DispatchOffsetFromLjDispatchUpdate(b []byte) (uint6
159159//
160160//nolint:lll
161161func (x * x86Extractor ) findLjDispatchUpdateAddr (b []byte , addr uint64 ) (uint64 , error ) {
162- b , ip := skipEndBranch (b )
162+ b , ip := xh . SkipEndBranch (b )
163163 var Lreg x86asm.Reg
164164 rdiHasG := false
165165 for len (b ) > 0 {
@@ -212,7 +212,7 @@ func (x *x86Extractor) findLjDispatchUpdateAddr(b []byte, addr uint64) (uint64,
212212// ----------- 0x430 is the G to J->traces offset
213213// libluajit-5.1.so[0x637a1] <+33>: movq 0x430(%rdx), %rdx
214214func (x * x86Extractor ) findG2TracesOffsetFromChecktrace (b []byte ) (uint64 , error ) {
215- b , _ = skipEndBranch (b )
215+ b , _ = xh . SkipEndBranch (b )
216216 var Greg x86asm.Reg
217217 for len (b ) > 0 {
218218 i , err := x86asm .Decode (b , 64 )
@@ -236,7 +236,7 @@ func (x *x86Extractor) findG2TracesOffsetFromChecktrace(b []byte) (uint64, error
236236}
237237
238238func (x * x86Extractor ) findFirstCall (b []byte , baseAddr int64 ) (uint64 , error ) {
239- b , ip := skipEndBranch (b )
239+ b , ip := xh . SkipEndBranch (b )
240240 for len (b ) > 0 {
241241 i , err := x86asm .Decode (b , 64 )
242242 if err != nil {
@@ -258,7 +258,7 @@ func (x *x86Extractor) findFirstCall(b []byte, baseAddr int64) (uint64, error) {
258258
259259// Return true if the code in b calls targetCall.
260260func (x * x86Extractor ) callExists (b []byte , baseAddr , targetCall int64 ) (bool , error ) {
261- b , ip := skipEndBranch (b )
261+ b , ip := xh . SkipEndBranch (b )
262262 for len (b ) > 0 {
263263 i , err := x86asm .Decode (b , 64 )
264264 if err != nil {
@@ -289,7 +289,7 @@ func (x *x86Extractor) callExists(b []byte, baseAddr, targetCall int64) (bool, e
289289func findRipRelativeLea2ndArgTo2ndCall (b []byte , baseAddr , targetCall int64 ) (uint64 , error ) {
290290 var leaRsi int64
291291 calls := 2
292- b , ip := skipEndBranch (b )
292+ b , ip := xh . SkipEndBranch (b )
293293 for len (b ) > 0 {
294294 i , err := x86asm .Decode (b , 64 )
295295 if err != nil {
@@ -364,7 +364,7 @@ func skipCallsAABA(b []byte, ip, baseAddr int64) ([]byte, int64, error) {
364364func (x * x86Extractor ) find3rdArgToLibPreregCall (b []byte , baseAddr int64 ) (uint64 , error ) {
365365 var rdxAddr int64
366366 calls := 3
367- b , ip := skipEndBranch (b )
367+ b , ip := xh . SkipEndBranch (b )
368368 // Skip the lua_push* call sequence (and all the preceding calls which varies depending on
369369 // inlining).
370370 // libluajit-5.1.so[0x700a5] <+133>: movq %rbx, %rdi
@@ -432,7 +432,7 @@ func (x *x86Extractor) find3rdArgToLibPreregCall(b []byte, baseAddr int64) (uint
432432// bbc2: c3 ret
433433func (x * x86Extractor ) find4thArgToLibRegCall (b []byte , baseAddr int64 ) (int64 , error ) {
434434 var ip int64
435- b , ip = skipEndBranch (b )
435+ b , ip = xh . SkipEndBranch (b )
436436 for len (b ) > 0 {
437437 i , err := x86asm .Decode (b , 64 )
438438 if err != nil {
@@ -468,24 +468,6 @@ func calcRipRelativeAddr(a1 x86asm.Mem, baseAddr, ip int64) int64 {
468468 return baseAddr + ip + int64 (disp )
469469}
470470
471- var endbr64 = [4 ]byte {0xf3 , 0x0f , 0x1e , 0xfa }
472-
473- // On some binaries the function starts like this:
474- //
475- // 0x0000000000012860 <+0>: f3 0f 1e fa endbr64
476- // 0x0000000000012864 <+4>: 41 55 push %r13
477- //
478- // This is some kind of stack smashing indirect jump protection, treat it as a nop,
479- // x86asm doesn't know how to handle it.
480- //
481- //nolint:gocritic
482- func skipEndBranch (b []byte ) ([]byte , int64 ) {
483- if slices .Equal (b [0 :4 ], endbr64 [:]) {
484- return b [4 :], 4
485- }
486- return b , 0
487- }
488-
489471// If we're dealing with 32bit values compilers will use R or E prefix
490472// interchangeably (E refs are just zero padded).
491473func sameReg (r1 , r2 x86asm.Reg ) bool {
0 commit comments