package frama-c

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file generic.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

open Cil_types
open Options
module List = Extends.List
module Typ = Extends.Typ
module Build = Cil_builder.Pure


(* Types of variadic parameter and argument *)

let vpar_typ attr =
  TPtr (TPtr (TVoid [], [Attr ("const", [])]), attr)
let vpar_name = "__va_params"
let vpar =
  (vpar_name, vpar_typ [], [])


(* Translation of variadic types (not deeply) *)

let translate_type = function
  | TFun (ret_typ, args, is_variadic, attributes) ->
    let new_args =
      if is_variadic
      then
        let ng_args, g_args = Cil.argsToPairOfLists args in
        Some (ng_args @ [vpar] @ g_args)
      else args
    in
    TFun (ret_typ, new_args, false, attributes)

  | TBuiltin_va_list attr -> vpar_typ attr

  | typ -> typ


(* Adding the vpar parameter to variadic functions *)

let add_vpar vi =
  let formals = Cil.getFormalsDecl vi in
  let n_formals, g_formals = List.partition (fun v -> not v.vghost) formals in
  (* Add the vpar formal once *)
  if not (List.exists (fun vi -> vi.vname = vpar_name) formals) then
    begin
      (* Register the new formal *)
      let new_formal = Cil.makeFormalsVarDecl vpar in
      let new_formals = n_formals @ [new_formal] @ g_formals in
      Cil.unsafeSetFormalsDecl vi new_formals
    end


(* Translation of va_* builtins  *)

let translate_va_builtin caller inst =
  let vi, args, loc = match inst with
    | Call(_, {enode = Lval(Var vi, _)}, args, loc) ->
      vi, args, loc
    | _ -> assert false
  in

  let translate_va_start () =
    let va_list = match args with
      | [{enode=Lval va_list}] -> va_list
      | _ -> Self.abort "Unexpected arguments to va_start"
    and varg =
      try Extlib.last (Cil.getFormalsDecl caller.svar)
      with Invalid_argument _ ->
        Self.abort "Using va_start macro in a function which is not variadic."
    in
    [ Set (va_list, Cil.evar ~loc varg, loc) ]
  in

  let translate_va_copy () =
    let dest, src = match args with
      | [{enode=Lval dest}; src] -> dest, src
      | _ -> Self.abort "Unexpected arguments to va_copy"
    in
    [ Set (dest, src, loc) ]
  in

  let translate_va_arg () =
    let va_list, ty, lv = match args with
      | [{enode=Lval va_list};
         {enode=SizeOf ty};
         {enode=CastE(_, {enode=AddrOf lv})}] -> va_list, ty, lv
      | _ -> Self.abort "Unexpected arguments to va_arg"
    in
    (* Check validity of type *)
    if Cil.isIntegralType ty then begin
      let promoted_type = Cil.integralPromotion ty in
      if promoted_type <> ty then
        Self.warning ~current:true ~wkey:wkey_typing
          "Wrong type argument in va_start: %a is promoted to %a when used \
           in the variadic part of the arguments. (You should pass %a to \
           va_start)"
          Printer.pp_typ ty
          Printer.pp_typ promoted_type
          Printer.pp_typ promoted_type
    end;
    (* Build the replacing instruction *)
    let va_list, ty, lv = Build.(of_lval va_list, of_ctyp ty, of_lval lv) in
    List.map (Build.cil_instr ~loc) Build.([
        lv := mem (cast (ptr ty) (mem va_list));
        va_list += of_int 1
      ])
  in

  begin match vi.vname with
    | "__builtin_va_start" -> translate_va_start ()
    | "__builtin_va_copy" -> translate_va_copy ()
    | "__builtin_va_arg" -> translate_va_arg ()
    | "__builtin_va_end" -> [] (* No need to do anything for va_end *)
    | _ -> assert false
  end


(* Translation of calls to variadic functions *)

let translate_call ~builder callee pars =
  let module Build = (val builder : Builder.S) in
  Build.start_translation ();

  (* Log translation *)
  Self.result ~current:true ~level:2
    "Generic translation of call to variadic function.";

  (* Split params into static, variadic and ghost part *)
  let ng_params, g_params = Typ.ghost_partitioned_params (Cil.typeOf callee) in
  let static_size = List.length ng_params - 1 in
  let s_exps, r_exps = List.break static_size pars in
  let variadic_size = (List.length r_exps) - (List.length g_params) in
  let v_exps, g_exps = List.break variadic_size r_exps in

  (* Create temporary variables to hold parameters *)
  let add_var i e =
    let name = "__va_arg" ^ string_of_int i in
    Build.(local' (Cil.typeOf e) name ~init:(of_exp e))
  in
  let vis = List.mapi add_var v_exps in

  (* Build an array to store addresses *)
  let init = match vis with (* C standard forbids arrays of size 0 *)
    | [] -> [Build.(of_init (Cil.makeZeroInit ~loc Cil.voidPtrType))]
    | l -> List.map Build.addr l
  in
  let ty = Build.(array (ptr void) ~size:(List.length init)) in
  let vargs = Build.(local ty "__va_args" ~init) in

  (* Translate the call *)
  let new_arg = Build.(cast' (vpar_typ []) (addr vargs)) in
  let new_args = Build.(of_exp_list s_exps @ [new_arg] @ of_exp_list g_exps) in
  Build.(translated_call (of_exp callee) new_args)
OCaml

Innovation. Community. Security.