#include "stdafx.h" #include "doctest.h" #include "scratch.h" #include "rendering/common/drivercrashguard.h" #include #include #include #include #include #if defined(_MSC_VER) #include #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{file}, std::istreambuf_iterator{}}; } 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(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