Python bindings: Changes to how structures are copied so that capstone works correctly in PyPy

This commit is contained in:
Andrew Dutcher 2016-06-09 19:04:27 -07:00
parent 2d94c511ab
commit 1647720656
9 changed files with 37 additions and 25 deletions

View File

@ -4,7 +4,6 @@ from platform import system
_python2 = sys.version_info[0] < 3
if _python2:
range = xrange
from . import arm, arm64, mips, ppc, sparc, systemz, x86, xcore
__all__ = [
'Cs',
@ -251,6 +250,19 @@ if _found == False:
# low-level structure for C code
def copy_ctypes(src):
"""Returns a new ctypes object which is a bitwise copy of an existing one"""
dst = type(src)()
ctypes.memmove(ctypes.byref(dst), ctypes.byref(src), ctypes.sizeof(type(src)))
return dst
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, mips, ppc, sparc, systemz, x86, xcore
class _cs_arch(ctypes.Union):
_fields_ = (
('arm64', arm64.CsArm64),
@ -435,14 +447,6 @@ def cs_disasm_lite(arch, mode, code, offset, count=0):
raise CsError(status)
# alternately
def copy_ctypes(src):
"""Returns a new ctypes object which is a bitwise copy of an existing one"""
dst = type(src)()
ctypes.pointer(dst)[0] = src
return dst
# Python-style class to disasm code
class CsInsn(object):
def __init__(self, cs, all_info):

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .arm_const import *
# define the API
@ -74,5 +75,5 @@ class CsArm(ctypes.Structure):
def get_arch_info(a):
return (a.usermode, a.vector_size, a.vector_data, a.cps_mode, a.cps_flag, a.cc, a.update_flags, \
a.writeback, a.mem_barrier, copy.deepcopy(a.operands[:a.op_count]))
a.writeback, a.mem_barrier, copy_ctypes_list(a.operands[:a.op_count]))

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .arm64_const import *
# define the API
@ -84,5 +85,5 @@ class CsArm64(ctypes.Structure):
)
def get_arch_info(a):
return (a.cc, a.update_flags, a.writeback, copy.deepcopy(a.operands[:a.op_count]))
return (a.cc, a.update_flags, a.writeback, copy_ctypes_list(a.operands[:a.op_count]))

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .mips_const import *
# define the API
@ -43,5 +44,5 @@ class CsMips(ctypes.Structure):
)
def get_arch_info(a):
return copy.deepcopy(a.operands[:a.op_count])
return copy_ctypes_list(a.operands[:a.op_count])

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .ppc_const import *
# define the API
@ -58,5 +59,5 @@ class CsPpc(ctypes.Structure):
)
def get_arch_info(a):
return (a.bc, a.bh, a.update_cr0, copy.deepcopy(a.operands[:a.op_count]))
return (a.bc, a.bh, a.update_cr0, copy_ctypes_list(a.operands[:a.op_count]))

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .sparc_const import *
# define the API
@ -46,5 +47,5 @@ class CsSparc(ctypes.Structure):
)
def get_arch_info(a):
return (a.cc, a.hint, copy.deepcopy(a.operands[:a.op_count]))
return (a.cc, a.hint, copy_ctypes_list(a.operands[:a.op_count]))

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .sysz_const import *
# define the API
@ -46,5 +47,5 @@ class CsSysz(ctypes.Structure):
)
def get_arch_info(a):
return (a.cc, copy.deepcopy(a.operands[:a.op_count]))
return (a.cc, copy_ctypes_list(a.operands[:a.op_count]))

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .x86_const import *
# define the API
@ -71,5 +72,5 @@ def get_arch_info(a):
return (a.prefix[:], a.opcode[:], a.rex, a.addr_size, \
a.modrm, a.sib, a.disp, a.sib_index, a.sib_scale, \
a.sib_base, a.sse_cc, a.avx_cc, a.avx_sae, a.avx_rm, \
copy.deepcopy(a.operands[:a.op_count]))
copy_ctypes_list(a.operands[:a.op_count]))

View File

@ -1,6 +1,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import ctypes, copy
import ctypes
from . import copy_ctypes_list
from .xcore_const import *
# define the API
@ -45,5 +46,5 @@ class CsXcore(ctypes.Structure):
)
def get_arch_info(a):
return (copy.deepcopy(a.operands[:a.op_count]))
return (copy_ctypes_list(a.operands[:a.op_count]))