Skip to content

Commit e7e2842

Browse files
committed
[Timer] initialize timer options before group
Ensures timer options are initialized before NamedGroupTimers. A crash can occur if a user does bitcode parsing before initialization of command-line options.
1 parent ac4bd74 commit e7e2842

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

llvm/lib/Support/Timer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,25 @@ const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
501501

502502
void TimerGroup::constructForStatistics() {
503503
(void)getLibSupportInfoOutputFilename();
504+
// Make sure initTimerOptions() is called here, and before NamedGroupTimers is
505+
// initialized. Rationale:
506+
//
507+
// TimerGroup will print itself on destruction, and its destructor accesses
508+
// one of the timer options (SortTimers) to determine how to do this:
509+
// ~TimerGroup::TimerGroup()
510+
// -> TimerGroup::removeTimer()
511+
// -> TimerGroup::PrintQueuedTimers()
512+
// -> if (*SortTimers) llvm::sort(TimersToPrint);
513+
//
514+
// It's possible the timer options may not be initialized at this point. This
515+
// could happen if a client of LLVM calls the bitcode parser before
516+
// initializing command-line options, for example. Inside the parser are
517+
// statistic counters, which when incremented will initialize the TimerGroup
518+
// here. If NamedGroupTimers is created before timer options, it will later be
519+
// destroyed AFTER the timer options (ManagedStatics are destroyed in reverse
520+
// order), causing a crash when attempting to access the already-destroyed
521+
// timer option.
522+
initTimerOptions();
504523
(void)*NamedGroupedTimers;
505524
}
506525

0 commit comments

Comments
 (0)