package bap-std
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=63ada71fa4f602bd679174dc6bf780d54aeded40ad4ec20d256df15886e3d2d5
md5=b8b1aff8c6846f2213eafc54de07b304
doc/bap/Bap/Std/Addr/index.html
Module Std.Addr
Shortcut for bitvectors that represent addresses
include module type of Bitvector
with type t = addr
and type endian = endian
and type comparator_witness = Bitvector.comparator_witness
type t = addr
word
is an abbreviation to Bitvector.t
Common Interfaces
A bitvector is a value, first of all, so it supports a common set of a value interface: it can be stored, compared, it can be a key in a dictionary, etc. Moreover, being a number it can be compared with zero and applied to a common set of integer operations.
include Regular.Std.Regular.S
with type t := t
with type comparator_witness = Bitvector.comparator_witness
include Core_kernel.Bin_prot.Binable.S with type t := t
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (int -> t) Bin_prot.Read.reader
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_reader_t : t Bin_prot.Type_class.reader
val bin_t : t Bin_prot.Type_class.t
include Regular.Std.Printable.S with type t := t
val to_string : t -> string
to_string x
returns a human-readable representation of x
val str : unit -> t -> string
str () t
is formatted output function that matches "%a" conversion format specifier in functions, that prints to string, e.g., sprintf
, failwithf
, errorf
and, surprisingly all Lwt
printing function, including Lwt_io.printf
and logging (or any other function with type ('a,unit,string,...) formatN`. Example:
Or_error.errorf "type %a is not valid for %a"
Type.str ty Exp.str exp
val pps : unit -> t -> string
synonym for str
val ppo : Core_kernel.Out_channel.t -> t -> unit
will print to a standard output_channel
, useful for using in printf
, fprintf
, etc.
val pp_seq : Format.formatter -> t Core_kernel.Sequence.t -> unit
prints a sequence of values of type t
this will include pp
function from Core
that has type t printer
, and can be used in Format.printf
family of functions
include Core_kernel.Pretty_printer.S with type t := t
include Core_kernel.Comparable.S_binable
with type t := t
with type comparator_witness = Bitvector.comparator_witness
include Base.Comparable.S
with type t := t
with type comparator_witness = Bitvector.comparator_witness
compare t1 t2
returns 0 if t1
is equal to t2
, a negative integer if t1
is less than t2
, and a positive integer if t1
is greater than t2
.
ascending
is identical to compare
. descending x y = ascending y x
. These are intended to be mnemonic when used like List.sort ~compare:ascending
and List.sort ~cmp:descending
, since they cause the list to be sorted in ascending or descending order, respectively.
clamp_exn t ~min ~max
returns t'
, the closest value to t
such that between t' ~low:min ~high:max
is true.
Raises if not (min <= max)
.
type comparator_witness = Bitvector.comparator_witness
module Replace_polymorphic_compare :
Base.Comparable.Polymorphic_compare with type t := t
val comparator : (t, comparator_witness) Base__.Comparator.comparator
module Map :
Core_kernel.Map.S_binable
with type Key.t = t
with type Key.comparator_witness = comparator_witness
module Set :
Core_kernel.Set.S_binable
with type Elt.t = t
with type Elt.comparator_witness = comparator_witness
include Core_kernel.Hashable.S_binable with type t := t
val hash_fold_t :
Ppx_hash_lib.Std.Hash.state ->
t ->
Ppx_hash_lib.Std.Hash.state
val hash : t -> Ppx_hash_lib.Std.Hash.hash_value
val hashable : t Core_kernel.Hashtbl.Hashable.t
module Table : Core_kernel.Hashtbl.S_binable with type key = t
module Hash_set : Core_kernel.Hash_set.S_binable with type elt = t
module Hash_queue : Core_kernel.Hash_queue.S with type key = t
include Regular.Std.Data.S with type t := t
name,Ver v,desc
information attached to a particular reader or writer.
Data representation version. After any change in data representation the version should be increased.
Serializers that are derived from a data representation must have the same version as a version of the data structure, from which it is derived. This kind of serializers can only read and write data of the same version.
Other serializers can actually read and write data independent on its representation version. A serializer, that can't store data of current version simply shouldn't be added to a set of serializers.
It is assumed, that if a reader and a writer has the same name and version, then whatever was written by the writer should be readable by the reader. The round-trip equality is not required, thus it is acceptable if some information is lost.
It is also possible, that a reader and a writer that has the same name are compatible. In that case it is recommended to use semantic versioning.
val size_in_bytes : ?ver:string -> ?fmt:string -> t -> int
size_in_bytes ?ver ?fmt datum
returns the amount of bytes that is needed to represent datum
in the given format and version
val of_bytes : ?ver:string -> ?fmt:string -> Regular.Std.bytes -> t
of_bytes ?ver ?fmt bytes
deserializes a value from bytes.
val to_bytes : ?ver:string -> ?fmt:string -> t -> Regular.Std.bytes
to_bytes ?ver ?fmt datum
serializes a datum
to a sequence of bytes.
val blit_to_bytes :
?ver:string ->
?fmt:string ->
Regular.Std.bytes ->
t ->
int ->
unit
blit_to_bytes ?ver ?fmt buffer datum offset
copies a serialized representation of datum into a buffer
, starting from the offset
.
val of_bigstring : ?ver:string -> ?fmt:string -> Core_kernel.bigstring -> t
of_bigstring ?ver ?fmt buf
deserializes a datum from bigstring
val to_bigstring : ?ver:string -> ?fmt:string -> t -> Core_kernel.bigstring
of_bigstring ?ver ?fmt datum
serializes a datum to a sequence of bytes represented as bigstring
val blit_to_bigstring :
?ver:string ->
?fmt:string ->
Core_kernel.bigstring ->
t ->
int ->
unit
blit_to_bigstring ?ver ?fmt buffer datum offset
copies a serialized representation of datum into a buffer
, starting from offset
.
module Io : sig ... end
Input/Output functions for the given datum.
module Cache : sig ... end
Data cache.
val add_reader :
?desc:string ->
ver:string ->
string ->
t Regular.Std.reader ->
unit
add_reader ?desc ~ver name reader
registers a new reader
with a provided name
, version ver
and optional description desc
val add_writer :
?desc:string ->
ver:string ->
string ->
t Regular.Std.writer ->
unit
add_writer ?desc ~ver name writer
registers a new writer
with a provided name
, version ver
and optional description desc
val available_readers : unit -> info list
available_reader ()
lists available readers for the data type
val default_reader : unit -> info
default_reader
returns information about default reader
set_default_reader ?ver name
sets new default reader. If version is not specified then the latest available version is used. Raises an exception if a reader with a given name doesn't exist.
with_reader ?ver name operation
temporary sets a default reader to a reader with a specified name and version. The default reader is restored after operation
is finished.
val available_writers : unit -> info list
available_writer ()
lists available writers for the data type
val default_writer : unit -> info
default_writer
returns information about the default writer
set_default_writer ?ver name
sets new default writer. If version is not specified then the latest available version is used. Raises an exception if a writer with a given name doesn't exist.
with_writer ?ver name operation
temporary sets a default writer to a writer with a specified name and version. The default writer is restored after operation
is finished.
val default_printer : unit -> info option
default_writer
optionally returns an information about default printer
set_default_printer ?ver name
sets new default printer. If version is not specified then the latest available version is used. Raises an exception if a printer with a given name doesn't exist.
with_printer ?ver name operation
temporary sets a default printer to a printer with a specified name and version. The default printer is restored after operation
is finished.
Low level access to serializers
val find_reader : ?ver:string -> string -> t Regular.Std.reader option
find_reader ?ver name
lookups a reader with a given name. If version is not specified, then a reader with maximum version is returned.
val find_writer : ?ver:string -> string -> t Regular.Std.writer option
find_writer ?ver name
lookups a writer with a given name. If version is not specified, then a writer with maximum version is returned.
Bitvector implements a common set of operations that are expected from integral values.
module Mono : Core_kernel.Comparable with type t := t
A comparable interface with size-monomorphic comparison.
type endian = endian =
Specifies the order of bytes in a word.
val bin_size_endian : endian Core_kernel.Bin_prot.Size.sizer
val bin_write_endian : endian Core_kernel.Bin_prot.Write.writer
val bin_writer_endian : endian Core_kernel.Bin_prot.Type_class.writer
val bin_read_endian : endian Core_kernel.Bin_prot.Read.reader
val __bin_read_endian__ : (int -> endian) Core_kernel.Bin_prot.Read.reader
val bin_reader_endian : endian Core_kernel.Bin_prot.Type_class.reader
val bin_endian : endian Core_kernel.Bin_prot.Type_class.t
val sexp_of_endian : endian -> Ppx_sexp_conv_lib.Sexp.t
val endian_of_sexp : Ppx_sexp_conv_lib.Sexp.t -> endian
Constructors
val code_addr : Bap_core_theory.Theory.Target.t -> Bitvec.t -> t
code_addr t x
uses target's address size to create a word.
Same as create x (Theory.Target.code_addr_size t)
.
val data_addr : Bap_core_theory.Theory.Target.t -> Bitvec.t -> t
data_addr t x
uses target's code address size to create a word.
Same as create x (Theory.Target.data_addr_size t)
.
val data_word : Bap_core_theory.Theory.Target.t -> Bitvec.t -> t
data_word t x
uses target's word size to create a word.
Same as create x (Theory.Target.bits t)
.
val of_string : string -> t
of_string s
parses a bitvector from a string representation defined in section Clarification on string representation.
val of_bool : bool -> t
of_bool x
is a bitvector with length 1
and value b0
if x
is false and b1
otherwise.
val of_int : width:int -> int -> t
of_int ~width n
creates a bitvector of the specified bit-width
with the value equal to n
. If bits of the n
that doesn't fit into width
are ignored.
val of_int32 : ?width:int -> int32 -> t
of_int32 ?width n
creates a bitvector of the specified bit-width
with the value equal to n
. If bits of the n
that doesn't fit into width
are ignored. Parameter width
defaults to 32
.
val of_int64 : ?width:int -> int64 -> t
of_int32 ?width n
creates a bitvector of the specified bit-width
with the value equal to n
. If bits of the n
that doesn't fit into width
are ignored. Parameter width
defaults to 32
.
Some predefined constant constructors
val b0 : t
b0 = of_bool false
val b1 : t
b1 = of_bool true
Helpful shortcuts
val one : int -> t
one width
number one with a specified width
, is a shortcut for of_int 1 ~width
val zero : int -> t
zero width
zero with a specified width
, is a shortcut for of_int 0 ~width
val ones : int -> t
ones width
is a number with a specified width
, and all bits set to 1. It is a shortcut for lnot (zero width)
of_binary ?width endian num
creates a bitvector from a string interpreted as a sequence of bytes in a specified order.
The result is always positive and unsigned. The num
argument is not shared. width
defaults to the length of num
in bits, i.e. 8 * String.length num
.
Conversions to OCaml built in integer types
val to_int : t -> int Core_kernel.Or_error.t
to_int x
projects x
in to OCaml int
.
val to_int32 : t -> int32 Core_kernel.Or_error.t
to_int32 x
projects x
in to int32
val to_int64 : t -> int64 Core_kernel.Or_error.t
to_int64 x
projects x
in to int64
val to_int_exn : t -> int
to_int_exn x
projects x
in to OCaml int
.
val to_int32_exn : t -> int32
to_int32_exn x
projects x
in to int32
val to_int64_exn : t -> int64
to_int64_exn x
projects x
in to int64
val pp : Format.formatter -> t -> unit
printf "%a" pp x
prints x
into a formatter. This is a default printer, controlled by set_default_printer
. Multiple formats are available, see the available_writers
for the actual list of formats and a format description. Out of box it defaults to pp_hex_full
. Note, the printf
function from examples refers to the Format.printf
, thus it is assumed that the Format
module is open in the scope.
val pp_hex : Format.formatter -> t -> unit
printf "%a" pp_hex x
prints x
in the hexadecimal format omitting suffixes, and the prefix if it is not necessary. Example,
# printf "%a\n" pp_hex (Word.of_int32 0xDEADBEEFl);;
0xDEADBEEF
# printf "%a\n" pp_hex (Word.of_int32 0x1);;
1
val pp_dec : Format.formatter -> t -> unit
printf "%a" pp_dec x
prints x
in the decimal format omitting suffixes and prefixes. Example,
# printf "%a\n" pp_dec (Word.of_int32 0xDEADBEEFl);;
3735928559
# printf "%a\n" pp_dec (Word.of_int32 0x1);;
1
val pp_oct : Format.formatter -> t -> unit
printf "%a" pp_oct x
prints x
in the octal format omitting suffixes, and the prefix if it is not necessary. Example,
# printf "%a\n" pp_oct (Word.of_int32 0xDEADBEEFl);;
0o33653337357
# printf "%a\n" pp_oct (Word.of_int32 0x1);;
1
val pp_bin : Format.formatter -> t -> unit
printf "%a" pp_bin x
prints x
in the binary (0 and 1) format omitting suffixes, and the prefix if it is not necessary. Example,
# printf "%a\n" pp_bin (Word.of_int32 0xDEADBEEFl);;
0b11011110101011011011111011101111
# printf "%a\n" pp_bin (Word.of_int32 0x1);;
1
val pp_hex_full : Format.formatter -> t -> unit
printf "%a" pp_hex_full x
prints x
in the hexadecimal format with suffixes, and the prefix if it is necessary. Example,
# printf "%a\n" pp_hex_full (Word.of_int32 0xDEADBEEFl);;
0xDEADBEEF:32u
# printf "%a\n" pp_hex_full (Word.of_int32 0x1);;
1:32u
val pp_dec_full : Format.formatter -> t -> unit
printf "%a" pp_dec_full x
prints x
in the decimal format with suffixes and prefixes. Example,
# printf "%a\n" pp_dec_full (Word.of_int32 0xDEADBEEFl);;
3735928559:32u
# printf "%a\n" pp_dec_full (Word.of_int32 0x1);;
1:32u
val pp_oct_full : Format.formatter -> t -> unit
printf "%a" pp_oct_full x
prints x
in the octal format with suffixes, and the prefix if it is necessary. Example,
# printf "%a\n" pp_oct_full (Word.of_int32 0xDEADBEEFl);;
0o33653337357:32u
# printf "%a\n" pp_oct_full (Word.of_int32 0x1);;
1:32u
val pp_bin_full : Format.formatter -> t -> unit
printf "%a" pp_bin_full x
prints x
in the binary (0 and 1) format omitting suffixes, and the prefix if it is necessary. Example,
# printf "%a\n" pp_bin_full (Word.of_int32 0xDEADBEEFl);; 0b11011110101011011011111011101111:32u # printf "%a\n" pp_bin_full (Word.of_int32 0x1);; 1:32u
val pp_generic :
?case:[ `upper | `lower ] ->
?prefix:[ `auto | `base | `none | `this of string ] ->
?suffix:[ `none | `full | `size ] ->
?format:[ `hex | `dec | `oct | `bin ] ->
Format.formatter ->
t ->
unit
pp_generic ?case ?prefix ?suffix ?format ppf x
- a printer to suit all tastes.
Note: this is a generic printer factory that should be used if none of the nine preinstantiated suits you.
val string_of_value : ?hex:bool -> t -> string
string_of_value ?hex x
returns a textual representation of the x
value, i.e., ignores size and signedness. If hex
is true
(default), then it is in the hexadecimal representation, otherwise the decimal representation is used. The returned value is not prefixed. No leading zeros are printed. If a value is signed and negative, then a leading negative sign is printed. Hexadecimal letter literals are printed in a lowercase format.
signed t
casts t to a signed type, so that any operations applied on t
will be signed.
unsigned t
casts t
to an unsigned type, so that any operations applied to it will interpret t
as an unsigned word.
val is_zero : t -> bool
is_zero bv
is true iff all bits are set to zero.
val is_one : t -> bool
is_ones bv
is true if the least significant bit is equal to one
val bitwidth : t -> int
bitwidth bv
return a bit-width, i.e., the amount of bits
val extract : ?hi:int -> ?lo:int -> t -> t Core_kernel.Or_error.t
extract bv ~hi ~lo
extracts a subvector from bv
, starting from bit hi
and ending with lo
. Bits are enumerated from right to left (from least significant to most), starting from zero. hi
maybe greater than size
.
hi
defaults to width bv - 1
lo
defaults to 0
.
Example:
extract (of_int 17 ~width:8) ~hi:4 ~lo:3
will result in a two bit vector consisting of the forth and third bits, i.e., equal to a number 2
.
lo
and hi
should be non-negative numbers. lo
must be less then a width bv
and hi >= lo
.
extract_exn bv ~hi ~lo
is the same as extract
, but will raise an exception on error.
succ n
returns next value after n
. It is not guaranteed that signed (succ n) > signed n
val gcd : t -> t -> t Core_kernel.Or_error.t
gcd x y
is the greatest common divisor of x
and y
in the integers. Note that this is not always the greatest common divisor in the bitvectors of fixed length. For example, in the 32-bit unsigned integers, 2 = 2 + 2^32 = 2(1 + 2^31)
. Thus, 1 + 2^31
is a divisor of 2
, even though gcd 2 2 = 2
. Two properties that still hold are: 1. Both x
and y
are multiples of gcd x y
, and 2. gcd x y <= min (abs x) (abs y)
val lcm : t -> t -> t Core_kernel.Or_error.t
lcm x y
is the least common multiple of x
and y
in the integers. Note that, like gcd x y
, this is not always the least common multiple of x
and y
in the fixed- length bitvectors. See the gcd
documentation for an example. The result of this function will always be some common multiple of the inputs, even in the fixed-width bitvectors.
gcdext x y
returns (g, s, t)
where g = gcd x y
and g = s*x + t*y
. See the documentation for gcd x y
for why this function is tricky to use.
gcdext_exn x y
is the same as gcdext
, but will raise an exception on error.
Iteration over bitvector components
enum_bytes x order
returns a sequence of bytes of x
in a specified order
. Each byte is represented as a bitvector
itself.
enum_bytes x order
returns bytes of x
in a specified order
, with bytes represented by char
type
enum_bits x order
returns bits of x
in a specified order
. order
defines only the ordering of words in a bitvector, bits will always be in MSB first order. The length of the sequence is always a power of 8
.
Comparison with zero
Note, we're not including With_zero
interface, since it refers to the `Sign` module, that is available only in core_kernel >= 113.33.00.
val validate_positive : t Core_kernel.Validate.check
validate_positive
validates that a value is positive.
val validate_non_negative : t Core_kernel.Validate.check
validate_non_negative
validates that a value is non negative.
val validate_negative : t Core_kernel.Validate.check
validate_negative
validates that a value is negative.
val validate_non_positive : t Core_kernel.Validate.check
validate_non_positive
validates that a value is not positive.
val is_positive : t -> bool
is_positive x
is true if x
is greater than zero. Always true if x
is unsigned.
val is_non_negative : t -> bool
is_non_negative x
is true if x
is greater than or equal to zero. Tautology if x
is unsigned.
val is_negative : t -> bool
is_negative x
is true if x
is strictly less than zero. It is a contradiction if x
is not signed.
val is_non_positive : t -> bool
is_non_positive x
is true if x
is less than zero. It is a contradiction if x
is not signed.
module Int_err : sig ... end
module Stable : sig ... end
Stable marshaling interface.
module Trie : sig ... end
Prefix trees for bitvectors.
memref ?disp ?index ?scale base
mimics a memory reference syntax in gas assembler, dis(base,index,scale)
assembler operation. It returns address at base + index * scale + dis
.
All operations are taken modulo $2^n$
, where n = bitwidth base
.