Skip to content

Commit b1f5784

Browse files
authored
extends Core Theory with target registration and lookup (#1519)
1 parent 28e4d88 commit b1f5784

File tree

5 files changed

+495
-28
lines changed

5 files changed

+495
-28
lines changed

lib/bap_core_theory/bap_core_theory.mli

Lines changed: 194 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,37 @@ module Theory : sig
14371437
?package:string -> (** defaults to ["user"] *)
14381438
string -> t
14391439

1440+
1441+
1442+
(** [register <variants> t] generates and registers a list of targets.
1443+
1444+
For a list of possible target properties generate a set of
1445+
unique targets and declare them. To generate a unique name the
1446+
following scheme is used,
1447+
1448+
{v<name>-<system>-<abi><fabi>-<format>+<option>...v}
1449+
1450+
where if a property is [:unknown] then it is not listed in
1451+
the name (including the separator, if necessary).
1452+
1453+
E.g.,
1454+
{v
1455+
arm-linux-gnueabihf-elf
1456+
armv7-linux-gnueabihf+m3
1457+
v}
1458+
1459+
@since 2.5.0
1460+
*)
1461+
1462+
val register :
1463+
?systems:system list ->
1464+
?abis:abi list ->
1465+
?fabis:fabi list ->
1466+
?filetypes:filetype list ->
1467+
?options:options list ->
1468+
?package:string ->
1469+
t -> unit
1470+
14401471
(** [lookup ?package name] lookups a target with the given [name].
14411472
14421473
If [name] is unqualified then it is qualified with the
@@ -1447,6 +1478,77 @@ module Theory : sig
14471478
*)
14481479
val lookup : ?package:string -> string -> t option
14491480

1481+
1482+
(** [select <reqs> ()] selects a target that matches requirements.
1483+
1484+
Selects the least specific target that belongs to [parent] and
1485+
matches the specified requirements. If [unique] is [true] and
1486+
there is no single match then raises an exception, otherwise
1487+
returns the first in the family order match (i.e., the least
1488+
specific of the matches).
1489+
1490+
If there are no matching targets returns [parent] if [strict]
1491+
is [false], otherwise fails with an exception.
1492+
1493+
The matching procedure uses the [domain] structure of the
1494+
corresponding parameters. A target matches the constraint if
1495+
all properties of the target matches the corresponding
1496+
parameters. A property matches a parameter if the property is
1497+
greater or equal (in the domain order) than the parameter.
1498+
1499+
See also {!filter}.
1500+
1501+
@param parent defaults to [unknown]
1502+
@param unique defaults to [false]
1503+
@param strict defaults to [false]
1504+
@param system defaults to [System.unknown]
1505+
@param abi defaults to [Abi.unknown]
1506+
@param fabi defaults to [Fabi.unknown]
1507+
@param options defaults to [Options.empty]
1508+
1509+
@since 2.5.0 *)
1510+
val select :
1511+
?unique:bool ->
1512+
?strict:bool ->
1513+
?parent:t ->
1514+
?system:system ->
1515+
?abi:abi ->
1516+
?fabi:fabi ->
1517+
?filetype:filetype ->
1518+
?options:options -> unit -> t
1519+
1520+
1521+
(** [filter <reqs> ()] selects targets that matches requirements.
1522+
1523+
Filters targets that belong to [parent] and match the
1524+
specified requirements. The targets are returned in the family
1525+
order, i.e., the least specific target is comming first. This
1526+
is the same order in which [family] list targets.
1527+
1528+
The matching procedure uses the [domain] structure of the
1529+
corresponding parameters. A target matches the constraint if
1530+
all properties of the target matches the corresponding
1531+
parameters. A property matches a parameter if the property is
1532+
greater or equal (in the domain order) than the parameter.
1533+
1534+
See also {!select}.
1535+
1536+
@param parent defaults to [unknown]
1537+
@param system defaults to [System.unknown]
1538+
@param abi defaults to [Abi.unknown]
1539+
@param fabi defaults to [Fabi.unknown]
1540+
@param options defaults to [Options.empty]
1541+
1542+
@since 2.5.0 *)
1543+
val filter :
1544+
?strict:bool ->
1545+
?parent:t ->
1546+
?system:system ->
1547+
?abi:abi ->
1548+
?fabi:fabi ->
1549+
?filetype:filetype ->
1550+
?options:options -> unit -> t list
1551+
14501552
(** [get ?package name] returns the target with the given [name].
14511553
14521554
If the target with the given name wasn't declared raises an
@@ -2308,7 +2410,19 @@ module Theory : sig
23082410
(** The source code language.
23092411
23102412
@since 2.2.0 *)
2311-
module Language : KB.Enum.S with type t = language
2413+
module Language : sig
2414+
include KB.Enum.S with type t = language
2415+
2416+
2417+
(** {3 Predefined languages}
2418+
2419+
@since 2.5.0 *)
2420+
val c : language
2421+
val cxx : language
2422+
val ada : language
2423+
val fortran : language
2424+
val pascal : language
2425+
end
23122426

23132427
(** Defines how multibyte words are stored in the memory.
23142428
@@ -2337,19 +2451,94 @@ module Theory : sig
23372451

23382452
(** The Operating System.
23392453
@since 2.2.0 *)
2340-
module System : KB.Enum.S with type t = system
2454+
module System : sig
2455+
include KB.Enum.S with type t = system
2456+
2457+
2458+
(** {3 Predefined Operating System Interfaces}
2459+
2460+
@since 2.5.0 *)
2461+
2462+
2463+
(** Linux-based systems, including Android *)
2464+
val linux : system
2465+
2466+
(** Darwin-based systems, including iOS, macOS *)
2467+
val darwin : system
2468+
2469+
(** VxWorks RTOS *)
2470+
val vxworks : system
2471+
2472+
(** FreeBSD *)
2473+
val freebsd : system
2474+
2475+
(** [OpenBSD] *)
2476+
val openbsd : system
2477+
2478+
(** The MS Windows family of OSes *)
2479+
val windows : system
2480+
2481+
(** MS-DOS and other DOSes *)
2482+
val msdos : system
2483+
2484+
(** Unified Extensible Firmware Interface *)
2485+
val uefi : system
2486+
2487+
(** standalone *)
2488+
val none : system
2489+
end
23412490

23422491
(** The Application Binary Interface name.
23432492
@since 2.2.0 *)
2344-
module Abi : KB.Enum.S with type t = abi
2493+
module Abi : sig
2494+
include KB.Enum.S with type t = abi
2495+
2496+
(** {3 Predefined ABI}
2497+
@since 2.5.0 *)
2498+
2499+
(** GNU/SysV *)
2500+
val gnu : abi
2501+
2502+
(** ARM EABI (use gnu for linux targets) *)
2503+
val eabi : abi
2504+
2505+
(** GNU ARM EABI *)
2506+
val gnueabi : abi
2507+
2508+
val cdecl : abi
2509+
val stdcall : abi
2510+
val fastcall : abi
2511+
val watcom : abi
2512+
val ms : abi
2513+
end
23452514

23462515
(** The Application Floating-point Binary Interface name.
23472516
@since 2.2.0 *)
2348-
module Fabi : KB.Enum.S with type t = fabi
2517+
module Fabi : sig
2518+
include KB.Enum.S with type t = fabi
2519+
2520+
2521+
(** {3 Predefined FABI}
2522+
2523+
@since 2.5.0 *)
2524+
val hard : fabi
2525+
val soft : fabi
2526+
end
23492527

23502528
(** The file type that is used to pack the unit.
23512529
@since 2.3.0 *)
2352-
module Filetype : KB.Enum.S with type t = filetype
2530+
module Filetype : sig
2531+
include KB.Enum.S with type t = filetype
2532+
2533+
2534+
(** {3 Predefined File Formats}
2535+
2536+
@since 2.5.0 *)
2537+
val elf : filetype
2538+
val coff : filetype
2539+
val macho : filetype
2540+
val aout : filetype
2541+
end
23532542

23542543
(** Information about the compiler.
23552544

lib/bap_core_theory/bap_core_theory_program.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ let string_property ?(domain=name) ~desc cls name =
5454
~public:true
5555
~desc
5656

57-
module Language = Knowledge.Enum.Make()
57+
module Language = struct
58+
include Knowledge.Enum.Make()
59+
let c = declare ~package "c"
60+
let cxx = declare ~package "cxx"
61+
let ada = declare ~package "ada"
62+
let fortran = declare ~package "fortran"
63+
let pascal = declare ~package "pascal"
64+
end
5865
type language = Language.t
5966

6067
module Source = struct

lib/bap_core_theory/bap_core_theory_program.mli

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ module Source : sig
3535
val file : (cls,string option) slot
3636
end
3737

38-
module Language : Knowledge.Enum.S with type t = language
38+
module Language : sig
39+
include Knowledge.Enum.S with type t = language
40+
val c : language
41+
val cxx : language
42+
val ada : language
43+
val fortran : language
44+
val pascal : language
45+
end
3946

4047
module Unit : sig
4148
open Knowledge

0 commit comments

Comments
 (0)