Skip to content

Commit

Permalink
add Ethereum VM architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
aquynh committed Mar 31, 2018
1 parent 5c30212 commit ed1246d
Show file tree
Hide file tree
Showing 24 changed files with 173 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ tests/test_customized_mnem
tests/test_m68k
tests/test_tms320c64x
tests/test_m680x
tests/test_evm

# vim tmp file
*.swp
Expand Down
25 changes: 23 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ option(CAPSTONE_BUILD_DIET "Build diet library" OFF)
option(CAPSTONE_BUILD_TESTS "Build tests" ON)
option(CAPSTONE_USE_DEFAULT_ALLOC "Use default memory allocation functions" ON)

set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X)
set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x)
set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM)
set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM)

list(LENGTH SUPPORTED_ARCHITECTURES count)
math(EXPR count "${count}-1")
Expand Down Expand Up @@ -364,6 +364,23 @@ if (CAPSTONE_M680X_SUPPORT)
set(TEST_SOURCES ${TEST_SOURCES} test_m680x.c)
endif ()

if (CAPSTONE_EVM_SUPPORT)
add_definitions(-DCAPSTONE_HAS_EVM)
set(SOURCES_EVM
arch/EVM/EVMDisassembler.c
arch/EVM/EVMInstPrinter.c
arch/EVM/EVMMapping.c
arch/EVM/EVMModule.c
)
set(HEADERS_EVM
arch/EVM/EVMDisassembler.h
arch/EVM/EVMInstPrinter.h
arch/EVM/EVMMapping.h
arch/EVM/EVMMappingInsn.inc
)
set(TEST_SOURCES ${TEST_SOURCES} test_evm.c)
endif ()

if (CAPSTONE_OSXKERNEL_SUPPORT)
add_definitions(-DCAPSTONE_HAS_OSXKERNEL)
endif ()
Expand All @@ -381,6 +398,7 @@ set(ALL_SOURCES
${SOURCES_M68K}
${SOURCES_TMS320C64X}
${SOURCES_M680X}
${SOURCES_EVM}
)

