Repository navigation
Expand file tree
/
Copy pathdirect_call.hpp
More file actions
105 lines (90 loc) · 4.05 KB
/
Copy pathdirect_call.hpp
File metadata and controls
105 lines (90 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (C) 2026
* SPDX-License-Identifier: MIT
*/
// "This thread is currently inside the signed NGX runtime."
//
// The addon detours four NGX exports, and it also CALLS those exports itself
// (init, create, evaluate, release, shutdown). Without a fence, its own call
// re-enters its own detour: the wrapper would try to re-lock the
// non-recursive runtime_mutex on this thread and deadlock, and the
// command-state shadow would record the runtime's own binds as if the host
// had made them. The fence is this per-thread flag.
//
// Until v6.8.0 it was a bare `inline thread_local bool` raised and lowered by
// hand at seven call sites, and three of them (feature release, retired
// feature release, direct Shutdown) had no exception handling at all. The
// failure that costs is silent and permanent: a C++ exception escaping the
// runtime skips the `= false`, the flag stays raised for the LIFE OF THE
// THREAD, and from then on SuppressCommandStateObservation() is true on every
// call - so the shadow stops learning, every evaluate on that thread declines
// for a missing restore target, and nothing says why. The mod appears to
// switch itself off mid-session on one thread and keep working on the others
// (PLAN_REHAB_V7.md, R8).
//
// Two things are fixed by moving it here rather than by adding four more
// try/catch blocks:
//
// 1. The reset is a destructor, so it runs on every exit from the scope -
// return, break, or a C++ exception - without the site having to
// remember. (It is NOT a defence against an access violation in the
// runtime: this addon is built /EHsc, where `catch (...)` does not catch
// SEH and a structured unwind is not required to run C++ destructors.
// The four sites that carry `catch (...)` never caught the driver faults
// in the crash reports either - see the R8 row - and no claim to the
// contrary belongs in the README.)
// 2. The flag is PRIVATE, with Scope its only friend, so `inside_direct_call
// = true` no longer compiles. The invariant is held by the compiler
// rather than by a reviewer noticing the eighth site - which is the same
// reason vtable_slots.c is a pile of _Static_asserts.
//
// A depth counter, not a bool, for the same reason InjectedCommandScope is
// one: an inner scope must not lower the fence the outer scope raised.
//
// Dependency-free on purpose, so test/dlss5 can include it and pin the
// behaviour that matters - that the fence falls when the scope is left by an
// exception, and that nesting restores rather than clears.
#pragma once
#include <cstdint>
#include "ngx_serial.hpp"
namespace renodx::addons::dlss5 {
namespace direct_call {
class Scope;
// The state, with no public way to write it.
class State {
public:
static bool Active() noexcept { return depth_ != 0; }
// Exposed for tests and for telemetry; a non-zero depth outside a call is a
// leak, and one that is visible can be reported.
static std::uint32_t Depth() noexcept { return depth_; }
private:
friend class Scope;
static inline thread_local std::uint32_t depth_ = 0;
};
// Raise the fence for as long as this object lives.
class Scope {
public:
Scope() noexcept { ++State::depth_; }
~Scope() noexcept {
if (State::depth_ != 0) --State::depth_;
}
Scope(const Scope&) = delete;
Scope& operator=(const Scope&) = delete;
Scope(Scope&&) = delete;
Scope& operator=(Scope&&) = delete;
};
} // namespace direct_call
// The two names the addon uses. `DirectCallScope guard;` at the top of the
// smallest block that contains the runtime call, and InsideDirectCall()
// wherever the old flag was read. The call also takes its NGX turn
// (ngx_serial.hpp) before the fence goes up: the header says NGX is not
// thread safe, and the addon's own calls are NGX calls too.
struct DirectCallScope {
const ngx_serial::Call turn;
const direct_call::Scope fence;
};
inline bool InsideDirectCall() noexcept { return direct_call::State::Active(); }
inline std::uint32_t DirectCallDepth() noexcept {
return direct_call::State::Depth();
}
} // namespace renodx::addons::dlss5