main.go 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. // Copyright (c) 2019, The Garble Authors.
  2. // See LICENSE for licensing information.
  3. package main
  4. import (
  5. "bytes"
  6. "crypto/rand"
  7. "encoding/base64"
  8. "encoding/binary"
  9. "encoding/gob"
  10. "encoding/json"
  11. "errors"
  12. "flag"
  13. "fmt"
  14. "go/ast"
  15. "go/importer"
  16. "go/parser"
  17. "go/token"
  18. "go/types"
  19. "io"
  20. "io/fs"
  21. "log"
  22. mathrand "math/rand"
  23. "os"
  24. "os/exec"
  25. "path/filepath"
  26. "regexp"
  27. "runtime"
  28. "runtime/debug"
  29. "strconv"
  30. "strings"
  31. "time"
  32. "unicode"
  33. "unicode/utf8"
  34. "golang.org/x/exp/maps"
  35. "golang.org/x/exp/slices"
  36. "golang.org/x/mod/modfile"
  37. "golang.org/x/mod/module"
  38. "golang.org/x/mod/semver"
  39. "golang.org/x/tools/go/ast/astutil"
  40. "mvdan.cc/garble/internal/literals"
  41. )
  42. var flagSet = flag.NewFlagSet("garble", flag.ContinueOnError)
  43. var (
  44. flagLiterals bool
  45. flagTiny bool
  46. flagDebug bool
  47. flagDebugDir string
  48. flagSeed seedFlag
  49. )
  50. func init() {
  51. flagSet.Usage = usage
  52. flagSet.BoolVar(&flagLiterals, "literals", false, "Obfuscate literals such as strings")
  53. flagSet.BoolVar(&flagTiny, "tiny", false, "Optimize for binary size, losing some ability to reverse the process")
  54. flagSet.BoolVar(&flagDebug, "debug", false, "Print debug logs to stderr")
  55. flagSet.StringVar(&flagDebugDir, "debugdir", "", "Write the obfuscated source to a directory, e.g. -debugdir=out")
  56. flagSet.Var(&flagSeed, "seed", "Provide a base64-encoded seed, e.g. -seed=o9WDTZ4CN4w\nFor a random seed, provide -seed=random")
  57. }
  58. var rxGarbleFlag = regexp.MustCompile(`-(?:literals|tiny|debug|debugdir|seed)(?:$|=)`)
  59. type seedFlag struct {
  60. random bool
  61. bytes []byte
  62. }
  63. func (f seedFlag) present() bool { return len(f.bytes) > 0 }
  64. func (f seedFlag) String() string {
  65. return base64.RawStdEncoding.EncodeToString(f.bytes)
  66. }
  67. func (f *seedFlag) Set(s string) error {
  68. if s == "random" {
  69. f.bytes = make([]byte, 16) // random 128 bit seed
  70. if _, err := rand.Read(f.bytes); err != nil {
  71. return fmt.Errorf("error generating random seed: %v", err)
  72. }
  73. } else {
  74. // We expect unpadded base64, but to be nice, accept padded
  75. // strings too.
  76. s = strings.TrimRight(s, "=")
  77. seed, err := base64.RawStdEncoding.DecodeString(s)
  78. if err != nil {
  79. return fmt.Errorf("error decoding seed: %v", err)
  80. }
  81. if len(seed) < 8 {
  82. return fmt.Errorf("-seed needs at least 8 bytes, have %d", len(seed))
  83. }
  84. f.bytes = seed
  85. }
  86. return nil
  87. }
  88. func usage() {
  89. fmt.Fprintf(os.Stderr, `
  90. Garble obfuscates Go code by wrapping the Go toolchain.
  91. garble [garble flags] command [go flags] [go arguments]
  92. For example, to build an obfuscated program:
  93. garble build ./cmd/foo
  94. Similarly, to combine garble flags and Go build flags:
  95. garble -literals build -tags=purego ./cmd/foo
  96. The following commands are supported:
  97. build replace "go build"
  98. test replace "go test"
  99. version print Garble version
  100. reverse de-obfuscate output such as stack traces
  101. To learn more about a command, run "garble help <command>".
  102. garble accepts the following flags before a command:
  103. `[1:])
  104. flagSet.PrintDefaults()
  105. fmt.Fprintf(os.Stderr, `
  106. For more information, see https://github.com/burrowers/garble.
  107. `[1:])
  108. }
  109. func main() { os.Exit(main1()) }
  110. var (
  111. fset = token.NewFileSet()
  112. sharedTempDir = os.Getenv("GARBLE_SHARED")
  113. parentWorkDir = os.Getenv("GARBLE_PARENT_WORK")
  114. // origImporter is a go/types importer which uses the original versions
  115. // of packages, without any obfuscation. This is helpful to make
  116. // decisions on how to obfuscate our input code.
  117. origImporter = importerWithMap(importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) {
  118. pkg, err := listPackage(path)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return os.Open(pkg.Export)
  123. }).(types.ImporterFrom).ImportFrom)
  124. // Basic information about the package being currently compiled or linked.
  125. curPkg *listedPackage
  126. )
  127. type importerWithMap func(path, dir string, mode types.ImportMode) (*types.Package, error)
  128. func (fn importerWithMap) Import(path string) (*types.Package, error) {
  129. panic("should never be called")
  130. }
  131. func (fn importerWithMap) ImportFrom(path, dir string, mode types.ImportMode) (*types.Package, error) {
  132. if path2 := curPkg.ImportMap[path]; path2 != "" {
  133. path = path2
  134. }
  135. return fn(path, dir, mode)
  136. }
  137. // uniqueLineWriter sits underneath log.SetOutput to deduplicate log lines.
  138. // We log bits of useful information for debugging,
  139. // and logging the same detail twice is not going to help the user.
  140. // Duplicates are relatively normal, given that names tend to repeat.
  141. type uniqueLineWriter struct {
  142. out io.Writer
  143. seen map[string]bool
  144. }
  145. func (w *uniqueLineWriter) Write(p []byte) (n int, err error) {
  146. if !flagDebug {
  147. panic("unexpected use of uniqueLineWriter with -debug unset")
  148. }
  149. if bytes.Count(p, []byte("\n")) != 1 {
  150. panic(fmt.Sprintf("log write wasn't just one line: %q", p))
  151. }
  152. if w.seen[string(p)] {
  153. return len(p), nil
  154. }
  155. if w.seen == nil {
  156. w.seen = make(map[string]bool)
  157. }
  158. w.seen[string(p)] = true
  159. return w.out.Write(p)
  160. }
  161. // debugSince is like time.Since but resulting in shorter output.
  162. // A build process takes at least hundreds of milliseconds,
  163. // so extra decimal points in the order of microseconds aren't meaningful.
  164. func debugSince(start time.Time) time.Duration {
  165. return time.Since(start).Truncate(10 * time.Microsecond)
  166. }
  167. func main1() int {
  168. defer func() {
  169. if os.Getenv("GARBLE_WRITE_ALLOCS") != "true" {
  170. return
  171. }
  172. var memStats runtime.MemStats
  173. runtime.ReadMemStats(&memStats)
  174. fmt.Fprintf(os.Stderr, "garble allocs: %d\n", memStats.Mallocs)
  175. }()
  176. if err := flagSet.Parse(os.Args[1:]); err != nil {
  177. return 2
  178. }
  179. log.SetPrefix("[garble] ")
  180. log.SetFlags(0) // no timestamps, as they aren't very useful
  181. if flagDebug {
  182. // TODO: cover this in the tests.
  183. log.SetOutput(&uniqueLineWriter{out: os.Stderr})
  184. } else {
  185. log.SetOutput(io.Discard)
  186. }
  187. args := flagSet.Args()
  188. if len(args) < 1 {
  189. usage()
  190. return 2
  191. }
  192. if err := mainErr(args); err != nil {
  193. if code, ok := err.(errJustExit); ok {
  194. return int(code)
  195. }
  196. fmt.Fprintln(os.Stderr, err)
  197. // If the build failed and a random seed was used,
  198. // the failure might not reproduce with a different seed.
  199. // Print it before we exit.
  200. if flagSeed.random {
  201. fmt.Fprintf(os.Stderr, "random seed: %s\n", base64.RawStdEncoding.EncodeToString(flagSeed.bytes))
  202. }
  203. return 1
  204. }
  205. return 0
  206. }
  207. type errJustExit int
  208. func (e errJustExit) Error() string { return fmt.Sprintf("exit: %d", e) }
  209. // toolchainVersionSemver is a semver-compatible version of the Go toolchain currently
  210. // being used, as reported by "go env GOVERSION".
  211. // Note that the version of Go that built the garble binary might be newer.
  212. var toolchainVersionSemver string
  213. func goVersionOK() bool {
  214. const (
  215. minGoVersionSemver = "v1.18.0"
  216. suggestedGoVersion = "1.18.x"
  217. )
  218. // rxVersion looks for a version like "go1.2" or "go1.2.3"
  219. rxVersion := regexp.MustCompile(`go\d+\.\d+(?:\.\d+)?`)
  220. toolchainVersionFull := cache.GoEnv.GOVERSION
  221. toolchainVersion := rxVersion.FindString(cache.GoEnv.GOVERSION)
  222. if toolchainVersion == "" {
  223. // Go 1.15.x and older do not have GOVERSION yet.
  224. // We could go the extra mile and fetch it via 'go toolchainVersion',
  225. // but we'd have to error anyway.
  226. fmt.Fprintf(os.Stderr, "Go version is too old; please upgrade to Go %s or a newer devel version\n", suggestedGoVersion)
  227. return false
  228. }
  229. toolchainVersionSemver = "v" + strings.TrimPrefix(toolchainVersion, "go")
  230. if semver.Compare(toolchainVersionSemver, minGoVersionSemver) < 0 {
  231. fmt.Fprintf(os.Stderr, "Go version %q is too old; please upgrade to Go %s\n", toolchainVersionFull, suggestedGoVersion)
  232. return false
  233. }
  234. // Ensure that the version of Go that built the garble binary is equal or
  235. // newer than toolchainVersionSemver.
  236. builtVersionFull := os.Getenv("GARBLE_TEST_GOVERSION")
  237. if builtVersionFull == "" {
  238. builtVersionFull = runtime.Version()
  239. }
  240. builtVersion := rxVersion.FindString(builtVersionFull)
  241. if builtVersion == "" {
  242. // If garble built itself, we don't know what Go version was used.
  243. // Fall back to not performing the check against the toolchain version.
  244. return true
  245. }
  246. builtVersionSemver := "v" + strings.TrimPrefix(builtVersion, "go")
  247. if semver.Compare(builtVersionSemver, toolchainVersionSemver) < 0 {
  248. fmt.Fprintf(os.Stderr, "garble was built with %q and is being used with %q; please rebuild garble with the newer version\n",
  249. builtVersionFull, toolchainVersionFull)
  250. return false
  251. }
  252. return true
  253. }
  254. func mainErr(args []string) error {
  255. // If we recognize an argument, we're not running within -toolexec.
  256. switch command, args := args[0], args[1:]; command {
  257. case "help":
  258. if hasHelpFlag(args) || len(args) > 1 {
  259. fmt.Fprintf(os.Stderr, "usage: garble help [command]\n")
  260. return errJustExit(2)
  261. }
  262. if len(args) == 1 {
  263. return mainErr([]string{args[0], "-h"})
  264. }
  265. usage()
  266. return errJustExit(2)
  267. case "version":
  268. if hasHelpFlag(args) || len(args) > 0 {
  269. fmt.Fprintf(os.Stderr, "usage: garble version\n")
  270. return errJustExit(2)
  271. }
  272. info, ok := debug.ReadBuildInfo()
  273. if !ok {
  274. // The build binary was stripped of build info?
  275. // Could be the case if garble built itself.
  276. fmt.Println("unknown")
  277. return nil
  278. }
  279. mod := &info.Main
  280. if mod.Replace != nil {
  281. mod = mod.Replace
  282. }
  283. // For the tests.
  284. if v := os.Getenv("GARBLE_TEST_BUILDSETTINGS"); v != "" {
  285. var extra []debug.BuildSetting
  286. if err := json.Unmarshal([]byte(v), &extra); err != nil {
  287. return err
  288. }
  289. info.Settings = append(info.Settings, extra...)
  290. }
  291. // Until https://github.com/golang/go/issues/50603 is implemented,
  292. // manually construct something like a pseudo-version.
  293. // TODO: remove when this code is dead, hopefully in Go 1.20.
  294. if mod.Version == "(devel)" {
  295. var vcsTime time.Time
  296. var vcsRevision string
  297. for _, setting := range info.Settings {
  298. switch setting.Key {
  299. case "vcs.time":
  300. // If the format is invalid, we'll print a zero timestamp.
  301. vcsTime, _ = time.Parse(time.RFC3339Nano, setting.Value)
  302. case "vcs.revision":
  303. vcsRevision = setting.Value
  304. if len(vcsRevision) > 12 {
  305. vcsRevision = vcsRevision[:12]
  306. }
  307. }
  308. }
  309. if vcsRevision != "" {
  310. mod.Version = module.PseudoVersion("", "", vcsTime, vcsRevision)
  311. }
  312. }
  313. fmt.Printf("%s %s\n\n", mod.Path, mod.Version)
  314. fmt.Printf("Build settings:\n")
  315. for _, setting := range info.Settings {
  316. if setting.Value == "" {
  317. continue // do empty build settings even matter?
  318. }
  319. // The padding helps keep readability by aligning:
  320. //
  321. // veryverylong.key value
  322. // short.key some-other-value
  323. //
  324. // Empirically, 16 is enough; the longest key seen is "vcs.revision".
  325. fmt.Printf("%16s %s\n", setting.Key, setting.Value)
  326. }
  327. return nil
  328. case "reverse":
  329. return commandReverse(args)
  330. case "build", "test":
  331. cmd, err := toolexecCmd(command, args)
  332. defer os.RemoveAll(os.Getenv("GARBLE_SHARED"))
  333. if err != nil {
  334. return err
  335. }
  336. cmd.Stdout = os.Stdout
  337. cmd.Stderr = os.Stderr
  338. log.Printf("calling via toolexec: %s", cmd)
  339. return cmd.Run()
  340. }
  341. if !filepath.IsAbs(args[0]) {
  342. // -toolexec gives us an absolute path to the tool binary to
  343. // run, so this is most likely misuse of garble by a user.
  344. return fmt.Errorf("unknown command: %q", args[0])
  345. }
  346. // We're in a toolexec sub-process, not directly called by the user.
  347. // Load the shared data and wrap the tool, like the compiler or linker.
  348. if err := loadSharedCache(); err != nil {
  349. return err
  350. }
  351. _, tool := filepath.Split(args[0])
  352. if runtime.GOOS == "windows" {
  353. tool = strings.TrimSuffix(tool, ".exe")
  354. }
  355. if len(args) == 2 && args[1] == "-V=full" {
  356. return alterToolVersion(tool, args)
  357. }
  358. toolexecImportPath := os.Getenv("TOOLEXEC_IMPORTPATH")
  359. curPkg = cache.ListedPackages[toolexecImportPath]
  360. if curPkg == nil {
  361. return fmt.Errorf("TOOLEXEC_IMPORTPATH not found in listed packages: %s", toolexecImportPath)
  362. }
  363. transform := transformFuncs[tool]
  364. transformed := args[1:]
  365. if transform != nil {
  366. startTime := time.Now()
  367. log.Printf("transforming %s with args: %s", tool, strings.Join(transformed, " "))
  368. var err error
  369. if transformed, err = transform(transformed); err != nil {
  370. return err
  371. }
  372. log.Printf("transformed args for %s in %s: %s", tool, debugSince(startTime), strings.Join(transformed, " "))
  373. } else {
  374. log.Printf("skipping transform on %s with args: %s", tool, strings.Join(transformed, " "))
  375. }
  376. cmd := exec.Command(args[0], transformed...)
  377. cmd.Stdout = os.Stdout
  378. cmd.Stderr = os.Stderr
  379. if err := cmd.Run(); err != nil {
  380. return err
  381. }
  382. return nil
  383. }
  384. func hasHelpFlag(flags []string) bool {
  385. for _, f := range flags {
  386. switch f {
  387. case "-h", "-help", "--help":
  388. return true
  389. }
  390. }
  391. return false
  392. }
  393. // toolexecCmd builds an *exec.Cmd which is set up for running "go <command>"
  394. // with -toolexec=garble and the supplied arguments.
  395. //
  396. // Note that it uses and modifies global state; in general, it should only be
  397. // called once from mainErr in the top-level garble process.
  398. func toolexecCmd(command string, args []string) (*exec.Cmd, error) {
  399. // Split the flags from the package arguments, since we'll need
  400. // to run 'go list' on the same set of packages.
  401. flags, args := splitFlagsFromArgs(args)
  402. if hasHelpFlag(flags) {
  403. out, _ := exec.Command("go", command, "-h").CombinedOutput()
  404. fmt.Fprintf(os.Stderr, `
  405. usage: garble [garble flags] %s [arguments]
  406. This command wraps "go %s". Below is its help:
  407. %s`[1:], command, command, out)
  408. return nil, errJustExit(2)
  409. }
  410. for _, flag := range flags {
  411. if rxGarbleFlag.MatchString(flag) {
  412. return nil, fmt.Errorf("garble flags must precede command, like: garble %s build ./pkg", flag)
  413. }
  414. }
  415. // Here is the only place we initialize the cache.
  416. // The sub-processes will parse it from a shared gob file.
  417. cache = &sharedCache{}
  418. // Note that we also need to pass build flags to 'go list', such
  419. // as -tags.
  420. cache.ForwardBuildFlags, _ = filterForwardBuildFlags(flags)
  421. if command == "test" {
  422. cache.ForwardBuildFlags = append(cache.ForwardBuildFlags, "-test")
  423. }
  424. if err := fetchGoEnv(); err != nil {
  425. return nil, err
  426. }
  427. if !goVersionOK() {
  428. return nil, errJustExit(1)
  429. }
  430. var err error
  431. cache.ExecPath, err = os.Executable()
  432. if err != nil {
  433. return nil, err
  434. }
  435. binaryBuildID, err := buildidOf(cache.ExecPath)
  436. if err != nil {
  437. return nil, err
  438. }
  439. cache.BinaryContentID = decodeHash(splitContentID(binaryBuildID))
  440. if err := appendListedPackages(args, true); err != nil {
  441. return nil, err
  442. }
  443. sharedTempDir, err = saveSharedCache()
  444. if err != nil {
  445. return nil, err
  446. }
  447. os.Setenv("GARBLE_SHARED", sharedTempDir)
  448. wd, err := os.Getwd()
  449. if err != nil {
  450. return nil, err
  451. }
  452. os.Setenv("GARBLE_PARENT_WORK", wd)
  453. if flagDebugDir != "" {
  454. if !filepath.IsAbs(flagDebugDir) {
  455. flagDebugDir = filepath.Join(wd, flagDebugDir)
  456. }
  457. if err := os.RemoveAll(flagDebugDir); err != nil {
  458. return nil, fmt.Errorf("could not empty debugdir: %v", err)
  459. }
  460. if err := os.MkdirAll(flagDebugDir, 0o755); err != nil {
  461. return nil, err
  462. }
  463. }
  464. goArgs := []string{
  465. command,
  466. "-trimpath",
  467. "-buildvcs=false",
  468. }
  469. // Pass the garble flags down to each toolexec invocation.
  470. // This way, all garble processes see the same flag values.
  471. var toolexecFlag strings.Builder
  472. toolexecFlag.WriteString("-toolexec=")
  473. quotedExecPath, err := cmdgoQuotedJoin([]string{cache.ExecPath})
  474. if err != nil {
  475. // Can only happen if the absolute path to the garble binary contains
  476. // both single and double quotes. Seems extremely unlikely.
  477. return nil, err
  478. }
  479. toolexecFlag.WriteString(quotedExecPath)
  480. appendFlags(&toolexecFlag, false)
  481. goArgs = append(goArgs, toolexecFlag.String())
  482. if flagDebugDir != "" {
  483. // In case the user deletes the debug directory,
  484. // and a previous build is cached,
  485. // rebuild all packages to re-fill the debug dir.
  486. goArgs = append(goArgs, "-a")
  487. }
  488. if command == "test" {
  489. // vet is generally not useful on obfuscated code; keep it
  490. // disabled by default.
  491. goArgs = append(goArgs, "-vet=off")
  492. }
  493. goArgs = append(goArgs, flags...)
  494. goArgs = append(goArgs, args...)
  495. return exec.Command("go", goArgs...), nil
  496. }
  497. var transformFuncs = map[string]func([]string) ([]string, error){
  498. "asm": transformAsm,
  499. "compile": transformCompile,
  500. "link": transformLink,
  501. }
  502. func transformAsm(args []string) ([]string, error) {
  503. if !curPkg.ToObfuscate {
  504. return args, nil // we're not obfuscating this package
  505. }
  506. flags, paths := splitFlagsFromFiles(args, ".s")
  507. // When assembling, the import path can make its way into the output object file.
  508. if curPkg.Name != "main" {
  509. flags = flagSetValue(flags, "-p", curPkg.obfuscatedImportPath())
  510. }
  511. flags = alterTrimpath(flags)
  512. // If the assembler is running just for -gensymabis,
  513. // don't obfuscate the source, as we are not assembling yet.
  514. // The assembler will run again later; obfuscating twice is just wasteful.
  515. newPaths := make([]string, 0, len(paths))
  516. if !slices.Contains(args, "-gensymabis") {
  517. for _, path := range paths {
  518. name := filepath.Base(path)
  519. pkgDir := filepath.Join(sharedTempDir, filepath.FromSlash(curPkg.ImportPath))
  520. newPath := filepath.Join(pkgDir, name)
  521. newPaths = append(newPaths, newPath)
  522. }
  523. return append(flags, newPaths...), nil
  524. }
  525. // We need to replace all function references with their obfuscated name
  526. // counterparts.
  527. // Luckily, all func names in Go assembly files are immediately followed
  528. // by the unicode "middle dot", like:
  529. //
  530. // TEXT ·privateAdd(SB),$0-24
  531. const middleDot = '·'
  532. middleDotLen := utf8.RuneLen(middleDot)
  533. var buf bytes.Buffer
  534. for _, path := range paths {
  535. // Read the entire file into memory.
  536. // If we find issues with large files, we can use bufio.
  537. content, err := os.ReadFile(path)
  538. if err != nil {
  539. return nil, err
  540. }
  541. buf.Reset()
  542. // Find all middle-dot names, and replace them.
  543. remaining := content
  544. for {
  545. i := bytes.IndexRune(remaining, middleDot)
  546. if i < 0 {
  547. buf.Write(remaining)
  548. remaining = nil
  549. break
  550. }
  551. // We want to replace "OP ·foo" and "OP $·foo",
  552. // but not "OP somepkg·foo" just yet.
  553. // "somepkg" is often runtime, syscall, etc.
  554. // We don't obfuscate any of those for now.
  555. //
  556. // TODO: we'll likely need to deal with this
  557. // when we start obfuscating the runtime.
  558. // When we do, note that we can't hash with curPkg.
  559. localName := false
  560. if i >= 0 {
  561. switch remaining[i-1] {
  562. case ' ', '\t', '$':
  563. localName = true
  564. }
  565. }
  566. i += middleDotLen
  567. buf.Write(remaining[:i])
  568. remaining = remaining[i:]
  569. // The name ends at the first rune which cannot be part
  570. // of a Go identifier, such as a comma or space.
  571. nameEnd := 0
  572. for nameEnd < len(remaining) {
  573. c, size := utf8.DecodeRune(remaining[nameEnd:])
  574. if !unicode.IsLetter(c) && c != '_' && !unicode.IsDigit(c) {
  575. break
  576. }
  577. nameEnd += size
  578. }
  579. name := string(remaining[:nameEnd])
  580. remaining = remaining[nameEnd:]
  581. if !localName {
  582. buf.WriteString(name)
  583. continue
  584. }
  585. newName := hashWithPackage(curPkg, name)
  586. if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed
  587. log.Printf("asm name %q hashed with %x to %q", name, curPkg.GarbleActionID, newName)
  588. }
  589. buf.WriteString(newName)
  590. }
  591. // Uncomment for some quick debugging. Do not delete.
  592. // if curPkg.ToObfuscate {
  593. // fmt.Fprintf(os.Stderr, "\n-- %s --\n%s", path, buf.Bytes())
  594. // }
  595. name := filepath.Base(path)
  596. if path, err := writeTemp(name, buf.Bytes()); err != nil {
  597. return nil, err
  598. } else {
  599. newPaths = append(newPaths, path)
  600. }
  601. }
  602. return append(flags, newPaths...), nil
  603. }
  604. // writeTemp is a mix between os.CreateTemp and os.WriteFile, as it writes a
  605. // named source file in sharedTempDir given an input buffer.
  606. //
  607. // Note that the file is created under a directory tree following curPkg's
  608. // import path, mimicking how files are laid out in modules and GOROOT.
  609. func writeTemp(name string, content []byte) (string, error) {
  610. pkgDir := filepath.Join(sharedTempDir, filepath.FromSlash(curPkg.ImportPath))
  611. if err := os.MkdirAll(pkgDir, 0o777); err != nil {
  612. return "", err
  613. }
  614. dstPath := filepath.Join(pkgDir, name)
  615. if err := writeFileExclusive(dstPath, content); err != nil {
  616. return "", err
  617. }
  618. return dstPath, nil
  619. }
  620. func transformCompile(args []string) ([]string, error) {
  621. var err error
  622. flags, paths := splitFlagsFromFiles(args, ".go")
  623. // We will force the linker to drop DWARF via -w, so don't spend time
  624. // generating it.
  625. flags = append(flags, "-dwarf=false")
  626. var files []*ast.File
  627. for _, path := range paths {
  628. file, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution|parser.ParseComments)
  629. if err != nil {
  630. return nil, err
  631. }
  632. files = append(files, file)
  633. }
  634. tf := newTransformer()
  635. if err := tf.typecheck(files); err != nil {
  636. return nil, err
  637. }
  638. flags = alterTrimpath(flags)
  639. // Note that if the file already exists in the cache from another build,
  640. // we don't need to write to it again thanks to the hash.
  641. // TODO: as an optimization, just load that one gob file.
  642. if err := loadCachedOutputs(); err != nil {
  643. return nil, err
  644. }
  645. tf.findReflectFunctions(files)
  646. newImportCfg, err := processImportCfg(flags)
  647. if err != nil {
  648. return nil, err
  649. }
  650. // Literal obfuscation uses math/rand, so seed it deterministically.
  651. randSeed := curPkg.GarbleActionID
  652. if flagSeed.present() {
  653. randSeed = flagSeed.bytes
  654. }
  655. // log.Printf("seeding math/rand with %x\n", randSeed)
  656. mathrand.Seed(int64(binary.BigEndian.Uint64(randSeed)))
  657. if err := tf.prefillObjectMaps(files); err != nil {
  658. return nil, err
  659. }
  660. // If this is a package to obfuscate, swap the -p flag with the new package path.
  661. // We don't if it's the main package, as that just uses "-p main".
  662. // We only set newPkgPath if we're obfuscating the import path,
  663. // to replace the original package name in the package clause below.
  664. newPkgPath := ""
  665. if curPkg.Name != "main" && curPkg.ToObfuscate {
  666. newPkgPath = curPkg.obfuscatedImportPath()
  667. flags = flagSetValue(flags, "-p", newPkgPath)
  668. }
  669. newPaths := make([]string, 0, len(files))
  670. for i, file := range files {
  671. filename := filepath.Base(paths[i])
  672. log.Printf("obfuscating %s", filename)
  673. if curPkg.ImportPath == "runtime" && flagTiny {
  674. // strip unneeded runtime code
  675. stripRuntime(filename, file)
  676. tf.removeUnnecessaryImports(file)
  677. }
  678. tf.handleDirectives(file.Comments)
  679. file = tf.transformGo(file)
  680. if newPkgPath != "" {
  681. file.Name.Name = newPkgPath
  682. }
  683. src, err := printFile(file)
  684. if err != nil {
  685. return nil, err
  686. }
  687. // It is possible to end up in an edge case where two instances of the
  688. // same package have different Action IDs, but their obfuscation and
  689. // builds produce exactly the same results.
  690. // In such an edge case, Go's build cache is smart enough for the second
  691. // instance to reuse the first's build artifact.
  692. // However, garble's caching via garbleExportFile is not as smart,
  693. // as we base the location of these files purely based on Action IDs.
  694. // Thus, the incremental build can fail to find garble's cached file.
  695. // To sidestep this bug entirely, ensure that different action IDs never
  696. // produce the same cached output when building with garble.
  697. // Note that this edge case tends to happen when a -seed is provided,
  698. // as then a package's Action ID is not used as an obfuscation seed.
  699. // TODO(mvdan): replace this workaround with an actual fix if we can.
  700. // This workaround is presumably worse on the build cache,
  701. // as we end up with extra near-duplicate cached artifacts.
  702. if i == 0 {
  703. src = append(src, fmt.Sprintf(
  704. "\nvar garbleActionID = %q\n", hashToString(curPkg.GarbleActionID),
  705. )...)
  706. }
  707. // Uncomment for some quick debugging. Do not delete.
  708. // if curPkg.ToObfuscate {
  709. // fmt.Fprintf(os.Stderr, "\n-- %s/%s --\n%s", curPkg.ImportPath, filename, src)
  710. // }
  711. if path, err := writeTemp(filename, src); err != nil {
  712. return nil, err
  713. } else {
  714. newPaths = append(newPaths, path)
  715. }
  716. if flagDebugDir != "" {
  717. osPkgPath := filepath.FromSlash(curPkg.ImportPath)
  718. pkgDebugDir := filepath.Join(flagDebugDir, osPkgPath)
  719. if err := os.MkdirAll(pkgDebugDir, 0o755); err != nil {
  720. return nil, err
  721. }
  722. debugFilePath := filepath.Join(pkgDebugDir, filename)
  723. if err := os.WriteFile(debugFilePath, src, 0o666); err != nil {
  724. return nil, err
  725. }
  726. }
  727. }
  728. flags = flagSetValue(flags, "-importcfg", newImportCfg)
  729. if err := writeGobExclusive(
  730. garbleExportFile(curPkg),
  731. cachedOutput,
  732. ); err != nil && !errors.Is(err, fs.ErrExist) {
  733. return nil, err
  734. }
  735. return append(flags, newPaths...), nil
  736. }
  737. // handleDirectives looks at all the comments in a file containing build
  738. // directives, and does the necessary for the obfuscation process to work.
  739. //
  740. // Right now, this means recording what local names are used with go:linkname,
  741. // and rewriting those directives to use obfuscated name from other packages.
  742. func (tf *transformer) handleDirectives(comments []*ast.CommentGroup) {
  743. for _, group := range comments {
  744. for _, comment := range group.List {
  745. if !strings.HasPrefix(comment.Text, "//go:linkname ") {
  746. continue
  747. }
  748. fields := strings.Fields(comment.Text)
  749. if len(fields) != 3 {
  750. // TODO: the 2nd argument is optional, handle when it's not present
  751. continue
  752. }
  753. // This directive has two arguments: "go:linkname localName newName"
  754. // obfuscate the local name, if the current package is obfuscated
  755. if curPkg.ToObfuscate {
  756. fields[1] = hashWithPackage(curPkg, fields[1])
  757. }
  758. // If the new name is of the form "pkgpath.Name", and
  759. // we've obfuscated "Name" in that package, rewrite the
  760. // directive to use the obfuscated name.
  761. newName := fields[2]
  762. dotCnt := strings.Count(newName, ".")
  763. if dotCnt < 1 {
  764. // cgo-generated code uses linknames to made up symbol names,
  765. // which do not have a package path at all.
  766. // Replace the comment in case the local name was obfuscated.
  767. comment.Text = strings.Join(fields, " ")
  768. continue
  769. }
  770. switch newName {
  771. case "main.main", "main..inittask", "runtime..inittask":
  772. // The runtime uses some special symbols with "..".
  773. // We aren't touching those at the moment.
  774. continue
  775. }
  776. // If the package path has multiple dots, split on the
  777. // last one.
  778. lastDotIdx := strings.LastIndex(newName, ".")
  779. pkgPath, name := newName[:lastDotIdx], newName[lastDotIdx+1:]
  780. lpkg, err := listPackage(pkgPath)
  781. if err != nil {
  782. // Probably a made up name like above, but with a dot.
  783. comment.Text = strings.Join(fields, " ")
  784. continue
  785. }
  786. if lpkg.ToObfuscate {
  787. // The name exists and was obfuscated; obfuscate
  788. // the new name.
  789. newName := hashWithPackage(lpkg, name)
  790. newPkgPath := pkgPath
  791. if pkgPath != "main" {
  792. newPkgPath = lpkg.obfuscatedImportPath()
  793. }
  794. fields[2] = newPkgPath + "." + newName
  795. }
  796. comment.Text = strings.Join(fields, " ")
  797. }
  798. }
  799. }
  800. // processImportCfg parses the importcfg file passed to a compile or link step.
  801. // It also builds a new importcfg file to account for obfuscated import paths.
  802. func processImportCfg(flags []string) (newImportCfg string, _ error) {
  803. importCfg := flagValue(flags, "-importcfg")
  804. if importCfg == "" {
  805. return "", fmt.Errorf("could not find -importcfg argument")
  806. }
  807. data, err := os.ReadFile(importCfg)
  808. if err != nil {
  809. return "", err
  810. }
  811. var packagefiles, importmaps [][2]string
  812. for _, line := range strings.Split(string(data), "\n") {
  813. if line == "" || strings.HasPrefix(line, "#") {
  814. continue
  815. }
  816. verb, args, found := strings.Cut(line, " ")
  817. if !found {
  818. continue
  819. }
  820. switch verb {
  821. case "importmap":
  822. beforePath, afterPath, found := strings.Cut(args, "=")
  823. if !found {
  824. continue
  825. }
  826. importmaps = append(importmaps, [2]string{beforePath, afterPath})
  827. case "packagefile":
  828. importPath, objectPath, found := strings.Cut(args, "=")
  829. if !found {
  830. continue
  831. }
  832. packagefiles = append(packagefiles, [2]string{importPath, objectPath})
  833. }
  834. }
  835. // Produce the modified importcfg file.
  836. // This is mainly replacing the obfuscated paths.
  837. // Note that we range over maps, so this is non-deterministic, but that
  838. // should not matter as the file is treated like a lookup table.
  839. newCfg, err := os.CreateTemp(sharedTempDir, "importcfg")
  840. if err != nil {
  841. return "", err
  842. }
  843. for _, pair := range importmaps {
  844. beforePath, afterPath := pair[0], pair[1]
  845. lpkg, err := listPackage(beforePath)
  846. if err != nil {
  847. panic(err) // shouldn't happen
  848. }
  849. if lpkg.ToObfuscate {
  850. // Note that beforePath is not the canonical path.
  851. // For beforePath="vendor/foo", afterPath and
  852. // lpkg.ImportPath can be just "foo".
  853. // Don't use obfuscatedImportPath here.
  854. beforePath = hashWithPackage(lpkg, beforePath)
  855. afterPath = lpkg.obfuscatedImportPath()
  856. }
  857. fmt.Fprintf(newCfg, "importmap %s=%s\n", beforePath, afterPath)
  858. }
  859. for _, pair := range packagefiles {
  860. impPath, pkgfile := pair[0], pair[1]
  861. lpkg, err := listPackage(impPath)
  862. if err != nil {
  863. // TODO: it's unclear why an importcfg can include an import path
  864. // that's not a dependency in an edge case with "go test ./...".
  865. // See exporttest/*.go in testdata/scripts/test.txt.
  866. // For now, spot the pattern and avoid the unnecessary error;
  867. // the dependency is unused, so the packagefile line is redundant.
  868. // This still triggers as of go1.19beta1.
  869. if strings.HasSuffix(curPkg.ImportPath, ".test]") && strings.HasPrefix(curPkg.ImportPath, impPath) {
  870. continue
  871. }
  872. panic(err) // shouldn't happen
  873. }
  874. if lpkg.Name != "main" {
  875. impPath = lpkg.obfuscatedImportPath()
  876. }
  877. fmt.Fprintf(newCfg, "packagefile %s=%s\n", impPath, pkgfile)
  878. }
  879. // Uncomment to debug the transformed importcfg. Do not delete.
  880. // newCfg.Seek(0, 0)
  881. // io.Copy(os.Stderr, newCfg)
  882. if err := newCfg.Close(); err != nil {
  883. return "", err
  884. }
  885. return newCfg.Name(), nil
  886. }
  887. type (
  888. funcFullName = string // as per go/types.Func.FullName
  889. objectString = string // as per recordedObjectString
  890. reflectParameter struct {
  891. Position int // 0-indexed
  892. Variadic bool // ...int
  893. }
  894. typeName struct {
  895. PkgPath, Name string
  896. }
  897. )
  898. // TODO: read-write globals like these should probably be inside transformer
  899. // knownCannotObfuscateUnexported is like KnownCannotObfuscate but for
  900. // unexported names. We don't need to store this in the build cache,
  901. // because these names cannot be referenced by downstream packages.
  902. var knownCannotObfuscateUnexported = map[types.Object]bool{}
  903. // cachedOutput contains information that will be stored as per garbleExportFile.
  904. // Note that cachedOutput gets loaded from all direct package dependencies,
  905. // and gets filled while obfuscating the current package, so it ends up
  906. // containing entries for the current package and its transitive dependencies.
  907. var cachedOutput = struct {
  908. // KnownReflectAPIs is a static record of what std APIs use reflection on their
  909. // parameters, so we can avoid obfuscating types used with them.
  910. //
  911. // TODO: we're not including fmt.Printf, as it would have many false positives,
  912. // unless we were smart enough to detect which arguments get used as %#v or %T.
  913. KnownReflectAPIs map[funcFullName][]reflectParameter
  914. // KnownCannotObfuscate is filled with the fully qualified names from each
  915. // package that we cannot obfuscate.
  916. // This record is necessary for knowing what names from imported packages
  917. // weren't obfuscated, so we can obfuscate their local uses accordingly.
  918. KnownCannotObfuscate map[objectString]struct{}
  919. // KnownEmbeddedAliasFields records which embedded fields use a type alias.
  920. // They are the only instance where a type alias matters for obfuscation,
  921. // because the embedded field name is derived from the type alias itself,
  922. // and not the type that the alias points to.
  923. // In that way, the type alias is obfuscated as a form of named type,
  924. // bearing in mind that it may be owned by a different package.
  925. KnownEmbeddedAliasFields map[objectString]typeName
  926. }{
  927. KnownReflectAPIs: map[funcFullName][]reflectParameter{
  928. "reflect.TypeOf": {{Position: 0, Variadic: false}},
  929. "reflect.ValueOf": {{Position: 0, Variadic: false}},
  930. },
  931. KnownCannotObfuscate: map[objectString]struct{}{},
  932. KnownEmbeddedAliasFields: map[objectString]typeName{},
  933. }
  934. // garbleExportFile returns an absolute path to a build cache entry
  935. // which belongs to garble and corresponds to the given Go package.
  936. //
  937. // Unlike pkg.Export, it is only read and written by garble itself.
  938. // Also unlike pkg.Export, it includes GarbleActionID,
  939. // so its path will change if the obfuscated build changes.
  940. //
  941. // The purpose of such a file is to store garble-specific information
  942. // in the build cache, to be reused at a later time.
  943. // The file should have the same lifetime as pkg.Export,
  944. // as it lives under the same cache directory that gets trimmed automatically.
  945. func garbleExportFile(pkg *listedPackage) string {
  946. trimmed := strings.TrimSuffix(pkg.Export, "-d")
  947. if trimmed == pkg.Export {
  948. panic(fmt.Sprintf("unexpected export path of %s: %q", pkg.ImportPath, pkg.Export))
  949. }
  950. return trimmed + "-garble-" + hashToString(pkg.GarbleActionID) + "-d"
  951. }
  952. func loadCachedOutputs() error {
  953. startTime := time.Now()
  954. loaded := 0
  955. for _, path := range curPkg.Deps {
  956. pkg, err := listPackage(path)
  957. if err != nil {
  958. panic(err) // shouldn't happen
  959. }
  960. if pkg.Export == "" {
  961. continue // nothing to load
  962. }
  963. // this function literal is used for the deferred close
  964. if err := func() error {
  965. filename := garbleExportFile(pkg)
  966. f, err := os.Open(filename)
  967. if err != nil {
  968. return err
  969. }
  970. defer f.Close()
  971. // Decode appends new entries to the existing maps
  972. if err := gob.NewDecoder(f).Decode(&cachedOutput); err != nil {
  973. return fmt.Errorf("gob decode: %w", err)
  974. }
  975. return nil
  976. }(); err != nil {
  977. return fmt.Errorf("cannot load garble export file for %s: %w", path, err)
  978. }
  979. loaded++
  980. }
  981. log.Printf("%d cached output files loaded in %s", loaded, debugSince(startTime))
  982. return nil
  983. }
  984. func (tf *transformer) findReflectFunctions(files []*ast.File) {
  985. seenReflectParams := make(map[*types.Var]bool)
  986. visitFuncDecl := func(funcDecl *ast.FuncDecl) {
  987. funcObj := tf.info.Defs[funcDecl.Name].(*types.Func)
  988. funcType := funcObj.Type().(*types.Signature)
  989. funcParams := funcType.Params()
  990. maps.Clear(seenReflectParams)
  991. for i := 0; i < funcParams.Len(); i++ {
  992. seenReflectParams[funcParams.At(i)] = false
  993. }
  994. ast.Inspect(funcDecl, func(node ast.Node) bool {
  995. call, ok := node.(*ast.CallExpr)
  996. if !ok {
  997. return true
  998. }
  999. sel, ok := call.Fun.(*ast.SelectorExpr)
  1000. if !ok {
  1001. return true
  1002. }
  1003. calledFunc, _ := tf.info.Uses[sel.Sel].(*types.Func)
  1004. if calledFunc == nil || calledFunc.Pkg() == nil {
  1005. return true
  1006. }
  1007. fullName := calledFunc.FullName()
  1008. for _, reflectParam := range cachedOutput.KnownReflectAPIs[fullName] {
  1009. // We need a range to handle any number of variadic arguments,
  1010. // which could be 0 or multiple.
  1011. // The non-variadic case is always one argument,
  1012. // but we still use the range to deduplicate code.
  1013. argStart := reflectParam.Position
  1014. argEnd := argStart + 1
  1015. if reflectParam.Variadic {
  1016. argEnd = len(call.Args)
  1017. }
  1018. for _, arg := range call.Args[argStart:argEnd] {
  1019. ident, ok := arg.(*ast.Ident)
  1020. if !ok {
  1021. continue
  1022. }
  1023. obj, _ := tf.info.Uses[ident].(*types.Var)
  1024. if obj == nil {
  1025. continue
  1026. }
  1027. if _, ok := seenReflectParams[obj]; ok {
  1028. seenReflectParams[obj] = true
  1029. }
  1030. }
  1031. }
  1032. var reflectParams []reflectParameter
  1033. for i := 0; i < funcParams.Len(); i++ {
  1034. if seenReflectParams[funcParams.At(i)] {
  1035. reflectParams = append(reflectParams, reflectParameter{
  1036. Position: i,
  1037. Variadic: funcType.Variadic() && i == funcParams.Len()-1,
  1038. })
  1039. }
  1040. }
  1041. if len(reflectParams) > 0 {
  1042. cachedOutput.KnownReflectAPIs[funcObj.FullName()] = reflectParams
  1043. }
  1044. return true
  1045. })
  1046. }
  1047. lenPrevKnownReflectAPIs := len(cachedOutput.KnownReflectAPIs)
  1048. for _, file := range files {
  1049. for _, decl := range file.Decls {
  1050. if decl, ok := decl.(*ast.FuncDecl); ok {
  1051. visitFuncDecl(decl)
  1052. }
  1053. }
  1054. }
  1055. // if a new reflectAPI is found we need to Re-evaluate all functions which might be using that API
  1056. if len(cachedOutput.KnownReflectAPIs) > lenPrevKnownReflectAPIs {
  1057. tf.findReflectFunctions(files)
  1058. }
  1059. }
  1060. // cmd/bundle will include a go:generate directive in its output by default.
  1061. // Ours specifies a version and doesn't assume bundle is in $PATH, so drop it.
  1062. //go:generate go run golang.org/x/tools/cmd/bundle@v0.1.9 -o cmdgo_quoted.go -prefix cmdgoQuoted cmd/internal/quoted
  1063. //go:generate sed -i /go:generate/d cmdgo_quoted.go
  1064. // prefillObjectMaps collects objects which should not be obfuscated,
  1065. // such as those used as arguments to reflect.TypeOf or reflect.ValueOf.
  1066. // Since we obfuscate one package at a time, we only detect those if the type
  1067. // definition and the reflect usage are both in the same package.
  1068. func (tf *transformer) prefillObjectMaps(files []*ast.File) error {
  1069. tf.linkerVariableStrings = make(map[*types.Var]string)
  1070. // TODO: this is a linker flag that affects how we obfuscate a package at
  1071. // compile time. Note that, if the user changes ldflags, then Go may only
  1072. // re-link the final binary, without re-compiling any packages at all.
  1073. // It's possible that this could result in:
  1074. //
  1075. // garble -literals build -ldflags=-X=pkg.name=before # name="before"
  1076. // garble -literals build -ldflags=-X=pkg.name=after # name="before" as cached
  1077. //
  1078. // We haven't been able to reproduce this problem for now,
  1079. // but it's worth noting it and keeping an eye out for it in the future.
  1080. // If we do confirm this theoretical bug,
  1081. // the solution will be to either find a different solution for -literals,
  1082. // or to force including -ldflags into the build cache key.
  1083. ldflags, err := cmdgoQuotedSplit(flagValue(cache.ForwardBuildFlags, "-ldflags"))
  1084. if err != nil {
  1085. return err
  1086. }
  1087. flagValueIter(ldflags, "-X", func(val string) {
  1088. // val is in the form of "foo.com/bar.name=value".
  1089. fullName, stringValue, found := strings.Cut(val, "=")
  1090. if !found {
  1091. return // invalid
  1092. }
  1093. // fullName is "foo.com/bar.name"
  1094. i := strings.LastIndexByte(fullName, '.')
  1095. path, name := fullName[:i], fullName[i+1:]
  1096. // -X represents the main package as "main", not its import path.
  1097. if path != curPkg.ImportPath && !(path == "main" && curPkg.Name == "main") {
  1098. return // not the current package
  1099. }
  1100. obj, _ := tf.pkg.Scope().Lookup(name).(*types.Var)
  1101. if obj == nil {
  1102. return // no such variable; skip
  1103. }
  1104. tf.linkerVariableStrings[obj] = stringValue
  1105. })
  1106. visit := func(node ast.Node) bool {
  1107. call, ok := node.(*ast.CallExpr)
  1108. if !ok {
  1109. return true
  1110. }
  1111. ident, ok := call.Fun.(*ast.Ident)
  1112. if !ok {
  1113. sel, ok := call.Fun.(*ast.SelectorExpr)
  1114. if !ok {
  1115. return true
  1116. }
  1117. ident = sel.Sel
  1118. }
  1119. fnType, _ := tf.info.Uses[ident].(*types.Func)
  1120. if fnType == nil || fnType.Pkg() == nil {
  1121. return true
  1122. }
  1123. fullName := fnType.FullName()
  1124. for _, reflectParam := range cachedOutput.KnownReflectAPIs[fullName] {
  1125. argStart := reflectParam.Position
  1126. argEnd := argStart + 1
  1127. if reflectParam.Variadic {
  1128. argEnd = len(call.Args)
  1129. }
  1130. for _, arg := range call.Args[argStart:argEnd] {
  1131. argType := tf.info.TypeOf(arg)
  1132. tf.recursivelyRecordAsNotObfuscated(argType)
  1133. }
  1134. }
  1135. return true
  1136. }
  1137. for _, file := range files {
  1138. ast.Inspect(file, visit)
  1139. }
  1140. return nil
  1141. }
  1142. // transformer holds all the information and state necessary to obfuscate a
  1143. // single Go package.
  1144. type transformer struct {
  1145. // The type-checking results; the package itself, and the Info struct.
  1146. pkg *types.Package
  1147. info *types.Info
  1148. // linkerVariableStrings is also initialized by prefillObjectMaps.
  1149. // It records objects for variables used in -ldflags=-X flags,
  1150. // as well as the strings the user wants to inject them with.
  1151. linkerVariableStrings map[*types.Var]string
  1152. // recordTypeDone helps avoid type cycles in recordType.
  1153. // We only need to track named types, as all cycles must use them.
  1154. recordTypeDone map[*types.Named]bool
  1155. // fieldToStruct helps locate struct types from any of their field
  1156. // objects. Useful when obfuscating field names.
  1157. fieldToStruct map[*types.Var]*types.Struct
  1158. }
  1159. // newTransformer helps initialize some maps.
  1160. func newTransformer() *transformer {
  1161. return &transformer{
  1162. info: &types.Info{
  1163. Types: make(map[ast.Expr]types.TypeAndValue),
  1164. Defs: make(map[*ast.Ident]types.Object),
  1165. Uses: make(map[*ast.Ident]types.Object),
  1166. },
  1167. recordTypeDone: make(map[*types.Named]bool),
  1168. fieldToStruct: make(map[*types.Var]*types.Struct),
  1169. }
  1170. }
  1171. func (tf *transformer) typecheck(files []*ast.File) error {
  1172. origTypesConfig := types.Config{Importer: origImporter}
  1173. pkg, err := origTypesConfig.Check(curPkg.ImportPath, fset, files, tf.info)
  1174. if err != nil {
  1175. return fmt.Errorf("typecheck error: %v", err)
  1176. }
  1177. tf.pkg = pkg
  1178. // Run recordType on all types reachable via types.Info.
  1179. // A bit hacky, but I could not find an easier way to do this.
  1180. for _, obj := range tf.info.Defs {
  1181. if obj != nil {
  1182. tf.recordType(obj.Type(), nil)
  1183. }
  1184. }
  1185. for name, obj := range tf.info.Uses {
  1186. if obj == nil {
  1187. continue
  1188. }
  1189. tf.recordType(obj.Type(), nil)
  1190. // Record into KnownEmbeddedAliasFields.
  1191. obj, ok := obj.(*types.TypeName)
  1192. if !ok || !obj.IsAlias() {
  1193. continue
  1194. }
  1195. vr, _ := tf.info.Defs[name].(*types.Var)
  1196. if vr == nil || !vr.Embedded() {
  1197. continue
  1198. }
  1199. vrStr := recordedObjectString(vr)
  1200. if vrStr == "" {
  1201. continue
  1202. }
  1203. aliasTypeName := typeName{
  1204. PkgPath: obj.Pkg().Path(),
  1205. Name: obj.Name(),
  1206. }
  1207. cachedOutput.KnownEmbeddedAliasFields[vrStr] = aliasTypeName
  1208. }
  1209. for _, tv := range tf.info.Types {
  1210. tf.recordType(tv.Type, nil)
  1211. }
  1212. return nil
  1213. }
  1214. // recordType visits every reachable type after typechecking a package.
  1215. // Right now, all it does is fill the fieldToStruct field.
  1216. // Since types can be recursive, we need a map to avoid cycles.
  1217. func (tf *transformer) recordType(used, origin types.Type) {
  1218. if origin == nil {
  1219. origin = used
  1220. }
  1221. type Container interface{ Elem() types.Type }
  1222. switch used := used.(type) {
  1223. case Container:
  1224. origin := origin.(Container)
  1225. tf.recordType(used.Elem(), origin.Elem())
  1226. case *types.Named:
  1227. if tf.recordTypeDone[used] {
  1228. return
  1229. }
  1230. tf.recordTypeDone[used] = true
  1231. // If we have a generic struct like
  1232. //
  1233. // type Foo[T any] struct { Bar T }
  1234. //
  1235. // then we want the hashing to use the original "Bar T",
  1236. // because otherwise different instances like "Bar int" and "Bar bool"
  1237. // will result in different hashes and the field names will break.
  1238. // Ensure we record the original generic struct, if there is one.
  1239. tf.recordType(used.Underlying(), used.Origin().Underlying())
  1240. case *types.Struct:
  1241. origin := origin.(*types.Struct)
  1242. for i := 0; i < used.NumFields(); i++ {
  1243. field := used.Field(i)
  1244. tf.fieldToStruct[field] = origin
  1245. if field.Embedded() {
  1246. tf.recordType(field.Type(), origin.Field(i).Type())
  1247. }
  1248. }
  1249. }
  1250. }
  1251. // TODO: consider caching recordedObjectString via a map,
  1252. // if that shows an improvement in our benchmark
  1253. func recordedObjectString(obj types.Object) objectString {
  1254. if obj, ok := obj.(*types.Var); ok && obj.IsField() {
  1255. // For exported fields, "pkgpath.Field" is not unique,
  1256. // because two exported top-level types could share "Field".
  1257. //
  1258. // Moreover, note that not all fields belong to named struct types;
  1259. // an API could be exposing:
  1260. //
  1261. // var usedInReflection = struct{Field string}
  1262. //
  1263. // For now, a hack: assume that packages don't declare the same field
  1264. // more than once in the same line. This works in practice, but one
  1265. // could craft Go code to break this assumption.
  1266. // Also note that the compiler's object files include filenames and line
  1267. // numbers, but not column numbers nor byte offsets.
  1268. // TODO(mvdan): give this another think, and add tests involving anon types.
  1269. pos := fset.Position(obj.Pos())
  1270. return fmt.Sprintf("%s.%s - %s:%d", obj.Pkg().Path(), obj.Name(),
  1271. filepath.Base(pos.Filename), pos.Line)
  1272. }
  1273. // Names which are not at the top level cannot be imported,
  1274. // so we don't need to record them either.
  1275. // Note that this doesn't apply to fields, which are never top-level.
  1276. if obj.Pkg().Scope().Lookup(obj.Name()) != obj {
  1277. return ""
  1278. }
  1279. // For top-level exported names, "pkgpath.Name" is unique.
  1280. return fmt.Sprintf("%s.%s", obj.Pkg().Path(), obj.Name())
  1281. }
  1282. // recordAsNotObfuscated records all the objects whose names we cannot obfuscate.
  1283. // An object is any named entity, such as a declared variable or type.
  1284. //
  1285. // As of June 2022, this only records types which are used in reflection.
  1286. // TODO(mvdan): If this is still the case in a year's time,
  1287. // we should probably rename "not obfuscated" and "cannot obfuscate" to be
  1288. // directly about reflection, e.g. "used in reflection".
  1289. func recordAsNotObfuscated(obj types.Object) {
  1290. if obj.Pkg().Path() != curPkg.ImportPath {
  1291. panic("called recordedAsNotObfuscated with a foreign object")
  1292. }
  1293. if !obj.Exported() {
  1294. // Unexported names will never be used by other packages,
  1295. // so we don't need to bother recording them in cachedOutput.
  1296. knownCannotObfuscateUnexported[obj] = true
  1297. return
  1298. }
  1299. objStr := recordedObjectString(obj)
  1300. if objStr == "" {
  1301. // If the object can't be described via a qualified string,
  1302. // then other packages can't use it.
  1303. // TODO: should we still record it in knownCannotObfuscateUnexported?
  1304. return
  1305. }
  1306. cachedOutput.KnownCannotObfuscate[objStr] = struct{}{}
  1307. }
  1308. func recordedAsNotObfuscated(obj types.Object) bool {
  1309. if knownCannotObfuscateUnexported[obj] {
  1310. return true
  1311. }
  1312. objStr := recordedObjectString(obj)
  1313. if objStr == "" {
  1314. return false
  1315. }
  1316. _, ok := cachedOutput.KnownCannotObfuscate[objStr]
  1317. return ok
  1318. }
  1319. func (tf *transformer) removeUnnecessaryImports(file *ast.File) {
  1320. usedImports := make(map[string]bool)
  1321. ast.Inspect(file, func(n ast.Node) bool {
  1322. node, ok := n.(*ast.Ident)
  1323. if !ok {
  1324. return true
  1325. }
  1326. uses, ok := tf.info.Uses[node].(*types.PkgName)
  1327. if !ok {
  1328. return true
  1329. }
  1330. usedImports[uses.Imported().Path()] = true
  1331. return true
  1332. })
  1333. for _, imp := range file.Imports {
  1334. if imp.Name != nil && (imp.Name.Name == "_" || imp.Name.Name == ".") {
  1335. continue
  1336. }
  1337. path, err := strconv.Unquote(imp.Path.Value)
  1338. if err != nil {
  1339. panic(err)
  1340. }
  1341. // The import path can't be used directly here, because the actual
  1342. // path resolved via go/types might be different from the naive path.
  1343. lpkg, err := listPackage(path)
  1344. if err != nil {
  1345. panic(err)
  1346. }
  1347. if usedImports[lpkg.ImportPath] {
  1348. continue
  1349. }
  1350. imp.Name = ast.NewIdent("_")
  1351. }
  1352. }
  1353. // transformGo obfuscates the provided Go syntax file.
  1354. func (tf *transformer) transformGo(file *ast.File) *ast.File {
  1355. // Only obfuscate the literals here if the flag is on
  1356. // and if the package in question is to be obfuscated.
  1357. //
  1358. // We can't obfuscate literals in the runtime and its dependencies,
  1359. // because obfuscated literals sometimes escape to heap,
  1360. // and that's not allowed in the runtime itself.
  1361. if flagLiterals && curPkg.ToObfuscate {
  1362. file = literals.Obfuscate(file, tf.info, fset, tf.linkerVariableStrings)
  1363. // some imported constants might not be needed anymore, remove unnecessary imports
  1364. tf.removeUnnecessaryImports(file)
  1365. }
  1366. pre := func(cursor *astutil.Cursor) bool {
  1367. node, ok := cursor.Node().(*ast.Ident)
  1368. if !ok {
  1369. return true
  1370. }
  1371. name := node.Name
  1372. if name == "_" {
  1373. return true // unnamed remains unnamed
  1374. }
  1375. obj := tf.info.ObjectOf(node)
  1376. if obj == nil {
  1377. _, isImplicit := tf.info.Defs[node]
  1378. _, parentIsFile := cursor.Parent().(*ast.File)
  1379. if !isImplicit || parentIsFile {
  1380. // We only care about nil objects in the switch scenario below.
  1381. return true
  1382. }
  1383. // In a type switch like "switch foo := bar.(type) {",
  1384. // "foo" is being declared as a symbolic variable,
  1385. // as it is only actually declared in each "case SomeType:".
  1386. //
  1387. // As such, the symbolic "foo" in the syntax tree has no object,
  1388. // but it is still recorded under Defs with a nil value.
  1389. // We still want to obfuscate that syntax tree identifier,
  1390. // so if we detect the case, create a dummy types.Var for it.
  1391. //
  1392. // Note that "package mypkg" also denotes a nil object in Defs,
  1393. // and we don't want to treat that "mypkg" as a variable,
  1394. // so avoid that case by checking the type of cursor.Parent.
  1395. obj = types.NewVar(node.Pos(), tf.pkg, name, nil)
  1396. }
  1397. pkg := obj.Pkg()
  1398. if vr, ok := obj.(*types.Var); ok && vr.Embedded() {
  1399. // The docs for ObjectOf say:
  1400. //
  1401. // If id is an embedded struct field, ObjectOf returns the
  1402. // field (*Var) it defines, not the type (*TypeName) it uses.
  1403. //
  1404. // If this embedded field is a type alias, we want to
  1405. // handle the alias's TypeName instead of treating it as
  1406. // the type the alias points to.
  1407. //
  1408. // Alternatively, if we don't have an alias, we still want to
  1409. // use the embedded type, not the field.
  1410. vrStr := recordedObjectString(vr)
  1411. aliasTypeName, ok := cachedOutput.KnownEmbeddedAliasFields[vrStr]
  1412. if ok {
  1413. pkg2 := tf.pkg
  1414. if path := aliasTypeName.PkgPath; pkg2.Path() != path {
  1415. // If the package is a dependency, import it.
  1416. // We can't grab the package via tf.pkg.Imports,
  1417. // because some of the packages under there are incomplete.
  1418. // ImportFrom will cache complete imports, anyway.
  1419. var err error
  1420. pkg2, err = origImporter.ImportFrom(path, parentWorkDir, 0)
  1421. if err != nil {
  1422. panic(err)
  1423. }
  1424. }
  1425. tname, ok := pkg2.Scope().Lookup(aliasTypeName.Name).(*types.TypeName)
  1426. if !ok || !tname.IsAlias() {
  1427. if !ok {
  1428. panic(fmt.Sprintf("KnownEmbeddedAliasFields pointed %q to a missing type %q", vrStr, aliasTypeName))
  1429. }
  1430. panic(fmt.Sprintf("KnownEmbeddedAliasFields pointed %q to a non-alias type %q", vrStr, aliasTypeName))
  1431. }
  1432. obj = tname
  1433. } else {
  1434. named := namedType(obj.Type())
  1435. if named == nil {
  1436. return true // unnamed type (probably a basic type, e.g. int)
  1437. }
  1438. obj = named.Obj()
  1439. }
  1440. pkg = obj.Pkg()
  1441. }
  1442. if pkg == nil {
  1443. return true // universe scope
  1444. }
  1445. if pkg.Path() == "embed" {
  1446. // The Go compiler needs to detect types such as embed.FS.
  1447. // That will fail if we change the import path or type name.
  1448. // Leave it as is.
  1449. // Luckily, the embed package just declares the FS type.
  1450. return true
  1451. }
  1452. // The package that declared this object did not obfuscate it.
  1453. if recordedAsNotObfuscated(obj) {
  1454. return true
  1455. }
  1456. // TODO(mvdan): investigate obfuscating these too.
  1457. filename := fset.Position(obj.Pos()).Filename
  1458. if strings.HasPrefix(filename, "_cgo_") || strings.Contains(filename, ".cgo1.") {
  1459. return true
  1460. }
  1461. path := pkg.Path()
  1462. lpkg, err := listPackage(path)
  1463. if err != nil {
  1464. panic(err) // shouldn't happen
  1465. }
  1466. if !lpkg.ToObfuscate {
  1467. return true // we're not obfuscating this package
  1468. }
  1469. hashToUse := lpkg.GarbleActionID
  1470. debugName := "variable"
  1471. // log.Printf("%s: %#v %T", fset.Position(node.Pos()), node, obj)
  1472. switch obj := obj.(type) {
  1473. case *types.Var:
  1474. if !obj.IsField() {
  1475. // Identifiers denoting variables are always obfuscated.
  1476. break
  1477. }
  1478. debugName = "field"
  1479. // From this point on, we deal with struct fields.
  1480. // Fields don't get hashed with the package's action ID.
  1481. // They get hashed with the type of their parent struct.
  1482. // This is because one struct can be converted to another,
  1483. // as long as the underlying types are identical,
  1484. // even if the structs are defined in different packages.
  1485. //
  1486. // TODO: Consider only doing this for structs where all
  1487. // fields are exported. We only need this special case
  1488. // for cross-package conversions, which can't work if
  1489. // any field is unexported. If that is done, add a test
  1490. // that ensures unexported fields from different
  1491. // packages result in different obfuscated names.
  1492. strct := tf.fieldToStruct[obj]
  1493. if strct == nil {
  1494. panic("could not find for " + name)
  1495. }
  1496. node.Name = hashWithStruct(strct, name)
  1497. if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed
  1498. log.Printf("%s %q hashed with struct fields to %q", debugName, name, node.Name)
  1499. }
  1500. return true
  1501. case *types.TypeName:
  1502. debugName = "type"
  1503. case *types.Func:
  1504. sign := obj.Type().(*types.Signature)
  1505. if sign.Recv() == nil {
  1506. debugName = "func"
  1507. } else {
  1508. debugName = "method"
  1509. }
  1510. if obj.Exported() && sign.Recv() != nil {
  1511. return true // might implement an interface
  1512. }
  1513. switch name {
  1514. case "main", "init", "TestMain":
  1515. return true // don't break them
  1516. }
  1517. if strings.HasPrefix(name, "Test") && isTestSignature(sign) {
  1518. return true // don't break tests
  1519. }
  1520. default:
  1521. return true // we only want to rename the above
  1522. }
  1523. node.Name = hashWithPackage(lpkg, name)
  1524. // TODO: probably move the debugf lines inside the hash funcs
  1525. if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed
  1526. log.Printf("%s %q hashed with %x… to %q", debugName, name, hashToUse[:4], node.Name)
  1527. }
  1528. return true
  1529. }
  1530. post := func(cursor *astutil.Cursor) bool {
  1531. imp, ok := cursor.Node().(*ast.ImportSpec)
  1532. if !ok {
  1533. return true
  1534. }
  1535. path, err := strconv.Unquote(imp.Path.Value)
  1536. if err != nil {
  1537. panic(err) // should never happen
  1538. }
  1539. // We're importing an obfuscated package.
  1540. // Replace the import path with its obfuscated version.
  1541. // If the import was unnamed, give it the name of the
  1542. // original package name, to keep references working.
  1543. lpkg, err := listPackage(path)
  1544. if err != nil {
  1545. panic(err) // should never happen
  1546. }
  1547. if !lpkg.ToObfuscate {
  1548. return true
  1549. }
  1550. newPath := lpkg.obfuscatedImportPath()
  1551. imp.Path.Value = strconv.Quote(newPath)
  1552. if imp.Name == nil {
  1553. imp.Name = &ast.Ident{
  1554. NamePos: imp.Path.ValuePos, // ensure it ends up on the same line
  1555. Name: lpkg.Name,
  1556. }
  1557. }
  1558. return true
  1559. }
  1560. return astutil.Apply(file, pre, post).(*ast.File)
  1561. }
  1562. // recursivelyRecordAsNotObfuscated calls recordAsNotObfuscated on any named
  1563. // types and fields under typ.
  1564. //
  1565. // Only the names declared in the current package are recorded. This is to ensure
  1566. // that reflection detection only happens within the package declaring a type.
  1567. // Detecting it in downstream packages could result in inconsistencies.
  1568. func (tf *transformer) recursivelyRecordAsNotObfuscated(t types.Type) {
  1569. switch t := t.(type) {
  1570. case *types.Named:
  1571. obj := t.Obj()
  1572. if obj.Pkg() == nil || obj.Pkg() != tf.pkg {
  1573. return // not from the specified package
  1574. }
  1575. if recordedAsNotObfuscated(obj) {
  1576. return // prevent endless recursion
  1577. }
  1578. recordAsNotObfuscated(obj)
  1579. // Record the underlying type, too.
  1580. tf.recursivelyRecordAsNotObfuscated(t.Underlying())
  1581. case *types.Struct:
  1582. for i := 0; i < t.NumFields(); i++ {
  1583. field := t.Field(i)
  1584. // This check is similar to the one in *types.Named.
  1585. // It's necessary for unnamed struct types,
  1586. // as they aren't named but still have named fields.
  1587. if field.Pkg() == nil || field.Pkg() != tf.pkg {
  1588. return // not from the specified package
  1589. }
  1590. // Record the field itself, too.
  1591. recordAsNotObfuscated(field)
  1592. tf.recursivelyRecordAsNotObfuscated(field.Type())
  1593. }
  1594. case interface{ Elem() types.Type }:
  1595. // Get past pointers, slices, etc.
  1596. tf.recursivelyRecordAsNotObfuscated(t.Elem())
  1597. }
  1598. }
  1599. // named tries to obtain the *types.Named behind a type, if there is one.
  1600. // This is useful to obtain "testing.T" from "*testing.T", or to obtain the type
  1601. // declaration object from an embedded field.
  1602. func namedType(t types.Type) *types.Named {
  1603. switch t := t.(type) {
  1604. case *types.Named:
  1605. return t
  1606. case interface{ Elem() types.Type }:
  1607. return namedType(t.Elem())
  1608. default:
  1609. return nil
  1610. }
  1611. }
  1612. // isTestSignature returns true if the signature matches "func _(*testing.T)".
  1613. func isTestSignature(sign *types.Signature) bool {
  1614. if sign.Recv() != nil {
  1615. return false // test funcs don't have receivers
  1616. }
  1617. params := sign.Params()
  1618. if params.Len() != 1 {
  1619. return false // too many parameters for a test func
  1620. }
  1621. named := namedType(params.At(0).Type())
  1622. if named == nil {
  1623. return false // the only parameter isn't named, like "string"
  1624. }
  1625. obj := named.Obj()
  1626. return obj != nil && obj.Pkg().Path() == "testing" && obj.Name() == "T"
  1627. }
  1628. func transformLink(args []string) ([]string, error) {
  1629. // We can't split by the ".a" extension, because cached object files
  1630. // lack any extension.
  1631. flags, args := splitFlagsFromArgs(args)
  1632. newImportCfg, err := processImportCfg(flags)
  1633. if err != nil {
  1634. return nil, err
  1635. }
  1636. // TODO: unify this logic with the -X handling when using -literals.
  1637. // We should be able to handle both cases via the syntax tree.
  1638. //
  1639. // Make sure -X works with obfuscated identifiers.
  1640. // To cover both obfuscated and non-obfuscated names,
  1641. // duplicate each flag with a obfuscated version.
  1642. flagValueIter(flags, "-X", func(val string) {
  1643. // val is in the form of "foo.com/bar.name=value".
  1644. fullName, stringValue, found := strings.Cut(val, "=")
  1645. if !found {
  1646. return // invalid
  1647. }
  1648. // fullName is "foo.com/bar.name"
  1649. i := strings.LastIndexByte(fullName, '.')
  1650. path, name := fullName[:i], fullName[i+1:]
  1651. // If the package path is "main", it's the current top-level
  1652. // package we are linking.
  1653. // Otherwise, find it in the cache.
  1654. lpkg := curPkg
  1655. if path != "main" {
  1656. lpkg = cache.ListedPackages[path]
  1657. }
  1658. if lpkg == nil {
  1659. // We couldn't find the package.
  1660. // Perhaps a typo, perhaps not part of the build.
  1661. // cmd/link ignores those, so we should too.
  1662. return
  1663. }
  1664. // As before, the main package must remain as "main".
  1665. newPath := path
  1666. if path != "main" {
  1667. newPath = lpkg.obfuscatedImportPath()
  1668. }
  1669. newName := hashWithPackage(lpkg, name)
  1670. flags = append(flags, fmt.Sprintf("-X=%s.%s=%s", newPath, newName, stringValue))
  1671. })
  1672. // Starting in Go 1.17, Go's version is implicitly injected by the linker.
  1673. // It's the same method as -X, so we can override it with an extra flag.
  1674. flags = append(flags, "-X=runtime.buildVersion=unknown")
  1675. // Ensure we strip the -buildid flag, to not leak any build IDs for the
  1676. // link operation or the main package's compilation.
  1677. flags = flagSetValue(flags, "-buildid", "")
  1678. // Strip debug information and symbol tables.
  1679. flags = append(flags, "-w", "-s")
  1680. flags = flagSetValue(flags, "-importcfg", newImportCfg)
  1681. return append(flags, args...), nil
  1682. }
  1683. func splitFlagsFromArgs(all []string) (flags, args []string) {
  1684. for i := 0; i < len(all); i++ {
  1685. arg := all[i]
  1686. if !strings.HasPrefix(arg, "-") {
  1687. return all[:i:i], all[i:]
  1688. }
  1689. if booleanFlags[arg] || strings.Contains(arg, "=") {
  1690. // Either "-bool" or "-name=value".
  1691. continue
  1692. }
  1693. // "-name value", so the next arg is part of this flag.
  1694. i++
  1695. }
  1696. return all, nil
  1697. }
  1698. func alterTrimpath(flags []string) []string {
  1699. // If the value of -trimpath doesn't contain the separator ';', the 'go
  1700. // build' command is most likely not using '-trimpath'.
  1701. trimpath := flagValue(flags, "-trimpath")
  1702. // Add our temporary dir to the beginning of -trimpath, so that we don't
  1703. // leak temporary dirs. Needs to be at the beginning, since there may be
  1704. // shorter prefixes later in the list, such as $PWD if TMPDIR=$PWD/tmp.
  1705. return flagSetValue(flags, "-trimpath", sharedTempDir+"=>;"+trimpath)
  1706. }
  1707. // forwardBuildFlags is obtained from 'go help build' as of Go 1.18beta1.
  1708. var forwardBuildFlags = map[string]bool{
  1709. // These shouldn't be used in nested cmd/go calls.
  1710. "-a": false,
  1711. "-n": false,
  1712. "-x": false,
  1713. "-v": false,
  1714. // These are always set by garble.
  1715. "-trimpath": false,
  1716. "-toolexec": false,
  1717. "-buildvcs": false,
  1718. "-p": true,
  1719. "-race": true,
  1720. "-msan": true,
  1721. "-asan": true,
  1722. "-work": true,
  1723. "-asmflags": true,
  1724. "-buildmode": true,
  1725. "-compiler": true,
  1726. "-gccgoflags": true,
  1727. "-gcflags": true,
  1728. "-installsuffix": true,
  1729. "-ldflags": true,
  1730. "-linkshared": true,
  1731. "-mod": true,
  1732. "-modcacherw": true,
  1733. "-modfile": true,
  1734. "-pkgdir": true,
  1735. "-tags": true,
  1736. "-workfile": true,
  1737. "-overlay": true,
  1738. }
  1739. // booleanFlags is obtained from 'go help build' and 'go help testflag' as of Go 1.19beta1.
  1740. var booleanFlags = map[string]bool{
  1741. // Shared build flags.
  1742. "-a": true,
  1743. "-i": true,
  1744. "-n": true,
  1745. "-v": true,
  1746. "-work": true,
  1747. "-x": true,
  1748. "-race": true,
  1749. "-msan": true,
  1750. "-asan": true,
  1751. "-linkshared": true,
  1752. "-modcacherw": true,
  1753. "-trimpath": true,
  1754. "-buildvcs": true,
  1755. // Test flags (TODO: support its special -args flag)
  1756. "-c": true,
  1757. "-json": true,
  1758. "-cover": true,
  1759. "-failfast": true,
  1760. "-short": true,
  1761. "-benchmem": true,
  1762. }
  1763. func filterForwardBuildFlags(flags []string) (filtered []string, firstUnknown string) {
  1764. for i := 0; i < len(flags); i++ {
  1765. arg := flags[i]
  1766. if strings.HasPrefix(arg, "--") {
  1767. arg = arg[1:] // "--name" to "-name"; keep the short form
  1768. }
  1769. name, _, _ := strings.Cut(arg, "=") // "-name=value" to "-name"
  1770. buildFlag := forwardBuildFlags[name]
  1771. if buildFlag {
  1772. filtered = append(filtered, arg)
  1773. } else {
  1774. firstUnknown = name
  1775. }
  1776. if booleanFlags[arg] || strings.Contains(arg, "=") {
  1777. // Either "-bool" or "-name=value".
  1778. continue
  1779. }
  1780. // "-name value", so the next arg is part of this flag.
  1781. if i++; buildFlag && i < len(flags) {
  1782. filtered = append(filtered, flags[i])
  1783. }
  1784. }
  1785. return filtered, firstUnknown
  1786. }
  1787. // splitFlagsFromFiles splits args into a list of flag and file arguments. Since
  1788. // we can't rely on "--" being present, and we don't parse all flags upfront, we
  1789. // rely on finding the first argument that doesn't begin with "-" and that has
  1790. // the extension we expect for the list of paths.
  1791. //
  1792. // This function only makes sense for lower-level tool commands, such as
  1793. // "compile" or "link", since their arguments are predictable.
  1794. //
  1795. // We iterate from the end rather than from the start, to better protect
  1796. // oursrelves from flag arguments that may look like paths, such as:
  1797. //
  1798. // compile [flags...] -p pkg/path.go [more flags...] file1.go file2.go
  1799. //
  1800. // For now, since those confusing flags are always followed by more flags,
  1801. // iterating in reverse order works around them entirely.
  1802. func splitFlagsFromFiles(all []string, ext string) (flags, paths []string) {
  1803. for i := len(all) - 1; i >= 0; i-- {
  1804. arg := all[i]
  1805. if strings.HasPrefix(arg, "-") || !strings.HasSuffix(arg, ext) {
  1806. cutoff := i + 1 // arg is a flag, not a path
  1807. return all[:cutoff:cutoff], all[cutoff:]
  1808. }
  1809. }
  1810. return nil, all
  1811. }
  1812. // flagValue retrieves the value of a flag such as "-foo", from strings in the
  1813. // list of arguments like "-foo=bar" or "-foo" "bar". If the flag is repeated,
  1814. // the last value is returned.
  1815. func flagValue(flags []string, name string) string {
  1816. lastVal := ""
  1817. flagValueIter(flags, name, func(val string) {
  1818. lastVal = val
  1819. })
  1820. return lastVal
  1821. }
  1822. // flagValueIter retrieves all the values for a flag such as "-foo", like
  1823. // flagValue. The difference is that it allows handling complex flags, such as
  1824. // those whose values compose a list.
  1825. func flagValueIter(flags []string, name string, fn func(string)) {
  1826. for i, arg := range flags {
  1827. if val := strings.TrimPrefix(arg, name+"="); val != arg {
  1828. // -name=value
  1829. fn(val)
  1830. }
  1831. if arg == name { // -name ...
  1832. if i+1 < len(flags) {
  1833. // -name value
  1834. fn(flags[i+1])
  1835. }
  1836. }
  1837. }
  1838. }
  1839. func flagSetValue(flags []string, name, value string) []string {
  1840. for i, arg := range flags {
  1841. if strings.HasPrefix(arg, name+"=") {
  1842. // -name=value
  1843. flags[i] = name + "=" + value
  1844. return flags
  1845. }
  1846. if arg == name { // -name ...
  1847. if i+1 < len(flags) {
  1848. // -name value
  1849. flags[i+1] = value
  1850. return flags
  1851. }
  1852. return flags
  1853. }
  1854. }
  1855. return append(flags, name+"="+value)
  1856. }
  1857. func fetchGoEnv() error {
  1858. out, err := exec.Command("go", "env", "-json",
  1859. "GOOS", "GOPRIVATE", "GOMOD", "GOVERSION", "GOCACHE",
  1860. ).CombinedOutput()
  1861. if err != nil {
  1862. // TODO: cover this in the tests.
  1863. fmt.Fprintf(os.Stderr, `Can't find the Go toolchain: %v
  1864. This is likely due to Go not being installed/setup correctly.
  1865. To install Go, see: https://go.dev/doc/install
  1866. `, err)
  1867. return errJustExit(1)
  1868. }
  1869. if err := json.Unmarshal(out, &cache.GoEnv); err != nil {
  1870. return fmt.Errorf(`cannot unmarshal from "go env -json": %w`, err)
  1871. }
  1872. cache.GOGARBLE = os.Getenv("GOGARBLE")
  1873. if cache.GOGARBLE != "" {
  1874. // GOGARBLE is non-empty; nothing to do.
  1875. } else if cache.GoEnv.GOPRIVATE != "" {
  1876. // GOGARBLE is empty and GOPRIVATE is non-empty.
  1877. // Set GOGARBLE to GOPRIVATE's value.
  1878. cache.GOGARBLE = cache.GoEnv.GOPRIVATE
  1879. } else {
  1880. // If GOPRIVATE isn't set and we're in a module, use its module
  1881. // path as a GOPRIVATE default. Include a _test variant too.
  1882. // TODO(mvdan): we shouldn't need the _test variant here,
  1883. // as the import path should not include it; only the package name.
  1884. if mod, err := os.ReadFile(cache.GoEnv.GOMOD); err == nil {
  1885. modpath := modfile.ModulePath(mod)
  1886. if modpath != "" {
  1887. cache.GOGARBLE = modpath + "," + modpath + "_test"
  1888. }
  1889. }
  1890. }
  1891. return nil
  1892. }