diff --git a/.github/workflows/build-amxx-dll.yml b/.github/workflows/build-amxx-dll.yml new file mode 100644 index 00000000..8684fd9a --- /dev/null +++ b/.github/workflows/build-amxx-dll.yml @@ -0,0 +1,44 @@ +name: Build AMXX DLL (x86) + +on: + push: + branches: + - fix-312-metamod-r-abi + workflow_dispatch: + +jobs: + build-amxx: + runs-on: windows-2022 + env: + VCPKG_ROOT: ${{ github.workspace }}/vcpkg + VCPKG_DEFAULT_TRIPLET: x86-windows-static + VCPKG_DEFAULT_HOST_TRIPLET: x86-windows-static + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + fetch-depth: 0 + + - name: Set up vcpkg + uses: lukka/run-vcpkg@v11 + with: + vcpkgJsonGlob: vcpkg.json + vcpkgDirectory: ${{ env.VCPKG_ROOT }} + vcpkgGitCommitId: ef7dbf94b9198bc58f45951adcf1f041fcbc5ea0 + runVcpkgInstall: false + + - name: Configure and build AMXX DLL + uses: lukka/run-cmake@v10 + with: + configurePreset: github-actions + buildPreset: github-actions + buildPresetAdditionalArgs: "['--target', 'bugfixedapi_amxx']" + env: + VCPKG_FORCE_SYSTEM_BINARIES: 1 + + - name: Upload DLL (and debug symbols if present) + uses: actions/upload-artifact@v4 + with: + name: bugfixedapi-amxx-x86 + path: _build/github-actions/**/bugfixedapi_amxx.dll + if-no-files-found: error diff --git a/CMakeLists.txt b/CMakeLists.txt index 64ace799..cffb52a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -457,4 +457,4 @@ add_custom_target( ci-client ) add_dependencies( ci-client client test_client ) add_custom_target( ci-server ) -add_dependencies( ci-server server test_server bugfixedapi_amxx ) +add_dependencies( ci-server server test_server bugfixedapi_amxx test_bugfixedapi_enginefuncs_abi ) diff --git a/src/bugfixedapi_amxx/CMakeLists.txt b/src/bugfixedapi_amxx/CMakeLists.txt index ae4fa716..2831cad4 100644 --- a/src/bugfixedapi_amxx/CMakeLists.txt +++ b/src/bugfixedapi_amxx/CMakeLists.txt @@ -9,6 +9,7 @@ set( SOURCE_FILES amxxmodule.cpp amxxmodule.h + enginefuncs_abi.h moduleconfig.h IGameConfigs.h ITextParsers.h diff --git a/src/bugfixedapi_amxx/amxxmodule.cpp b/src/bugfixedapi_amxx/amxxmodule.cpp index b6769aad..2f0e2040 100644 --- a/src/bugfixedapi_amxx/amxxmodule.cpp +++ b/src/bugfixedapi_amxx/amxxmodule.cpp @@ -18,6 +18,7 @@ #include #include #include "amxxmodule.h" +#include "enginefuncs_abi.h" /************* METAMOD SUPPORT *************/ #ifdef USE_METAMOD @@ -2137,7 +2138,7 @@ C_DLLEXPORT int GetEngineFunctions(enginefuncs_t *pengfuncsFromEngine, int *inte *interfaceVersion = ENGINE_INTERFACE_VERSION; return (FALSE); } - memcpy(pengfuncsFromEngine, &g_EngineFuncs_Table, sizeof(enginefuncs_t)); + metamod_abi::CopyEngineFuncsPrefix(pengfuncsFromEngine, &g_EngineFuncs_Table); g_pengfuncsTable = pengfuncsFromEngine; return TRUE; } @@ -2157,7 +2158,7 @@ C_DLLEXPORT int GetEngineFunctions_Post(enginefuncs_t *pengfuncsFromEngine, int *interfaceVersion = ENGINE_INTERFACE_VERSION; return (FALSE); } - memcpy(pengfuncsFromEngine, &g_EngineFuncs_Post_Table, sizeof(enginefuncs_t)); + metamod_abi::CopyEngineFuncsPrefix(pengfuncsFromEngine, &g_EngineFuncs_Post_Table); g_pengfuncsTable_Post = pengfuncsFromEngine; return TRUE; } @@ -2320,7 +2321,7 @@ C_DLLEXPORT int Meta_Detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) C_DLLEXPORT void WINAPI GiveFnptrsToDll(enginefuncs_t *pengfuncsFromEngine, globalvars_t *pGlobals) { - memcpy(&g_engfuncs, pengfuncsFromEngine, sizeof(enginefuncs_t)); + metamod_abi::InitializeEngineFuncsFromHost(g_engfuncs, pengfuncsFromEngine); gpGlobals = pGlobals; } diff --git a/src/bugfixedapi_amxx/enginefuncs_abi.h b/src/bugfixedapi_amxx/enginefuncs_abi.h new file mode 100644 index 00000000..4d50a9c1 --- /dev/null +++ b/src/bugfixedapi_amxx/enginefuncs_abi.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +namespace metamod_abi +{ +constexpr size_t EngineFuncsPrefixSize = + offsetof(enginefuncs_t, pfnPEntityOfEntIndexAllEntities); + +inline void CopyEngineFuncsPrefix(void *destination, const void *source) +{ + memcpy(destination, source, EngineFuncsPrefixSize); +} + +inline void InitializeEngineFuncsFromHost(enginefuncs_t &destination, const void *source) +{ + destination = {}; + CopyEngineFuncsPrefix(&destination, source); +} +} diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 8832187f..edf9eac3 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -29,6 +29,26 @@ if( BUILD_TESTING ) server/sv_exports.h ) + add_executable( test_bugfixedapi_enginefuncs_abi + bugfixedapi_enginefuncs_abi.cpp + ) + + target_include_directories( test_bugfixedapi_enginefuncs_abi PRIVATE + ${CMAKE_SOURCE_DIR}/src/bugfixedapi_amxx + ${GAME_COMMON_INCLUDE_PATHS} + ${CMAKE_SOURCE_DIR}/external/metamod/metamod + ${SOURCE_SDK_INCLUDE_PATHS} + ) + + target_compile_definitions( test_bugfixedapi_enginefuncs_abi PRIVATE + ${GAME_COMMON_DEFINES} + ${SOURCE_SDK_DEFINES} + USE_METAMOD + HAVE_STDINT_H + MATHLIB_USE_C_ASSERT + MATHLIB_VECTOR_NONTRIVIAL + ) + #----------------------------------------------------------------- add_executable( test_client @@ -111,6 +131,10 @@ if( BUILD_TESTING ) WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/workdir" ) + add_test( NAME server_bugfixedapi_enginefuncs_abi + COMMAND test_bugfixedapi_enginefuncs_abi + ) + set_tests_properties( client server PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=.:$ENV{LD_LIBRARY_PATH}") endif() diff --git a/src/tests/bugfixedapi_enginefuncs_abi.cpp b/src/tests/bugfixedapi_enginefuncs_abi.cpp new file mode 100644 index 00000000..8dd33144 --- /dev/null +++ b/src/tests/bugfixedapi_enginefuncs_abi.cpp @@ -0,0 +1,42 @@ +#include + +#include "extdll.h" +#include "enginefuncs_abi.h" + +static edict_t *StubEntityOfEntIndex(int) +{ + return nullptr; +} + +int main() +{ + enginefuncs_t source{}; + source.pfnPEntityOfEntIndexAllEntities = StubEntityOfEntIndex; + + struct GuardedDestination + { + unsigned char table[metamod_abi::EngineFuncsPrefixSize]; + unsigned char guard[16]; + } guarded; + memset(&guarded, 0xA5, sizeof(guarded)); + + metamod_abi::CopyEngineFuncsPrefix(guarded.table, &source); + if (memcmp(guarded.table, &source, sizeof(guarded.table)) != 0) + return 1; + + for (unsigned char value : guarded.guard) + { + if (value != 0xA5) + return 2; + } + + enginefuncs_t imported{}; + imported.pfnPEntityOfEntIndexAllEntities = StubEntityOfEntIndex; + metamod_abi::InitializeEngineFuncsFromHost(imported, &source); + if (memcmp(&imported, &source, metamod_abi::EngineFuncsPrefixSize) != 0) + return 3; + if (imported.pfnPEntityOfEntIndexAllEntities != nullptr) + return 4; + + return 0; +}