Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion link/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"reflect"
"runtime"
"strings"
"unsafe"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
25 changes: 24 additions & 1 deletion link/typelink.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package link

import (
"unsafe"

"github.com/pkujhd/goloader/constants"
)

Expand All @@ -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
}
}
}