Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the array index variable and its value in value analysis (Frama-C)

I want to query the value analysis plugin in Frama-C for instructions to get their value. For each array, it returns the value range of the whole array. For example, if the instruction is array[i] = 1;, I got result = {1} for array[i] from value analysis plugin. Now, for e.g. array[i], I would like to get the variable name i and its value from value analysis.

Below is a sample of my code

class print_VA_result out = object 
inherit Visitor.frama_c_inplace
     (**..*)
 method vstmt_aux s = 
  if Db.Value.is_computed () then
     match s.skind with
    | Instr i ->
       begin
         match i with
         | Set(lval,_,_) ->     
            print_value lval s
         |_ -> "<>"   
 ...

Can anybody help me for this? Thanks a lot in advance.

like image 518
user2544482 Avatar asked Mar 24 '23 04:03

user2544482


1 Answers

I assume that you know how to write a function print_val of type Db.Value.t -> unit The following code, to be placed before your matching on a Set instruction, will catch an access to an array at index e

match i with
(* Access to an array *)
| Set ((_, Index (e, _)), _, _) ->
  let v = !Db.Value.access_expr (Kstmt s) e in
  print_val v 
(* End special case *)
| Set(lval,_,_) ->     
  print_value lval s

Alternatively, if you know the name of your variable, you can use the function Globals.Vars.find_from_astinfo to find the corresponding varinfo vi, and this code to query the contents of the varinfo.

open Cil_types    

let vi_content stmt vi =
  let lv = (Var vi, NoOffset) in
  !Db.Value.access (Kstmt stmt) lv
like image 52
byako Avatar answered Apr 26 '23 16:04

byako