Exemplo n.º 1
0
/*
#<pydoc>
def dbg_read_memory(ea, sz):
    """
    Reads from the debugee's memory at the specified ea
    @return:
        - The read buffer (as a string)
        - Or None on failure
    """
    pass
#</pydoc>
*/
static PyObject *dbg_read_memory(PyObject *py_ea, PyObject *py_sz)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();

  uint64 ea, sz;
  if ( !dbg_can_query() || !PyW_GetNumber(py_ea, &ea) || !PyW_GetNumber(py_sz, &sz) )
    Py_RETURN_NONE;

  // Create a Python string
  PyObject *ret = PyString_FromStringAndSize(NULL, Py_ssize_t(sz));
  if ( ret == NULL )
    Py_RETURN_NONE;

  // Get the internal buffer
  Py_ssize_t len;
  char *buf;
  PyString_AsStringAndSize(ret, &buf, &len);

  if ( (size_t)read_dbg_memory(ea_t(ea), buf, size_t(sz)) != sz )
  {
    // Release the string on failure
    Py_DECREF(ret);
    // Return None on failure
    Py_RETURN_NONE;
  }
  return ret;
}
Exemplo n.º 2
0
  // !=NULL means variable size datatype
  static asize_t idaapi s_calc_item_size(
    // This function is used to determine
    // size of the (possible) item at 'ea'
    void *ud,                       // user-defined data
    ea_t ea,                        // address of the item
    asize_t maxsize)               // maximal size of the item
  {
    PYW_GIL_GET;
    // Returns: 0-no such item can be created/displayed
    // this callback is required only for varsize datatypes
    py_custom_data_type_t *_this = (py_custom_data_type_t *)ud;
    newref_t py_result(
            PyObject_CallMethod(
                    _this->py_self,
                    (char *)S_CALC_ITEM_SIZE,
                    PY_FMT64 PY_FMT64,
                    pyul_t(ea),
                    pyul_t(maxsize)));

    if ( PyW_ShowCbErr(S_CALC_ITEM_SIZE) || py_result == NULL )
      return 0;

    uint64 num = 0;
    PyW_GetNumber(py_result.o, &num);
    return asize_t(num);
  }