set(ALL_HEADERS
Expand All @@ -397,6 +415,7 @@ set(ALL_HEADERS
${HEADERS_M68K}
${HEADERS_TMS320C64X}
${HEADERS_M680X}
${HEADERS_EVM}
)

include_directories("${PROJECT_SOURCE_DIR}/include")
Expand Down Expand Up @@ -473,6 +492,7 @@ source_group("Source\\XCore" FILES ${SOURCES_XCORE})
source_group("Source\\M68K" FILES ${SOURCES_M68K})
source_group("Source\\TMS320C64x" FILES ${SOURCES_TMS320C64X})
source_group("Source\\M680X" FILES ${SOURCES_M680X})
source_group("Source\\EVM" FILES ${SOURCES_EVM})

source_group("Include\\Common" FILES ${HEADERS_COMMON})
source_group("Include\\Engine" FILES ${HEADERS_ENGINE})
Expand All @@ -487,6 +507,7 @@ source_group("Include\\XCore" FILES ${HEADERS_XCORE})
source_group("Include\\M68K" FILES ${HEADERS_M68K})
source_group("Include\\TMS320C64x" FILES ${HEADERS_TMS320C64X})
source_group("Include\\M680X" FILES ${HEADERS_MC680X})
source_group("Include\\EVM" FILES ${HEADERS_EVM})

### test library 64bit routine:
get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
Expand Down
13 changes: 8 additions & 5 deletions COMPILE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.

(0) Tailor Capstone to your need.

Out of 9 archtitectures supported by Capstone (Arm, Arm64, M68K, Mips, PPC,
Sparc, SystemZ, XCore & X86), if you just need several selected archs, choose
which ones you want to compile in by editing "config.mk" before going to
next steps.
Out of 12 archtitectures supported by Capstone (Arm, Arm64, M68K, Mips, PPC,
Sparc, SystemZ, XCore, X86, M680X, TMS320C64x & EVM), if you just need several
selected archs, choose the ones you want to compile in by editing "config.mk"
before going to next steps.

By default, all 9 architectures are compiled.
By default, all 12 architectures are compiled.

The other way of customize Capstone without having to edit config.mk is to
pass the desired options on the commandline to ./make.sh. Currently,
Expand Down Expand Up @@ -87,11 +87,14 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.
/usr/include/capstone/x86.h
/usr/include/capstone/arm.h
/usr/include/capstone/arm64.h
/usr/include/capstone/evm.h
/usr/include/capstone/m68k.h
/usr/include/capstone/m680x.h
/usr/include/capstone/mips.h
/usr/include/capstone/ppc.h
/usr/include/capstone/sparc.h
/usr/include/capstone/systemz.h
/usr/include/capstone/tms320c64x.h
/usr/include/capstone/xcore.h
/usr/include/capstone/platform.h
/usr/lib/libcapstone.so (for Linux/*nix), or /usr/lib/libcapstone.dylib (OSX)
Expand Down
7 changes: 5 additions & 2 deletions COMPILE_CMAKE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Get CMake for free from http://www.cmake.org.

(0) Tailor Capstone to your need.

Out of 9 archtitectures supported by Capstone (Arm, Arm64, M68K, Mips, PPC,
Sparc, SystemZ, X86 & XCore), if you just need several selected archs,
Out of 12 archtitectures supported by Capstone (Arm, Arm64, M68K, Mips, PPC,
Sparc, SystemZ, X86, XCore, M680X, TMS320C64x & EVM), if you just need several selected archs,
run "cmake" with the unwanted archs disabled (set to 0) as followings.

- CAPSTONE_ARM_SUPPORT: support ARM. Run cmake with -DCAPSTONE_ARM_SUPPORT=0 to remove ARM.
Expand All @@ -30,6 +30,9 @@ Get CMake for free from http://www.cmake.org.
- CAPSTONE_SYSZ_SUPPORT: support SystemZ. Run cmake with -DCAPSTONE_SYSZ_SUPPORT=0 to remove SystemZ.
- CAPSTONE_XCORE_SUPPORT: support XCore. Run cmake with -DCAPSTONE_XCORE_SUPPORT=0 to remove XCore.
- CAPSTONE_X86_SUPPORT: support X86. Run cmake with -DCAPSTONE_X86_SUPPORT=0 to remove X86.
- CAPSTONE_X86_TMS320C64X: support TMS320C64X. Run cmake with -DCAPSTONE_TMS320C64X_SUPPORT=0 to remove TMS320C64X.
- CAPSTONE_X86_M680X: support M680X. Run cmake with -DCAPSTONE_M680X_SUPPORT=0 to remove M680X.
- CAPSTONE_X86_EVM: support EVM. Run cmake with -DCAPSTONE_EVM_SUPPORT=0 to remove EVM.

By default, all 10 architectures are compiled in.

Expand Down
1 change: 1 addition & 0 deletions MCInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ struct MCInst {
uint8_t ac_idx;
uint8_t popcode_adjust; // Pseudo X86 instruction adjust
char assembly[8]; // for special instruction, so that we dont need printer
unsigned char evm_data[32]; // for EVM PUSH operand
};

void MCInst_Init(MCInst *inst);
Expand Down
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,21 @@ ifneq (,$(findstring m680x,$(CAPSTONE_ARCHS)))
LIBOBJ_M680X += $(LIBSRC_M680X:%.c=$(OBJDIR)/%.o)
endif


DEP_EVM =
DEP_EVM += $(wildcard arch/EVM/EVM*.inc)

LIBOBJ_EVM =
ifneq (,$(findstring evm,$(CAPSTONE_ARCHS)))
CFLAGS += -DCAPSTONE_HAS_EVM
LIBSRC_EVM += $(wildcard arch/EVM/EVM*.c)
LIBOBJ_EVM += $(LIBSRC_EVM:%.c=$(OBJDIR)/%.o)
endif


LIBOBJ =
LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o
LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X)
LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM)
LIBOBJ += $(OBJDIR)/MCInst.o


Expand Down Expand Up @@ -363,6 +375,7 @@ $(LIBOBJ_X86): $(DEP_X86)
$(LIBOBJ_XCORE): $(DEP_XCORE)
$(LIBOBJ_TMS320C64X): $(DEP_TMS320C64X)
$(LIBOBJ_M680X): $(DEP_M680X)
$(LIBOBJ_EVM): $(DEP_EVM)

ifeq ($(CAPSTONE_STATIC),yes)
$(ARCHIVE): $(LIBOBJ)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ disasm engine for binary analysis and reversing in the security community.
Created by Nguyen Anh Quynh, then developed and maintained by a small community,
Capstone offers some unparalleled features:

- Support multiple hardware architectures: ARM, ARM64 (ARMv8), M68K, Mips, PPC,
Sparc, SystemZ, TMS320C64X, M680X, XCore and X86 (including X86_64).
- Support multiple hardware architectures: ARM, ARM64 (ARMv8), Ethereum VM, M68K,
Mips, PPC, Sparc, SystemZ, TMS320C64X, M680X, XCore and X86 (including X86_64).

- Having clean/simple/lightweight/intuitive architecture-neutral API.

Expand Down
5 changes: 4 additions & 1 deletion bindings/const_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

INCL_DIR = '../include/capstone/'

include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.h', 'm680x.h' ]
include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.h', 'm680x.h', 'evm.h' ]

template = {
'java': {
Expand All @@ -25,6 +25,7 @@
'xcore.h': 'Xcore',
'tms320c64x.h': 'TMS320C64x',
'm680x.h': 'M680x',
'evm.h': 'Evm',
'comment_open': '\t//',
'comment_close': '',
},
Expand All @@ -45,6 +46,7 @@
'xcore.h': 'xcore',
'tms320c64x.h': 'tms320c64x',
'm680x.h': 'm680x',
'evm.h': 'evm',
'comment_open': '#',
'comment_close': '',
},
Expand All @@ -65,6 +67,7 @@
'xcore.h': 'xcore',
'tms320c64x.h': 'tms320c64x',
'm680x.h': 'm680x',
'evm.h': 'evm',
'comment_open': '(*',
'comment_close': ' *)',
},
Expand Down
11 changes: 8 additions & 3 deletions bindings/python/capstone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'CS_ARCH_M68K',
'CS_ARCH_TMS320C64X',
'CS_ARCH_M680X',
'CS_ARCH_EVM',
'CS_ARCH_ALL',

'CS_MODE_LITTLE_ENDIAN',
Expand Down Expand Up @@ -149,7 +150,8 @@
CS_ARCH_M68K = 8
CS_ARCH_TMS320C64X = 9
CS_ARCH_M680X = 10
CS_ARCH_MAX = 11
CS_ARCH_EVM = 11
CS_ARCH_MAX = 12
CS_ARCH_ALL = 0xFFFF

# disasm mode
Expand Down Expand Up @@ -327,7 +329,7 @@ def copy_ctypes_list(src):
return [copy_ctypes(n) for n in src]

# Weird import placement because these modules are needed by the below code but need the above functions
from . import arm, arm64, m68k, mips, ppc, sparc, systemz, x86, xcore, tms320c64x, m680x
from . import arm, arm64, m68k, mips, ppc, sparc, systemz, x86, xcore, tms320c64x, m680x, evm

class _cs_arch(ctypes.Union):
_fields_ = (
Expand All @@ -342,6 +344,7 @@ class _cs_arch(ctypes.Union):
('xcore', xcore.CsXcore),
('tms320c64x', tms320c64x.CsTMS320C64x),
('m680x', m680x.CsM680x),
('evm', evm.CsEvm),
)

class _cs_detail(ctypes.Structure):
Expand Down Expand Up @@ -648,6 +651,8 @@ def __gen_detail(self):
(self.condition, self.funit, self.parallel, self.operands) = tms320c64x.get_arch_info(self._raw.detail.contents.arch.tms320c64x)
elif arch == CS_ARCH_M680X:
(self.flags, self.operands) = m680x.get_arch_info(self._raw.detail.contents.arch.m680x)
elif arch == CS_ARCH_EVM:
(self.pop, self.push, self.fee) = evm.get_arch_info(self._raw.detail.contents.arch.evm)


def __getattr__(self, name):
Expand Down Expand Up @@ -1072,7 +1077,7 @@ def debug():
archs = { "arm": CS_ARCH_ARM, "arm64": CS_ARCH_ARM64, "m68k": CS_ARCH_M68K, \
"mips": CS_ARCH_MIPS, "ppc": CS_ARCH_PPC, "sparc": CS_ARCH_SPARC, \
"sysz": CS_ARCH_SYSZ, 'xcore': CS_ARCH_XCORE, "tms320c64x": CS_ARCH_TMS320C64X, \
"m680x": CS_ARCH_M680X }
"m680x": CS_ARCH_M680X, 'evm': CS_ARCH_EVM }

all_archs = ""
keys = archs.keys()
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/setup_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
compile_args = ['-O3', '-fomit-frame-pointer', '-I' + HEADERS_DIR]
link_args = ['-L' + LIBS_DIR]

ext_module_names = ['arm', 'arm_const', 'arm64', 'arm64_const', 'm68k', 'm68k_const', 'm680x', 'm680x_const', 'mips', 'mips_const', 'ppc', 'ppc_const', 'x86', 'x86_const', 'sparc', 'sparc_const', 'systemz', 'sysz_const', 'xcore', 'xcore_const', 'tms320c64x', 'tms320c64x_const']
ext_module_names = ['arm', 'arm_const', 'arm64', 'arm64_const', 'm68k', 'm68k_const', 'm680x', 'm680x_const', 'mips', 'mips_const', 'ppc', 'ppc_const', 'x86', 'x86_const', 'sparc', 'sparc_const', 'systemz', 'sysz_const', 'xcore', 'xcore_const', 'tms320c64x', 'tms320c64x_const', 'evm', 'evm_const' ]

ext_modules = [Extension("capstone.ccapstone",
["pyx/ccapstone.pyx"],
Expand Down
2 changes: 1 addition & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
################################################################################
# Specify which archs you want to compile in. By default, we build all archs.

CAPSTONE_ARCHS ?= arm aarch64 m68k mips powerpc sparc systemz x86 xcore tms320c64x m680x
CAPSTONE_ARCHS ?= arm aarch64 m68k mips powerpc sparc systemz x86 xcore tms320c64x m680x evm


################################################################################
Expand Down
26 changes: 25 additions & 1 deletion cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ extern void Sparc_enable(void);
extern void SystemZ_enable(void);
extern void XCore_enable(void);
extern void TMS320C64x_enable(void);
extern void EVM_enable(void);

static void archs_enable(void)
{
Expand Down Expand Up @@ -107,6 +108,9 @@ static void archs_enable(void)
#ifdef CAPSTONE_HAS_TMS320C64X
TMS320C64x_enable();
#endif
#ifdef CAPSTONE_HAS_EVM
EVM_enable();
#endif


initialized = true;
Expand Down Expand Up @@ -187,7 +191,7 @@ bool CAPSTONE_API cs_support(int query)
(1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
(1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
(1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
(1 << CS_ARCH_M680X));
(1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM));

if ((unsigned int)query < CS_ARCH_MAX)
return all_arch & (1 << query);
Expand Down Expand Up @@ -447,6 +451,9 @@ static uint8_t skipdata_size(cs_struct *handle)
case CS_ARCH_M680X:
// M680X alignment is 1.
return 1;
case CS_ARCH_EVM:
// EVM alignment is 1.
return 1;
}
}

Expand Down Expand Up @@ -1153,6 +1160,13 @@ int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
count++;
break;
case CS_ARCH_EVM:
#if 0
for (i = 0; i < insn->detail->evm.op_count; i++)
if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
count++;
#endif
break;
}

return count;
Expand Down Expand Up @@ -1278,6 +1292,16 @@ int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
return i;
}
break;
case CS_ARCH_EVM:
#if 0
for (i = 0; i < insn->detail->evm.op_count; i++) {
if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
count++;
if (count == post)
return i;
}
#endif
break;
}

return -1;
Expand Down
Loading

0 comments on commit ed1246d

Please sign in to comment.