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

caml_leave_blocking section and errno corruption #5982

Closed
vicuna opened this issue Apr 12, 2013 · 2 comments
Closed

caml_leave_blocking section and errno corruption #5982

vicuna opened this issue Apr 12, 2013 · 2 comments
Assignees
Milestone

Comments

@vicuna
Copy link

@vicuna vicuna commented Apr 12, 2013

Original bug ID: 5982
Reporter: @diml
Assigned to: @diml
Status: closed (set by @xavierleroy on 2015-12-11T18:18:54Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.01.0+dev
Target version: 4.02.0+dev
Fixed in version: 4.01.0+dev
Category: runtime system and C interface
Monitored by: @ygrek @hcarty

Bug description

This is a widespread idiom for writing C stubs:

enter_blocking_section();
retcode = select(maxfd + 1, &read, &write, &except, tvp);
leave_blocking_section();
if (retcode == -1) uerror("select", Nothing);

Here [uerror] uses the global variable [errno]. The problem is that [leave_blocking_section] can run arbitrary code and so modify [errno]. It can run signal handlers for instance. Attached is an example of program where the call to select is expected to fail with EINTR but instead fails with EROFS.

Obviously bindings should be written this way:

enter_blocking_section();
retcode = select(maxfd + 1, &read, &write, &except, tvp);
saved_errno = errno;
leave_blocking_section();
if (retcode == -1) unix_error(saved_errno, "select", Nothing);

But since this is very common I propose that [leave_blocking_section] saves and restores [errno].

Additional information

let () =
(* Force initialization of the thread library. This modify
[caml_try_leave_blocking_section_hook] so that all
signals are executed by [caml_leave_blocking_section]
and never asynchronously. *)
ignore (Thread.self ());

Sys.set_signal Sys.sigalrm
(Sys.Signal_handle (fun _ ->
try
(* This will modify [errno]. *)
ignore (Unix.openfile "/etc/passwd" [Unix.O_WRONLY] 0)
with _ ->
()));

ignore (Unix.alarm 1);
try
ignore (Unix.select [] [] [] (-1.0))
with exn ->
prerr_endline (Printexc.to_string exn);
exit 2

@vicuna
Copy link
Author

@vicuna vicuna commented Apr 29, 2013

Comment author: @diml

errno should also be saved in handle_signal since it can be executed while the runtime system lock is released and this could break the C code. It seems to be common practice to save errno in signal handlers that can modify it.

@vicuna
Copy link
Author

@vicuna vicuna commented May 14, 2013

Comment author: @diml

Fixed in commit 13667 and 13668.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants