Skip to content

Commit

Permalink
only obfuscate literals in packages to obfuscate
Browse files Browse the repository at this point in the history
Add a regression test in gogarble.txt,
as that test is already set up with packages to not obfuscate.

This bug manifested in the form of a build failure for GOOS=plan9
with -literals turned on:

	[...]/os/file_plan9.go:151:12: invalid operation: cannot call non-function append (variable of type bool)

In this case, the "os" package is not to be obfuscated,
but we would still obfuscate its literals as per the bug.

But, since the package's identifiers were not obfuscated,
names like "append" were not replaced as per ea2e0bd,
meaning that the shadowing would still affect us.

Fixes burrowers#417.
  • Loading branch information
mvdan committed Dec 5, 2021
1 parent fceb19f commit b560fc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,9 @@ func (tf *transformer) recordType(t types.Type) {

// transformGo obfuscates the provided Go syntax file.
func (tf *transformer) transformGo(file *ast.File) *ast.File {
if opts.ObfuscateLiterals {
// Only obfuscate the literals here if the flag is on
// and if the package in question is to be obfuscated.
if opts.ObfuscateLiterals && curPkg.ToObfuscate {
file = literals.Obfuscate(file, tf.info, fset, tf.ignoreObjects)
}

Expand Down
15 changes: 11 additions & 4 deletions testdata/scripts/gogarble.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ env GOPRIVATE=match-absolutely/nothing
! garble build -o=out ./standalone
stderr '^GOGARBLE="match-absolutely/nothing" does not match any packages to be built$'

# A build where just some packages are obfuscated.
env GOGARBLE=test/main/imported
garble build ./importer
garble -literals build -o=out ./importer

! binsubstr out 'some long string to obfuscate'
binsubstr out 'some long string to not obfuscate'

# Obfuscated packages which import non-obfuscated std packages.
# Some of the imported std packages use "import maps" due to vendoring,
Expand Down Expand Up @@ -49,15 +53,18 @@ package main

func main() {}
-- importer/importer.go --
package importer
package main

import "test/main/imported"

var _ = imported.Name
func main() {
println(imported.LongString)
println("some long string to not obfuscate")
}
-- imported/imported.go --
package imported

var Name = "value"
var LongString = "some long string to obfuscate"
-- stdimporter/main.go --
package main

Expand Down

0 comments on commit b560fc2

Please sign in to comment.