The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

`natdynlink`: `dlclose` on error (#14391)

* Add test

* natdynlink: dlclose on error

* Promote test

* Only dlclose if unit has not initialized, fix test

* Accept

* Call ndl_register after CRC check

* Accept

* Simplify

* Accept

* Changes

* Fix test script

* Fix test script

* Accept

* Disable test on Windows

authored by

Nicolás Ojeda Bär and committed by
GitHub
19c757bc 1e05f341

+157 -48
+4
Changes
··· 210 210 and socklen_param_type for socklen_t. 211 211 (Antonin Décimo, review by Nicolás Ojeda Bär, David Allsopp and Samuel Hym) 212 212 213 + - #14391: unload native dynlinked objects when an error occurs and it is safe to 214 + do so. (Fixes #14323) 215 + (Nicolás Ojeda Bär, review by Vincent Laviron) 216 + 213 217 ### Tools: 214 218 215 219 - #14055: Invert BUILD_PATH_PREFIX_MAP in directories loaded at startup
-2
otherlibs/dynlink/byte/dynlink.ml
··· 103 103 init 104 104 !default_crcs 105 105 106 - let run_shared_startup _ = () 107 - 108 106 let with_lock lock f = 109 107 Mutex.lock lock; 110 108 Fun.protect f
+33 -12
otherlibs/dynlink/native/dynlink.ml
··· 32 32 } 33 33 34 34 module Native = struct 35 - type handle 35 + type ndl_handle 36 + type initialized = 37 + | Initialized 38 + | Not_yet_initialized of string list 39 + type handle = { 40 + ndl_handle : ndl_handle; 41 + mutable initialized : initialized; 42 + } 36 43 37 - external ndl_open : string -> bool -> handle * dynheader 44 + external ndl_open : string -> bool -> ndl_handle * dynheader 38 45 = "caml_natdynlink_open" 39 - external ndl_register : handle -> string array -> unit 46 + external ndl_close : ndl_handle -> unit 47 + = "caml_natdynlink_close" 48 + external ndl_register : ndl_handle -> string array -> unit 40 49 = "caml_natdynlink_register" 41 - external ndl_run : handle -> string -> unit = "caml_natdynlink_run" 50 + external ndl_run : ndl_handle -> string -> unit = "caml_natdynlink_run" 42 51 external ndl_getmap : unit -> global_map list = "caml_natdynlink_getmap" 43 52 external ndl_globals_inited : unit -> int = "caml_natdynlink_globals_inited" 44 53 external ndl_loadsym : string -> Obj.t = "caml_natdynlink_loadsym" ··· 78 87 (ndl_getmap ()) 79 88 80 89 let run_shared_startup handle = 81 - ndl_run handle "_shared_startup" 90 + match handle.initialized with 91 + | Initialized -> () 92 + | Not_yet_initialized syms -> 93 + handle.initialized <- Initialized; 94 + begin try 95 + ndl_register handle.ndl_handle (Array.of_list syms); 96 + with exn -> 97 + raise (DT.Error (Cannot_open_dynamic_library exn)) 98 + end; 99 + ndl_run handle.ndl_handle "_shared_startup" 82 100 83 101 let run _lock handle ~unit_header ~priv:_ = 102 + run_shared_startup handle; 84 103 List.iter (fun cu -> 85 - try ndl_run handle cu 104 + try ndl_run handle.ndl_handle cu 86 105 with exn -> 87 106 Printexc.raise_with_backtrace 88 107 (DT.Error (Library's_module_initializers_failed exn)) ··· 90 109 (Unit_header.defined_symbols unit_header) 91 110 92 111 let load ~filename ~priv = 93 - let handle, header = 112 + let ndl_handle, header = 94 113 try ndl_open filename (not priv) 95 114 with exn -> raise (DT.Error (Cannot_open_dynamic_library exn)) 96 115 in 97 116 if header.dynu_magic <> Config.cmxs_magic_number then begin 117 + ndl_close ndl_handle; 98 118 raise (DT.Error (Not_a_bytecode_file filename)) 99 119 end; 100 120 let syms = 101 121 "_shared_startup" :: 102 122 List.concat_map Unit_header.defined_symbols header.dynu_units 103 123 in 104 - try 105 - ndl_register handle (Array.of_list syms); 106 - handle, header.dynu_units 107 - with exn -> raise (DT.Error (Cannot_open_dynamic_library exn)) 124 + let handle = { ndl_handle; initialized = Not_yet_initialized syms } in 125 + handle, header.dynu_units 108 126 109 127 let unsafe_get_global_value ~bytecode_or_asm_symbol = 110 128 match ndl_loadsym bytecode_or_asm_symbol with 111 129 | exception _ -> None 112 130 | obj -> Some obj 113 131 114 - let finish _handle = () 132 + let finish handle = 133 + match handle.initialized with 134 + | Initialized -> () 135 + | Not_yet_initialized _ -> ndl_close handle.ndl_handle 115 136 end 116 137 117 138 include DC.Make (Native)
+14 -14
testsuite/tests/backtrace/backtrace_dynlink.flambda.reference
··· 1 1 Raised by primitive operation at Backtrace_dynlink_plugin in file "backtrace_dynlink_plugin.ml", line 6, characters 13-38 2 - Called from Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 85, characters 12-29 2 + Called from Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 104, characters 12-40 3 3 Called from Stdlib__List.iter in file "list.ml" (inlined), line 114, characters 12-15 4 - Called from Dynlink.Native.run in file "otherlibs/dynlink/native/dynlink.ml", lines 84-90, characters 4-47 5 - Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 356, characters 11-54 4 + Called from Dynlink.Native.run in file "otherlibs/dynlink/native/dynlink.ml", lines 103-109, characters 4-47 5 + Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 355, characters 11-54 6 6 Called from Stdlib__List.iter in file "list.ml" (inlined), line 114, characters 12-15 7 - Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml" (inlined), lines 352-361, characters 6-13 7 + Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml" (inlined), lines 351-360, characters 6-13 8 8 Called from Stdlib__Fun.protect in file "fun.ml" (inlined), line 34, characters 8-15 9 - Called from Dynlink_common.Make.load in file "otherlibs/dynlink/dynlink_common.ml", lines 345-362, characters 4-7 10 - Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 364, characters 26-45 9 + Called from Dynlink_common.Make.load in file "otherlibs/dynlink/dynlink_common.ml", lines 345-361, characters 4-7 10 + Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 363, characters 26-45 11 11 Called from Backtrace_dynlink in file "backtrace_dynlink.ml", line 39, characters 4-52 12 12 execution of module initializers in the shared library failed: Failure("SUCCESS") 13 - Raised by primitive operation at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 85, characters 12-29 14 - Re-raised at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", lines 87-89, characters 10-43 13 + Raised by primitive operation at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 104, characters 12-40 14 + Re-raised at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", lines 106-108, characters 10-43 15 15 Called from Stdlib__List.iter in file "list.ml" (inlined), line 114, characters 12-15 16 - Called from Dynlink.Native.run in file "otherlibs/dynlink/native/dynlink.ml", lines 84-90, characters 4-47 17 - Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 356, characters 11-54 16 + Called from Dynlink.Native.run in file "otherlibs/dynlink/native/dynlink.ml", lines 103-109, characters 4-47 17 + Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 355, characters 11-54 18 18 Called from Stdlib__List.iter in file "list.ml" (inlined), line 114, characters 12-15 19 - Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml" (inlined), lines 352-361, characters 6-13 19 + Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml" (inlined), lines 351-360, characters 6-13 20 20 Called from Stdlib__Fun.protect in file "fun.ml" (inlined), line 34, characters 8-15 21 - Called from Dynlink_common.Make.load in file "otherlibs/dynlink/dynlink_common.ml", lines 345-362, characters 4-7 21 + Called from Dynlink_common.Make.load in file "otherlibs/dynlink/dynlink_common.ml", lines 345-361, characters 4-7 22 22 Re-raised at Stdlib__Fun.protect in file "fun.ml" (inlined), line 39, characters 6-52 23 - Called from Dynlink_common.Make.load in file "otherlibs/dynlink/dynlink_common.ml", lines 345-362, characters 4-7 24 - Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 364, characters 26-45 23 + Called from Dynlink_common.Make.load in file "otherlibs/dynlink/dynlink_common.ml", lines 345-361, characters 4-7 24 + Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 363, characters 26-45 25 25 Called from Backtrace_dynlink in file "backtrace_dynlink.ml", line 39, characters 4-52
+7 -7
testsuite/tests/backtrace/backtrace_dynlink.reference
··· 1 1 Raised by primitive operation at Backtrace_dynlink_plugin in file "backtrace_dynlink_plugin.ml", line 6, characters 13-38 2 - Called from Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 85, characters 12-29 2 + Called from Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 104, characters 12-40 3 3 Called from Stdlib__List.iter in file "list.ml", line 114, characters 12-15 4 - Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 356, characters 11-54 4 + Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 355, characters 11-54 5 5 Called from Stdlib__List.iter in file "list.ml", line 114, characters 12-15 6 6 Called from Stdlib__Fun.protect in file "fun.ml", line 34, characters 8-15 7 - Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 364, characters 26-45 7 + Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 363, characters 26-45 8 8 Called from Backtrace_dynlink in file "backtrace_dynlink.ml", line 39, characters 4-52 9 9 execution of module initializers in the shared library failed: Failure("SUCCESS") 10 - Raised by primitive operation at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 85, characters 12-29 11 - Re-raised at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", lines 87-89, characters 10-43 10 + Raised by primitive operation at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 104, characters 12-40 11 + Re-raised at Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", lines 106-108, characters 10-43 12 12 Called from Stdlib__List.iter in file "list.ml", line 114, characters 12-15 13 - Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 356, characters 11-54 13 + Called from Dynlink_common.Make.load.(fun) in file "otherlibs/dynlink/dynlink_common.ml", line 355, characters 11-54 14 14 Called from Stdlib__List.iter in file "list.ml", line 114, characters 12-15 15 15 Called from Stdlib__Fun.protect in file "fun.ml", line 34, characters 8-15 16 16 Re-raised at Stdlib__Fun.protect in file "fun.ml", line 39, characters 6-52 17 - Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 364, characters 26-45 17 + Called from Dynlink_common.Make.loadfile in file "otherlibs/dynlink/dynlink_common.ml" (inlined), line 363, characters 26-45 18 18 Called from Backtrace_dynlink in file "backtrace_dynlink.ml", line 39, characters 4-52