diff --git a/internal/literals/literals.go b/internal/literals/literals.go index 07dee56..335a75a 100644 --- a/internal/literals/literals.go +++ b/internal/literals/literals.go @@ -191,14 +191,21 @@ func withPos(node ast.Node, pos token.Pos) ast.Node { node.ValuePos = pos case *ast.Ident: node.NamePos = pos - case *ast.StarExpr: - node.Star = pos case *ast.CompositeLit: node.Lbrace = pos + node.Rbrace = pos case *ast.ArrayType: node.Lbrack = pos case *ast.FuncType: node.Func = pos + case *ast.BinaryExpr: + node.OpPos = pos + case *ast.StarExpr: + node.Star = pos + case *ast.CallExpr: + node.Lparen = pos + node.Rparen = pos + case *ast.GenDecl: node.TokPos = pos case *ast.ReturnStmt: @@ -207,8 +214,8 @@ func withPos(node ast.Node, pos token.Pos) ast.Node { node.For = pos case *ast.RangeStmt: node.For = pos - case *ast.CallExpr: - node.Lparen = pos + case *ast.BranchStmt: + node.TokPos = pos } return true }) @@ -278,8 +285,13 @@ func RecordUsedAsConstants(node ast.Node, info *types.Info, ignoreObj map[types. return true } + // Only record *types.Const objects. + // Other objects, such as builtins or type names, + // must not be recorded as they would be false positives. obj := info.ObjectOf(ident) - ignoreObj[obj] = true + if _, ok := obj.(*types.Const); ok { + ignoreObj[obj] = true + } return true } diff --git a/testdata/scripts/literals.txt b/testdata/scripts/literals.txt index d097b24..8054e1d 100644 --- a/testdata/scripts/literals.txt +++ b/testdata/scripts/literals.txt @@ -27,15 +27,11 @@ binsubstr main$exe 'Lorem' 'dolor' 'second assign' 'First Line' 'Second Line' 'm # seconds, it means we're trying to obfuscate them. generate-literals extra_literals.go -# Also check that the binary is different from previous builds. -rm main$exe -garble -literals -debugdir=debug1 -seed=8J+Ri/Cfh6fwn4e+ build -! bincmp main$exe main_old$exe - +garble -literals -debugdir=debug1 build exec ./main$exe cmp stderr main.stderr -# Check obfuscators +# Check obfuscators. # Xor obfuscator. Detect a[i] = a[i] (^|-|+) b[i] grep '^\s+\w+\[\w+\] = \w+\[\w+\] [\^\-+] \w+$' debug1/test/main/extra_literals.go @@ -53,6 +49,11 @@ grep '^\s+\w+ = .*\bappend\(\w+,(\s+\w+\[\d+\][\^\-+]\w+\[\d+\],?)+\)$' debug1/t # XorSeed obfuscator. Detect type decFunc func(byte) decFunc grep '^\s+type \w+ func\(byte\) \w+$' debug1/test/main/extra_literals.go +# Finally, sanity check that we can build all of std with -literals. +# Analogous to goprivate.txt. +env GOPRIVATE='*' +garble -literals build std + -- go.mod -- module test/main @@ -60,6 +61,8 @@ go 1.16 -- main.go -- package main +import "test/main/imported" + type strucTest struct { field string anotherfield string @@ -203,6 +206,11 @@ func constantTest() { const f = "foo" // skip const i = length + len(f) println(length, i) + + // We should still obfuscate ImportedType here. + // Otherwise, the build will fail, + // as the name was obfuscated in the original package. + const impType = imported.ImportedType(3) } func byteTest() { @@ -242,6 +250,10 @@ const ( iota0 uint8 = iota iota1 ) +-- imported/imported.go -- +package imported + +type ImportedType int -- directives.go -- // If we misplace any of the directives below, // cmd/compile will complain with "misplaced compiler directive".