Skip to content
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

Add CIL's ptranal for EvalFunvar queries #1063

Merged
merged 5 commits into from
Jan 23, 2024
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
31 changes: 31 additions & 0 deletions src/analyses/ptranalAnalysis.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(** CIL's {!GoblintCil.Ptranal} for function pointer evaluation ([ptranal]).

Useful for sound analysis of function pointers without base. *)

(* TODO: fix unsoundness on some bench repo examples: https://github.com/goblint/analyzer/pull/1063 *)

open GoblintCil
open Analyses

module Spec =
struct
include UnitAnalysis.Spec

let name () = "ptranal"

let query ctx (type a) (q: a Queries.t): a Queries.result =
match q with
| Queries.EvalFunvar (Lval (Mem e, _)) ->
let funs = Ptranal.resolve_exp e in
(* TODO: filter compatible function pointers by type? *)
List.fold_left (fun xs f -> Queries.AD.add (Queries.AD.Addr.of_var f) xs) (Queries.AD.empty ()) funs
| _ -> Queries.Result.top q

let init _: unit =
Ptranal.analyze_file !Cilfacade.current_file;
Ptranal.compute_results false

end

let _ =
MCP.register_analysis (module Spec : MCPSpec)
1 change: 1 addition & 0 deletions src/goblint_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ module TaintPartialContexts = TaintPartialContexts
module UnassumeAnalysis = UnassumeAnalysis
module ExpRelation = ExpRelation
module AbortUnless = AbortUnless
module PtranalAnalysis = PtranalAnalysis


(** {1 Domains}
Expand Down
14 changes: 14 additions & 0 deletions tests/regression/33-constants/05-fun_ptranal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//PARAM: --set ana.activated '["constants", "ptranal"]'
// intentional explicit ana.activated to do tutorial in isolation
int f(int a, int b){
int d = 3;
int z = a + d;
return z;
}

int main(){
int d = 0;
int (*fp)(int,int) = &f;
d = fp(2, 3);
return 0;
}