From 22088f74f36dd9a480929d73d6f81d71b7e94393 Mon Sep 17 00:00:00 2001 From: Pagran <67878280+pagran@users.noreply.github.com> Date: Fri, 14 Aug 2020 21:10:05 +0300 Subject: [PATCH] Optimize tiny.txt test and refactoring --- line_obfuscator.go | 5 +-- main.go | 5 ++- testdata/scripts/lines.txt | 62 -------------------------------------- testdata/scripts/tiny.txt | 40 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 67 deletions(-) delete mode 100644 testdata/scripts/lines.txt create mode 100644 testdata/scripts/tiny.txt diff --git a/line_obfuscator.go b/line_obfuscator.go index 6f582e6..cf43cee 100644 --- a/line_obfuscator.go +++ b/line_obfuscator.go @@ -11,11 +11,11 @@ import ( const ( // PosMax is the largest line or column value that can be represented without loss. - // Source: https://golang.org/src/cmd/compile/internal/syntax/pos.go + // Source: https://go.googlesource.com/go/+/refs/heads/master/src/cmd/compile/internal/syntax/pos.go#11 PosMax = 1 << 30 // PosMin is the smallest correct value for the line number. - // Source: https://github.com/golang/go/blob/2001685ec01c240eda84762a3bc612ddd3ca93fe/src/cmd/compile/internal/syntax/parser_test.go#L229 + // Source: https://go.googlesource.com/go/+/refs/heads/master/src/cmd/compile/internal/syntax/parser_test.go#229 PosMin = 1 ) @@ -98,6 +98,7 @@ func transformLineInfo(fileIndex int, file *ast.File) ([]string, *ast.File) { return true } + // TODO: Optimize the generated values of line numbers to reduce space usage. linePos := hashWithAsUint64(buildInfo.buildID, fmt.Sprintf("%d:%s", fileIndex, funcDecl.Name), PosMin, PosMax) comment := &ast.Comment{Text: fmt.Sprintf("//line %c.go:%d", nameCharset[mathrand.Intn(len(nameCharset))], linePos)} funcDecl.Doc = prependComment(funcDecl.Doc, comment) diff --git a/main.go b/main.go index a4c4042..ab22f76 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,7 @@ var ( func init() { flagSet.Usage = usage flagSet.BoolVar(&flagGarbleLiterals, "literals", false, "Encrypt all literals with AES, currently only literal strings are supported") - flagSet.BoolVar(&flagGarbleTiny, "tiny", false, "Removes information about file names and line numbers irretrievably") + flagSet.BoolVar(&flagGarbleTiny, "tiny", false, "Optimize for binary size, losing the ability to reverse the process") flagSet.StringVar(&flagDebugDir, "debugdir", "", "Write the garbled source to a given directory: '-debugdir=./debug'") flagSet.StringVar(&flagSeed, "seed", "", "Provide a custom base64-encoded seed: '-seed=o9WDTZ4CN4w=' \nFor a random seed provide: '-seed=random'") } @@ -539,8 +539,7 @@ func transformCompile(args []string) ([]string, error) { if len(extraComments) > 0 { for _, comment := range extraComments { - _, err = printWriter.Write([]byte(comment + "\n")) - if err != nil { + if _, err = printWriter.Write([]byte(comment + "\n")); err != nil { return nil, err } } diff --git a/testdata/scripts/lines.txt b/testdata/scripts/lines.txt deleted file mode 100644 index 0c8a70c..0000000 --- a/testdata/scripts/lines.txt +++ /dev/null @@ -1,62 +0,0 @@ -garble -debugdir=.obf-src build - -env TINY_PATTERN='^\/\/line :1$' -env DEFAULT_PATTERN='^\/\/line \w\.go:[1-9][0-9]*$' -env DEFAULT_STACK_PATTERN='^\t\w\.go:[1-9][0-9]*(\s\+0x[0-9a-f]+)?' -env TINY_STACK_PATTERN='^\t\?\?:[0-9][0-9]*(\s\+0x[0-9a-f]+)?$' - -# Default mode - -# Check for file name leak protection -grep $TINY_PATTERN $WORK/.obf-src/main/main.go -grep $TINY_PATTERN $WORK/.obf-src/main/main1.go - -# Check for default line obfuscation -grep $DEFAULT_PATTERN $WORK/.obf-src/main/main.go -grep $DEFAULT_PATTERN $WORK/.obf-src/main/main1.go - -exec ./main$exe -cp stderr default.stderr -! grep 'main1?\.go' default.stderr -! grep $TINY_STACK_PATTERN default.stderr -grep $DEFAULT_STACK_PATTERN default.stderr - -# Tiny mode - -garble -tiny -debugdir=.obf-src build - -grep $TINY_PATTERN $WORK/.obf-src/main/main.go -grep $TINY_PATTERN $WORK/.obf-src/main/main1.go - -! grep $DEFAULT_PATTERN $WORK/.obf-src/main/main.go -! grep $DEFAULT_PATTERN $WORK/.obf-src/main/main1.go - -exec ./main$exe -cp stderr tiny.stderr -! grep 'main1?\.go' tiny.stderr -! grep $DEFAULT_STACK_PATTERN tiny.stderr -grep $TINY_STACK_PATTERN tiny.stderr - --- go.mod -- -module main --- main.go -- -package main - -import "runtime/debug" - -func dump1() { - debug.PrintStack() -} - -func main() { - dump1() - dump2() -} --- main1.go -- -package main - -import "runtime/debug" - -func dump2() { - debug.PrintStack() -} \ No newline at end of file diff --git a/testdata/scripts/tiny.txt b/testdata/scripts/tiny.txt new file mode 100644 index 0000000..9ebc9b0 --- /dev/null +++ b/testdata/scripts/tiny.txt @@ -0,0 +1,40 @@ +garble -debugdir=.obf-src build + +env TINY_PATTERN='^\/\/line :1$' +env DEFAULT_PATTERN='^\/\/line \w\.go:[1-9][0-9]*$' +env DEFAULT_STACK_PATTERN='^\t\w\.go:[1-9][0-9]*(\s\+0x[0-9a-f]+)?' +env TINY_STACK_PATTERN='^\t\?\?:[0-9][0-9]*(\s\+0x[0-9a-f]+)?$' + +# Default mode + +# Check for file name leak protection +grep $TINY_PATTERN .obf-src/main/main.go + +# Check for default line obfuscation +grep $DEFAULT_PATTERN .obf-src/main/main.go + +! exec ./main$exe +! stderr 'main\.go' +! stderr $TINY_STACK_PATTERN +stderr $DEFAULT_STACK_PATTERN + +# Tiny mode + +garble -tiny -debugdir=.obf-src build + +grep $TINY_PATTERN .obf-src/main/main.go +! grep $DEFAULT_PATTERN .obf-src/main/main.go + +! exec ./main$exe +! stderr 'main\.go' +! stderr $DEFAULT_STACK_PATTERN +stderr $TINY_STACK_PATTERN + +-- go.mod -- +module main +-- main.go -- +package main + +func main() { + panic("Test") +} \ No newline at end of file