diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 596ad0ae92..52e3fe8888 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h @@ -15,12 +15,13 @@ #include #include #include +#include #include #include #include #include -#include #include +#include #include #include #include @@ -28,6 +29,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + namespace leonida { // --------------------------------------------------------------------------------------------------------------- @@ -982,12 +987,22 @@ class ShaderLibrary { template bool load_index(const std::filesystem::path& dir, const std::string& prefix, Fail&& fail) { const auto index = dir / "index.json"; - std::ifstream file(index, std::ios::binary); +#ifdef _WIN32 + auto* handle = _wfsopen(index.c_str(), L"rb", _SH_DENYNO); +#else + auto* handle = std::fopen(index.c_str(), "rb"); +#endif + const auto close = [](std::FILE* f) { std::fclose(f); }; + std::unique_ptr file(handle, close); if (!file) { const auto path = index.u8string(); return fail("cannot open " + std::string(path.begin(), path.end())); } - const std::string text((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + std::string text; + char block[16 * 1024]; + while (const auto count = std::fread(block, 1, sizeof block, file.get())) + text.append(block, count); + if (std::ferror(file.get())) return fail("index.json could not be read"); constexpr std::string_view utf8_bom = "\xef\xbb\xbf"; std::string_view json(text); if (json.starts_with(utf8_bom)) json.remove_prefix(utf8_bom.size()); diff --git a/runtime/test/shader_library_test.cpp b/runtime/test/shader_library_test.cpp index 55105d987f..65ac6dd018 100644 --- a/runtime/test/shader_library_test.cpp +++ b/runtime/test/shader_library_test.cpp @@ -13,6 +13,13 @@ #include #include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif + #define CHECK(condition) do { \ if (!(condition)) { \ std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ @@ -633,6 +640,33 @@ int main() { std::filesystem::create_directories(dir); const std::string hash = sha256_hex(skewed(600)); const std::string hash2 = sha256_hex(bytes("another program")); +#ifdef _WIN32 + // A complete JSON object in the readable prefix must not hide a read error in the tail. + { + const auto locked_dir = dir / "locked-tail"; + std::filesystem::create_directory(locked_dir); + const auto path = locked_dir / "index.json"; + constexpr DWORD lock_offset = 64 * 1024; + { + std::ofstream index(path, std::ios::binary); + index << "{}" << std::string(lock_offset - 2, ' ') << '!'; + CHECK(index.good()); + } + const HANDLE lock = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + CHECK(lock != INVALID_HANDLE_VALUE); + OVERLAPPED range{}; + range.Offset = lock_offset; + const BOOL locked = LockFileEx(lock, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &range); + ShaderLibrary partial; + std::string error; + const bool loaded = locked && partial.load(locked_dir, &error); + CloseHandle(lock); + CHECK(locked); + CHECK(!loaded); + CHECK(!partial.loaded() && error == "index.json could not be read"); + } +#endif { std::ofstream index(dir / "index.json", std::ios::binary); index << "\xef\xbb\xbf" "{\"cs_test\": {\"stage\": \"cs\", \"fssm_sha256\": \"" << hash