You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
254 lines
7.9 KiB
254 lines
7.9 KiB
9 years ago
|
(*
|
||
|
* Copyright (c) 2016 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*)
|
||
|
|
||
8 years ago
|
open! IStd
|
||
9 years ago
|
|
||
9 years ago
|
module F = Format
|
||
|
|
||
9 years ago
|
(** Control-flow graph for a single procedure (as opposed to cfg.ml, which represents a cfg for a
|
||
9 years ago
|
file). Defines useful wrappers that allows us to do tricks like turn a forward cfg into a
|
||
|
backward one, or view a cfg as having a single instruction per node. *)
|
||
9 years ago
|
|
||
8 years ago
|
type index = Node_index | Instr_index of int [@@deriving compare]
|
||
9 years ago
|
|
||
9 years ago
|
module type Node = sig
|
||
|
type t
|
||
|
type id
|
||
|
|
||
8 years ago
|
val kind : t -> Procdesc.Node.nodekind
|
||
9 years ago
|
val id : t -> id
|
||
8 years ago
|
val hash : t -> int
|
||
8 years ago
|
val loc : t -> Location.t
|
||
8 years ago
|
val underlying_node : t -> Procdesc.Node.t
|
||
8 years ago
|
val compare_id : id -> id -> int
|
||
9 years ago
|
val pp_id : F.formatter -> id -> unit
|
||
|
end
|
||
|
|
||
|
module DefaultNode = struct
|
||
8 years ago
|
type t = Procdesc.Node.t
|
||
|
type id = Procdesc.Node.id
|
||
9 years ago
|
|
||
8 years ago
|
let kind = Procdesc.Node.get_kind
|
||
|
let id = Procdesc.Node.get_id
|
||
8 years ago
|
let hash = Procdesc.Node.hash
|
||
8 years ago
|
let loc = Procdesc.Node.get_loc
|
||
8 years ago
|
let underlying_node t = t
|
||
8 years ago
|
let compare_id = Procdesc.Node.compare_id
|
||
8 years ago
|
let pp_id = Procdesc.Node.pp_id
|
||
9 years ago
|
end
|
||
|
|
||
9 years ago
|
module InstrNode = struct
|
||
8 years ago
|
type t = Procdesc.Node.t
|
||
|
type id = Procdesc.Node.id * index
|
||
9 years ago
|
|
||
8 years ago
|
let kind = Procdesc.Node.get_kind
|
||
9 years ago
|
|
||
8 years ago
|
let underlying_node t = t
|
||
9 years ago
|
|
||
8 years ago
|
let id t = Procdesc.Node.get_id (underlying_node t), Node_index
|
||
9 years ago
|
|
||
8 years ago
|
let hash node = Hashtbl.hash (id node)
|
||
|
|
||
8 years ago
|
let loc t = Procdesc.Node.get_loc t
|
||
8 years ago
|
|
||
8 years ago
|
let compare_index = compare_index
|
||
9 years ago
|
|
||
8 years ago
|
let compare_id (id1, index1) (id2, index2) =
|
||
8 years ago
|
let n = Procdesc.Node.compare_id id1 id2 in
|
||
9 years ago
|
if n <> 0 then n
|
||
8 years ago
|
else compare_index index1 index2
|
||
9 years ago
|
|
||
9 years ago
|
let pp_id fmt (id, index) = match index with
|
||
8 years ago
|
| Node_index -> Procdesc.Node.pp_id fmt id
|
||
|
| Instr_index i -> F.fprintf fmt "(%a: %d)" Procdesc.Node.pp_id id i
|
||
9 years ago
|
end
|
||
|
|
||
9 years ago
|
module type S = sig
|
||
9 years ago
|
type t
|
||
|
type node
|
||
9 years ago
|
include (Node with type t := node)
|
||
9 years ago
|
|
||
9 years ago
|
(** get the instructions from a node *)
|
||
|
val instrs : node -> Sil.instr list
|
||
8 years ago
|
|
||
9 years ago
|
(** explode a block into its instructions and an optional id for the instruction. the purpose of
|
||
|
this is to specify a policy for fine-grained storage of invariants by the abstract
|
||
|
interpreter. the interpreter will forget invariants at program points where the id is None,
|
||
|
and remember them otherwise *)
|
||
|
val instr_ids : node -> (Sil.instr * id option) list
|
||
8 years ago
|
|
||
9 years ago
|
val succs : t -> node -> node list
|
||
8 years ago
|
|
||
9 years ago
|
(** all predecessors (normal and exceptional) *)
|
||
9 years ago
|
val preds : t -> node -> node list
|
||
8 years ago
|
|
||
9 years ago
|
(** non-exceptional successors *)
|
||
|
val normal_succs : t -> node -> node list
|
||
8 years ago
|
|
||
9 years ago
|
(** non-exceptional predecessors *)
|
||
|
val normal_preds : t -> node -> node list
|
||
8 years ago
|
|
||
9 years ago
|
(** exceptional successors *)
|
||
|
val exceptional_succs : t -> node -> node list
|
||
8 years ago
|
|
||
9 years ago
|
(** exceptional predescessors *)
|
||
|
val exceptional_preds : t -> node -> node list
|
||
8 years ago
|
|
||
9 years ago
|
val start_node : t -> node
|
||
8 years ago
|
|
||
9 years ago
|
val exit_node : t -> node
|
||
8 years ago
|
|
||
8 years ago
|
val proc_desc : t -> Procdesc.t
|
||
8 years ago
|
|
||
9 years ago
|
val nodes : t -> node list
|
||
8 years ago
|
|
||
8 years ago
|
val from_pdesc : Procdesc.t -> t
|
||
8 years ago
|
|
||
|
val is_loop_head : Procdesc.t -> node -> bool
|
||
9 years ago
|
end
|
||
|
|
||
9 years ago
|
(** Forward CFG with no exceptional control-flow *)
|
||
|
module Normal = struct
|
||
8 years ago
|
type t = Procdesc.t
|
||
9 years ago
|
type node = DefaultNode.t
|
||
|
include (DefaultNode : module type of DefaultNode with type t := node)
|
||
9 years ago
|
|
||
8 years ago
|
let instrs = Procdesc.Node.get_instrs
|
||
8 years ago
|
let instr_ids n = List.map ~f:(fun i -> i, None) (instrs n)
|
||
8 years ago
|
let normal_succs _ n = Procdesc.Node.get_succs n
|
||
|
let normal_preds _ n = Procdesc.Node.get_preds n
|
||
9 years ago
|
(* prune away exceptional control flow *)
|
||
|
let exceptional_succs _ _ = []
|
||
|
let exceptional_preds _ _ = []
|
||
|
let succs = normal_succs
|
||
|
let preds = normal_preds
|
||
8 years ago
|
let start_node = Procdesc.get_start_node
|
||
|
let exit_node = Procdesc.get_exit_node
|
||
9 years ago
|
let proc_desc t = t
|
||
8 years ago
|
let nodes = Procdesc.get_nodes
|
||
9 years ago
|
let from_pdesc pdesc = pdesc
|
||
8 years ago
|
let is_loop_head = Procdesc.is_loop_head
|
||
9 years ago
|
end
|
||
9 years ago
|
|
||
9 years ago
|
(** Forward CFG with exceptional control-flow *)
|
||
9 years ago
|
module Exceptional = struct
|
||
9 years ago
|
type node = DefaultNode.t
|
||
8 years ago
|
type id_node_map = node list Procdesc.IdMap.t
|
||
|
type t = Procdesc.t * id_node_map
|
||
9 years ago
|
include (DefaultNode : module type of DefaultNode with type t := node)
|
||
9 years ago
|
|
||
8 years ago
|
let exceptional_succs _ n = match Procdesc.Node.get_kind n with
|
||
|
| Procdesc.Node.Stmt_node ("call_noexcept") ->
|
||
|
(* Hack: signal from the frontend that this node should be modelled as non-throwing.
|
||
|
Eventually, we'll just avoid translating the exceptional edge in the frontend instead. *)
|
||
|
[]
|
||
|
| _ ->
|
||
|
Procdesc.Node.get_exn n
|
||
|
|
||
9 years ago
|
let from_pdesc pdesc =
|
||
|
(* map from a node to its exceptional predecessors *)
|
||
|
let add_exn_preds exn_preds_acc n =
|
||
|
let add_exn_pred exn_preds_acc exn_succ_node =
|
||
8 years ago
|
let exn_succ_node_id = Procdesc.Node.get_id exn_succ_node in
|
||
9 years ago
|
let existing_exn_preds =
|
||
8 years ago
|
try Procdesc.IdMap.find exn_succ_node_id exn_preds_acc
|
||
9 years ago
|
with Not_found -> [] in
|
||
8 years ago
|
if not (List.mem ~equal:Procdesc.Node.equal existing_exn_preds n)
|
||
9 years ago
|
then (* don't add duplicates *)
|
||
8 years ago
|
Procdesc.IdMap.add exn_succ_node_id (n :: existing_exn_preds) exn_preds_acc
|
||
9 years ago
|
else
|
||
|
exn_preds_acc in
|
||
8 years ago
|
List.fold ~f:add_exn_pred ~init:exn_preds_acc (exceptional_succs pdesc n) in
|
||
9 years ago
|
let exceptional_preds =
|
||
8 years ago
|
List.fold ~f:add_exn_preds ~init:Procdesc.IdMap.empty (Procdesc.get_nodes pdesc) in
|
||
9 years ago
|
pdesc, exceptional_preds
|
||
|
|
||
8 years ago
|
let instrs = Procdesc.Node.get_instrs
|
||
9 years ago
|
|
||
8 years ago
|
let instr_ids n = List.map ~f:(fun i -> i, None) (instrs n)
|
||
9 years ago
|
|
||
8 years ago
|
let nodes (t, _) = Procdesc.get_nodes t
|
||
9 years ago
|
|
||
8 years ago
|
let normal_succs _ n = Procdesc.Node.get_succs n
|
||
9 years ago
|
|
||
8 years ago
|
let normal_preds _ n = Procdesc.Node.get_preds n
|
||
9 years ago
|
|
||
|
let exceptional_preds (_, exn_pred_map) n =
|
||
8 years ago
|
try Procdesc.IdMap.find (Procdesc.Node.get_id n) exn_pred_map
|
||
9 years ago
|
with Not_found -> []
|
||
|
|
||
|
(** get all normal and exceptional successors of [n]. *)
|
||
|
let succs t n =
|
||
|
let normal_succs = normal_succs t n in
|
||
|
match exceptional_succs t n with
|
||
|
| [] ->
|
||
|
normal_succs
|
||
|
| exceptional_succs ->
|
||
|
normal_succs @ exceptional_succs
|
||
8 years ago
|
|> List.sort ~cmp:Procdesc.Node.compare
|
||
|
|> List.remove_consecutive_duplicates ~equal:Procdesc.Node.equal
|
||
9 years ago
|
|
||
|
(** get all normal and exceptional predecessors of [n]. *)
|
||
|
let preds t n =
|
||
|
let normal_preds = normal_preds t n in
|
||
|
match exceptional_preds t n with
|
||
|
| [] ->
|
||
|
normal_preds
|
||
|
| exceptional_preds ->
|
||
|
normal_preds @ exceptional_preds
|
||
8 years ago
|
|> List.sort ~cmp:Procdesc.Node.compare
|
||
|
|> List.remove_consecutive_duplicates ~equal:Procdesc.Node.equal
|
||
9 years ago
|
|
||
|
let proc_desc (pdesc, _) = pdesc
|
||
8 years ago
|
let start_node (pdesc, _) = Procdesc.get_start_node pdesc
|
||
|
let exit_node (pdesc, _) = Procdesc.get_exit_node pdesc
|
||
8 years ago
|
let is_loop_head = Procdesc.is_loop_head
|
||
9 years ago
|
end
|
||
|
|
||
9 years ago
|
(** Wrapper that reverses the direction of the CFG *)
|
||
|
module Backward (Base : S) = struct
|
||
|
include Base
|
||
8 years ago
|
let instrs n = List.rev (Base.instrs n)
|
||
|
let instr_ids n = List.rev (Base.instr_ids n)
|
||
9 years ago
|
|
||
|
let succs = Base.preds
|
||
|
let preds = Base.succs
|
||
|
let start_node = Base.exit_node
|
||
|
let exit_node = Base.start_node
|
||
|
let normal_succs = Base.normal_preds
|
||
|
let normal_preds = Base.normal_succs
|
||
|
let exceptional_succs = Base.exceptional_preds
|
||
|
let exceptional_preds = Base.exceptional_succs
|
||
9 years ago
|
end
|
||
9 years ago
|
|
||
8 years ago
|
module OneInstrPerNode (Base : S with type node = Procdesc.Node.t
|
||
|
and type id = Procdesc.Node.id) = struct
|
||
|
include (Base : module type of Base with type id := Procdesc.Node.id and type t = Base.t)
|
||
9 years ago
|
type id = Base.id * index
|
||
|
include (InstrNode : module type of InstrNode with type t := node and type id := id)
|
||
|
|
||
|
(* keep the invariants before/after each instruction *)
|
||
|
let instr_ids t =
|
||
8 years ago
|
List.mapi
|
||
|
~f:(fun i instr ->
|
||
8 years ago
|
let id = Procdesc.Node.get_id t, Instr_index i in
|
||
9 years ago
|
instr, Some id)
|
||
|
(instrs t)
|
||
9 years ago
|
end
|
||
|
|
||
8 years ago
|
module NodeIdMap (CFG : S) = Caml.Map.Make(struct
|
||
9 years ago
|
type t = CFG.id
|
||
8 years ago
|
let compare = CFG.compare_id
|
||
9 years ago
|
end)
|
||
|
|
||
8 years ago
|
module NodeIdSet (CFG : S) = Caml.Set.Make(struct
|
||
9 years ago
|
type t = CFG.id
|
||
8 years ago
|
let compare = CFG.compare_id
|
||
9 years ago
|
end)
|