mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 10:37:07 +01:00
48b7c679a0
A player's simulator (Vulkan, GeForce RTX 4060, NVIDIA 610.88, through Steam) crashed at every start as the cab loaded: an access violation (read of address 8) inside nvgpucomp64.dll while vkCreateComputePipelines compiled water_snowlayer.comp. The crash came on $baltyk_skm1.scn and $td.scn alike, at the same address in the driver. The snow shaders (shaders/vk14 and shaders/ogl46, the same in both) no longer use what the compute shader alone had: a vector component written at an index known only while running through an inout parameter (snRef), constant arrays of structs and of ints indexed at run time (sn_grids, sn_lump_tiles, the neighbours of snSpan) and a local vec4 indexed by the blade. They are worked out by small functions and explicit components instead (snRefPut, snRefWord, snGrid, snLumpTile, snNear), and the blade is read from the storage as water_snow.comp, which the same driver compiled, does. The results are the same. glslc compiles both variants and spirv-val passes; whether 610.88 compiles them now can't be checked without that driver. So that a driver crashing on a program the renderer can go without doesn't take the simulator down, the programs of the windscreen water are made under gfx::DriverCrashGuard (rendering/common/drivercrashguard.*): - with msvc the fault of the driver is caught (SEH) and the session goes on without the program: the glasses without the snow, or without the simulation of the water; - the program is written down in the cache of the renderer (driver_crash_attempt.txt) before the driver gets it and crossed out after. a program a session ended on is suspect (driver_crash_suspects.txt); when a second session ends on it, or when a fault was caught, it is recorded (driver_crashes.txt) and not made again with that driver. a new driver or a changed program is tried again; - Vulkan makes these pipelines without the VkPipelineCache, whose lock a faulting driver might leave taken (the drivers keep their own cache on the disk). Tested: unit tests (1211/1211, with a caught access violation), Vulkan and OpenGL 4.6 in the cab of 303E on $td.scn (RTX 4070, 621.14), and on Vulkan with a real access violation injected into the pipeline of water_snowlayer.comp: the session went on without the snow, the next one skipped it. No change of the game data.
233 lines
6.4 KiB
C++
233 lines
6.4 KiB
C++
/*
|
|
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 <algorithm>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#if defined(_MSC_VER)
|
|
#include <excpt.h>
|
|
#endif
|
|
|
|
namespace gfx
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
constexpr char separator{'\t'};
|
|
|
|
using Records = std::vector<std::pair<std::string, std::string>>;
|
|
|
|
// 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<std::string, std::string> 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
|