Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C Code cleanup #1018

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 42 additions & 52 deletions miasm/jitter/JitCore.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux <[email protected]> */

#include <Python.h>
#include "structmember.h"
#include <stdint.h>
Expand All @@ -13,16 +15,15 @@

void JitCpu_dealloc(JitCpu* self)
{
Py_TYPE(self)->tp_free((PyObject*)self);
Py_TYPE(self)->tp_free((PyObject*)self);
}


PyObject * JitCpu_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
JitCpu *self;
JitCpu *self;

self = (JitCpu *)type->tp_alloc(type, 0);
return (PyObject *)self;
self = (JitCpu *)type->tp_alloc(type, 0);
return (PyObject *)self;
}

PyObject * JitCpu_get_vmmngr(JitCpu *self, void *closure)
Expand Down Expand Up @@ -69,7 +70,7 @@ uint16_t MEM_LOOKUP_16(JitCpu* jitcpu, uint64_t addr)

uint32_t MEM_LOOKUP_32(JitCpu* jitcpu, uint64_t addr)
{
return vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), addr);
return vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), addr);
}

uint64_t MEM_LOOKUP_64(JitCpu* jitcpu, uint64_t addr)
Expand All @@ -86,8 +87,7 @@ bn_t MEM_LOOKUP_BN_BN(JitCpu* jitcpu, int size, bn_t addr)

ptr = bignum_to_uint64(addr);


for (i=0; i < size; i += 8) {
for (i = 0; i < size; i += 8) {
tmp = vm_MEM_LOOKUP_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr);
ptr += 1;
val = bignum_or(val, bignum_lshift(bignum_from_int(tmp), i));
Expand All @@ -96,7 +96,6 @@ bn_t MEM_LOOKUP_BN_BN(JitCpu* jitcpu, int size, bn_t addr)
return val;
}


uint64_t MEM_LOOKUP_BN_INT(JitCpu* jitcpu, int size, bn_t addr)
{
uint64_t ptr;
Expand All @@ -105,36 +104,34 @@ uint64_t MEM_LOOKUP_BN_INT(JitCpu* jitcpu, int size, bn_t addr)
ptr = bignum_to_uint64(addr);

switch (size) {
case 8:
val = vm_MEM_LOOKUP_08(&(jitcpu->pyvm->vm_mngr), ptr);
break;
case 16:
val = vm_MEM_LOOKUP_16(&(jitcpu->pyvm->vm_mngr), ptr);
break;
case 32:
val = vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), ptr);
break;
case 64:
val = vm_MEM_LOOKUP_64(&(jitcpu->pyvm->vm_mngr), ptr);
break;
default:
fprintf(stderr, "Error: bad READ size %d\n", size);
exit(-1);
break;
case 8:
val = vm_MEM_LOOKUP_08(&(jitcpu->pyvm->vm_mngr), ptr);
break;
case 16:
val = vm_MEM_LOOKUP_16(&(jitcpu->pyvm->vm_mngr), ptr);
break;
case 32:
val = vm_MEM_LOOKUP_32(&(jitcpu->pyvm->vm_mngr), ptr);
break;
case 64:
val = vm_MEM_LOOKUP_64(&(jitcpu->pyvm->vm_mngr), ptr);
break;
default:
fprintf(stderr, "Error: bad READ size %d\n", size);
exit(-1);
break;
}

return val;
}



bn_t MEM_LOOKUP_INT_BN(JitCpu* jitcpu, int size, uint64_t addr)
{
int i;
uint8_t tmp;
bn_t val = bignum_from_int(0);

for (i=0; i < size; i += 8) {
for (i = 0; i < size; i += 8) {
tmp = vm_MEM_LOOKUP_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr);
addr += 1;
val = bignum_or(val, bignum_lshift(bignum_from_int(tmp), i));
Expand All @@ -143,7 +140,6 @@ bn_t MEM_LOOKUP_INT_BN(JitCpu* jitcpu, int size, uint64_t addr)
return val;
}


void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr)
{
bn_t ret;
Expand All @@ -157,45 +153,43 @@ void MEM_LOOKUP_INT_BN_TO_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr
memcpy(ptr, (char*)&ret, size / 8);
}


void MEM_WRITE_BN_BN(JitCpu* jitcpu, int size, bn_t addr, bn_t src)
{
uint64_t ptr;
int val;
int i;

ptr = bignum_to_uint64(addr);
for (i=0; i < size; i += 8) {
for (i = 0; i < size; i += 8) {
val = bignum_to_uint64(src) & 0xFF;
vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, val);
ptr += 1;
src = bignum_rshift(src, 8);
}
}


