Skip to content

Commit d2f4435

Browse files
committed
Untested outside of macOS: sign binaries before dynlinking
1 parent 446f39d commit d2f4435

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

arrayjit/lib/cc_backend.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ let%track7_sexp c_compile_and_load ~f_name =
6868
(optimization_level ()) libname log_fname
6969
in
7070
let rc : int = Stdlib.Sys.command cmdline in
71+
(* Note: it seems waiting for the file to exist is necessary here and below regardless of needing
72+
the logs. *)
7173
while rc = 0 && (not @@ (Stdlib.Sys.file_exists libname && Stdlib.Sys.file_exists log_fname)) do
7274
Unix.sleepf 0.001
7375
done;
@@ -78,6 +80,26 @@ let%track7_sexp c_compile_and_load ~f_name =
7880
in
7981
Stdio.prerr_endline errors;
8082
invalid_arg errors);
83+
(* Expected to succeed on MacOS only. *)
84+
let sign_log_fname = base_name ^ "_run_id_" ^ run_id ^ "-sign.log" in
85+
let rc =
86+
Stdlib.Sys.command @@ Printf.sprintf "codesign -s - %s >> %s 2>&1" libname sign_log_fname
87+
in
88+
while
89+
rc = 0 && (not @@ (Stdlib.Sys.file_exists libname && Stdlib.Sys.file_exists sign_log_fname))
90+
do
91+
Unix.sleepf 0.001
92+
done;
93+
let verify_codesign =
94+
Utils.get_global_flag ~default:false ~arg_name:"cc_backend_verify_codesign"
95+
in
96+
if verify_codesign && rc <> 0 then (
97+
let errors =
98+
"Cc_backend.c_compile_and_load: codesign failed with errors:\n"
99+
^ Stdio.In_channel.read_all sign_log_fname
100+
in
101+
Stdio.prerr_endline errors;
102+
invalid_arg errors);
81103
(* Note: RTLD_DEEPBIND not available on MacOS. *)
82104
let result = { lib = Dl.dlopen ~filename:libname ~flags:[ RTLD_NOW ]; libname } in
83105
let%track7_sexp finalize (lib : library) : unit = Dl.dlclose ~handle:lib.lib in

0 commit comments

Comments
 (0)