package mopsa

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

Source file 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
(****************************************************************************)
(*                                                                          *)
(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)
(*                                                                          *)
(* Copyright (C) 2017-2019 The MOPSA Project.                               *)
(*                                                                          *)
(* This program is free software: 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, either version 3 of the License, or     *)
(* (at your option) any later version.                                      *)
(*                                                                          *)
(* This program 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.                      *)
(*                                                                          *)
(* You should have received a copy of the GNU Lesser General Public License *)
(* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *)
(*                                                                          *)
(****************************************************************************)

(** Management of command-line options *)

open Mopsa_utils
open ArgExt
module StringSet = SetExt.StringSet

 (** Command-line option *)
type opt =
  | O_builtin of arg
  (** Built-in option *)

  | O_language of string * arg
  (** Language option *)

  | O_domain of string * arg
  (** Domain option *)

  | O_shared of string * arg
  (** Shared options that can be imported by several domains. *)

(** {2 Registration} *)
(** **************** *)

(** List of registered options *)
let options : opt list ref = ref []

(** Map giving the shared options imported by a domain *)
let imports : (string (* domain *), StringSet.t (* imported options *)) Hashtbl.t = Hashtbl.create 16

(** Register a built-in option *)
let register_builtin_option (arg:arg) =
  options := (O_builtin arg) :: !options

(** Register a language option. *)
let register_language_option (lang:string) (arg:arg) =
  options := (O_language (lang, arg)) :: !options

(** Register a domain option. *)
let register_domain_option (dom:string) (arg:arg) =
  options := (O_domain (dom, arg)) :: !options

(** Register a shared option *)
let register_shared_option (name:string) (arg:arg) =
  options := (O_shared (name, arg)) :: !options

(** Import a shared option into a domain *)
let import_shared_option (name:string) (domain:string) =
  let old = try Hashtbl.find imports domain with Not_found -> StringSet.empty in
  Hashtbl.replace imports domain (StringSet.add name old)

(** Get the imported options of a domain. *)
let find_domain_imports (dom:string) =
  try Hashtbl.find imports dom with Not_found -> StringSet.empty


(** {2 Interface with Arg and Output} *)
(** ********************************* *)

let opt_to_arg opt =
  match opt with
  | O_builtin d | O_language (_, d) | O_domain (_, d) | O_shared (_, d) ->
    d

(** {2 Filters} *)
(** *********** *)

(** Return the list of options *)
let get_options () =
  List.map opt_to_arg !options

 (** Return the list of built-in options *)
let get_builtin_options () =
  List.filter (fun opt ->
      match opt with
      | O_builtin _ -> true
      | _ -> false
    ) !options |>
  List.map opt_to_arg

(** Return the list of registered options of a language *)
let get_language_options (lang:string) =
  List.filter (fun opt ->
      match opt with
      | O_language (l, arg) -> l = lang
      | _ -> false
    ) !options |>
  List.map opt_to_arg

(** Find a standalone option *)
let find_shared_option (name:string) =
  List.find (fun opt ->
      match opt with
      | O_shared (n, _) -> n = name
      | _ -> false
    ) !options

(** Return the list of registered options of a domain *)
let get_domain_options (dom:string) =
  (* Options registered by the domain *)
  let opt1 = List.filter (fun opt ->
      match opt with
      | O_domain (d, arg) -> d = dom
      | _ -> false
    ) !options
  in
  (* Options registered by the groups of the domain *)
  let opt2 =
    find_domain_imports dom |>
    StringSet.elements |>
    List.map find_shared_option
  in
  List.map opt_to_arg (opt1 @ opt2)



(** {2 Built-in options} *)
(** ******************** *)


(** Path to share directory *)
let () =
  register_builtin_option {
    key = "-share-dir";
    category = "Configuration";
    doc = " path to the share directory";
    spec = ArgExt.Set_string Paths.opt_share_dir;
    default = "";
  }


