CharmedBaryon / CommonLibSSE-NG

This is a reverse engineered library for Skyrim Special Edition and Skyrim VR.
MIT License
142 stars 32 forks source link

Crash using ForEachReference #91

Open psych0v0yager opened 6 months ago

psych0v0yager commented 6 months ago

I am attempting to rebuild the skypal_ng mod so it works with the latest 1170 version. The code was able to compile fine, however the mod crashes in game when using the grid function

Function

std::vector<RE::TESObjectREFR*> Grid(RE::StaticFunctionTag*) {
    std::vector<RE::TESObjectREFR*> refs;

    auto* tes = RE::TES::GetSingleton();
    if (!tes) {
        logger::error("{} couldn't get TES singleton", __func__);
        return refs;
    } else {
    logger::info("TES singleton address: {}", (void*)tes);
    }

    tes->ForEachReference([&](RE::TESObjectREFR* akRef) {
        // RE::TESObjectREFR* ref = &akRef;
        logger::info("akRef ID: {}, Name: {}", akRef->GetFormID(), akRef->GetName());
        // if (akRef){
        //     refs.push_back(akRef);
        // }
        if (akRef) {
            logger::info("Valid reference ID: {}", akRef->GetFormID());
            refs.push_back(akRef);
        } else {
            logger::warn("Received a null reference in ForEachReference");
        }

        logger::info("Processed successfully.");
        return RE::BSContainer::ForEachResult::kContinue;
    });
    logger::info("Loop rocessed successfully.");
    return refs;
}

Papyrus Binding

bool BindPapyrusFunctions(RE::BSScript::IVirtualMachine* vm) {
    logger::info("Binding Papyrus Functions");

    //functions 
    vm->RegisterFunction("All", "SkyPal_References", All);
    vm->RegisterFunction("All_Filter_Bases", "SkyPal_References", All_Filter_Bases);
    vm->RegisterFunction("All_Filter_Bases_Form_List", "SkyPal_References", All_Filter_Bases_Form_List);
    vm->RegisterFunction("Grid", "SkyPal_References", Grid);

Here is the in game papyrus function

Scriptname SkyPalTestEffectScript extends activemagiceffect  

Event OnEffectStart(actor target, actor caster)

    Actor player = Game.GetPlayer()

    if SkyPal.Has_DLL()
        Debug.MessageBox("SkyPal DLL works and is installed")
    else
        Debug.MessageBox("SkyPal DLL does not work and is not installed")
    endIf    

    ObjectReference playerobject = player as ObjectReference

EndEvent

And here is the output from the crashlog

Unhandled exception "EXCEPTION_ACCESS_VIOLATION" at 0x7FF642D0B0B3 SkyrimSE.exe+030B0B3

The log from the mod is as follows (I truncated the redundant middle sections)

[2024-04-30 21:17:45.139] [log] [trace] [main.cpp:32] SetupLog level set to 0
[2024-04-30 21:17:48.477] [log] [info] [main.cpp:2004] Binding Papyrus Functions
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:131] TES singleton address: 0x28b738d3900
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:136] akRef ID: 232462, Name: Bucket
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:141] Valid reference ID: 232462
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:147] Processed successfully.
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:136] akRef ID: 232407, Name: Goat Cheese Wheel
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:141] Valid reference ID: 232407
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:147] Processed successfully.
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:136] akRef ID: 369645, Name: 
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:141] Valid reference ID: 369645
[2024-04-30 21:18:24.629] [log] [info] [main.cpp:147] Processed successfully.
[2024-04-30 21:18:24.632] [log] [info] [main.cpp:136] akRef ID: 78984, Name: Camilla Valerius
[2024-04-30 21:18:24.632] [log] [info] [main.cpp:141] Valid reference ID: 78984
[2024-04-30 21:18:24.632] [log] [info] [main.cpp:147] Processed successfully.

The logger never reaches the final logging message before the crash. Why is this so?

megapatato commented 6 months ago

Hey there, I've also arrived at this issue.

For SkyPal NG, you're talking about https://github.com/Dylbill-Iroh/Skypal_NG ? The instructions for that use the Vcpck asset maintained in the repo for https://gitlab.com/colorglass/vcpkg-colorglass ; that asset is quite old, the latest commit was about a year ago. @psych0v0yager did you compile SkyPal_NG with a more modern version of CommonLib than what's in the GitLab? (any tips for how to do so? I'm spoiled by Rust and am confused by all these almost-working setup tools; the Conan build's requirements don't agree with what's generated for the CMake system, so I get errors about header-only reqs. that are missing as libraries....)

Anyhow, building Skypal_NG in debug mode against the latest version of CommonLib available in Vcpck https://github.com/CharmedBaryon/CommonLibSSE-NG/tree/c4ab853d095e81e3390b282d7ba01ab2f24ebf25 yields crashes involving the Grid function. My stacks (via CrashLogger) look like the below. I'll put links to the relevant lines, going back in time to the state of the repo. at the relevant commit.