void MEM_WRITE_BN_INT(JitCpu* jitcpu, int size, bn_t addr, uint64_t src)
{
uint64_t ptr;
ptr = bignum_to_uint64(addr);

switch (size) {
case 8:
vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned char)src);
break;
case 16:
vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned short)src);
break;
case 32:
vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned int)src);
break;
case 64:
vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, src);
break;
default:
fprintf(stderr, "Error: bad write size %d\n", size);
exit(-1);
break;
case 8:
vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned char)src);
break;
case 16:
vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned short)src);
break;
case 32:
vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, (unsigned int)src);
break;
case 64:
vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, ptr, src);
break;
default:
fprintf(stderr, "Error: bad write size %d\n", size);
exit(-1);
break;
}
}

Expand All @@ -212,7 +206,6 @@ void MEM_WRITE_INT_BN(JitCpu* jitcpu, int size, uint64_t addr, bn_t src)
}
}


void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* ptr)
{
bn_t val;
Expand All @@ -227,8 +220,6 @@ void MEM_WRITE_INT_BN_FROM_PTR(JitCpu* jitcpu, int size, uint64_t addr, char* pt
MEM_WRITE_INT_BN(jitcpu, size, addr, val);
}



PyObject* vm_get_mem(JitCpu *self, PyObject* args)
{
PyObject *py_addr;
Expand All @@ -247,7 +238,6 @@ PyObject* vm_get_mem(JitCpu *self, PyObject* args)
PyGetInt_uint64_t(py_addr, addr);
PyGetInt_uint64_t(py_len, size);


if (size > SSIZE_MAX) {
fprintf(stderr, "Read size wider than supported system\n");
exit(EXIT_FAILURE);
Expand Down
9 changes: 4 additions & 5 deletions miasm/jitter/JitCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
PyObject* cst_32; \
PyObject* cst_ffffffff; \
uint64_t tmp; \
if (PyLong_Check(py_long)){ \
if (PyLong_Check(py_long)) { \
Py_INCREF(py_long); \
} else { \
RAISE(PyExc_TypeError,"arg must be int"); \
Expand Down Expand Up @@ -125,13 +125,12 @@
PyObject* cst_ffffffff; \
uint64_t tmp; \
\
if (PyInt_Check(py_long)){ \
if (PyInt_Check(py_long)) { \
tmp = (uint64_t)PyInt_AsLong(py_long); \
py_long = PyLong_FromLong((long)tmp); \
} else if (PyLong_Check(py_long)){ \
} else if (PyLong_Check(py_long)) { \
Py_INCREF(py_long); \
} \
else{ \
} else { \
RAISE(PyExc_TypeError,"arg must be int"); \
} \
\
Expand Down
39 changes: 19 additions & 20 deletions miasm/jitter/Jitgcc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* Copyright (C) 2011-2019 EADS France, Fabrice Desclaux <[email protected]> */

#include <Python.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
#include "compat_py23.h"

typedef struct {
Expand All @@ -23,7 +26,7 @@ PyObject* gcc_exec_block(PyObject* self, PyObject* args)
block_id BlockDst;
uint64_t max_exec_per_call = 0;
uint64_t cpt;
int do_cpt;
bool do_cpt;


if (!PyArg_ParseTuple(args, "OOOO|K",
Expand All @@ -35,61 +38,57 @@ PyObject* gcc_exec_block(PyObject* self, PyObject* args)
Py_INCREF(retaddr);

if (max_exec_per_call == 0) {
do_cpt = 0;
do_cpt = false;
cpt = 1;
} else {
do_cpt = 1;
do_cpt = true;
cpt = max_exec_per_call;
}



for (;;) {
if (cpt == 0)
return retaddr;
if (do_cpt)
while (cpt) {
if (do_cpt) {
cpt --;
}
// Init
BlockDst.is_local = 0;
BlockDst.address = 0;

// Get the expected jitted function address
func_py = PyDict_GetItem(lbl2ptr, retaddr);
if (func_py)
if (func_py) {
func = (jitted_func) PyLong_AsVoidPtr((PyObject*) func_py);
else {
} else {
if (BlockDst.is_local == 1) {
fprintf(stderr, "return on local label!\n");
exit(EXIT_FAILURE);
}
// retaddr is not jitted yet
return retaddr;
break;
}
// Execute it
status = func(&BlockDst, jitcpu);
Py_DECREF(retaddr);
retaddr = PyLong_FromUnsignedLongLong(BlockDst.address);

// Check exception
if (status)
return retaddr;
if (status) {
break;
}

// Check stop offsets
if (PySet_Contains(stop_offsets, retaddr))
return retaddr;
if (PySet_Contains(stop_offsets, retaddr)) {
break;
}
}
return retaddr;
}



static PyMethodDef GccMethods[] = {
{"gcc_exec_block", gcc_exec_block, METH_VARARGS,
"gcc exec block"},
{NULL, NULL, 0, NULL} /* Sentinel */
};



MOD_INIT(Jitgcc)
{
PyObject *module = NULL;
Expand Down
Loading