(** Analysis configuration *)
let () =
  register_builtin_option {
    key = "-config";
    category = "Configuration";
    doc = " path to the configuration file to use for the analysis";
    spec = ArgExt.Set_string Config.Parser.opt_config;
    default = "";
  }

(** Warnings *)
let () =
  register_builtin_option {
    key = "-no-warning";
    category = "Debugging";
    doc = " deactivate warning messages";
    spec = ArgExt.Clear Debug.print_warnings;
    default = "";
  }

(** Active hooks *)
let () =
  register_builtin_option {
    key = "-hook";
    category = "Configuration";
    doc = " activate a hook";
    spec = ArgExt.String (fun s ->
        try Core.Hook.activate_hook s
        with Not_found -> Exceptions.panic "hook %s not found" s
      );
    default = "";
  }


(** Size of the cache *)
let () =
  register_builtin_option {
    key = "-cache";
    category = "Configuration";
    doc = " size of the analysis cache";
    spec = ArgExt.Set_int Core.Cache.opt_cache;
    default = "5";
  }


(** Debug channels *)

(* Activate "print" channel by default *)
let () = Debug.parse "print"
let () =
  register_builtin_option {
    key = "-debug";
    category = "Debugging";
    doc = " select active debug channels. (syntax: <c1>,<c2>,...,<cn> and '_' can be used as a wildcard)";
    spec = ArgExt.String (fun s ->
        (* Always keep "print" channel *)
        Debug.parse ("print," ^ s)
      );
    default = "print";
  };
  register_builtin_option {
    key = "-no-color";
    category = "Debugging";
    doc = " deactivate colors in debug messages.";
    spec = ArgExt.Clear Debug.print_color;
    default = "";
  }

(** List of available domains *)
let () =
  register_builtin_option {
    key = "-list";
    category = "Help";
    doc = " list available domains/checks/hooks; if a configuration is specified, only used domains are listed";
    spec = ArgExt.Symbol_exit (
        ["domains"; "checks"; "hooks"],
        (fun selection ->
           match selection with
           | "domains" ->
             let domains =
               if !Config.Parser.opt_config = "" then
                 Config.Parser.all_domains ()
               else
                 Paths.resolve_config_file !Config.Parser.opt_config |>
                 Config.Parser.domains
             in
             List.sort_uniq compare domains |>
             Output.Factory.list_domains

           | "checks" ->
             let checks =
               if !Config.Parser.opt_config = "" then
                 (* List checks of all registered domains *)
                 let domains = Config.Parser.all_domains () in
                 List.fold_left
                   (fun acc domain ->
                      try
                        let module D = (val Sig.Abstraction.Stacked.find_stacked_domain domain) in
                        D.checks @ acc
                      with Not_found ->
                      try
                        let module D = (val Sig.Abstraction.Domain.find_standard_domain domain) in
                        D.checks @ acc
                      with Not_found ->
                      try
                        let module D = (val Sig.Abstraction.Stateless.find_stateless_domain domain) in
                        D.checks @ acc
                      with Not_found -> acc
                   ) [] domains
               else
                 let abstraction = Config.Parser.(parse @@ Paths.resolve_config_file !opt_config) in
                 let domain = Config.Builder.from_json abstraction.domain in
                 let module Domain = (val domain) in
                 Domain.checks
             in
             List.sort_uniq compare checks |>
             Output.Factory.list_checks

           | "hooks" ->
             let d =
               List.map
                 (fun (h:(module Core.Hook.HOOK)) ->
                    let module H = (val h) in
                    H.name
                 ) (Core.Hook.list_hooks ())
             in
             List.sort_uniq compare d |>
             Output.Factory.list_hooks

           | _ -> assert false
        ));
    default = "";
  }

(** Output format *)
let () =
  register_builtin_option {
    key = "-format";
    category = "Output";
    doc = " selects the output format.";
    spec = ArgExt.Symbol (
        ["text"; "json"],
        (fun s ->
           match s with
           | "text" -> Output.Common.(opt_format := F_text)
           | "json" ->
              Output.Common.(opt_format := F_json);
              Debug.print_color := false
           | _ -> assert false
        )
      );
    default = "text";
  }

