Optimize tiny.txt test and refactoring

pull/94/head
Pagran 5 years ago
parent de6bd96bc4
commit 22088f74f3

@ -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)

@ -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
}
}

@ -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()
}

@ -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")
}
Loading…
Cancel
Save