From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-3.3 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS,URIBL_BLACK shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 7DA3D1F8C6 for ; Fri, 30 Jul 2021 20:00:28 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 67B993983821 for ; Fri, 30 Jul 2021 20:00:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67B993983821 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1627675227; bh=HxLU2b/JBkT1vZcwH/Az8YMM1/7yL6rQszXY0+vSPpM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=p8Xze/tTDQ8DLfi2iwp4BN6rIjA/W+78OzK0cWfGefCi4ogpawNlDYSDTiOEjKBJu l2/x9JSR0lTEuaLHME7ueVyU9Z4oFCMf9m+9nOstunI32qSL9pSNvDOR/tB8fTwcKQ eO9R3NMkvNy/XoJJTWv9wThodPVX4HL12EFAArGY= Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by sourceware.org (Postfix) with ESMTPS id 5BFF7397EC05 for ; Fri, 30 Jul 2021 19:47:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5BFF7397EC05 Received: by mail-pj1-x102c.google.com with SMTP id mz5-20020a17090b3785b0290176ecf64922so22301341pjb.3 for ; Fri, 30 Jul 2021 12:47:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HxLU2b/JBkT1vZcwH/Az8YMM1/7yL6rQszXY0+vSPpM=; b=TeKbUB7+C0Av/BbwokeGPeCrhvPTR8wtqqlk79JCgJc8BbMPwRXNxFfQWUvBgqeuq4 2q3gFvPZs1Erpb/ll7qHOXb3TEBfh8vj5XgGF1iCp8x99vRESgXQegnnFS01Q11mu8C0 GypDmQIDzeL4mkaW6NQYiDfQjSatK/rqyOzw8EJ2Ky6SPsHSfnusBjgN8neHiSyDm3Ny 7UNrsUaHJQisM+8D0auRov/nJWC9bSs5an3aM+u/AIVm19eJLPnVaX0SEtyvTjNzoqdi cj5QSY1SW/f8J1yT9zO9mVZTGVCLsw2UCqVgmNnFx0B7gyR2DMoCqOx1g/UUMbtgr6ko dh+A== X-Gm-Message-State: AOAM531PbYVhwMgMhv9UKenY2y9sI66sRi9R/MsP5m1aniOf3L26+UWf Q+6HMPVOC0+OlEgKGBI2yp/eivyEMhPRBA== X-Google-Smtp-Source: ABdhPJw9arYiX9eybvsdVeIQlMr9pzJyfQr/Cge++jPneKFrdwXMs9gtu7hJGA+cZ/421RiaZW49FQ== X-Received: by 2002:a05:6a00:2171:b029:3ab:eca3:af59 with SMTP id r17-20020a056a002171b02903abeca3af59mr4168643pff.46.1627674473193; Fri, 30 Jul 2021 12:47:53 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:43e2:6c33:fd81:e602:d33]) by smtp.gmail.com with ESMTPSA id c12sm3041426pfl.56.2021.07.30.12.47.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 12:47:52 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 16/20] elf: Add la_activity during application exit Date: Fri, 30 Jul 2021 16:47:11 -0300 Message-Id: <20210730194715.881900-17-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210730194715.881900-1-adhemerval.zanella@linaro.org> References: <20210730194715.881900-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Adhemerval Zanella via Libc-alpha Reply-To: Adhemerval Zanella Cc: John Mellor-Crummey Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" la_activity() is not called during application exit, even though la_objclose is. Checked on x86_64-linux-gnu. --- elf/Makefile | 6 ++ elf/dl-fini.c | 9 +++ elf/tst-audit23.c | 161 +++++++++++++++++++++++++++++++++++++++++++ elf/tst-audit23mod.c | 23 +++++++ elf/tst-auditmod23.c | 78 +++++++++++++++++++++ 5 files changed, 277 insertions(+) create mode 100644 elf/tst-audit23.c create mode 100644 elf/tst-audit23mod.c create mode 100644 elf/tst-auditmod23.c diff --git a/elf/Makefile b/elf/Makefile index 11f87ba46a..bbe59176b2 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -226,6 +226,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-audit20 \ tst-audit21 \ tst-audit22 \ + tst-audit23 \ tst-single_threaded tst-single_threaded-pthread \ tst-tls-ie tst-tls-ie-dlmopen argv0test \ tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \ @@ -312,6 +313,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-auditmod20 tst-audit20mod \ tst-auditmod21 \ tst-auditmod22 \ + tst-auditmod23 tst-audit23mod \ $(if $(CXX),tst-unique3lib tst-unique3lib2 tst-unique4lib \ tst-nodelete-uniquemod tst-nodelete-rtldmod \ tst-nodelete-zmod \ @@ -1531,6 +1533,10 @@ tst-audit21-ENV = LD_AUDIT=$(objpfx)tst-auditmod21.so $(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so tst-audit22-ARGS = -- $(host-test-program-cmd) +$(objpfx)tst-audit23.out: $(objpfx)tst-auditmod23.so \ + $(objpfx)tst-audit23mod.so +tst-audit23-ARGS = -- $(host-test-program-cmd) + # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ -Wl,--version-script=tst-sonamemove-linkmod1.map \ diff --git a/elf/dl-fini.c b/elf/dl-fini.c index 35a5471dcd..5e12525c7c 100644 --- a/elf/dl-fini.c +++ b/elf/dl-fini.c @@ -64,6 +64,10 @@ _dl_fini (void) __rtld_lock_unlock_recursive (GL(dl_load_lock)); else { +#ifdef SHARED + /* Auditing checkpoint: we will start deleting objects. */ + _dl_audit_activity_nsid (ns, LA_ACT_DELETE); +#endif /* Now we can allocate an array to hold all the pointers and copy the pointers in. */ struct link_map *maps[nloaded]; @@ -154,6 +158,11 @@ _dl_fini (void) /* Correct the previous increment. */ --l->l_direct_opencount; } + +#ifdef SHARED + /* Auditing checkpoint: we will start deleting objects. */ + _dl_audit_activity_nsid (ns, LA_ACT_CONSISTENT); +#endif } } diff --git a/elf/tst-audit23.c b/elf/tst-audit23.c new file mode 100644 index 0000000000..41bb4cb367 --- /dev/null +++ b/elf/tst-audit23.c @@ -0,0 +1,161 @@ +/* Check DT_AUDIT la_objopen and la_objclose for all objects. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int restart; +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, + +static int +handle_restart (void) +{ + xdlopen ("tst-audit23mod.so", RTLD_NOW); + xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW); + + return 0; +} + +static inline bool +startswith (const char *str, const char *pre) +{ + size_t lenpre = strlen (pre); + size_t lenstr = strlen (str); + return lenstr < lenpre ? false : memcmp (pre, str, lenpre) == 0; +} + +static int +do_test (int argc, char *argv[]) +{ + /* We must have either: + - One our fource parameters left if called initially: + + path to ld.so optional + + "--library-path" optional + + the library path optional + + the application name */ + if (restart) + return handle_restart (); + + char *spargv[9]; + int i = 0; + for (; i < argc - 1; i++) + spargv[i] = argv[i + 1]; + spargv[i++] = (char *) "--direct"; + spargv[i++] = (char *) "--restart"; + spargv[i] = NULL; + + setenv ("LD_AUDIT", "tst-auditmod23.so", 0); + struct support_capture_subprocess result + = support_capture_subprogram (spargv[0], spargv); + support_capture_subprocess_check (&result, "tst-audit22", 0, sc_allow_stderr); + + + /* We expect la_objopen/la_objclose for the objects: + 1. executable + 2. loader + 3. libc.so + 4. tst-audit23mod.so + 5. libc.so (LM_ID_NEWLM) */ + enum { max_objs = 5 }; + struct la_obj_t + { + char *lname; + uintptr_t laddr; + Lmid_t lmid; + bool closed; + } objs[max_objs] = { [0 ... max_objs-1] = { .closed = false } }; + size_t nobjs = 0; + + FILE *out = fmemopen (result.err.buffer, result.err.length, "r"); + TEST_VERIFY (out != NULL); + char *buffer = NULL; + size_t buffer_length = 0; + while (xgetline (&buffer, &buffer_length, out)) + { + enum { LA_OBJOPEN, LA_OBJCLOSE} mode; + ptrdiff_t offset; + if (startswith (buffer, "la_objopen: ")) + { + offset = strlen ("la_objopen: "); + mode = LA_OBJOPEN; + } + else if (startswith (buffer, "la_objclose: ")) + { + offset = strlen ("la_objclose: "); + mode = LA_OBJCLOSE; + } + else + continue; + + char *lname; + uintptr_t laddr; + Lmid_t lmid; + int r = sscanf (buffer + offset, "%ms %"SCNxPTR" %ld", &lname, &laddr, + &lmid); + TEST_COMPARE (r, 3); + + if (mode == LA_OBJOPEN) + { + if (nobjs == max_objs) + FAIL_EXIT1 ("non expected la_objopen: %s %"PRIxPTR" %ld", + lname, laddr, lmid); + objs[nobjs].lname = lname; + objs[nobjs].laddr = laddr; + objs[nobjs].lmid = lmid; + objs[nobjs].closed = false; + nobjs++; + } + else if (mode == LA_OBJCLOSE) + { + for (size_t i = 0; i < nobjs; i++) + { + if (strcmp (lname, objs[i].lname) == 0 && lmid == objs[i].lmid) + { + TEST_COMPARE (objs[i].closed, false); + objs[i].closed = true; + break; + } + } + } + } + + for (size_t i = 0; i < nobjs; i++) + { + TEST_COMPARE (objs[i].closed, true); + free (objs[i].lname); + } + + free (buffer); + xfclose (out); + + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include diff --git a/elf/tst-audit23mod.c b/elf/tst-audit23mod.c new file mode 100644 index 0000000000..4ca66cf772 --- /dev/null +++ b/elf/tst-audit23mod.c @@ -0,0 +1,23 @@ +/* Extra modules for tst-audit23 + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +int +foo (void) +{ + return 0; +} diff --git a/elf/tst-auditmod23.c b/elf/tst-auditmod23.c new file mode 100644 index 0000000000..20ef19945d --- /dev/null +++ b/elf/tst-auditmod23.c @@ -0,0 +1,78 @@ +/* Audit modules loaded by tst-audit23. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +static uintptr_t vdso_addr; + +unsigned int +la_version (unsigned int version) +{ + vdso_addr = getauxval (AT_SYSINFO_EHDR); + /* The main_map for the executable returns 0 for l_addr. */ + if (vdso_addr == 0) + vdso_addr = -1; + return LAV_CURRENT; +} + +struct map_desc_t +{ + char *lname; + uintptr_t laddr; + Lmid_t lmid; +}; + + +unsigned int +la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie) +{ + if (map->l_addr == vdso_addr) + return 0; + + fprintf (stderr, "%s: %s %"PRIxPTR" %ld\n", __func__, + map->l_name, map->l_addr, lmid); + + struct map_desc_t *map_desc = malloc (sizeof (struct map_desc_t)); + if (map_desc == NULL) + abort(); + + map_desc->lname = strdup (map->l_name); + map_desc->laddr = map->l_addr; + map_desc->lmid = lmid; + + *cookie = (uintptr_t) map_desc; + + return 0; +} + +unsigned int +la_objclose (uintptr_t *cookie) +{ + struct map_desc_t *map_desc = (struct map_desc_t *) *cookie; + fprintf (stderr, "%s: %s %"PRIxPTR" %ld\n", __func__, + map_desc->lname, map_desc->laddr, map_desc->lmid); + free (map_desc->lname); + free (map_desc); + + return 0; +} -- 2.30.2