Skip to contentSkip to conversation

tools/ocaml: Fixes to Xenctrl.domain_getinfo{,list}()

PATCH In review Andrew Cooper 5 participants 15 messages Jul 28 – Jul 30

lore
Share & tools
lore
#0 lore raw

cover tools/ocaml: Fixes to Xenctrl.domain_getinfo{,list}()

For 4.22(?). This fixes a segfault in the Ocaml runtime on ARM.

It's not a regression vs 4.21, but it is deterministic and easy to fix. Technically patch 1 is sufficient to fix the segfault, but producing an unconditional exception is almost as useless.

Andrew Cooper (1):
tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM

Julian Vetter (1):
tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

 tools/ocaml/libs/xc/xenctrl_stubs.c | 37 +++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 10 deletions(-)

base-commit: 75f920bd47a4f59eaaa4596aa3f4e12a447d26d2

Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/2 tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM

The Store_field(result, 16, arch_config) sits inside an ifdef x86, meaning that on other archtiectures the pointer is not filled in. The Ocaml runtime then falls over a NULL pointer (really the Val_unit used to initialise 'result') when the layout in the heap doesn't match the type system.

Rearrange alloc_domaininfo() to avoid this. Similarly to physinfo_arch_caps(), raise an exception if the architecture code hasn't filled in an appropriate tag. Move the setup of arch_domainconfig to be common logic.

In order to simplify the addition of other architectures, remove the arch_config variable (resuing tmp as it's touched exactly once), and rename x86_arch_config to be arch_config so each architecture can fill in a suitable one without needing more local variables.

---
CC: Andrew Cooper <[email protected]>
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>
CC: Julian Vetter <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
 tools/ocaml/libs/xc/xenctrl_stubs.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +14 -9
index 7f6381cdd2fe..441e1d83cfec 100644
@@ -414,7 +414,8 @@ CAMLprim value stub_xc_domain_shutdown(value xch_val, value domid, value reason)
 static value alloc_domaininfo(xc_domaininfo_t * info)
 {
 	CAMLparam0();
-	CAMLlocal5(result, tmp, arch_config, x86_arch_config, emul_list);
+	CAMLlocal4(result, tmp, arch_config, emul_list);
+	int tag = -1;
 	int i;
 
 	result = caml_alloc_tuple(17);
@@ -444,6 +445,9 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 	Store_field(result, 15, tmp);
 
 #if defined(__i386__) || defined(__x86_64__)
+
+	tag = 1; /* tag x86 */
+
 	/*
 	 * emulation_flags: x86_arch_emulation_flags list;
 	 */
@@ -452,16 +456,17 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 		(info->arch_config.emulation_flags);
 
 	/* xen_x86_arch_domainconfig */
-	x86_arch_config = caml_alloc_tuple(1);
-	Store_field(x86_arch_config, 0, emul_list);
+	arch_config = caml_alloc_tuple(1);
+	Field(arch_config, 0) = emul_list;
 
-	/* arch_config: arch_domainconfig */
-	arch_config = caml_alloc_small(1, 1);
-
-	Store_field(arch_config, 0, x86_arch_config);
-
-	Store_field(result, 16, arch_config);
 #endif
+	if (tag < 0)
+		caml_failwith("Unimplemented architecutre in alloc_domaininfo()");
+
+	/* arch_config: arch_domainconfig */
+	tmp = caml_alloc_small(1, tag);
+	Field(tmp, 0) = arch_config;
+	Field(result, 16) = tmp;
 
 	CAMLreturn(result);
 }
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/2] tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM

Quoted 8 lines · Andrew Cooper
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -2
@@ -452,16 +456,17 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 		(info->arch_config.emulation_flags);
 
 	/* xen_x86_arch_domainconfig */
-	x86_arch_config = caml_alloc_tuple(1);
-	Store_field(x86_arch_config, 0, emul_list);
+	arch_config = caml_alloc_tuple(1);
+	Field(arch_config, 0) = emul_list;

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 464

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -6
 
