From 3a61f8f2700862fba0eceeb51381f006816bad5e Mon Sep 17 00:00:00 2001 From: Zeex Date: Fri, 7 Sep 2018 18:53:56 +0600 Subject: [PATCH] Fix C++ test crash because of misaligned stack --- tests/CMakeLists.txt | 3 +-- tests/foo.cpp | 5 +++++ tests/foo.sh | 7 ------- tests/foo_32.asm | 8 ++++---- tests/foo_main.cpp | 6 ++++++ tests/test.c | 3 ++- tests/test.cpp | 3 ++- 7 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 tests/foo.cpp delete mode 100644 tests/foo.sh create mode 100644 tests/foo_main.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 92c8461..2a2ab84 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,8 +24,7 @@ else() endif() set(options "-f" "${asm_format}") -if(APPLE - OR (WIN32 AND (CMAKE_SIZEOF_VOID_P EQUAL 4 OR SUBHOOK_FORCE_32BIT))) +if(APPLE OR (WIN32 AND (CMAKE_SIZEOF_VOID_P EQUAL 4 OR SUBHOOK_FORCE_32BIT))) list(APPEND options "--prefix=_") endif() if(CMAKE_SYSTEM_NAME MATCHES "Linux") diff --git a/tests/foo.cpp b/tests/foo.cpp new file mode 100644 index 0000000..c3e7def --- /dev/null +++ b/tests/foo.cpp @@ -0,0 +1,5 @@ +#include + +extern "C" void foo() { + puts("foo() called"); +} diff --git a/tests/foo.sh b/tests/foo.sh deleted file mode 100644 index 0f0e940..0000000 --- a/tests/foo.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -yasm -f elf64 foo_64_unix.asm -gcc -c foo_main.c -o foo_main.o -gcc foo_64_unix.o foo_main.o -o foo - -./foo diff --git a/tests/foo_32.asm b/tests/foo_32.asm index 16a2533..f1de5b3 100644 --- a/tests/foo_32.asm +++ b/tests/foo_32.asm @@ -1,7 +1,7 @@ extern puts global foo -section .data +section .rodata message: db 'foo() called', 0 @@ -21,11 +21,11 @@ section .text %define nop9 db 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 ; 66 NOP DWORD ptr [EAX + EAX*1 + 00000000H] foo: - nop push ebp + mov ebp, esp + sub esp, 4 ; align the stack to a 16-byte boundary push message call puts - add esp, 4 + mov esp, ebp pop ebp - xor eax, eax ret diff --git a/tests/foo_main.cpp b/tests/foo_main.cpp new file mode 100644 index 0000000..b49a3cc --- /dev/null +++ b/tests/foo_main.cpp @@ -0,0 +1,6 @@ +extern "C" void foo(void); + +int main() { + foo(); + return 0; +} diff --git a/tests/test.c b/tests/test.c index 513c060..bbf75b5 100644 --- a/tests/test.c +++ b/tests/test.c @@ -11,7 +11,8 @@ typedef void (*foo_func_t)(void); #elif defined SUBHOOK_UNIX #define FOO_CALL __attribute__((cdecl)) #endif -#else +#endif +#ifndef FOO_CALL #define FOO_CALL #endif diff --git a/tests/test.cpp b/tests/test.cpp index 0eee1fa..5536a36 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -9,7 +9,8 @@ typedef void (*foo_func_t)(); #elif defined SUBHOOK_UNIX #define FOO_CALL __attribute__((cdecl)) #endif -#else +#endif +#ifndef FOO_CALL #define FOO_CALL #endif