From 0150aa8bb0ecc95965540c784aa055d361e4ebc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 17 Apr 2021 23:08:23 +0100 Subject: [PATCH] support reversing field names THey don't show up in stack traces, and if they show up in regular program output, we should in theory not obfuscate those names via the detection of reflection. However, there's one relatively common scenario where obfuscated field names can appear: in build error messages, when obfuscation fails a build. --- reverse.go | 4 ++++ testdata/scripts/reverse.txt | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/reverse.go b/reverse.go index d4c3912..29504f8 100644 --- a/reverse.go +++ b/reverse.go @@ -81,6 +81,10 @@ func commandReverse(args []string) error { addReplace(node.Name.Name) case *ast.TypeSpec: addReplace(node.Name.Name) + case *ast.Field: + for _, name := range node.Names { + addReplace(name.Name) + } case *ast.CallExpr: // continues below diff --git a/testdata/scripts/reverse.txt b/testdata/scripts/reverse.txt index 856ffc4..236b5be 100644 --- a/testdata/scripts/reverse.txt +++ b/testdata/scripts/reverse.txt @@ -12,12 +12,20 @@ cp stderr main.stderr # This output is not reproducible between 'go test' runs, # so we can't use a static golden file. grep 'goroutine 1 \[running\]' main.stderr -! grep 'ExportedLibFunc|unexportedMainFunc|test/main|main\.go|lib\.go' main.stderr +# Note that ExportedLibMethod isn't obfuscated. +! grep 'ExportedLib(Type|Field)|unexportedMainFunc|test/main|main\.go|lib\.go' main.stderr stdin main.stderr garble reverse cmp stdout reverse.stdout +! garble build ./build-error +cp stderr build-error.stderr + +stdin build-error.stderr +garble reverse ./build-error +cmp stdout build-error-reverse.stdout + [short] stop # no need to verify this with -short # Ensure that the reversed output matches the non-garbled output. @@ -93,6 +101,25 @@ func printStackTrace(w io.Writer) error { _, err := w.Write(stack) return err } +-- build-error/error.go -- +package p + +import "reflect" + +// This program is especially crafted to work with "go build", +// but fail with "garble build". +// This is because we attempt to convert from two different struct types, +// since only the anonymous one has its field name obfuscated. +// This is useful, because we test that build errors can be reversed, +// and it also includes a field name. + +type UnobfuscatedStruct struct { + SomeField int +} + +var _ = reflect.TypeOf(UnobfuscatedStruct{}) + +var _ = struct{SomeField int}(UnobfuscatedStruct{}) -- reverse.stdout -- goroutine 1 [running]: runtime/debug.Stack(0x??, 0x??, 0x??) @@ -107,3 +134,8 @@ main.unexportedMainFunc() test/main/main.go:20 +0x?? main.main() test/main/main.go:10 +0x?? +-- build-error-reverse.stdout -- +# test/main/build-error +test/main/build-error/error.go:18: cannot convert UnobfuscatedStruct{} (type UnobfuscatedStruct) to type struct { SomeField int } +exit status 2 +exit status 2