Files
ReSkate/Extension/World/stack_check_report.cpp
T
AyeZeeBB a83cb83bb4 Raise the streamed texture limit to 32764, and warn when it is close
The game can stream 20480 textures at once. When a map and its mods
need one more, it takes a slot past the end of its table and waits for
ever on a byte that is part of a counter: a hang, then a crash in the
hang's own report (crash reports of 2026-10-11).

The limit is raised to 32764, the most a texture's 16-bit handle can
index, before the game makes its texture streaming manager:

- The manager is allocated longer (6.0 MB, was 2.3 MB) and its slot
  table and per-slot floats moved behind its end. Each of the 226
  instructions that hold one of those offsets or sizes gets another
  number; no code is added. Its four fixed lists are pointed at longer
  storage once the constructor has run.
- The renderer keeps a block of floats per slot for each of four
  layers. Its object grows to match (27 instructions), and the two
  places that index a layer's block as layer * 20480 become a multiply
  by the new count, in the same twelve bytes.
- Three functions keep 20480 entries on their own stack. Two list
  filters go through a longer list a stretch at a time, and the sort
  is given scratch room of its own past that many. Up to 20480 they
  run as the game wrote them.

Every instruction is checked byte for byte first, and nothing is
changed if any differs or the manager already exists. An empty
texture_limit_stock.txt beside the game leaves the limit as it is.

Players are told when they come close: a notice at 90% of the limit
in force and an error at 100%, each once until use falls below 80%.
The log records each new high in steps of 5%.

Also: when a function of the game finds its stack written over, the
game ends itself with exit code 0xc0000409 and no crash report. The
log now names that function and its callers first.

The site table (texture_streaming_sites.inc) is generated from a sweep
of the supported build; texture_streaming.h describes the layout.

Tested in game below 20480 textures only. Nothing has run above the
old limit, where the three stack guards and the longer lists first
matter. At 32764 the game still hangs; the notice is the protection.
2026-10-11 19:26:23 -05:00

63 lines
2.6 KiB
C++

#include "stack_check_report.h"
#include "Engine/Core/Hooks/hooks.h"
#include "Engine/Core/Log/logging.h"
#include "Engine/Core/Platform/memory.h"
#include "Engine/Game/Build/addresses.h"
#include "Engine/Game/Build/20260929/texture_streaming.h"
#include <Windows.h>
#include <array>
#include <atomic>
#include <intrin.h>
#include <string>
namespace dingosdk::stack_check_report {
namespace {
namespace ts = addr::texture_streaming;
using Report = void (*)(std::uintptr_t cookie);
Report original{};
std::uintptr_t image{};
std::atomic<bool> installed{};
std::string place(std::uintptr_t address) {
char text[40]{};
if (address >= image && address - image < 0x10000000) sprintf_s(text, "Skate.exe+0x%llx", static_cast<unsigned long long>(address - image));
else sprintf_s(text, "0x%llx", static_cast<unsigned long long>(address));
return text;
}
// The game is about to end itself with a fast fail, which no crash handler sees: say where
// first. The return address is in the function whose stack was written over; the frames behind
// it may be part of what was written over.
void reported(std::uintptr_t cookie) {
const auto from = reinterpret_cast<std::uintptr_t>(_ReturnAddress());
try {
void* frames[24]{};
const auto count = CaptureStackBackTrace(1, 24, frames, nullptr);
std::string trace;
for (unsigned i = 0; i < count; ++i) trace += (i ? " < " : "") + place(reinterpret_cast<std::uintptr_t>(frames[i]));
logging::log(logging::Level::error, logging::Channel::runtime,
"The game's stack check failed in the function that returns to {} (it ends the game at once, with no crash report). Callers: {}",
place(from), trace.empty() ? std::string("unknown") : trace);
logging::flush();
} catch (...) {}
original(cookie);
}
} // namespace
bool install(std::uintptr_t base) noexcept {
if (installed.exchange(true)) return original != nullptr;
if (!base) return false;
image = base;
std::array<unsigned char, 8> head{};
if (!memory::peek(base + ts::stack_check_failed, head) || head != ts::stack_check_failed_bytes) return false;
const auto target = reinterpret_cast<void*>(base + ts::stack_check_failed);
if (hook_prepare(target, reinterpret_cast<void*>(&reported), reinterpret_cast<void**>(&original)) != HookOk || hook_enable(target) != HookOk) {
original = nullptr;
try {
logging::log(logging::Level::warning, logging::Channel::runtime, "Stack check report: the game's routine could not be hooked.");
} catch (...) {}
return false;
}
return true;
}
} // namespace dingosdk::stack_check_report