Skip to content

Commit dda5c07

Browse files
committed
Perform null-checks when assigning initial value of heap allocations
1 parent f82ecfe commit dda5c07

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/CStarToC11.ml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,19 +372,30 @@ and mk_for_loop name qs t init test incr body =
372372

373373
(* Takes e_array of type (Buf t). e_size = size of the array, in number of
374374
elements *)
375-
and mk_initializer t e_array e_size e_value: C.stmt =
375+
and mk_initializer ~null_check t e_array e_size e_value: C.stmt =
376376
(* KPrint.bprintf "element_size is: %s\n" (C11.show_expr e_size); *)
377377
(* KPrint.bprintf "t is: %s\n" (C11.show_type_name t); *)
378+
let if_not_null e: C.stmt =
379+
if null_check then
380+
if !Options.curly_braces then
381+
If (Op2 (K.Neq, e_array, Name "NULL"), Compound [ e ])
382+
else
383+
If (Op2 (K.Neq, e_array, Name "NULL"), e)
384+
else
385+
e
386+
in
378387
match e_size with
379388
| C.Constant (_, "1")
380389
| C.Cast (_, C.Constant (_, "1")) ->
390+
if_not_null @@
381391
Expr (Op2 (K.Assign, Index (e_array, Constant (K.UInt32, "0")), e_value))
382392

383393
| _ ->
384394
match e_value with
385395
| C.Constant (_, s)
386396
| C.Cast (_, C.Constant (_, s)) when int_of_string s = 0 ->
387397
(* KPrint.bprintf "need memset 0\n"; *)
398+
if_not_null @@
388399
mk_memset t e_array e_size (C.Constant (K.UInt8, "0"))
389400

390401
| C.Name "Lib_IntVector_Intrinsics_vec128_zero"
@@ -394,14 +405,17 @@ and mk_initializer t e_array e_size e_value: C.stmt =
394405
allocating simd state. Under the hood, the C memset will use suitable instructions to
395406
go fast. *)
396407
(* KPrint.bprintf "need memset 1\n"; *)
408+
if_not_null @@
397409
mk_memset t e_array e_size (C.Constant (K.UInt8, "0"))
398410

399411
| C.Constant (K.UInt8, _)
400412
| C.Cast (_, C.Constant (K.UInt8, _)) ->
401413
(* KPrint.bprintf "need memset 2\n"; *)
414+
if_not_null @@
402415
mk_memset t e_array e_size e_value
403416

404417
| _ ->
418+
if_not_null @@
405419
mk_for_loop "_i" [] (Int K.UInt32) zero
406420
(Op2 (K.Lt, Name "_i", e_size))
407421
(Op1 (K.PreIncr, Name "_i"))
@@ -501,7 +515,7 @@ and mk_eternal_bufcreate m buf (t: CStar.typ) init size =
501515
mk_malloc m t size, []
502516
| _ ->
503517
mk_malloc m t size,
504-
[ mk_initializer (mk_type m t) (mk_expr m buf) size (mk_expr m init) ]
518+
[ mk_initializer ~null_check:true (mk_type m t) (mk_expr m buf) size (mk_expr m init) ]
505519
in
506520
mk_check_size m t size, e, extra_stmt
507521

@@ -678,7 +692,7 @@ and mk_stmt m (stmt: stmt): C.stmt list =
678692
let qs, spec, decl = mk_spec_and_declarator m binder.name t in
679693
let extra_stmt: C.stmt list =
680694
if needs_init then
681-
[ mk_initializer (mk_type m (assert_pointer t)) (Name binder.name) n_elements init ]
695+
[ mk_initializer ~null_check:false (mk_type m (assert_pointer t)) (Name binder.name) n_elements init ]
682696
else
683697
[]
684698
in
@@ -753,7 +767,8 @@ and mk_stmt m (stmt: stmt): C.stmt list =
753767
let size = mk_expr m size in
754768
let stmt_init = mk_stmt m (Decl ({ name = name_init; typ = t_elt }, init)) in
755769
let stmt_assign = [ Expr (Assign (mk_expr m e1, mk_malloc m t_elt size)) ] in
756-
let stmt_fill = mk_initializer (mk_type m t_elt) (mk_expr m e1) size (mk_expr m (Var name_init)) in
770+
(* GC'd allocation, not null-checking this -- this is LEGACY *)
771+
let stmt_fill = mk_initializer ~null_check:false (mk_type m t_elt) (mk_expr m e1) size (mk_expr m (Var name_init)) in
757772
stmt_init @
758773
stmt_assign @
759774
[ stmt_fill ]
@@ -791,7 +806,7 @@ and mk_stmt m (stmt: stmt): C.stmt list =
791806

792807
| BufFill (t, buf, v, size) ->
793808
(* Again, assuming that these are non-effectful. *)
794-
[ mk_initializer (mk_type m t) (mk_expr m buf) (mk_expr m size) (mk_expr m v) ]
809+
[ mk_initializer ~null_check:false (mk_type m t) (mk_expr m buf) (mk_expr m size) (mk_expr m v) ]
795810

796811
| BufFree (t, e) ->
797812
[ Expr (mk_free t (mk_expr m e)) ]

0 commit comments

Comments
 (0)