This commit is contained in:
Richard W.M. Jones 2023-12-14 09:01:18 +00:00
parent 9044c5f85a
commit a23f99a011
3 changed files with 151 additions and 1 deletions

View File

@ -0,0 +1,106 @@
From db48794fa89547a4799b832331e82b4b8b98f03d Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Wed, 13 Dec 2023 22:32:12 +0000
Subject: [PATCH 1/2] ocaml: Use Gc.finalize instead of a C finalizer
Since OCaml 5.1.1, C finalizers no longer allow calling
caml_enter_blocking_section. They are relatively inflexible compared
to registering an OCaml finalizer (Gc.finalize) to call NBD.close, so
use that instead.
I didn't replace NBD.Buffer which still uses a C finalizer. This one
doesn't call caml_enter_blocking_section.
Suggested-by: Guillaume Munch-Maccagnoni
See: https://github.com/ocaml/ocaml/issues/12820
---
generator/OCaml.ml | 7 ++++++-
ocaml/handle.c | 21 ++++++++-------------
ocaml/nbd-c.h | 3 +--
3 files changed, 15 insertions(+), 16 deletions(-)
diff --git a/generator/OCaml.ml b/generator/OCaml.ml
index 095a31df27..4d8501b518 100644
--- a/generator/OCaml.ml
+++ b/generator/OCaml.ml
@@ -343,9 +343,14 @@ let
type t
-external create : unit -> t = \"nbd_internal_ocaml_nbd_create\"
+external _create : unit -> t = \"nbd_internal_ocaml_nbd_create\"
external close : t -> unit = \"nbd_internal_ocaml_nbd_close\"
+let create () =
+ let nbd = _create () in
+ Gc.finalise close nbd;
+ nbd
+
let with_handle f =
let nbd = create () in
try let r = f nbd in close nbd; r with exn -> close nbd; raise exn
diff --git a/ocaml/handle.c b/ocaml/handle.c
index b3e5a0fc33..0e809c6f0d 100644
--- a/ocaml/handle.c
+++ b/ocaml/handle.c
@@ -32,16 +32,6 @@
#include "nbd-c.h"
-void
-nbd_internal_ocaml_handle_finalize (value hv)
-{
- struct nbd_handle *h = NBD_val (hv);
-
- caml_enter_blocking_section ();
- nbd_close (h);
- caml_leave_blocking_section ();
-}
-
value
nbd_internal_ocaml_nbd_create (value unitv)
{
@@ -61,11 +51,16 @@ value
nbd_internal_ocaml_nbd_close (value hv)
{
CAMLparam1 (hv);
+ struct nbd_handle *h = NBD_val (hv);
- nbd_internal_ocaml_handle_finalize (hv);
+ if (h) {
+ caml_enter_blocking_section ();
+ nbd_close (h);
+ caml_leave_blocking_section ();
- /* So we don't double-free in the finalizer. */
- NBD_val (hv) = NULL;
+ /* So we don't double-free. */
+ NBD_val (hv) = NULL;
+ }
CAMLreturn (Val_unit);
}
diff --git a/ocaml/nbd-c.h b/ocaml/nbd-c.h
index adcdd15aa8..134c3a1608 100644
--- a/ocaml/nbd-c.h
+++ b/ocaml/nbd-c.h
@@ -54,7 +54,6 @@ caml_alloc_initialized_string (mlsize_t len, const char *p)
}
#endif
-extern void nbd_internal_ocaml_handle_finalize (value);
extern void nbd_internal_ocaml_buffer_finalize (value);
extern void nbd_internal_ocaml_raise_error (void) Noreturn;
@@ -72,7 +71,7 @@ extern void nbd_internal_ocaml_exception_in_wrapper (const char *, value);
static struct custom_operations libnbd_custom_operations = {
"libnbd_custom_operations",
- nbd_internal_ocaml_handle_finalize,
+ custom_finalize_default,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
--
2.43.0

View File

@ -0,0 +1,37 @@
From 37997f7e9a694715c764528567e569812fa3066a Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 14 Dec 2023 08:34:56 +0000
Subject: [PATCH 2/2] ocaml: Nullify custom block before releasing runtime lock
Avoids a potential, though if possible then very rare, double free
path.
Suggested-by: Guillaume Munch-Maccagnoni
See: https://github.com/ocaml/ocaml/issues/12820
---
ocaml/handle.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/ocaml/handle.c b/ocaml/handle.c
index 0e809c6f0d..8d8a632fef 100644
--- a/ocaml/handle.c
+++ b/ocaml/handle.c
@@ -54,12 +54,12 @@ nbd_internal_ocaml_nbd_close (value hv)
struct nbd_handle *h = NBD_val (hv);
if (h) {
+ /* So we don't double-free. */
+ NBD_val (hv) = NULL;
+
caml_enter_blocking_section ();
nbd_close (h);
caml_leave_blocking_section ();
-
- /* So we don't double-free. */
- NBD_val (hv) = NULL;
}
CAMLreturn (Val_unit);
--
2.43.0

View File

@ -9,7 +9,7 @@
Name: libnbd Name: libnbd
Version: 1.19.2 Version: 1.19.2
Release: 2%{?dist} Release: 3%{?dist}
Summary: NBD client library in userspace Summary: NBD client library in userspace
License: LGPL-2.0-or-later AND BSD-3-Clause License: LGPL-2.0-or-later AND BSD-3-Clause
@ -25,6 +25,10 @@ Source2: libguestfs.keyring
# Maintainer script which helps with handling patches. # Maintainer script which helps with handling patches.
Source3: copy-patches.sh Source3: copy-patches.sh
# Fixes for https://github.com/ocaml/ocaml/issues/12820
Patch: 0001-ocaml-Use-Gc.finalize-instead-of-a-C-finalizer.patch
Patch: 0002-ocaml-Nullify-custom-block-before-releasing-runtime-.patch
%if 0%{patches_touch_autotools} %if 0%{patches_touch_autotools}
BuildRequires: autoconf, automake, libtool BuildRequires: autoconf, automake, libtool
%endif %endif
@ -375,6 +379,9 @@ make %{?_smp_mflags} check || {
%changelog %changelog
* Thu Dec 14 2023 Richard W.M. Jones <rjones@redhat.com> - 1.19.2-3
- Fixes for https://github.com/ocaml/ocaml/issues/12820
* Tue Dec 12 2023 Richard W.M. Jones <rjones@redhat.com> - 1.19.2-2 * Tue Dec 12 2023 Richard W.M. Jones <rjones@redhat.com> - 1.19.2-2
- OCaml 5.1.1 rebuild for Fedora 40 - OCaml 5.1.1 rebuild for Fedora 40