Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit declaration of C primitives in runtime headers #9919

Merged
merged 3 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes
Expand Up @@ -42,6 +42,10 @@ Working version
Leroy and Damien Doligez, benchmarking by Shubham Kumar and KC
Sivaramakrishnan)

- #9919: Introduce caml_record_backtraces and update Interfacing with C to
refer to it (previous instruction to use caml_record_backtrace primitive was
not possible without defining CAML_INTERNALS)

- #10102: Ignore PROFINFO_WIDTH if WITH_PROFINFO is not defined (technically
a breaking change if the configuration system was being abused before).
(David Allsopp, review by Xavier Leroy)
Expand Down
5 changes: 3 additions & 2 deletions bytecomp/bytelink.ml
Expand Up @@ -473,7 +473,8 @@ let link_bytecode_as_c tolink outfile with_main =
\nextern \"C\" {\
\n#endif\
\n#include <caml/mlvalues.h>\
\n#include <caml/startup.h>\n";
\n#include <caml/startup.h>\
\n#include <caml/sys.h>\n";
output_string outchan "static int caml_code[] = {\n";
Symtable.init();
clear_crc_interfaces ();
Expand Down Expand Up @@ -516,7 +517,7 @@ let link_bytecode_as_c tolink outfile with_main =
\n caml_sections, sizeof(caml_sections),\
\n /* pooling */ 0,\
\n argv);\
\n caml_sys_exit(Val_int(0));\
\n caml_do_exit(0);\
\n return 0; /* not reached */\
\n}\n"
end else begin
Expand Down
3 changes: 2 additions & 1 deletion manual/src/cmds/intf-c.etex
Expand Up @@ -1740,7 +1740,8 @@ information is available, but the backtrace mechanism needs to be
turned on programmatically. This can be achieved from the OCaml side
by calling "Printexc.record_backtrace true" in the initialization of
one of the OCaml modules. This can also be achieved from the C side
by calling "caml_record_backtrace(Val_int(1));" in the OCaml-C glue code.
by calling "caml_record_backtraces(1);" in the OCaml-C glue code.
("caml_record_backtraces" is declared in "backtrace.h")

\paragraph{Unloading the runtime.}

Expand Down
10 changes: 7 additions & 3 deletions runtime/backtrace.c
Expand Up @@ -35,10 +35,8 @@ void caml_init_backtrace(void)
}

/* Start or stop the backtrace machinery */
CAMLprim value caml_record_backtrace(value vflag)
CAMLexport void caml_record_backtraces(int flag)
{
int flag = Int_val(vflag);

if (flag != Caml_state->backtrace_active) {
Caml_state->backtrace_active = flag;
Caml_state->backtrace_pos = 0;
Expand All @@ -49,6 +47,12 @@ CAMLprim value caml_record_backtrace(value vflag)
Caml_state->backtrace_buffer). So we don't have to allocate it here.
*/
}
return;
}

CAMLprim value caml_record_backtrace(value flag)
{
caml_record_backtraces(Int_val(flag));
return Val_unit;
}

Expand Down
25 changes: 13 additions & 12 deletions runtime/caml/backtrace.h
Expand Up @@ -16,9 +16,19 @@
#ifndef CAML_BACKTRACE_H
#define CAML_BACKTRACE_H

#include "mlvalues.h"

/* [caml_record_backtraces] controls backtrace recording.
* This function can be called at runtime by user-code, or during
* initialization if backtraces were requested.
*
* It might be called before GC initialization, so it shouldn't do OCaml
* allocation.
*/
CAMLextern void caml_record_backtraces(int);

#ifdef CAML_INTERNALS

#include "mlvalues.h"
#include "exec.h"

/* Runtime support for backtrace generation.
Expand Down Expand Up @@ -52,7 +62,8 @@
* OCaml values of algebraic data-type [Printexc.backtrace_slot]
*/
/* [Caml_state->backtrace_active] is non zero iff backtraces are recorded.
* This variable must be changed with [caml_record_backtrace].
* This variable must be changed with [caml_record_backtrace] in OCaml or
* [caml_record_backtraces] in C.
*/
#define caml_backtrace_active (Caml_state_field(backtrace_active))
/* The [Caml_state->backtrace_buffer] and [Caml_state->backtrace_last_exn]
Expand Down Expand Up @@ -89,16 +100,6 @@
runtimes for raise.
*/

/* [caml_record_backtrace] toggle backtrace recording on and off.
* This function can be called at runtime by user-code, or during
* initialization if backtraces were requested.
*
* It might be called before GC initialization, so it shouldn't do OCaml
* allocation.
*/
CAMLextern value caml_record_backtrace(value vflag);


#ifndef NATIVE_CODE

/* Path to the file containing debug information, if any, or NULL. */
Expand Down
3 changes: 0 additions & 3 deletions runtime/caml/compatibility.h
Expand Up @@ -217,8 +217,6 @@
#define page_table caml_page_table

