CNode¶
The CNode API is one of the representation in Bip for
the AST nodes of an Hexrays decompiled function. Of the two representations,
it is the one which is prefered. The class CNode is an
abstract class which is used as parent for the actual classes representing the
nodes. For more informations about AST nodes and their different types see
Ast Nodes & Visitors.
For recuperating the CNode objects for a particular
function several methods of HxCFunc can be used:
- the
root_node()property for getting the root of the AST for a particular function; - the
get_cnode_filter()andget_cnode_filter_type()methods provide list of the nodes in the functions (those are based on the visitors); - the
visit_cnode()andvisit_cnode_filterlist()methods which allow to visit the AST using a Deep-First Search (DFS) algorithm with preorder-traversal.
For a list of possible node type see AST Node types.
Two internal mechanisms are important to understand
the CNode implementation:
- all
CNodeare generated from acitem_tobject in IDA, the correct classes is determine depending of the type of thecitem_t(HxCType). Thefrom_citem()static method exist for finding the correct child class ofCNodecorresponding to thecitem_tand creating it, as a general rule the constructors should not be called directly. - most of the
CNodesubclasses are created dynamically from their equivalentHxCItem, this is for avoiding code duplication. For more information see CNode generation and internals.
CNode API¶
-
class
bip.hexrays.CNode(citem, hxcfunc, parent)¶ Abstract class which allow to represent C expression and C statement decompiled from HexRays. This is an equivalent class to
HxCItembut is designed for visiting an AST generated by HexRays. The main advantage to use this class and its subclasses and not theHxCItemis the additional features which it provides, such as:- access to the parent node,
- access to the parent
HxCFunc, - visitor for nodes below the current one,
- several node specific methods, listed in Methods specific to CNode implementation.
The parrent class
AbstractCItemalso provides common functionnality with theHxCItemobjects. All subclasses of this class have the same behavior as the one fromHxCItemwith just some aditional feature.This is an abstract class and no object of this class should ever be created. The static method
from_citem()allow to create object of the correct subclass which inherit fromCNode.-
__init__(citem, hxcfunc, parent)¶ Constructor for the abstract class
CNode. This should never be used directly.Parameters: - citem – a
citem_tobject, in practice this should always be acexpr_tor acinsn_tobject. - hxcfunc – A
HxCFuncobject corresponding to the function which contains this node/item. - parent – An object which inherit from
CNodecorresponding to the parent expression or statement of this object. This may beNoneif this node is the root of the AST tree for its function.
- citem – a
-
_hxcfunc= None¶ The function associated with this node. This is a
HxCFuncobject. This is private and should not be modify. Modifying this attribute will not make any modification to the data stored in IDA.
-
_parent= None¶ The parent node of this node. This should be an object which inherit from
CNode. This may beNoneif this node is the root node of the AST tree for its function. This is private and should not be modify. Modifying this attribute will not make any modification to the data stored in IDA.
-
closest_ea¶ Property which return the closest address for this
CNode.By default this should be equivalent to the
ea()property except if it returnidc.BADADDR, in this case it will try and get the address of the parent. If the most parrent node of this node (which should be the root node of the function) still has no address,Noneis return.Returns: An integer corresponding to the closest address for this node. If no address where found this method will return None.
-
cstr¶ Property which return the C code corresponding to the decompilation of this
CNodeas a onliner. This will include the children of this object.Return str: The human readable C string corresponding to this CNodeas a onliner.
-
has_parent¶ Property which return true if the
CNodeas a parent. Only CNode which are root of a function should not have a parent.
-
parent¶ Property which return the parent of this
CNode. If this node does not have a parent aRuntimeErrorexception will be raised.Returns: A CNodeobject parent of this node.
-
hxcfunc¶ Property returning the
HxCFuncto which this node is associated.Returns: A HxCFuncobject corresponding to the function associated with this node.
-
comment¶ Get the comment at the closest address of this node.
Note that two node with the same closest_ea will have the same comment.
Todo
Default ITP will work only for expression, should make another one for statement.
Note
This use the default itp (at the semi-colon level), see
HxCFunc.get_cmt().
-
visit_cnode(callback)¶ Method which allow to visit this
CNodeelements and all those below it. This is implemented using a DFS algorithm. This does not use the hexrays visitor. For more information about the implementation seevisit_dfs_cnode()(this method is just a wrapper).Parameters: callback – A callable which will be called on this and all CNodebelow it. The call should take only one argument which correspond to theCNodecurrently visited. If this callback return False the visit is stoped, all other result is ignored.
-
visit_cnode_filterlist(callback, filter_list)¶ Method which allow to visit
CNodeelements which are of a type present in a list. Start with the currentCNodeand look in all its children. This is implemented using a DFS algorithm. This does not use the hexrays visitor. For more information about the implementation seevisit_dfs_cnode_filterlist()(this method is just a wrapper).Parameters: - callback – A callable which will be called on all
CNodein the function decompiled by hexrays. The call should take only one argument which correspond to theCNodecurrently visited. If this callback return False the visit is stoped, all other result is ignored. - filter_list – A list of class which inherit from
CNode. The callback will be called only for the node from a class in this list.
- callback – A callable which will be called on all
-
get_cnode_filter(cb_filter)¶ Method which return a list of
CNodefor which a filter return true. Internally this use thevisit_cnode()method which visit all nodes below (and including) the current one, this is just a wrapper.Parameters: cb_filter – A callable which take a CNodein parameter and return a boolean. This callback will be called on all children nodes and all nodes for which it returns true will be added in a list which will be returned by this function.Returns: A list of CNodewhich have match the filter. This list is order in which the node have been visited (seevisit_cnode()for more information).
-
get_cnode_filter_type(type_filter)¶ Method which return a list of
CNodeof a particular type(s). Internally this use thevisit_cnode_filterlist()method which visit all nodes below and including the current one, this is just a wrapper.Parameters: type_filter – The type(s) of CNodeto get. OnlyCNodematching the isinstance of this type will be returned. This can be a type, a class or a tuple (or list) of class and type.Returns: A list of CNodewhich have match the type. This list is order in which the node have been visited (seevisit_cnode()for more information).
-
ignore_cast¶ Property which return this node if it is not a cast or its child if it is a cast. This is designed for allowing to quickly ignore cast when going through some nodes.
Returns: The first CNodewhich is not a cast.
-
ignore_cast_parent¶ Property which return this node if it is not a cast or its parent if it is a cast. This is designed for allowing to quickly ignore cast when going backward through some nodes.
Returns: The first CNodewhich is not a cast.
-
_create_child(citem)¶ Internal method which allow to create a
CNodeobject from acitem_tchild of the current node. This must be used byCNodeStmtandCNodeExprfor creating their child expression and statement. This method is used for having compatibility with theHxCItemclass.Internally this function is a wrapper on
from_citem()which is call with the same function than this object and with this object as parent.Parameters: citem – A citem_tfrom ida.Returns: The equivalent node object to the citem_tfor bip. This will be an object which inherit fromCNode.
-
static
from_citem(citem, hxcfunc, parent)¶ Static method which allow to create an object of the correct child class of
CNodewhich is equivalent to acitem_tfrom ida. In particular it will be used for convertingcexpr_tandcinsn_tfrom ida toCNodeExprandCNodeStmtin bip. If noCNodechild object exist corresponding to thecitemprovided aValueErrorexception will be raised.This is the equivalent of
HxCItem.from_citem()but for theCNode.Note
CNodeExprandCNodeStmtshould not used this function for creating child item butCNode._create_child().Parameters: - citem – A
citem_tfrom ida. - hxcfunc – A
HxCFuncobject corresponding to the function which contains this node/item. - parent – An object which inherit from
CNodecorresponding to the parent expression or statement of this object. This may beNoneif this node is the root of the AST tree for its function.
Returns: The equivalent object to the
citem_tfor bip. This will be an object which inherit fromHxCItem.- citem – A
CNodeExpr API¶
-
class
bip.hexrays.CNodeExpr(cexpr, hxcfunc, parent)¶ Abstract class for representing a C Expression decompiled from HexRays. This is an abstract class which is used as a wrapper on top of the
cexpr_tobject. This is the equivalent of theHxCExprclass but which inherit from theCNodeand is made for visiting an AST.No object of this class should be instanstiated, for getting an expression the function
from_citem()should be used.Todo
something better could be done here for avoiding code duplication of the
HxCExprclass.-
__init__(cexpr, hxcfunc, parent)¶ Constructor for the
CNodeExprobject. Arguments are used by theCNodeconstructor.Parameters: - cexpr – A
cexpr_tobject from ida. - hxcfunc – A
HxCFuncobject corresponding to the function which contains this node/item. - parent – An object which inherit from
CNodecorresponding to the parent expression or statement of this object. This may beNoneif this node is the root of the AST tree for its function.
- cexpr – A
-
_cexpr= None¶ The
cexpr_tobject from ida.
-
__str__()¶ Surcharge for printing a CExpr
-
ops¶ Function which return the C Expressions child of this expression. This is used only when the expression is recursive.
Returns: A listof object inheriting fromCNodeExprand child of the current expression.
-
type¶ Property which return the type (
BipType) of this expression.Returns: An object which inherit from BipTypewhich correspond to the type of this object. Change to this type object will not change the type of this expression.
-
find_final_left_node()¶ Return the node which is the left most “final” expression (inherit from
CNodeExprFinal) below this node. If this node is a final expression it is returned.
-
find_left_node_notmatching(li)¶ Find the most left node not matching some classes. If the current node does not match any classes in the list provided it will be returned.
This function allow to bypass nodes to ignored. A common utilisation will be to bypass some unary operand for getting a final value or to bypass cast, reference or ptr derefence only. For getting the final node and ingore all other nodes see
find_final_left_node(). If a node found is a “final” node (inherit fromCNodeExprFinal) it will always be returned.For example for ignoring cast and ref use:
cn.find_left_node_notmatching([CNodeExprCast, CNodeExprRef]).Parameters: li – A listortupleof classes which inherit fromCNodeExprto ignore.Returns: A CNode object which is not of one of the class in li.
-
CNodeStmt API¶
-
class
bip.hexrays.CNodeStmt(cinsn, hxcfunc, parent)¶ Abstract class for representing a C Statement as returned by hexrays. This is an abstract class which is a wrapper on top of the
cinsn_tida object. This is the equivalent of theHxCStmtclass but which inherit from theCNodeand is made for visiting an AST.No object of this class should be instanstiated, for getting an expression the function
from_citem()should be used.A statement can contain one or more child statement and one or more child expression (
HxCExpr) object. By convention properties which will return child statement of an object will start with the prefixstmt_orst_.-
__init__(cinsn, hxcfunc, parent)¶ Constructor for a
CNodeStmtobject. Arguments are used by theCNodeconstructor.Parameters: - cinsn – A
cinsn_tfrom ida. - hxcfunc – A
HxCFuncobject corresponding to the function which contains this node/item. - parent – An object which inherit from
CNodecorresponding to the parent expression or statement of this object. This may beNoneif this node is the root of the AST tree for its function.
- cinsn – A
-
_cinsn= None¶ The
cinsn_tobject from ida.
-
__str__()¶ Surcharge for printing a CStmt.
-
stmt_children¶ Property which return a list of the statements which are children of this statement. This is used only when the statement is recursive, if not this will return an empty list.
Returns: A list of child statement of this object. Return type: Objects which inherit from CNodeStmt.
-
expr_children¶ Property which return a list of the expression (
CNodeExpr) which are children of this statement. This will not return children expression of the statement child of the current object.Returns: A list of child expression of this object. Return type: Objects which inherit from CNodeExpr.
-
Final Expression API¶
-
class
bip.hexrays.CNodeExprFinal(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFinalbut which inherit fromCNode. Automatically created bybuildCNode()-
__str__()¶ Surcharge for printing a HxCExprFinal.
-
value¶ Property which return the value of a final expression. This is abstract and if not overwritten it will raise a
RuntimeError.
-
-
class
bip.hexrays.CNodeExprEmpty(cexpr, hxcfunc, parent)¶ Copy of
HxCExprEmptybut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprNum(cexpr, hxcfunc, parent)¶ Copy of
HxCExprNumbut which inherit fromCNode. Automatically created bybuildCNode()-
size¶ Original size in bytes of the number.
Return int: the size in bytes.
-
value¶ Value of the number.
Return int: the value of this number.
-
-
class
bip.hexrays.CNodeExprFNum(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFNumbut which inherit fromCNode. Automatically created bybuildCNode()-
size¶ Original size in bytes of the number.
Return int: the size in bytes.
-
value¶ Todo
not sure if this works at all and probably not what we want anyway.
-
-
class
bip.hexrays.CNodeExprStr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprStrbut which inherit fromCNode. Automatically created bybuildCNode()-
value¶ String value.
Return str: the value of this string.
-
-
class
bip.hexrays.CNodeExprObj(cexpr, hxcfunc, parent)¶ Copy of
HxCExprObjbut which inherit fromCNode. Automatically created bybuildCNode()-
value¶ Address of the object.
Return int: the address of the object.
-
-
class
bip.hexrays.CNodeExprVar(cexpr, hxcfunc, parent)¶ Copy of
HxCExprVarbut which inherit fromCNode. Automatically created bybuildCNode()-
index¶ Index in the lvar array.
Todo
this should probably directly return the lvar ?
Return int: the index in the lvar array.
-
lvar¶ Property which allow direct access to the
HxLvarobject referenced by this node.This method exist only for the
CNodeimplementation.Returns: the HxLvarobject referenced by this node.
-
lvar_name¶ Property which allow to get the name of the
HxLvarreferenced by this node.This method exist only for the
CNodeimplementation.Returns: The name of the lvar as a string.
-
value¶ Index in the lvar array.
Return int: the index in the lvar array.
-
-
class
bip.hexrays.CNodeExprHelper(cexpr, hxcfunc, parent)¶ Copy of
HxCExprHelperbut which inherit fromCNode. Automatically created bybuildCNode()-
value¶ Value of this helper.
Return str: the string containing the value of the helper.
-
-
class
bip.hexrays.CNodeExprInsn(cexpr, hxcfunc, parent)¶ Copy of
HxCExprInsnbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprType(cexpr, hxcfunc, parent)¶ Copy of
HxCExprTypebut which inherit fromCNode. Automatically created bybuildCNode()
MemAccess Expression API¶
-
class
bip.hexrays.CNodeExprMemAccess(cexpr, hxcfunc, parent)¶ Copy of
HxCExprMemAccessbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprIdx(cexpr, hxcfunc, parent)¶ Copy of
HxCExprIdxbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprMemref(cexpr, hxcfunc, parent)¶ Copy of
HxCExprMemrefbut which inherit fromCNode. Automatically created bybuildCNode()-
mem¶ Property which return a
HxCExprrepresenting the object which is access by this expression.Returns: An operand of the expression, an object which inherit from HxCExpr.
-
off¶ Property which return the memory offset which is access by this expression. In practice this should be an offset in a structure or an enum.
Return int: The memory offset.
-
-
class
bip.hexrays.CNodeExprMemptr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprMemptrbut which inherit fromCNode. Automatically created bybuildCNode()-
access_size¶ Property which return the size which is access by the derefencement of this pointer.
Returns: An int corresponding to the size acceded.
-
off¶ Property which return the memory offset which is access by this expression. In practice this should be an offset in a structure or an enum.
Return int: The memory offset.
-
Unary Operation Expression API¶
-
class
bip.hexrays.CNodeExprUnaryOperation(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUnaryOperationbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprPtr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprPtrbut which inherit fromCNode. Automatically created bybuildCNode()-
access_size¶ Property which return the size which is access by the derefencement of this pointer.
Returns: An int corresponding to the size acceded.
-
-
class
bip.hexrays.CNodeExprFneg(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFnegbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprNeg(cexpr, hxcfunc, parent)¶ Copy of
HxCExprNegbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprCast(cexpr, hxcfunc, parent)¶ Copy of
HxCExprCastbut which inherit fromCNode. Automatically created bybuildCNode()-
ignore_cast¶ Property which return the first child expression of this node cast which is not a cast. This is implemented for all
CNodefor allowing to quickly ignore cast (seeignore_cast()). Multiple chained cast are handle.Returns: A CNodeExprwhich is not a cast.
-
-
class
bip.hexrays.CNodeExprLnot(cexpr, hxcfunc, parent)¶ Copy of
HxCExprLnotbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprBnot(cexpr, hxcfunc, parent)¶ Copy of
HxCExprBnotbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprRef(cexpr, hxcfunc, parent)¶ Copy of
HxCExprRefbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprPostinc(cexpr, hxcfunc, parent)¶ Copy of
HxCExprPostincbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprPostdec(cexpr, hxcfunc, parent)¶ Copy of
HxCExprPostdecbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprPreinc(cexpr, hxcfunc, parent)¶ Copy of
HxCExprPreincbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprPredec(cexpr, hxcfunc, parent)¶ Copy of
HxCExprPredecbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSizeof(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSizeofbut which inherit fromCNode. Automatically created bybuildCNode()
Double Operation Expression API¶
-
class
bip.hexrays.CNodeExprDoubleOperation(cexpr, hxcfunc, parent)¶ Copy of
HxCExprDoubleOperationbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprComma(cexpr, hxcfunc, parent)¶ Copy of
HxCExprCommabut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprLor(cexpr, hxcfunc, parent)¶ Copy of
HxCExprLorbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprLand(cexpr, hxcfunc, parent)¶ Copy of
HxCExprLandbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprBor(cexpr, hxcfunc, parent)¶ Copy of
HxCExprBorbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprXor(cexpr, hxcfunc, parent)¶ Copy of
HxCExprXorbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprBand(cexpr, hxcfunc, parent)¶ Copy of
HxCExprBandbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprEq(cexpr, hxcfunc, parent)¶ Copy of
HxCExprEqbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprNe(cexpr, hxcfunc, parent)¶ Copy of
HxCExprNebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSge(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSgebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUge(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUgebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSle(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSlebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUle(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUlebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSgt(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSgtbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUgt(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUgtbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSlt(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSltbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUlt(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUltbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSshr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSshrbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUshr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUshrbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprShl(cexpr, hxcfunc, parent)¶ Copy of
HxCExprShlbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAdd(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAddbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSub(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSubbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprMul(cexpr, hxcfunc, parent)¶ Copy of
HxCExprMulbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSdiv(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSdivbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUdiv(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUdivbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprSmod(cexpr, hxcfunc, parent)¶ Copy of
HxCExprSmodbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprUmod(cexpr, hxcfunc, parent)¶ Copy of
HxCExprUmodbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprFadd(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFaddbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprFsub(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFsubbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprFmul(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFmulbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprFdiv(cexpr, hxcfunc, parent)¶ Copy of
HxCExprFdivbut which inherit fromCNode. Automatically created bybuildCNode()
Assignment¶
-
class
bip.hexrays.CNodeExprAssignment(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAssignmentbut which inherit fromCNode. Automatically created bybuildCNode()-
dst¶ Helper property which return the destination of the assignment.
This is just a wrapper on top of
first_op().Returns: The first operand of the expression, an object which inherit from HxCExpr.
-
src¶ Helper property which return the source of the assignment.
This is just a wrapper on top of
second_op().Returns: The second operand of the expression, an object which inherit from HxCExpr.
-
-
class
bip.hexrays.CNodeExprAsg(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgbor(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgborbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgxor(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgxorbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgband(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgbandbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgadd(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgaddbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgsub(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgsubbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgmul(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgmulbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgsshr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgsshrbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgushr(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgushrbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgshl(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgshlbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgsdiv(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgsdivbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgudiv(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgudivbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgsmod(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgsmodbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeExprAsgumod(cexpr, hxcfunc, parent)¶ Copy of
HxCExprAsgumodbut which inherit fromCNode. Automatically created bybuildCNode()
Other leaf Expressions API¶
-
class
bip.hexrays.CNodeExprTernary(cexpr, hxcfunc, parent)¶ Copy of
HxCExprTernarybut which inherit fromCNode. Automatically created bybuildCNode()-
cond¶ Property which return the condition of the ternary expression.
Returns: An object which inherit from HxCExpr.
-
-
class
bip.hexrays.CNodeExprCall(cexpr, hxcfunc, parent)¶ Copy of
HxCExprCallbut which inherit fromCNode. Automatically created bybuildCNode()-
_carglist¶ Property which return the
carglist_tfrom IDA.
-
_get_carg(num)¶ Internal function which give access to the argument at position number
num. There is no check on the value of num.
-
args¶ Property which return the args of the call. Those args are also expressions.
Returns: A list of HxCExpr.
-
args_iter¶ Property which return an iterator on the args of the call. This is similar to
args()but with an iterator, and should have more perf.
-
caller¶ Property which return the caller of this expression. The caller is also an expression.
Returns: An object which inherit from HxCExpr.
-
caller_addr¶ Property which return the address called by this
CNodeExprCallif it exist.This property ignore the cast.
Returns: An integer corresponding to the address called or Noneif the caller is not aCNodeExprObjnode.
-
caller_func¶ Property which return the
BipFunctioncalled by thisCNodeExprCallif it exist.This property ignore the cast and works only for function in the binary. If this function is not able to create the
BipFunctionNonewill be returned (in particular this will not work for the imported function).Returns: The BipFunctioncalled by this node or None if it was not able to get it.
-
get_arg(num)¶ Function which return one of the argument of the call to a function. Each argument is also an expression.
Parameters: num (int) – The argument number. Raises: ValueError – if numis superior to the number of arguments in the call expression.Returns: An object which inherit from HxCExpr.
-
get_arg_intval(argnum)¶ Method for getting the integer value used for an argument at a given position. This allows to quickly get the value of an argument which is an address (
CNodeExprObj) or an integer (CNodeExprNum), this function will returnNonefor all other case.This property ignore the cast.
Parameters: argnum (int) – The argument number for which to get the value. Raises: ValueError – If argument at position argnumdoes not exist.Returns: The number or address passed in that argument or None if a number could not be found.
-
is_helper¶ Property which return true if this function is a decompiler helper function.
Returns: A bool indicating if this is a helper function (true) or a “real” call.
-
number_args¶ Property which return the number of arguments pass to the call expresion.
Return int: The number of argument to the call expression.
-
Final Statement API¶
-
class
bip.hexrays.CNodeStmtFinal(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtFinalbut which inherit fromCNode. Automatically created bybuildCNode()-
__str__()¶ Surcharge for printing a HxCExprFinal.
-
value¶ Property which return the value of a final expression. This is abstract and if not overwritten it will raise a
RuntimeError.
-
-
class
bip.hexrays.CNodeStmtEmpty(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtEmptybut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeStmtExpr(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtExprbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeStmtGoto(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtGotobut which inherit fromCNode. Automatically created bybuildCNode()-
cnode_dst¶ Property which return the
CNodewhich is the destination of thegoto. This is not a child node, but a link to on other part of the AST.
-
label¶ Property which return the label number of the goto statement.
Returns: An integer representing the label number.
-
-
class
bip.hexrays.CNodeStmtAsm(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtAsmbut which inherit fromCNode. Automatically created bybuildCNode()-
addr_instr¶ Property which return a list of address corresponding to the ASM instruction which are inline.
Returns: A list of address (integer) representing the address of the inline assembly instruction in the binary.
-
length¶ Property which return the number of instruction in this ASM statement.
-
-
class
bip.hexrays.CNodeStmtReturn(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtReturnbut which inherit fromCNode. Automatically created bybuildCNode()
Loop Statement API¶
-
class
bip.hexrays.CNodeStmtLoop(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtLoopbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeStmtFor(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtForbut which inherit fromCNode. Automatically created bybuildCNode()-
cond¶ Property which return the expression used as a condition for the for.
Returns: An object which inherits from HxCExpr.
-
init¶ Property which return the expression for the initialization of the for loop.
Returns: An object which inherits from HxCExpr.
-
-
class
bip.hexrays.CNodeStmtWhile(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtWhilebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeStmtDoWhile(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtDoWhilebut which inherit fromCNode. Automatically created bybuildCNode()
Other leaf Statement API¶
-
class
bip.hexrays.CNodeStmtIf(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtIfbut which inherit fromCNode. Automatically created bybuildCNode()-
cond¶ Property which return the expression used as a condition for the if.
Returns: An object which inherits from HxCExpr.
-
has_else¶ Property which indicate if this if statement as an else condtion.
Returns: True if this statement has an else condition, False otherwise.
-
st_else¶ Property which return the executed if the condition (
cond()) is False.This property will return
Noneif this condition has no else statement. This can be tested by checkinghas_else().Returns: An object which inherits from HxCStmt.
-
-
class
bip.hexrays.CNodeStmtSwitch(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtSwitchbut which inherit fromCNode. Automatically created bybuildCNode()-
cases_val¶ Property which return the list of values for each statement. This list is in the same order as the list of statement return by
st_cases().As each case can have several values for triggering it, this property return a list of list of values.
Returns: A list of lists of values (int), each list correspond to a different case and contain the values which will make the code enter this path. An empty list means it the default case.
-
expr¶ Property which return the expression used as the switch expression.
Returns: An object which inherits from HxCExpr.
-
max_val¶ Property which return the case maximum value for the switch.
Returns: An integer which is the maximum case value for the switch.
-
-
class
bip.hexrays.CNodeStmtContinue(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtContinuebut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeStmtBreak(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtBreakbut which inherit fromCNode. Automatically created bybuildCNode()
-
class
bip.hexrays.CNodeStmtBlock(cinsn, hxcfunc, parent)¶ Copy of
HxCStmtBlockbut which inherit fromCNode. Automatically created bybuildCNode()
Methods specific to CNode implementation¶
This list the methods which exist only in the implementation of
the CNode and not in the HxCItem.
Those methods are documented correctly in their respective class, they are
listed here only as an easy way to see functionnality which are
CNode specific. Access to parent
HxCFunc and parent node are not listed here.
Internal Hexrays Visitor API¶
-
bip.hexrays.cnode_visitor.visit_dfs_cnode(cnode, callback)¶ Basic visitor for a CNode: this will allow to call a callback on every node under the current one (and including the current node) in the AST. This method does not used the visitor from hexrays (
ctree_visitor_t). The callback will receive aCNodein argument.This visitor implements a Deep-First Search (DFS) algorithm which will always visit first the
CNodeExprand then theCNodeStmt. The callback is called before visiting deeper. For an object which inherit fromCNodeExprthe child nodes will be called in the same order than returned byops(); for an object which inherit fromCNodeStmtthe child nodes will be called in the same order than returned byexpr_children()(expression) follow by the nodes in the same order asstmt_children()(statements).If the callback return
Falsethe visitor will stop. Every other result is ignore.Note
This function will not visit a statement which is under a
CNodeExprInsn. Those should not be present in the last stage of a ctree and so this should not be a problem.An exception raised by the callback will interupt the visitor.
Parameters: - cnode – An object which inherit from
CNode. This object and all its child will be visited. - callback – A callable taking one argument which will be called
on all the
CNodevisited with theCNodeas argument. If it returns False the visitor stops.
- cnode – An object which inherit from
-
bip.hexrays.cnode_visitor.visit_dfs_cnode_filterlist(cnode, callback, filter_list)¶ Visitor for
CNodewith filtering. This function is the same thanvisit_dfs_cnode()but allow to use the callback only onCNodewhich are in a list (white listing of the node to visit).For information about the visitor implementation see
visit_dfs_cnode(). If the filter_list parameter contain only statement (CNodeStmt) the expression will not be visited at all, this should allow a little performance gain.If the callback return
Falsethe visitor will stop. Every other result is ignore.Parameters: - cnode – An object which inherit from
CNode. This object and all its child will be visited. - callback – A callable taking one argument which will be called
on all the
CNodevisited with theCNodeas argument. - filter_list – A list or tuple of class or a class which inherit
from
CNode. The callback will be called only for the node from a class in this list. If it returns False the visitor stops.
- cnode – An object which inherit from
CNode generation and internals¶
This description explains how the CNode subclasses are
created and is mainly destined to developers and maintainers of Bip.
For avoiding code duplication most classes which inherit
from CNode are created dynamically from their equivalent
HxCItem (the direct wrapper on the IDA implementation).
The classes for both representation will have mainly the same attributes and
the same method. When generating the CNode subclasses
the names for the classes are generated by replacing the HxC prefix by
the CNode prefix, for example HxCExprAdd will become
CNodeExprAdd.
Three classes are not generated dynamically. The CNode
class (equivalent to HxCItem) which contains the added
method and attributes which are specific to this implementation, this is the
main based abstract class for all of this implementation. Both
CNode and HxCItem inherit from
the AbstractCItem abstract class which contains most
of the code common to those two classes. The CNodeExpr
(equivalent to HxCExpr) and
CNodeStmt (equivalent to HxCStmt)
classes are not generated dynamically mainly because their constructors must
be changed.
All other subclasses of CNode are created dynamically
using the buildCNode() class decorator. This
decorator is used on all the HxCItem subclasses and
for each class it is used on it will create the equivalent
CNode subclasses. For doing that the following steps are
taken:
- recreating the architecture of inheritance for the new class,
- performing a copy of the attributes of the base class and modifying the
__doc__and__module__, - creating the name for the new class (replace
HxCbyCNodeprefix), - adding methods which are specific to the CNode version of the implementation if any,
- creating the actual new class,
- registering the new class in the corresponding module globals
(
bip.hexrays.cnodeaccessible for a user directly frombip.hexraysor frombip) for being able to access it later on, - registering the new class for being able to create the equivalent when rebuilding the inheritance.
For being able to rebuild the inheritance of the class it is necessary to
keep an equivalence between the HxCItem subclasses and
the CNode ones. This equivalence is stored in the
_citem2cnode global dictionary and updated each
time a new class is created by the bip.hexrays.cnode.buildCNode()
decorator (first and last step of the previous algorithm).
Note
The system of using bip.hexrays.cnode.buildCNode() as a
decorator may be changed in the future as a metaclass is probably more
appropriate for this.
For being able to add new methods to a CNode subclass,
another decorator exists: addCNodeMethod(). This
decorator take in argument a string representing the name of the CNode
subclasses the method should be added and an optional second argument
which allow to specify a different name for the class method than the one
the function decorated has. This decorator will simply add the
method in the _cnodeMethods global array which
will then be used by the buildCNode() decorator for
adding the methods and properties at the class creation. Because of the way
this work it is necessary for all specific methods to be created before
the actual class creation. Presently, because the number of specific methods
is quite small, those methods are directly at the end of the cnode.py
file.
Internal generation API¶
Those functions are not currently exported by Bip, and are documented for internal use only:
-
bip.hexrays.cnode.buildCNode(cls)¶ Class decorator for automatically building a class equivalent to the one pass in argument but which inherit from
CNodeinstead ofHxCItem.Internally this will:
- find the equivalent of the base classes by looking
in
_citem2cnode. - create a class identicall to the one in arguments but with name
change for being prefix by
CNodeinstead ofHxC. Attributes of the class are copied into the new class. - set the new class created as global to this module (the cnode one, not the one it was used in).
- find the equivalent of the base classes by looking
in
-
bip.hexrays.cnode.addCNodeMethod(cnode_name, func_name=None)¶ Decorator for a function, allow to add a method to a CNode class. This is for supporting to add method specific to a CNode which are not implemented (probably because it is not possible to do so) in their equivalent HxCItem class. This is design to be used in conjonction with the
buildCNode()decorator, methods which are added this way should be done before calling it. If the method already exist it will be overwrite by this implementation, this allow to redefine base methods from the HxCExpr. Internally this use the_cnodeMethodsglobal dictionary.It is possible to add properties using this method, if no
func_nameparameter is provided the name of the getter will be taken (and so the property must have a getter name). It is possible to usepropertyas a decorator but this will work only for getter:@addCNodeMethod(myclassname) @property #order of those decorator is important def my_new_property(self): """ Documentation will be correctly seen when generating the doc. """ pass # THE CODE
Todo
correctly handle all property decorators
Warning
All methods/properties decorated by this functions should be defined before the creation of the corresponding CNode! Currently the simplest way to do this is by adding it at the end of the
cnode.pyfiles.Parameters: - cnode_name (str) – The name of the CNode class to which add the property.
- func_name (str) – The name to use for adding to the CNode class, if None the name of the function will be used.
-
bip.hexrays.cnode._cnodeMethods¶ Dictionary which allows to add method to a particular CNode implementation. This is used by
addCNodeMethod()for adding a method in a CNode class which does not exist (is not possible to implement) in the HxCItem class equivalent. When the object is created bybuildCNodethe method will be added.This dictionary as the name of the class for key, and a parameter a list of tuples. Each tuple consist of the name of the method as first element follow by the function object.
-
bip.hexrays.cnode._citem2cnode¶ Dictionary which contain an equivalence between the class which inherit from
HxCItemand the one which inherit fromCNode. This is used for automatically constructing the classes which inherit fromCNodedynamically and should not be modified by hand. It is initialized with the 3 base classes which have a constructor.