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
144 changes: 144 additions & 0 deletions runtime/shims/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
#include <intrin.h>

#include <atomic>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <cwctype>
#include <filesystem>
#include <io.h>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <sys/stat.h>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -1715,6 +1718,131 @@ BOOL WINAPI get_disk_free_space_a(LPCSTR root_path_name,
number_of_free_clusters, total_number_of_clusters);
}

namespace {

int set_crt_errno_from_win32(DWORD error) {
_set_doserrno(static_cast<int>(error));
int mapped = EINVAL;
switch (error) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
case ERROR_INVALID_NAME:
case ERROR_BAD_NETPATH:
case ERROR_BAD_PATHNAME:
case ERROR_FILENAME_EXCED_RANGE:
mapped = ENOENT;
break;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
case ERROR_WRITE_PROTECT:
case ERROR_CURRENT_DIRECTORY:
mapped = EACCES;
break;
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
mapped = ENOMEM;
break;
case ERROR_FILE_EXISTS:
case ERROR_ALREADY_EXISTS:
mapped = EEXIST;
break;
case ERROR_INVALID_PARAMETER:
default:
mapped = EINVAL;
break;
}
_set_errno(mapped);
return -1;
}

template <typename StatBuf, typename HostStatW>
int crt_stat_console_w(const wchar_t* name, StatBuf* buffer, HostStatW host_stat_w) {
std::vector<std::wstring> parts;
const Parsed parsed = parse_console(name, parts);
if (parsed == Parsed::Host) return host_stat_w(name, buffer);
if (parsed == Parsed::Refused) return set_crt_errno_from_win32(ERROR_INVALID_NAME);

const Roots r = roots();
const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir);
if (r.overlay.empty() && (scratch || r.game.empty())) return set_crt_errno_from_win32(ERROR_PATH_NOT_FOUND);
const View v = view(r, parts);
if (v.kind == Kind::None) return set_crt_errno_from_win32(missing_error(r, parts));
const std::filesystem::path resolved = v.in_overlay ? disk_path(r.overlay, parts) : disk_path(r.game, parts);
return host_stat_w(resolved.c_str(), buffer);
}

template <typename StatBuf, typename HostStatA, typename HostStatW>
int crt_stat_a(const char* name, StatBuf* buffer, HostStatA host_stat_a, HostStatW host_stat_w) {
if (!name || !buffer) {
_set_doserrno(0);
_set_errno(EINVAL);
return -1;
}
const std::wstring wide = widen_ansi(name);
std::vector<std::wstring> parts;
if (parse_console(wide, parts) == Parsed::Host) return host_stat_a(name, buffer);
return crt_stat_console_w(wide.c_str(), buffer, host_stat_w);
}

template <typename StatBuf, typename HostStatW>
int crt_stat_w(const wchar_t* name, StatBuf* buffer, HostStatW host_stat_w) {
if (!name || !buffer) {
_set_doserrno(0);
_set_errno(EINVAL);
return -1;
}
return crt_stat_console_w(name, buffer, host_stat_w);
}

int __cdecl crt_wstat64(const wchar_t* name, struct __stat64* buffer) { return crt_stat_w(name, buffer, ::_wstat64); }
int __cdecl crt_stat64(const char* name, struct __stat64* buffer) {
return crt_stat_a(name, buffer, ::_stat64, ::_wstat64);
}
int __cdecl crt_wstat64i32(const wchar_t* name, struct _stat64i32* buffer) {
return crt_stat_w(name, buffer, ::_wstat64i32);
}
int __cdecl crt_stat64i32(const char* name, struct _stat64i32* buffer) {
return crt_stat_a(name, buffer, ::_stat64i32, ::_wstat64i32);
}
int __cdecl crt_wstat32(const wchar_t* name, struct _stat32* buffer) { return crt_stat_w(name, buffer, ::_wstat32); }
int __cdecl crt_stat32(const char* name, struct _stat32* buffer) {
return crt_stat_a(name, buffer, ::_stat32, ::_wstat32);
}
int __cdecl crt_wstat32i64(const wchar_t* name, struct _stat32i64* buffer) {
return crt_stat_w(name, buffer, ::_wstat32i64);
}
int __cdecl crt_stat32i64(const char* name, struct _stat32i64* buffer) {
return crt_stat_a(name, buffer, ::_stat32i64, ::_wstat32i64);
}

int __cdecl crt_wremove(const wchar_t* name) {
if (!name) {
_set_doserrno(0);
_set_errno(EINVAL);
return -1;
}
std::vector<std::wstring> parts;
if (parse_console(name, parts) == Parsed::Host) return ::_wremove(name);
if (delete_file_w(name)) return 0;
return set_crt_errno_from_win32(::GetLastError());
}

