package codex

  1. Overview
  2. Docs
The Codex library for building static analysers based on abstract interpretation

Install

dune-project
 Dependency

Authors

Maintainers

Sources

1.0-rc4.tar.gz
md5=bc7266a140c6886add673ede90e335d3
sha512=8da42c0ff2c1098c5f9cb2b5b43b306faf7ac93b8f5ae00c176918cee761f249ff45b29309f31a05bbcf6312304f86a0d5a000eb3f1094d3d3c2b9b4c7f5c386

doc/src/codex.operator/operator_autolog.ml.html

Source file operator_autolog.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
(**************************************************************************)
(*  This file is part of the Codex semantics library.                     *)
(*                                                                        *)
(*  Copyright (C) 2013-2025                                               *)
(*    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 LICENSE).                      *)
(*                                                                        *)
(**************************************************************************)

(* Helpers to automatically log calls to transfer functions.   *)

open Operator_sig
module In_bits = Units.In_bits

module type BOOLEAN_CONVERSION = sig
  module Arity : ARITY
  type boolean
  type 'a pp
  val bool_printer : boolean pp
  val ar0: 'r pp -> (Format.formatter -> unit) -> 'r Arity.ar0 -> 'r Arity.ar0
  val ar1: 'a pp -> 'r pp -> (Format.formatter -> unit) ->
    ('a,'r) Arity.ar1 -> ('a,'r) Arity.ar1
  val ar2: 'a pp -> 'b pp -> 'r pp -> (Format.formatter -> unit) ->
    ('a,'b,'r) Arity.ar2 -> ('a,'b,'r) Arity.ar2
end

module type INTEGER_CONVERSION = sig
  include BOOLEAN_CONVERSION
  type integer
  val integer_printer : integer pp
end

module type ENUM_CONVERSION = sig
  include BOOLEAN_CONVERSION
  type enum
  val enum_printer : enum pp
end

module type BITVECTOR_CONVERSION = sig
  include BOOLEAN_CONVERSION
  type bitvector
  val bv_printer : size:In_bits.t -> bitvector pp    
end

module type MEMORY_CONVERSION = sig
  include BOOLEAN_CONVERSION
  val ar3: 'a pp -> 'b pp -> 'c pp -> 'r pp -> (Format.formatter -> unit) ->
    ('a,'b,'c,'r) Arity.ar3 -> ('a,'b,'c,'r) Arity.ar3
  
  type block
  type memory
  type address
  type value
  val block_printer: block pp    
  val address_printer: address pp
  val memory_printer: memory pp
  val value_printer: size:In_bits.t -> value pp
  val prod_printer: 'a pp -> 'b pp -> ('a * 'b) pp
  (* type bitvector *)
  (* val bv_printer : size:int -> bitvector pp     *)
end


