Skip to content

Commit 0737ca9

Browse files
authored
More prudent handling of static names for string literals (#579)
- Make sure the names are fresh and not already used. - Mark the names specially so that the special printing of initializers as strings applies only to those names.
1 parent d9377f6 commit 0737ca9

4 files changed

Lines changed: 36 additions & 16 deletions

File tree

backend/PrintAsm.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,11 @@ module Printer(Target:TARGET) =
127127
symbol_offset (symb, ofs)
128128

129129

130-
let print_init_data oc name id =
131-
if Str.string_match PrintCsyntax.re_string_literal (extern_atom name) 0
132-
&& List.for_all (function Init_int8 _ -> true | _ -> false) id
133-
then
134-
fprintf oc " .ascii \"%s\"\n" (PrintCsyntax.string_of_init id)
130+
let print_init_data oc name il =
131+
if C2C.atom_literal name = C2C.String_literal then
132+
fprintf oc " .ascii \"%s\"\n" (PrintCsyntax.string_of_init il)
135133
else
136-
List.iter (print_init oc) id
134+
List.iter (print_init oc) il
137135

138136
let print_var oc name v =
139137
match v.gvar_init with

cfrontend/C2C.ml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type inline_status =
3232
| Noinline (* The atom is declared with the noinline attribute *)
3333
| Inline (* The atom is declared inline *)
3434

35+
type literal_status =
36+
| No_literal
37+
| String_literal
38+
| Wide_string_literal
39+
3540
type atom_info =
3641
{ a_storage: C.storage; (* storage class *)
3742
a_defined: bool; (* defined in the current comp. unit? *)
@@ -41,6 +46,7 @@ type atom_info =
4146
(* 1 section for data, 3 sections (code/lit/jumptbl) for functions *)
4247
a_access: Sections.access_mode; (* access mode, e.g. small data area *)
4348
a_inline: inline_status; (* function declared inline? *)
49+
a_literal: literal_status; (* is this a string literal? *)
4450
a_loc: location (* source location *)
4551
}
4652

@@ -138,6 +144,12 @@ let atom_location a =
138144
with Not_found ->
139145
Cutil.no_loc
140146

147+
let atom_literal a =
148+
try
149+
(Hashtbl.find decl_atom a).a_literal
150+
with Not_found ->
151+
No_literal
152+
141153
(** The current environment of composite definitions *)
142154

143155
let comp_env : composite_env ref = ref Maps.PTree.empty
@@ -594,6 +606,15 @@ let is_int64 env ty =
594606
(** String literals *)
595607

596608
let stringNum = ref 0 (* number of next global for string literals *)
609+
610+
let rec gensym_string_literal () =
611+
incr stringNum;
612+
let name = Printf.sprintf "__stringlit_%d" !stringNum in
613+
(* Make sure the name is not already used in the source program *)
614+
if Hashtbl.mem atom_of_string name
615+
then gensym_string_literal ()
616+
else name
617+
597618
let stringTable : (string, AST.ident) Hashtbl.t = Hashtbl.create 47
598619
let wstringTable : (int64 list * ikind, AST.ident) Hashtbl.t = Hashtbl.create 47
599620

@@ -603,8 +624,7 @@ let name_for_string_literal s =
603624
try
604625
Hashtbl.find stringTable s
605626
with Not_found ->
606-
incr stringNum;
607-
let name = Printf.sprintf "__stringlit_%d" !stringNum in
627+
let name = gensym_string_literal () in
608628
let id = intern_string name in
609629
let mergeable = if is_C_string s then 1 else 0 in
610630
Hashtbl.add decl_atom id
@@ -615,6 +635,7 @@ let name_for_string_literal s =
615635
a_sections = [Sections.for_stringlit mergeable];
616636
a_access = Sections.Access_default;
617637
a_inline = No_specifier;
638+
a_literal = String_literal;
618639
a_loc = Cutil.no_loc };
619640
Hashtbl.add stringTable s id;
620641
id
@@ -638,8 +659,7 @@ let name_for_wide_string_literal s ik =
638659
try
639660
Hashtbl.find wstringTable (s, ik)
640661
with Not_found ->
641-
incr stringNum;
642-
let name = Printf.sprintf "__stringlit_%d" !stringNum in
662+
let name = gensym_string_literal () in
643663
let id = intern_string name in
644664
let wchar_size = Cutil.sizeof_ikind ik in
645665
let mergeable = if is_C_wide_string s then wchar_size else 0 in
@@ -652,6 +672,7 @@ let name_for_wide_string_literal s ik =
652672
a_sections = [Sections.for_stringlit mergeable];
653673
a_access = Sections.Access_default;
654674
a_inline = No_specifier;
675+
a_literal = Wide_string_literal;
655676
a_loc = Cutil.no_loc };
656677
Hashtbl.add wstringTable (s, ik) id;
657678
id
@@ -1185,6 +1206,7 @@ let convertFundef loc env fd =
11851206
a_sections = Sections.for_function env loc id' fd.fd_attrib;
11861207
a_access = Sections.Access_default;
11871208
a_inline = inline;
1209+
a_literal = No_literal;
11881210
a_loc = loc };
11891211
(id', AST.Gfun(Ctypes.Internal
11901212
{fn_return = ret;
@@ -1276,6 +1298,7 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
12761298
a_sections = [section];
12771299
a_access = access;
12781300
a_inline = No_specifier;
1301+
a_literal = No_literal;
12791302
a_loc = loc };
12801303
let volatile = List.mem C.AVolatile attr in
12811304
let readonly = List.mem C.AConst attr && not volatile in

cfrontend/PrintCsyntax.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ let string_of_init id =
450450
let b = Buffer.create (List.length id) in
451451
let add_init = function
452452
| Init_int8 n ->
453-
let c = Int32.to_int (camlint_of_coqint n) in
453+
let n = camlint_of_coqint n in
454+
assert (n >= 0l && n < 256l);
455+
let c = Int32.to_int n in
454456
if c >= 32 && c <= 126 && c <> Char.code '\"' && c <> Char.code '\\'
455457
then Buffer.add_char b (Char.chr c)
456458
else Buffer.add_string b (Printf.sprintf "\\%03o" c)
@@ -486,8 +488,6 @@ let print_composite_init p il =
486488
il;
487489
fprintf p "}"
488490

489-
let re_string_literal = Str.regexp "__stringlit_[0-9]+"
490-
491491
let print_globvar p id v =
492492
let name1 = extern_atom id in
493493
let name2 = if v.gvar_readonly then "const " ^ name1 else name1 in
@@ -506,8 +506,7 @@ let print_globvar p id v =
506506
[i1] ->
507507
print_init p i1
508508
| _, il ->
509-
if Str.string_match re_string_literal (extern_atom id) 0
510-
&& List.for_all (function Init_int8 _ -> true | _ -> false) il
509+
if C2C.atom_literal id = C2C.String_literal
511510
then fprintf p "\"%s\"" (string_of_init (chop_last_nul il))
512511
else print_composite_init p il
513512
end;

0 commit comments

Comments
 (0)