/* **** md5.c */
#define md5_string caml_md5_string
#define md5_chan caml_md5_chan
#define MD5Init caml_MD5Init
#define MD5Update caml_MD5Update
#define MD5Final caml_MD5Final
Expand Down Expand Up @@ -289,7 +287,6 @@

/* **** sys.c */
#define sys_error caml_sys_error
#define sys_exit caml_sys_exit

/* **** terminfo.c */

Expand Down
1 change: 0 additions & 1 deletion runtime/caml/finalise.h
Expand Up @@ -28,7 +28,6 @@ void caml_final_invert_finalisable_values (void);
void caml_final_oldify_young_roots (void);
void caml_final_empty_young (void);
void caml_final_update_minor_roots(void);
value caml_final_register (value f, value v);
void caml_final_invariant_check(void);

#endif /* CAML_INTERNALS */
Expand Down
2 changes: 0 additions & 2 deletions runtime/caml/gc_ctrl.h
Expand Up @@ -49,8 +49,6 @@ void caml_init_gc (uintnat minor_size, uintnat major_size, uintnat major_incr,
uintnat policy);


CAMLextern value caml_gc_stat(value v);

#ifdef DEBUG
void caml_heap_check (void);
#endif
Expand Down
2 changes: 0 additions & 2 deletions runtime/caml/md5.h
Expand Up @@ -23,8 +23,6 @@
#include "mlvalues.h"
#include "io.h"

CAMLextern value caml_md5_string (value str, value ofs, value len);
CAMLextern value caml_md5_chan (value vchan, value len);
CAMLextern void caml_md5_block(unsigned char digest[16],
void * data, uintnat len);

Expand Down
4 changes: 1 addition & 3 deletions runtime/caml/sys.h
Expand Up @@ -38,11 +38,9 @@ CAMLextern double caml_sys_time_unboxed(value);
CAMLextern void caml_sys_init (char_os * exe_name, char_os ** argv);

CAMLnoreturn_start
CAMLextern value caml_sys_exit (value)
CAMLextern void caml_do_exit (int)
CAMLnoreturn_end;

CAMLextern value caml_sys_get_argv(value unit);

extern char_os * caml_exe_name;

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion runtime/main.c
Expand Up @@ -39,6 +39,6 @@ int main(int argc, char **argv)
#endif

caml_main(argv);
caml_sys_exit(Val_int(0));
caml_do_exit(0);
return 0; /* not reached */
}
3 changes: 1 addition & 2 deletions runtime/startup_aux.c
Expand Up @@ -111,8 +111,7 @@ void caml_parse_ocamlrunparam(void)
while (*opt != '\0'){
switch (*opt++){
case 'a': scanmult (opt, &caml_init_policy); break;
case 'b': scanmult (opt, &p); caml_record_backtrace(Val_int (p));
break;
case 'b': scanmult (opt, &p); caml_record_backtraces(p); break;
case 'c': scanmult (opt, &p); caml_cleanup_on_exit = (p != 0); break;
case 'h': scanmult (opt, &caml_init_heap_wsz); break;
case 'H': scanmult (opt, &caml_use_huge_pages); break;
Expand Down
2 changes: 1 addition & 1 deletion runtime/startup_byt.c
Expand Up @@ -321,7 +321,7 @@ static int parse_command_line(char_os **argv)
exit(0);
break;
case 'b':
caml_record_backtrace(Val_true);
caml_record_backtraces(1);
break;
case 'I':
if (argv[i + 1] != NULL) {
Expand Down
9 changes: 6 additions & 3 deletions runtime/sys.c
Expand Up @@ -113,10 +113,8 @@ static void caml_sys_check_path(value name)
}
}

CAMLprim value caml_sys_exit(value retcode_v)
CAMLexport void caml_do_exit(int retcode)
{
int retcode = Int_val(retcode_v);

if ((caml_verb_gc & 0x400) != 0) {
/* cf caml_gc_counters */
double minwords = Caml_state->stat_minor_words
Expand Down Expand Up @@ -171,6 +169,11 @@ CAMLprim value caml_sys_exit(value retcode_v)
exit(retcode);
}

CAMLprim value caml_sys_exit(value retcode)
{
caml_do_exit(Int_val(retcode));
}

#ifndef O_BINARY
#define O_BINARY 0
#endif
Expand Down
2 changes: 1 addition & 1 deletion runtime/win32.c
Expand Up @@ -450,7 +450,7 @@ void caml_signal_thread(void * lpParam)
char iobuf[2];
/* This shall always return a single character */
ret = ReadFile(h, iobuf, 1, &numread, NULL);
if (!ret || numread != 1) caml_sys_exit(Val_int(2));
if (!ret || numread != 1) caml_do_exit(2);
switch (iobuf[0]) {
case 'C':
caml_record_signal(SIGINT);
Expand Down