From ad572e77802c3c383619fe63a4832b5c75dbea82 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Wed, 9 Oct 2024 12:36:03 -0700 Subject: [PATCH] glib/gutf8: use ifunc to check for valgrind This attempts to use GCC __attribute__((ifunc("resolver_func"))) to check for valgrind early in the process startup so that the proper function is dispatched instead of runtime checks within the function. This should make #3493 less annoying when run under Valgrind. --- glib/gutf8.c | 60 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/glib/gutf8.c b/glib/gutf8.c index f54a0a2be..0f75c6b28 100644 --- a/glib/gutf8.c +++ b/glib/gutf8.c @@ -40,6 +40,7 @@ #include "gtypes.h" #include "gthread.h" #include "glibintl.h" +#include "gvalgrind.h" #define UTF8_COMPUTE(Char, Mask, Len) \ if (Char < 128) \ @@ -1824,6 +1825,43 @@ out: *lenp = len; } +static gboolean +g_utf8_validate_native (const char *str, + gssize max_len, + const char **end) +{ + if (max_len >= 0) + return g_utf8_validate_len (str, max_len, end); + + utf8_verify (&str, NULL); + + if (end != NULL) + *end = str; + + return *str == 0; +} + +#if g_macro__has_attribute(ifunc) && !defined(G_OS_WIN32) +static gboolean +g_utf8_validate_valgrind (const char *str, + gssize max_len, + const char **end) +{ + if (max_len < 0) + max_len = strlen (str); + + return g_utf8_validate_len (str, max_len, end); +} + +static gboolean (*resolve_g_utf8_validate (void)) (const char *, gssize, const char **) +{ + if (RUNNING_ON_VALGRIND) + return g_utf8_validate_valgrind; + else + return g_utf8_validate_native; +} +#endif + /** * g_utf8_validate: * @str: (array length=max_len) (element-type guint8): a pointer to character data @@ -1850,22 +1888,20 @@ out: * * Returns: `TRUE` if the text was valid UTF-8 */ +#if g_macro__has_attribute(no_sanitize_address) + __attribute__((no_sanitize_address)) +#endif gboolean g_utf8_validate (const char *str, - gssize max_len, - const gchar **end) - + gssize max_len, + const gchar **end) +#if g_macro__has_attribute(ifunc) && !defined(G_OS_WIN32) + __attribute__((ifunc ("resolve_g_utf8_validate"))); +#else { - if (max_len >= 0) - return g_utf8_validate_len (str, max_len, end); - - utf8_verify (&str, NULL); - - if (end != NULL) - *end = str; - - return *str == 0; + return g_utf8_validate_native (str, max_len, end); } +#endif /** * g_utf8_validate_len: