Skip to content

Use generic Spec lifters for delayed widening #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 99 additions & 7 deletions src/lifters/specLifters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ module type LatticeLifter =
val unlift: t -> L.t
end

module DomainLifter (N: NameLifter) (F: LatticeLifter) (S:Spec)
: Spec with module G = S.G
and module C = S.C
module DomainLifter (N: NameLifter) (F: LatticeLifter) (S:Spec):
sig
include Spec with module G = S.G
and module C = S.C
and module D = F (S.D)
and module V = S.V
val conv: (D.t, G.t, C.t, V.t) man -> (S.D.t, G.t, C.t, V.t) man
end
=
struct
module D = F (S.D)
Expand Down Expand Up @@ -104,6 +109,89 @@ struct
D.lift @@ S.event (conv man) e (conv oman)
end

module GlobalDomainLifter (N: NameLifter) (F: LatticeLifter) (S:Spec):
sig
include Spec with module D = S.D
and module G = F (S.G)
and module C = S.C
and module V = S.V
val conv: (D.t, G.t, C.t, V.t) man -> (D.t, S.G.t, C.t, V.t) man
end
=
struct
module D = S.D
module G = F (S.G)
module C = S.C
module V = S.V
module P = S.P

let name () = N.lift_name (S.name ())

type marshal = S.marshal
let init = S.init
let finalize = S.finalize

let startstate = S.startstate
let exitstate = S.exitstate
let morphstate = S.morphstate

let conv man =
{ man with global = (fun v -> G.unlift (man.global v))
; sideg = (fun v g -> man.sideg v (G.lift g))
}

let context man fd = S.context (conv man) fd
let startcontext () = S.startcontext ()

let sync man reason =
S.sync (conv man) reason

let query man =
S.query (conv man)

let assign man lv e =
S.assign (conv man) lv e

let vdecl man v =
S.vdecl (conv man) v

let branch man e tv =
S.branch (conv man) e tv

let body man f =
S.body (conv man) f

let return man r f =
S.return (conv man) r f

let asm man =
S.asm (conv man)

let skip man =
S.skip (conv man)

let enter man r f args =
S.enter (conv man) r f args

let special man r f args =
S.special (conv man) r f args

let combine_env man r fe f args fc es f_ask =
S.combine_env (conv man) r fe f args fc es f_ask

let combine_assign man r fe f args fc es f_ask =
S.combine_assign (conv man) r fe f args fc es f_ask

let threadenter man ~multiple lval f args =
S.threadenter (conv man) ~multiple lval f args

let threadspawn man ~multiple lval f args fman =
S.threadspawn (conv man) ~multiple lval f args (conv fman)

let paths_as_set man = S.paths_as_set (conv man)
let event man e oman = S.event (conv man) e (conv oman)
end

(** Lifts a [Spec] so that the domain is [Hashcons]d *)
module HashconsLifter (S: Spec) = (* keep functor eta-expanded to look up option when lifter is actually used *)
struct
Expand Down Expand Up @@ -133,10 +221,14 @@ module type PrintableLifter =
val unlift: t -> P.t
end

module ContextLifter (N: NameLifter) (F: PrintableLifter) (S:Spec)
: Spec with module D = S.D
and module G = S.G
and module C = F (S.C)
module ContextLifter (N: NameLifter) (F: PrintableLifter) (S:Spec):
sig
include Spec with module D = S.D
and module G = S.G
and module C = F (S.C)
and module V = S.V
val conv: (D.t, G.t, C.t, V.t) man -> (D.t, G.t, S.C.t, V.t) man
end
=
struct
module D = S.D
Expand Down
129 changes: 20 additions & 109 deletions src/lifters/wideningDelay.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct
module Chain = Printable.Chain (ChainParams)
include Printable.Prod (Base) (Chain)

let lift d = (d, 0)
let unlift (d, _) = d

let bot () = (Base.bot (), 0)
let is_bot (b, _) = Base.is_bot b
let top () = (Base.top (), ChainParams.n ())
Expand Down Expand Up @@ -52,134 +55,42 @@ end
All transfer functions reset the counter to 0, so counting only happens between old and new values at a local unknown. *)
module DLifter (S: Spec): Spec =
struct
module D =
module DD (D: Lattice.S) =
struct
include Dom (S.D) (LocalChainParams)
include Dom (D) (LocalChainParams)

let printXml f (b, i) =
BatPrintf.fprintf f "%a<analysis name=\"widen-delay\">%a</analysis>" S.D.printXml b Chain.printXml i
BatPrintf.fprintf f "%a<analysis name=\"widen-delay\">%a</analysis>" D.printXml b Chain.printXml i
end
module G = S.G
module C = S.C
module V = S.V
module P =

module NameLifter =
struct
include S.P
let of_elt (x, _) = of_elt x
let lift_name x = x ^ " with widening delay"
end
include SpecLifters.DomainLifter (NameLifter) (DD) (S)

let name () = S.name () ^ " with widening delay"

type marshal = S.marshal
let init = S.init
let finalize = S.finalize
(* Redefine morphstate and paths_as_set to keep counter instead of resetting to 0. *)

let startstate v = (S.startstate v, 0)
let exitstate v = (S.exitstate v, 0)
let morphstate v (d, l) = (S.morphstate v d, l)

