-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
262 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
internal/loader/funcdata_go118_122.go → internal/loader/funcdata_go118_123.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package loader | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"runtime" | ||
"unsafe" | ||
) | ||
|
||
type mdFieldInfo struct { | ||
off uintptr | ||
sz uintptr | ||
} | ||
|
||
var ( | ||
moduleDataType reflect.Type // runtime.moduledata | ||
|
||
rtModuleDataFields map[string]mdFieldInfo | ||
fgModuleDataFields map[string]mdFieldInfo | ||
) | ||
|
||
func moduledataPanic(reason string) { | ||
panic(" moduledata compatibility issue found: " + reason) | ||
} | ||
|
||
func searchStructForModuleData(t reflect.Type, depth int) bool { | ||
if depth == 0 { | ||
return false | ||
} | ||
for t.Kind() == reflect.Ptr { | ||
t = t.Elem() | ||
} | ||
if t.Kind() != reflect.Struct { | ||
return false | ||
} | ||
if t.Name() == "moduledata" && t.PkgPath() == "runtime" { | ||
moduleDataType = t | ||
return true | ||
} | ||
for i := 0; i < t.NumField(); i++ { | ||
if searchStructForModuleData(t.Field(i).Type, depth-1) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func init() { | ||
// extract reflect.Type of runtime.moduledata from runtime.Frame | ||
t := reflect.TypeOf(runtime.Frame{}) | ||
if !searchStructForModuleData(t, 3) { | ||
moduledataPanic("not found runtime.moduledata in runtime.Frame{}") | ||
} | ||
|
||
t = moduleDataType | ||
rtModuleDataFields = map[string]mdFieldInfo{} | ||
for i := 0; i < t.NumField(); i++ { | ||
f := t.Field(i) | ||
rtModuleDataFields[f.Name] = mdFieldInfo{off: f.Offset, sz: f.Type.Size()} | ||
} | ||
|
||
t = reflect.TypeOf(_ModuleData{}) | ||
fgModuleDataFields = map[string]mdFieldInfo{} | ||
for i := 0; i < t.NumField(); i++ { | ||
f := t.Field(i) | ||
fgModuleDataFields[f.Name] = mdFieldInfo{off: f.Offset, sz: f.Type.Size()} | ||
} | ||
|
||
for name, fi := range fgModuleDataFields { | ||
fi0, ok := rtModuleDataFields[name] | ||
if !ok { | ||
moduledataPanic(fmt.Sprintf("runtime.moduledata field %q gone?", name)) | ||
} | ||
if fi0.sz != fi.sz { | ||
moduledataPanic(fmt.Sprintf("runtime.moduledata field %q type size mismatch.", name)) | ||
} | ||
} | ||
} | ||
|
||
func getModuleDataType() reflect.Type { | ||
// extract reflect.Type of runtime.moduledata from runtime.Frame | ||
t := reflect.TypeOf(runtime.Frame{}) | ||
f, ok := t.FieldByName("funcInfo") | ||
if !ok { | ||
moduledataPanic("missing runtime.Frame{}.funcInfo") | ||
} | ||
t = f.Type | ||
f, ok = t.FieldByName("datap") | ||
if !ok { | ||
moduledataPanic("missing runtime.Frame{}.funcInfo.datap") | ||
} | ||
t = f.Type | ||
return t.Elem() // *runtime.moduledata -> runtime.moduledata | ||
} | ||
|
||
func asRuntimeModuleData(m *_ModuleData) unsafe.Pointer { | ||
b := make([]byte, moduleDataType.Size()) | ||
dst := unsafe.Pointer(&b[0]) | ||
src := unsafe.Pointer(m) | ||
for name, fsrc := range fgModuleDataFields { | ||
fdst, _ := rtModuleDataFields[name] | ||
for i := uintptr(0); i < fsrc.sz; i++ { // copy by byte | ||
*(*byte)(unsafe.Add(dst, fdst.off+i)) = *(*byte)(unsafe.Add(src, fsrc.off+i)) | ||
} | ||
} | ||
return dst | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.