GO Plugin 编译问题

2019-08-17  本文已影响0人  唐西铭

GO Plugin 编译问题

初始问题

现在用go mod和docker multi-stage生成的plugin在workflow中加载的时候,会遇到plugin与workflow用到的共用package(如:github.com/pkg/errors)版本不一致导致plugin加载失败。


image.png

问题追踪

运行时

  1. plugin.open(): https://golang.org/src/plugin/plugin_dlopen.go
func open(name string) (*Plugin, error) {
  // ...
    // 调用运行时方法
    pluginpath, syms, errstr := lastmoduleinit()
  if errstr != "" {
    plugins[filepath] = &Plugin{
      pluginpath: pluginpath,
      err:        errstr,
    }
    pluginsMu.Unlock()
    return nil, errors.New(`plugin.Open("` + name + `"): ` + errstr)
  }
    // ...
}

// lastmoduleinit is defined in package runtime
func lastmoduleinit() (pluginpath string, syms map[string]interface{}, errstr string)
  1. lastmoduleinit(): https://golang.org/src/runtime/plugin.go
//go:linkname plugin_lastmoduleinit plugin.lastmoduleinit
func plugin_lastmoduleinit() (path string, syms map[string]interface{}, errstr string) {
  // ...
    for _, pkghash := range md.pkghashes {
        // 对比pkg链接与运行时的hash是否一致
    if pkghash.linktimehash != *pkghash.runtimehash {
      md.bad = true
      return "", nil, "plugin was built with a different version of package " + pkghash.modulename
    }
  }
    // ...
}
  1. modulehash: https://golang.org/src/runtime/symtab.go
    a. cmd/internal/ld/symtab.go:symtab
// moduledata records information about the layout of the executable
// image. It is written by the linker. Any changes here must be
// matched changes to the code in cmd/internal/ld/symtab.go:symtab.
// moduledata is stored in statically allocated non-pointer memory;
// none of the pointers here are visible to the garbage collector.
type moduledata struct {
    // ...
    pkghashes  []modulehash
    // ...
}

// A modulehash is used to compare the ABI of a new module or a
// package in a new module with the loaded program.
//
// For each shared library a module links against, the linker creates an entry in the
// moduledata.modulehashes slice containing the name of the module, the abi hash seen
// at link time and a pointer to the runtime abi hash. These are checked in
// moduledataverify1 below.
//
// For each loaded plugin, the pkghashes slice has a modulehash of the
// newly loaded package that can be used to check the plugin's version of
// a package against any previously loaded version of the package.
// This is done in plugin.lastmoduleinit.
type modulehash struct {
    modulename   string
    linktimehash string
    runtimehash  *string
}

编译时

  1. symtab(): https://golang.org/src/cmd/link/internal/ld/symtab.go
func (ctxt *Link) symtab() {
    // ...
    // Information about the layout of the executable image for the
    // runtime to use. Any changes here must be matched by changes to
    // the definition of moduledata in runtime/symtab.go.
    // This code uses several global variables that are set by pcln.go:pclntab.
    moduledata := ctxt.Moduledata
    // ...
    if ctxt.BuildMode == BuildModePlugin {
        // ...
        for i, l := range ctxt.Library {
            // pkghashes[i].name
            addgostring(ctxt, pkghashes, fmt.Sprintf("go.link.pkgname.%d", i), l.Pkg)
            // pkghashes[i].linktimehash
            addgostring(ctxt, pkghashes, fmt.Sprintf("go.link.pkglinkhash.%d", i), l.Hash)
            // pkghashes[i].runtimehash
            hash := ctxt.Syms.ROLookup("go.link.pkghash."+l.Pkg, 0)
            pkghashes.AddAddr(ctxt.Arch, hash)
        }
        // ...
    }
}
  1. addlibpath(): https://golang.org/src/cmd/link/internal/ld/ld.go
    a. l.pkg: package import path, e.g. container/vector
/*
 * add library to library list, return added library.
 *  srcref: src file referring to package
 *  objref: object file referring to package
 *  file: object file, e.g., /home/rsc/go/pkg/container/vector.a
 *  pkg: package import path, e.g. container/vector
 *  shlib: path to shared library, or .shlibname file holding path
 */
func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlib string) *sym.Library {
    if l := ctxt.LibraryByPkg[pkg]; l != nil {
        return l
    }

    if ctxt.Debugvlog > 1 {
        ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlib: %s\n", Cputime(), srcref, objref, file, pkg, shlib)
    }

    l := &sym.Library{}
    ctxt.LibraryByPkg[pkg] = l
    ctxt.Library = append(ctxt.Library, l)
    l.Objref = objref
    l.Srcref = srcref
    l.File = file
    l.Pkg = pkg
    // ...
    return l
}
  1. loadlib():https://golang.org/src/cmd/link/internal/ld/lib.go
    a. l.hash: toolchain version and any GOEXPERIMENT flags
func (ctxt *Link) loadlib() {
    // ctxt.Library grows during the loop, so not a range loop.
    for i := 0; i < len(ctxt.Library); i++ {
        lib := ctxt.Library[i]
        if lib.Shlib == "" {
            if ctxt.Debugvlog > 1 {
                ctxt.Logf("%5.2f autolib: %s (from %s)\n", Cputime(), lib.File, lib.Objref)
            }
            loadobjfile(ctxt, lib)
        }
    }
    
    // ...
    
    // If package versioning is required, generate a hash of the
    // packages used in the link.
    if ctxt.BuildMode == BuildModeShared || ctxt.BuildMode == BuildModePlugin || ctxt.CanUsePlugins() {
        for _, lib := range ctxt.Library {
            if lib.Shlib == "" {
                genhash(ctxt, lib)
            }
        }
    }
}

func genhash(ctxt *Link, lib *sym.Library) {
    // ...
    h := sha1.New()

    // To compute the hash of a package, we hash the first line of
    // __.PKGDEF (which contains the toolchain version and any
    // GOEXPERIMENT flags) and the export data (which is between
    // the first two occurrences of "\n$$").
    lib.Hash = hex.EncodeToString(h.Sum(nil))
}
  1. main(): https://golang.org/src/cmd/link/internal/ld/main.go
// Main is the main entry point for the linker code.
func Main(arch *sys.Arch, theArch Arch) {
    // ...
    switch ctxt.BuildMode {
    case BuildModePlugin:
        addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "")
    default:
        addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "")
    }
    ctxt.loadlib()
}

结论

main程序与plugin对【同一个第三方包】(即:import包名相同)的依赖,需要保证如下两点,才能让main程序成功加载plugin:

编译问题解决思路

Main程序与Plugin在相同环境编译

由于Main程序代码只能在我们这边,而plugin的代码在用户侧,若想要编译环境一致:

Main程序自定义import path

既然用户侧的plugin我们无法控制,可以尝试控制Main程序对于第三方包的依赖:

自定义import路径有两种方式:

  1. 搭建Go-Get Proxy,参考:https://www.jianshu.com/p/449345975453
    • 只能更改直接依赖的第三方包的import path
  2. 本地Fork第三方依赖
  3. 通过go.mod的replace功能也能实现

修改GO源码编译

即将 https://golang.org/src/runtime/plugin.go中的检查注释后重新编译GO,可能引入新的问题,暂不采用。

编译可行性验证

Main程序与Plugin在相同环境下编译

步骤:

Main程序自定义import path

Main程序引用自定义第三方包,验证是否依然报错

步骤:

Main程序引用自定义第三方包,验证是否确定被认为不同的包

步骤:

Main程序间接引用,Plugin直接引用,验证是否会存在问题

步骤:

编译最终解决方案

Main程序与Plugin在相同环境下编译

Main程序自定义import路径

参考文档

https://github.com/golang/go/issues/26759
https://github.com/golang/go/issues/16860
https://www.atatech.org/articles/116635#modules
https://supereagle.github.io/2018/06/17/multiple-dep-versions/
http://www.cppblog.com/sunicdavy/archive/2017/07/06/215057.html
http://razil.cc/post/2018/08/go-plugin-package-version-error/
https://groups.google.com/forum/#!topic/golang-codereviews/_kALgmWInGQ

上一篇 下一篇

猜你喜欢

热点阅读