Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/support/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "support/utilities.h"
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <ostream>
#include <string>
#include <vector>
Expand Down
28 changes: 28 additions & 0 deletions src/support/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,39 @@
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <mutex>

#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#include "sanitizer/common_interface_defs.h"
#endif

#ifndef THROW_ON_FATAL
#ifdef __LLVM_INSTR_PROFILE_GENERATE
[[noreturn]] void wasm::Fatal::exit(const std::string& msg,
int (*flushCallerProfile)()) {
#else
[[noreturn]] void wasm::Fatal::exit(const std::string& msg) {
#endif
// Serialize fatal exits so concurrent worker threads do not interleave
// output or race while flushing instrumentation profiles before _Exit.
static std::mutex fatalMutex;
std::lock_guard<std::mutex> lock(fatalMutex);
std::cerr << msg << std::endl;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the mutex out of the ifdef beginning on line 40? Do we need it for some reason other than LLVM's profile writing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is that what "interleave output" means? So the idea is to guard against parallel Fatals?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Not necessary when considering just the use case of ensuring consistent profile writes, but seems like a nice improvement at best and harmless at worst.

#ifdef __LLVM_INSTR_PROFILE_GENERATE
// Flush this library's profile runtime and, if distinct, the caller's profile
// runtime before _Exit bypasses exit handlers.
__llvm_profile_write_file();
if (flushCallerProfile != __llvm_profile_write_file) {

Check warning on line 44 in src/support/utilities.cpp

View workflow job for this annotation

GitHub Actions / coverage

One-sided branch in diff

Branch `flushCallerProfile != __llvm_profile_write_file` -> missing TRUE branch (True: 0x, False: 165x)
flushCallerProfile();
}

Check warning on line 46 in src/support/utilities.cpp

View workflow job for this annotation

GitHub Actions / coverage

Uncovered diff lines

Lines 45-46 not executed by tests (0x)
#endif
// Use _Exit here to avoid calling static destructors. This avoids deadlocks
// in (for example) the thread worker pool, where workers hold a lock while
// performing their work.
_Exit(EXIT_FAILURE);
}
#endif

[[noreturn]] void
wasm::handle_unreachable(const char* msg, const char* file, unsigned line) {
#ifndef NDEBUG
Expand Down
29 changes: 20 additions & 9 deletions src/support/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
#include "compiler-support.h"

#include <cassert>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <memory>
#include <sstream>
#include <type_traits>

#include "support/bits.h"
#ifdef __LLVM_INSTR_PROFILE_GENERATE
extern "C" int __llvm_profile_write_file();
#endif

namespace wasm {

Expand All @@ -52,6 +51,18 @@ class Fatal {
private:
std::stringstream buffer;

#ifndef THROW_ON_FATAL
#ifdef __LLVM_INSTR_PROFILE_GENERATE
// Pass the caller's profile flush function because shared-library builds link
// separate hidden copies of the profile runtime into the library and each
// executable.
[[noreturn]] static void exit(const std::string& msg,
int (*flushCallerProfile)());
#else
[[noreturn]] static void exit(const std::string& msg);
#endif
#endif

public:
Fatal() { buffer << "Fatal: "; }
template<typename T> Fatal& operator<<(T&& arg) {
Expand All @@ -60,11 +71,11 @@ class Fatal {
}
#ifndef THROW_ON_FATAL
[[noreturn]] ~Fatal() {
std::cerr << buffer.str() << std::endl;
// Use _Exit here to avoid calling static destructors. This avoids deadlocks
// in (for example) the thread worker pool, where workers hold a lock while
// performing their work.
_Exit(EXIT_FAILURE);
#ifdef __LLVM_INSTR_PROFILE_GENERATE
exit(buffer.str(), __llvm_profile_write_file);
#else
exit(buffer.str());
#endif
}
#else
// This variation is a best-effort attempt to make fatal errors recoverable
Expand Down
Loading