Only obfuscate required identifiers (#79)

The following identifiers are now skipped,
because they never show up in the binary:

- constant identifiers
- identifiers of local variables
(includes function params and named returns)
- identifiers of local types
pull/67/head
lu4p 4 years ago committed by GitHub
parent 5cbbac56f3
commit 21c67b91b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -730,11 +730,6 @@ func transformGo(file *ast.File, info *types.Info, blacklist map[types.Object]st
}
obj := info.ObjectOf(node)
if obj == nil {
switch cursor.Parent().(type) {
case *ast.AssignStmt:
// symbolic var v in v := expr.(type)
node.Name = hashWith(buildInfo.buildID, node.Name)
}
return true
}
pkg := obj.Pkg()
@ -771,8 +766,17 @@ func transformGo(file *ast.File, info *types.Info, blacklist map[types.Object]st
// encoding/json without struct tags
return true
}
case *types.Const:
if obj.Parent() != pkg.Scope() {
// identifiers of non-global variables never show up in the binary
return true
}
case *types.TypeName:
if obj.Parent() != pkg.Scope() {
// identifiers of non-global types never show up in the binary
return true
}
case *types.Func:
sign := obj.Type().(*types.Signature)
if obj.Exported() && sign.Recv() != nil {

@ -1,9 +1,20 @@
garble build main.go
exec ./main
go build
exec ./main$exe
cmp stderr main.stderr
! binsubstr main$exe 'valuable information'
binsubstr main$exe 'globalVar' # 'globalType' only matches on go < 1.15
! binsubstr main$exe 'localName' 'globalConst'
garble -debugdir=debug build
exec ./main$exe
cmp stderr main.stderr
! binsubstr main$exe 'localName' 'globalConst' 'globalVar' 'globalType' 'valuable information'
binsubstr debug/main/z1.go 'localName' 'globalConst'
-- go.mod --
module test/main
-- main.go --
package main
@ -53,7 +64,41 @@ func main() {
enc, _ := json.Marshal(EncodingT{Foo: 3})
println(string(enc))
scopesTest()
}
-- scopes.go --
package main
const globalConst = 1
type globalType int
var (
globalVar = 1
globalVarTyped globalType = 1
)
func scopesTest() {
println(globalVar, globalConst, globalVarTyped)
const localNameConst = 1
localNameShort := 4
type localNameType int
var (
localNameVar = 5
localNameTypeVar localNameType = 1
)
println(localNameConst, localNameShort, localNameVar, localNameTypeVar, input("input"))
}
func input(localNameParam string) (localNameReturn string) { return localNameParam }
-- main.stderr --
nil case
{"Foo":3}
1 1 1
1 4 5 1 input

Loading…
Cancel
Save