Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005982OCamlOCaml runtime systempublic2013-04-12 11:492013-05-31 13:59
Reporterdim 
Assigned Todim 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionfixed 
PlatformOSOS Version
Product Version4.01.0+dev 
Target Version4.02.0+devFixed in Version4.01.0+dev 
Summary0005982: caml_leave_blocking section and errno corruption
DescriptionThis 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 Informationlet () =
  (* 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
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0009224)
dim (developer)
2013-04-29 12:34

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.
(0009284)
dim (developer)
2013-05-14 18:02

Fixed in commit 13667 and 13668.

- Issue History
Date Modified Username Field Change
2013-04-12 11:49 dim New Issue
2013-04-12 11:49 dim Status new => assigned
2013-04-12 11:49 dim Assigned To => dim
2013-04-29 12:34 dim Note Added: 0009224
2013-04-29 16:30 frisch Severity major => minor
2013-04-29 16:31 frisch Target Version => 4.02.0+dev
2013-05-14 17:20 dim Additional Information Updated View Revisions
2013-05-14 18:02 dim Note Added: 0009284
2013-05-14 18:03 dim Status assigned => resolved
2013-05-14 18:04 dim Resolution open => fixed
2013-05-14 18:04 dim Fixed in Version => 4.02.0+dev
2013-05-31 13:59 gasche Fixed in Version 4.02.0+dev => 4.01.0+dev


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker