summaryrefslogtreecommitdiff
path: root/plugincheck/check.go
blob: 8d3b683882117acf0293538f3a3bf941cd5397fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package plugincheck

import (
	"debug/buildinfo"
	"errors"
	"fmt"
	"runtime/debug"
)

// CheckPluginCompatibility verifies that the plugin .so file was built
// with the same Go version and dependency versions as the host binary.
func CheckPluginCompatibility(pluginPath string) error {
	pluginInfo, err := buildinfo.ReadFile(pluginPath)
	if err != nil {
		return fmt.Errorf("failed to read plugin build info: %w", err)
	}

	mainInfo, ok := debug.ReadBuildInfo()
	if !ok {
		return errors.New("failed to read main binary build info")
	}

	if pluginInfo.GoVersion != mainInfo.GoVersion {
		return fmt.Errorf("Go version mismatch: plugin=%s, host=%s",
			pluginInfo.GoVersion, mainInfo.GoVersion)
	}

	// Create a map of main binary dependencies for quick lookup
	hostDeps := make(map[string]string)
	for _, dep := range mainInfo.Deps {
		hostDeps[dep.Path] = dep.Version
	}

	// Compare plugin dependencies
	for _, dep := range pluginInfo.Deps {
		hostVer, ok := hostDeps[dep.Path]
		if !ok {
			return fmt.Errorf("dependency %s not found in host binary", dep.Path)
		}
		if dep.Version != hostVer {
			return fmt.Errorf("dependency version mismatch for %s: plugin=%s, host=%s",
				dep.Path, dep.Version, hostVer)
		}
	}

	return nil
}