gpt4 book ai didi

ocaml - Frama-C 插件开发 : Getting result of value-analysis

转载 作者:行者123 更新时间:2023-12-04 20:15:56 26 4
gpt4 key购买 nike

我正在使用值(value)分析为 Frama-C 开发插件。我只是想在每个语句之后打印变量(值)的状态(我认为解决方案很简单,但我无法弄清楚)。

我在访问者的 vstmt_aux 方法中使用 Db.Value.get_stmt_state 获得了当前状态。

我现在如何获取变量的值?

PS:我找到了这篇文章,但没有帮助,没有真正的解决方案,并且在描述的帮助下我无法做到: How to use functions in Value.Eval_expr, Value.Eval_op etc modules of Frama-c Value plugin

最佳答案

这是一个具体示例,说明如何为每个局部和全局变量打印在给定函数中的每个语句之前由 Value 计算的结果(从下到上读取函数):

open Cil_types

(* Prints the value associated to variable [vi] before [stmt]. *)
let pretty_vi fmt stmt vi =
let kinstr = Kstmt stmt in (* make a kinstr from a stmt *)
let lval = (Var vi, NoOffset) in (* make an lval from a varinfo *)
let loc = (* make a location from a kinstr + an lval *)
!Db.Value.lval_to_loc kinstr ~with_alarms:CilE.warn_none_mode lval
in
Db.Value.fold_state_callstack
(fun state () ->
(* for each state in the callstack *)
let value = Db.Value.find state loc in (* obtain value for location *)
Format.fprintf fmt "%a -> %a@." Printer.pp_varinfo vi
Locations.Location_Bytes.pretty value (* print mapping *)
) () ~after:false kinstr

(* Prints the state at statement [stmt] for each local variable in [kf],
and for each global variable. *)
let pretty_local_and_global_vars kf fmt stmt =
let locals = Kernel_function.get_locals kf in
List.iter (fun vi -> pretty_vi fmt stmt vi) locals;
Globals.Vars.iter (fun vi _ -> pretty_vi fmt stmt vi)

(* Visits each statement in [kf] and prints the result of Value before the
statement. *)
class stmt_val_visitor kf =
object (self)
inherit Visitor.frama_c_inplace
method! vstmt_aux stmt =
(match stmt.skind with
| Instr _ ->
Format.printf "state for all variables before stmt: %a@.%a@."
Printer.pp_stmt stmt (pretty_local_and_global_vars kf) stmt
| _ -> ());
Cil.DoChildren
end

(* usage: frama-c file.c -load-script print_vals.ml *)
let () =
Db.Main.extend (fun () ->
Format.printf "computing value...@.";
!Db.Value.compute ();
let fun_name = "main" in
Format.printf "visiting function: %s@." fun_name;
let kf_vis = new stmt_val_visitor in
let kf = Globals.Functions.find_by_name fun_name in
let fundec = Kernel_function.get_definition kf in
ignore (Visitor.visitFramacFunction (kf_vis kf) fundec);
Format.printf "done!@.")

这远非理想,而且输出比简单地使用 Cvalue.Model.pretty state 更丑陋,但它可以作为进一步修改的基础。

这个脚本已经用 Frama-C Magnesium 测试过。

要在语句之后检索状态,只需将 fold_state_callstack 中的 ~after:false 参数替换为 ~after:true 。我以前版本的代码使用了一个已经为 pre-state 绑定(bind)该值的函数,但是没有为 post-state 导出这样的函数,所以我们必须使用 fold_state_callstack (顺便说一下更强大,因为它允许检索每个调用堆栈的特定状态)。

关于ocaml - Frama-C 插件开发 : Getting result of value-analysis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36132777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com