18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 11:47:06 +01:00
Files
maszyna/tests/unit/test_driver_crash_guard.cpp
Hirek 48b7c679a0 Windscreen snow: no crash of the simulator when the driver's compiler faults
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.
2026-11-30 19:39:29 +01:00

165 lines
7.9 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "scratch.h"
#include "rendering/common/drivercrashguard.h"
#include <filesystem>
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <string>
#if defined(_MSC_VER)
#include <windows.h>
#endif
// the programs a graphics driver crashed on while it compiled them (rendering/common/drivercrashguard.h): written down before the
// driver gets them, crossed out after; one whose fault was caught, or one left behind by two sessions the driver took down, isn't
// made again with that driver
namespace {
std::string readFile(std::string const &path) {
std::ifstream file{path};
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
}
struct MakeCall {
int calls{0};
std::string attemptPath;
std::string attemptSeen; // what the attempt file held while the program was made
};
void makeProgram(void *const context) {
auto *const call{static_cast<MakeCall *>(context)};
++call->calls;
call->attemptSeen = readFile(call->attemptPath);
}
void throwFromMake(void *const context) {
makeProgram(context);
throw std::runtime_error("no program");
}
#if defined(_MSC_VER)
void faultInMake(void *const context) {
makeProgram(context);
::RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 0, nullptr);
}
#endif
} // namespace
TEST_CASE("driver crash guard: a program made is written down while the driver has it, then crossed out") {
test::scratch_folder const folder{"driver_crash_guard_made"};
gfx::DriverCrashGuard guard;
guard.Init(folder.path(), "vulkan nvidia 610.88");
MakeCall call{0, folder.path("driver_crash_attempt.txt")};
CHECK(guard.Run("compute pipeline water_snowlayer.comp 0123", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::made);
CHECK(call.calls == 1);
CHECK(call.attemptSeen == "vulkan nvidia 610.88\tcompute pipeline water_snowlayer.comp 0123\n");
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crash_attempt.txt")));
CHECK_FALSE(guard.Crashed("compute pipeline water_snowlayer.comp 0123"));
// and made again the next time
CHECK(guard.Run("compute pipeline water_snowlayer.comp 0123", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::made);
CHECK(call.calls == 2);
}
TEST_CASE("driver crash guard: a session ending once while the driver makes a program leaves it suspect, still made") {
test::scratch_folder const folder{"driver_crash_guard_suspect"};
folder.write("driver_crash_attempt.txt", "vulkan nvidia 610.88\tcompute pipeline water_gather.comp 0123\n");
gfx::DriverCrashGuard guard;
guard.Init(folder.path(), "vulkan nvidia 610.88");
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crash_attempt.txt")));
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crashes.txt")));
CHECK(guard.Suspect("compute pipeline water_gather.comp 0123"));
CHECK_FALSE(guard.Crashed("compute pipeline water_gather.comp 0123"));
// made well, it is no longer suspect, in this session or the next
MakeCall call{0, folder.path("driver_crash_attempt.txt")};
CHECK(guard.Run("compute pipeline water_gather.comp 0123", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::made);
CHECK(call.calls == 1);
CHECK_FALSE(guard.Suspect("compute pipeline water_gather.comp 0123"));
gfx::DriverCrashGuard next;
next.Init(folder.path(), "vulkan nvidia 610.88");
CHECK_FALSE(next.Suspect("compute pipeline water_gather.comp 0123"));
// a leftover of another driver doesn't matter
folder.write("driver_crash_attempt.txt", "vulkan nvidia 600.00\tcompute pipeline water_gather.comp 0123\n");
gfx::DriverCrashGuard updated;
updated.Init(folder.path(), "vulkan nvidia 610.88");
CHECK_FALSE(updated.Suspect("compute pipeline water_gather.comp 0123"));
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crash_attempt.txt")));
}
TEST_CASE("driver crash guard: the program two sessions were making when they ended isn't made again with that driver") {
test::scratch_folder const folder{"driver_crash_guard_attempt"};
folder.write("driver_crash_attempt.txt", "vulkan nvidia 610.88\tcompute pipeline water_snowlayer.comp 0123\n");
gfx::DriverCrashGuard first;
first.Init(folder.path(), "vulkan nvidia 610.88");
CHECK(first.Suspect("compute pipeline water_snowlayer.comp 0123"));
// the second session ends while it makes the program again
folder.write("driver_crash_attempt.txt", "vulkan nvidia 610.88\tcompute pipeline water_snowlayer.comp 0123\n");
gfx::DriverCrashGuard guard;
guard.Init(folder.path(), "vulkan nvidia 610.88");
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crash_attempt.txt")));
CHECK(readFile(folder.path("driver_crashes.txt")) == "vulkan nvidia 610.88\tcompute pipeline water_snowlayer.comp 0123\n");
CHECK(readFile(folder.path("driver_crash_suspects.txt")).empty());
CHECK(guard.Crashed("compute pipeline water_snowlayer.comp 0123"));
CHECK_FALSE(guard.Suspect("compute pipeline water_snowlayer.comp 0123"));
MakeCall call{0, folder.path("driver_crash_attempt.txt")};
CHECK(guard.Run("compute pipeline water_snowlayer.comp 0123", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::skipped);
CHECK(call.calls == 0);
// another program, and the same one changed (another hash), are made
CHECK(guard.Run("compute pipeline water_snow.comp 4567", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::made);
CHECK(guard.Run("compute pipeline water_snowlayer.comp 89ab", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::made);
CHECK(call.calls == 2);
// the records are read again by the next session; another version of the driver tries the program again
gfx::DriverCrashGuard same;
same.Init(folder.path(), "vulkan nvidia 610.88");
CHECK(same.Crashed("compute pipeline water_snowlayer.comp 0123"));
gfx::DriverCrashGuard updated;
updated.Init(folder.path(), "vulkan nvidia 621.14");
CHECK_FALSE(updated.Crashed("compute pipeline water_snowlayer.comp 0123"));
CHECK(updated.Run("compute pipeline water_snowlayer.comp 0123", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::made);
}
TEST_CASE("driver crash guard: an exception of the program goes on through, the program crossed out and not recorded") {
test::scratch_folder const folder{"driver_crash_guard_exception"};
gfx::DriverCrashGuard guard;
guard.Init(folder.path(), "opengl nvidia 610.88");
MakeCall call{0, folder.path("driver_crash_attempt.txt")};
CHECK_THROWS_AS(guard.Run("program water_snowlayer.comp 0123", &throwFromMake, &call), std::runtime_error);
CHECK(call.calls == 1);
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crash_attempt.txt")));
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crashes.txt")));
CHECK_FALSE(guard.Crashed("program water_snowlayer.comp 0123"));
}
TEST_CASE("driver crash guard: tabs and line ends of the names don't break the records") {
test::scratch_folder const folder{"driver_crash_guard_fields"};
folder.write("driver_crashes.txt", "vulkan nvidia 610.88\tprogram a b\r\n");
gfx::DriverCrashGuard guard;
guard.Init(folder.path(), "vulkan\tnvidia\n610.88");
CHECK(guard.Crashed("program\ta\rb"));
}
#if defined(_MSC_VER)
TEST_CASE("driver crash guard: a fault of the driver is caught, recorded and the program not made again") {
test::scratch_folder const folder{"driver_crash_guard_fault"};
gfx::DriverCrashGuard guard;
guard.Init(folder.path(), "vulkan nvidia 610.88");
MakeCall call{0, folder.path("driver_crash_attempt.txt")};
CHECK(guard.Run("compute pipeline water_snowlayer.comp 0123", &faultInMake, &call) == gfx::DriverCrashGuard::Outcome::crashed);
CHECK(call.calls == 1);
CHECK_FALSE(std::filesystem::exists(folder.path("driver_crash_attempt.txt")));
CHECK(readFile(folder.path("driver_crashes.txt")) == "vulkan nvidia 610.88\tcompute pipeline water_snowlayer.comp 0123\n");
CHECK(guard.Run("compute pipeline water_snowlayer.comp 0123", &makeProgram, &call) == gfx::DriverCrashGuard::Outcome::skipped);
CHECK(call.calls == 1);
CHECK_FALSE(gfx::CallCatchingFaults(&faultInMake, &call));
CHECK(gfx::CallCatchingFaults(&makeProgram, &call));
}
#endif