let conv (man: (D.t, G.t, C.t, V.t) man): (S.D.t, S.G.t, S.C.t, S.V.t) man =
{ man with local = fst man.local
; split = (fun d es -> man.split (d, 0) es)
}

let context man fd (d, _) = S.context (conv man) fd d
let startcontext () = S.startcontext ()

let lift_fun man f g h =
f @@ h (g (conv man))

let lift d = (d, 0)

let sync man reason = lift_fun man lift S.sync ((|>) reason)
let query man (type a) (q: a Queries.t): a Queries.result = S.query (conv man) q
let assign man lv e = lift_fun man lift S.assign ((|>) e % (|>) lv)
let vdecl man v = lift_fun man lift S.vdecl ((|>) v)
let branch man e tv = lift_fun man lift S.branch ((|>) tv % (|>) e)
let body man f = lift_fun man lift S.body ((|>) f)
let return man r f = lift_fun man lift S.return ((|>) f % (|>) r)
let asm man = lift_fun man lift S.asm identity
let skip man = lift_fun man lift S.skip identity
let special man r f args = lift_fun man lift S.special ((|>) args % (|>) f % (|>) r)

let enter man r f args =
let liftmap = List.map (Tuple2.mapn lift) in
lift_fun man liftmap S.enter ((|>) args % (|>) f % (|>) r)
let combine_env man r fe f args fc es f_ask = lift_fun man lift S.combine_env (fun p -> p r fe f args fc (fst es) f_ask)
let combine_assign man r fe f args fc es f_ask = lift_fun man lift S.combine_assign (fun p -> p r fe f args fc (fst es) f_ask)

let threadenter man ~multiple lval f args = lift_fun man (List.map lift) (S.threadenter ~multiple) ((|>) args % (|>) f % (|>) lval)
let threadspawn man ~multiple lval f args fman = lift_fun man lift (S.threadspawn ~multiple) ((|>) (conv fman) % (|>) args % (|>) f % (|>) lval)

let paths_as_set man =
let liftmap = List.map (fun x -> (x, snd man.local)) in
lift_fun man liftmap S.paths_as_set Fun.id

let event man e oman =
lift_fun man lift S.event ((|>) (conv oman) % (|>) e)
List.map (fun x -> (x, snd man.local)) @@ S.paths_as_set (conv man)
end

(** Lift {!S} to use widening delay for global unknowns. *)
module GLifter (S: Spec): Spec =
struct
module D = S.D
module G =
module GG (G: Lattice.S) =
struct
include Dom (S.G) (GlobalChainParams)
include Dom (G) (GlobalChainParams)

let printXml f (b, i) =
BatPrintf.fprintf f "%a<analysis name=\"widen-delay\">%a</analysis>" S.G.printXml b Chain.printXml i
BatPrintf.fprintf f "%a<analysis name=\"widen-delay\">%a</analysis>" G.printXml b Chain.printXml i
end
module C = S.C
module V = S.V
module P = S.P

let name () = S.name () ^ " with widening delay"

type marshal = S.marshal
let init = S.init
let finalize = S.finalize

let startstate v = S.startstate v
let exitstate v = S.exitstate v
let morphstate v d = S.morphstate v d

let conv (man: (D.t, G.t, C.t, V.t) man): (S.D.t, S.G.t, S.C.t, S.V.t) man =
{ man with global = (fun v -> fst (man.global v))
; sideg = (fun v g -> man.sideg v (g, 0))
}

let context man fd d = S.context (conv man) fd d
let startcontext () = S.startcontext ()

let lift_fun man f g h =
f @@ h (g (conv man))

let lift d = d

let sync man reason = lift_fun man lift S.sync ((|>) reason)
let query man (type a) (q: a Queries.t): a Queries.result = S.query (conv man) q
let assign man lv e = lift_fun man lift S.assign ((|>) e % (|>) lv)
let vdecl man v = lift_fun man lift S.vdecl ((|>) v)
let branch man e tv = lift_fun man lift S.branch ((|>) tv % (|>) e)
let body man f = lift_fun man lift S.body ((|>) f)
let return man r f = lift_fun man lift S.return ((|>) f % (|>) r)
let asm man = lift_fun man lift S.asm identity
let skip man = lift_fun man lift S.skip identity
let special man r f args = lift_fun man lift S.special ((|>) args % (|>) f % (|>) r)

let enter man r f args =
let liftmap = List.map (Tuple2.mapn lift) in
lift_fun man liftmap S.enter ((|>) args % (|>) f % (|>) r)
let combine_env man r fe f args fc es f_ask = lift_fun man lift S.combine_env (fun p -> p r fe f args fc es f_ask)
let combine_assign man r fe f args fc es f_ask = lift_fun man lift S.combine_assign (fun p -> p r fe f args fc es f_ask)

let threadenter man ~multiple lval f args = lift_fun man (List.map lift) (S.threadenter ~multiple) ((|>) args % (|>) f % (|>) lval)
let threadspawn man ~multiple lval f args fman = lift_fun man lift (S.threadspawn ~multiple) ((|>) (conv fman) % (|>) args % (|>) f % (|>) lval)

let paths_as_set man =
lift_fun man Fun.id S.paths_as_set Fun.id

let event man e oman =
lift_fun man lift S.event ((|>) (conv oman) % (|>) e)
module NameLifter =
struct
let lift_name x = x ^ " with widening delay"
end
include SpecLifters.GlobalDomainLifter (NameLifter) (GG) (S)
end
Loading