From d411fe9b4a3aab2a75dfbed4d6e4f01d758ef668 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:25:47 -0600 Subject: [PATCH] d3d12, process: null barrier arrays, CopyTiles in a bundle, calibration wait, entry thread and getmainargs expand From kevincommits' finding (item 4) in #1608. - d3d12_com.cpp: - ResourceBarrier: a count with no array fails the list (check(false)) instead of reading through null, as WriteBufferImmediate does. - Barrier: the same for a null group array, and for a group with barriers but no barrier array (the three array pointers are one union). - CopyTiles: a bundle is refused for reserved buffers too, not only for sparse images; no copy is allowed in a bundle. - submitted_calibration: vkGetQueryPoolResults asks VK_QUERY_RESULT_WAIT_BIT, so the read cannot answer VK_NOT_READY. - process.cpp: - run_entry passes the entry point itself to the game thread instead of a pointer to its own stack: another thread can end the run before the game thread first runs, and run_entry's frame is then gone. - __getmainargs/__wgetmainargs expand wildcards when `expand` is nonzero (mode 2, as _configure_*_argv does) and fail on a null argc or argv. Tests: d3d12_com_test (null arrays fail Close; counts of 0 and valid calls do not), d3d12_buffer_tiles_test (CopyTiles of a reserved buffer in a bundle fails Close), process_cmdline_test (expand 0 and 1, null argc/argv). Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_com.cpp | 13 +++++- runtime/shims/process.cpp | 25 ++++++----- runtime/test/d3d12_buffer_tiles_test.cpp | 12 ++++++ runtime/test/d3d12_com_test.cpp | 32 ++++++++++++++ runtime/test/process_cmdline_test.cpp | 54 ++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 15 deletions(-) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4a61ffadbc..3970b354e4 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -589,6 +589,7 @@ class GraphicsCommandList final void STDMETHODCALLTYPE ResourceBarrier(UINT count, const D3D12_RESOURCE_BARRIER* barriers) override { d3d12_trace::record("ID3D12GraphicsCommandList::ResourceBarrier"); if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); + if (count && !barriers) return check(false); // as WriteBufferImmediate: the list fails, nothing is read for (UINT i = 0; i < count; ++i) { const auto& b = barriers[i]; switch (b.Type) { @@ -625,7 +626,14 @@ class GraphicsCommandList final void STDMETHODCALLTYPE Barrier(UINT32 groups, const D3D12_BARRIER_GROUP* group) override { d3d12_trace::record("ID3D12GraphicsCommandList7::Barrier"); if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); + if (groups && !group) return check(false); for (UINT32 g = 0; g < groups; ++g) { + // A group with barriers and no array fails the list; nothing is read. The three array pointers are one + // union, so any of them says whether there is an array. + if (group[g].NumBarriers && !group[g].pGlobalBarriers) { + check(false); + continue; + } for (UINT32 i = 0; i < group[g].NumBarriers; ++i) { switch (group[g].Type) { case D3D12_BARRIER_TYPE_GLOBAL: { @@ -701,6 +709,7 @@ class GraphicsCommandList final const D3D12_TILE_REGION_SIZE* size, ID3D12Resource* linear, UINT64 linear_offset, D3D12_TILE_COPY_FLAGS flags) override { d3d12_trace::record("ID3D12GraphicsCommandList::CopyTiles"); + if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); // no copy is allowed in a bundle const auto direction = flags & ~D3D12_TILE_COPY_FLAG_NO_HAZARD; const bool to_linear = direction == D3D12_TILE_COPY_FLAG_SWIZZLED_TILED_RESOURCE_TO_LINEAR_BUFFER; const auto grid = tile_grid(tiled); @@ -714,7 +723,7 @@ class GraphicsCommandList final const auto buffer = linear->GetDesc(); const auto desc = tiled->GetDesc(); const auto count = size->UseBox ? std::uint64_t(size->Width) * size->Height : size->NumTiles; - if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || buffer.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || + if (buffer.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || linear_offset > buffer.Width || count > (buffer.Width - linear_offset) / leonida::tile_size_in_bytes) return check(false); constexpr auto shape = leonida::element_tile_shape(false, 4); @@ -2703,7 +2712,7 @@ class CommandQueue final std::uint64_t stamp = 0; if (context_.observe_result(stamp_.get_query_pool_results(context_.device(), stamp_.queries, 0, 1, sizeof(stamp), &stamp, sizeof(stamp), - VK_QUERY_RESULT_64_BIT), + VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT), "vkGetQueryPoolResults (calibration)") != VK_SUCCESS) return E_FAIL; if (clock_.timestamp_valid_bits < 64) stamp &= (std::uint64_t(1) << clock_.timestamp_valid_bits) - 1; diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 2c37292e20..29663a92f3 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -417,30 +417,30 @@ wchar_t** current_wide_environment() { return _wenviron; } -// msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) -int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { - if (const int error = game_configure_narrow_argv(1)) return error; +// msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo). A nonzero +// `expand` (the CRT's _dowildcard) expands wildcards in the arguments, as _configure_narrow_argv's mode 2 does. +int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int expand, void*) { + if (!argc || !argv) return -1; + if (const int error = game_configure_narrow_argv(expand ? 2 : 1)) return error; *argc = g_argc; *argv = g_argv_ptr; if (env) *env = current_environment(); return 0; } -int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { - if (const int error = game_configure_wide_argv(1)) return error; +int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int expand, void*) { + if (!argc || !argv) return -1; + if (const int error = game_configure_wide_argv(expand ? 2 : 1)) return error; *argc = g_argc; *argv = g_wargv_ptr; if (env) *env = current_wide_environment(); return 0; } -struct EntryCall { - void* entry; -}; - +// `p` is the entry point itself: run_entry can return (another thread ended the run) before this thread first runs, +// so nothing of run_entry's frame may be read here. DWORD WINAPI game_thread(void* p) { - auto* call = static_cast(p); // An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code. - auto entry = reinterpret_cast(call->entry); + auto entry = reinterpret_cast(p); const DWORD code = entry(nullptr); end_run(static_cast(code)); // returning from the entry point is an exit too } @@ -480,8 +480,7 @@ int run_entry(void* entry, size_t stack_bytes) { g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); if (!g_done) return -1; g_running = true; - EntryCall call{entry}; - HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr); + HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, entry, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr); if (!thread) { g_running = false; CloseHandle(g_done); diff --git a/runtime/test/d3d12_buffer_tiles_test.cpp b/runtime/test/d3d12_buffer_tiles_test.cpp index b63729b407..f4f84c79d3 100644 --- a/runtime/test/d3d12_buffer_tiles_test.cpp +++ b/runtime/test/d3d12_buffer_tiles_test.cpp @@ -113,6 +113,18 @@ bool run(ID3D12Device& device, Results& result, bool layer = false) { const D3D12_TILE_REGION_SIZE all{tiles_per_buffer, FALSE, 0, 0, 0}; queue->CopyTileMappings(b.Get(), &origin, a.Get(), &origin, &all, D3D12_TILE_MAPPING_FLAG_NONE); EXPECT(SUCCEEDED(device.GetDeviceRemovedReason())); + if (layer) { + // No copy is allowed in a bundle: CopyTiles of a reserved buffer fails the bundle's Close, as it does for a + // sparse image and as CopyBufferRegion does (the same call is valid on the direct list below). + ComPtr bundle_allocator; + ComPtr bundle; + EXPECT(SUCCEEDED(device.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, IID_PPV_ARGS(&bundle_allocator)))); + EXPECT(SUCCEEDED(device.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, bundle_allocator.Get(), nullptr, + IID_PPV_ARGS(&bundle)))); + bundle->CopyTiles(a.Get(), &origin, &all, upload.Get(), tile_bytes, + D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE); + EXPECT(bundle->Close() == E_INVALIDARG); + } lists[0]->CopyTiles(a.Get(), &origin, &all, upload.Get(), tile_bytes, D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE); D3D12_RESOURCE_BARRIER alias{}; diff --git a/runtime/test/d3d12_com_test.cpp b/runtime/test/d3d12_com_test.cpp index 8691e5a740..fc8a71d729 100644 --- a/runtime/test/d3d12_com_test.cpp +++ b/runtime/test/d3d12_com_test.cpp @@ -337,6 +337,38 @@ int main(int argc, char** argv) { const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER write{0x1000, 1}; b->WriteBufferImmediate(1, &write, nullptr); })); + // A barrier call with a count and no array fails a direct list's Close instead of reading through null, as + // WriteBufferImmediate does; a count of 0 needs no array. + auto invalid_direct_call = [&](auto record) { + Ref test_allocator; + Ref test_list; + if (FAILED(rt::d3d12::create_command_allocator(D3D12_COMMAND_LIST_TYPE_DIRECT, + IID_PPV_ARGS(&test_allocator.p)))) + return false; + if (FAILED(rt::d3d12::create_command_list(D3D12_COMMAND_LIST_TYPE_DIRECT, test_allocator.p, + IID_PPV_ARGS(&test_list.p)))) + return false; + record(test_list.p); + return test_list->Close() == E_INVALIDARG; + }; + EXPECT(invalid_direct_call([&](auto* l) { l->ResourceBarrier(1, nullptr); })); + EXPECT(invalid_direct_call([&](auto* l) { l->Barrier(1, nullptr); })); + for (const auto type : {D3D12_BARRIER_TYPE_GLOBAL, D3D12_BARRIER_TYPE_TEXTURE, D3D12_BARRIER_TYPE_BUFFER}) { + D3D12_BARRIER_GROUP no_array{}; + no_array.Type = type; + no_array.NumBarriers = 1; + EXPECT(invalid_direct_call([&](auto* l) { l->Barrier(1, &no_array); })); + // The same group after a valid one: the valid one is recorded, the list still fails. + const D3D12_BARRIER_GROUP both[] = {group, no_array}; + EXPECT(invalid_direct_call([&](auto* l) { l->Barrier(2, both); })); + } + EXPECT(!invalid_direct_call([&](auto* l) { l->ResourceBarrier(0, nullptr); })); + EXPECT(!invalid_direct_call([&](auto* l) { l->Barrier(0, nullptr); })); + D3D12_BARRIER_GROUP empty_group{}; + empty_group.Type = D3D12_BARRIER_TYPE_TEXTURE; // no barriers, no array + EXPECT(!invalid_direct_call([&](auto* l) { l->Barrier(1, &empty_group); })); + EXPECT(!invalid_direct_call([&](auto* l) { l->ResourceBarrier(1, &transition); })); // the control + list->Barrier(1, &group); // Not implemented: reported once by its generated stub, recording continues. list->SetProtectedResourceSession(nullptr); diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index bf55821d16..f30d4607f6 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp @@ -218,6 +218,59 @@ void check_getmainargs_environment() { ++failures; } } + +// __getmainargs/__wgetmainargs's `expand` (the CRT's _dowildcard): nonzero expands wildcards in the arguments, as +// _configure_*_argv's mode 2 does; zero leaves them as typed. A null argc or argv fails instead of being written +// through. Synthetic files in a temporary folder. +void check_getmainargs_expand() { + using Narrow = int(__cdecl*)(int*, char***, char***, int, void*); + using Wide = int(__cdecl*)(int*, wchar_t***, wchar_t***, int, void*); + const auto narrow_fn = reinterpret_cast(rt::shims::find("msvcrt.dll", "__getmainargs")); + const auto wide_fn = reinterpret_cast(rt::shims::find("msvcrt.dll", "__wgetmainargs")); + if (!narrow_fn || !wide_fn) return; // check_getmainargs_environment reports it + wchar_t temp[MAX_PATH]{}; + if (!GetTempPathW(MAX_PATH, temp)) { + std::fprintf(stderr, "FAIL GetTempPathW\n"); + ++failures; + return; + } + const std::wstring dir = std::wstring(temp) + L"leonida_getmainargs_" + std::to_wstring(GetCurrentProcessId()); + CreateDirectoryW(dir.c_str(), nullptr); + const std::wstring files[] = {dir + L"\\a1.lx", dir + L"\\a2.lx"}; + for (const auto& f : files) { + const HANDLE h = CreateFileW(f.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (h != INVALID_HANDLE_VALUE) CloseHandle(h); + } + const std::wstring pattern = dir + L"\\*.lx"; + rt::process::set_command_line(L"game.exe", {pattern}); + for (const int expand : {0, 1}) { + const int want = expand ? 3 : 2; // game.exe and the two files, or game.exe and the pattern + int argc = 0; + char** argv = nullptr; + if (narrow_fn(&argc, &argv, nullptr, expand, nullptr) != 0 || argc != want || !argv || + (!expand && std::strchr(argv[1], '*') == nullptr) || (expand && std::strchr(argv[1], '*') != nullptr)) { + std::fprintf(stderr, "FAIL __getmainargs(expand=%d): argc %d, want %d\n", expand, argc, want); + ++failures; + } + int wargc = 0; + wchar_t** wargv = nullptr; + if (wide_fn(&wargc, &wargv, nullptr, expand, nullptr) != 0 || wargc != want || !wargv || + (!expand && std::wcschr(wargv[1], L'*') == nullptr) || (expand && std::wcschr(wargv[1], L'*') != nullptr)) { + std::fprintf(stderr, "FAIL __wgetmainargs(expand=%d): argc %d, want %d\n", expand, wargc, want); + ++failures; + } + } + int argc = 0; + char** argv = nullptr; + wchar_t** wargv = nullptr; + if (narrow_fn(nullptr, &argv, nullptr, 0, nullptr) == 0 || narrow_fn(&argc, nullptr, nullptr, 0, nullptr) == 0 || + wide_fn(nullptr, &wargv, nullptr, 0, nullptr) == 0 || wide_fn(&argc, nullptr, nullptr, 0, nullptr) == 0) { + std::fprintf(stderr, "FAIL __getmainargs/__wgetmainargs accepted a null argc or argv\n"); + ++failures; + } + for (const auto& f : files) DeleteFileW(f.c_str()); + RemoveDirectoryW(dir.c_str()); +} } // namespace int main() { @@ -234,6 +287,7 @@ int main() { check_import_names(); check_argv_reset(); check_getmainargs_environment(); + check_getmainargs_expand(); check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); check_winmain(L"game.exe", {}, L"", "no arguments");