Skip to content

Commit e757c4d

Browse files
jhuber6tstellar
authored andcommitted
[LinkerWrapper] Fix memory issues due to unguarded accesses to global state
There were intemittent errors in the linker wrapper when using the sanitizers in parallel. First, this is because the `TempFiles` global was not guarded when creating a new file. Second, even though the `Args` list is passed as const, the internal state is mutable when adding a string. So that needs to be guarded too. Fixes #60437 Reviewed By: tianshilei1992 Differential Revision: https://reviews.llvm.org/D142985 (cherry picked from commit 9c4591d)
1 parent 66c1717 commit e757c4d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ static StringRef ExecutableName;
7777
/// Binary path for the CUDA installation.
7878
static std::string CudaBinaryPath;
7979

80+
/// Mutex lock to protect writes to shared TempFiles in parallel.
81+
static std::mutex TempFilesMutex;
82+
8083
/// Temporary files created by the linker wrapper.
8184
static std::list<SmallString<128>> TempFiles;
8285

@@ -200,6 +203,7 @@ std::string getMainExecutable(const char *Name) {
200203

201204
/// Get a temporary filename suitable for output.
202205
Expected<StringRef> createOutputFile(const Twine &Prefix, StringRef Extension) {
206+
std::scoped_lock<decltype(TempFilesMutex)> Lock(TempFilesMutex);
203207
SmallString<128> OutputFile;
204208
if (SaveTemps) {
205209
(Prefix + "." + Extension).toNullTerminatedStringRef(OutputFile);
@@ -1047,6 +1051,7 @@ linkAndWrapDeviceFiles(SmallVectorImpl<OffloadFile> &LinkerInputFiles,
10471051
return createFileError(*OutputOrErr, EC);
10481052
}
10491053

1054+
std::scoped_lock<decltype(ImageMtx)> Guard(ImageMtx);
10501055
OffloadingImage TheImage{};
10511056
TheImage.TheImageKind =
10521057
Args.hasArg(OPT_embed_bitcode) ? IMG_Bitcode : IMG_Object;
@@ -1058,7 +1063,6 @@ linkAndWrapDeviceFiles(SmallVectorImpl<OffloadFile> &LinkerInputFiles,
10581063
Args.MakeArgString(LinkerArgs.getLastArgValue(OPT_arch_EQ))}};
10591064
TheImage.Image = std::move(*FileOrErr);
10601065

1061-
std::lock_guard<decltype(ImageMtx)> Guard(ImageMtx);
10621066
Images[Kind].emplace_back(std::move(TheImage));
10631067
}
10641068
return Error::success();

0 commit comments

Comments
 (0)