From patchwork Fri Jan 31 19:10:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 860991 Delivered-To: patch@linaro.org Received: by 2002:adf:fb05:0:b0:385:e875:8a9e with SMTP id c5csp887186wrr; Fri, 31 Jan 2025 11:11:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUYOnyBA0uGjL3JSGvHXokBH8tGqrW6ElStzHRxeHcVQ09KgnTrqTF12/izC4mARQdzbAz5sg==@linaro.org X-Google-Smtp-Source: AGHT+IFURCxjaJ0JF8E2sKq4UqL0wDDX6tdAoOKB5iH5CK529BMwTowDZvffX+AohID9W33mMpY9 X-Received: by 2002:a05:6102:9d4:b0:4b2:5ca3:2b09 with SMTP id ada2fe7eead31-4b9a525d5b6mr12219993137.19.1738350693707; Fri, 31 Jan 2025 11:11:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1738350693; cv=pass; d=google.com; s=arc-20240605; b=AN7iwAQSb6zf1nNdTNThwI5BVem+zQ5UxSCSPOd0AZIn6TvsAXQc6q0qBIxRa6zP+P sCtonId8gOPkhI3MbojE3lO980hjWgeGLBjlVjtf6Ly0pzzuo5URND1zSe6bXDyS+r3g u9q8WtoVTLjFvs90i5VXankqtg/h+YKJyFaUeQci42l6Kp2kZsQJjW78Gh05IO2eHuXI DUMQqphV2bFrDmiI7ED+JXhei6xptljxrYKgrahpKBbtL+nHfWOCA/c5UzVYFcQItoqG YIx/Zox6tPuSEOmcbjhHQkD7IxSI99jrULdNv6g1uGyVkjXK8Spk6OwE89WqMLMBPOSS zEIQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=MLwN5fdJduNDcV9tiJn/vSp0WVCgPpCp+UBiLL55T5E=; fh=VGcb4L0QH2ORepDqfIc66HG9yXriDgcL5cHiPceeAeQ=; b=PEPGn6cHMzVrbUTFuw2yE2nsOVUBX4kKabLxbYmYmhPQ0ArHCLq8WKj2K82AGFPZ7D UOcyrE6d8GwSEPk6mBHuiTzt34JT96Ax5swclEcEX4jAFRzFGU60xoXlPuPxG0u7E7c5 66N/rnkvUyzrJE9B5++aMspg3i8fGcVUkqIX80ELuvBASgRRpSpgDaCMxmF+m+yFXnus EDZj1KcLo4DgBkP0/JaY71583zZhOatPUHG6DlDHwNwPRVLU2uclizsm5rGTp0y0tVN5 n7jtBGD1lHTmT05xtvcX7CYnSKflv9UT9GrQBtg7P2B4ERUihlSnH9iUq6006UP1GGXQ cYRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g9QUYT7Z; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ada2fe7eead31-4b9baa653efsi1087665137.20.2025.01.31.11.11.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jan 2025 11:11:33 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g9QUYT7Z; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D5413858D37 for ; Fri, 31 Jan 2025 19:11:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D5413858D37 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=g9QUYT7Z X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id AFB903858C42 for ; Fri, 31 Jan 2025 19:11:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AFB903858C42 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AFB903858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::635 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738350676; cv=none; b=DvhY4xCitZBdhc4DYtFq7I0T/CzjURrOetEY9mhwIDwZofx7fNVLVPqgwMFbVDtlm0jCpmXsG5ALEMkkL2JTJ4UwZkUs5tYHUycQ7GTIiMpDF1cd8vPMLoz/CnhXZEJXtYu7thapYKOyU0u56/WFxi/9YIU8Asrzoz2DzI580xA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1738350676; c=relaxed/simple; bh=kuYalZxzNETp+ucRQfsEhL0vYz1yj5o/OU0KkynN2fw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Hq5vz1AOVzIFbZAaJRGpxo3++oC+p62ebW9fOIpe3rBZnKYQkFk5cUF9nZeLNDbKLENVwBqxK8fW7thVeLnYnto06uxllzir7IQ3S6hpxfz60j6G2X9gEyyZpkKuwUBsBHCxOI24benNXTlG+f2pO1+pmGQGhbXJTfk/65YX5P8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AFB903858C42 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-21649a7bcdcso42639075ad.1 for ; Fri, 31 Jan 2025 11:11:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1738350670; x=1738955470; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MLwN5fdJduNDcV9tiJn/vSp0WVCgPpCp+UBiLL55T5E=; b=g9QUYT7Z4HbRxuajWPzpGn1ffygadUWnxmeMg9GnRol86RwjkCogu1HYMXCTI9nG8X Y/lIrDgcQeULMofSmsaXA1Fq3dZGaPPxpTAqAFD9PXXipStRZZR/+auHwgUaQsMPHxiS zIsqRw+OQRWnNrakT1sO1f3DTUo0XEMAL1YdgGuy/Z7Cc7KDlDGknXtkJrIsvqC9FKNb bltK1vaJfOyfMU3UxFA0X3xdxIV9TuJnE5bCmJqaAioWKkcamAPPVoyvOi5H0SRlIUDk XHM5bFaTrgEWI0jMjL0bvKe8ibYpjDO/AtE2eMULY8QV4R4gjDehxgTcVYrTG0apBEZP fXsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738350670; x=1738955470; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MLwN5fdJduNDcV9tiJn/vSp0WVCgPpCp+UBiLL55T5E=; b=DOCIzFkzrt/MNeagrG2ROqSwUTlRlziM4jm48ZpuD7AoGMpFhxyPqtyhKBkUnM3U9m BBx5Aeq3OfttMFhuRpa8mfUifEq8gn8ax48GglR0Hsj1P/PMBzMrIh8thD3kpe0judpb tD63taMdX3jrUF83D5oIdNSc6BTIr/n8XtFekvZFdxkLK+Ua/swghgAgtK51C8vWM/8Q 62gnqgLqhavUOdH/jlkOPxocJeiShmZllH7cf3lEiRsAjLS2DsdNJtClOQDgo+gqLgX2 4EAqqvnzQ2oTzwJvMNUzsVwP0fUNn+sruWdwsCOuIeGqmgdWHnmz+JWLcURjFXwbix/T bAHQ== X-Gm-Message-State: AOJu0Ywl2B0h0XJexVWtVDBPVjPNxUkc3AyPeRDrHEhrg6ZzRYhJ8ZFv ymccvvk4qQEeV6M/FljIhFKE7ItGcjM1gQm/M80QgPTv6Jr28jlm0lqxdT5vy2q+VrdEEXZC0Y6 D X-Gm-Gg: ASbGncvnH3oT3MzzX9/YKnTKwTKo1lygLu0SKR7F6rTcCW8x94X/pR39a/plJPLVrzB RS0M2WrbKwopGYm7Z2NHOnnRJvvgcfiJkCQDMFzrKtT7mvN9A6K3BBLU/vOsuwtT15lsg7o2J4r +TbXp8rMT4IMSzt0Ph7efylWz39+qb6F2LhcR0qpiuP/n/+h3iJJZNSm/UpCBIC8s0IdpdXzf+U NZRmS/m1etYYMth/rCoWklMZMQA6ksmk7mRJ9nXr3UC/Hpl2YQiqsiVuEf2EvGg55Qbk5I5h1o8 T9k1Mk9clqSbgqe0hiYgr0A39aH0wg== X-Received: by 2002:a05:6a20:d498:b0:1e1:a932:4a3f with SMTP id adf61e73a8af0-1ed7a5b6710mr20227412637.3.1738350668511; Fri, 31 Jan 2025 11:11:08 -0800 (PST) Received: from ubuntu-vm.. ([177.103.113.118]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72fe69ba4a0sm3769376b3a.102.2025.01.31.11.11.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jan 2025 11:11:07 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Siddhesh Poyarekar , fweimer@redhat.com Subject: [PATCH] assert: Refactor assert/assert_perror Date: Fri, 31 Jan 2025 16:10:47 -0300 Message-ID: <20250131191103.2582519-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org It now calls __libc_message, which contains similar logic. The assert now does not require any dynamic memory allocation, so test-assert2.c is adapted to handle it. It also removes the fxprintf from assert/assert_perror; although it is not 100% backwards-compatible (write message only if there is a file descriptor associated with the stderr) it nows write bytes directly to without going through the wide stream state. Checked on aarch64-linux-gnu. --- assert/assert-perr.c | 22 ++++++-- assert/assert.c | 112 ++++++----------------------------------- assert/test-assert-2.c | 18 ++----- include/stdio.h | 12 +++-- 4 files changed, 46 insertions(+), 118 deletions(-) diff --git a/assert/assert-perr.c b/assert/assert-perr.c index 83f0b3a76f..04f890bcab 100644 --- a/assert/assert-perr.c +++ b/assert/assert-perr.c @@ -15,10 +15,15 @@ License along with the GNU C Library; if not, see . */ +#include <_itoa.h> +#include #include +#include #include #include +#include +extern const char *__progname; /* This function, when passed an error number, a filename, and a line number, prints a message on the standard error stream of the form: @@ -31,8 +36,19 @@ __assert_perror_fail (int errnum, { char errbuf[1024]; - char *e = __strerror_r (errnum, errbuf, sizeof errbuf); - __assert_fail_base (_("%s%s%s:%u: %s%sUnexpected error: %s.\n"), - e, file, line, function); + const char *e = __strerror_r (errnum, errbuf, sizeof errbuf); + + char linebuf[INT_BUFSIZE_BOUND (unsigned int)]; + array_end (linebuf)[-1] = '\0'; + char *linestr = _itoa_word (line, array_end (linebuf) - 1, 10, 0); + + __libc_message (_("%s%s%s:%s: %s%sUnexpected error: %s.\n"), + __progname, + __progname[0] ? ": " : "", + file, + linestr, + function ? function : "", + function ? ": " : "", + e); } libc_hidden_def (__assert_perror_fail) diff --git a/assert/assert.c b/assert/assert.c index d21d76fa62..499a90f4c6 100644 --- a/assert/assert.c +++ b/assert/assert.c @@ -15,115 +15,31 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include #include -#include -#include -#include -#include +#include extern const char *__progname; -#define fflush(s) _IO_fflush (s) - -/* This function, when passed a string containing an asserted - expression, a filename, and a line number, prints a message - on the standard error stream of the form: - a.c:10: foobar: Assertion `a == b' failed. - It then aborts program execution via a call to `abort'. */ - -#ifdef FATAL_PREPARE_INCLUDE -# include FATAL_PREPARE_INCLUDE -#endif - - -void -__assert_fail_base (const char *fmt, const char *assertion, const char *file, - unsigned int line, const char *function) -{ - char *str; - -#ifdef FATAL_PREPARE - FATAL_PREPARE; -#endif - - int total = __asprintf (&str, fmt, - __progname, __progname[0] ? ": " : "", - file, line, - function ? function : "", function ? ": " : "", - assertion); - if (total >= 0) - { - /* Print the message. */ - (void) __fxprintf (NULL, "%s", str); - (void) fflush (stderr); - - total = ALIGN_UP (total + sizeof (struct abort_msg_s) + 1, - GLRO(dl_pagesize)); - struct abort_msg_s *buf = __mmap (NULL, total, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); - if (__glibc_likely (buf != MAP_FAILED)) - { - buf->size = total; - strcpy (buf->msg, str); - __set_vma_name (buf, total, " glibc: assert"); - - /* We have to free the old buffer since the application might - catch the SIGABRT signal. */ - struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg, buf); - - if (old != NULL) - __munmap (old, old->size); - } - - free (str); - } - else - { - /* At least print a minimal message. */ - char linebuf[INT_STRLEN_BOUND (int) + sizeof ":: "]; - struct iovec v[9]; - int i = 0; - -#define WS(s) (v[i].iov_len = strlen (v[i].iov_base = (void *) (s)), i++) - - if (__progname) - { - WS (__progname); - WS (": "); - } - - WS (file); - v[i++] = (struct iovec) {.iov_base = linebuf, - .iov_len = sprintf (linebuf, ":%d: ", line)}; - - if (function) - { - WS (function); - WS (": "); - } - - WS ("Assertion `"); - WS (assertion); - /* We omit the '.' here so that the assert tests can tell when - this code path is taken. */ - WS ("' failed\n"); - - (void) __writev (STDERR_FILENO, v, i); - } - - abort (); -} - - #undef __assert_fail void __assert_fail (const char *assertion, const char *file, unsigned int line, const char *function) { - __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n"), - assertion, file, line, function); + char linebuf[INT_BUFSIZE_BOUND (unsigned int)]; + array_end (linebuf)[-1] = '\0'; + char *linestr = _itoa_word (line, array_end (linebuf) - 1, 10, 0); + + __libc_message (_("%s%s%s:%s: %s%sAssertion `%s' failed.\n"), + __progname, + __progname[0] ? ": " : "", + file, + linestr, + function ? function : "", + function ? ": " : "", + assertion); } diff --git a/assert/test-assert-2.c b/assert/test-assert-2.c index 6d54ef9ba6..9997c98d1a 100644 --- a/assert/test-assert-2.c +++ b/assert/test-assert-2.c @@ -127,7 +127,7 @@ check_posix (const char *buf, int rv, int line, } static void -one_test (void (*func)(void *), int which_path) +one_test (void (*func)(void *)) { struct support_capture_subprocess sp; int rv; @@ -140,17 +140,7 @@ one_test (void (*func)(void *), int which_path) check_posix (sp.err.buffer, rv, do_lineno, do_funcname, "1 == 2"); - /* Look for intentional subtle differences in messages to verify - that the intended code path was taken. */ - switch (which_path) - { - case 0: - TEST_VERIFY (strstr (sp.err.buffer, "failed.\n") != NULL); - break; - case 1: - TEST_VERIFY (strstr (sp.err.buffer, "failed\n") != NULL); - break; - } + TEST_VERIFY (strstr (sp.err.buffer, "failed.\n") != NULL); support_capture_subprocess_free (&sp); } @@ -158,8 +148,8 @@ one_test (void (*func)(void *), int which_path) static int do_test(void) { - one_test (test_assert_normal, 0); - one_test (test_assert_nomalloc, 1); + one_test (test_assert_normal); + one_test (test_assert_nomalloc); return 0; } diff --git a/include/stdio.h b/include/stdio.h index e48d709919..b3d9ed0d7a 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -171,7 +171,7 @@ extern void __fortify_fail (const char *msg) __attribute__ ((__noreturn__)); libc_hidden_proto (__fortify_fail) /* The maximum number of varargs allowed in a __libc_message format string */ -#define LIBC_MESSAGE_MAX_ARGS 4 +#define LIBC_MESSAGE_MAX_ARGS 7 _Noreturn void __libc_message_impl (const char *__fnt, ...) attribute_hidden __attribute__ ((__format__ (__printf__, 1, 2))); @@ -186,13 +186,19 @@ _Noreturn void __libc_message_impl (const char *__fnt, ...) attribute_hidden __libc_message_impl (fmt, a1, a2, a3) #define __libc_message4(fmt, a1, a2, a3, a4) \ __libc_message_impl (fmt, a1, a2, a3, a4) +#define __libc_message5(fmt, a1, a2, a3, a4, a5) \ + __libc_message_impl (fmt, a1, a2, a3, a4, a5) +#define __libc_message6(fmt, a1, a2, a3, a4, a5, a6) \ + __libc_message_impl (fmt, a1, a2, a3, a4, a5, a6) +#define __libc_message7(fmt, a1, a2, a3, a4, a5, a6, a7) \ + __libc_message_impl (fmt, a1, a2, a3, a4, a5, a6, a7) #define __libc_message_concat_x(a,b) a##b #define __libc_message_concat(a,b) __libc_message_concat_x (a, b) -#define __libc_message_nargs_x(a0,a1,a2,a3,a4,a5,a6,...) a6 +#define __libc_message_nargs_x(a0,a1,a2,a3,a4,a5,a6,a7,...) a7 #define __libc_message_nargs(b, ...) \ - __libc_message_nargs_x (__VA_ARGS__,6,5,4,3,2,1,0,) + __libc_message_nargs_x (__VA_ARGS__,7,6,5,4,3,2,1,0,) #define __libc_message_disp(b, ...) \ __libc_message_concat (b, __libc_message_nargs (__VA_ARGS__))(__VA_ARGS__) #define __libc_message(...) \