Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++
8.4 KB
Raw Blame
#include "veyra/ngx/NvapiArchSpoof.h"

#include "veyra/Log.h"
#include "compat/RestoreMemory.h"

#include <format>
#include <mutex>

namespace veyra::ngx {
namespace {

// NV_GPU_ARCH_INFO_V1 from the public nvapi.h: version, architecture,
// implementation, revision. The provider sets the version field to 0x20010
// before calling (seen in the audited disassembly at 0x180020DB4).
struct NvGpuArchInfo {
    uint32_t version;
    uint32_t architecture;
    uint32_t implementation;
    uint32_t revision;
};
using GetArchInfoFn = int(__cdecl*)(void* physicalGpu, NvGpuArchInfo* info);
using EnumPhysicalGpusFn = int(__cdecl*)(void* handles, uint32_t* count);
using QueryInterfaceFn = void*(__cdecl*)(uint32_t id);
constexpr uint32_t kNvapiEnumPhysicalGpusId = 0xADD604D1u;
constexpr uint32_t kNvapiInitializeId = 0x0150E828u;
constexpr uint32_t kNvGpuArchInfoVersion = 0x20010u;
using NvapiInitializeFn = int(__cdecl*)();

// The audited provider resolves NvAPI_GPU_GetArchInfo through a small internal
// wrapper at RVA 0x1670. Only its first 16 bytes are relocated into a
// trampoline: they are pure stack/register moves and therefore position
// independent. The instruction that follows is RIP-relative
// ("lock add [rip+0x73144C],1") and must keep executing at its original
// address, so the trampoline jumps back to entry+16. Copying that instruction
// verbatim broke the provider inside NVSDK_NGX_D3D12_Init (seh=0xC0000005)
// because the displacement then pointed somewhere else entirely.
constexpr uint8_t kExpectedPrologue[16] = {
    0x48, 0x89, 0x74, 0x24, 0x10, 0x57, 0x48, 0x83, 0xEC, 0x20,
    0x48, 0x8B, 0xFA, 0x48, 0x8B, 0xF1};
// Structural check of the RIP-relative instruction we deliberately leave in
// place (lock add dword ptr [rip+0x73144C],1).
constexpr uint8_t kExpectedRipRelative[8] = {
    0xF0, 0x83, 0x05, 0x4C, 0x14, 0x73, 0x00, 0x01};
constexpr size_t kRelocatedBytes = sizeof(kExpectedPrologue);
constexpr size_t kJumpBytes = 14; // ff 25 00 00 00 00 <addr64>
constexpr size_t kTrampolineBytes = kRelocatedBytes + kJumpBytes;

struct Global {
    std::mutex mutex;
    uint8_t* entry = nullptr;
    uint8_t* trampoline = nullptr;
    uint32_t reportedArchitecture = 0;
    uint32_t realArchitectureSeen = 0;
    bool targetResolved = false;
    bool installed = false;
    std::wstring detail;
};

Global& global() {
    static Global value;
    return value;
}

int __cdecl SpoofedGetArchInfo(void* physicalGpu, NvGpuArchInfo* info) {
    auto& g = global();
    if (g.trampoline == nullptr) {
        return -1;
    }
    const int status = reinterpret_cast<GetArchInfoFn>(g.trampoline)(physicalGpu, info);
    if (status == 0 && info != nullptr && g.reportedArchitecture != 0) {
        if (g.realArchitectureSeen == 0) {
            g.realArchitectureSeen = info->architecture;
        }
        info->architecture = g.reportedArchitecture;
    }
    return status;
}

} // namespace

bool NvapiArchSpoof::install(HMODULE module, uint32_t reportedArchitecture) {
    auto& g = global();
    std::lock_guard lock(g.mutex);
    if (g.installed) {
        return true;
    }
    if (module == nullptr || reportedArchitecture == 0) {
        g.detail = L"module or reported architecture missing";
        return false;
    }

    // Record what the driver really reports, purely for the log.
    HMODULE nvapi = LoadLibraryExW(L"nvapi64.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
    if (nvapi != nullptr) {
        const auto query = reinterpret_cast<QueryInterfaceFn>(GetProcAddress(nvapi, "nvapi_QueryInterface"));
        if (query != nullptr) {
            const auto getArchInfo = reinterpret_cast<GetArchInfoFn>(query(kNvapiGetArchInfoId));
            const auto enumGpus = reinterpret_cast<EnumPhysicalGpusFn>(query(kNvapiEnumPhysicalGpusId));
            const auto initialize = reinterpret_cast<NvapiInitializeFn>(query(kNvapiInitializeId));
            if (initialize != nullptr) {
                initialize();
            }
            if (getArchInfo != nullptr && enumGpus != nullptr) {
                uint64_t handles[64]{};
                uint32_t count = 0;
                if (enumGpus(handles, &count) == 0 && count > 0) {
                    NvGpuArchInfo info{};
                    info.version = kNvGpuArchInfoVersion;
                    if (getArchInfo(reinterpret_cast<void*>(handles[0]), &info) == 0) {
                        g.realArchitectureSeen = info.architecture;
                    }
                }
            }
        }
    }

    g.entry = reinterpret_cast<uint8_t*>(module) + kGetArchInfoWrapperRva;
    if (std::memcmp(g.entry, kExpectedPrologue, kRelocatedBytes) != 0) {
        g.detail = L"provider GetArchInfo wrapper prologue does not match the audited build";
        g.entry = nullptr;
        return false;
    }
    if (std::memcmp(g.entry + kRelocatedBytes, kExpectedRipRelative, sizeof(kExpectedRipRelative)) != 0) {
        g.detail = L"provider GetArchInfo wrapper layout changed after the relocated prologue";
        g.entry = nullptr;
        return false;
    }
    g.targetResolved = true;

    // Trampoline: the 16 relocated bytes followed by "jmp [rip+0] <wrapper+16>".
    g.trampoline = static_cast<uint8_t*>(VirtualAlloc(
        nullptr, kTrampolineBytes, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
    if (g.trampoline == nullptr) {
        g.detail = L"trampoline allocation failed";
        g.entry = nullptr;
        return false;
    }
    std::memcpy(g.trampoline, g.entry, kRelocatedBytes);
    {
        uint8_t* jump = g.trampoline + kRelocatedBytes;
        jump[0] = 0xFF; jump[1] = 0x25;
        const uint32_t zero = 0;
        std::memcpy(jump + 2, &zero, 4);
        const uint64_t back = reinterpret_cast<uint64_t>(g.entry + kRelocatedBytes);
        std::memcpy(jump + 6, &back, 8);
    }
    DWORD trampolineProtection = 0;
    if (!VirtualProtect(g.trampoline, kTrampolineBytes, PAGE_EXECUTE_READ, &trampolineProtection) ||
        !FlushInstructionCache(GetCurrentProcess(), g.trampoline, kTrampolineBytes)) {
        VirtualFree(g.trampoline, 0, MEM_RELEASE);
        g.trampoline = nullptr; g.entry = nullptr;
        g.detail = L"trampoline executable publication failed";
        return false;
    }

    // Entry: "jmp [rip+0] <SpoofedGetArchInfo>" (14 bytes, no register touched).
    uint8_t patch[kJumpBytes]{};
    patch[0] = 0xFF; patch[1] = 0x25;
    const uint32_t zero = 0;
    std::memcpy(patch + 2, &zero, 4);
    const uint64_t target = reinterpret_cast<uint64_t>(&SpoofedGetArchInfo);
    std::memcpy(patch + 6, &target, 8);
    // No provider calls can run before the owning session completes startup.
    // This 14-byte jump is larger than the atomic gate helper supports.
    if (!compat::restoreMemory(g.entry, patch, kJumpBytes)) {
        g.detail = L"provider wrapper publication failed";
        // An uncertain publication may still reference the trampoline. Session
        // teardown must restore the entry before releasing this allocation.
        g.installed = true;
        return false;
    }
    g.reportedArchitecture = reportedArchitecture;
    g.installed = true;
    log::info("nvapi-spoof", std::format(
        "provider GetArchInfo wrapper hooked (trampoline at {}): will report 0x{:X}",
        static_cast<const void*>(g.trampoline), reportedArchitecture));
    return true;
}

bool NvapiArchSpoof::release() {
    auto& g = global();
    std::lock_guard lock(g.mutex);
    if (!g.installed) {
        return true;
    }
    const bool restored = compat::restoreMemory(g.entry, kExpectedPrologue, kRelocatedBytes);
    if (!restored) { log::error("nvapi-spoof", "wrapper restoration failed; retaining trampoline"); return false; }
    if (g.trampoline != nullptr) {
        VirtualFree(g.trampoline, 0, MEM_RELEASE);
        g.trampoline = nullptr;
    }
    log::info("nvapi-spoof", std::format(
        "provider GetArchInfo wrapper restored={} (process memory only)", restored ? 1 : 0));
    g.installed = false;
    g.entry = nullptr;
    return true;
}

bool NvapiArchSpoof::installed() {
    auto& g = global();
    std::lock_guard lock(g.mutex);
    return g.installed;
}

NvapiArchSpoof::State NvapiArchSpoof::snapshot() {
    auto& g = global();
    std::lock_guard lock(g.mutex);
    State state;
    state.installed = g.installed;
    state.targetResolved = g.targetResolved;
    state.slotWritten = g.entry != nullptr;
    state.reportedArchitecture = g.reportedArchitecture;
    state.detail = g.detail;
    return state;
}

} // namespace veyra::ngx