Skip to content

Commit

Permalink
Merge pull request #541 from AbsInt/dollar-ident
Browse files Browse the repository at this point in the history
Protect against `$NNN` identifiers
  • Loading branch information
xavierleroy authored Dec 6, 2024
2 parents 3710994 + cdfff5e commit d4c6a8c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
17 changes: 14 additions & 3 deletions arm/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,18 @@ struct

let symbol = elf_symbol

let symbol_offset = elf_symbol_offset
let symbol_paren oc symb =
let s = extern_atom symb in
if String.length s > 0 && s.[0] = '$'
then fprintf oc "(%s)" s
else fprintf oc "%s" s

let symbol_offset oc (symb, ofs) =
let ofs = camlint64_of_ptrofs ofs in
if ofs = 0L then
symbol_paren oc symb
else
fprintf oc "(%a + %Ld)" symbol symb ofs

let ireg oc r = output_string oc (int_reg_name r)
let freg oc r = output_string oc (float_reg_name r)
Expand Down Expand Up @@ -227,11 +238,11 @@ struct
| Pbne lbl ->
fprintf oc " bne %a\n" print_label lbl
| Pbsymb(id, sg) ->
fprintf oc " b %a\n" symbol id
fprintf oc " b %a\n" symbol_paren id
| Pbreg(r, sg) ->
fprintf oc " bx %a\n" ireg r
| Pblsymb(id, sg) ->
fprintf oc " bl %a\n" symbol id
fprintf oc " bl %a\n" symbol_paren id
| Pblreg(r, sg) ->
fprintf oc " blx %a\n" ireg r
| Pbic(r1, r2, so) ->
Expand Down
2 changes: 1 addition & 1 deletion test
28 changes: 22 additions & 6 deletions x86/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module type SYSTEM =
val comment: string
val raw_symbol: out_channel -> string -> unit
val symbol: out_channel -> P.t -> unit
val symbol_paren: out_channel -> P.t -> unit
val label: out_channel -> int -> unit
val name_of_section: section_name -> string
val stack_alignment: int
Expand All @@ -130,6 +131,12 @@ module ELF_System : SYSTEM =

let symbol = elf_symbol

let symbol_paren oc symb =
let s = extern_atom symb in
if String.length s > 0 && s.[0] = '$'
then fprintf oc "(%s)" s
else fprintf oc "%s" s

let label = elf_label

let name_of_section = function
Expand Down Expand Up @@ -164,8 +171,8 @@ module ELF_System : SYSTEM =

let print_mov_rs oc rd id =
if Archi.ptr64
then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol id ireg64 rd
else fprintf oc " movl $%a, %a\n" symbol id ireg32 rd
then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol_paren id ireg64 rd
else fprintf oc " movl $%a, %a\n" symbol_paren id ireg32 rd

let print_fun_info = elf_print_fun_info

Expand Down Expand Up @@ -196,6 +203,9 @@ module MacOS_System : SYSTEM =
let symbol oc symb =
raw_symbol oc (extern_atom symb)

let symbol_paren = symbol
(* the leading '_' protects the leading '$' *)

let label oc lbl =
fprintf oc "L%d" lbl

Expand Down Expand Up @@ -262,6 +272,12 @@ module Cygwin_System : SYSTEM =
let symbol oc symb =
raw_symbol oc (extern_atom symb)

let symbol_paren oc symb =
let s = extern_atom symb in
if String.length s > 0 && s.[0] = '$'
then fprintf oc "(%a)" raw_symbol s
else raw_symbol oc s

let label oc lbl =
fprintf oc "L%d" lbl

Expand Down Expand Up @@ -341,13 +357,13 @@ module Target(System: SYSTEM):TARGET =
(* RIP-relative addressing *)
let ofs' = Z.to_int64 ofs in
if ofs' = 0L
then fprintf oc "%a(%%rip)" symbol id
then fprintf oc "%a(%%rip)" symbol_paren id
else fprintf oc "(%a + %Ld)(%%rip)" symbol id ofs'
end else begin
(* Absolute addressing *)
let ofs' = Z.to_int32 ofs in
if ofs' = 0l
then fprintf oc "%a" symbol id
then fprintf oc "%a" symbol_paren id
else fprintf oc "(%a + %ld)" symbol id ofs'
end
end;
Expand Down Expand Up @@ -707,7 +723,7 @@ module Target(System: SYSTEM):TARGET =
| Pjmp_l(l) ->
fprintf oc " jmp %a\n" label (transl_label l)
| Pjmp_s(f, sg) ->
fprintf oc " jmp %a\n" symbol f
fprintf oc " jmp %a\n" symbol_paren f
| Pjmp_r(r, sg) ->
fprintf oc " jmp *%a\n" ireg r
| Pjcc(c, l) ->
Expand All @@ -733,7 +749,7 @@ module Target(System: SYSTEM):TARGET =
fprintf oc " jmp *%a(, %a, 4)\n" label l ireg r
end
| Pcall_s(f, sg) ->
fprintf oc " call %a\n" symbol f;
fprintf oc " call %a\n" symbol_paren f;
if (not Archi.ptr64) && sg.sig_cc.cc_structret then
fprintf oc " pushl %%eax\n"
| Pcall_r(r, sg) ->
Expand Down

0 comments on commit d4c6a8c

Please sign in to comment.