From d1e8ae40aff51fe196d25f898bf8372121841ed1 Mon Sep 17 00:00:00 2001 From: Scott Ehlert Date: Tue, 24 Nov 2009 07:33:52 -0600 Subject: [PATCH] Fixed crashes in code generated by SourceHook's HookManGen on Mac OS X (bug 3514, r=dvander). This fixes two problems: 1) On OS X structures/objects with sizes of 1, 2, 4, or 8 are returned from functions in registers, not memory addresses. 2) A function returning anything which was 8 bytes in size was crashing. This problem affected all compilers/operating systems. --- core/sourcehook/sourcehook_hookmangen.cpp | 18 ++++++++++++++---- core/sourcehook/test/Makefile | 8 +++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/sourcehook/sourcehook_hookmangen.cpp b/core/sourcehook/sourcehook_hookmangen.cpp index 8578330..ecc1765 100644 --- a/core/sourcehook/sourcehook_hookmangen.cpp +++ b/core/sourcehook/sourcehook_hookmangen.cpp @@ -1,5 +1,5 @@ /* ======== SourceHook ======== -* Copyright (C) 2004-2008 Metamod:Source Development Team +* Copyright (C) 2004-2009 Metamod:Source Development Team * No warranties of any kind * * License: zlib/libpng @@ -873,7 +873,7 @@ namespace SourceHook // mov eax, [ecx] // mov edx, [ecx+4] IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG); - IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4); + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_ECX, 4); } else { @@ -1869,8 +1869,18 @@ namespace SourceHook // MSVC seems to return _all_ structs, classes, unions in memory pi.flags |= PassInfo::PassFlag_RetMem; #elif SH_COMP == SH_COMP_GCC - // Same goes for GCC :) - pi.flags |= PassInfo::PassFlag_RetMem; +#if SH_SYS == SH_SYS_APPLE + // Apple GCC returns in memory if size isn't a power of 2 or > 8 + if ((pi.size & (pi.size - 1)) == 0 && pi.size <= 8) + { + pi.flags |= PassInfo::PassFlag_RetReg; + } + else +#endif + { + // GCC on Linux does same thing as MSVC + pi.flags |= PassInfo::PassFlag_RetMem; + } #endif } } diff --git a/core/sourcehook/test/Makefile b/core/sourcehook/test/Makefile index 8129069..e85f092 100644 --- a/core/sourcehook/test/Makefile +++ b/core/sourcehook/test/Makefile @@ -1,4 +1,4 @@ -#(C)2004-2008 SourceMM Development Team +# (C)2004-2009 Metamod:Source Development Team # Makefile written by David "BAILOPAN" Anderson and Pavol Marko OPT_FLAGS = -O3 -funroll-loops -s -pipe @@ -9,7 +9,7 @@ INCLUDE = -I. -I.. MAX_PARAMS=20 BINARY = sourcehook_test -OBJECTS = main.cpp sourcehook.cpp ../sourcehook_hookmangen.cpp $(shell ls -t test*.cpp) +OBJECTS = main.cpp sourcehook.cpp sourcehook_hookmangen.cpp $(shell ls -t test*.cpp) HEADERS = ../sh_list.h ../sh_tinyhash.h ../sh_memory.h ../sh_string.h ../sh_vector.h ../sourcehook_impl.h ../FastDelegate.h ../sourcehook.h ../sh_memfuncinfo.h ifeq "$(DEBUG)" "true" @@ -46,10 +46,12 @@ debug: all: mkdir -p $(BIN_DIR) - ln -sf ../sourcehook.cpp sourcehook.cpp + ln -sf ../sourcehook.cpp + ln -sf ../sourcehook_hookmangen.cpp $(MAKE) $(BINARY) rm -f $(BINARY) rm -f sourcehook.cpp + rm -f sourcehook_hookmangen.cpp ln -sf $(BIN_DIR)/$(BINARY) $(BINARY)