From 4d773743d06bfb25af44ccdb5bde0e50dbfa435c Mon Sep 17 00:00:00 2001 From: BitmapLong <37871599+BitmapLong@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:33:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A7=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8D=B8=E8=BD=BD=E5=90=8E=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=82=AC=E7=A9=BA=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加宿主程序接口断言、Interface Switch 缓存符号注册;记录 Runtime typemap 地址。 --- link/register.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/link/register.go b/link/register.go index 7b76fa6f..5b6e7a37 100644 --- a/link/register.go +++ b/link/register.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "reflect" + "runtime" "strings" "unsafe" @@ -56,12 +57,20 @@ func registerType(t *_type, symPtr map[string]uintptr) { } } +// runtimePinnedTypemapsAddr points to runtime.pinnedTypemaps. This private +// Runtime layout was validated only on Go 1.23.2 linux/amd64; fail closed +// on every other toolchain/platform until separately verified. +var runtimePinnedTypemapsAddr uintptr + func RegSymbol(symPtr map[string]uintptr) error { path, err := os.Executable() if err != nil { return err } err = regSymbol(symPtr, path, false) + if runtime.Version() == "go1.23.2" && runtime.GOOS == "linux" && runtime.GOARCH == "amd64" { + runtimePinnedTypemapsAddr = symPtr["runtime.pinnedTypemaps"] + } addLinkName(symPtr) typelinksRegister(symPtr) regsiterItablinks(symPtr) @@ -90,7 +99,15 @@ func regSymbol(symPtr map[string]uintptr, path string, isIgnoreItab bool) error for _, sym := range symbols { code := strings.ToUpper(string(sym.Code)) if code == "B" || code == "D" || code == "T" || code == "R" { - if !strings.HasPrefix(sym.Name, constants.DefaultPkgPath) && !isTypeName(sym.Name) { + // The host executable's type-assertion and interface-switch caches + // must be registered even though ordinary main-package symbols are + // intentionally excluded. Unload resets these caches; omitting them + // leaves stale *_type pointers after a module is unmapped. + isMainCache := strings.HasPrefix(sym.Name, constants.DefaultPkgPath) && + (strings.Contains(sym.Name, "..typeAssert.") || strings.Contains(sym.Name, "..interfaceSwitch.")) + if isMainCache { + symPtr[sym.Name] = uintptr(int64(sym.Addr) + addrOff) + } else if !strings.HasPrefix(sym.Name, constants.DefaultPkgPath) && !isTypeName(sym.Name) { if !(isItabName(sym.Name) && isIgnoreItab) { symPtr[sym.Name] = uintptr(int64(sym.Addr) + addrOff) } From 414638ca020d4efb9f1025f6b1c75a83a127af30 Mon Sep 17 00:00:00 2001 From: BitmapLong <37871599+BitmapLong@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:35:44 +0800 Subject: [PATCH 2/2] Add removeModuleToTypelinks function Implement logic to remove module typemap from pinned maps. --- link/typelink.1.8.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/link/typelink.1.8.go b/link/typelink.1.8.go index 970d3e81..46751f10 100644 --- a/link/typelink.1.8.go +++ b/link/typelink.1.8.go @@ -4,6 +4,8 @@ package link import ( + "unsafe" + "github.com/pkujhd/goloader/constants" ) @@ -26,4 +28,25 @@ func (linker *Linker) AddTypeLink(codeModule *CodeModule) { } } -func removeModuleToTypelinks(md *moduledata) {} +func removeModuleToTypelinks(md *moduledata) { + // The tested Go 1.23.2 runtime.typelinksinit pins every module typemap in the + // runtime.pinnedTypemaps slice. The stock goloader implementation leaves + // these maps pinned forever, even after moduledata is removed. Remove the + // exact map belonging to this module before unmapping its data segment. + if runtimePinnedTypemapsAddr == 0 || md == nil || md.typemap == nil { + return + } + pinned := (*[]map[typeOff]*_type)(unsafe.Pointer(runtimePinnedTypemapsAddr)) + maps := *pinned + target := *(*uintptr)(unsafe.Pointer(&md.typemap)) + for i := range maps { + current := *(*uintptr)(unsafe.Pointer(&maps[i])) + if current == target { + copy(maps[i:], maps[i+1:]) + maps[len(maps)-1] = nil + *pinned = maps[:len(maps)-1] + md.typemap = nil + return + } + } +}