-	/* arch_config: arch_domainconfig */
-	arch_config = caml_alloc_small(1, 1);
-
-	Store_field(arch_config, 0, x86_arch_config);
-
-	Store_field(result, 16, arch_config);
 #endif
+	if (tag < 0)
+		caml_failwith("Unimplemented architecutre in alloc_domaininfo()");

As I now ended up looking here as well (to determine whether this series will want backporting): s/architecutre/architecture/ .

As to backporting: Both patches may want to have Fixes: tags?

Jan

#3 lore raw

Re: [PATCH 1/2] tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM

Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -2
@@ -452,16 +456,17 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 		(info->arch_config.emulation_flags);
 
 	/* xen_x86_arch_domainconfig */
-	x86_arch_config = caml_alloc_tuple(1);
-	Store_field(x86_arch_config, 0, emul_list);
+	arch_config = caml_alloc_tuple(1);
+	Field(arch_config, 0) = emul_list;
 
Quoted 11 lines

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 464

Quoted 9 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -6
-	/* arch_config: arch_domainconfig */
-	arch_config = caml_alloc_small(1, 1);
-
-	Store_field(arch_config, 0, x86_arch_config);
-
-	Store_field(result, 16, arch_config);
 #endif
+	if (tag < 0)
+		caml_failwith("Unimplemented architecutre in alloc_domaininfo()");

As I now ended up looking here as well (to determine whether this series will want backporting): s/architecutre/architecture/ .

Fixed.  There was also a typo in the commit message.

Quoted 1 line

As to backporting: Both patches may want to have Fixes: tags?

Hmm, yes.

Technically, Fixes: 81838c9067ab ("ocaml: fix arm build") was the one which caused the pointer not to be filled in, but it was a build fix for

Fixes 9d683b5e375d ("tools/ocaml: Expose arch_config in domaininfo")

which was the main one intending to produce an arch_config

I guess I should go with both tags.

Patch 2 is interesting as well, because really it's both tags too.  In my main first patch (if it had compiled), we would have ended up handing an xen_x86_arch_domainconfig back when an ARM toolstack asked.

Fixed up locally.

~Andrew

Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM

In reply to Andrew Cooper

Le 28/07/2026 à 17:49, Andrew Cooper a écrit :

Quoted 10 lines

The Store_field(result, 16, arch_config) sits inside an ifdef x86, meaning that on other archtiectures the pointer is not filled in. The Ocaml runtime then falls over a NULL pointer (really the Val_unit used to initialise 'result') when the layout in the heap doesn't match the type system.

Rearrange alloc_domaininfo() to avoid this. Similarly to physinfo_arch_caps(), raise an exception if the architecture code hasn't filled in an appropriate tag. Move the setup of arch_domainconfig to be common logic.

Quoted 7 lines

