Skip to content

Conversation

@Prince781
Copy link
Contributor

Ensures timer options are initialized before NamedGroupTimers. A crash can occur if a user does bitcode parsing before initialization of command-line options.

Ensures timer options are initialized before NamedGroupTimers. A crash
can occur if a user does bitcode parsing before initialization of
command-line options.
@llvmbot
Copy link
Member

llvmbot commented Oct 25, 2024

@llvm/pr-subscribers-llvm-support

Author: Princeton Ferro (Prince781)

Changes

Ensures timer options are initialized before NamedGroupTimers. A crash can occur if a user does bitcode parsing before initialization of command-line options.


Full diff: https://github.com/llvm/llvm-project/pull/113719.diff

1 Files Affected:

  • (modified) llvm/lib/Support/Timer.cpp (+19)
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index c1b0fdbc077bb0..e8db0d42c4dbfe 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -501,6 +501,25 @@ const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
 
 void TimerGroup::constructForStatistics() {
   (void)getLibSupportInfoOutputFilename();
+  // Make sure initTimerOptions() is called here, and before NamedGroupTimers is
+  // initialized. Rationale:
+  //
+  // TimerGroup will print itself on destruction, and its destructor accesses
+  // one of the timer options (SortTimers) to determine how to do this:
+  // ~TimerGroup::TimerGroup()
+  //   -> TimerGroup::removeTimer()
+  //   -> TimerGroup::PrintQueuedTimers()
+  //   -> if (*SortTimers) llvm::sort(TimersToPrint);
+  //
+  // It's possible the timer options may not be initialized at this point. This
+  // could happen if a client of LLVM calls the bitcode parser before
+  // initializing command-line options, for example. Inside the parser are
+  // statistic counters, which when incremented will initialize the TimerGroup
+  // here. If NamedGroupTimers is created before timer options, it will later be
+  // destroyed AFTER the timer options (ManagedStatics are destroyed in reverse
+  // order), causing a crash when attempting to access the already-destroyed
+  // timer option.
+  initTimerOptions();
   (void)*NamedGroupedTimers;
 }
 

@Prince781
Copy link
Contributor Author

Ping.

@Prince781
Copy link
Contributor Author

@nikic these failures look unrelated to my change. Would it be possible for you to assign someone to review this?

@nikic
Copy link
Contributor

nikic commented Nov 13, 2024

A crash can occur if a user does bitcode parsing before initialization of command-line options.

I don't think this is supported? The bitcode parser also uses command-line options, right?

@Prince781
Copy link
Contributor Author

Issue seems to be resolved in latest LLVM, so I'm closing this PR.

@Prince781 Prince781 closed this Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants