package binsec_codex

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file codex_options.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(**************************************************************************)
(*  This file is part of the Codex semantics library.                     *)
(*                                                                        *)
(*  Copyright (C) 2013-2025                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file LICENSE).                      *)
(*                                                                        *)
(**************************************************************************)

(* Helper functions to parse the command line.
   Note: must be called after the types and image are loaded. *)

(** Parses a type from a string *)
let type_of_string = Types.Parse_ctypes.type_of_string

(* Parse an address or a symbol. *)
let address_of_string s =
  match s.[0] with
  | '0' -> Z.of_string s
  | _ -> begin
      match Loader_utils.address_of_symbol_by_name ~name:s @@ Kernel_functions.get_img () with
      | None -> failwith ("Unknown address `" ^ s ^ "'")
      | Some x -> Z.of_int x
    end
;;

let directive_of_string s =
  match s with
  | "stop" -> `stop
  | "nop" -> `nop
  | "explore" -> `explore
  | _ ->
    try
      (* Codex_log.feedback "Scanning |%s|" s; *)
      let typ =  Scanf.sscanf s "return_unknown(%s@)" type_of_string in
      `return_unknown(typ)
    with Scanf.Scan_failure _ ->
    try
      (* Codex_log.feedback "Scanning |%s|" s; *)
      let address =  Scanf.sscanf s "skip_to(%s@)" address_of_string in
      `skip_to(Virtual_address.create (Z.to_int address))
    with Scanf.Scan_failure _ -> failwith ("Unknown directive: " ^ s)

;;

include Cli.Make (struct
  let name = "codex"
  let shortname = "codex"
end)

module ApplicationFile = struct
  include Builder.String(struct
    let name = "app-file"
    let default = ""
    let doc = "Path to an application ELF"
  end)
end

module TypeConfigurationFile = struct
  include Builder.String(struct
    let name = "type-file"
    let default = ""
    let doc = "Path to the file containing the type definitions"
  end)

end

module X86Types = struct
  include Builder.String (struct
    let name = "x86-types"
    let default = "rr"
    let doc = "Version of the C types for x86 OS"
  end)
end

module UseShape = struct
  include Builder.No (struct
    let name = "use-shape"
    let doc = "Set to not use shape"
  end)
end

module NbTasks = struct
  include Builder.Integer (struct
    let name = "nb-tasks"
    let doc = "Number of tasks (required)"
    let default = 0
  end)
end

module AnalyzeKernel = struct
  include Builder.False (struct
    let name = "analyze-kernel"
    let doc = "Whether to analyze a kernel (tailored, more complex analysis)"
  end)
end

module DynThreads = struct
  include Builder.No (struct
    let name = "dyn-threads"
    let doc = "Analyzed kernel features dynamic thread creation"
  end)
end

(*
module FnArgs = struct
  include Builder.String_list (struct
    let name = "fn-args"
    let doc = "Argument types to put in the stack at the initial state"
  end)
  let get() = get() |> List.map type_of_string
end
*)

module GlobalsTypes = struct
  include Builder.String_list (struct
      let name = "globals-types"
      let doc = "Assignment of globals to types. It is a coma-separated list of addr=typ, where address can be symbols or hexa."
    end)
  let get() = get() |> List.map (fun s ->
      let addr,typ = match String.split_on_char '=' s with
        | [a;b] -> String.trim a,String.trim b
        | _ -> failwith ("Global spec should be of the form addr=typ; got " ^ s)
      in
      let addr = address_of_string addr in
      let typ = type_of_string typ in
      (addr,typ)
    )
end

module Hooks = struct
  include Builder.String_list (struct
      let name = "hooks"
      let doc = "Hooks at certain addresses (which can be symbols or hexa) to directives, of the form addr=directive, where directive is stop|explore|skip. stop stops here, explore means do meet over all paths for the function from this point, and skip means immediately return (must be at function entry)"
    end)
  let get() = get() |> List.map (fun s ->
      let addr,directive = match String.split_on_char '=' s with
        | [a;b] -> String.trim a,String.trim b
        | _ -> failwith ("Global spec should be of the form addr=directive; got " ^ s)
      in
      let addr = Virtual_address.create @@ Z.to_int @@ address_of_string addr in
      let directive = directive_of_string directive in
      (addr,directive)
    )
end


module Focusing = struct
  include Builder.No (struct
    let name = "focusing"
    let doc = "Activate focusing"
  end)
end

module Output_Html = struct
  include Builder.String_option(struct
    let name = "output-html"
    let doc = "Output the results in an html file"
  end)
end

module UseLoopDomain = struct
  include Builder.False (struct
    let name = "use-loop-domain"
    let doc = "Activate inductive loop domain"
  end)
end

module VariableDisplay = Builder.Variant_choice(struct
  type t = Domains.Term_domain.pretty_terms
  let name = "variable-display"
  let doc = "How codex should display variable information. \
          \ One of 'value' (only print inferred value, default)\
          \ 'symbolic' (print associated symbolic term)\
          \ 'both' (print both value and symbolic term)\
          \ 'relation' (print value, symbolic term, and union-find relation associated with this term)."


  open Domains.Term_domain
  let to_string = function
    | Value -> "value"
    | Both -> "both"
    | Symbolic -> "symbolic"
    | Relation -> "relation"
  let of_string = function
    | "value" -> Value
    | "both" -> Both
    | "symbolic" -> Symbolic
    | "relation" -> Relation
    | _ -> assert false

  let choices = [ "value"; "both"; "symbolic"; "relation" ]
  let default = Value
end)

module MMIOs = struct
  include Builder.String_list (struct
      let name = "mmios"
      let doc = "sets memory blocks of the form addr+size as readable but unknown content"
    end)
  let get() = get() |> List.map (fun s ->
      let addr,size = match String.split_on_char '+' s with
        | [a;b] -> String.trim a,String.trim b
        | _ -> failwith ("Global spec should be of the form addr+size; got " ^ s)
      in
      let size = int_of_string size in
      let addr = int_of_string addr in
      (addr,size)
    )
end
(*
module GlobalSymbols = struct
  include Builder.String_list (struct
      let name = "global-symbols"
      let doc = "Declaration of global symbols and their types. It is a coma-separated list of sym=typ, where symbols are identifiers"
    end)
  let get() = get() |> List.map (fun s ->
      let symb,typ = match String.split_on_char '=' s with
        | [a;b] -> String.trim a,String.trim b
        | _ -> failwith ("Global spec should be of the form symb=typ; got " ^ s)
      in
      let typ = type_of_string typ in
      (symb,typ)
    )
end
*)
module Location = struct

  (* Absolute location representing an address. *)
  type address_location = int64 Syntax_tree.Location_identifier.t

  module L = struct
    include (Tracelog:sig type location = Tracelog.location = .. end)
    type location +=
      | Function of string
      | Instruction of { locid: address_location; address:int64 }
      | Dba_instr of { locid: Dba.Instr.t Syntax_tree.Location_identifier.t; dba_instr: Dba.Instr.t } (* Instr and position in the dhunk. *)
      | Dba_expr of { locid: Dba.Expr.t Syntax_tree.Location_identifier.t; dba_expr: Dba.Expr.t }     (* Expr and position in the expr. *)
    type t = location

  end

  include L

  let pp_loc fmt location = match location with
    | Function f -> Format.fprintf fmt "In function `%s':@ " f
    | Instruction {address} -> Format.fprintf fmt "In function `%08Lx':@ " address
    | Dba_instr{dba_instr} ->
      Format.fprintf fmt "In Dba_instr `%s':@ "
        (dba_instr |>
         Binsec2syntax_tree.instr_of_binsec_instr |>
         Syntax_tree.string_of_expr)
    | Dba_expr {dba_expr} ->
      Format.fprintf fmt "In Dba_expr `%s':@ "
        (dba_expr |>
         Binsec2syntax_tree.expr_of_binsec_expr |>
         Syntax_tree.string_of_expr)
    | _ -> assert false

  let pp_loc_stack fmt stack = match stack with
    | [] -> ()
    | _ -> Format.fprintf fmt "@[<hv>"; List.iter (pp_loc fmt) (List.rev stack); Format.fprintf fmt "@]"

  let () = Tracelog.set_pp_location_stack pp_loc_stack
end