sarif_gen.ml 7.85 KB
Newer Older
1
2
3
4
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
Andre Maroneze's avatar
Andre Maroneze committed
5
(*  Copyright (C) 2007-2020                                               *)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(*    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 licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

23
24
25
26
open Sarif

let frama_c_sarif =
  let name = "frama-c" in
27
28
  let version = Fc_config.version_and_codename in
  let semanticVersion = Fc_config.version in
29
30
31
  let fullName = name ^ "-" ^ version in
  let downloadUri = "https://frama-c.com/download.html" in
  Tool.create
Andre Maroneze's avatar
Andre Maroneze committed
32
    (Driver.create ~name ~version ~semanticVersion ~fullName ~downloadUri ())
33
34
35
36
37
38

let get_remarks () =
  let f = Mdr_params.Remarks.get () in
  if f <> "" then Parse_remarks.get_remarks f
  else Datatype.String.Map.empty

39
40
41
42
43
let get_remark remarks label =
  match Datatype.String.Map.find_opt label remarks with
  | None -> []
  | Some l -> l

44
45
46
47
48
49
50
51
52
53
let command_line () = Array.to_list Sys.argv

module Analysis_cmdline =
  State_builder.Ref(Datatype.List(Datatype.String))
    (struct
      let name = "Sarif_gen.Analysis_cmdline"
      let dependencies = []
      let default = command_line
    end)

54
let gen_invocation () =
55
56
57
  let cl = Analysis_cmdline.get () in
  let commandLine = String.concat " " cl in
  let arguments = List.tl cl in
58
59
  Invocation.create ~commandLine ~arguments ()

60
61
62
63
64
let gen_remark alarm =
  let open Markdown in
  [ Block
      [ Text
          (plain
65
             (Printf.sprintf "This alarm represents a potential %s."
66
67
68
69
70
71
                (Alarms.get_description alarm)
             )
          )
      ]
  ]

Andre Maroneze's avatar
Andre Maroneze committed
72
let kind_of_status =
73
  let open Property_status.Feedback in
Andre Maroneze's avatar
Andre Maroneze committed
74
  let open Sarif.Result_kind in
75
76
77
  function
  | Never_tried -> notApplicable
  | Considered_valid | Valid | Valid_under_hyp | Valid_but_dead -> pass
Andre Maroneze's avatar
Andre Maroneze committed
78
79
80
81
82
83
84
85
86
87
88
  | Unknown | Unknown_but_dead -> open_
  | Invalid | Invalid_under_hyp | Invalid_but_dead -> fail
  | Inconsistent -> review

let level_of_status =
  let open Property_status.Feedback in
  let open Sarif.Result_level in
  function
  | Never_tried -> none
  | Considered_valid | Valid | Valid_under_hyp | Valid_but_dead -> none
  | Unknown | Unknown_but_dead -> none
89
  | Invalid | Invalid_under_hyp | Invalid_but_dead -> error
Andre Maroneze's avatar
Andre Maroneze committed
90
  | Inconsistent -> none
91

92
93
let make_message alarm annot remark =
  let open Markdown in
94
95
96
  let name = Alarms.get_name alarm in
  let text = name ^ "." in
  let kind = plain (name ^ ":") in
97
  let descr = codeblock ~lang:"acsl" "%a" Printer.pp_code_annotation annot in
98
  let summary = Block (Text kind :: descr) in
99
100
  let markdown =
    match remark with
Virgile Prevosto's avatar
Virgile Prevosto committed
101
102
    | [] -> summary :: gen_remark alarm
    | _ -> summary :: remark
103
  in
104
  let markdown =
105
106
    String.trim
      (Format.asprintf "@[%a@]" (Markdown.pp_elements ~page:"") markdown)
107
  in
108
  Message.create ~text ~markdown ()
109

110
111
112
113
let opt_physical_location_of_loc loc =
  if loc = Cil_datatype.Location.unknown then []
  else [ Location.of_loc loc ]

114
let gen_results remarks =
115
  let treat_alarm _e kf s ~rank:_ alarm annot (i, rules, content) =
116
    let prop = Property.ip_of_code_annot_single kf s annot in
117
118
119
120
    let ruleId = Alarms.get_name alarm in
    let rules =
      Datatype.String.Map.add ruleId (Alarms.get_description alarm) rules
    in
121
    let label = "Alarm-" ^ string_of_int i in
Andre Maroneze's avatar
Andre Maroneze committed
122
    let kind = kind_of_status (Property_status.Feedback.get prop) in
123
    let level = level_of_status (Property_status.Feedback.get prop) in
124
125
    let remark = get_remark remarks label in
    let message = make_message alarm annot remark in
126
    let locations = opt_physical_location_of_loc (Cil_datatype.Stmt.loc s) in
127
    let res =
Andre Maroneze's avatar
Andre Maroneze committed
128
      Sarif_result.create ~kind ~level ~ruleId ~message ~locations ()
129
    in
130
131
132
133
    (i+1, rules, res :: content)
  in
  let _, rules, content =
    Alarms.fold treat_alarm (0, Datatype.String.Map.empty,[])
134
  in
135
  rules, List.rev content
136

137
let is_alarm = function
138
  | Property.(IPCodeAnnot { ica_ca }) -> Extlib.has_some (Alarms.find ica_ca)
139
140
141
  | _ -> false

let make_ip_message ip =
142
  let text = Format.asprintf "@[%a.@]" Property.short_pretty ip in
143
144
  Message.plain_text ~text ()

145
146
let user_annot_id = "user-spec"

147
148
149
let gen_status ip =
  let status = Property_status.Feedback.get ip in
  let level = level_of_status status in
150
  let locations = opt_physical_location_of_loc (Property.location ip) in
151
  let message = make_ip_message ip in
152
  Sarif_result.create ~ruleId:user_annot_id ~level ~locations ~message ()
153
154
155
156
157
158
159

let gen_statuses () =
  let f ip content =
    if is_alarm ip then content else (gen_status ip) :: content
  in
  List.rev (Property_status.fold f [])

160
let gen_artifacts () =
161
  let add_src_file f =
162
    let uri = Filepath.Normalized.to_pretty_string f in
163
    let location = ArtifactLocation.create ~uri () in
164
165
    let roles = [ Role.analysisTarget ] in
    let mimeType = "text/x-csrc" in
166
    Artifact.create ~location ~roles ~mimeType ()
167
168
169
  in
  List.map add_src_file (Kernel.Files.get ())

170
171
let add_rule id desc l =
  let text = desc ^ "." in
172
173
174
  let shortDescription = MultiformatMessageString.create ~text () in
  let rule = ReportingDescriptor.create ~id ~shortDescription () in
  rule :: l
175

176
let make_taxonomies rules = Datatype.String.Map.fold add_rule rules []
177

178
let gen_run remarks =
179
  let tool = frama_c_sarif in
180
  let name = "frama-c" in
181
  let invocations = [gen_invocation ()] in
Virgile Prevosto's avatar
Virgile Prevosto committed
182
183
184
185
  let rules, results = gen_results remarks in
  let user_annot_results = gen_statuses () in
  let rules =
    match user_annot_results with
Virgile Prevosto's avatar
Virgile Prevosto committed
186
187
188
    | [] -> rules
    | _ ->
      Datatype.String.Map.add
189
        user_annot_id "User-written ACSL specification" rules
Virgile Prevosto's avatar
Virgile Prevosto committed
190
  in
191
  let rules = make_taxonomies rules in
192
  let taxonomies = [ToolComponent.create ~name ~rules ()] in
Virgile Prevosto's avatar
Virgile Prevosto committed
193
  let results = results @ user_annot_results in
194
  let artifacts = gen_artifacts () in
195
196
197
198
199
200
201
202
203
204
205
206
207
  let uriBases = ("PWD", Sys.getcwd ()) :: Filepath.all_symbolic_dirs () in
  let uriBasesJson =
    List.fold_left (fun acc (name, dir) ->
        (name, `Assoc [("uri", `String dir)]) :: acc
      ) [] uriBases
  in
  let originalUriBaseIds =
    match ArtifactLocationDictionary.of_yojson (`Assoc uriBasesJson) with
    | Ok x -> x
    | Error s -> failwith s
  in
  Run.create ~tool ~invocations ~results ~taxonomies ~artifacts
    ~originalUriBaseIds ()
208
209

let generate () =
210
211
  let remarks = get_remarks () in
  let runs = [ gen_run remarks ] in
212
213
214
215
216
217
218
219
220
221
222
  let json = Schema.create ~runs () |> Schema.to_yojson in
  let file = Mdr_params.Output.get () in
  if file = "" then
    Log.print_on_output (fun fmt -> Yojson.Safe.pretty_print fmt json)
  else
    try
      Command.write_file file
        (fun out -> Yojson.Safe.pretty_to_channel ~std:true out json) ;
      Mdr_params.result "Report %s generated" file
    with Sys_error s ->
      Mdr_params.abort "Unable to generate %s (%s)" file s