From d2bbd69fd516100c379c7a062e15c0546b82b510 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 14 Dec 2022 23:55:19 +0000 Subject: [PATCH] Allow using msort_r CHERI-enabled architectures If we are sorting something that is a multiple of sizeof(void*), we have to ensure that we swap one pointer at a time since swapping using sub-pointer-size stores invalidate the pointers (pointers have a hidden validity tags that is invalidated when performing non-monotonic operations such as storing only part of the pointers). While touching this code also use G_ALIGNOF() instead of a macro that is generated at configure time. Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842 --- glib/gqsort.c | 18 +++++++++--------- meson.build | 4 ---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/glib/gqsort.c b/glib/gqsort.c index a5060e403..a0edcd4d9 100644 --- a/glib/gqsort.c +++ b/glib/gqsort.c @@ -128,23 +128,23 @@ msort_with_tmp (const struct msort_param *p, void *b, size_t n) case 2: while (n1 > 0 && n2 > 0) { - unsigned long *tmpl = (unsigned long *) tmp; - unsigned long *bl; + guintptr *tmpl = (guintptr *) tmp; + guintptr *bl; tmp += s; if ((*cmp) (b1, b2, arg) <= 0) { - bl = (unsigned long *) b1; + bl = (guintptr *) b1; b1 += s; --n1; } else { - bl = (unsigned long *) b2; + bl = (guintptr *) b2; b2 += s; --n2; } - while (tmpl < (unsigned long *) tmp) + while (tmpl < (guintptr *) tmp) *tmpl++ = *bl++; } break; @@ -265,15 +265,15 @@ msort_r (void *b, size_t n, size_t s, GCompareDataFunc cmp, void *arg) else { if ((s & (sizeof (guint32) - 1)) == 0 - && (guintptr) b % ALIGNOF_GUINT32 == 0) + && (gsize) (guintptr) b % G_ALIGNOF(guint32) == 0) { if (s == sizeof (guint32)) p.var = 0; else if (s == sizeof (guint64) - && (guintptr) b % ALIGNOF_GUINT64 == 0) + && (gsize) (guintptr) b % G_ALIGNOF(guint64) == 0) p.var = 1; - else if ((s & (sizeof (unsigned long) - 1)) == 0 - && (guintptr) b % ALIGNOF_UNSIGNED_LONG == 0) + else if ((s & (sizeof (void *) - 1)) == 0 + && (gsize) (guintptr) b % G_ALIGNOF(void *) == 0) p.var = 2; } msort_with_tmp (&p, b, n); diff --git a/meson.build b/meson.build index 19d8b1777..50ca47b6d 100644 --- a/meson.build +++ b/meson.build @@ -1477,8 +1477,6 @@ long_long_align = cc.alignment('long long') # be 64-bit in C99, and it is available on all supported compilers sizet_align = cc.alignment('size_t') -glib_conf.set('ALIGNOF_UNSIGNED_LONG', long_align) - glib_conf.set('SIZEOF_CHAR', char_size) glib_conf.set('SIZEOF_INT', int_size) glib_conf.set('SIZEOF_SHORT', short_size) @@ -1532,7 +1530,6 @@ glibconfig_conf.set('gint32', gint32) glibconfig_conf.set_quoted('gint32_modifier', gint32_modifier) glibconfig_conf.set_quoted('gint32_format', gint32_format) glibconfig_conf.set_quoted('guint32_format', guint32_format) -glib_conf.set('ALIGNOF_GUINT32', guint32_align) if int_size == 8 gint64 = 'int' @@ -1571,7 +1568,6 @@ glibconfig_conf.set_quoted('gint64_format', gint64_format) glibconfig_conf.set_quoted('guint64_format', guint64_format) glibconfig_conf.set('gint64_constant', gint64_constant) glibconfig_conf.set('guint64_constant', guint64_constant) -glib_conf.set('ALIGNOF_GUINT64', guint64_align) if host_system == 'windows' glibconfig_conf.set('g_pid_type', 'void*')