0

I am writing a Frama-C-plugin. In this plugin, I want to add annotations to functions (e.g. if function name = "test", add a requires clause, saying parameter == 1).

I found the function Annotations.add_requires, but I don't know some of the parameters (Emitter.t, Identified_predicates). Is the string-parameter the function name or an own name for the predicate?

How do I use this function? Can anyone show an example?

Thomas Böhm
  • 1,456
  • 1
  • 15
  • 27
  • 1
    Did you try something? Did you manage to find the "test" function (`kernel_function`) and the variable (`varinfo`) of its "parameter" ? Now, before using `add_requires`, you have to build the predicate `parameter == 1`, right? Did you find out how to do that? – Anne Mar 30 '16 at 15:20
  • I got the kernel_function and the varinfo. I set the third parameter (I think it is the name) to "test". Now there are 2 more parameters (the first and the last) which I do not know how to build them. Emitter has a create-function, but it gets several lists, where I do not know what they do (I now tried `Emitter.create "name" [Code_annot] [] []`. Furthermore, I do not know how to build the predicate-list, since it contains several other strange things (so i also cannot test the other two attempts with the name and the emitter). – Thomas Böhm Mar 31 '16 at 06:05
  • I now tried this : `let pred = Cil_types.Ptrue in let id_pred = {ip_name = [];ip_loc= location; ip_id= 1; ip_content= pred}`, but I don't know what location should be used? – Thomas Böhm Mar 31 '16 at 06:52

1 Answers1

1

The emitter identify your plugin and has to declare what it is suppose to modify. In your case, as you want to add properties to the specifications, you can build it with:

let emitter = Emitter.create "My plugin" [ Emitter.Funspec ]
    ~correctness:[] ~tuning:[]

Now, for a kernel_function this is an example of how to build a precondition saying that the first parameter is equal to one:

let add_pre kf = match Kernel_function.get_formals kf with
  | [] -> ()
  | c_var::_ ->
    let l_var = Cil.cvar_to_lvar c_var in
    let var_term = Logic_const.tvar l_var in
    let cnst_term = Logic_const.tinteger 1 in
    let eq_pred = Logic_const.prel (Cil_types.Req, var_term, cnst_term) in
    let pred = Logic_const.new_predicate eq_pred in
    let bname = Cil.default_behavior_name in
    Annotations.add_requires emitter kf ~behavior:bname [pred] 
Thomas Böhm
  • 1,456
  • 1
  • 15
  • 27
Anne
  • 1,270
  • 6
  • 15
  • I'm using your code snippet (which seems to make sense). Following problem: When I write //@requires config == 1; in the C-Code and make the call of add_pre to a comment, the value-analysis shows `config_0 -> {{ NULL -> {1} }}`, but when removing the hand-written requires and calling your function, it ends up as `config_0 -> {{ NULL -> [--..--] }}` (like it didn't do anything) In both cases, I call `!Db.Value.compute ()` after the call to your function (which is commented in the successful case) Shouldn't it react the same way? – Thomas Böhm Mar 31 '16 at 08:43
  • Maybe try to use the `-print` option to see if the property is correctly added. – Anne Mar 31 '16 at 09:16
  • And also try `frama-c file.c -your-plugin-option -then -val` (this one seems to work here). – Anne Mar 31 '16 at 09:21
  • Maybe `Value` has been already computed before the call to `add_pre`, and in this case, your call to `!Db.Value.compute ()` may do nothing. – Anne Mar 31 '16 at 09:31
  • Thanks, your input solved my problem. Had a small mistake in a part of my code :) One question: Where did you learn how to use the framework? Are there any good tutorials, or is there example-code available? The Developer guide only shows small parts, and without help on Stackoverflow, it would take centuries to complete this code – Thomas Böhm Mar 31 '16 at 10:05
  • I have been one of the developers: it helps ;-) But a good tip to find information is to use `grep` in the source code as soon as you have a starting point. – Anne Mar 31 '16 at 10:47
  • Do you also have an idea for my next question: http://stackoverflow.com/questions/36326495/frama-c-plugin-set-value-of-variable-in-plugin – Thomas Böhm Apr 04 '16 at 10:17