From 9dbbb518d19a4067ab28b1aaff4a5c536340a746 Mon Sep 17 00:00:00 2001 From: lu4p Date: Mon, 28 Aug 2023 00:07:29 +0200 Subject: [PATCH] recheck types after obfuscation of literals Fixes #649 --- main.go | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 1dd2fb59..6bb11e10 100644 --- a/main.go +++ b/main.go @@ -42,8 +42,8 @@ import ( "golang.org/x/mod/semver" "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/ssa" - "mvdan.cc/garble/internal/ctrlflow" + "mvdan.cc/garble/internal/ctrlflow" "mvdan.cc/garble/internal/linker" "mvdan.cc/garble/internal/literals" ) @@ -1038,6 +1038,32 @@ func (tf *transformer) transformCompile(args []string) ([]string, error) { } } tf.transformDirectives(file.Comments) + // Only obfuscate the literals here if the flag is on + // and if the package in question is to be obfuscated. + // + // We can't obfuscate literals in the runtime and its dependencies, + // because obfuscated literals sometimes escape to heap, + // and that's not allowed in the runtime itself. + if flagLiterals && tf.curPkg.ToObfuscate { + file = literals.Obfuscate(tf.obfRand, file, tf.info, tf.linkerVariableStrings) + + // some imported constants might not be needed anymore, remove unnecessary imports + tf.useAllImports(file) + } + + files[i] = file + } + + if flagLiterals && tf.curPkg.ToObfuscate { + if tf.pkg, tf.info, err = typecheck(tf.curPkg.ImportPath, files, tf.origImporter); err != nil { + return nil, err + } + } + + for i, file := range files { + basename := filepath.Base(paths[i]) + log.Printf("obfuscating %s", basename) + file = tf.transformGoFile(file) // newPkgPath might be the original ImportPath in some edge cases like // compilerIntrinsics; we don't want to use slashes in package names. @@ -1832,19 +1858,6 @@ func (tf *transformer) useAllImports(file *ast.File) { // transformGoFile obfuscates the provided Go syntax file. func (tf *transformer) transformGoFile(file *ast.File) *ast.File { - // Only obfuscate the literals here if the flag is on - // and if the package in question is to be obfuscated. - // - // We can't obfuscate literals in the runtime and its dependencies, - // because obfuscated literals sometimes escape to heap, - // and that's not allowed in the runtime itself. - if flagLiterals && tf.curPkg.ToObfuscate { - file = literals.Obfuscate(tf.obfRand, file, tf.info, tf.linkerVariableStrings) - - // some imported constants might not be needed anymore, remove unnecessary imports - tf.useAllImports(file) - } - pre := func(cursor *astutil.Cursor) bool { node, ok := cursor.Node().(*ast.Ident) if !ok {