In order to simplify the addition of other architectures, remove the arch_config variable (resuing tmp as it's touched exactly once), and rename x86_arch_config to be arch_config so each architecture can fill in a suitable one without needing more local variables.

I guess that requires the OCaml user side to be appropriately patched to not get data in the wrong order ?

Teddy

#5 lore raw

Re: [PATCH 1/2] tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM

Quoted 20 lines · Teddy Astie

Le 28/07/2026 à 17:49, Andrew Cooper a écrit :

Quoted 19 lines

The Store_field(result, 16, arch_config) sits inside an ifdef x86, meaning
that on other archtiectures the pointer is not filled in.  The Ocaml runtime
then falls over a NULL pointer (really the Val_unit used to initialise 'result') when the layout in the heap doesn't match the type system.

Rearrange alloc_domaininfo() to avoid this.  Similarly to physinfo_arch_caps(), raise an exception if the architecture code hasn't filled in an appropriate tag.  Move the setup of arch_domainconfig to be common logic.

In order to simplify the addition of other architectures, remove the arch_config variable (resuing tmp as it's touched exactly once), and rename
x86_arch_config to be arch_config so each architecture can fill in a suitable
one without needing more local variables.

Quoted 4 lines
Quoted 2 lines

Thanks.

Quoted 2 lines

I guess that requires the OCaml user side to be appropriately patched to not get data in the wrong order ?

I don't quite understand the question.  There are no changes needed to the OCaml side.  The problem is that this piece of C is not producing an object that the type system describes.

Prior to this patch, Xenctrl.domain_getinfo on ARM produces a malformed object with a missing (NULL-ish) interior pointer.  Attempts to interpret this object in Ocaml code segfault.

With this patch, Xenctrl.domain_getinfo on ARM will unconditionally raise Failure.  Ocaml code won't segfault, but the overall behaviour isn't helpful.

With patch 2, Xenctrl.domain_getinfo on ARM gets you back a good object.

~Andrew

#6 lore raw

PATCH 2/2 tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

In reply to Andrew Cooper

From: Julian Vetter <[email protected]>

Add the missing ARM logic, populating xen_arm_arch_domainconfig from the raw xc_domaininfo_t the same way the x86 branch does for xen_x86_arch_domainconfig.

---
CC: Andrew Cooper <[email protected]>
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>
CC: Julian Vetter <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
 tools/ocaml/libs/xc/xenctrl_stubs.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +14 -2
index 441e1d83cfec..fb983709066f 100644
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
 	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;
+
+#elif defined(__i386__) || defined(__x86_64__)
+
+        tag = 1; /* tag x86 */
 
 	/*
 	 * emulation_flags: x86_arch_emulation_flags list;
Signature · 1 line
2.39.5
#7 lore raw

Re: [PATCH 2/2] tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

Quoted 16 lines · Andrew Cooper
tools/ocaml/libs/xc/xenctrl_stubs.c +7 -2
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
 	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 464

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +7 -0
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;
+
+#elif defined(__i386__) || defined(__x86_64__)
+
+        tag = 1; /* tag x86 */
 
 	/*
 	 * emulation_flags: x86_arch_emulation_flags list;

I know nothing about style rules or conventions here, but simply from looking at the diff I came to wonder: Is this mix of tab vs space indentation deliberate?

Jan

#8 lore raw

Re: [PATCH 2/2] tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

Quoted 25 lines · Jan Beulich
Quoted 24 lines · Andrew Cooper
tools/ocaml/libs/xc/xenctrl_stubs.c +14 -2
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
 	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;
+
+#elif defined(__i386__) || defined(__x86_64__)
+
+        tag = 1; /* tag x86 */
 
Quoted 5 lines

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 464

Quoted 2 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +0 -0
 	/*
 	 * emulation_flags: x86_arch_emulation_flags list;

I know nothing about style rules or conventions here, but simply from looking at the diff I came to wonder: Is this mix of tab vs space indentation deliberate?

No, unintentional.  I'll fix up locally.

~Andrew

Reviewed-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

In reply to Andrew Cooper

Le 28/07/2026 à 17:50, Andrew Cooper a écrit :

Quoted 8 lines

From: Julian Vetter <[email protected]>

Add the missing ARM logic, populating xen_arm_arch_domainconfig from the raw xc_domaininfo_t the same way the x86 branch does for xen_x86_arch_domainconfig.

Quoted 28 lines

---
CC: Andrew Cooper <[email protected]>
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>
CC: Julian Vetter <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
  tools/ocaml/libs/xc/xenctrl_stubs.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +7 -2
index 441e1d83cfec..fb983709066f 100644
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
  	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 464

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +7 -0
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;
+
+#elif defined(__i386__) || defined(__x86_64__)
+
+        tag = 1; /* tag x86 */
 
  	/*
  	 * emulation_flags: x86_arch_emulation_flags list;

Teddy

#10 lore raw

Re: [PATCH 2/2] tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

In reply to Andrew Cooper
Quoted 29 lines · Andrew Cooper

From: Julian Vetter <[email protected]>

Add the missing ARM logic, populating xen_arm_arch_domainconfig from the raw xc_domaininfo_t the same way the x86 branch does for xen_x86_arch_domainconfig.

---
CC: Andrew Cooper <[email protected]>
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>
CC: Julian Vetter <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
  tools/ocaml/libs/xc/xenctrl_stubs.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index 441e1d83cfec..fb983709066f 100644
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
  	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 457

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +9 -1
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;

Shouldn't the Field() be a Store_field(arch_config, 2, tmp);, because the caml_copy_int32 makes an allocation on the minor heap to allow OCAML's GC to keep track of this pointer if ever the arch_config is promoted?

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 464

Quoted 7 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +4 -0
+
+#elif defined(__i386__) || defined(__x86_64__)
+
+        tag = 1; /* tag x86 */
 
  	/*
  	 * emulation_flags: x86_arch_emulation_flags list;

--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#11 lore raw

Re: [PATCH 2/2] tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

Quoted 31 lines · Julian Vetter
Quoted 29 lines · Andrew Cooper

From: Julian Vetter <[email protected]>

Add the missing ARM logic, populating xen_arm_arch_domainconfig from the raw xc_domaininfo_t the same way the x86 branch does for xen_x86_arch_domainconfig.

---
CC: Andrew Cooper <[email protected]>
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>
CC: Julian Vetter <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
  tools/ocaml/libs/xc/xenctrl_stubs.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index 441e1d83cfec..fb983709066f 100644
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
  	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 
Quoted 14 lines

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 457

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +9 -1
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;

Shouldn't the Field() be a Store_field(arch_config, 2, tmp);, because the caml_copy_int32 makes an allocation on the minor heap to allow OCAML's GC to keep track of this pointer if ever the arch_config is promoted?

No (ish).  The Ocaml/C manual is subtly wrong in it's advice about this.

Using Store_field() would be safe, but it has an obscene overhead for a scalar store, and caml_modify() does a whole bunch of barriers/atomics which are not necessary on anything which isn't allocated from caml_alloc_shr() (which we don't use at all).

The buggy construct is:

    Field(foo, bar) = caml_{copy,alloc,etc}(baz);

because C does not provide an ordering between evaluation of the Rvalue vs the Lvalue.  Specifically, if the Lvalue were evaluated prior to the function call, then indeed we could store into a stale pointer.

But, using tmp in the way we do:

    tmp = caml_copy_*(baz);
    Field(foo, bar) = tmp;

puts a sequence point after calling caml_copy_*(), and prior to evaluating Field(), so guarantees to do them in the correct order.

In the Store_field() macro, this occurs by taking (val) into a local variable, and not because of how it calls caml_modify().

In a perfect world we'd have a Store_field() that arranged for the intermediate safety without calling caml_modify().

It is also telling that the Ocaml runtime has a total of 15 uses of Store_field, and we've got more than that in this function alone, let alone the rest of our bindings.

I do have a cleanup patch for those as well, once we got this bug sorted.

~Andrew

Reviewed-by Tested-by Tested-by
#12 lore raw

Re: [PATCH 2/2] tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()

Quoted 62 lines · Andrew Cooper
Quoted 31 lines · Julian Vetter
Quoted 29 lines · Andrew Cooper

From: Julian Vetter <[email protected]>

Add the missing ARM logic, populating xen_arm_arch_domainconfig from the raw xc_domaininfo_t the same way the x86 branch does for xen_x86_arch_domainconfig.

---
CC: Andrew Cooper <[email protected]>
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>
CC: Julian Vetter <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
   tools/ocaml/libs/xc/xenctrl_stubs.c | 16 ++++++++++++++--
   1 file changed, 14 insertions(+), 2 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index 441e1d83cfec..fb983709066f 100644
@@ -444,9 +444,21 @@ static value alloc_domaininfo(xc_domaininfo_t * info)
 
   	Store_field(result, 15, tmp);
 
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(__arm__) || defined(__aarch64__)
 
Quoted 14 lines

tools/ocaml/libs/xc/xenctrl_stubs.c static value alloc_domaininfo(xc_domaininfo_t * info) · line 457

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +9 -1
-	tag = 1; /* tag x86 */
+	tag = 0; /* tag ARM */
+
+        /* xen_arm_arch_domainconfig */
+        arch_config = caml_alloc_tuple(3);
+        Field(arch_config, 0) = Val_int(info->arch_config.gic_version);
+        Field(arch_config, 1) = Val_int(info->arch_config.nr_spis);
+
+	tmp = caml_copy_int32(info->arch_config.clock_frequency);
+        Field(arch_config, 2) = tmp;

Shouldn't the Field() be a Store_field(arch_config, 2, tmp);, because the caml_copy_int32 makes an allocation on the minor heap to allow OCAML's GC to keep track of this pointer if ever the arch_config is promoted?

No (ish).  The Ocaml/C manual is subtly wrong in it's advice about this.

Using Store_field() would be safe, but it has an obscene overhead for a scalar store, and caml_modify() does a whole bunch of barriers/atomics which are not necessary on anything which isn't allocated from caml_alloc_shr() (which we don't use at all).

The buggy construct is:

    Field(foo, bar) = caml_{copy,alloc,etc}(baz);

because C does not provide an ordering between evaluation of the Rvalue vs the Lvalue.  Specifically, if the Lvalue were evaluated prior to the function call, then indeed we could store into a stale pointer.

Quoted 8 lines

But, using tmp in the way we do:

    tmp = caml_copy_*(baz);
    Field(foo, bar) = tmp;

puts a sequence point after calling caml_copy_*(), and prior to evaluating Field(), so guarantees to do them in the correct order.

Aha, thank you for the clarification. Indeed the Ocaml/C manual suggested that this might be triggered by any caml_copy_* and not just a caml_alloc_shr().

I've tested this series (both patches applied together) on real Ampere ARM64 hardware (XCP-ng 9.0'ish, based on Xen 4.21.1). The crash in Xenctrl.domain_getinfo{,list} on ARM is gone, and VMs create and boot correctly. So,

