package mopsa

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

Source file json.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
(****************************************************************************)
(*                                                                          *)
(* 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/>.    *)
(*                                                                          *)
(****************************************************************************)

(** Format the results of the analysis in JSON. *)

open Mopsa_utils
open Yojson.Basic
open ArgExt
open Core.All
open Callstack
open Location
open Common

module AlarmKindSet = SetExt.Make(struct type t = alarm_kind let compare = compare_alarm_kind end)


let print out json =
  let channel =
    match out with
    | None -> stdout
    | Some file ->
       open_out_gen [Open_creat; Open_wronly; Open_creat] 0o644 file
  in
  Yojson.Basic.pretty_to_channel channel json

let render_pos pos =
  let file = Location.get_pos_file pos in
  let line = Location.get_pos_line pos in
  let column = Location.get_pos_column pos in
  `Assoc [
    "file", `String file;
    "line", `Int line;
    "column", `Int column;
  ]


let render_range range =
  if not @@ is_orig_range @@ untag_range range then
    `Assoc []
  else
    `Assoc [
        "start", render_pos (Location.get_range_start range);
        "end", render_pos (Location.get_range_end range)
      ]

let render_call (c:callsite)  =
  `Assoc [
    "function", `String c.call_fun_orig_name;
    "range", render_range c.call_range;
  ]

let render_callstack cs  =
  `List (List.map render_call cs)

let aggregate_alarms report =
  RangeCallStackMap.fold
    (fun (range, cs) checks acc ->
       CheckMap.fold
         (fun check diag acc ->
            match diag.diag_kind with
            | Safe | Unreachable -> acc
            | Error | Warning ->
              let csl = AlarmSet.elements diag.diag_alarms |>
                        List.map callstack_of_alarm in
              (range,check,csl) :: acc
         ) checks acc
    ) report.report_diagnostics []

let render_check check =
  let title = Format.asprintf "%a" pp_check check in
  `String title

let render_alarm_messages kinds =
  `String (Format.asprintf "%a" (Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt "@,") pp_alarm_kind) kinds)

let render_alarms report =
  RangeCallStackMap.fold
    (fun (range, cs) checks acc ->
      CheckMap.fold
        (fun check diag (safe, total, acc) ->
          match diag.diag_kind with
          | Safe when not !opt_show_safe_checks ->
             safe + 1, total + 1, acc

          | Safe | Error | Warning ->
              (* Get the set of alarms kinds and callstacks *)
              let kinds =
                AlarmSet.fold
                  (fun a kinds ->
                     AlarmKindSet.add a.alarm_kind kinds
                  ) diag.diag_alarms AlarmKindSet.empty in
              (* Join alarm kinds *)
              let rec iter = function
                | [] -> []
                | hd::tl ->
                  let hd',tl' = iter_with hd tl in
                  hd'::iter tl'
              and iter_with a = function
                | [] -> a,[]
                | hd::tl ->
                  match join_alarm_kind a hd with
                  | None    ->
                    let a',tl' = iter_with a tl in
                    a',hd::tl'
                  | Some aa ->
                    let aa',tl' = iter_with aa tl in
                    aa',tl'
              in
              let kinds' = iter (AlarmKindSet.elements kinds) in
              let json_diag =
                `Assoc [
                    "kind", `String (Format.asprintf "%a" pp_diagnostic_kind diag.diag_kind);
                    "title", render_check check;
                    "messages", render_alarm_messages kinds';
                    "range", render_range range;
                    "callstack", render_callstack diag.diag_callstack
                  ]
              in
              (if diag.diag_kind = Safe then safe + 1 else safe),
              total + 1,
              json_diag :: acc
          | _ -> safe, total, acc
        ) checks acc
    ) report.report_diagnostics (0, 0, [])


let render_soudness_assumtion h =
  let msg = Format.asprintf "%a" pp_assumption_kind h.assumption_kind in
  match h.assumption_scope with
  | A_global ->
    `Assoc [
      "message", `String msg;
    ]

  | A_local r ->
    `Assoc [
      "range", render_range r;
      "message", `String msg;
    ]


let render_var var  =
  `String var.vname

let render_value value  =
  `String value

let render_env (var,value)  =
  `Assoc [
    "var", render_var var;
    "val"   , render_value value;
  ]


let report man flow ~time ~files ~out : unit =
  let rep = Flow.get_report flow in
  let safe, total, checks = render_alarms rep in 
  let json  = `Assoc [
      "success", `Bool true;
      "time", `Float time;
      "mopsa_version", `String Version.version;
      "mopsa_dev_version", `String Version.dev_version;
      "files", `List (List.map (fun f -> `String f) files);
      "selectivity", `String (Format.asprintf "%d/%d" safe total);
      "checks", `List checks;
      "assumptions", `List (AssumptionSet.elements rep.report_assumptions |> List.map render_soudness_assumtion );
    ]
  in
  print out json


let panic exn ~btrace ~time ~files ~out _ =
  let open Exceptions in
  let error,range,cs =
    match exn with
    | Panic (msg, loc) -> msg, None, None
    | PanicAtLocation (range, msg, loc) -> msg,Some range,None
    | PanicAtFrame (range, cs, msg, loc) -> msg,Some range,Some cs
    | SyntaxError(range, msg) -> msg,Some range,None
    | SyntaxErrorList l -> String.concat ", " (List.map snd l),None,None
    |  _ -> Printexc.to_string exn,None,None
  in
  let assoc =
    [ "success", `Bool false;
      "time", `Float time;
      "mopsa_version", `String Version.version;
      "mopsa_dev_version", `String Version.dev_version;
      "files", `List (List.map (fun f -> `String f) files);
      "exception", `String error;
      "backtrace", `String btrace; ]
    @ (match range with
        | None -> []
        | Some r -> [ "range", render_range r ] )
    @ (match cs with
        | None -> []
        | Some c -> [ "callstack", render_callstack c ] )
  in
  print out (`Assoc assoc)


let help (args:ArgExt.arg list) ~out =
  let json  = `List (
      args |>
      List.map (fun arg ->
          `Assoc [
            "key", `String arg.key;
            "doc", `String arg.doc;
            "category", `String arg.category;
            "default", `String arg.default;
            "type", `String (
              match arg.spec with
              | Bool _ -> "bool"
              | Set _ -> "set"
              | Clear _ -> "clear"
              | Unit _ -> "unit"
              | String _ -> "string"
              | Set_string _ -> "string"
              | Int _ -> "int"
              | Set_int _ -> "int"
              | Symbol (l, _) -> "symbol:" ^ (String.concat "," l)
              | _ -> failwith "Not implemented"
            )
          ]
        )
    )
  in
  print out json

let list_domains (domains:string list) ~out =
  let json = `List (
      domains |>
      List.map (fun d -> `String d)
    )
  in
  print out json

let list_reductions (reductions:string list) ~out =
  let json = `List (
      reductions |>
      List.map (fun d -> `String d)
    )
  in
  print out json

let list_checks checks ~out =
  let json = `List (
      List.map render_check checks
    )
  in
  print out json

let list_hooks hooks ~out =
  let json = `List (
      hooks |>
      List.map (fun d -> `String d)
    )
  in
  print out json

let print printer ~range ~out =
  let json =
    `Assoc [
       "print", print_object_to_json (get_printed_object printer);
       "range", render_range range;
     ]
  in
  print out json
OCaml

Innovation. Community. Security.