From nobody Thu Apr 17 02:23:22 2025 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 4ZdMBf6X97z5sMkH; Thu, 17 Apr 2025 02:23:22 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZdMBf4NVGz3gqN; Thu, 17 Apr 2025 02:23:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744856602; 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=IN24Laeux1XEghDaMdWbVJSAjz05zLjZOnq80NT47Tk=; b=r45tYQHvLlyg5sRuYn26UZ4kI0IpRIM2wObYksitGjFnlcrqTF1agyi/yzVklTDDBfR0hU JVRNh6nfZ0WOQKNLvc93qx0i+gpqMpc/jpR8jU/a4XheTyuu+agUhwQxqJhKfkYuB4SouX 05MZURqsZPuzYrYpxif/QQfms20LrM2RA+btE62r883IRlBq0yB0xpQGQqcXcBR8bwgt8t KoSqjP/+cOYmYxbdb6xAAgHlW+Zi4ZTrgY//J8ZP7OryCOSMjUD2IitQA3QTzpFgsWC8tZ 7Dw6PlSXH1l/mtqiwWJt3GvmXmiOqS+HeQ7qkpb0/AWgguH9IXhSvPb4wSDQ8w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1744856602; a=rsa-sha256; cv=none; b=Gq0jn1B2X/Et3LHCfdQbfcSmqXvdf/UhNlfxzsAQU4+PaQEsLkJPMqgp5Xv5QRURwdvEv3 JAwzuZ3p6CXGTeP1SDeO/47dkobNdLm1leQ9LnxiRvzok/wZG5PLl78/QwUjCBfOuRcpiq Brvjo+PCV135J7jRjw9tjIRyYa/iYYfBJ9YJwilZ5uG7KlymmpFcGOTDWhmy3sVscov0k8 WxEzQLji9glMxW0B5qxT6hxBaqxmdcI39WVCIYLPh45HTxTq2W3ym0QxZ7SnFOJvRj46+B xaYZMhbuFBLzC4GS6pVGCP0vVbF0600KgQxRNPULI6Iy2e6o0RzeXG7MULZ++Q== 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=1744856602; 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=IN24Laeux1XEghDaMdWbVJSAjz05zLjZOnq80NT47Tk=; b=NtVbEOdtTnkQiPPEzplXSer8VviyFty3/itPc+wmJtCTAoxQVSPiX6ExdgosZUNx1Q4JIK cyPg6X45Lsef1HzG0u1OFH667KvPPNsdL6PGrAKY42ODDUPf/xLvvK+i8dCEiD6ifiIL4t 7F1Vesvv40n+WCB6ZPW0KjrjmzTYo02usIWL6h6leShkreBJU6vkP/2eQv2+UN6Ltr0sCB 70ogqqOZ061eY2V3HkPLFk3ShXXsgY5DT8w6cw7hB+VIBNAKvA9oT0dkVtPxEwgmcrgDq3 OYbxR+HM4k+F8WxctTj8iyM58edFxmCZ5FDPViO0WQ6M5VKQOxJFpD4wh2NJdQ== 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 4ZdMBf3Dr9zDGL; Thu, 17 Apr 2025 02:23:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 53H2NMZX011216; Thu, 17 Apr 2025 02:23:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 53H2NMCt011213; Thu, 17 Apr 2025 02:23:22 GMT (envelope-from git) Date: Thu, 17 Apr 2025 02:23:22 GMT Message-Id: <202504170223.53H2NMCt011213@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: cc7bff26328d - stable/13 - libc: tests: add some tests for __cxa_atexit handling 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: cc7bff26328d81268a8fba620670428ea0867d81 Auto-Submitted: auto-generated The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=cc7bff26328d81268a8fba620670428ea0867d81 commit cc7bff26328d81268a8fba620670428ea0867d81 Author: Kyle Evans AuthorDate: 2025-04-05 00:47:54 +0000 Commit: Kyle Evans CommitDate: 2025-04-17 01:02:43 +0000 libc: tests: add some tests for __cxa_atexit handling This adds a basic test that __cxa_atexit works, and also adds some tests for __cxa_atexit handlers registered in the middle of __cxa_finalize. PR: 285870 (cherry picked from commit ee9ce1078c596f5719f312feedd616ab0fb41dc9) --- lib/libc/tests/stdlib/Makefile | 2 + lib/libc/tests/stdlib/cxa_atexit_test.c | 132 +++++++++++++++++++++++++++ lib/libc/tests/stdlib/libatexit/Makefile | 11 +++ lib/libc/tests/stdlib/libatexit/libatexit.cc | 67 ++++++++++++++ 4 files changed, 212 insertions(+) diff --git a/lib/libc/tests/stdlib/Makefile b/lib/libc/tests/stdlib/Makefile index aa2fa7683cb7..9df98bd4f435 100644 --- a/lib/libc/tests/stdlib/Makefile +++ b/lib/libc/tests/stdlib/Makefile @@ -1,6 +1,7 @@ .include +ATF_TESTS_C+= cxa_atexit_test ATF_TESTS_C+= dynthr_test ATF_TESTS_C+= heapsort_test ATF_TESTS_C+= mergesort_test @@ -77,5 +78,6 @@ LIBADD.${t}+= netbsd util LIBADD.strtod_test+= m SUBDIR+= dynthr_mod +SUBDIR+= libatexit .include diff --git a/lib/libc/tests/stdlib/cxa_atexit_test.c b/lib/libc/tests/stdlib/cxa_atexit_test.c new file mode 100644 index 000000000000..7e2cafbce850 --- /dev/null +++ b/lib/libc/tests/stdlib/cxa_atexit_test.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 2025 Kyle Evans + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include +#include +#include + +#include + +#define ARBITRARY_EXIT_CODE 42 + +static char * +get_shlib(const char *srcdir) +{ + char *shlib; + + shlib = NULL; + if (asprintf(&shlib, "%s/libatexit.so", srcdir) < 0) + atf_tc_fail("failed to construct path to libatexit.so"); + return (shlib); +} + +static void +run_test(const atf_tc_t *tc, bool with_fatal_atexit, bool with_exit) +{ + pid_t p; + void (*set_fatal_atexit)(bool); + void (*set_exit_code)(int); + void *hdl; + char *shlib; + + shlib = get_shlib(atf_tc_get_config_var(tc, "srcdir")); + + hdl = dlopen(shlib, RTLD_LAZY); + ATF_REQUIRE_MSG(hdl != NULL, "dlopen: %s", dlerror()); + + free(shlib); + + if (with_fatal_atexit) { + set_fatal_atexit = dlsym(hdl, "set_fatal_atexit"); + ATF_REQUIRE_MSG(set_fatal_atexit != NULL, + "set_fatal_atexit: %s", dlerror()); + } + if (with_exit) { + set_exit_code = dlsym(hdl, "set_exit_code"); + ATF_REQUIRE_MSG(set_exit_code != NULL, "set_exit_code: %s", + dlerror()); + } + + p = atf_utils_fork(); + if (p == 0) { + /* + * Don't let the child clobber the results file; stderr/stdout + * have been replaced by atf_utils_fork() to capture it. We're + * intentionally using exit() instead of _exit() here to run + * __cxa_finalize at exit, otherwise we'd just leave it be. + */ + closefrom(3); + + if (with_fatal_atexit) + set_fatal_atexit(true); + if (with_exit) + set_exit_code(ARBITRARY_EXIT_CODE); + + dlclose(hdl); + + /* + * If the dtor was supposed to exit (most cases), then we should + * not have made it to this point. If it's not supposed to + * exit, then we just exit with success here because we might + * be expecting either a clean exit or a signal on our way out + * as the final __cxa_finalize tries to run a callback in the + * unloaded DSO. + */ + if (with_exit) + exit(1); + exit(0); + } + + dlclose(hdl); + atf_utils_wait(p, with_exit ? ARBITRARY_EXIT_CODE : 0, "", ""); +} + +ATF_TC_WITHOUT_HEAD(simple_cxa_atexit); +ATF_TC_BODY(simple_cxa_atexit, tc) +{ + /* + * This test exits in a global object's dtor so that we check for our + * dtor being run at dlclose() time. If it isn't, then the forked child + * will have a chance to exit(1) after dlclose() to raise a failure. + */ + run_test(tc, false, true); +} + +ATF_TC_WITHOUT_HEAD(late_cxa_atexit); +ATF_TC_BODY(late_cxa_atexit, tc) +{ + /* + * This test creates another global object during a __cxa_atexit handler + * invocation. It's been observed in the wild that we weren't executing + * it, then the DSO gets torn down and it was executed at application + * exit time instead. In the best case scenario we would crash if + * something else hadn't been mapped there. + */ + run_test(tc, true, false); +} + +ATF_TC_WITHOUT_HEAD(late_cxa_atexit_ran); +ATF_TC_BODY(late_cxa_atexit_ran, tc) +{ + /* + * This is a slight variation of the previous test where we trigger an + * exit() in our late-registered __cxa_atexit handler so that we can + * ensure it was ran *before* dlclose() finished and not through some + * weird chain of events afterwards. + */ + run_test(tc, true, true); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, simple_cxa_atexit); + ATF_TP_ADD_TC(tp, late_cxa_atexit); + ATF_TP_ADD_TC(tp, late_cxa_atexit_ran); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/libatexit/Makefile b/lib/libc/tests/stdlib/libatexit/Makefile new file mode 100644 index 000000000000..9ba04c77af62 --- /dev/null +++ b/lib/libc/tests/stdlib/libatexit/Makefile @@ -0,0 +1,11 @@ +SHLIB_CXX= libatexit +SHLIB_NAME= libatexit.so +SHLIB_MAJOR= 1 +SHLIBDIR= ${TESTSDIR} +PACKAGE= tests +SRCS= libatexit.cc + +TESTSDIR:= ${TESTSBASE}/${RELDIR:C/libc\/tests/libc/:H} + + +.include diff --git a/lib/libc/tests/stdlib/libatexit/libatexit.cc b/lib/libc/tests/stdlib/libatexit/libatexit.cc new file mode 100644 index 000000000000..bb286c97e421 --- /dev/null +++ b/lib/libc/tests/stdlib/libatexit/libatexit.cc @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2025 Kyle Evans + * + * SPDX-License-Identifier: BSD-2-Clause + * + */ + +#include + +static int exit_code = -1; +static bool fatal_atexit; + +extern "C" { + void set_fatal_atexit(bool); + void set_exit_code(int); +} + +void +set_fatal_atexit(bool fexit) +{ + fatal_atexit = fexit; +} + +void +set_exit_code(int code) +{ + exit_code = code; +} + +struct other_object { + ~other_object() { + + /* + * In previous versions of our __cxa_atexit handling, we would + * never actually execute this handler because it's added during + * ~object() below; __cxa_finalize would never revisit it. We + * will allow the caller to configure us to exit with a certain + * exit code so that it can run us twice: once to ensure we + * don't crash at the end, and again to make sure the handler + * actually ran. + */ + if (exit_code != -1) + _exit(exit_code); + } +}; + +void +create_staticobj() +{ + static other_object obj; +} + +struct object { + ~object() { + /* + * If we're doing the fatal_atexit behavior (i.e., create an + * object that will add its own dtor for __cxa_finalize), then + * we don't exit here. + */ + if (fatal_atexit) + create_staticobj(); + else if (exit_code != -1) + _exit(exit_code); + } +}; + +static object obj;