From 9f50e1a8a5726b992bd631e20045738273af1d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 20 May 2023 16:59:05 +0100 Subject: [PATCH] tweak when we read and write cachedOutput files We first called the typecheck method, which starts filling cachedOutput with information from the current package, and later we would load the gob files for all dependencies via loadCachedOutputs. This was a bit confusing; instead, load the cached gob files first, and then do all the operations which fill information for curPkg. Similarly, we were waiting until the very end of transformCompile to write curPkg's cachedOutput gob file to the disk cache. We can write the file at an earlier point, before we have obfuscated and re-printed all Go files for the current package. We can also write the file before other work like processImportCfg. None of these changes should affect garble's behavior, but they will make the cache redesign for #708 easier. --- main.go | 76 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/main.go b/main.go index 86367db..38206e1 100644 --- a/main.go +++ b/main.go @@ -894,40 +894,57 @@ func transformCompile(args []string) ([]string, error) { files = append(files, file) } tf := newTransformer() - if err := tf.typecheck(files); err != nil { - return nil, err - } - - flags = alterTrimpath(flags) + // cachedOutput is modified starting at this point, with the typecheck call. + // We use an extra syntax block to clarify what bits of code set up the caching. // Note that if the file already exists in the cache from another build, // we don't need to write to it again thanks to the hash. // TODO: as an optimization, just load that one gob file. - if err := loadCachedOutputs(); err != nil { - return nil, err - } + { + if err := loadCachedOutputs(); err != nil { + return nil, err + } + + if err := tf.typecheck(files); err != nil { + return nil, err + } - ssaProg := ssa.NewProgram(fset, 0) + ssaProg := ssa.NewProgram(fset, 0) - // Create SSA packages for all imports. - // Order is not significant. - created := make(map[*types.Package]bool) - var createAll func(pkgs []*types.Package) - createAll = func(pkgs []*types.Package) { - for _, p := range pkgs { - if !created[p] { - created[p] = true - ssaProg.CreatePackage(p, nil, nil, true) - createAll(p.Imports()) + // Create SSA packages for all imports. + // Order is not significant. + created := make(map[*types.Package]bool) + var createAll func(pkgs []*types.Package) + createAll = func(pkgs []*types.Package) { + for _, p := range pkgs { + if !created[p] { + created[p] = true + ssaProg.CreatePackage(p, nil, nil, true) + createAll(p.Imports()) + } } } - } - createAll(tf.pkg.Imports()) + createAll(tf.pkg.Imports()) + + ssaPkg := ssaProg.CreatePackage(tf.pkg, files, tf.info, false) + ssaPkg.Build() - ssaPkg := ssaProg.CreatePackage(tf.pkg, files, tf.info, false) - ssaPkg.Build() + tf.recordReflection(ssaPkg) - tf.recordReflection(ssaPkg) + if err := tf.prefillObjectMaps(files); err != nil { + return nil, err + } + + if err := writeGobExclusive( + garbleExportFile(curPkg), + cachedOutput, + ); err != nil && !errors.Is(err, fs.ErrExist) { + return nil, err + } + } + // cachedOutput isn't modified after this point. + + flags = alterTrimpath(flags) newImportCfg, err := processImportCfg(flags) if err != nil { return nil, err @@ -941,10 +958,6 @@ func transformCompile(args []string) ([]string, error) { // log.Printf("seeding math/rand with %x\n", randSeed) obfRand = mathrand.New(mathrand.NewSource(int64(binary.BigEndian.Uint64(randSeed)))) - if err := tf.prefillObjectMaps(files); err != nil { - return nil, err - } - // If this is a package to obfuscate, swap the -p flag with the new package path. // We don't if it's the main package, as that just uses "-p main". // We only set newPkgPath if we're obfuscating the import path, @@ -1016,13 +1029,6 @@ func transformCompile(args []string) ([]string, error) { } flags = flagSetValue(flags, "-importcfg", newImportCfg) - if err := writeGobExclusive( - garbleExportFile(curPkg), - cachedOutput, - ); err != nil && !errors.Is(err, fs.ErrExist) { - return nil, err - } - return append(flags, newPaths...), nil }