module Log_Boolean_Backward
      (C : BOOLEAN_CONVERSION)
      (F : BOOLEAN_BACKWARD
             with module Arity := C.Arity
              and type boolean := C.boolean) :
    BOOLEAN_BACKWARD with module Arity := C.Arity and type boolean := C.boolean =
  struct
    open C

    let ar1_bool_bool = ar1 C.bool_printer C.bool_printer
    let ar2_bool_bool_bool = ar2 C.bool_printer C.bool_printer C.bool_printer

    let (&&) =
      ar2_bool_bool_bool (fun fmt -> Format.fprintf fmt "(&&)") F.(&&)

    let (||) =
      ar2_bool_bool_bool (fun fmt -> Format.fprintf fmt "(||)") F.(||)

    let not = ar1_bool_bool (fun fmt -> Format.fprintf fmt "not") F.not
  end

  module Log_Integer_Backward
      (C : INTEGER_CONVERSION)
      (F : INTEGER_BACKWARD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type integer := C.integer) :
    INTEGER_BACKWARD
      with module Arity := C.Arity
       and type boolean := C.boolean
       and type integer := C.integer = struct
    open C

    let ar1_int_int = ar1 C.integer_printer C.integer_printer

    let ar2_int_int_int =
      ar2 C.integer_printer C.integer_printer C.integer_printer

    let ar2_int_int_bool =
      ar2 C.integer_printer C.integer_printer C.bool_printer

    let itimes z =
      ar1_int_int
        (fun fmt -> Format.fprintf fmt "itimes %s" (Z.to_string z))
        (F.itimes z)

    let iadd = ar2_int_int_int (fun fmt -> Format.fprintf fmt "iadd") F.iadd
    let imul = ar2_int_int_int (fun fmt -> Format.fprintf fmt "imul") F.imul
    let isub = ar2_int_int_int (fun fmt -> Format.fprintf fmt "isub") F.isub
    let idiv = ar2_int_int_int (fun fmt -> Format.fprintf fmt "idiv") F.idiv
    let imod = ar2_int_int_int (fun fmt -> Format.fprintf fmt "imod") F.imod
    let ishl = ar2_int_int_int (fun fmt -> Format.fprintf fmt "ishl") F.ishl
    let ishr = ar2_int_int_int (fun fmt -> Format.fprintf fmt "ishr") F.ishr
    let iand = ar2_int_int_int (fun fmt -> Format.fprintf fmt "iand") F.iand
    let ior = ar2_int_int_int (fun fmt -> Format.fprintf fmt "ior") F.ior
    let ixor = ar2_int_int_int (fun fmt -> Format.fprintf fmt "ixor") F.ixor
    let ieq = ar2_int_int_bool (fun fmt -> Format.fprintf fmt "ieq") F.ieq
    let ile = ar2_int_int_bool (fun fmt -> Format.fprintf fmt "ile") F.ile
  end


  module Log_Bitvector_Forward
      (C : BITVECTOR_CONVERSION)
      (F : BITVECTOR_FORWARD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type bitvector := C.bitvector) :
    BITVECTOR_FORWARD
      with module Arity := C.Arity
       and type boolean := C.boolean
       and type bitvector := C.bitvector = struct
    open C

    let ar2_bin_bin_bin ~size =
      ar2 (C.bv_printer ~size) (C.bv_printer ~size) (C.bv_printer ~size)

    let ar2_bin_bin_bool ~size =
      ar2 (C.bv_printer ~size) (C.bv_printer ~size) C.bool_printer

    let biconst ~size z =
      ar0 (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "biconst ~size:%d %s" (size:>int) (Z.to_string z))
        (F.biconst ~size z)

    let buext ~size ~oldsize =
      ar1 (C.bv_printer ~size:oldsize)  (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "buext ~size:%d ~oldsize:%d" (size:>int) (oldsize:>int))
        (F.buext ~size ~oldsize)

    let bsext ~size ~oldsize =
      ar1 (C.bv_printer ~size:oldsize)  (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bsext ~size:%d oldsize:%d" (size:>int) (oldsize:>int))
        (F.bsext ~size ~oldsize)    

    let bextract ~size ~index ~oldsize =
      ar1 (C.bv_printer ~size:oldsize)  (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bextract ~size:%d ~index:%d ~oldsize:%d"
            (size:>int) (In_bits.to_int index)  (oldsize:>int))
        (F.bextract ~size ~index ~oldsize)    

    let bofbool ~size =
      ar1 (C.bool_printer)  (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bofbool ~size:%d" (size:>int))
        (F.bofbool ~size)

    let biadd ~size ~flags =
      let Flags.Bisub.{nsw;nuw;nusw} = Flags.Biadd.unpack flags in
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "biadd ~size:%d ~nsw:%b ~nuw:%b ~nusw:%b" (size:>int) nsw nuw nusw)
        (F.biadd ~size ~flags)

    let bisub ~size ~flags =
      let Flags.Bisub.{nsw;nuw;nusw} = Flags.Bisub.unpack flags in
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bisub ~size:%d ~nsw:%b ~nuw:%b ~nusw:%b" (size:>int) nsw nuw nusw)
        (F.bisub ~size ~flags)
    
    let bimul ~size ~flags =
      let Flags.Bimul.{nsw;nuw} = Flags.Bimul.unpack flags in
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bimul ~size:%d ~nsw:%b ~nuw:%b" (size:>int)
            nsw nuw)
        (F.bimul ~size ~flags)

    let bshl ~size ~flags =
      let Flags.Bshl.{nsw;nuw} = Flags.Bshl.unpack flags in
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bshl ~size:%d ~nsw:%b ~nuw:%b" (size:>int) nsw nuw)
        (F.bshl ~size ~flags)

    let bisdiv ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bisdiv ~size:%d" (size:>int))
        (F.bisdiv ~size)

    let bismod ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bismod ~size:%d" (size:>int))
        (F.bismod ~size)

    let biudiv ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "biudiv ~size:%d" (size:>int))
        (F.biudiv ~size)

    let biumod ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "biumod ~size:%d" (size:>int))
        (F.biumod ~size)

    let bashr ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bashr ~size:%d" (size:>int))
        (F.bashr ~size)

    let blshr ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "blshr ~size:%d" (size:>int))
        (F.blshr ~size)

    let band ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "band ~size:%d" (size:>int))
        (F.band ~size)

    let bor ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bor ~size:%d" (size:>int))
        (F.bor ~size)

    let bxor ~size =
      ar2_bin_bin_bin ~size
        (fun fmt -> Format.fprintf fmt "bxor ~size:%d" (size:>int))
        (F.bxor ~size)

    let beq ~size =
      ar2_bin_bin_bool ~size
        (fun fmt -> Format.fprintf fmt "beq ~size:%d" (size:>int))
        (F.beq ~size)

    let bisle ~size =
      ar2_bin_bin_bool ~size
        (fun fmt -> Format.fprintf fmt "bisle ~size:%d" (size:>int))
        (F.bisle ~size)

    let biule ~size =
      ar2_bin_bin_bool ~size
        (fun fmt -> Format.fprintf fmt "biule ~size:%d" (size:>int))
        (F.biule ~size)

    let bconcat ~size1 ~size2 =
      ar2 (C.bv_printer ~size:size1) (C.bv_printer ~size:size2)
        (C.bv_printer ~size:In_bits.(size1 + size2))
        (fun fmt -> Format.fprintf fmt "bconcat ~~size1:%d ~size2:%d"
            (size1:>int) (size2:>int))
        (F.bconcat ~size1 ~size2)

  end

  module Log_Bitvector_Forward_With_Bimul_add
      (C : BITVECTOR_CONVERSION)
      (F : BITVECTOR_FORWARD_WITH_BIMUL_ADD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type bitvector := C.bitvector) :
    BITVECTOR_FORWARD_WITH_BIMUL_ADD
      with module Arity := C.Arity
       and type boolean := C.boolean
       and type bitvector := C.bitvector = struct
    include Log_Bitvector_Forward(C)(F)
    let bimul_add ~size ~prod ~offset =
      C.ar1 (C.bv_printer ~size) (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bextract ~size:%d ~prod:%s ~offset:%s" (size:>int)
            (Z.to_string prod) (Z.to_string offset))
        (F.bimul_add ~size ~prod ~offset)        
  end


  module Log_Binary_Forward
      (C : BITVECTOR_CONVERSION)
      (F : BINARY_FORWARD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type binary := C.bitvector) :
    BINARY_FORWARD
      with module Arity := C.Arity
       and type boolean := C.boolean
       and type binary := C.bitvector = struct
    include Log_Bitvector_Forward(C)(F)
    let buninit ~size =
      C.ar0 (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "buninit ~size:%d" (size:>int))
        (F.buninit ~size)
    let valid ~size access_type =
      C.ar1 (C.bv_printer ~size) (C.bool_printer)
        (fun fmt -> Format.fprintf fmt "valid ~size:%d %s" (size:>int) (match access_type with Read -> "read" | Write -> "write"))
        (F.valid ~size access_type)        
    let valid_ptr_arith ~size arith_type =
      C.ar2 (C.bv_printer ~size) (C.bv_printer ~size) (C.bool_printer)
        (fun fmt -> Format.fprintf fmt "valid_ptr_arith ~size:%d %s" (size:>int)
            (match arith_type with Plus -> "plus" | Minus -> "minus"))
        (F.valid_ptr_arith ~size arith_type)
    let bshift ~size ~offset ~max =
      C.ar1 (C.bv_printer ~size) (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bshift ~size:%d %d %s" (size:>int) offset
            (match max with None -> "None" | Some x -> string_of_int x))
        (F.bshift ~size ~offset ~max)
    let bindex ~size (scale:int) =
      C.ar2 (C.bv_printer ~size) (C.bv_printer ~size) (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bindex ~size:%d %d" (size:>int) scale)
        (F.bindex ~size scale)
    let bchoose ~size (cond:Operator_ids.Choice.t) =
      C.ar1 (C.bv_printer ~size) (C.bv_printer ~size)
        (fun fmt -> Format.fprintf fmt "bchoose ~size:%d %d" (size:>int) (cond :> int))
        (F.bchoose ~size cond)
  end




  module Log_Enum_Forward
      (C : ENUM_CONVERSION)
      (F : ENUM_FORWARD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type enum := C.enum) :
    ENUM_FORWARD
      with module Arity := C.Arity
       and type boolean := C.boolean
       and type enum := C.enum = struct
    let caseof ~case =
      C.ar1 C.enum_printer C.bool_printer
        (fun fmt -> Format.fprintf fmt "caseof ~case:%d" case)
        (F.caseof ~case)

    let enum_const ~case =
      C.ar0 C.enum_printer
        (fun fmt -> Format.fprintf fmt "enum_const ~case:%d" case)
        (F.enum_const ~case)
  end


  module Log_Memory_Forward
      (C : MEMORY_CONVERSION)
      (F : MEMORY_FORWARD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type memory := C.memory
              and type block := C.block
              and type address := C.address
              and type value := C.value) :
    MEMORY_FORWARD
             with module Arity := C.Arity
              and type boolean := C.boolean
              and type memory := C.memory
              and type block := C.block
              and type value := C.value
              and type address := C.address
= struct
  let load ~size =
    C.ar2 C.memory_printer C.address_printer
      C.(prod_printer (value_printer ~size) memory_printer)
      (fun fmt -> Format.fprintf fmt "load ~size:%d" (In_bits.to_int size))
      (F.load ~size)

  let load_block =
    C.ar2 C.memory_printer C.address_printer
      C.(prod_printer block_printer memory_printer)
      (fun fmt -> Format.fprintf fmt "load_block")
      F.load_block

  
  let store ~size =
    C.ar3 C.memory_printer C.address_printer (C.value_printer ~size) C.memory_printer
      (fun fmt -> Format.fprintf fmt "store ~size:%d" (In_bits.to_int size))
      (F.store ~size)

  let store_block =
    C.ar3 C.memory_printer C.address_printer C.block_printer C.memory_printer
      (fun fmt -> Format.fprintf fmt "store_block")
      F.store_block

  let memcpy ~size =
    C.ar3 C.memory_printer C.address_printer C.address_printer C.memory_printer
      (fun fmt -> Format.fprintf fmt "memcpy size:%d" (In_bits.to_int size))
      (F.memcpy ~size)

  let malloc ~id ~malloc_size =
    C.ar1 C.memory_printer C.(prod_printer address_printer memory_printer)
      (fun fmt -> Format.fprintf fmt "malloc id:%s malloc_size:%d"
          (Operator_ids.Malloc_id.to_string id) (Units.In_bytes.to_int malloc_size))
      (F.malloc ~id ~malloc_size)
  
  let free =
    C.ar2 C.memory_printer C.address_printer C.memory_printer
      (fun fmt -> Format.fprintf fmt "free")
      F.free
  
  let unknown ~level =
    C.ar0 C.memory_printer
      (fun fmt -> Format.fprintf fmt "unknown level:%d" level)
      (F.unknown ~level)
  end