Message ID | 20230522104623.214121-1-blarsen@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> 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 4AA983858426 for <patchwork@sourceware.org>; Mon, 22 May 2023 10:47:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AA983858426 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684752452; bh=dQzQ/5jcm2KcS2EtOo3etjgb/TE81g2t8m7TNdCmyOY=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ZGcwrvkIoWPOlQjnTu9/U8egChacFue5e/VCDUqF55k5udmH6Fy4VmEHe73td/OYJ 5e6DHGKfiCASuuCR0G4ENh9KRZXF9S+mxdIlwSOhJoU4TpnPAOfy2BrxNiKo5/851t MEmvGecYVubhBTbM/blV3essPqU8EOeyOP9CCwvs= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4C0E63858D35 for <gdb-patches@sourceware.org>; Mon, 22 May 2023 10:47:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C0E63858D35 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-250-YrcALtskOGCbteceXm_jIg-1; Mon, 22 May 2023 06:46:59 -0400 X-MC-Unique: YrcALtskOGCbteceXm_jIg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39DE31C05134 for <gdb-patches@sourceware.org>; Mon, 22 May 2023 10:46:59 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.226.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8F99B1121314; Mon, 22 May 2023 10:46:58 +0000 (UTC) To: gdb-patches@sourceware.org Cc: aburgess@redhat.com, Bruno Larsen <blarsen@redhat.com> Subject: [PATCH v2] gdb/testsuite: change hardcoded assembly in gdb.arch/disp-step-insn-reloc.exp Date: Mon, 22 May 2023 12:46:24 +0200 Message-Id: <20230522104623.214121-1-blarsen@redhat.com> In-Reply-To: <20230426132916.1988539-1-blarsen@redhat.com> References: <20230426132916.1988539-1-blarsen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list <gdb-patches.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=subscribe> From: Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> Reply-To: Bruno Larsen <blarsen@redhat.com> Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> |
Series |
[v2] gdb/testsuite: change hardcoded assembly in gdb.arch/disp-step-insn-reloc.exp
|
|
Commit Message
Guinevere Larsen
May 22, 2023, 10:46 a.m. UTC
When testing gdb.arch/disp-step-insn-reloc.exp with clang in an x86_64 machine, the compiled test case would segfault when returning from the function can_relocate_call, with a suggestion of a broken stack. The example assembly in the commment was the following: f: MOV $1, %[ok] JMP end set_point0: CALL f ; tracepoint here. end: And the segmentation fault happening at the final "ret" instruction of can_relocate_call. Looking at the disassembled version of the later half of the important function, we see: Clang version (f starting at 11a4): 00000000000011ae <set_point0>: 11ae: e8 f1 ff ff ff callq 11a4 <can_relocate_call+0x14> 11b3: 89 45 fc mov %eax,-0x4(%rbp) 11b6: 83 7d fc 01 cmpl $0x1,-0x4(%rbp) 11ba: 0f 85 0a 00 00 00 jne 11ca <set_point0+0x1c> 11c0: e8 5b 00 00 00 callq 1220 <pass> 11c5: e9 05 00 00 00 jmpq 11cf <set_point0+0x21> 11ca: e8 61 00 00 00 callq 1230 <fail> 11cf: 48 83 c4 10 add $0x10,%rsp 11d3: 5d pop %rbp 11d4: c3 retq 11d5: 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1) 11dc: 00 00 00 00 gcc version (f starting at 401125): 000000000040112c <set_point0>: 40112c: e8 f4 ff ff ff callq 401125 <can_relocate_call+0x11> 401131: 89 45 fc mov %eax,-0x4(%rbp) 401134: 83 7d fc 01 cmpl $0x1,-0x4(%rbp) 401138: 75 07 jne 401141 <set_point0+0x15> 40113a: e8 c7 ff ff ff callq 401106 <pass> 40113f: eb 05 jmp 401146 <set_point0+0x1a> 401141: e8 c7 ff ff ff callq 40110d <fail> 401146: 90 nop 401147: c9 leaveq 401148: c3 retq The epilogue of set_point0 (11cf for clang, 401146 for gcc) is the main difference: GCC's version uses the leaveq instruction, which resets rsp based on rbp, while clang adds the same constant to rsp that it subtracted in the prologue. Clang fails because the return address that is added by the "call f" instruction isn't accounted for. This commit fixes that by adding a return instruction to f, which leaves the rsp as the compilers would expect. --- gdb/testsuite/gdb.arch/insn-reloc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
Comments
Bruno Larsen <blarsen@redhat.com> writes: > When testing gdb.arch/disp-step-insn-reloc.exp with clang in an x86_64 > machine, the compiled test case would segfault when returning from > the function can_relocate_call, with a suggestion of a broken stack. > The example assembly in the commment was the following: > > f: > MOV $1, %[ok] > JMP end > set_point0: > CALL f ; tracepoint here. > end: > > And the segmentation fault happening at the final "ret" instruction of > can_relocate_call. Looking at the disassembled version of the later > half of the important function, we see: > > Clang version (f starting at 11a4): > 00000000000011ae <set_point0>: > 11ae: e8 f1 ff ff ff callq 11a4 <can_relocate_call+0x14> > 11b3: 89 45 fc mov %eax,-0x4(%rbp) > 11b6: 83 7d fc 01 cmpl $0x1,-0x4(%rbp) > 11ba: 0f 85 0a 00 00 00 jne 11ca <set_point0+0x1c> > 11c0: e8 5b 00 00 00 callq 1220 <pass> > 11c5: e9 05 00 00 00 jmpq 11cf <set_point0+0x21> > 11ca: e8 61 00 00 00 callq 1230 <fail> > 11cf: 48 83 c4 10 add $0x10,%rsp > 11d3: 5d pop %rbp > 11d4: c3 retq > 11d5: 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1) > 11dc: 00 00 00 00 > > gcc version (f starting at 401125): > 000000000040112c <set_point0>: > 40112c: e8 f4 ff ff ff callq 401125 <can_relocate_call+0x11> > 401131: 89 45 fc mov %eax,-0x4(%rbp) > 401134: 83 7d fc 01 cmpl $0x1,-0x4(%rbp) > 401138: 75 07 jne 401141 <set_point0+0x15> > 40113a: e8 c7 ff ff ff callq 401106 <pass> > 40113f: eb 05 jmp 401146 <set_point0+0x1a> > 401141: e8 c7 ff ff ff callq 40110d <fail> > 401146: 90 nop > 401147: c9 leaveq > 401148: c3 retq > > The epilogue of set_point0 (11cf for clang, 401146 for gcc) is the main > difference: GCC's version uses the leaveq instruction, which resets rsp > based on rbp, while clang adds the same constant to rsp that it > subtracted in the prologue. Clang fails because the return address that > is added by the "call f" instruction isn't accounted for. > > This commit fixes that by adding a return instruction to f, which leaves > the rsp as the compilers would expect. Looks great. Thanks for updating the commit message. Approved-By: Andrew Burgess <aburgess@redhat.com> Thanks, Andrew > --- > gdb/testsuite/gdb.arch/insn-reloc.c | 6 ++---- > 1 file changed, 2 insertions(+), 4 deletions(-) > > diff --git a/gdb/testsuite/gdb.arch/insn-reloc.c b/gdb/testsuite/gdb.arch/insn-reloc.c > index f687c2c5631..365e6180057 100644 > --- a/gdb/testsuite/gdb.arch/insn-reloc.c > +++ b/gdb/testsuite/gdb.arch/insn-reloc.c > @@ -49,10 +49,9 @@ fail (void) > JMP set_point0 > f: > MOV $1, %[ok] > - JMP end > + RET > set_point0: > CALL f ; tracepoint here. > - end: > > */ > > @@ -65,10 +64,9 @@ can_relocate_call (void) > " jmp " SYMBOL (set_point0) "\n" > "0:\n" > " mov $1, %[ok]\n" > - " jmp 1f\n" > + " ret\n" > SYMBOL (set_point0) ":\n" > " call 0b\n" > - "1:\n" > : [ok] "=r" (ok)); > > if (ok == 1) > -- > 2.40.1
diff --git a/gdb/testsuite/gdb.arch/insn-reloc.c b/gdb/testsuite/gdb.arch/insn-reloc.c index f687c2c5631..365e6180057 100644 --- a/gdb/testsuite/gdb.arch/insn-reloc.c +++ b/gdb/testsuite/gdb.arch/insn-reloc.c @@ -49,10 +49,9 @@ fail (void) JMP set_point0 f: MOV $1, %[ok] - JMP end + RET set_point0: CALL f ; tracepoint here. - end: */ @@ -65,10 +64,9 @@ can_relocate_call (void) " jmp " SYMBOL (set_point0) "\n" "0:\n" " mov $1, %[ok]\n" - " jmp 1f\n" + " ret\n" SYMBOL (set_point0) ":\n" " call 0b\n" - "1:\n" : [ok] "=r" (ok)); if (ok == 1)