From 3346c0918bb939f3e82033ddde0a2378c602650d Mon Sep 17 00:00:00 2001 From: Tom Scogland Date: Sat, 5 Nov 2022 00:09:35 -1000 Subject: Fix relocation to avoid overwriting merged constant strings (#32253) Compilers and linker optimize string constants for space by aliasing them when one is a suffix of another. For gcc / binutils this happens already at -O1, due to -fmerge-constants. This means that we have to take care during relocation to always preserve a certain length of the suffix of those prefixes that are C-strings. In this commit we pick length 7 as a safe suffix length, assuming the suffix is typically the 7 characters from the hash (i.e. random), so it's unlikely to alias with any string constant used in the sources. In general we now pad shortened strings from the left with leading dir seperators, but in the case of C-strings that are much shorter and don't share a common suffix (due to projections), we do allow shrinking the C-string, appending a null, and retaining the old part of the prefix. Also when rewiring, we ensure that the new hash preserves the last 7 bytes of the old hash. Co-authored-by: Harmen Stoppels --- var/spack/repos/builtin.mock/packages/garply/package.py | 7 ++++++- var/spack/repos/builtin.mock/packages/quux/package.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'var') diff --git a/var/spack/repos/builtin.mock/packages/garply/package.py b/var/spack/repos/builtin.mock/packages/garply/package.py index 819da45286..7fc3fd484a 100644 --- a/var/spack/repos/builtin.mock/packages/garply/package.py +++ b/var/spack/repos/builtin.mock/packages/garply/package.py @@ -83,7 +83,12 @@ const int garply_version_minor = %s; f.write(garply_cc % prefix.config) with open("%s/garply/garplinator.cc" % self.stage.source_path, "w") as f: f.write(garplinator_cc) - gpp = which("/usr/bin/g++") + gpp = which( + "g++", + path=":".join( + [s for s in os.environ["PATH"].split(os.pathsep) if "lib/spack/env" not in s] + ), + ) if sys.platform == "darwin": gpp = which("/usr/bin/clang++") gpp( diff --git a/var/spack/repos/builtin.mock/packages/quux/package.py b/var/spack/repos/builtin.mock/packages/quux/package.py index 693ef07ba4..de4f8b9327 100644 --- a/var/spack/repos/builtin.mock/packages/quux/package.py +++ b/var/spack/repos/builtin.mock/packages/quux/package.py @@ -97,7 +97,12 @@ const int quux_version_minor = %s; f.write(quux_h) with open("%s/quux/quuxifier.cc" % self.stage.source_path, "w") as f: f.write(quuxifier_cc) - gpp = which("/usr/bin/g++") + gpp = which( + "g++", + path=":".join( + [s for s in os.environ["PATH"].split(os.pathsep) if "lib/spack/env" not in s] + ), + ) if sys.platform == "darwin": gpp = which("/usr/bin/clang++") gpp( -- cgit v1.2.3-60-g2f50