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
This commit is contained in:
Alex Richardson 2022-12-14 23:55:19 +00:00
parent eafd19da29
commit d2bbd69fd5
2 changed files with 9 additions and 13 deletions

View File

@ -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);

View File

@ -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*')