From 2aed76fc944dc9c4517ec1f9209c39d9a1ae78e4 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 14 Dec 2022 23:55:18 +0000 Subject: [PATCH 1/4] Fix GHashTableIter layout for CHERI targets Last field needs to be pointer-size to match GHashTableIter. This happened to work for most architecture due to alignment padding/pointer size, but for CHERI targets with 128-bit pointers RealIter ends up being smaller than GHashTableIter. Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842 --- glib/ghash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glib/ghash.c b/glib/ghash.c index 132e3ac5e..eafb37bb3 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -292,7 +292,7 @@ typedef struct gpointer dummy2; gint position; gboolean dummy3; - gint version; + gintptr version; } RealIter; G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter)); From 5d8ecac1ec8d00c08b77ea25db0a9858e95e8426 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 14 Dec 2022 23:55:18 +0000 Subject: [PATCH 2/4] Make GVariantBuilder and GVariantDict layout compatible with CHERI Use guinptr instead if gsize. While this increases the structure size for Morello, it is still only 256 bytes which should not matter for a type where we expect few instances to exist at any given time. This does not increase the size for existing architectures since gsize is the same as guintptr for all of them. Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842 --- glib/gvariant.c | 4 ++-- glib/gvariant.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/glib/gvariant.c b/glib/gvariant.c index cd4c21ced..f76374915 100644 --- a/glib/gvariant.c +++ b/glib/gvariant.c @@ -3220,7 +3220,7 @@ struct heap_builder /* Just to make sure that by adding a union to GVariantBuilder, we * didn't accidentally change ABI. */ -G_STATIC_ASSERT (sizeof (GVariantBuilder) == sizeof (gsize[16])); +G_STATIC_ASSERT (sizeof (GVariantBuilder) == sizeof (guintptr[16])); static gboolean ensure_valid_builder (GVariantBuilder *builder) @@ -3907,7 +3907,7 @@ struct heap_dict /* Just to make sure that by adding a union to GVariantDict, we didn't * accidentally change ABI. */ -G_STATIC_ASSERT (sizeof (GVariantDict) == sizeof (gsize[16])); +G_STATIC_ASSERT (sizeof (GVariantDict) == sizeof (guintptr[16])); static gboolean ensure_valid_dict (GVariantDict *dict) diff --git a/glib/gvariant.h b/glib/gvariant.h index e7087a1f6..ac05d71d4 100644 --- a/glib/gvariant.h +++ b/glib/gvariant.h @@ -304,9 +304,9 @@ struct _GVariantBuilder { struct { gsize partial_magic; const GVariantType *type; - gsize y[14]; + guintptr y[14]; } s; - gsize x[16]; + guintptr x[16]; } u; }; @@ -453,9 +453,9 @@ struct _GVariantDict { struct { GVariant *asv; gsize partial_magic; - gsize y[14]; + guintptr y[14]; } s; - gsize x[16]; + guintptr x[16]; } u; }; From 147777e342d20b991e9370d76ac01e26625f11ad Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 14 Dec 2022 23:55:18 +0000 Subject: [PATCH 3/4] GHash: Don't use SMALL_ENTRY_SIZE for CHERI The code for SMALL_ENTRY_SIZE assumes pointers are no larger than 8 bytes, so instead of trying to make it work disable the optimization for now. Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842 Co-authored-by: Graeme Jenkinson --- glib/ghash.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/glib/ghash.c b/glib/ghash.c index eafb37bb3..500d503c3 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -251,7 +251,8 @@ #define BIG_ENTRY_SIZE (SIZEOF_VOID_P) #define SMALL_ENTRY_SIZE (SIZEOF_INT) -#if SMALL_ENTRY_SIZE < BIG_ENTRY_SIZE +/* NB: The USE_SMALL_ARRAYS code assumes pointers are at most 8 bytes. */ +#if SMALL_ENTRY_SIZE < BIG_ENTRY_SIZE && BIG_ENTRY_SIZE <= 8 # define USE_SMALL_ARRAYS #endif From 9f8edb1e3f4b97555871a1c50d3de22d99c2ad3e Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 14 Dec 2022 23:55:20 +0000 Subject: [PATCH 4/4] GVariant: ensure GVariantIter alignment for CHERI systems For CHERI-enabled architectures such as Arm Morello we have to align the GVariantIter to pointer alignment (16 bytes for Morello) but defining the opaque struct as 16 gsize elements only aligns to 8 bytes. This does not change the layout for existing architecutre since there gsize is the same as guintptr. Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842 --- glib/gvariant.c | 2 ++ glib/gvariant.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/glib/gvariant.c b/glib/gvariant.c index f76374915..770b0418f 100644 --- a/glib/gvariant.c +++ b/glib/gvariant.c @@ -2951,6 +2951,8 @@ struct heap_iter gsize magic; }; +G_STATIC_ASSERT (sizeof (struct heap_iter) <= sizeof (GVariantIter)); + #define GVSI(i) ((struct stack_iter *) (i)) #define GVHI(i) ((struct heap_iter *) (i)) #define GVSI_MAGIC ((gsize) 3579507750u) diff --git a/glib/gvariant.h b/glib/gvariant.h index ac05d71d4..bdc37951d 100644 --- a/glib/gvariant.h +++ b/glib/gvariant.h @@ -270,7 +270,7 @@ GVariant * g_variant_new_from_data (const G typedef struct _GVariantIter GVariantIter; struct _GVariantIter { /*< private >*/ - gsize x[16]; + guintptr x[16]; }; GLIB_AVAILABLE_IN_ALL