mirror of
https://github.com/burrowers/garble.git
synced 2026-04-22 15:47:04 +08:00
clean up uses of encoding/gob and go/ssa
encoding/gob.Decoder already has "map copy" semantics; any decoded map keys get inserted, and other existing map keys are left. This is exactly the same behavior that the new code did with maps.Copy, so the new code is unnecessary. If there is any bug with this code, it needs to be shown via a test. As far as I can tell, there is no bug, because no two packages should clash in terms of map keys. Add a helper to obtain a go/ssa.Function's origin if there is one, falling back to itself if there is none. This mimics how go/types.Func.Origin works, and lets us simplify quite a bit of code. Finally, fieldToStruct uses origin objects only, so don't do a double lookup in reverse.go. While here, fix up the func mentions in two type aliases.
This commit is contained in:
+4
-14
@@ -20,8 +20,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
funcFullName = string // as per go/types.Func.FullName
|
funcFullName = string // the result of [types.Func.FullName] plus [stripTypeArgs]
|
||||||
objectString = string // as per recordedObjectString
|
objectString = string // the result of [reflectInspector.obfuscatedObjectName]
|
||||||
)
|
)
|
||||||
|
|
||||||
// pkgCache contains information about a package that will be stored in fsCache.
|
// pkgCache contains information about a package that will be stored in fsCache.
|
||||||
@@ -45,17 +45,6 @@ func (c *pkgCache) CopyFrom(c2 pkgCache) {
|
|||||||
maps.Copy(c.ReflectObjectNames, c2.ReflectObjectNames)
|
maps.Copy(c.ReflectObjectNames, c2.ReflectObjectNames)
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodePkgCache(r io.Reader, dst *pkgCache) error {
|
|
||||||
// Decode into a fresh value first: gob merges into non-nil maps, so decoding
|
|
||||||
// directly into dst can leave stale entries in nested maps.
|
|
||||||
var decoded pkgCache
|
|
||||||
if err := gob.NewDecoder(r).Decode(&decoded); err != nil {
|
|
||||||
return fmt.Errorf("gob decode: %w", err)
|
|
||||||
}
|
|
||||||
dst.CopyFrom(decoded)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ssaBuildPkg(pkg *types.Package, files []*ast.File, info *types.Info) *ssa.Package {
|
func ssaBuildPkg(pkg *types.Package, files []*ast.File, info *types.Info) *ssa.Package {
|
||||||
// Create SSA packages for all imports. Order is not significant.
|
// Create SSA packages for all imports. Order is not significant.
|
||||||
ssaProg := ssa.NewProgram(fset, 0)
|
ssaProg := ssa.NewProgram(fset, 0)
|
||||||
@@ -190,7 +179,8 @@ func computePkgCache(fsCache *cache.Cache, lpkg *listedPackage, pkg *types.Packa
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
if err := decodePkgCache(f, &computed); err != nil {
|
// The gob decoder
|
||||||
|
if err := gob.NewDecoder(f).Decode(&computed); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+9
-14
@@ -235,11 +235,7 @@ func (ri *reflectInspector) checkFunction(fun *ssa.Function) {
|
|||||||
// fun.WriteTo(os.Stdout)
|
// fun.WriteTo(os.Stdout)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
originFun := fun.Origin()
|
f, _ := ssaFuncOrigin(fun).Object().(*types.Func)
|
||||||
if originFun == nil {
|
|
||||||
originFun = fun
|
|
||||||
}
|
|
||||||
f, _ := originFun.Object().(*types.Func)
|
|
||||||
var funcName string
|
var funcName string
|
||||||
genericFunc := false
|
genericFunc := false
|
||||||
if f != nil {
|
if f != nil {
|
||||||
@@ -281,16 +277,8 @@ func (ri *reflectInspector) checkFunction(fun *ssa.Function) {
|
|||||||
case *ssa.Call:
|
case *ssa.Call:
|
||||||
callName := ""
|
callName := ""
|
||||||
if callee := inst.Call.StaticCallee(); callee != nil {
|
if callee := inst.Call.StaticCallee(); callee != nil {
|
||||||
if obj, ok := callee.Object().(*types.Func); ok && obj != nil {
|
if obj, ok := ssaFuncOrigin(callee).Object().(*types.Func); ok && obj != nil {
|
||||||
callName = obj.FullName()
|
callName = obj.FullName()
|
||||||
} else {
|
|
||||||
originCallee := callee.Origin()
|
|
||||||
if originCallee == nil {
|
|
||||||
originCallee = callee
|
|
||||||
}
|
|
||||||
if obj, ok := originCallee.Object().(*types.Func); ok && obj != nil {
|
|
||||||
callName = obj.FullName()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if callName == "" && inst.Call.Method != nil {
|
if callName == "" && inst.Call.Method != nil {
|
||||||
@@ -648,3 +636,10 @@ func stripTypeArgs(name string) (string, bool) {
|
|||||||
}
|
}
|
||||||
return b.String(), true
|
return b.String(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ssaFuncOrigin(fn *ssa.Function) *ssa.Function {
|
||||||
|
if orig := fn.Origin(); orig != nil {
|
||||||
|
return orig
|
||||||
|
}
|
||||||
|
return fn
|
||||||
|
}
|
||||||
|
|||||||
@@ -105,9 +105,6 @@ One can reverse a captured panic stack trace as follows:
|
|||||||
}
|
}
|
||||||
originObj := obj.Origin()
|
originObj := obj.Origin()
|
||||||
strct := fieldToStruct[originObj]
|
strct := fieldToStruct[originObj]
|
||||||
if strct == nil {
|
|
||||||
strct = fieldToStruct[obj]
|
|
||||||
}
|
|
||||||
if strct == nil {
|
if strct == nil {
|
||||||
panic("could not find struct for field " + name.Name)
|
panic("could not find struct for field " + name.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user