Skip to content

RR plugin: build with the static C/C++ runtime (/MT) - #13

Merged
phantoslayer21 merged 1 commit into
mainfrom
fix/rr-plugin-old-msvcp
Nov 13, 2026
Merged

phantoslayer21 merged 1 commit into
mainfrom
fix/rr-plugin-old-msvcp

Conversation

@phantoslayer21

@phantoslayer21 phantoslayer21 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Reported by @j0nsh1n in #9.

What was wrong

WrongGPU-RR.asi was the only binary of the release built against the shared C/C++ runtime (/MD in
clean/rr_f/proxy/tools/build.py). WrongGPU.asi and wronggpu_runtime.dll are built with the static runtime, as
BUILDING.md's CMake flags say (CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded).

The 0.6.0 plugin was built with linker version 14.51. Its std::mutex objects are constant-initialised. dumpbin /imports of
the shipped plugin shows _Mtx_lock and _Mtx_unlock from MSVCP140.dll and no _Mtx_init_in_situ. With an older
msvcp140.dll (a Proton prefix, or an old VC++ 2015-2022 redistributable on Windows) the first lock reads a null
pointer inside msvcp140 and the process dies in InitializeASI.

The 14.40 threshold is from Microsoft, not from a measurement here. Microsoft's STL changelog lists, under
VS 2022 17.10, "Fixed mutex's constructor to be constexpr" and warns that programs that don't follow the binary
compatibility restrictions "may encounter null dereferences in mutex machinery"
(https://github.com/microsoft/STL/wiki/VS-2022-Changelog). VS 2022 17.10 is MSVC Build Tools 14.40
(https://learn.microsoft.com/en-us/cpp/overview/compiler-versions), and the restriction is that the Redistributable
must be at least as new as the latest build tools used by any app component
(https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017). So a plugin built with a constexpr std::mutex
needs msvcp140.dll 14.40 or newer. What was measured here is one old and one new runtime (see Results): 14.29.30157.0
crashes, 14.51.36247.0 doesn't. No runtime between them was tried.

Why the plugin was /MD

No reason for /MD was found in the code or its comments. The project's rule is static-runtime release builds. Nothing
in the plugin needs the shared runtime: only plain C structures and function pointers cross its boundaries (the game
interface, OptiScaler's plugin table, the RR runtime table in include/wg_rr_adapter.h). No FILE*, heap block or C++
object passes between modules.

What changed

  • build.py: CLFLAGS uses /MT. The plugin (release and probe), the file-replacement proxies and the test programs
    that compile the plugin's sources all build with it. live_harness.py builds test_live_harness with the same
    CLFLAGS, so that program is /MT too. It shares no C++ object with another module (it reaches the plugin through
    C functions only). It needs the GPU and was not built or run here.
  • The fake runtime library and the two test programs that load it (test_proxy_dll, test_hook_dll) keep /MD
    (SHARED_CRT_CLFLAGS). They share C++ containers through tests/fake_runtime.h, so they need one heap. The
    plugin under test never sees those objects.
  • test_setupapi_identity.cpp: one check expected the exe's two SetupAPI imports in a fixed order. The linker picks
    that order, and it changed with the runtime library, so the check now accepts either order. The redirect itself
    (both slots patched) was unchanged.

We did not use /D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR: it would still tie the plugin to whatever msvcp140.dll the
game process has loaded. With /MT the plugin loads none.

Static runtime and DllMain: the hook plugin's and the proxy's DllMain call DisableThreadLibraryCalls, which
Microsoft's documentation advises against in a DLL linked with the static runtime. This change makes those DLLs
static-runtime. The shipped static-runtime binaries (WrongGPU.asi and the RR runtime DLL) already make the same call,
the plugin's thread_local variables are plain pointers and flags with constant initialisers, and the tests below pass.
No failure was found; the calls are left as they are in this change.

Results

  • The rebuilt WrongGPU-RR.asi imports only KERNEL32 and VERSION: no MSVCP140, VCRUNTIME140 or api-ms-win-crt
    import. The probe plugin and the 2.14 release and probe proxies match (KERNEL32, plus VERSION for the probe plugin).
    The plugin grows from 366080 to 560128 bytes.
  • Old-runtime test on Windows. A small test host (not part of this PR) loads the plugin from its own folder, next to
    msvcp140.dll, vcruntime140.dll and vcruntime140_1.dll 14.29.30157.0. Those were taken from Microsoft's
    VC++ 2015-2019 redistributable without running it. The host then calls InitializeASI:
    • 0.6.0 plugin: access violation at MSVCP140.dll+0x13080, read of address 0. In that msvcp140.dll,
      _Mtx_lock jumps to the lock routine, which dereferences the mutex's implementation pointer; the old runtime
      expects _Mtx_init_in_situ to have set it. No wronggpu-rr.log is written, as in the report.
    • Rebuilt plugin: no msvcp140.dll is loaded at all. InitializeASI returns normally and writes its log.
    • Control: the 0.6.0 plugin with the PC's installed runtime (14.51.36247.0) also returns normally.

How it was tested

python clean\rr_f\proxy\tools\build.py --out <absolute path>\out\rr-proxy with its tests. They are CPU-only: the
D3D12 parts use WARP, with no GPU and no game. Everything passes with 0 failures:

  • Python: 33 tests OK (1 skipped).
  • test_proxy_core 1159 checks, test_probe_core 890, test_setupapi_identity 142, test_ngx_rr 334,
    test_replay_profile 1782259.
  • For each of 2.7, 2.8 and 2.14:
    • test_proxy_dll, with and without the runtime (893 / 882 checks for 2.7; 898 / 887 for 2.8 and 2.14).
    • test_probe_dll (1633 / 1634 / 1634 checks).
    • The hook plugin's release and probe runs, including the delay-load and wrong-version runs.

Not tested

  • No game run and no Proton run. Whether this resolves WrongGPU-RR.asi crashes with msvcp140.dll older than 14.40 (constexpr std::mutex, _Mtx_lock null read) #9 on the reporter's setup is not confirmed.
  • OptiScaler's own binaries were not examined, nor the reporter's build of the plugin. The report that OptiScaler's
    dxgi.dll imports _Mtx_init_in_situ is unverified here; the fix doesn't depend on it.
  • The imports of the rebuilt proxies were checked for 2.14 only, not for 2.7 and 2.8.
  • test_live_harness was not built or run (it needs the GPU).

Fixes #9.

WrongGPU-RR.asi was the only release binary built against the shared
runtime (/MD); WrongGPU.asi and wronggpu_runtime.dll are /MT. Built with
linker 14.51, its std::mutex is constant-initialised, which needs
msvcp140.dll 14.40 or newer (Microsoft STL changelog, VS 2022 17.10). With an older msvcp140.dll (a Wine/Proton
prefix, or an old VC++ redistributable on Windows) the first lock reads a
null pointer inside msvcp140 and the process dies in InitializeASI.

Only plain C structures and function pointers cross the plugin's
boundaries (game interface, OptiScaler's plugin table, the RR runtime
table), so nothing needs a shared C runtime. The rebuilt plugin imports
only KERNEL32 and VERSION.

The fake runtime library and the two test programs that load it share
std:: containers (tests/fake_runtime.h), so those three keep /MD. The
setupapi identity test no longer depends on the order the linker puts
two imports in, which changes with the runtime library.

Reported-by: j0nsh1n
Fixes #9

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 marked this pull request as ready for review November 13, 2026 15:31
@phantoslayer21
phantoslayer21 force-pushed the fix/rr-plugin-old-msvcp branch from ba5f217 to 6f71930 Compare November 13, 2026 15:31
@phantoslayer21
phantoslayer21 merged commit 3fd1844 into main Nov 13, 2026
2 checks passed
@phantoslayer21
phantoslayer21 deleted the fix/rr-plugin-old-msvcp branch November 13, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WrongGPU-RR.asi crashes with msvcp140.dll older than 14.40 (constexpr std::mutex, _Mtx_lock null read)

1 participant