mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-04 00:03:59 +00:00
fix: 兼容旧 ABI 插件 + 移除 plugin.Open 致命 fallback
- CABINumMin 恢复为 1: 旧工具链编译的插件写入整数 version=1/2, 与新版内核结构体兼容(仅缺 stage 写回能力), 应允许加载而非拒绝 - tryLoadSO 移除 Go plugin.Open fallback: 本项目插件统一为 c-shared, 对 c-shared .so 调用 plugin.Open 会 fatal (no plugin module data) 不可恢复, 直接返回 cabi.Load 错误避免启动崩溃
This commit is contained in:
@ -44,7 +44,10 @@ const (
|
|||||||
// CABINum 是 C 层协商用的整数版本(major*100 + minor),随 ABIVersion 派生。
|
// CABINum 是 C 层协商用的整数版本(major*100 + minor),随 ABIVersion 派生。
|
||||||
CABINum = 900
|
CABINum = 900
|
||||||
// CABINumMin 是 C 层兼容的最低整数版本。
|
// CABINumMin 是 C 层兼容的最低整数版本。
|
||||||
CABINumMin = 800
|
// 旧工具链(v0.8 之前)写入的整数 version=1,无写回能力但与新内核结构兼容,
|
||||||
|
// 因此最小值保持 1 以兼容全部旧插件(新插件 900 匹配,旧插件 1/2 通过);
|
||||||
|
// 仅当未来内核 ABI 破坏兼容时才提高该值。
|
||||||
|
CABINumMin = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// ---- Dispatch Method IDs ----
|
// ---- Dispatch Method IDs ----
|
||||||
|
|||||||
@ -3,14 +3,10 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"plugin"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
pubsdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
pubsdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||||
@ -74,53 +70,10 @@ func tryLoadSO(dir, name string, config map[string]interface{}) (sdk.Plugin, err
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return &cabiPlugin{name: name, handle: handle}, nil
|
return &cabiPlugin{name: name, handle: handle}, nil
|
||||||
}
|
}
|
||||||
|
// 本项目插件统一由 plugindev 编译为 c-shared 走 C ABI;
|
||||||
data, err := os.ReadFile(soPath)
|
// 对 c-shared .so 调用 Go plugin.Open 会 fatal(no plugin module data),
|
||||||
if err != nil {
|
// 因此不再 fallback 到 Go plugin,直接返回加载错误避免崩溃。
|
||||||
return nil, fmt.Errorf("read %s: %w", soPath, err)
|
return nil, err
|
||||||
}
|
|
||||||
h := sha256.Sum256(data)
|
|
||||||
cacheKey := fmt.Sprintf("plugin_%s_%s.so", name, hex.EncodeToString(h[:8]))
|
|
||||||
cachePath := filepath.Join(os.TempDir(), cacheKey)
|
|
||||||
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
|
|
||||||
if err := os.WriteFile(cachePath, data, 0644); err != nil {
|
|
||||||
return nil, fmt.Errorf("write cache %s: %w", cachePath, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := plugin.Open(cachePath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("plugin.Open %s: %w", cachePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sym, err := p.Lookup("NewPlugin")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(".so %s must export NewPlugin: %w", soPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rv := reflect.ValueOf(sym)
|
|
||||||
if rv.Kind() != reflect.Func {
|
|
||||||
return nil, fmt.Errorf("NewPlugin in %s is not a function (type=%T)", soPath, sym)
|
|
||||||
}
|
|
||||||
if rv.Type().NumIn() != 2 || rv.Type().NumOut() != 2 {
|
|
||||||
return nil, fmt.Errorf("NewPlugin in %s has wrong arity", soPath)
|
|
||||||
}
|
|
||||||
outs := rv.Call([]reflect.Value{reflect.ValueOf(name), reflect.ValueOf(config)})
|
|
||||||
if len(outs) != 2 {
|
|
||||||
return nil, fmt.Errorf("NewPlugin in %s returned unexpected values", soPath)
|
|
||||||
}
|
|
||||||
if !outs[1].IsNil() {
|
|
||||||
if err, ok := outs[1].Interface().(error); ok {
|
|
||||||
return nil, fmt.Errorf("NewPlugin %s: %w", name, err)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("NewPlugin %s returned non-error second value", name)
|
|
||||||
}
|
|
||||||
plg, ok := outs[0].Interface().(pubsdk.Plugin)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("NewPlugin in %s does not implement pubsdk.Plugin", soPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &dynamicPlugin{name: name, impl: plg}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = json.Marshal
|
var _ = json.Marshal
|
||||||
|
|||||||
Reference in New Issue
Block a user