From 89dbdb69a1c2701e3378244d02524694db27b3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 28 Feb 2021 22:12:14 +0000 Subject: [PATCH] start working on Go 1.16 support (#244) There are three minor bugs breaking Go 1.16 with the current version of garble, after the import path obfuscation refactor: 1) Stripping the runtime results in an unused import error. This PR fixes that. 2) The asm.txt test seems to be broken; something to do with the export data not being right for the exported assembly func. 3) The obfuscated build of std fails, since our runtimeRelated table was generated for Go 1.15, not 1.16. This PR fixes the first issue, adds conditional skip lines for 1.16 for the other two issues, and enables 1.16 on CI. Note that 1.16 support is not here just yet, because of the other two issues. As such, no doc changes. Updates #124. --- .github/workflows/test.yml | 2 +- runtime_strip.go | 26 +++++++++++++++++++++++++- testdata/scripts/asm.txt | 2 ++ testdata/scripts/goprivate.txt | 2 ++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aab51bc..67fe24c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: test: strategy: matrix: - go-version: [1.15.x] + go-version: [1.15.x, 1.16.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/runtime_strip.go b/runtime_strip.go index 718ddd6..51e59cc 100644 --- a/runtime_strip.go +++ b/runtime_strip.go @@ -119,7 +119,20 @@ func stripRuntime(filename string, file *ast.File) { } } - if filename == "print.go" { + switch filename { + case "runtime1.go": + // On Go 1.16.x, the code above results in runtime1.go having an + // unused import. Mark it as used via "var _ = pkg.Func". + // Note that the file in Go 1.15.x does not import bytealg. + // If this is a recurring problem, we could go for a more + // generic solution like x/tools/imports. + for _, imp := range file.Imports { + if imp.Path.Value == `"internal/bytealg"` { + file.Decls = append(file.Decls, markUsedBytealg) + break + } + } + case "print.go": file.Decls = append(file.Decls, hidePrintDecl) return } @@ -142,6 +155,17 @@ func removeImport(importPath string, specs []ast.Spec) []ast.Spec { return specs } +var markUsedBytealg = &ast.GenDecl{ + Tok: token.VAR, + Specs: []ast.Spec{&ast.ValueSpec{ + Names: []*ast.Ident{{Name: "_"}}, + Values: []ast.Expr{&ast.SelectorExpr{ + X: &ast.Ident{Name: "bytealg"}, + Sel: &ast.Ident{Name: "IndexByteString"}, + }}, + }}, +} + var hidePrintDecl = &ast.FuncDecl{ Name: ast.NewIdent("hidePrint"), Type: &ast.FuncType{Params: &ast.FieldList{ diff --git a/testdata/scripts/asm.txt b/testdata/scripts/asm.txt index 47b5b9e..94ac048 100644 --- a/testdata/scripts/asm.txt +++ b/testdata/scripts/asm.txt @@ -1,3 +1,5 @@ +[go1.16] skip 'TODO(mvdan): fix for Go 1.16' + env GOPRIVATE=test/main garble build diff --git a/testdata/scripts/goprivate.txt b/testdata/scripts/goprivate.txt index 24a319a..85a6cf1 100644 --- a/testdata/scripts/goprivate.txt +++ b/testdata/scripts/goprivate.txt @@ -8,6 +8,8 @@ stderr '^public package "test/main/importer" can''t depend on obfuscated package [short] stop +[go1.16] skip 'TODO(mvdan): fix for Go 1.16' + # Try garbling all of std, given some std packages. # No need for a main package here; building the std packages directly works the # same, and is faster as we don't need to link a binary.