sarif_gen.ml 6.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
(*  Copyright (C) 2007-2019                                               *)
(*    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
27
28
29
30
open Sarif

let frama_c_sarif =
  let name = "frama-c" in
  let version = Config.version_and_codename in
  let semanticVersion = Config.version in
  let fullName = name ^ "-" ^ version in
  let downloadUri = "https://frama-c.com/download.html" in
31
  let sarifLoggerVersion = Mdr_version.version in
32
33
34
35
  Tool.create
    ~name ~version ~semanticVersion
    ~fullName ~downloadUri ~sarifLoggerVersion ()

36

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

42
43
44
45
46
let get_remark remarks label =
  match Datatype.String.Map.find_opt label remarks with
  | None -> []
  | Some l -> l

47
48
49
50
51
52
53
54
55
56
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)

57
let gen_invocation () =
58
59
60
  let cl = Analysis_cmdline.get () in
  let commandLine = String.concat " " cl in
  let arguments = List.tl cl in
61
62
  Invocation.create ~commandLine ~arguments ()

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

75
76
77
78
79
80
81
82
83
84
let level_of_status =
  let open Property_status.Feedback in
  let open Sarif.Result_level in
  function
  | Never_tried -> notApplicable
  | Considered_valid | Valid | Valid_under_hyp | Valid_but_dead -> pass
  | Unknown | Unknown_but_dead -> warning
  | Invalid | Invalid_under_hyp | Invalid_but_dead -> error
  | Inconsistent -> note

85
86
let make_message alarm annot remark =
  let open Markdown in
87
88
89
  let name = Alarms.get_name alarm in
  let text = name ^ "." in
  let kind = plain (name ^ ":") in
90
91
  let descr = codelines "acsl" Printer.pp_code_annotation annot in
  let summary = Block [Text kind; descr] in
92
93
  let markdown =
    match remark with
Virgile Prevosto's avatar
Virgile Prevosto committed
94
95
    | [] -> summary :: gen_remark alarm
    | _ -> summary :: remark
96
  in
97
98
99
  let richText =
    String.trim (Format.asprintf "@[%a@]" Markdown.pp_elements markdown)
  in
100
  Message.create ~text ~richText ()
101
102

let gen_results remarks =
103
  let treat_alarm _e kf s ~rank:_ alarm annot (i, rules, content) =
104
    let prop = Property.ip_of_code_annot_single kf s annot in
105
106
107
108
    let ruleId = Alarms.get_name alarm in
    let rules =
      Datatype.String.Map.add ruleId (Alarms.get_description alarm) rules
    in
109
    let label = "Alarm-" ^ string_of_int i in
110
    let level = level_of_status (Property_status.Feedback.get prop) in
111
112
113
114
    let remark = get_remark remarks label in
    let message = make_message alarm annot remark in
    let locations = [ Location.of_loc (Cil_datatype.Stmt.loc s) ] in
    let res =
115
      Sarif_result.create ~level ~ruleId ~message ~locations ()
116
    in
117
118
119
120
    (i+1, rules, res :: content)
  in
  let _, rules, content =
    Alarms.fold treat_alarm (0, Datatype.String.Map.empty,[])
121
  in
122
  rules, List.rev content
123

124
let is_alarm = function
125
  | Property.(IPCodeAnnot { ica_ca }) -> Extlib.has_some (Alarms.find ica_ca)
126
127
128
  | _ -> false

let make_ip_message ip =
129
  let text = Format.asprintf "@[%a.@]" Property.short_pretty ip in
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  Message.plain_text ~text ()

let gen_status ip =
  let status = Property_status.Feedback.get ip in
  let level = level_of_status status in
  let locations = [ Location.of_loc (Property.location ip) ] in
  let message = make_ip_message ip in
  Sarif_result.create ~level ~locations ~message ()

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 [])

145
146
147
148
149
150
151
152
153
154
let gen_files () =
  let add_src_file f =
    let key = Filename.chop_extension (Filename.basename f) in
    let fileLocation = FileLocation.create ~uri:(Filepath.normalize f) () in
    let roles = [ Role.analysisTarget ] in
    let mimeType = "text/x-csrc" in
    key, File.create ~fileLocation ~roles ~mimeType ()
  in
  List.map add_src_file (Kernel.Files.get ())

155
156
157
158
159
160
161
162
let add_rule id desc l =
  let text = desc ^ "." in
  let shortDescription = Message.plain_text ~text () in
  let rule = Rule.create ~id ~shortDescription () in
  (id, rule) :: l

let make_rule_dictionary rules = Datatype.String.Map.fold add_rule rules []

163
let gen_run remarks =
164
  let tool = frama_c_sarif in
165
  let invocations = [gen_invocation ()] in
Virgile Prevosto's avatar
Virgile Prevosto committed
166
167
168
169
  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
170
171
172
173
    | [] -> rules
    | _ ->
      Datatype.String.Map.add
        "user-spec" "User written ACSL specification" rules
Virgile Prevosto's avatar
Virgile Prevosto committed
174
175
  in
  let rules = make_rule_dictionary rules in
176
  let resources = Resources.create ~rules () in
Virgile Prevosto's avatar
Virgile Prevosto committed
177
  let results = results @ user_annot_results in
178
  let files = gen_files () in
179
  Run.create ~tool ~invocations ~results ~resources ~files ()
180
181

let generate () =
182
183
  let remarks = get_remarks () in
  let runs = [ gen_run remarks ] in
184
185
186
187
188
189
190
191
192
193
194
  let json = Schema.create ~runs () in
  let out = Mdr_params.Output.get () in
  let chan =
    if out = "" then stdout
    else begin
      try open_out out
      with Sys_error s ->
        Mdr_params.abort "Unable to open output file %s: %s" out s
    end
  in
  Yojson.Safe.to_channel chan (Schema.to_yojson json)