Skip to content

Commit

Permalink
Fixed unsafe macro expansion behavior on SK_ReleaseAssert, SK_LOG* an…
Browse files Browse the repository at this point in the history
…d SK_RunOnce
  • Loading branch information
Kaldaien committed Oct 1, 2023
1 parent 7eaa2fd commit d2e5ee6
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 179 deletions.
16 changes: 9 additions & 7 deletions include/SpecialK/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ std::wstring
__stdcall
SK_SummarizeCaller (LPVOID lpReturnAddr = _ReturnAddress ());

#define SK_LOG_CALL(source) { \
#define SK_LOG_CALL(source) do { \
char szSymbol [1024] = { }; \
ULONG ulLen = 1024; \
\
Expand All @@ -158,27 +158,29 @@ SK_SummarizeCaller (LPVOID lpReturnAddr = _ReturnAddress ());
__FUNCTIONW__, \
SK_SummarizeCaller ().c_str () \
); \
}
} while (0)


#define SK_ARGS(...) __VA_ARGS__
#define SK_STRIP_PARENTHESIS(X) X
#define SK_VARIADIC(X) SK_STRIP_PARENTHESIS( SK_ARGS X )

#define SK_LOG_EX(level,first,expr) \
if (config.system.log_level >= (level)) \
dll_log->LogEx (first, SK_VARIADIC (expr))
#define SK_LOG_EX(level,first,expr) do { \
if (config.system.log_level >= (level)) \
dll_log->LogEx (first, SK_VARIADIC (expr)); \
} while (0)

#define SK_LOG0_EX(first,expr) SK_LOG_EX(0,first,expr)
#define SK_LOG1_EX(first,expr) SK_LOG_EX(1,first,expr)
#define SK_LOG2_EX(first,expr) SK_LOG_EX(2,first,expr)
#define SK_LOG3_EX(first,expr) SK_LOG_EX(3,first,expr)
#define SK_LOG4_EX(first,expr) SK_LOG_EX(4,first,expr)

#define SK_LOG(expr,level,source) \
#define SK_LOG(expr,level,source) do { \
if (config.system.log_level >= level) \
dll_log->Log (L"[" source L"] " \
SK_VARIADIC (expr))
SK_VARIADIC (expr)); \
} while (0)

#define SK_LOG0(expr,src) SK_LOG(expr,0,src)
#define SK_LOG1(expr,src) SK_LOG(expr,1,src)
Expand Down
14 changes: 7 additions & 7 deletions include/SpecialK/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,21 @@ SK_GetBitness (void)

// Avoid the C++ stdlib and use CPU interlocked instructions instead, so this
// is safe to use even by parts of the DLL that run before the CRT initializes
#define SK_RunOnce(x) { static volatile LONG __once = TRUE; \
if (InterlockedCompareExchange (&__once, FALSE, TRUE)) { x; } }
#define SK_RunOnce(x) do { static volatile LONG __once = TRUE; \
if (InterlockedCompareExchange (&__once, FALSE, TRUE)) { x; } } while (0)
static inline auto
SK_RunOnceEx =
[](auto x){ static std::once_flag the_wuncler;
std::call_once ( the_wuncler, [&]{ (x)(); }); };

#define SK_RunIf32Bit(x) { SK_GetBitness () == ThirtyTwoBit ? (x) : 0; }
#define SK_RunIf64Bit(x) { SK_GetBitness () == SixtyFourBit ? (x) : 0; }
#define SK_RunLHIfBitness(b,l,r) SK_GetBitness () == (b) ? (l) : (r)
#define SK_RunLHIfBitness(b,l,r) (SK_GetBitness () == (b) ? (l) : (r))


