pkujhd / goloader

load and run golang code at runtime.
Apache License 2.0
497 stars 58 forks source link

build constraints exclude all Go files in ... #90

Closed uberswe closed 11 months ago

uberswe commented 11 months ago

Hello, I am currently working on updating my benchmarking repository which compares goloader to several other "plugin" implementations using Go. I am trying to use docker to standardise my benchmarks and improve my testing as I update to 1.21. However, I am having issues getting goloader running in my latest branch seen here: https://github.com/uberswe/go-plugin-benchmark/tree/docker%2Bautomation

What I do is I run the following commands:

cp -r /usr/local/go/src/cmd/internal /usr/local/go/src/cmd/objfile
go list -export -f '{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}' std `go list -f {{.Imports}} ./goloader/main.go | awk '{sub(/^\[/, ""); print }' | awk '{sub(/\]$/, ""); print }'` > importcfg
go tool compile -importcfg importcfg -o ./goloader.o ./goloader/main.go

My goloader/main.go file looks like this:

package main

import (
    "math/rand"
)

// RandInt uses math/rand to return a random integer
func RandInt() int {
    return rand.Int()
}

And then when I run my benchmark I get the following error:

go: downloading github.com/pkujhd/goloader v0.0.0-20220509034319-898ebfc025e3
# github.com/uberswe/go-plugin-benchmark
package github.com/uberswe/go-plugin-benchmark (test)
        imports github.com/pkujhd/goloader
        imports github.com/pkujhd/goloader/objabi/dataindex: build constraints exclude all Go files in /go/pkg/mod/github.com/pkujhd/goloader@v0.0.0-20220509034319-898ebfc025e3/objabi/dataindex
FAIL    github.com/uberswe/go-plugin-benchmark [setup failed]

This is the relevant benchmarking code

func BenchmarkGoloaderRandInt(b *testing.B) {
    linker, err := goloader.ReadObjs([]string{"goloader.o"}, []string{"importcfg"})
    if err != nil {
        b.Error(err)
        return
    }

    run := "main.RandInt"

    symPtr := make(map[string]uintptr)
    err = goloader.RegSymbol(symPtr)
    if err != nil {
        b.Error(err)
        return
    }

    codeModule, err := goloader.Load(linker, symPtr)
    if err != nil {
        fmt.Println("Load error:", err)
        return
    }
    runFuncPtr := codeModule.Syms[run]
    if runFuncPtr == 0 {
        fmt.Println("Load error! not find function:", run)
        return
    }
    funcPtrContainer := (uintptr)(unsafe.Pointer(&runFuncPtr))
    runFunc := *(*func() int)(unsafe.Pointer(&funcPtrContainer))

    b.Run("goloader", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            _ = runFunc()
        }
    })

    os.Stdout.Sync()
    codeModule.Unload()
}

Any ideas on what I might be doing wrong?

pkujhd commented 11 months ago

you download goloader version is 20220509, you need current master version which it is support golang 1.21

uberswe commented 11 months ago

Thank you, sometimes the most simple mistakes get overlooked