From nobody Thu Oct 5 00:01:38 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4S1BYb1CR4z4wkCC; Thu, 5 Oct 2023 00:01:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4S1BYb0GrMz4knT; Thu, 5 Oct 2023 00:01:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696464099; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rdpJERkba2HT/L09iKgmFK7L4OO8OcL8xhW3cWcoKNo=; b=xrj5W6XXP048PitRYWOpTp3CLQw0OsZZzt1/Tej0hEZqto7LBipOseykA2WJUZ+0E0LhSq ohgEVPv9wz7bgiSKTce/tDZfqB6CqRUlx+w3qma/kErnx+RTHiFpqje9ZZ4M4vgVaRaT2u /vzIXgoQTI7O7jefI8f8GKaLGl08LkEU/a7l8BtQxLJirQeg8YES29sdhheE4su7EgmbWe 4moMhvbVhAoNpU9XNdavnS+lVMp5OC7t7D7Ad6hoGqDVmRW01v2qWe7zcDNB4gL++TBJmM 8u2dWJ6iIim2iB69es2PuijaECDmSdsmfSr7oD042ye6azkoINAvnlb1MwsxDA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1696464099; a=rsa-sha256; cv=none; b=U2iZC43sR6e7OAY5xRKpm5eMBWhdhtYaMNMMaPkmUxISJ30KqBxG+W+ZXByGmfTbbzcCOh 66Lx+rRvZcuY6/qDPFYdMqMF112/UcnEu8itYEYFNtdzIo55xt1LluSLzUnwKIMSj1Vy4L tvbCd3lAz7+ZLj0PpS0G8yJUVM7znfKBXHIDwJCyb+efKI+9os0ikW3+aDASMFgK+liih4 SR8ZmLFC1paGY8YZwBCRtRUO5w28B+JN0seAMpd9a9PwyQwh/42/oGqYzskv4pE6sNotl/ WoDUnyvbYDATGV1ojsRE9AVXxMuCYozfNvIWUf3yrW+KiBOMe2tnr4DTO5VIhA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696464099; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rdpJERkba2HT/L09iKgmFK7L4OO8OcL8xhW3cWcoKNo=; b=nKR/ubWMUGcK1odeVnt2o82g66yjEc2ARNW69V0fvH5Kl94ULw4A+1V4NfkXVuFn4oH3gv T8dTYCkVJg9ciwLssyIiPBTYz5NsfuQeQiFowLYYzmfVhoSpHzCi3J6JCzwMOOV+WWTU7P BTZO4bum3MPH+P4gdlORo9ZvpjIQzL9BPsKaPXXn2195wFU3DTiQVA7tYp5RONp2ikYqHV LXs4MtqhL2zu77iroFUQR+DIKoOY68L2J3i+jp16/La/eyyW5fnE2y2rS8Sf0wQg4GAizE axqzvyHDksr8C2KWJE6UmgwsBR2JIWoElIJHhDr/iVHogwrV+tnNfdWCs1YUkg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4S1BYZ6PkWzyV3; Thu, 5 Oct 2023 00:01:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 39501cuo051575; Thu, 5 Oct 2023 00:01:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39501cEx051572; Thu, 5 Oct 2023 00:01:38 GMT (envelope-from git) Date: Thu, 5 Oct 2023 00:01:38 GMT Message-Id: <202310050001.39501cEx051572@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: b9c361e4af26 - stable/14 - libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: b9c361e4af26547b7a373d5dda4eb7a6fee4f7c6 Auto-Submitted: auto-generated The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=b9c361e4af26547b7a373d5dda4eb7a6fee4f7c6 commit b9c361e4af26547b7a373d5dda4eb7a6fee4f7c6 Author: Dag-Erling Smørgrav AuthorDate: 2023-09-26 20:06:27 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-10-04 23:59:43 +0000 libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics. Compiler memory barriers do not prevent the CPU from executing the code out of order. Switch to C11 atomics. This also lets us get rid of the mutex; instead, loop until the compare_exchange succeeds. While here, change the return value of at_quick_exit() on failure to the more traditional -1, matching atexit(). Sponsored by: Klara, Inc. Reviewed by: Olivier Certner, kevans, kib Differential Revision: https://reviews.freebsd.org/D41936 (cherry picked from commit 1dc3abb052430279e47c8922d22b30922adcf0f6) libc: Add a rudimentary test for quick_exit(3). Sponsored by: Klara, Inc. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D41937 (cherry picked from commit c7dd4601aeebbc1bbe131cbe6747476c124b47fe) --- lib/libc/stdlib/quick_exit.c | 40 +++++++--------- lib/libc/tests/stdlib/Makefile | 1 + lib/libc/tests/stdlib/quick_exit_test.c | 81 +++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 24 deletions(-) diff --git a/lib/libc/stdlib/quick_exit.c b/lib/libc/stdlib/quick_exit.c index 05db690cb6e7..4dee7b20bd2b 100644 --- a/lib/libc/stdlib/quick_exit.c +++ b/lib/libc/stdlib/quick_exit.c @@ -2,6 +2,7 @@ * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2011 David Chisnall + * Copyright (c) 2023 Klara, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,44 +28,35 @@ */ #include -#include + +#include #include -#include /** - * Linked list of quick exit handlers. This is simpler than the atexit() - * version, because it is not required to support C++ destructors or - * DSO-specific cleanups. + * Linked list of quick exit handlers. These will be invoked in reverse + * order of insertion when quick_exit() is called. This is simpler than + * the atexit() version, because it is not required to support C++ + * destructors or DSO-specific cleanups. */ struct quick_exit_handler { struct quick_exit_handler *next; void (*cleanup)(void); }; -/** - * Lock protecting the handlers list. - */ -static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER; -/** - * Stack of cleanup handlers. These will be invoked in reverse order when - */ -static struct quick_exit_handler *handlers; +static _Atomic(struct quick_exit_handler *) handlers; int at_quick_exit(void (*func)(void)) { struct quick_exit_handler *h; - - h = malloc(sizeof(*h)); - if (NULL == h) - return (1); + if ((h = calloc(1, sizeof(*h))) == NULL) { + return (-1); + } h->cleanup = func; - pthread_mutex_lock(&atexit_mutex); - h->next = handlers; - __compiler_membar(); - handlers = h; - pthread_mutex_unlock(&atexit_mutex); + while (!atomic_compare_exchange_strong(&handlers, &h->next, h)) { + /* nothing */ ; + } return (0); } @@ -77,8 +69,8 @@ quick_exit(int status) * XXX: The C++ spec requires us to call std::terminate if there is an * exception here. */ - for (h = handlers; NULL != h; h = h->next) { - __compiler_membar(); + for (h = atomic_load_explicit(&handlers, memory_order_acquire); + h != NULL; h = h->next) { h->cleanup(); } _Exit(status); diff --git a/lib/libc/tests/stdlib/Makefile b/lib/libc/tests/stdlib/Makefile index a2a6420aba41..860e530389df 100644 --- a/lib/libc/tests/stdlib/Makefile +++ b/lib/libc/tests/stdlib/Makefile @@ -12,6 +12,7 @@ ATF_TESTS_C+= qsort_b_test ATF_TESTS_C+= qsort_r_compat_test ATF_TESTS_C+= qsort_r_test ATF_TESTS_C+= qsort_s_test +ATF_TESTS_C+= quick_exit_test ATF_TESTS_C+= set_constraint_handler_s_test ATF_TESTS_C+= strfmon_test ATF_TESTS_C+= tsearch_test diff --git a/lib/libc/tests/stdlib/quick_exit_test.c b/lib/libc/tests/stdlib/quick_exit_test.c new file mode 100644 index 000000000000..9feed8a6fa63 --- /dev/null +++ b/lib/libc/tests/stdlib/quick_exit_test.c @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 2023 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include +#include + +#include + +static void func_a(void) +{ + if (write(STDOUT_FILENO, "a", 1) != 1) + _Exit(1); +} + +static void func_b(void) +{ + if (write(STDOUT_FILENO, "b", 1) != 1) + _Exit(1); +} + +static void func_c(void) +{ + if (write(STDOUT_FILENO, "c", 1) != 1) + _Exit(1); +} + +static void child(void) +{ + // this will be received by the parent + printf("hello, "); + fflush(stdout); + // this won't, because quick_exit() does not flush + printf("world"); + // these will be called in reverse order, producing "abc" + if (at_quick_exit(func_c) != 0 || + at_quick_exit(func_b) != 0 || + at_quick_exit(func_a) != 0) + _Exit(1); + quick_exit(0); +} + +ATF_TC_WITHOUT_HEAD(quick_exit); +ATF_TC_BODY(quick_exit, tc) +{ + char buf[100] = ""; + ssize_t len; + int p[2], wstatus = 0; + pid_t pid; + + ATF_REQUIRE(pipe(p) == 0); + pid = fork(); + if (pid == 0) { + if (dup2(p[1], STDOUT_FILENO) < 0) + _Exit(1); + (void)close(p[1]); + (void)close(p[0]); + child(); + _Exit(1); + } + ATF_REQUIRE_MSG(pid > 0, + "expect fork() to succeed"); + ATF_CHECK_EQ_MSG(pid, waitpid(pid, &wstatus, 0), + "expect to collect child process"); + ATF_CHECK_EQ_MSG(0, wstatus, + "expect child to exit cleanly"); + ATF_CHECK_MSG((len = read(p[0], buf, sizeof(buf))) > 0, + "expect to receive output from child"); + ATF_CHECK_STREQ("hello, abc", buf); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, quick_exit); + return (atf_no_error()); +}