(** Output last flow *)
let () =
  register_builtin_option {
    key = "-lflow";
    category = "Output";
    doc = " display the last output";
    spec = ArgExt.Set Output.Common.opt_display_lastflow;
    default = "false";
  }


(** Ignore alarms when returning a value to the shell *)
let () =
  register_builtin_option {
    key = "-silent";
    category = "Output";
    doc = " do not return a non-zero value when detecting alarms";
    spec = ArgExt.Set Output.Common.opt_silent;
    default = "unset";
  }


(** Output stream *)
let () =
  register_builtin_option {
    key = "-output";
    category = "Output";
    doc = " redirect output to a file";
    spec = ArgExt.String (fun s -> Output.Common.opt_file := Some s);
    default = "";
  }


let () =
  register_builtin_option {
    key = "-show-callstacks";
    category = "Alarms";
    doc = " display the call stacks when reporting alarms in text format";
    spec = ArgExt.Set Output.Text.opt_show_callstacks;
    default = "false";
  }

let () =
  register_builtin_option {
    key = "-tw";
    category = "Output";
    doc = " set the tab width";
    spec = ArgExt.Set_int Output.Text.opt_tw;
    default = "4";
  }


let () =
  register_builtin_option {
    key = "-show-safe-checks";
    category = "Alarms";
    doc = " show safe checks when reporting alarms in text format";
    spec = ArgExt.Set Output.Common.opt_show_safe_checks;
    default = "false";
  }

(** Apply cleaners on T_cur only *)
let () =
  register_builtin_option {
    key = "-clean-cur-only";
    category = "Configuration";
    doc = " flag to apply cleaners on the current environment only";
    spec = ArgExt.Set Core.Cases.opt_clean_cur_only;
    default = "";
  }

let () = register_builtin_option {
    key = "-hash-heap-address";
    category = "Heap";
    doc = "  format heap addresses with their hash";
    spec = ArgExt.Bool (fun b -> Core.Ast.Addr.opt_hash_addr := b);
    default = "false";
  }

let () = register_builtin_option {
    key = "-working-dir";
    category = "Configuration";
    doc = " set the working directory, used when resolving relative paths";
    spec = ArgExt.String (fun s ->
        if Sys.file_exists s
        then Sys.chdir s
        else Exceptions.panic "'%s' does not exist" s
      );
    default = "";
  }

(** Help message *)
let help () =
  let options =
    if !Config.Parser.opt_config = "" then
      List.map opt_to_arg !options
    else
      (* Get the language and domains of selected configuration *)
      let config = Paths.resolve_config_file !Config.Parser.opt_config in
      let lang = Config.Parser.(language config) in
      let domains = Config.Parser.(domains config) in

      (* Get the options *)
      (get_builtin_options ())    @
      (get_language_options lang) @
      (List.map get_domain_options domains |> List.flatten)
  in
  Output.Factory.help options

let () =
  register_builtin_option {
    key  = "-help";
    category = "Help";
    doc  = " display the list of options";
    spec = ArgExt.Unit_exit help;
    default = "";
  };
  register_builtin_option {
    key  = "--help";
    category = "Help";
    doc  = " display the list of options";
    spec = ArgExt.Unit_exit help;
    default = "";
  };
  register_builtin_option {
    key  = "-h";
    category = "Help";
    doc  = " display the list of options";
    spec = ArgExt.Unit_exit help;
    default = "";
  }


(** Version *)

let print_version () =
  Printf.printf "%s (%s)\n" Version.version Version.dev_version

let () =
  register_builtin_option {
    key = "-v";
    category = "Configuration";
    doc = " Mopsa version";
    spec = ArgExt.Unit_exit print_version;
    default = "";
  }
OCaml

Innovation. Community. Security.