Skip to content

Commit 4f41994

Browse files
committed
[flang] Fix race condition in runtime
The code that initializes the default units 5 & 6 had a race condition that would allow threads access to the unit map before it had been populated. Also add some missing calls to va_end() that will never be called (they're in program abort situations) but might elicit warnings if absent. Differential Revision: https://reviews.llvm.org/D101928
1 parent 22aece5 commit 4f41994

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

flang/runtime/io-error.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ void IoErrorHandler::SignalError(int iostatOrErrno, const char *msg, ...) {
3636
va_start(ap, msg);
3737
std::vsnprintf(buffer, sizeof buffer, msg, ap);
3838
ioMsg_ = SaveDefaultCharacter(buffer, std::strlen(buffer) + 1, *this);
39+
va_end(ap);
3940
}
4041
}
4142
} else if (msg) {
4243
va_list ap;
4344
va_start(ap, msg);
4445
CrashArgs(msg, ap);
46+
va_end(ap);
4547
} else if (const char *errstr{IostatErrorString(iostatOrErrno)}) {
4648
Crash(errstr);
4749
} else {

flang/runtime/terminator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Fortran::runtime {
1616
va_list ap;
1717
va_start(ap, message);
1818
CrashArgs(message, ap);
19+
va_end(ap);
1920
}
2021

2122
static void (*crashHandler)(const char *, int, const char *, va_list &){

flang/runtime/unit.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,20 @@ UnitMap &ExternalFileUnit::GetUnitMap() {
205205
}
206206
Terminator terminator{__FILE__, __LINE__};
207207
IoErrorHandler handler{terminator};
208-
unitMap = New<UnitMap>{terminator}().release();
209-
ExternalFileUnit &out{ExternalFileUnit::CreateNew(6, terminator)};
208+
UnitMap *newUnitMap{New<UnitMap>{terminator}().release()};
209+
bool wasExtant{false};
210+
ExternalFileUnit &out{newUnitMap->LookUpOrCreate(6, terminator, wasExtant)};
211+
RUNTIME_CHECK(terminator, !wasExtant);
210212
out.Predefine(1);
211213
out.SetDirection(Direction::Output, handler);
212214
defaultOutput = &out;
213-
ExternalFileUnit &in{ExternalFileUnit::CreateNew(5, terminator)};
215+
ExternalFileUnit &in{newUnitMap->LookUpOrCreate(5, terminator, wasExtant)};
216+
RUNTIME_CHECK(terminator, !wasExtant);
214217
in.Predefine(0);
215218
in.SetDirection(Direction::Input, handler);
216219
defaultInput = &in;
217220
// TODO: Set UTF-8 mode from the environment
221+
unitMap = newUnitMap;
218222
return *unitMap;
219223
}
220224

0 commit comments

Comments
 (0)