package bap-std
IR language term.
Term is a building block of the Intermediate Representation of the binary program.
This module provides functions that are overloaded for different term classes. Term class is denoted with an explicit instance of type ('a,'b)cls
, where 'a
stands for the parent term and 'b
for the child term.
Example
Give a block
# let b = Blk.create ();;
val b : Blk.t = 00000003:
We can append a definition to it with an overloaded Term.append
# let b = Term.append def_t b d_1;;
val b : blk term = 00000003: 00000001: x := y + z
Update a value of a definition in the block:
# let b = Term.update def_t b d_2;;
val b : blk term = 00000003: 00000001: x := true
type 'a t = 'a term
term type
clone term
creates an object with a fresh new identifier that has the same contents as term
, i.e., that is syntactically the same. The clone operation is shallow, all subterms of term
are unchanged.
same x y
returns true if x
and y
represents the same entity, i.e., Tid.(tid x = tid y)
val name : 'a t -> string
name t
returns a string representation of a term t
identity
length t p
returns an amount of terms of t
class in a parent term p
find t p id
is Some c
if term p
has a subterm of type t
such that tid c = id
.
find_exn t p id
like find
but raises Not_found
if nothing is found.
update t p c
if term p
contains a term with id equal to tid c
then return p
with this term substituted with p
remove t p id
returns a term that doesn't contain element with the a given id
change t p id f
if p
contains subterm with of a given kind t
and identifier id
, then apply f
to this subterm. Otherwise, apply f
to None
. If f
return None
, then remove this subterm (a given it did exist), otherwise, update parent with a new subterm.
enum ?rev t p
enumerate all subterms of type t
of the a given term p
to_sequence ?rev t p
is a synonym for enum
.
map t p ~f
returns term p
with all subterms of type t
mapped with function f
filter_map t p ~f
returns term p
with all subterms of type t
filter_mapped with function f
, i.e., all terms for which function f
returned Some thing
are substituted by the thing
, otherwise they're removed from the parent term
concat_map t p ~f
substitute subterm c
of type t
in parent term p
with f c
. If f c
is an empty list, then c
doesn't occur in a new parent term, if f c
is a singleton list, then c
is substituted with the f c
, like in map
. If f c
is a list of n
elements, then in the place of c
this n
elements are inserted.
filter t p ~f
returns a term p
with subterms c
for which f c = false
removed.
first t p
returns the first subterm of type t
of a given parent p
last t p
returns a last subterm of type t
of a given parent p
next t p id
returns a term that is after a term with a given id
, if such exists.
prev t p id
returns a term that precedes a term with a given id
, if such exists.
after t ?rev p tid
returns all subterms in term p
that occur after a term with a given tid
. if rev
is true
or omitted then terms are returned in the evaluation order. Otherwise they're reversed. If there is no term with a given tid
, then an empty sequence is returned.
before t ?rev p tid
returns all term that occurs before definition with a given tid
in blk. If there is no such definition, then the sequence will be empty.
append t ~after:this p c
returns the p
term with c
appended after this
term. If after
is not specified, then append def
to the end of the parent term (if it makes sense, otherwise it is just added). If this
doesn't occur in the p
term then do nothing. The term tid is preserved.
prepend t ~before:this p c
returns the p
with c
inserted before this
term. If before
is left unspecified, then insert the c
at the beginning of the p
if it is a sequence, otherwise just insert. If this
is specified but doesn't occur in the p
then p
is returned as is. In all cases, the returned term has the same tid
as p
.
nth_exn t p n
same as nth
, but raises exception if n
is not a valid position number.
Attributes
Terms attribute set can be extended, using universal values. A value of type 'a tag
is used to denote an attribute of type 'a
with the name Value.Tag.name tag
.
With the provided interface Term can be treated as an extensible record.
set_attr term attr value
attaches an value
to attribute attr
in term
with_attrs term attributes
returns a term with a new set of attributes
get_attr term attr
returns a value of the a given attr
in term
Predefined attributes
val synthetic : unit tag
a term was introduced artificially by an analysis.
val live : unit tag
a term is identified as always non dead
val dead : unit tag
a term is identified as dead
val visited : unit tag
to mark a term as visited by some algorithm
Higher order mapping
class mapper : object ... end
Mapper performs deep identity term mapping. If you override any method make sure that you didn't forget to invoke parent's method, as OCaml will not call it for you.
class 'a visitor : object ... end
Visitor performs deep visiting. As always, don't forget to overrid parent methods. The visitor comes with useful enter_T
leave_T
that are no-ops in this visitor, so if you inherit directly from it, then you may not call to the parent method.
val switch :
('p, 't) cls ->
program:(program term -> 'a) ->
sub:(sub term -> 'a) ->
arg:(arg term -> 'a) ->
blk:(blk term -> 'a) ->
phi:(phi term -> 'a) ->
def:(def term -> 'a) ->
jmp:(jmp term -> 'a) ->
't term ->
'a
switch cls t ~program ~sub .. ~jmp
performs a pattern matching over a term t
based on its type class cls
. It is guaranteed that only one function will be called for a term.
val proj :
('p, 't) cls ->
?program:(program term -> 'a option) ->
?sub:(sub term -> 'a option) ->
?arg:(arg term -> 'a option) ->
?blk:(blk term -> 'a option) ->
?phi:(phi term -> 'a option) ->
?def:(def term -> 'a option) ->
?jmp:(jmp term -> 'a option) ->
't term ->
'a option
proj cls t ?case
a special case of pattern matching, where all cases by default returns None
val cata :
('p, 't) cls ->
init:'a ->
?program:(program term -> 'a) ->
?sub:(sub term -> 'a) ->
?arg:(arg term -> 'a) ->
?blk:(blk term -> 'a) ->
?phi:(phi term -> 'a) ->
?def:(def term -> 'a) ->
?jmp:(jmp term -> 'a) ->
't term ->
'a
cata cls ~init t ?case
performs a pattern matching. All methods by default returns init
. Note: cata
stands for catamorphism
val slot :
(Bap_core_theory.Theory.Program.Semantics.cls, blk term list)
Bap_knowledge.Knowledge.slot
the graphical representation of the program