Exemplo n.º 3
0
/*
header: typeinf.hpp
#<pydoc>
def apply_type_to_stkarg(op, v, type, name):
    """
    Apply type information to a stack variable

    @param op: reference to instruction operand
    @param v: immediate value in the operand (usually op.addr)
    @param type: type string. Retrieve from idc.ParseType("type string", flags)[1]
    @param name: stack variable name

    @return: Boolean
    """
    pass
#</pydoc>
*/
bool py_apply_type_to_stkarg(
    PyObject *py_op,
    PyObject *py_uv,
    PyObject *py_type,
    const char *name)
{
  uint64 v;
  PYW_GIL_CHECK_LOCKED_SCOPE();
  op_t *op = op_t_get_clink(py_op);
  if ( op == NULL || !PyW_GetNumber(py_uv, &v) || !PyString_Check(py_type))
  {
    return false;
  }
  else
  {
    const type_t *t = (type_t *) PyString_AsString(py_type);
    tinfo_t tif;
    tif.deserialize(idati, &t);
    borref_t br(py_op);
    bool rc;
    Py_BEGIN_ALLOW_THREADS;
    rc = apply_tinfo_to_stkarg(*op, uval_t(v), tif, name);
    Py_END_ALLOW_THREADS;
    return rc;
  }
}
Exemplo n.º 4
0
/*
header: frame.hpp
#<pydoc>
def add_stkvar3(op, v, flags):
    """
    Automatically add stack variable if doesn't exist
    Processor modules should use ua_stkvar2()
    @param op: reference to instruction operand
    @param v: immediate value in the operand (usually op.addr)
    @param flags: combination of STKVAR_... constants
    @return: Boolean
    """
    pass
#</pydoc>
*/
bool py_add_stkvar3(PyObject *py_op, PyObject *py_v, int flags)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();
  op_t *op = op_t_get_clink(py_op);
  uint64 v;
  return ( op == NULL || !PyW_GetNumber(py_v, &v) || !add_stkvar3(*op, sval_t(v), flags)) ? false : true;
}
Exemplo n.º 5
0
static void switch_info_ex_t_set_jumps(PyObject *self, PyObject *value)
{
  switch_info_ex_t *link = switch_info_ex_t_get_clink(self);
  if ( link == NULL )
    return;
  PYW_GIL_CHECK_LOCKED_SCOPE();
  uint64 v(0); PyW_GetNumber(value, &v);
  link->jumps = (pyul_t)v;
}
Exemplo n.º 6
0
static void op_t_set_specval(PyObject *self, PyObject *value)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();
  op_t *link = op_t_get_clink(self);
  if ( link == NULL )
    return;
  uint64 v(0);
  PyW_GetNumber(value, &v);
  link->specval = ea_t(v);
}
Exemplo n.º 7
0
static void insn_t_set_ea(PyObject *self, PyObject *value)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();
  insn_t *link = insn_t_get_clink(self);
  if ( link == NULL )
    return;
  uint64 v(0);
  PyW_GetNumber(value, &v);
  link->ea = ea_t(v);
}
Exemplo n.º 8
0
//---------------------------------------------------------------------------
static int pylist_to_intvec_cb(
  PyObject *py_item,
  Py_ssize_t /*index*/,
  void *ud)
{
  intvec_t &intvec = *(intvec_t *)ud;
  uint64 num;
  if (!PyW_GetNumber(py_item, &num))
    num = 0;

  intvec.push_back(int(num));
  return CIP_OK;
}
Exemplo n.º 9
0
/*
#<pydoc>
def dbg_write_memory(ea, buffer):
    """
    Writes a buffer to the debugee's memory
    @return: Boolean
    """
    pass
#</pydoc>
*/
static PyObject *dbg_write_memory(PyObject *py_ea, PyObject *py_buf)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();

  uint64 ea;
  if ( !dbg_can_query() || !PyString_Check(py_buf) || !PyW_GetNumber(py_ea, &ea) )
    Py_RETURN_NONE;

  size_t sz = PyString_GET_SIZE(py_buf);
  void *buf = (void *)PyString_AS_STRING(py_buf);
  if ( write_dbg_memory(ea_t(ea), buf, sz) != sz )
    Py_RETURN_FALSE;
  Py_RETURN_TRUE;
}
Exemplo n.º 10
0
/*
#<pydoc>
def out_name_expr(op, ea, off):
    """
    Output a name expression
    @param op: operand (of type op_t)
    @param ea: address of expression
    @param off: the value of name expression. this parameter is used only to
                check that the name expression will have the wanted value.
                You may pass BADADDR for this parameter.
    @return: true if the name expression has been produced
    """
    pass
#</pydoc>
*/
bool py_out_name_expr(
  PyObject *py_op,
  ea_t ea,
  PyObject *py_off)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();
  op_t *op = op_t_get_clink(py_op);
  uint64 v(0);
  adiff_t off;
  if ( PyW_GetNumber(py_off, &v) )
    off = adiff_t(v);
  else
    off = BADADDR;

  return op == NULL ? false : out_name_expr(*op, ea, off);
}
Exemplo n.º 11
0
//---------------------------------------------------------------------------
static int idaapi pylist_to_intvec_cb(
        const ref_t &py_item,
        Py_ssize_t /*index*/,
        void *ud)
{
  intvec_t &intvec = *(intvec_t *)ud;
  uint64 num;
  {
    PYW_GIL_CHECK_LOCKED_SCOPE();
    if (!PyW_GetNumber(py_item.o, &num))
      num = 0;
  }

  intvec.push_back(int(num));
  return CIP_OK;
}
Exemplo n.º 12
0
/*
#<pydoc>
def get_stkvar(op, v):
    """
    Get pointer to stack variable
    @param op: reference to instruction operand
    @param v: immediate value in the operand (usually op.addr)
    @return:
        - None on failure
        - tuple(member_t, actval)
          where actval: actual value used to fetch stack variable
    """
    pass
#</pydoc>
*/
PyObject *py_get_stkvar(PyObject *py_op, PyObject *py_v)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();
  op_t *op = op_t_get_clink(py_op);
  uint64 v;
  if ( op == NULL || !PyW_GetNumber(py_v, &v) )
    Py_RETURN_NONE;

  sval_t actval;
  member_t *member = get_stkvar(*op, sval_t(v), &actval);
  if ( member == NULL )
    Py_RETURN_NONE;

  return Py_BuildValue("(O" PY_SFMT64 ")",
    SWIG_NewPointerObj(SWIG_as_voidptr(member), SWIGTYPE_p_member_t, 0),
    pyl_t(actval));
}
Exemplo n.º 13
0
/*
#<pydoc>
# Conversion options for get_strlit_contents():
STRCONV_ESCAPE   = 0x00000001 # convert non-printable characters to C escapes (\n, \xNN, \uNNNN)

def get_strlit_contents(ea, len, type, flags = 0):
  """
  Get bytes contents at location, possibly converted.
  It works even if the string has not been created in the database yet.

  Note that this will <b>always</b> return a simple string of bytes
  (i.e., a 'str' instance), and not a string of unicode characters.

  If you want auto-conversion to unicode strings (that is: real strings),
  you should probably be using the idautils.Strings class.

  @param ea: linear address of the string
  @param len: length of the string in bytes (including terminating 0)
  @param type: type of the string. Represents both the character encoding,
               <u>and</u> the 'type' of string at the given location.
  @param flags: combination of STRCONV_..., to perform output conversion.
  @return: a bytes-filled str object.
  """
  pass
#</pydoc>
*/
static PyObject *py_get_strlit_contents(
        ea_t ea,
        PyObject *py_len,
        int32 type,
        int flags = 0)
{
  uint64 len;
  if ( !PyW_GetNumber(py_len, &len) )
    Py_RETURN_NONE;
  if ( len == BADADDR )
    len = uint64(-1);
  qstring buf;
  if ( len != uint64(-1) && ea_t(ea + len) < ea
    || get_strlit_contents(&buf, ea, len, type, NULL, flags) < 0 )
  {
    Py_RETURN_NONE;
  }
  if ( type == STRTYPE_C && buf.length() > 0 && buf.last() == '\0' )
    buf.remove_last();
  PYW_GIL_CHECK_LOCKED_SCOPE();
  newref_t py_buf(PyString_FromStringAndSize(buf.begin(), buf.length()));
  py_buf.incref();
  return py_buf.o;
}