Skip to content

Commit

Permalink
Lock channels before doing I/O
Browse files Browse the repository at this point in the history
The debugger's use of channels doesn't support locking, but it
doesn't work on threaded programs anyway.
  • Loading branch information
stedolan committed Jul 27, 2020
1 parent ba2b986 commit e678885
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ocamltest/run_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ static void logToChannel(void *voidchannel, const char *fmt, va_list ap)
if (text == NULL) return;
if (vsnprintf(text, length, fmt, ap) != length) goto end;
}
Lock(channel);
caml_putblock(channel, text, length);
caml_flush(channel);
Unlock(channel);
end:
free(text);
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/backtrace_byt.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ static void read_main_debug_info(struct debug_info *di)
if (caml_seek_optional_section(fd, &trail, "DBUG") != -1) {
chan = caml_open_descriptor_in(fd);

Lock(chan);
num_events = caml_getword(chan);
events = caml_alloc(num_events, 0);

Expand All @@ -401,6 +402,7 @@ static void read_main_debug_info(struct debug_info *di)
/* Record event list */
Store_field(events, i, evl);
}
Unlock(chan);

caml_close_channel(chan);

Expand Down
2 changes: 1 addition & 1 deletion runtime/caml/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ enum {
*/

/* Functions and macros that can be called from C. Take arguments of
type struct channel *. No locking is performed. */
type struct channel *. The channel must be locked before calling these. */

#define caml_putch(channel, ch) do{ \
if ((channel)->curr >= (channel)->end) caml_flush_partial(channel); \
Expand Down
6 changes: 6 additions & 0 deletions runtime/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ static void open_connection(void)
#endif
dbg_in = caml_open_descriptor_in(dbg_socket);
dbg_out = caml_open_descriptor_out(dbg_socket);
/* The code in this file does not bracket channel I/O operations with
Lock and Unlock, so fail if those are not no-ops. */
if (caml_channel_mutex_lock != NULL ||
caml_channel_mutex_unlock != NULL ||
caml_channel_mutex_unlock_exn != NULL)
caml_fatal_error("debugger does not support channel locks");
if (!caml_debugger_in_use) caml_putword(dbg_out, -1); /* first connection */
#ifdef _WIN32
caml_putword(dbg_out, _getpid());
Expand Down
2 changes: 2 additions & 0 deletions runtime/startup_byt.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ CAMLexport void caml_main(char_os **argv)
/* Load the globals */
caml_seek_section(fd, &trail, "DATA");
chan = caml_open_descriptor_in(fd);
Lock(chan);
caml_global_data = caml_input_val(chan);
Unlock(chan);
caml_close_channel(chan); /* this also closes fd */
caml_stat_free(trail.section);
/* Ensure that the globals are in the major heap. */
Expand Down

0 comments on commit e678885

Please sign in to comment.