PROBABLE CALL STACK:
    [ 0] 0x7FF77802B0B3      SkyrimSE.exe+030B0B3 -> 20543+0x43 mov rdi, [rsi+0xF8]
    [ 1] 0x7FF96F6FFA89 doticu_skypal.dll+020FA89   add rsp, 0x58 |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\include\REL\Relocation.h:347 ??$invoke@P8TESWorldSpace@RE@@EAAPEAVTESObjectCELL@2@XZPEAV12@@REL@@YAPEAVTESObjectCELL@RE@@$$QEAP8TESWorldSpace@2@EAAPEAV12@XZ$$QEAPEAV32@@Z
    [ 2] 0x7FF96F6FF9D0 doticu_skypal.dll+020F9D0   add rsp, 0x48 |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\include\REL\Relocation.h:1743 ??$?RPEAVTESWorldSpace@RE@@@?$Relocation@P8TESWorldSpace@RE@@EAAPEAVTESObjectCELL@2@XZ@REL@@QEBAPEAVTESObjectCELL@RE@@$$QEAPEAVTESWorldSpace@3@@Z
    [ 3] 0x7FF96F6FF89A doticu_skypal.dll+020F89A   mov rdi, rax |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\src\RE\T\TESWorldSpace.cpp:15 ?GetSkyCell@TESWorldSpace@RE@@QEAAPEAVTESObjectCELL@2@XZ
    [ 4] 0x7FF96F6C4A06 doticu_skypal.dll+01D4A06   mov [rsp+0x160], rax |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\src\RE\T\TES.cpp:41 ?ForEachReference@TES@RE@@QEAAXV?$function@$$A6A?AW4ForEachResult@BSContainer@RE@@AEAVTESObjectREFR@3@@Z@std@@@Z
    [ 5] 0x7FF96F52BD13 doticu_skypal.dll+003BD13   nop |  E:\Git\Skypal_NG\plugin.cpp:132 ?Grid@@YA?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z
    [ 6] 0x7FF96F5AF85A doticu_skypal.dll+00BF85A   mov eax, [rsp+0x20] |  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\type_traits:1715 ??$invoke@AEAP6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@ZPEAU34@$$V@std@@YA?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@0@AEAP6A?AV10@PEAUStaticFunctionTag@RE@@@Z$$QEAPEAU23@@Z
    [ 7] 0x7FF96F626D87 doticu_skypal.dll+0136D87   mov eax, [rsp+0x20] |  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\functional:876 ?_Do_call@?$_Func_impl_no_alloc@P6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@ZV12@PEAU34@@std@@EEAA?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@2@$$QEAPEAUStaticFunctionTag@RE@@@Z C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\functional:876 ?_Do_call@?$_Func_impl_no_alloc@P6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@ZV12@PEAU34@@std@@EEAA?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@2@$$QEAPEAUStaticFunctionTag@RE@@@Z
    [ 8] 0x7FF96F610146 doticu_skypal.dll+0120146   mov eax, [rsp+0x28] |  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\functional:920 ??R?$_Func_class@V?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@std@@QEBA?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@1@PEAUStaticFunctionTag@RE@@@Z
    [ 9] 0x7FF96F5B1553 doticu_skypal.dll+00C1553   mov eax, [rsp+0x20] |  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\type_traits:1715 ??$invoke@AEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@PEAUStaticFunctionTag@RE@@$$V@std@@YA?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@0@AEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@0@$$QEAPEAUStaticFunctionTag@RE@@@Z
    [10] 0x7FF96F551F45 doticu_skypal.dll+0061F45   mov eax, [rsp+0x20] |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\x64-windows-static\include\RE\N\NativeFunction.h:22 ??$CallbackImpl@AEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@V?$tuple@$$V@2@$S$$ZPEAUStaticFunctionTag@RE@@@Impl@BSScript@RE@@YA?A_TAEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@$$QEAV?$tuple@$$V@4@U?$integer_sequence@_K$S@4@$$QEAPEAUStaticFunctionTag@2@@Z
    [11] 0x7FF96F551206 doticu_skypal.dll+0061206   mov eax, [rsp+0x34] |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\x64-windows-static\include\RE\N\NativeFunction.h:31 ??$CallBack@AEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@V?$tuple@$$V@2@PEAUStaticFunctionTag@RE@@@Impl@BSScript@RE@@YA?A_TAEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@$$QEAV?$tuple@$$V@4@$$QEAPEAUStaticFunctionTag@2@@Z E:\Git\Skypal_NG\build\debug\vcpkg_installed\x64-windows-static\include\RE\N\NativeFunction.h:31 ??$CallBack@AEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@V?$tuple@$$V@2@PEAUStaticFunctionTag@RE@@@Impl@BSScript@RE@@YA?A_TAEBV?$function@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@Z@std@@$$QEAV?$tuple@$$V@4@$$QEAPEAUStaticFunctionTag@2@@Z
    [12] 0x7FF96F61889A doticu_skypal.dll+012889A   nop |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\x64-windows-static\include\RE\N\NativeFunction.h:106 ?MarshallAndDispatch@?$NativeFunction@$0A@$$A6A?AV?$vector@PEAVTESObjectREFR@RE@@V?$allocator@PEAVTESObjectREFR@RE@@@std@@@std@@PEAUStaticFunctionTag@RE@@@ZV12@PEAU34@$$V@BSScript@RE@@UEBA_NAEAVVariable@23@AEAVVirtualMachine@Internal@23@I0AEBVStackFrame@23@@Z
    [13] 0x7FF77915DE05      SkyrimSE.exe+143DE05 -> 104651+0x205   test al, al
    [14] 0x7FF96F6C2586 doticu_skypal.dll+01D2586   add rsp, 0x68 |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\include\REL\Relocation.h:347 ??$invoke@P8NativeFunctionBase@NF_util@BSScript@RE@@EAA?AW4CallResult@IFunction@34@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@4@PEAVErrorLogger@34@PEAVVirtualMachine@Internal@34@_N@ZPEAV1234@AEBV74@AEAPEAV834@AEAPEAV9Internal@34@AEA_N@REL@@YA?AW4CallResult@IFunction@BSScript@RE@@$$QEAP8NativeFunctionBase@NF_util@34@EAA?AW41234@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@4@PEAVErrorLogger@34@PEAVVirtualMachine@Internal@34@_N@Z$$QEAPEAV5634@0AEAPEAV834@AEAPEAV9Internal@34@AEA_N@Z
    [15] 0x7FF96F6C2154 doticu_skypal.dll+01D2154   add rsp, 0x58 |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\include\REL\Relocation.h:1743 ??$?RPEAVNativeFunctionBase@NF_util@BSScript@RE@@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@3@AEAPEAVErrorLogger@23@AEAPEAVVirtualMachine@Internal@23@AEA_N@?$Relocation@P8NativeFunctionBase@NF_util@BSScript@RE@@EAA?AW4CallResult@IFunction@34@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@4@PEAVErrorLogger@34@PEAVVirtualMachine@Internal@34@_N@Z@REL@@QEBA?AW4CallResult@IFunction@BSScript@RE@@$$QEAPEAVNativeFunctionBase@NF_util@45@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@5@AEAPEAVErrorLogger@45@AEAPEAVVirtualMachine@Internal@45@AEA_N@Z
    [16] 0x7FF96F6C1BB2 doticu_skypal.dll+01D1BB2   mov rdi, rax |  E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\src\RE\N\NativeFunctionBase.cpp:112 ?Call@NativeFunctionBase@NF_util@BSScript@RE@@UEAA?AW4CallResult@IFunction@34@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@4@PEAVErrorLogger@34@PEAVVirtualMachine@Internal@34@_N@Z E:\Git\Skypal_NG\build\debug\vcpkg_installed\vcpkg\blds\commonlibsse-ng\src\b2f24ebf25-de85b71281.clean\src\RE\N\NativeFunctionBase.cpp:122 ?Call@NativeFunctionBase@NF_util@BSScript@RE@@UEAA?AW4CallResult@IFunction@34@AEBV?$BSTSmartPointer@VStack@BSScript@RE@@UBSTSmartPointerIntrusiveRefCount@3@@4@PEAVErrorLogger@34@PEAVVirtualMachine@Internal@34@_N@Z
    [17] 0x7FF77916EAF9      SkyrimSE.exe+144EAF9 -> 104853+0x789   mov r14d, eax
    [18] 0x7FF77916F7B3      SkyrimSE.exe+144F7B3 -> 104857+0x193   call 0x00007FF7789EA240
    [19] 0x7FF779164314      SkyrimSE.exe+1444314 -> 104767+0x104   lea r8, [rbp+0x67]
    [20] 0x7FF7786E09E7      SkyrimSE.exe+09C09E7 -> 53926+0xEE7    test r15b, r15b
    [21] 0x7FF7783F2CF3      SkyrimSE.exe+06D2CF3 -> 39074+0x23 xor ebx, ebx
    [22] 0x7FF778A17888      SkyrimSE.exe+0CF7888 -> 69378+0xD8 mov ecx, [rbx+0x0C]
    [23] 0x7FF778A17E51      SkyrimSE.exe+0CF7E51 -> 69380+0x361    mov r15d, eax
    [24] 0x7FF778A161DA      SkyrimSE.exe+0CF61DA -> 69344+0x8A movzx eax, byte ptr [rbx+0xA74]
    [25] 0x7FF7789F0DBD      SkyrimSE.exe+0CD0DBD -> 68445+0x3D mov rcx, [0x00007FF77AEEC318]
    [26] 0x7FFA3ADF7344      KERNEL32.DLL+0017344
    [27] 0x7FFA3BF626B1         ntdll.dll+00526B1

The Skypal_NG code appears on index 5, and line 132 of the relevant file does have a ForEachReference member call.

In turn, that points to a call on TES.cpp:41 involving the skyCell.

In turn, that points to a call on TESWoldSpace.cpp:15, which is a scope-closing curly bracket.

Upstream of that, there's calls to Relocation.h:1743 and then Relocation.h:347, which are also scope-closing brackets; so the destructor is causing Exception Access Violations..?

Is the ForEachReference returning things it shouldn't?