Patch 1 ("tools/ocaml: Fix crash in Xenctrl.domain_getinfo{,list} on ARM"):

Patch 2 ("tools/ocaml: Fill arch_config for ARM in domain_getinfo{,list}()"):

Quoted 7 lines

In the Store_field() macro, this occurs by taking (val) into a local variable, and not because of how it calls caml_modify().

In a perfect world we'd have a Store_field() that arranged for the intermediate safety without calling caml_modify().

Quoted 8 lines

It is also telling that the Ocaml runtime has a total of 15 uses of Store_field, and we've got more than that in this function alone, let alone the rest of our bindings.

I do have a cleanup patch for those as well, once we got this bug sorted.

~Andrew

--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#13 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

For 4.22(?). This fixes a segfault in the Ocaml runtime on ARM.

It's not a regression vs 4.21, but it is deterministic and easy to fix. Technically patch 1 is sufficient to fix the segfault, but producing an unconditional exception is almost as useless.

For both patches:

~ Oleksii

#14 lore raw
Quoted 5 lines · Oleksii Kurochko
Quoted 2 lines · Andrew Cooper

For 4.22(?). This fixes a segfault in the Ocaml runtime on ARM.

Quoted 7 lines
Quoted 3 lines

It's not a regression vs 4.21, but it is deterministic and easy to fix. Technically patch 1 is sufficient to fix the segfault, but producing an unconditional exception is almost as useless.

For both patches:

Thanks.  I've managed to get some testing in GitlabCI showing the segfault:

  https://gitlab.com/xen-project/hardware/xen-staging/-/jobs/15583141354

and things working properly with this series in place:

  https://gitlab.com/xen-project/hardware/xen-staging/-/jobs/15583735066

~Andrew