From 47f580fe94bc41a39c010559c78d918d6fabc2db Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Wed, 14 Aug 2019 15:15:21 -0700 Subject: Pass the old wrapper path as a linker argument BUG=chromium:773875 TEST=unit test and comparison to old wrapper Change-Id: I97cff81f2b42a01f82ba66668693b17a27c14672 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1754130 Tested-by: Tobias Bosch Reviewed-by: George Burgess --- compiler_wrapper/build.py | 4 +++- compiler_wrapper/bundle.README | 3 ++- compiler_wrapper/config.go | 26 ++++++++++++++---------- compiler_wrapper/config_test.go | 18 ++++++++++++++++ compiler_wrapper/cros_hardened_config_test.go | 6 +++--- compiler_wrapper/cros_host_config_test.go | 8 ++++---- compiler_wrapper/cros_nonhardened_config_test.go | 4 ++-- compiler_wrapper/main.go | 2 ++ compiler_wrapper/testutil_test.go | 8 ++++---- 9 files changed, 53 insertions(+), 26 deletions(-) diff --git a/compiler_wrapper/build.py b/compiler_wrapper/build.py index 33259281..40e8aa64 100755 --- a/compiler_wrapper/build.py +++ b/compiler_wrapper/build.py @@ -21,6 +21,7 @@ def parse_args(): required=True, choices=['cros.hardened', 'cros.nonhardened', 'cros.host']) parser.add_argument('--use_ccache', required=True, choices=['true', 'false']) + parser.add_argument('--old_wrapper_path', required=True) parser.add_argument('--output_file', required=True, type=str) return parser.parse_args() @@ -30,7 +31,8 @@ def calc_go_args(args): # build a fully static binary in go. ldFlags = [ '-X', 'main.ConfigName=' + args.config, '-X', - 'main.UseCCache=' + args.use_ccache, "-extldflags '-static'" + 'main.UseCCache=' + args.use_ccache, '-X', + 'main.OldWrapperPath=' + args.old_wrapper_path, "-extldflags '-static'" ] return [ 'go', 'build', '-o', diff --git a/compiler_wrapper/bundle.README b/compiler_wrapper/bundle.README index a969d934..530d04ea 100644 --- a/compiler_wrapper/bundle.README +++ b/compiler_wrapper/bundle.README @@ -5,7 +5,8 @@ found in the LICENSE file. Toolchain utils compiler wrapper sources. Build the wrapper: -./build --config= --use_ccache= --output_file= +./build --config= --use_ccache= \ + --old_wrapper_path= --output_file= ATTENTION: The files in this folder are generated. Do not modify manually! diff --git a/compiler_wrapper/config.go b/compiler_wrapper/config.go index 7567d0e2..70d08c15 100644 --- a/compiler_wrapper/config.go +++ b/compiler_wrapper/config.go @@ -29,6 +29,10 @@ type config struct { newWarningsDir string } +// OldWrapperPath can be set via a linker flag. +// Value fills config.oldWrapperPath. +var OldWrapperPath = "" + // UseCCache can be set via a linker flag. // Value will be passed to strconv.ParseBool. // E.g. go build -ldflags '-X config.UseCCache=true'. @@ -46,33 +50,33 @@ func getRealConfig() (*config, error) { if err != nil { return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") } - config, err := getConfig(useCCache, ConfigName) + config, err := getConfig(useCCache, ConfigName, OldWrapperPath) if err != nil { return nil, err } return config, nil } -func getConfig(useCCache bool, configName string) (*config, error) { +func getConfig(useCCache bool, configName string, oldWrapperPath string) (*config, error) { switch configName { case "cros.hardened": - return getCrosHardenedConfig(useCCache), nil + return getCrosHardenedConfig(useCCache, oldWrapperPath), nil case "cros.nonhardened": - return getCrosNonHardenedConfig(useCCache), nil + return getCrosNonHardenedConfig(useCCache, oldWrapperPath), nil case "cros.host": - return getCrosHostConfig(), nil + return getCrosHostConfig(oldWrapperPath), nil default: return nil, newErrorwithSourceLocf("unknown config name: %s", configName) } } // Full hardening. -func getCrosHardenedConfig(useCCache bool) *config { +func getCrosHardenedConfig(useCCache bool, oldWrapperPath string) *config { // Temporarily disable function splitting because of chromium:434751. return &config{ useCCache: useCCache, rootRelPath: "../../../../..", - oldWrapperPath: "./sysroot_wrapper.hardened.old", + oldWrapperPath: oldWrapperPath, commonFlags: []string{ "-fstack-protector-strong", "-fPIE", @@ -104,11 +108,11 @@ func getCrosHardenedConfig(useCCache bool) *config { } // Flags to be added to non-hardened toolchain. -func getCrosNonHardenedConfig(useCCache bool) *config { +func getCrosNonHardenedConfig(useCCache bool, oldWrapperPath string) *config { return &config{ useCCache: useCCache, rootRelPath: "../../../../..", - oldWrapperPath: "./sysroot_wrapper.old", + oldWrapperPath: oldWrapperPath, commonFlags: []string{}, gccFlags: []string{ "-Wno-maybe-uninitialized", @@ -132,12 +136,12 @@ func getCrosNonHardenedConfig(useCCache bool) *config { } // Flags to be added to host toolchain. -func getCrosHostConfig() *config { +func getCrosHostConfig(oldWrapperPath string) *config { return &config{ isHostWrapper: true, useCCache: false, rootRelPath: "../..", - oldWrapperPath: "./host_wrapper.old", + oldWrapperPath: oldWrapperPath, commonFlags: []string{}, gccFlags: []string{ "-Wno-maybe-uninitialized", diff --git a/compiler_wrapper/config_test.go b/compiler_wrapper/config_test.go index 6b2deff7..bdcf5069 100644 --- a/compiler_wrapper/config_test.go +++ b/compiler_wrapper/config_test.go @@ -77,6 +77,23 @@ func TestRealConfigWithConfigNameFlag(t *testing.T) { } } +func TestRealConfigWithOldWrapperPath(t *testing.T) { + resetGlobals() + defer resetGlobals() + UseCCache = "false" + ConfigName = "cros.hardened" + + OldWrapperPath = "somepath" + + cfg, err := getRealConfig() + if err != nil { + t.Fatal(err) + } + if cfg.oldWrapperPath != "somepath" { + t.Fatalf("OldWrapperPath: Expected somepath, got %s", cfg.oldWrapperPath) + } +} + func isSysrootHardened(cfg *config) bool { for _, arg := range cfg.commonFlags { if arg == "-pie" { @@ -88,6 +105,7 @@ func isSysrootHardened(cfg *config) bool { func resetGlobals() { // Set all global variables to a defined state. + OldWrapperPath = "" ConfigName = "unknown" UseCCache = "unknown" } diff --git a/compiler_wrapper/cros_hardened_config_test.go b/compiler_wrapper/cros_hardened_config_test.go index 5ae2f7e1..b87320b5 100644 --- a/compiler_wrapper/cros_hardened_config_test.go +++ b/compiler_wrapper/cros_hardened_config_test.go @@ -14,14 +14,14 @@ import ( "testing" ) -const oldHardenedWrapperPathForTest = "/usr/x86_64-pc-linux-gnu/x86_64-cros-linux-gnu/gcc-bin/4.9.x/sysroot_wrapper.hardened" +const oldHardenedWrapperPathForTest = "$CHROOT/usr/x86_64-pc-linux-gnu/x86_64-cros-linux-gnu/gcc-bin/4.9.x/sysroot_wrapper.hardened" const crosHardenedGoldenDir = "testdata/cros_hardened_golden" const crosHardenedNoCCacheGoldenDir = "testdata/cros_hardened_noccache_golden" func TestCrosHardenedConfig(t *testing.T) { withTestContext(t, func(ctx *testContext) { useCCache := true - ctx.updateConfig(oldHardenedWrapperPathForTest, getCrosHardenedConfig(useCCache)) + ctx.updateConfig(getCrosHardenedConfig(useCCache, oldHardenedWrapperPathForTest)) runGoldenRecords(ctx, crosHardenedGoldenDir, createSyswrapperGoldenInputs(ctx)) }) @@ -30,7 +30,7 @@ func TestCrosHardenedConfig(t *testing.T) { func TestCrosHardenedConfigWithoutCCache(t *testing.T) { withTestContext(t, func(ctx *testContext) { useCCache := false - ctx.updateConfig(oldHardenedWrapperPathForTest, getCrosHardenedConfig(useCCache)) + ctx.updateConfig(getCrosHardenedConfig(useCCache, oldHardenedWrapperPathForTest)) // Create a copy of the old wrapper where the CCACHE_DEFAULT is false. if ctx.cfg.oldWrapperPath != "" { diff --git a/compiler_wrapper/cros_host_config_test.go b/compiler_wrapper/cros_host_config_test.go index a68cedd7..61f1b867 100644 --- a/compiler_wrapper/cros_host_config_test.go +++ b/compiler_wrapper/cros_host_config_test.go @@ -9,14 +9,14 @@ import ( "testing" ) -const oldClangHostWrapperPathForTest = "/usr/bin/clang_host_wrapper" -const oldGccHostWrapperPathForTest = "../src/third_party/chromiumos-overlay/sys-devel/gcc/files/host_wrapper" +const oldClangHostWrapperPathForTest = "$CHROOT/usr/bin/clang_host_wrapper" +const oldGccHostWrapperPathForTest = "$CHROOT/../src/third_party/chromiumos-overlay/sys-devel/gcc/files/host_wrapper" const crosClangHostGoldenDir = "testdata/cros_clang_host_golden" const crosGccHostGoldenDir = "testdata/cros_gcc_host_golden" func TestCrosClangHostConfig(t *testing.T) { withTestContext(t, func(ctx *testContext) { - ctx.updateConfig(oldClangHostWrapperPathForTest, getCrosHostConfig()) + ctx.updateConfig(getCrosHostConfig(oldClangHostWrapperPathForTest)) gomaPath := path.Join(ctx.tempDir, "gomacc") ctx.writeFile(gomaPath, "") @@ -39,7 +39,7 @@ func TestCrosClangHostConfig(t *testing.T) { func TestCrosGccHostConfig(t *testing.T) { withTestContext(t, func(ctx *testContext) { - ctx.updateConfig(oldGccHostWrapperPathForTest, getCrosHostConfig()) + ctx.updateConfig(getCrosHostConfig(oldGccHostWrapperPathForTest)) gomaPath := path.Join(ctx.tempDir, "gomacc") ctx.writeFile(gomaPath, "") diff --git a/compiler_wrapper/cros_nonhardened_config_test.go b/compiler_wrapper/cros_nonhardened_config_test.go index 41e3d7c9..e876d309 100644 --- a/compiler_wrapper/cros_nonhardened_config_test.go +++ b/compiler_wrapper/cros_nonhardened_config_test.go @@ -8,13 +8,13 @@ import ( "testing" ) -const oldNonHardenedWrapperPathForTest = "/usr/x86_64-pc-linux-gnu/arm-none-eabi/gcc-bin/4.9.x/sysroot_wrapper" +const oldNonHardenedWrapperPathForTest = "$CHROOT/usr/x86_64-pc-linux-gnu/arm-none-eabi/gcc-bin/4.9.x/sysroot_wrapper" const crosNonHardenedGoldenDir = "testdata/cros_nonhardened_golden" func TestCrosNonHardenedConfig(t *testing.T) { withTestContext(t, func(ctx *testContext) { useCCache := true - ctx.updateConfig(oldNonHardenedWrapperPathForTest, getCrosNonHardenedConfig(useCCache)) + ctx.updateConfig(getCrosNonHardenedConfig(useCCache, oldNonHardenedWrapperPathForTest)) runGoldenRecords(ctx, crosNonHardenedGoldenDir, createSyswrapperGoldenInputs(ctx)) }) diff --git a/compiler_wrapper/main.go b/compiler_wrapper/main.go index 780e9155..3fcf3a66 100644 --- a/compiler_wrapper/main.go +++ b/compiler_wrapper/main.go @@ -6,6 +6,8 @@ // - main.UseCCache: Whether to use ccache. // - main.ConfigName: Name of the configuration to use. // See config.go for the supported values. +// - main.OldWrapperPath: Path to the old wrapper to compare commands +// against. Comparison is deactivated if empty. // // The script ./build simplifies the call to `go build`. // E.g. ./build --use_ccache=true --config=cros.hardened will build a diff --git a/compiler_wrapper/testutil_test.go b/compiler_wrapper/testutil_test.go index fa321a07..0dbf817e 100644 --- a/compiler_wrapper/testutil_test.go +++ b/compiler_wrapper/testutil_test.go @@ -58,7 +58,7 @@ func withTestContext(t *testing.T, work func(ctx *testContext)) { env: nil, cfg: &config{}, } - ctx.updateConfig("", &config{}) + ctx.updateConfig(&config{}) work(&ctx) } @@ -140,12 +140,12 @@ func (ctx *testContext) mustFail(exitCode int) string { return ctx.stderrString() } -func (ctx *testContext) updateConfig(wrapperChrootPath string, cfg *config) { +func (ctx *testContext) updateConfig(cfg *config) { *ctx.cfg = *cfg ctx.cfg.mockOldWrapperCmds = true ctx.cfg.newWarningsDir = filepath.Join(ctx.tempDir, "fatal_clang_warnings") - if *crosRootDirFlag != "" && wrapperChrootPath != "" { - ctx.cfg.oldWrapperPath = filepath.Join(*crosRootDirFlag, wrapperChrootPath) + if *crosRootDirFlag != "" && ctx.cfg.oldWrapperPath != "" { + ctx.cfg.oldWrapperPath = strings.Replace(ctx.cfg.oldWrapperPath, "$CHROOT", *crosRootDirFlag, -1) } else { ctx.cfg.oldWrapperPath = "" } -- cgit v1.2.3