eh-steve / goloader

Compile, load and run Go code at runtime.
Apache License 2.0
54 stars 4 forks source link

any document for load compied binary? #29

Open blue-troy opened 10 months ago

blue-troy commented 10 months ago

I try to use this project with load compied binary like https://github.com/pkujhd/goloader's usage type like:

linker, err := goloader.ReadObjs....
err = goloader.RegSymbol(symPtr....
codeModule, err := goloader.Load...

but faild, can you add any document?

blue-troy commented 10 months ago

this is my demo:

func TestGoLoader(t *testing.T) {
    linker, err := goloader.ReadObjs([]string{"custom_pro.o"}, []string{"custom"}, nil, goloader.WithForceTestRelocationEpilogues())
    if err != nil {
        t.Error(err)
        return
    }
    run := "custom.Run"

    symPtr := make(map[string]uintptr)
    pkgSet := make(map[string]struct{})
    err = goloader.RegSymbol(symPtr, pkgSet)
    if err != nil {
        t.Fatal(err)
    }

    codeModule, err := goloader.Load(linker, symPtr)
    if err != nil {
        t.Fatal(err)
    }
    runFuncPtr, ok := codeModule.Syms[run]
    if !ok || runFuncPtr == 0 {
        t.Fatalf("Load error! not find function: %s", run)
    }
    funcPtrContainer := (uintptr)(unsafe.Pointer(&runFuncPtr))
    runFunc := *(*func(event *beat.Event) (*beat.Event, error))(unsafe.Pointer(&funcPtrContainer))
    event := getEvent()
    res, err := runFunc(event)
    fmt.Println(res)
    os.Stdout.Sync()
    codeModule.Unload()
}

which print:

2024/01/17 16:28:25 Got a TLS symbol [runtime.tls_g] emitted in the main binary (value 0x0 or 0x1024267c0), but not sure what to do with it
=== RUN   TestGoLoader
2024/01/17 16:28:31 Got a TLS symbol [runtime.tls_g] emitted in the main binary (value 0x0 or 0x1024267c0), but not sure what to do with it
--- FAIL: TestGoLoader (27.57s)
panic: runtime error: index out of range [0] with length 0 [recovered]
    panic: runtime error: index out of range [0] with length 0

goroutine 7 [running]:
testing.tRunner.func1.2({0x101995be0, 0x140024a2030})
    /opt/homebrew/opt/go/libexec/src/testing/testing.go:1545 +0x3c0
testing.tRunner.func1()
    /opt/homebrew/opt/go/libexec/src/testing/testing.go:1548 +0x5d0
panic({0x101995be0?, 0x140024a2030?})
    /opt/homebrew/opt/go/libexec/src/runtime/panic.go:920 +0x254
github.com/eh-steve/goloader.(*Linker).buildModule(0x140007fa240, 0x14000350000, 0x14000362270)
    /Users/bluetroy/go/pkg/mod/github.com/eh-steve/goloader@v0.0.0-20240111193324-64f971021f52/ld.go:888 +0xbf4
github.com/eh-steve/goloader.Load(0x140007fa240, 0x140026e9ce8)
    /Users/bluetroy/go/pkg/mod/github.com/eh-steve/goloader@v0.0.0-20240111193324-64f971021f52/ld.go:1279 +0x9fc
github.com/elastic/beats/v7/libbeat/processors/actions.TestGoLoader(0x1400055b380)
    /Users/bluetroy/IdeaProjects/github.com/elastic/beats/libbeat/processors/actions/bench_test.go:210 +0x4bc
testing.tRunner(0x1400055b380, 0x101a25188)
    /opt/homebrew/opt/go/libexec/src/testing/testing.go:1595 +0x1b4
created by testing.(*T).Run in goroutine 1
    /opt/homebrew/opt/go/libexec/src/testing/testing.go:1648 +0x660
eh-steve commented 10 months ago

The intended usage is as per the README. If you want to load precompiled packages, you can either set KeepTempFiles to true, and use the resulting package archives in the same way that the jit package does, or wait for me to get around to implementing https://github.com/eh-steve/goloader/issues/18

In this fork, you should use SymbolsByPkg not Syms and you don't need any unsafe function pointer casts.

If you share some basic reproducing source code for custom_pro.o and your imports for github.com/elastic/beats/libbeat/beat, I can see if I can reproduce on my side