#define SK_LOG_FIRST_CALL { SK_RunOnce ({ \
#define SK_LOG_FIRST_CALL SK_RunOnce ({ \
SK_LOG0 ( (L"[!] > First Call: %34s", __FUNCTIONW__), __SK_SUBSYSTEM__); \
SK_LOG1 ( (L" <*> %s", SK_SummarizeCaller ().c_str ()), __SK_SUBSYSTEM__); }); }
SK_LOG1 ( (L" <*> %s", SK_SummarizeCaller ().c_str ()), __SK_SUBSYSTEM__); });


void SK_ImGui_Warning (const wchar_t* wszMessage);
Expand Down Expand Up @@ -395,9 +395,9 @@ void SK_ImGui_WarningWithTitle (const wchar_t* wszMessage,
L"First-Chance Assertion Failure" ) \
); } } }

#define SK_ReleaseAssert(expr) { SK_ReleaseAssertEx ( (expr),L#expr, \
#define SK_ReleaseAssert(expr) do { SK_ReleaseAssertEx ( (expr),L#expr, \
__FILEW__,__LINE__, \
__FUNCSIG__ ) }
__FUNCSIG__ ) } while (0)


struct SK_ThreadSuspension_Ctx {
Expand Down
2 changes: 1 addition & 1 deletion src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2975,7 +2975,7 @@ SK_FrameCallback ( SK_RenderBackend& rb,

game_window.active |=
(SK_GetForegroundWindow () == game_window.hWnd);
})
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics/debug_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ SK_Debug_LoadHelper (void)
else
SK_Thread_SpinUntilAtomicMin (&__init, 2);

SK_ReleaseAssert (hModDbgHelp != nullptr)
SK_ReleaseAssert (hModDbgHelp != nullptr);

