From patchwork Wed May 12 17:49:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 43419 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 489A439B6032; Wed, 12 May 2021 17:50:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 489A439B6032 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620841807; bh=zlm5umY5ItS5E7lkbQVTr3c5NjGF9/xeo4KUEQESLu8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=jgAZj20wFd6ITF+7T14F07DL9vP/Ecpoj4cqzPhcQXEssINTFEU1KBjcRJyBEZ5/i w1/MS0qlhEdO9zJr1xrc2gReyRdApGqDgheKVYa5az8MqjNUvK2/tAeLgPsobzYHE1 xJ4mMOC2vDqr63rLpAV27Z0ZLm2sosGHBOYflXpU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by sourceware.org (Postfix) with ESMTPS id 2DDDA39B602C for ; Wed, 12 May 2021 17:50:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2DDDA39B602C Received: by mail-qk1-x735.google.com with SMTP id v8so5089172qkv.1 for ; Wed, 12 May 2021 10:50:04 -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:mime-version :content-transfer-encoding; bh=zlm5umY5ItS5E7lkbQVTr3c5NjGF9/xeo4KUEQESLu8=; b=aWL8C4BQXg7dBHshBpj1J8pYAx13KHZsm2YPoDh4oNhkCPFyRY1Cyp3sTRdlRvP/q9 QLCyBkcXW96Vm+hvZyiDx7KPCzipPzqj5QvPPs5t4g2H9NP3lRNHWfApETJ/PpHvUbmU PngMDkQTscWujfHWgnhJEhD0EyDaBOuvR25YxBdjTzBfmJKzBg1eWEmonlhMWyJlOJ8M gKkLuNXLBApiynnO8Ar5D2DFmR5/S5zkCbKUAMEp+/wJJR9cEaN3MjDbJCYWdpJgc56D nEgv6HN3aSC3Kl6UaTrMCAA499lotE4Vy4fb+JqyHXJV3op+8/moSl8I6AeVecghvSVz 8KOw== X-Gm-Message-State: AOAM533nWlqjKwSVvZes5OC+oVmoaSqEC8Dq+T0i2FZP0QwaCIXIzfEr hN99P6wS0TNghllKXeyBLw/Sn0vpAzbnKQ== X-Google-Smtp-Source: ABdhPJwTbe/ulkjACPUUYfhAi+QhiJP0ERkWTmnc4XhGoasNeCwLzuwG7ReGcUEx0xS3UgLBok5Rgw== X-Received: by 2002:a37:e10b:: with SMTP id c11mr34103417qkm.152.1620841803453; Wed, 12 May 2021 10:50:03 -0700 (PDT) Received: from birita.. ([177.194.37.86]) by smtp.googlemail.com with ESMTPSA id j196sm550862qke.25.2021.05.12.10.50.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 10:50:03 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Fix tst-pthread-gdb-attach for ptrace_scope equal 1 Date: Wed, 12 May 2021 14:49:59 -0300 Message-Id: <20210512174959.310818-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This is similar to the fix for elf/tst-pldd (2f9046fb059e94fe25): it checks ptrace_scope value (values higher than 2 are too restrictive to allow the test to run) and it rearranges the spawned processes to make the target process the gdb child. Checked on x86_64-linux-gnu with ptrace_scope set to 1. --- nptl/tst-pthread-gdb-attach.c | 69 ++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/nptl/tst-pthread-gdb-attach.c b/nptl/tst-pthread-gdb-attach.c index 901a120034..d315bc9ef2 100644 --- a/nptl/tst-pthread-gdb-attach.c +++ b/nptl/tst-pthread-gdb-attach.c @@ -26,7 +26,10 @@ #include #include #include +#include #include +#include +#include #include #include #include @@ -141,7 +144,7 @@ subprocess_thread (void *closure) second thread, waiting for its value to change to 2, and checks that the main thread also changed its value to 1. */ static void -in_subprocess (void) +in_subprocess (void *arg) { pthread_t thr = xpthread_create (NULL, subprocess_thread, NULL); TEST_VERIFY (xpthread_join (thr) == NULL); @@ -149,6 +152,29 @@ in_subprocess (void) _exit (0); } +static void +gdb_process (void *arg) +{ + const char *gdb_path = (const char *) arg; + + /* Create a copy of current test to check with gdb. As the + target_process is a child of this pldd_process, pldd is also able + to attach to target_process if YAMA is configured to 1 = + "restricted ptrace". */ + struct support_subprocess target = support_subprocess (in_subprocess, NULL); + + char *gdbscript; + xclose (create_temp_file ("tst-pthread-gdb-attach-", &gdbscript)); + write_gdbscript (gdbscript, target.pid); + + xdup2 (STDOUT_FILENO, STDERR_FILENO); + execl (gdb_path, "gdb", "-nx", "-batch", "-x", gdbscript, NULL); + if (errno == ENOENT) + _exit (EXIT_UNSUPPORTED); + else + _exit (1); +} + static int do_test (void) { @@ -179,37 +205,20 @@ do_test (void) free (threaddb_path); } - pid_t tested_pid = xfork (); - if (tested_pid == 0) - in_subprocess (); - char *tested_pid_string = xasprintf ("%d", tested_pid); - - char *gdbscript; - xclose (create_temp_file ("tst-pthread-gdb-attach-", &gdbscript)); - write_gdbscript (gdbscript, tested_pid); + /* Check if our subprocess can be debugged with ptrace. */ + { + int ptrace_scope = support_ptrace_scope (); + if (ptrace_scope >= 2) + FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2"); + } - pid_t gdb_pid = xfork (); - if (gdb_pid == 0) - { - xdup2 (STDOUT_FILENO, STDERR_FILENO); - execl (gdb_path, "gdb", "-nx", "-batch", "-x", gdbscript, NULL); - if (errno == ENOENT) - _exit (EXIT_UNSUPPORTED); - else - _exit (1); - } + /* Run 'gdb' on test subprocess which will be created in gdb_process. + The pid of the subprocess will be written to target_pid_ptr. */ + struct support_capture_subprocess gdb; + gdb = support_capture_subprocess (gdb_process, gdb_path); + support_capture_subprocess_check (&gdb, "gdb", 0, sc_allow_stdout); + support_capture_subprocess_free (&gdb); - int status; - TEST_COMPARE (xwaitpid (gdb_pid, &status, 0), gdb_pid); - if (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_UNSUPPORTED) - /* gdb is not installed. */ - return EXIT_UNSUPPORTED; - TEST_COMPARE (status, 0); - TEST_COMPARE (xwaitpid (tested_pid, &status, 0), tested_pid); - TEST_COMPARE (status, 0); - - free (tested_pid_string); - free (gdbscript); free (gdb_path); return 0; }