Skip to content

Commit

Permalink
Merge pull request capstone-engine#537 from 07151129/master
Browse files Browse the repository at this point in the history
OS X kernel memory management support
  • Loading branch information
aquynh committed Nov 11, 2015
2 parents ae7c484 + 0148b04 commit 14e3b78
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ endif

ifeq ($(CAPSTONE_HAS_OSXKERNEL), yes)
CFLAGS += -DCAPSTONE_HAS_OSXKERNEL
SDKROOT ?= $(shell xcodebuild -version -sdk macosx Path)
CFLAGS += -mmacosx-version-min=10.5 \
-isysroot$(SDKROOT) \
-I$(SDKROOT)/System/Library/Frameworks/Kernel.framework/Headers \
-mkernel \
-fno-builtin
endif

CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
Expand Down
7 changes: 7 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ CAPSTONE_STATIC ?= yes
# a shared library.

CAPSTONE_SHARED ?= yes

################################################################################
# Change 'CAPSTONE_HAS_OSXKERNEL = no' to 'CAPSTONE_HAS_OSXKERNEL = yes' to
# enable OS X kernel embedding support. If 'CAPSTONE_USE_SYS_DYN_MEM = yes',
# then kern_os_* functions are used for memory management.

CAPSTONE_HAS_OSXKERNEL ?= no
19 changes: 18 additions & 1 deletion cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "utils.h"
#include "MCRegisterInfo.h"

#ifdef CAPSTONE_USE_SYS_DYN_MEM
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET)
#define INSN_CACHE_SIZE 32
#else
// reduce stack variable size for kernel/firmware
Expand Down Expand Up @@ -79,12 +79,29 @@ static void archs_enable(void)
unsigned int all_arch = 0;

#ifdef CAPSTONE_USE_SYS_DYN_MEM
#ifndef CAPSTONE_HAS_OSXKERNEL
cs_malloc_t cs_mem_malloc = malloc;
cs_calloc_t cs_mem_calloc = calloc;
cs_realloc_t cs_mem_realloc = realloc;
cs_free_t cs_mem_free = free;
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
#else
extern void* kern_os_malloc(size_t size);
extern void kern_os_free(void* addr);
extern void* kern_os_realloc(void* addr, size_t nsize);

static void* cs_kern_os_calloc(size_t num, size_t size)
{
return kern_os_malloc(num * size); // malloc bzeroes the buffer
}

cs_malloc_t cs_mem_malloc = kern_os_malloc;
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
cs_realloc_t cs_mem_realloc = kern_os_realloc;
cs_free_t cs_mem_free = kern_os_free;
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
#endif
#else
cs_malloc_t cs_mem_malloc = NULL;
cs_calloc_t cs_mem_calloc = NULL;
cs_realloc_t cs_mem_realloc = NULL;
Expand Down
1 change: 1 addition & 0 deletions make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ case "$TARGET" in
"ios_armv7" ) build_iOS armv7 $*;;
"ios_armv7s" ) build_iOS armv7s $*;;
"ios_arm64" ) build_iOS arm64 $*;;
"osx-kernel" ) CAPSTONE_USE_SYS_DYN_MEM=yes CAPSTONE_HAS_OSXKERNEL=yes CAPSTONE_ARCHS=x86 CAPSTONE_SHARED=no CAPSTONE_BUILD_CORE_ONLY=yes build $*;;
* ) echo "Usage: make.sh [nix32|cross-win32|cross-win64|cygwin-mingw32|cygwin-mingw64|ios|ios_armv7|ios_armv7s|ios_arm64|cross-android arm|cross-android arm64|clang|gcc|install|uninstall]"; exit 1;;
esac

0 comments on commit 14e3b78

Please sign in to comment.