From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 80BE0385802B for ; Fri, 7 Jan 2022 12:11:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 80BE0385802B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from localhost.localdomain (unknown [111.18.94.40]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9DxeMrYLdhhIXoAAA--.1769S2; Fri, 07 Jan 2022 20:11:05 +0800 (CST) From: Tiezhu Yang To: gdb-patches@sourceware.org, Tom de Vries Subject: [PATCH v3] gdb: testsuite: fix wrong expected result in attach-pie-noexec.exp Date: Fri, 7 Jan 2022 20:11:01 +0800 Message-Id: <20220107121101.5651-1-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf9DxeMrYLdhhIXoAAA--.1769S2 X-Coremail-Antispam: 1UD129KBjvJXoWxZr18AF47Xr48JF1fuFy7Awb_yoW5CF1rpF Z2qryFyF18Xa4aqa9xA3y8XryFy3y0gry5u3s7Jr1rZw4DCr18Gry3try5ZF9xJrWvk39x ZwnFvr4rCr1jvFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyIb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4 A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IE w4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMc vjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x0EwIxGrwCFx2Iq xVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r 106r1rMI8E67AF67kF1VAFwI0_Jrv_JF1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AK xVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7 xG6rW3Jr0E3s1lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWU JVW8JbIYCTnIWIevJa73UjIFyTuYvjxUgg_TUUUUU X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Jan 2022 12:11:14 -0000 If /proc/sys/kernel/yama/ptrace_scope is 1, when execute the test case gdb.base/attach-pie-noexec.exp without superuser, the gdb.log shows the following info: (gdb) attach 6500 Attaching to process 6500 ptrace: Operation not permitted. (gdb) PASS: gdb.base/attach-pie-noexec.exp: attach It is obviously wrong, the expected result should be UNSUPPORTED in such a case. It is better to make can_spawn_for_attach to return false for this case. It would have to setup a small test program, compile it to exec, spawn it and try to attach to it. With this patch, we can see "please check privileges and try again" in the log info, and then we can do the following processes to test: (1) set ptrace_scope as 0 $ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope $ make check-gdb TESTS="gdb.base/attach-pie-noexec.exp" (2) use sudo $ sudo make check-gdb TESTS="gdb.base/attach-pie-noexec.exp" Signed-off-by: Tiezhu Yang --- gdb/testsuite/lib/gdb.exp | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 60f7b83571a..8af2c011a3e 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -5055,7 +5055,10 @@ proc gdb_exit { } { # Return true if we can spawn a program on the target and attach to # it. -proc can_spawn_for_attach { } { +gdb_caching_proc can_spawn_for_attach { + global gdb_prompt + set test "can spawn for attach" + # We use exp_pid to get the inferior's pid, assuming that gives # back the pid of the program. On remote boards, that would give # us instead the PID of e.g., the ssh client, etc. @@ -5070,6 +5073,32 @@ proc can_spawn_for_attach { } { return 0 } + set me "can_spawn_for_attach" + set src { int main() { sleep (600); return 0; } } + if {![gdb_simple_compile $me $src executable]} { + return 0 + } + + set test_spawn_id [spawn_wait_for_attach $obj] + set test_pid [spawn_id_get_pid $test_spawn_id] + + gdb_start + file delete $obj + gdb_test_multiple "attach $test_pid" $test { + -re "Attaching to process $test_pid\r\n.*No executable file now.*\r\n$gdb_prompt $" { + pass $test + kill_wait_spawned_process $test_spawn_id + return 1 + } + -re "Attaching to process $test_pid\r\n.*ptrace: Operation not permitted\\.\r\n$gdb_prompt $" { + unsupported "$test (please check privileges and try again)" + kill_wait_spawned_process $test_spawn_id + return 0 + } + } + + kill_wait_spawned_process $test_spawn_id + # Assume yes. return 1 } @@ -5119,12 +5148,6 @@ proc spawn_id_get_pid { spawn_id } { proc spawn_wait_for_attach { executable_list } { set spawn_id_list {} - if ![can_spawn_for_attach] { - # The caller should have checked can_spawn_for_attach itself - # before getting here. - error "can't spawn for attach with this target/board" - } - foreach {executable} $executable_list { # Note we use Expect's spawn, not Tcl's exec, because with # spawn we control when to wait for/reap the process. That -- 2.27.0