Skip to content

Commit c89512d

Browse files
authored
flambda-backend: More error checking for natdynlink symbols (#1005)
1 parent 27d68bf commit c89512d

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

asmcomp/asmlink.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ let make_startup_file ~ppf_dump units_list ~crc_interfaces =
273273
List.flatten (List.map (fun (info,_,_) -> info.ui_defines) units_list) in
274274
compile_phrase (Cmm_helpers.entry_point name_list);
275275
let units = List.map (fun (info,_,_) -> info) units_list in
276-
List.iter compile_phrase (Cmm_helpers.generic_functions false units);
276+
List.iter compile_phrase
277+
(Cmm_helpers.emit_preallocated_blocks []
278+
(Cmm_helpers.generic_functions false units));
277279
Array.iteri
278280
(fun i name -> compile_phrase (Cmm_helpers.predef_exception i name))
279281
Runtimedef.builtin_exceptions;
@@ -309,7 +311,8 @@ let make_shared_startup_file ~ppf_dump units =
309311
Compilenv.reset shared_startup_comp_unit;
310312
Emit.begin_assembly ();
311313
List.iter compile_phrase
312-
(Cmm_helpers.generic_functions true (List.map fst units));
314+
(Cmm_helpers.emit_preallocated_blocks []
315+
(Cmm_helpers.generic_functions true (List.map fst units)));
313316
compile_phrase (Cmm_helpers.plugin_header units);
314317
compile_phrase
315318
(Cmm_helpers.global_table (List.map (fun (ui,_) -> ui.ui_unit) units));

runtime/dynlink_nat.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ static void *getsym(void *handle, const char *module, const char *name){
5151
return sym;
5252
}
5353

54+
static void *getsym_exn(void *handle, const char *module, const char *name){
55+
void *sym = getsym(handle, module, name);
56+
if (sym == NULL) {
57+
value msg = caml_alloc_sprintf(
58+
"Dynlink: failed to load module due to missing symbol %s%s",
59+
module, name);
60+
caml_invalid_argument_value(msg);
61+
}
62+
return sym;
63+
}
64+
5465
CAMLprim value caml_natdynlink_getmap(value unit)
5566
{
5667
return caml_input_value_from_block(caml_globals_map, INT_MAX);
@@ -98,40 +109,32 @@ CAMLprim value caml_natdynlink_run(value handle_v, value symbol) {
98109
CAMLlocal1 (result);
99110
void *sym,*sym2;
100111
void* handle = Handle_val(handle_v);
101-
102-
#define optsym(n) getsym(handle,unit,n)
103-
const char *unit;
112+
const char *unit = String_val(symbol);
104113
void (*entrypoint)(void);
105114

106-
unit = String_val(symbol);
107-
108-
sym = optsym("__gc_roots");
115+
sym = getsym_exn(handle, unit, "__gc_roots");
109116
/* [caml_register_dyn_global] can raise, so do it prior to registering
110117
frametables etc. */
111-
if (NULL != sym) caml_register_dyn_global(sym);
118+
caml_register_dyn_global(sym);
112119

113-
sym = optsym("__frametable");
114-
if (NULL != sym) caml_register_frametable(sym);
120+
sym = getsym_exn(handle, unit, "__frametable");
121+
caml_register_frametable(sym);
115122

116-
sym = optsym("__data_begin");
117-
sym2 = optsym("__data_end");
118-
if (NULL != sym && NULL != sym2)
119-
caml_page_table_add(In_static_data, sym, sym2);
123+
sym = getsym_exn(handle, unit, "__data_begin");
124+
sym2 = getsym_exn(handle, unit, "__data_end");
125+
caml_page_table_add(In_static_data, sym, sym2);
120126

121-
sym = optsym("__code_begin");
122-
sym2 = optsym("__code_end");
123-
if (NULL != sym && NULL != sym2)
124-
caml_register_code_fragment((char *) sym, (char *) sym2,
125-
DIGEST_LATER, NULL);
127+
sym = getsym_exn(handle, unit, "__code_begin");
128+
sym2 = getsym_exn(handle, unit, "__code_end");
129+
caml_register_code_fragment((char *) sym, (char *) sym2,
130+
DIGEST_LATER, NULL);
126131

127132
if( caml_natdynlink_hook != NULL ) caml_natdynlink_hook(handle,unit);
128133

129-
entrypoint = optsym("__entry");
134+
entrypoint = getsym(handle, unit, "__entry");
130135
if (NULL != entrypoint) result = caml_callback((value)(&entrypoint), 0);
131136
else result = Val_unit;
132137

133-
#undef optsym
134-
135138
CAMLreturn (result);
136139
}
137140

0 commit comments

Comments
 (0)