return
hModDbgHelp;
Expand Down
30 changes: 15 additions & 15 deletions src/diagnostics/load_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ SK_TraceLoadLibrary ( HMODULE hCallingMod,
// NVIDIA's User-Mode D3D Frontend
StrStrI (lpFileName, SK_TEXT("nvd3dum.dll")) ||
StrStrIW (wszModName, L"nvd3dum.dll") ) )
SK_RunOnce (SK_BootD3D9 ())
SK_RunOnce (SK_BootD3D9 ());
#ifdef _M_IX86
else if ( (! (SK_GetDLLRole () & DLL_ROLE::D3D8)) && config.apis.d3d8.hook &&
( StrStrI (lpFileName, SK_TEXT("d3d8.dll")) ||
Expand All @@ -477,50 +477,50 @@ SK_TraceLoadLibrary ( HMODULE hCallingMod,
else if ( (! (SK_GetDLLRole () & DLL_ROLE::DXGI)) && config.apis.dxgi.d3d11.hook &&
( StrStrI (lpFileName, SK_TEXT("d3d11.dll")) ||
StrStrIW (wszModName, L"d3d11.dll") ))
SK_RunOnce (SK_BootDXGI ())
SK_RunOnce (SK_BootDXGI ());
else if ( (! (SK_GetDLLRole () & DLL_ROLE::DXGI)) && config.apis.dxgi.d3d11.hook &&
( StrStrI (lpFileName, SK_TEXT("dxcore.dll")) || // Unity?! WTF are you doing?
StrStrIW (wszModName, L"dxcore.dll") ))
SK_RunOnce (SK_BootDXGI ())
SK_RunOnce (SK_BootDXGI ());
else if ( (! (SK_GetDLLRole () & DLL_ROLE::DXGI)) && config.apis.dxgi.d3d12.hook &&
( StrStrI (lpFileName, SK_TEXT("d3d12.dll")) ||
StrStrIW (wszModName, L"d3d12.dll") ))
SK_RunOnce (SK_BootDXGI ())
SK_RunOnce (SK_BootDXGI ());
#ifdef _M_AMD64
else if ( StrStrI (lpFileName, SK_TEXT("vulkan-1.dll")) ||
StrStrIW (wszModName, L"vulkan-1.dll") )
SK_RunOnce (SK_BootVulkan ())
SK_RunOnce (SK_BootVulkan ());
#endif
else if ( (! (SK_GetDLLRole () & DLL_ROLE::OpenGL)) && config.apis.OpenGL.hook &&
( StrStrI (lpFileName, SK_TEXT("OpenGL32.dll")) ||
StrStrIW (wszModName, L"OpenGL32.dll") ))
SK_RunOnce (SK_BootOpenGL ())
SK_RunOnce (SK_BootOpenGL ());
else if ( //SK_XInput_LinkedVersion.empty () &&
StrStrI (lpFileName, SK_TEXT("xinput1_3.dll")) )
SK_RunOnce (SK_Input_HookXInput1_3 ())
SK_RunOnce (SK_Input_HookXInput1_3 ());
else if ( //SK_XInput_LinkedVersion.empty () &&
StrStrI (lpFileName, SK_TEXT("xinput1_4.dll")) )
SK_RunOnce (SK_Input_HookXInput1_4 ())
SK_RunOnce (SK_Input_HookXInput1_4 ());
else if ( //SK_XInput_LinkedVersion.empty () &&
StrStrI (lpFileName, SK_TEXT("xinput9_1_0.dll")) )
SK_RunOnce (SK_Input_HookXInput9_1_0 ())
SK_RunOnce (SK_Input_HookXInput9_1_0 ());
else if ( StrStrI (lpFileName, SK_TEXT("dinput8.dll")) )
SK_RunOnce (SK_Input_HookDI8 ())
SK_RunOnce (SK_Input_HookDI8 ());
else if ( StrStrI (lpFileName, SK_TEXT("dinput.dll")) )
SK_RunOnce (SK_Input_HookDI7 ())
SK_RunOnce (SK_Input_HookDI7 ());
else if ( StrStrI (lpFileName, SK_TEXT("hid.dll")) )
SK_RunOnce (SK_Input_HookHID ())
SK_RunOnce (SK_Input_HookHID ());
else if ( StrStrI ( lpFileName, SK_TEXT("EOSSDK-Win")) ||
StrStrIW (wszModName, L"EOSSDK-Win") )
SK_RunOnce (SK::EOS::Init (false))
SK_RunOnce (SK::EOS::Init (false));
else if ( StrStrI ( lpFileName, SK_TEXT("libScePad")) ||
StrStrIW (wszModName, L"libScePad") )
SK_RunOnce (SK_Input_HookScePad ())
SK_RunOnce (SK_Input_HookScePad ());
else if ( StrStrI ( lpFileName, SK_TEXT("dstorage.dll")) ||
StrStrIW (wszModName, L"dstorage.dll") )
{
extern void SK_DStorage_Init (void);
SK_RunOnce (SK_DStorage_Init ())
SK_RunOnce (SK_DStorage_Init ());
}
else if ( StrStrI ( lpFileName, SK_TEXT("sl.interposer.dll")) ||
StrStrIW (wszModName, L"sl.interposer.dll") )
Expand Down
11 changes: 3 additions & 8 deletions src/imgui/backends/imgui_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1996,9 +1996,7 @@ SK_D3D12_RenderCtx::init (IDXGISwapChain3 *pSwapChain, ID3D12CommandQueue *pComm

SK_ComPtr <ID3D12Device> pNativeDev12;
if (SK_slGetNativeInterface (_pDevice.p, (void **)&pNativeDev12.p) == sl::Result::eOk)
{
_pDevice = pNativeDev12;
}
_pDevice = pNativeDev12;
}

if (_pDevice.p != nullptr)
Expand All @@ -2024,14 +2022,11 @@ SK_D3D12_RenderCtx::init (IDXGISwapChain3 *pSwapChain, ID3D12CommandQueue *pComm
_pCommandQueue = pCommandQueue;

if (_pCommandQueue != nullptr)
{
_pCommandQueue->GetDevice (IID_PPV_ARGS (&_pDevice.p));
{ _pCommandQueue->GetDevice (IID_PPV_ARGS (&_pDevice.p));

SK_ComPtr <ID3D12Device> pNativeDev12;
if (SK_slGetNativeInterface (_pDevice, (void **)&pNativeDev12.p) == sl::Result::eOk)
{
_pDevice = pNativeDev12;
}
_pDevice = pNativeDev12;
}
#endif
}
Expand Down
22 changes: 11 additions & 11 deletions src/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ iSK_INI::reload (const wchar_t *fname)

if (fname == nullptr)
{
SK_ReleaseAssert (name.size () > 0)
SK_ReleaseAssert (name.size () > 0);
if ( name.empty ()) return false; // Empty String -> Dummy INI

fname = name.c_str ();
Expand Down Expand Up @@ -151,11 +151,11 @@ iSK_INI::reload (const wchar_t *fname)
sk::narrow_cast <long> (SK_File_GetSize (fname));

// A 4 MiB INI file seems pretty dman unlikely...
SK_ReleaseAssert (size >= 0 && size < (4L * 1024L * 1024L))
SK_ReleaseAssert (size >= 0 && size < (4L * 1024L * 1024L));

data.resize (size + 3);

SK_ReleaseAssert (data.size () > 0)
SK_ReleaseAssert (data.size () > 0);

if (data.size () == 0)
{
Expand Down Expand Up @@ -220,7 +220,7 @@ iSK_INI::reload (const wchar_t *fname)
char* string =
pTLS->scratch_memory->ini.utf8_string.alloc (real_size + 3, true);

SK_ReleaseAssert (string != nullptr)
SK_ReleaseAssert (string != nullptr);

if (string == nullptr)
{
Expand Down Expand Up @@ -253,7 +253,7 @@ iSK_INI::reload (const wchar_t *fname)
if (data.size () < converted_size + 3)
data.resize (converted_size + 3);

SK_ReleaseAssert (data.size () > 0)
SK_ReleaseAssert (data.size () > 0);

if (data.size () > 0)
{
Expand Down Expand Up @@ -284,7 +284,7 @@ iSK_INI::iSK_INI (const wchar_t* filename)
AddRef ();

SK_ReleaseAssert (
filename != nullptr)
filename != nullptr);
if ( filename == nullptr) return;
if ( *filename == L'\0' ) return; // Empty String -> Dummy INI

Expand All @@ -308,7 +308,7 @@ iSK_INI::~iSK_INI (void)
ULONG refs =
Release ();

SK_ReleaseAssert (refs == 0) // Memory leak?
SK_ReleaseAssert (refs == 0); // Memory leak?

if (refs == 0)
{
Expand Down Expand Up @@ -388,7 +388,7 @@ Process_Section ( iSK_INISection &kSection,

for (wchar_t* l = value; l <= end; l < end ? l = CharNextW (l) : nullptr)
{
SK_ReleaseAssert (l != nullptr)
SK_ReleaseAssert (l != nullptr);

if (l == nullptr) break;

Expand Down Expand Up @@ -516,7 +516,7 @@ void
__stdcall
iSK_INI::parse (void)
{
SK_ReleaseAssert (data.size () > 0)
SK_ReleaseAssert (data.size () > 0);

SK_TLS* pTLS =
SK_TLS_Bottom ();
Expand Down Expand Up @@ -1202,7 +1202,7 @@ iSK_INI::get_section_f ( _In_z_ _Printf_format_string_
// ASSERT: Length <= 127 characters
len += vswprintf (wszFormatted, _Format, _ArgList);

SK_ReleaseAssert (len <= 127)
SK_ReleaseAssert (len <= 127);
}
va_end (_ArgList);

Expand Down Expand Up @@ -1684,7 +1684,7 @@ iSK_INI::import_file (const wchar_t* fname)
}

SK_ReleaseAssert (
wszImportData != nullptr)
wszImportData != nullptr);
if (wszImportData != nullptr)
{
MultiByteToWideChar ( CP_UTF8, 0, string, real_size,
Expand Down
4 changes: 2 additions & 2 deletions src/input/xinput_hotplug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ SK_XInput_NotifyDeviceArrival (void)

SK_ReleaseAssert (
pDevHdr->dbch_size >= sizeof (DEV_BROADCAST_DEVICEINTERFACE_W)
)
);

DEV_BROADCAST_DEVICEINTERFACE_W *pDev =
(DEV_BROADCAST_DEVICEINTERFACE_W *)pDevHdr;
Expand Down Expand Up @@ -346,7 +346,7 @@ SK_XInput_NotifyDeviceArrival (void)
return 0;
}, L"[SK] HID Hotplug Dispatch", (LPVOID)SK_XInputHot_NotifyEvent
);
})
});
}


Expand Down
2 changes: 1 addition & 1 deletion src/plugins/persona4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,5 @@ SK_Persona4_InitPlugin (void)

plugin_mgr->config_fns.insert (SK_Persona4_PlugInCfg);
plugin_mgr->end_frame_fns.insert (SK_Persona4_EndFrame);
})
});
}
2 changes: 1 addition & 1 deletion src/render/d3d11/d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8503,7 +8503,7 @@ SK_D3D11_EndFrame (SK_TLS* pTLS)
// * This is recoverable and by the next full frame
// all of SK's Critical Sections will be setup
//
SK_ReleaseAssert (cs_render_view != nullptr)
SK_ReleaseAssert (cs_render_view != nullptr);

if (!cs_render_view) // Skip this frame, we'll get it
{ // on the next go-around.
Expand Down
2 changes: 1 addition & 1 deletion src/render/d3d11/d3d11_screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ struct ShaderBase
{
#pragma warning (push)
#pragma warning (disable: 4130) // No @#$% sherlock
SK_ReleaseAssert ("WTF?!" == nullptr)
SK_ReleaseAssert ("WTF?!" == nullptr);
#pragma warning (pop)
}

Expand Down
2 changes: 1 addition & 1 deletion src/render/d3d11/d3d11_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ SK_D3D11_SetShaderResources_Impl (
// Static analysis seems to think this is possible:
// ( Both are FALSE or Both are TRUE ),
// but I am pretty sure it never happens.
SK_ReleaseAssert (hooked ^ ( _vftable != nullptr ) )
SK_ReleaseAssert (hooked ^ ( _vftable != nullptr ) );

if ((! hooked) && (! _vftable))
return;
Expand Down
2 changes: 1 addition & 1 deletion src/render/d3d12/d3d11on12_shader_restitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ struct ShaderBase
{
#pragma warning (push)
#pragma warning (disable: 4130) // No @#$% sherlock
SK_ReleaseAssert ("WTF?!" == nullptr)
SK_ReleaseAssert ("WTF?!" == nullptr);
#pragma warning (pop)
}

Expand Down
16 changes: 4 additions & 12 deletions src/render/d3d12/d3d12_command_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,10 @@ _InstallCommandQueueHooksImpl (ID3D12Device* pDevice12)

if (bHasStreamline)
{
SK_LOGi0 (L"Hooking Streamline Native Interface for ID3D12CommandQueue...");

if (SK_slGetNativeInterface (pDevice12, (void **)&pDev12.p) != sl::Result::eOk)
{
SK_LOGi0 (L"Failed to Get Native Interface for D3D12 Device!");

pDev12 = pDevice12;
}
}

else
pDev12 = pDevice12;
if (SK_slGetNativeInterface (pDevice12, (void **)&pDev12.p) == sl::Result::eOk)
SK_LOGi0 (L"Hooking Streamline Native Interface for ID3D12CommandQueue...");
else pDev12 = pDevice12;
} else pDev12 = pDevice12;

SK_ComPtr < ID3D12CommandQueue > p12Queue;
D3D12_COMMAND_QUEUE_DESC queue_desc = { };
Expand Down
Loading

0 comments on commit d2e5ee6

Please sign in to comment.