Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions runtime/src/shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@
#include <bit>
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <map>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>

#ifdef _WIN32
#include <share.h>
#endif

namespace leonida {

// ---------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -982,12 +987,22 @@ class ShaderLibrary {
template<typename Fail>
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<std::FILE, decltype(close)> 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<char>(file)), std::istreambuf_iterator<char>());
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());
Expand Down
34 changes: 34 additions & 0 deletions runtime/test/shader_library_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#include <string>
#include <vector>

#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif

#define CHECK(condition) do { \
if (!(condition)) { \
std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
Expand Down Expand Up @@ -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
Expand Down
Loading