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
Milestone

Comments

@vicuna
Copy link

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 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 commented May 14, 2013

Comment author: @diml

Fixed in commit 13667 and 13668.

@vicuna vicuna closed this as completed Dec 11, 2015
@vicuna vicuna added the stdlib label Mar 14, 2019
@vicuna vicuna added this to the 4.02.0 milestone Mar 14, 2019
@vicuna vicuna assigned ghost Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
edwintorok added a commit to edwintorok/xen that referenced this issue Dec 6, 2022
Use 'uerror' from unixsupport.h to raise an exception containing error.
All the functions here (except for 'xenevtchn_fd') claim to set errno properly
in xenevtchn.h.

Although it may look like errno is already gone due to the call to
'caml_leave_blocking_section' that is how all the Unix bindings are written
and the compiler runtime will ensure that function/macro will save/restore
errno properly, see
ocaml/ocaml#5982
ocaml/ocaml@5de2108
(part of OCaml 4.01.0, and we require 4.02+ so this is safe)

The other argument to 'uerror' is passed as Nothing because that is reserved
for string input arguments to the underlying C library call, and there are none
in this case.

Signed-off-by: Edwin Török <edvin.torok@citrix.com>
edwintorok added a commit to edwintorok/xen that referenced this issue Dec 8, 2022
Use 'uerror' from unixsupport.h to raise an exception containing error.
All the functions here (except for 'xenevtchn_fd') claim to set errno properly
in xenevtchn.h.

Although it may look like errno is already gone due to the call to
'caml_leave_blocking_section' that is how all the Unix bindings are written
and the compiler runtime will ensure that function/macro will save/restore
errno properly, see
ocaml/ocaml#5982
ocaml/ocaml@5de2108
(part of OCaml 4.01.0, and we require 4.02+ so this is safe)

The other argument to 'uerror' is passed as Nothing because that is reserved
for string input arguments to the underlying C library call, and there are none
in this case.

Signed-off-by: Edwin Török <edvin.torok@citrix.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant