Detect mismatch between cxbxr modules

This commit is contained in:
ergo720 2021-04-04 17:55:02 +02:00
parent 52c88d7462
commit 1e0c75c3bd
8 changed files with 58 additions and 6 deletions

View File

@ -25,6 +25,8 @@ endif()
file (GLOB HEADERS
"${CXBXR_ROOT_DIR}/src/common/AddressRanges.h"
"${CXBXR_ROOT_DIR}/src/common/ReserveAddressRanges.h"
"${CXBXR_ROOT_DIR}/src/CxbxVersion.h"
"${CXBXR_ROOT_DIR}/src/version.h"
)
file (GLOB SOURCES

View File

@ -1,5 +1,14 @@
#pragma once
#include "version.h"
#include <string>
extern const char* CxbxVersionStr;
// Note: GitVersionMaxLength should be large enough to accomodate the longest git version string we can practically expect to have. This is necessary
// to avoid possible mismatches in the string length which can happen if the user mixes different cxbxr versions
inline constexpr const char *CxbxGitVersion = _GIT_VERSION;
inline constexpr size_t GitVersionLength = std::char_traits<char>::length(CxbxGitVersion);
inline constexpr size_t GitVersionMaxLength = 80;
static_assert(GitVersionLength < GitVersionMaxLength);

View File

@ -168,6 +168,7 @@ EmuShared::EmuShared()
for (auto& i : m_DeviceType) {
i = to_underlying(XBOX_INPUT_DEVICE::DEVICE_INVALID);
}
std::strncpy(m_git_version, CxbxGitVersion, GitVersionLength);
}
// ******************************************************************

View File

@ -30,8 +30,8 @@
#include "Mutex.h"
#include "common\IPCHybrid.hpp"
#include "common\input\Button.h"
#include "CxbxVersion.h"
#include "core/common/imgui/settings.h"
#include <memory.h>
extern HMODULE hActiveModule; // Equals EXE Module handle in (GUI) Cxbx.exe / cxbxr.exe, equals DLL Module handle in cxbxr-emu.dll
@ -66,6 +66,11 @@ class EmuShared : public Mutex
// ******************************************************************
unsigned int m_size;
// ******************************************************************
// * Git version string of cxbx.exe
// ******************************************************************
char m_git_version[GitVersionMaxLength];
// ******************************************************************
// * Each process needs to call this to initialize shared memory
// ******************************************************************
@ -286,6 +291,16 @@ class EmuShared : public Mutex
void GetOverlaySettings(overlay_settings *value) { Lock(); *value = m_imgui_overlay_settings; Unlock(); }
void SetOverlaySettings(const overlay_settings* value) { Lock(); m_imgui_overlay_settings = *value; Unlock(); }
// ******************************************************************
// * Git version Accessor (only the get method is provided because it should not be changed
// ******************************************************************
void GetGitVersion(char *value)
{
Lock();
std::strncpy(value, m_git_version, GitVersionLength + 1);
Unlock();
}
// ******************************************************************
// * Reset specific variables to default for kernel mode.
// ******************************************************************
@ -329,9 +344,8 @@ class EmuShared : public Mutex
bool m_bEmulating_status;
#ifndef CXBX_LOADER // Temporary usage for cxbx.exe's emu
unsigned int m_PreviousMmLayout;
int m_Reserved7[3];
#else
int m_Reserved7[4];
unsigned int m_Reserved;
#endif
bool m_bFirstLaunch;
bool m_bClipCursor;

View File

@ -685,6 +685,17 @@ bool HandleFirstLaunch()
void CxbxKrnlEmulate(unsigned int reserved_systems, blocks_reserved_t blocks_reserved)
{
#ifdef CXBXR_EMU
// First of all, check if the emulation dll version matches the gui version and abort otherwise
char GitVersionGui[GitVersionMaxLength];
g_EmuShared->GetGitVersion(GitVersionGui);
if (std::strncmp(GitVersionGui, CxbxGitVersion, GitVersionLength) != 0) {
PopupError(nullptr, "Mismatch detected between cxbx.exe and cxbxr-emu.dll, aborting.");
CxbxKrnlShutDown();
return;
}
#endif
std::string tempStr;
// NOTE: This is designated for standalone kernel mode launch without GUI

View File

@ -136,9 +136,6 @@ extern "C" {
extern Xbe::Certificate *g_pCertificate;
/*! validate version string match */
bool CxbxKrnlVerifyVersion(const char *szVersion);
extern bool g_bIsDebugKernel;
bool CreateSettings();

View File

@ -166,6 +166,15 @@ DWORD WINAPI Emulate(unsigned int reserved_systems, blocks_reserved_t blocks_res
return EXIT_FAILURE;
}
// Check if the loader version matches the gui version and abort otherwise
char GitVersionGui[GitVersionMaxLength];
g_EmuShared->GetGitVersion(GitVersionGui);
if (std::strncmp(GitVersionGui, reinterpret_cast<char *>(PHYSICAL_MAP1_BASE + 0x1000), GitVersionLength) != 0) {
PopupError(nullptr, "Mismatch detected between cxbx.exe and cxbxr-ldr.exe, aborting.");
EmuShared::Cleanup();
return EXIT_FAILURE;
}
if (!HandleFirstLaunch()) {
PopupError(nullptr, "First launch failed!");
EmuShared::Cleanup();

View File

@ -28,7 +28,9 @@
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <Windows.h> // For LPTSTR, FormatMessage, GetSystemInfo, etc
#include "strsafe.h" // For StringCchCopy
#include "..\CxbxVersion.h"
#include "..\Common\AddressRanges.h"
#include "..\Common\ReserveAddressRanges.h"
@ -193,6 +195,13 @@ DWORD CALLBACK rawMain()
return ERROR_RESOURCE_NOT_FOUND;
}
// We cannot just pass the gui version of the loader via the Emulate function. This, because if the user mixes a version which does the check (and thus expects 3 arguments)
// with an old version which doesn't do the check (and thus only has 2 arguments), the behavior will be undefined since the new version will attempt to use a
// non-existent argument. We instead pass the version string in the contiguous memory, which must have been successfully reserved by now or else the loader would
// have already aborted execution. This memory is backed by the paging file, and thus its contents will always be initialized to zero. Thus, in the above scenerio
// the check will fail because a version string cannot be zero.
StringCchCopy(reinterpret_cast<char *>(PHYSICAL_MAP1_BASE + 0x1000), GitVersionLength + 1, CxbxGitVersion);
// Find the main emulation function in our DLL
typedef void (WINAPI *Emulate_t)(unsigned int, blocks_reserved_t);
Emulate_t pfnEmulate = (Emulate_t)GetProcAddress(hEmulationDLL, "Emulate");