int __cdecl crt_remove(const char* name) {
if (!name) {
_set_doserrno(0);
_set_errno(EINVAL);
return -1;
}
const std::wstring wide = widen_ansi(name);
std::vector<std::wstring> parts;
if (parse_console(wide, parts) == Parsed::Host) return ::remove(name);
if (delete_file_w(wide.c_str())) return 0;
return set_crt_errno_from_win32(::GetLastError());
}

} // namespace

} // namespace rt::fs

namespace {
Expand Down Expand Up @@ -1791,6 +1919,20 @@ BOOL WINAPI read_file_scatter(HANDLE file, FILE_SEGMENT_ELEMENT segments[], DWOR
{dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \
{dll, "ReadFileEx", reinterpret_cast<void*>(&read_file_ex)}, \
{dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)}

#define CRT_FILE_API(dll) \
{dll, "remove", reinterpret_cast<void*>(&crt_remove)}, \
{dll, "_wremove", reinterpret_cast<void*>(&crt_wremove)}, \
{dll, "_unlink", reinterpret_cast<void*>(&crt_remove)}, \
{dll, "_wunlink", reinterpret_cast<void*>(&crt_wremove)}, \
{dll, "_wstat64", reinterpret_cast<void*>(&crt_wstat64)}, \
{dll, "_stat64", reinterpret_cast<void*>(&crt_stat64)}, \
{dll, "_wstat64i32", reinterpret_cast<void*>(&crt_wstat64i32)}, \
{dll, "_stat64i32", reinterpret_cast<void*>(&crt_stat64i32)}, \
{dll, "_wstat32", reinterpret_cast<void*>(&crt_wstat32)}, \
{dll, "_stat32", reinterpret_cast<void*>(&crt_stat32)}, \
{dll, "_wstat32i64", reinterpret_cast<void*>(&crt_wstat32i64)}, \
{dll, "_stat32i64", reinterpret_cast<void*>(&crt_stat32i64)}
} // namespace

SHIM_TABLE(file_system,
Expand All @@ -1799,4 +1941,6 @@ SHIM_TABLE(file_system,
FILE_API("api-ms-win-core-file-l1-1-0.dll"),
FILE_API("api-ms-win-core-file-l1-2-0.dll"),
FILE_API("api-ms-win-core-file-l2-1-0.dll"),
CRT_FILE_API("api-ms-win-crt-filesystem-l1-1-0.dll"),
CRT_FILE_API("ucrtbase.dll"),
);
91 changes: 91 additions & 0 deletions runtime/test/file_system_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <algorithm>
#include <atomic>
#include <cerrno>
#include <clocale>
#include <cstring>
#include <cstdio>
Expand All @@ -18,6 +19,7 @@
#include <sstream>
#include <initializer_list>
#include <string>
#include <sys/stat.h>
#include <thread>
#include <vector>

Expand Down Expand Up @@ -317,6 +319,94 @@ void check_read_file_first_touch(const std::filesystem::path& root) {
g_first_touch = nullptr;
}

void check_crt_stat_remove(const std::filesystem::path& root) {
using WStat64Fn = int(__cdecl*)(const wchar_t*, struct __stat64*);
using Stat64Fn = int(__cdecl*)(const char*, struct __stat64*);
using WStat64i32Fn = int(__cdecl*)(const wchar_t*, struct _stat64i32*);
using Stat64i32Fn = int(__cdecl*)(const char*, struct _stat64i32*);
using RemoveFn = int(__cdecl*)(const char*);
using WRemoveFn = int(__cdecl*)(const wchar_t*);

const auto wstat64 = resolve<WStat64Fn>("api-ms-win-crt-filesystem-l1-1-0.dll", "_wstat64");
const auto stat64 = resolve<Stat64Fn>("api-ms-win-crt-filesystem-l1-1-0.dll", "_stat64");
const auto wstat64i32 = resolve<WStat64i32Fn>("api-ms-win-crt-filesystem-l1-1-0.dll", "_wstat64i32");
const auto stat64i32 = resolve<Stat64i32Fn>("api-ms-win-crt-filesystem-l1-1-0.dll", "_stat64i32");
const auto crt_rm = resolve<RemoveFn>("api-ms-win-crt-filesystem-l1-1-0.dll", "remove");
const auto crt_wrm = resolve<WRemoveFn>("api-ms-win-crt-filesystem-l1-1-0.dll", "_wremove");
const auto crt_unlink = resolve<RemoveFn>("ucrtbase.dll", "_unlink");
const auto crt_wunlink = resolve<WRemoveFn>("ucrtbase.dll", "_wunlink");
check(wstat64 && stat64 && wstat64i32 && stat64i32 && crt_rm && crt_wrm && crt_unlink && crt_wunlink,
"api-ms-win-crt-filesystem-l1-1-0 and ucrtbase serve CRT stat and remove/unlink");
if (!wstat64 || !stat64 || !wstat64i32 || !stat64i32 || !crt_rm || !crt_wrm || !crt_unlink || !crt_wunlink) return;

const auto pack = root / "package";
const auto layer = root / "overlay";
const auto pkg_file = pack / "crt" / "asset.bin";
const auto ro_pkg_file = pack / "crt" / "ro.bin";
write_path(pkg_file, "123456");
write_path(ro_pkg_file, "readonly");
::SetFileAttributesW(ro_pkg_file.c_str(), FILE_ATTRIBUTE_READONLY);
rt::fs::configure(pack, layer);

struct __stat64 st{};
errno = 0;
check(wstat64(L"GAME:/crt/asset.bin", &st) == 0 && (st.st_mode & _S_IFREG) != 0 && st.st_size == 6,
"_wstat64 reads package file size and regular mode through GAME:");
struct _stat64i32 st32{};
check(stat64i32("G:/crt/asset.bin", &st32) == 0 && st32.st_size == 6,
"_stat64i32 reads package file through G:");

// Overlay file shadows package file.
write_path(layer / "crt" / "asset.bin", "overlay-longer");
check(wstat64(L"GAME:\\crt\\asset.bin", &st) == 0 && st.st_size == 14,
"_wstat64 sees overlay copy size when shadowed");

// Directory stat (GAME:, G:, T:) works, while missing/escaped paths fail with ENOENT.
struct __stat64 dir_st{};
check(wstat64(L"GAME:\\crt", &dir_st) == 0 && (dir_st.st_mode & _S_IFDIR) != 0,
"_wstat64 identifies GAME: directory");
check(wstat64(L"T:\\", &dir_st) == 0 && (dir_st.st_mode & _S_IFDIR) != 0,
"_wstat64 identifies T: scratch root");

const std::filesystem::path t_dir = own_t_folder(layer);
write_path(t_dir / "tmp.txt", "scratch-bytes");
check(!t_dir.empty() && stat64("T:/tmp.txt", &st) == 0 && st.st_size == 13 && crt_rm("T:/tmp.txt") == 0,
"_stat64 and remove read and delete scratch files on T:");
errno = 0;
check(wstat64i32(L"T:\\tmp.txt", &st32) == -1 && errno == ENOENT,
"_wstat64i32 reports ENOENT after remove on T:");

// Removing a package file places a delete marker, leaves package intact, and hides it from _wstat64.
check(crt_rm("GAME:/crt/asset.bin") == 0 && read_path(pkg_file) == "123456",
"remove deletes GAME: file in overlay/marker while leaving package file untouched");
errno = 0;
check(wstat64(L"GAME:/crt/asset.bin", &st) == -1 && errno == ENOENT,
"_wstat64 returns -1 (ENOENT) after CRT remove of GAME: file");

// Removing a read-only package file or a directory fails with EACCES, matching host CRT behavior.
errno = 0;
check(crt_wrm(L"GAME:\\crt\\ro.bin") == -1 && errno == EACCES && read_path(ro_pkg_file) == "readonly",
"_wremove refuses read-only package file with EACCES");
errno = 0;
check(crt_wunlink(L"GAME:\\crt") == -1 && errno == EACCES,
"_wunlink refuses a GAME: directory with EACCES");
::SetFileAttributesW(ro_pkg_file.c_str(), FILE_ATTRIBUTE_NORMAL);

// Path traversal escaping GAME: fails cleanly with ENOENT.
errno = 0;
check(crt_unlink("GAME:/../outside.txt") == -1 && errno == ENOENT,
"_unlink refuses paths climbing above GAME: with ENOENT");

// Host passthrough matches native CRT.
const auto host_file = root / "host_crt.txt";
write_path(host_file, "host-data");
check(wstat64(host_file.c_str(), &st) == 0 && st.st_size == 9 && crt_wunlink(host_file.c_str()) == 0,
"host paths pass through CRT _wstat64 and _wunlink");
errno = 0;
check(wstat64(host_file.c_str(), &st) == -1 && errno == ENOENT,
"deleted host file reports ENOENT through _wstat64");
}

} // namespace

int main() {
Expand All @@ -335,6 +425,7 @@ int main() {
// failure, the folder is still removed and the summary is still printed.
try {
check_read_file_first_touch(root / "first_touch");
check_crt_stat_remove(root / "crt_shims");
run_checks(root);
} catch (const std::exception& e) {
check(false, (std::string("the checks run to the end (stopped by: ") + e.what() + ")").c_str());
Expand Down
Loading