Skip to content

Commit 1e37bb9

Browse files
cp526rbanerjee20
andauthored
[CN-exec] RB+CP cleaning up executable checking utils.c and utils.h (#651)
--------- Co-authored-by: Rini Banerjee <[email protected]>
1 parent 447b6e9 commit 1e37bb9

File tree

7 files changed

+213
-142
lines changed

7 files changed

+213
-142
lines changed

backend/cn/lib/cn_internal_to_ail.ml

+69-27
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,23 @@ let generate_error_msg_info_update_stats ?(cn_source_loc_opt = None) () =
116116
]
117117

118118

119+
let cn_pop_msg_info_sym = Sym.fresh_pretty "cn_pop_msg_info"
120+
121+
let generate_cn_pop_msg_info =
122+
let expr_ = A.(AilEcall (mk_expr (AilEident cn_pop_msg_info_sym), [])) in
123+
[ A.(AilSexpr (mk_expr expr_)) ]
124+
125+
119126
let cn_assert_sym = Sym.fresh_pretty "cn_assert"
120127

121-
let generate_cn_assert ?(cn_source_loc_opt = None) ail_expr =
128+
let generate_cn_assert (*?(cn_source_loc_opt = None)*) ail_expr =
122129
let assertion_expr_ = A.(AilEcall (mk_expr (AilEident cn_assert_sym), [ ail_expr ])) in
123130
let assertion_stat = A.(AilSexpr (mk_expr assertion_expr_)) in
124-
let error_msg_update_stats_ =
131+
(*let error_msg_update_stats_ =
125132
generate_error_msg_info_update_stats ~cn_source_loc_opt ()
126-
in
127-
error_msg_update_stats_ @ [ assertion_stat ]
133+
in*)
134+
(*error_msg_update_stats_ @*)
135+
[ assertion_stat ]
128136

129137

