Merge pull request #8814 from hercules-ci/exception-self-check

initLibUtil: Add exception handling self-check
This commit is contained in:
John Ericson 2023-08-25 10:15:54 -04:00 committed by GitHub
commit cbd89568ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View file

@ -14,6 +14,11 @@ void BaseError::addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint, boo
err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .frame = frame }); err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .frame = frame });
} }
void throwExceptionSelfCheck(){
// This is meant to be caught in initLibUtil()
throw SysError("C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded.");
}
// c++ std::exception descendants must have a 'const char* what()' function. // c++ std::exception descendants must have a 'const char* what()' function.
// This stringifies the error and caches it for use by what(), or similarly by msg(). // This stringifies the error and caches it for use by what(), or similarly by msg().
const std::string & BaseError::calcWhat() const const std::string & BaseError::calcWhat() const

View file

@ -214,4 +214,8 @@ public:
} }
}; };
/** Throw an exception for the purpose of checking that exception handling works; see 'initLibUtil()'.
*/
void throwExceptionSelfCheck();
} }

View file

@ -48,6 +48,23 @@ extern char * * environ __attribute__((weak));
namespace nix { namespace nix {
void initLibUtil() { void initLibUtil() {
// Check that exception handling works. Exception handling has been observed
// not to work on darwin when the linker flags aren't quite right.
// In this case we don't want to expose the user to some unrelated uncaught
// exception, but rather tell them exactly that exception handling is
// broken.
// When exception handling fails, the message tends to be printed by the
// C++ runtime, followed by an abort.
// For example on macOS we might see an error such as
// libc++abi: terminating with uncaught exception of type nix::SysError: error: C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded.
bool caught = false;
try {
throwExceptionSelfCheck();
} catch (nix::Error _e) {
caught = true;
}
// This is not actually the main point of this check, but let's make sure anyway:
assert(caught);
} }
std::optional<std::string> getEnv(const std::string & key) std::optional<std::string> getEnv(const std::string & key)