Skip to content

Commit

Permalink
recheck types after obfuscation of literals
Browse files Browse the repository at this point in the history
Fixes #649
  • Loading branch information
lu4p committed Aug 27, 2023
1 parent 23c8641 commit 9dbbb51
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9dbbb51

Please sign in to comment.