130138
let rec bt_to_cn_base_type = function
@@ -689,8 +697,10 @@ let dest_with_unit_check
689697
fun d (b, s, e, is_unit) ->
690698
match d with
691699
| Assert loc ->
692-
let assert_stmts = generate_cn_assert ~cn_source_loc_opt:(Some loc) e in
693-
(b, s @ assert_stmts)
700+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
701+
let pop_s = generate_cn_pop_msg_info in
702+
let assert_stmts = generate_cn_assert (*~cn_source_loc_opt:(Some loc)*) e in
703+
(b, s @ upd_s @ assert_stmts @ pop_s)
694704
| Return ->
695705
let return_stmt = if is_unit then A.(AilSreturnVoid) else A.(AilSreturn e) in
696706
(b, s @ [ return_stmt ])
@@ -855,7 +865,7 @@ let rec cn_to_ail_expr_aux_internal
855865
a dest ->
856866
a
857867
=
858-
fun const_prop pred_name dts globals (IT (term_, basetype, loc)) d ->
868+
fun const_prop pred_name dts globals (IT (term_, basetype, _loc)) d ->
859869
match term_ with
860870
| Const const ->
861871
let ail_expr_, is_unit = cn_to_ail_const_internal const in
@@ -1331,10 +1341,12 @@ let rec cn_to_ail_expr_aux_internal
13311341
let bs, ss, es = list_split_three bs_ss_es in
13321342
let f = mk_expr A.(AilEident sym) in
13331343
let ail_expr_ = A.AilEcall (f, es) in
1334-
let error_msg_update_stats_ =
1344+
(*let error_msg_update_stats_ =
13351345
generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) ()
1336-
in
1337-
dest d (List.concat bs, List.concat ss @ error_msg_update_stats_, mk_expr ail_expr_)
1346+
in*)
1347+
dest
1348+
d
1349+
(List.concat bs, List.concat ss (*@ error_msg_update_stats_*), mk_expr ail_expr_)
13381350
| Let ((var, t1), body) ->
13391351
let b1, s1, e1 =
13401352
cn_to_ail_expr_aux_internal const_prop pred_name dts globals t1 PassBack
@@ -2542,7 +2554,7 @@ let cn_to_ail_resource_internal
25422554
dts
25432555
globals
25442556
(preds : (Sym.t * RP.definition) list)
2545-
loc
2557+
_loc
25462558
=
25472559
let calculate_return_type = function
25482560
| ResourceTypes.Owned (sct, _) ->
@@ -2597,9 +2609,9 @@ let cn_to_ail_resource_internal
25972609
list_split_three
25982610
(List.map (fun it -> cn_to_ail_expr_internal dts globals it PassBack) p.iargs)
25992611
in
2600-
let error_msg_update_stats_ =
2612+
(*let error_msg_update_stats_ =
26012613
generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) ()
2602-
in
2614+
in*)
26032615
let fcall =
26042616
A.(
26052617
AilEcall
@@ -2608,7 +2620,7 @@ let cn_to_ail_resource_internal
26082620
let binding = create_binding sym (bt_to_ail_ctype ~pred_sym:(Some pname) bt) in
26092621
( mk_expr fcall,
26102622
binding :: List.concat bs,
2611-
List.concat ss @ error_msg_update_stats_,
2623+
List.concat ss (*@ error_msg_update_stats_*),
26122624
None )
26132625
in
26142626
let s_decl =
@@ -2704,17 +2716,17 @@ let cn_to_ail_resource_internal
27042716
list_split_three
27052717
(List.map (fun it -> cn_to_ail_expr_internal dts globals it PassBack) q.iargs)
27062718
in
2707-
let error_msg_update_stats_ =
2719+
(*let error_msg_update_stats_ =
27082720
generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) ()
2709-
in
2721+
in*)
27102722
let fcall =
27112723
A.(
27122724
AilEcall
27132725
( mk_expr (AilEident pname),
27142726
(mk_expr (AilEident ptr_add_sym) :: es) @ [ mk_expr (AilEident enum_sym) ]
27152727
))
27162728
in
2717-
(mk_expr fcall, List.concat bs, List.concat ss @ error_msg_update_stats_, None)
2729+
(mk_expr fcall, List.concat bs, List.concat ss (*@ error_msg_update_stats_*), None)
27182730
in
27192731
let typedef_name = get_typedef_string (bt_to_ail_ctype i_bt) in
27202732
let incr_func_name =
@@ -2981,14 +2993,18 @@ let rec cn_to_ail_lat_internal ?(is_toplevel = true) dts pred_sym_opt globals pr
29812993
let b2, s2 = cn_to_ail_lat_internal ~is_toplevel dts pred_sym_opt globals preds lat in
29822994
(b1 @ b2 @ [ binding ], (decl :: s1) @ s2)
29832995
| LAT.Resource ((name, (ret, _bt)), (loc, _str_opt), lat) ->
2996+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
2997+
let pop_s = generate_cn_pop_msg_info in
29842998
let b1, s1 =
29852999
cn_to_ail_resource_internal ~is_pre:true ~is_toplevel name dts globals preds loc ret
29863000
in
29873001
let b2, s2 = cn_to_ail_lat_internal ~is_toplevel dts pred_sym_opt globals preds lat in
2988-
(b1 @ b2, s1 @ s2)
3002+
(b1 @ b2, upd_s @ s1 @ pop_s @ s2)
29893003
| LAT.Constraint (lc, (loc, _str_opt), lat) ->
29903004
let b1, s, e = cn_to_ail_logical_constraint_internal dts globals PassBack lc in
2991-
let ss = s @ generate_cn_assert ~cn_source_loc_opt:(Some loc) e in
3005+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3006+
let pop_s = generate_cn_pop_msg_info in
3007+
let ss = upd_s @ s @ generate_cn_assert (*~cn_source_loc_opt:(Some loc)*) e @ pop_s in
29923008
let b2, s2 = cn_to_ail_lat_internal ~is_toplevel dts pred_sym_opt globals preds lat in
29933009
(b1 @ b2, ss @ s2)
29943010
| LAT.I it ->
@@ -3019,6 +3035,13 @@ let cn_to_ail_predicate_internal
30193035
preds
30203036
c.packing_ft
30213037
in
3038+
let ss =
3039+
(*let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some rp_def.loc) () in
3040+
let pop_s = generate_cn_pop_msg_info in*)
3041+
(*upd_s @*)
3042+
ss
3043+
(*@ pop_s*)
3044+
in
30223045
(match c.guard with
30233046
| IT (Const (Bool true), _, _) ->
30243047
let bs'', ss'' = clause_translate cs in
@@ -3114,25 +3137,36 @@ let rec cn_to_ail_post_aux_internal dts globals preds = function
31143137
(b1 @ b2 @ [ binding ], (decl :: s1) @ s2)
31153138
| LRT.Resource ((name, (re, bt)), (loc, _str_opt), t) ->
31163139
let new_name = generate_sym_with_suffix ~suffix:"_cn" name in
3140+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3141+
let pop_s = generate_cn_pop_msg_info in
31173142
let b1, s1 =
31183143
cn_to_ail_resource_internal ~is_pre:false new_name dts globals preds loc re
31193144
in
31203145
let new_lrt =
31213146
Core_to_mucore.fn_spec_instrumentation_sym_subst_lrt (name, bt, new_name) t
31223147
in
31233148
let b2, s2 = cn_to_ail_post_aux_internal dts globals preds new_lrt in
3124-
(b1 @ b2, s1 @ s2)
3149+
(b1 @ b2, upd_s @ s1 @ pop_s @ s2)
31253150
| LRT.Constraint (lc, (loc, _str_opt), t) ->
3151+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3152+
let pop_s = generate_cn_pop_msg_info in
31263153
let b1, s, e = cn_to_ail_logical_constraint_internal dts globals PassBack lc in
3127-
let ss = s @ generate_cn_assert ~cn_source_loc_opt:(Some loc) e in
3154+
let ss = upd_s @ s @ generate_cn_assert (*~cn_source_loc_opt:(Some loc)*) e @ pop_s in
31283155
let b2, s2 = cn_to_ail_post_aux_internal dts globals preds t in
31293156
(b1 @ b2, ss @ s2)
31303157
| LRT.I -> ([], [])
31313158

31323159

3133-
let cn_to_ail_post_internal dts globals preds (RT.Computational (_bound, _oinfo, t)) =
3160+
let cn_to_ail_post_internal
3161+
(*loc*) dts
3162+
globals
3163+
preds
3164+
(RT.Computational (_bound, _oinfo, t))
3165+
=
3166+
(*let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3167+
let pop_s = generate_cn_pop_msg_info in*)
31343168
let bs, ss = cn_to_ail_post_aux_internal dts globals preds t in
3135-
(bs, List.map mk_stmt ss)
3169+
(bs, List.map mk_stmt (*upd_s @*) ss (*@ pop_s*))
31363170

31373171

31383172
(* TODO: Add destination passing *)
@@ -3196,8 +3230,10 @@ let rec cn_to_ail_cnprog_internal_aux dts globals = function
31963230
else
31973231
((loc', b1 @ b2 @ [binding], s @ ail_stat_ :: ss), false) *)
31983232
| Statement (loc, stmt) ->
3233+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3234+
let pop_s = generate_cn_pop_msg_info in
31993235
let (bs, ss), no_op = cn_to_ail_cnstatement_internal dts globals (Assert loc) stmt in
3200-
((bs, ss), no_op)
3236+
((bs, upd_s @ ss @ pop_s), no_op)
32013237

32023238

32033239
let cn_to_ail_cnprog_internal dts globals cn_prog =
@@ -3206,11 +3242,13 @@ let cn_to_ail_cnprog_internal dts globals cn_prog =
32063242

32073243

32083244
let cn_to_ail_statements dts globals (loc, cn_progs) =
3245+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3246+
let pop_s = generate_cn_pop_msg_info in
32093247
let bs_and_ss =
32103248
List.map (fun prog -> cn_to_ail_cnprog_internal dts globals prog) cn_progs
32113249
in
32123250
let bs, ss = List.split bs_and_ss in
3213-
(loc, (List.concat bs, List.concat ss))
3251+
(loc, (List.concat bs, upd_s @ List.concat ss @ pop_s))
32143252

32153253

32163254
let prepend_to_precondition ail_executable_spec (b1, s1) =
@@ -3242,6 +3280,8 @@ let rec cn_to_ail_lat_internal_2 with_ownership_checking dts globals preds c_ret
32423280
in
32433281
prepend_to_precondition ail_executable_spec (binding :: b1, decl :: s1)
32443282
| LAT.Resource ((name, (ret, bt)), (loc, _str_opt), lat) ->
3283+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3284+
let pop_s = generate_cn_pop_msg_info in
32453285
let new_name = generate_sym_with_suffix ~suffix:"_cn" name in
32463286
let b1, s1 =
32473287
cn_to_ail_resource_internal ~is_pre:true new_name dts globals preds loc ret
@@ -3258,10 +3298,12 @@ let rec cn_to_ail_lat_internal_2 with_ownership_checking dts globals preds c_ret
32583298
c_return_type
32593299
new_lat
32603300
in
3261-
prepend_to_precondition ail_executable_spec (b1, s1)
3301+
prepend_to_precondition ail_executable_spec (b1, upd_s @ s1 @ pop_s)
32623302
| LAT.Constraint (lc, (loc, _str_opt), lat) ->
3303+
let upd_s = generate_error_msg_info_update_stats ~cn_source_loc_opt:(Some loc) () in
3304+
let pop_s = generate_cn_pop_msg_info in
32633305
let b1, s, e = cn_to_ail_logical_constraint_internal dts globals PassBack lc in
3264-
let ss = s @ generate_cn_assert ~cn_source_loc_opt:(Some loc) e in
3306+
let ss = upd_s @ s @ generate_cn_assert (*~cn_source_loc_opt:(Some loc)*) e @ pop_s in
32653307
let ail_executable_spec =
32663308
cn_to_ail_lat_internal_2 with_ownership_checking dts globals preds c_return_type lat
32673309
in

backend/cn/lib/ownership_exec.ml

+6-11
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ let cn_ghost_state_struct_type =
99
mk_ctype ~annots:[ CF.Annot.Atypedef (Sym.fresh_pretty "ownership_ghost_state") ] C.Void
1010

1111

12-
let cn_stack_depth_sym = Sym.fresh_pretty "cn_stack_depth"
12+
let get_cn_stack_depth_sym = Sym.fresh_pretty "get_cn_stack_depth"
1313

1414
let cn_stack_depth_ctype = C.mk_ctype_integer (Signed Long)
1515

1616
let cn_stack_depth_incr_sym = Sym.fresh_pretty "ghost_stack_depth_incr"
1717

1818
let cn_stack_depth_decr_sym = Sym.fresh_pretty "ghost_stack_depth_decr"
1919

20-
let c_map_local_ownership_fn_sym = Sym.fresh_pretty "c_add_local_to_ghost_state"
20+
let c_add_ownership_fn_sym = Sym.fresh_pretty "c_add_to_ghost_state"
2121

22-
let c_remove_local_ownership_fn_sym = Sym.fresh_pretty "c_remove_local_from_ghost_state"
22+
let c_remove_ownership_fn_sym = Sym.fresh_pretty "c_remove_from_ghost_state"
2323

2424
(* TODO: Use these to reduce verbosity of output. Mirrors C+CN input slightly less since
2525
we replace declarations with a call to one of these macros *)
@@ -63,12 +63,6 @@ let get_end_loc ?(offset = 0) = function
6363
"get_end_loc: Location of AilSdeclaration should be Loc_region or Loc_regions"
6464

6565

66-
let create_ail_ownership_global_decls () =
67-
[ (cn_ghost_state_sym, C.mk_ctype_pointer empty_qualifiers cn_ghost_state_struct_type);
68-
(cn_stack_depth_sym, cn_stack_depth_ctype)
69-
]
70-
71-
7266
let get_ownership_global_init_stats () =
7367
let cn_ghost_state_init_fcall =
7468
mk_expr
@@ -96,9 +90,10 @@ let generate_c_local_ownership_entry_fcall (local_sym, local_ctype) =
9690
AilEcast (empty_qualifiers, C.uintptr_t, mk_expr (AilEunary (Address, local_ident))))
9791
in
9892
let arg2 = A.(AilEsizeof (empty_qualifiers, local_ctype)) in
93+
let arg3 = A.(AilEcall (mk_expr (AilEident get_cn_stack_depth_sym), [])) in
9994
mk_expr
10095
(AilEcall
101-
(mk_expr (AilEident c_map_local_ownership_fn_sym), List.map mk_expr [ arg1; arg2 ]))
96+
(mk_expr (AilEident c_add_ownership_fn_sym), List.map mk_expr [ arg1; arg2; arg3 ]))
10297

10398

10499
(* int x = 0, y = 5;
@@ -154,7 +149,7 @@ let generate_c_local_ownership_exit (local_sym, local_ctype) =
154149
(mk_expr
155150
A.(
156151
AilEcall
157-
( mk_expr (AilEident c_remove_local_ownership_fn_sym),
152+
( mk_expr (AilEident c_remove_ownership_fn_sym),
158153
List.map mk_expr [ arg1; arg2 ] ))))
159154

160155

runtime/libcn/include/cn-executable/utils.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct cn_error_message_info {
3939
char *file_name;
4040
int line_number;
4141
char *cn_source_loc;
42+
struct cn_error_message_info *parent;
4243
};
4344

4445
void initialise_error_msg_info_(const char *function_name, char *file_name, int line_number);
@@ -47,13 +48,15 @@ void initialise_error_msg_info_(const char *function_name, char *file_name, int
4748

4849

4950
/* TODO: Implement */
50-
struct cn_error_messages {
51+
/*struct cn_error_messages {
5152
struct cn_error_message_info *top_level_error_msg_info;
5253
struct cn_error_message_info *nested_error_msg_info;
53-
};
54+
};*/
5455

5556
void update_error_message_info_(const char *function_name, char *file_name, int line_number, char *cn_source_loc);
5657

58+
void cn_pop_msg_info();
59+
5760
#define update_cn_error_message_info(x)\
5861
update_error_message_info_(__func__, __FILE__, __LINE__ + 1, x)
5962

@@ -120,6 +123,7 @@ typedef hash_table cn_map;
120123

121124
void initialise_ownership_ghost_state(void);
122125
void initialise_ghost_stack_depth(void);
126+
signed long get_cn_stack_depth(void);
123127
void ghost_stack_depth_incr(void);
124128
void ghost_stack_depth_decr(void);
125129

@@ -522,13 +526,13 @@ void cn_assume_ownership(void *generic_c_ptr, unsigned long size, char *fun);
522526
void cn_check_ownership(enum OWNERSHIP owned_enum, uintptr_t generic_c_ptr, size_t size);
523527

524528
/* C ownership checking */
525-
void c_add_local_to_ghost_state(uintptr_t ptr_to_local, size_t size);
526-
void c_remove_local_from_ghost_state(uintptr_t ptr_to_local, size_t size);
527-
void c_ownership_check(uintptr_t generic_c_ptr, int offset);
529+
void c_add_to_ghost_state(uintptr_t ptr_to_local, size_t size, signed long stack_depth);
530+
void c_remove_from_ghost_state(uintptr_t ptr_to_local, size_t size);
531+
void c_ownership_check(char *access_kind, uintptr_t generic_c_ptr, int offset, signed long expected_stack_depth);
528532

529533
// Unused
530534
#define c_concat_with_mapping_stat(STAT, CTYPE, VAR_NAME, GHOST_STATE, STACK_DEPTH)\
531-
STAT; c_add_local_to_ghost_state((uintptr_t) &VAR_NAME, GHOST_STATE, sizeof(CTYPE), STACK_DEPTH);
535+
STAT; c_add_to_ghost_state((uintptr_t) &VAR_NAME, GHOST_STATE, sizeof(CTYPE), STACK_DEPTH);
532536

533537
#define c_declare_and_map_local(CTYPE, VAR_NAME)\
534538
c_concat_with_mapping_stat(CTYPE VAR_NAME, CTYPE, VAR_NAME)
@@ -558,7 +562,7 @@ static inline void cn_postfix(void *ptr, size_t size)
558562
({ \
559563
typeof(LV) *__tmp = &(LV); \
560564
update_cn_error_message_info_access_check(NULL); \
561-
c_ownership_check((uintptr_t) __tmp, sizeof(typeof(LV))); \
565+
c_ownership_check("Load", (uintptr_t) __tmp, sizeof(typeof(LV)), get_cn_stack_depth()); \
562566
cn_load(__tmp, sizeof(typeof(LV))); \
563567
*__tmp; \
564568
})
@@ -568,7 +572,7 @@ static inline void cn_postfix(void *ptr, size_t size)
568572
typeof(LV) *__tmp; \
569573
__tmp = &(LV); \
570574
update_cn_error_message_info_access_check(NULL); \
571-
c_ownership_check((uintptr_t) __tmp, sizeof(typeof(LV))); \
575+
c_ownership_check("Store", (uintptr_t) __tmp, sizeof(typeof(LV)), get_cn_stack_depth()); \
572576
cn_store(__tmp, sizeof(typeof(LV))); \
573577
*__tmp op##= (X); \
574578
})
@@ -580,7 +584,7 @@ static inline void cn_postfix(void *ptr, size_t size)
580584
typeof(LV) *__tmp; \
581585
__tmp = &(LV); \
582586
update_cn_error_message_info_access_check(NULL); \
583-
c_ownership_check((uintptr_t) __tmp, sizeof(typeof(LV))); \
587+
c_ownership_check("Postfix operation", (uintptr_t) __tmp, sizeof(typeof(LV)), get_cn_stack_depth()); \
584588
cn_postfix(__tmp, sizeof(typeof(LV))); \
585589
(*__tmp) OP; \
586590
})

0 commit comments

Comments
 (0)