summaryrefslogtreecommitdiffstats
path: root/llvm/tools
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2017-08-10 17:38:41 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2017-08-10 17:38:41 +0000
commit74fbf45f4c92d2c22e6b05b8e4659e47cf6620e1 (patch)
treed164d399a885253b596802c6485f25aa769876c4 /llvm/tools
parentfe67612eba9d58471fa832b378c536e77763a29c (diff)
downloadbcm5719-llvm-74fbf45f4c92d2c22e6b05b8e4659e47cf6620e1.tar.gz
bcm5719-llvm-74fbf45f4c92d2c22e6b05b8e4659e47cf6620e1.zip
[gold-plugin] Avoid race condition when creating temporary files.
This is both a potential security issue and a potential functionality issue because we create temporary files from multiple threads. Use the safe version of createTemporaryFile instead. llvm-svn: 310636
Diffstat (limited to 'llvm/tools')
-rw-r--r--llvm/tools/gold/gold-plugin.cpp23
1 files changed, 12 insertions, 11 deletions
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index 825fff1aa38..3123cde7950 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -699,11 +699,12 @@ static void recordFile(std::string Filename, bool TempOutFile) {
/// Return the desired output filename given a base input name, a flag
/// indicating whether a temp file should be generated, and an optional task id.
/// The new filename generated is returned in \p NewFilename.
-static void getOutputFileName(SmallString<128> InFilename, bool TempOutFile,
- SmallString<128> &NewFilename, int TaskID) {
+static int getOutputFileName(SmallString<128> InFilename, bool TempOutFile,
+ SmallString<128> &NewFilename, int TaskID) {
+ int FD = -1;
if (TempOutFile) {
std::error_code EC =
- sys::fs::createTemporaryFile("lto-llvm", "o", NewFilename);
+ sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
if (EC)
message(LDPL_FATAL, "Could not create temporary file: %s",
EC.message().c_str());
@@ -711,7 +712,13 @@ static void getOutputFileName(SmallString<128> InFilename, bool TempOutFile,
NewFilename = InFilename;
if (TaskID > 0)
NewFilename += utostr(TaskID);
+ std::error_code EC =
+ sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None);
+ if (EC)
+ message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(),
+ EC.message().c_str());
}
+ return FD;
}
static CodeGenOpt::Level getCGOptLevel() {
@@ -899,14 +906,8 @@ static ld_plugin_status allSymbolsReadHook() {
auto AddStream =
[&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
IsTemporary[Task] = !SaveTemps;
- getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, Filenames[Task],
- Task);
- int FD;
- std::error_code EC =
- sys::fs::openFileForWrite(Filenames[Task], FD, sys::fs::F_None);
- if (EC)
- message(LDPL_FATAL, "Could not open file %s: %s", Filenames[Task].c_str(),
- EC.message().c_str());
+ int FD = getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps,
+ Filenames[Task], Task);
return llvm::make_unique<lto::NativeObjectStream>(
llvm::make_unique<llvm::raw_fd_ostream>(FD, true));
};
OpenPOWER on IntegriCloud