/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" #include "rendering/common/drivercrashguard.h" #include "utilities/Logs.h" #include #include #include #include #include #include #if defined(_MSC_VER) #include #endif namespace gfx { namespace { constexpr char separator{'\t'}; using Records = std::vector>; // the text as one field of a line of the records std::string oneField(std::string_view const text) { std::string result{text}; std::ranges::replace_if(result, [](char const c) { return (c == '\t') || (c == '\n') || (c == '\r'); }, ' '); return result; } // the lines of the records, the driver and the program; nothing when there's no file Records readRecords(std::string const &path) { Records result; std::ifstream file(path); std::string line; while (std::getline(file, line)) { if ((false == line.empty()) && (line.back() == '\r')) line.pop_back(); auto const tab{line.find(separator)}; if ((tab == std::string::npos) || (tab + 1 == line.size())) continue; result.emplace_back(line.substr(0, tab), line.substr(tab + 1)); } return result; } void appendRecord(std::string const &path, std::string_view const driver, std::string_view const key) { if (path.empty()) return; std::ofstream file(path, std::ios::app); file << driver << separator << key << '\n'; } #if defined(_MSC_VER) // the faults caught: the structured exceptions but those of c++ (thrown on) and the overflow of the stack (the guard page is gone // after it, the thread can't go on) int faultFilter(unsigned long const code) { constexpr unsigned long cppException{0xE06D7363ul}; constexpr unsigned long stackOverflow{0xC00000FDul}; return ((code == cppException) || (code == stackOverflow)) ? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_EXECUTE_HANDLER; } #endif } // namespace void DriverCrashGuard::Init(std::string_view const directory, std::string_view const driverName) { std::scoped_lock lock(mutex); std::string const folder{directory}; std::error_code error; std::filesystem::create_directories(std::filesystem::path(folder), error); recordsPath = folder + "driver_crashes.txt"; suspectsPath = folder + "driver_crash_suspects.txt"; attemptPath = folder + "driver_crash_attempt.txt"; driver = oneField(driverName); crashedKeys.clear(); suspectKeys.clear(); for (auto const &[recordDriver, key] : readRecords(recordsPath)) { if (recordDriver == driver) crashedKeys.insert(key); } for (auto const &[recordDriver, key] : readRecords(suspectsPath)) { if (recordDriver == driver) suspectKeys.insert(key); } // the program the session before was making when it ended (one of another driver no longer matters) for (auto const &[attemptDriver, key] : readRecords(attemptPath)) { if ((attemptDriver != driver) || crashedKeys.contains(key)) continue; if (suspectKeys.contains(key)) { ErrorLog(std::format("driver crash guard: the session before ended again while the driver ({}) made {}; it isn't made again with this driver ({} lists it)", driver, key, recordsPath)); recordCrash(key); continue; } suspectKeys.insert(key); appendRecord(suspectsPath, driver, key); WriteLog(std::format("driver crash guard: the session before ended while the driver ({}) made {}; if a session ends there again, it isn't made with this driver", driver, key)); } removeAttempt(); } DriverCrashGuard::Outcome DriverCrashGuard::Run(std::string_view const key, void (*make)(void *), void *context) { std::scoped_lock lock(mutex); std::string const name{oneField(key)}; if (crashedKeys.contains(name)) { ErrorLog(std::format("driver crash guard: {} isn't made, the driver ({}) crashed on it before ({} lists it)", name, driver, recordsPath)); return Outcome::skipped; } writeAttempt(name); bool faulted{false}; try { faulted = (false == CallCatchingFaults(make, context)); } catch (...) { removeAttempt(); throw; } if (faulted) { ErrorLog(std::format("driver crash guard: the driver ({}) crashed while it made {}; the simulator goes on without it and doesn't make it again with this driver ({} lists it)", driver, name, recordsPath)); recordCrash(name); } else { clearSuspect(name); } removeAttempt(); return (faulted ? Outcome::crashed : Outcome::made); } bool DriverCrashGuard::Crashed(std::string_view const key) const { std::scoped_lock lock(mutex); return crashedKeys.contains(oneField(key)); } bool DriverCrashGuard::Suspect(std::string_view const key) const { std::scoped_lock lock(mutex); return suspectKeys.contains(oneField(key)); } void DriverCrashGuard::writeAttempt(std::string const &key) const { if (attemptPath.empty()) return; // closed before the driver gets the program: what a crash of the process leaves behind is then with the system already std::ofstream file(attemptPath, std::ios::trunc); file << driver << separator << key << '\n'; } void DriverCrashGuard::removeAttempt() const { if (attemptPath.empty()) return; std::error_code error; std::filesystem::remove(attemptPath, error); } void DriverCrashGuard::recordCrash(std::string const &key) { crashedKeys.insert(key); appendRecord(recordsPath, driver, key); clearSuspect(key); } void DriverCrashGuard::clearSuspect(std::string const &key) { if (0 == suspectKeys.erase(key)) return; if (suspectsPath.empty()) return; // the file written anew without the program of this driver auto records{readRecords(suspectsPath)}; std::erase_if(records, [this, &key](std::pair const &record) { return (record.first == driver) && (record.second == key); }); std::ofstream file(suspectsPath, std::ios::trunc); for (auto const &[recordDriver, recordKey] : records) { file << recordDriver << separator << recordKey << '\n'; } } #if defined(_MSC_VER) bool CallCatchingFaults(void (*function)(void *), void *context) { // no object with a destructor in this function: msvc doesn't take __try where one would have to be unwound __try { function(context); } __except (faultFilter(GetExceptionCode())) { return false; } return true; } #else bool CallCatchingFaults(void (*function)(void *), void *context) { function(context); return true; } #endif } // namespace gfx