Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 664016 - sys-devel/gcc-7.3.0-r3 with -mx32 __builtin_eh_return uses movl to set return address, followed by retq
Summary: sys-devel/gcc-7.3.0-r3 with -mx32 __builtin_eh_return uses movl to set retur...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL: https://gcc.gnu.org/PR87014
Whiteboard:
Keywords: PATCH
Depends on:
Blocks: x32 664058
  Show dependency tree
 
Reported: 2018-08-19 06:54 UTC by camper
Modified: 2018-10-27 21:13 UTC (History)
3 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
gcc-7.3.0-fix-x32-eh_return.patch (file_664016.txt,519 bytes, patch)
2018-08-19 06:58 UTC, camper
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description camper 2018-08-19 06:54:56 UTC
long offset;
void* ptr;

void f(void)
{
    __builtin_eh_return(offset,ptr);
}

with gcc -mx32
produces:
f:
        pushq   %rbp
        movl    %esp, %ebp
        pushq   %rdx
        pushq   %rax
        movl    %fs:24, %eax
        movl    %eax, -20(%ebp)
        xorl    %eax, %eax
        movl    ptr(%rip), %eax
        movl    offset(%rip), %edx
        movl    %edx, %ecx
        movl    %eax, 8(%ebp,%ecx)     # 32-bit mov
        movq    -16(%ebp), %rax
        movq    -8(%ebp), %rdx
        leal    8(%rbp,%rcx), %ecx
        movl    0(%ebp), %ebp
        movl    %ecx, %esp
        ret                            # 64-bit return

Since the upper half of the return address is not cleared this can lead to SIGSEV in _Unwind_*
Comment 1 camper 2018-08-19 06:58:26 UTC
Created attachment 543982 [details, diff]
gcc-7.3.0-fix-x32-eh_return.patch

proposed patch (I don't really know what I'm doing)

compiles test case to
foo:
	pushq	%rbp
	movl	%esp, %ebp
	pushq	%rdx
	pushq	%rax
	movl	handler(%rip), %eax
	movl	offset(%rip), %edx
	movl	%edx, %ecx
	movl	%eax, %eax
	movq	%rax, 8(%ebp,%ecx)
	movq	-16(%ebp), %rax
	movq	-8(%ebp), %rdx
	leal	8(%rbp,%rcx), %ecx
	movl	0(%ebp), %ebp
	movl	%ecx, %esp
	ret
Comment 2 camper 2018-08-19 07:36:26 UTC
upstream bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87014
Comment 3 Sergei Trofimovich (RETIRED) gentoo-dev 2018-08-19 22:49:00 UTC
> Since the upper half of the return address is not cleared this can lead to SIGSEV in _Unwind_*

Does it actually happen for you or it's only expectation?
Comment 4 camper 2018-08-20 07:30:49 UTC
(In reply to Sergei Trofimovich from comment #3)
> > Since the upper half of the return address is not cleared this can lead to SIGSEV in _Unwind_*
> 
> Does it actually happen for you or it's only expectation?

Found it when trying to port libreoffice: https://bugs.gentoo.org/664058

test case (-O0):

using ll = long long;

void fillstack() {
    ll foo[] = {
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    };
}

void f(ll=-1,ll=-1,ll=-1,ll=-1,ll=-1,ll=-1,ll arg7_on_stack=-1) {
    throw 0;
}

void g() {
    try {
	f();
    } catch(int) {
    }
}

int main() {
   fillstack();
   g();
}
Comment 5 camper 2018-09-11 09:42:37 UTC
This has been fixed upstream:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87014#c11

Could we see this added to gentoo patch sets?
Comment 6 Larry the Git Cow gentoo-dev 2018-09-12 23:22:51 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/proj/gcc-patches.git/commit/?id=7bd9679989239cba83bb0c632bd2f0909b03d1b8

commit 7bd9679989239cba83bb0c632bd2f0909b03d1b8
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2018-09-12 23:20:12 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2018-09-12 23:20:12 +0000

    6/7/8 branches: add 101_all_x32-fix-eh-return-address.patch
    
    Pick upstream fix for x32 exception handler: """
    x86: Always update EH return address in word_mode
    
    On x86, return address is always popped in word_mode.  eh_return needs
    to put EH return address in word_mode on stack.
    """
    
    Reported-by: camper
    Bug: https://bugs.gentoo.org/664016
    Bug: https://gcc.gnu.org/PR87014
    Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>

 .../gentoo/101_all_x32-fix-eh-return-address.patch | 37 ++++++++++++++++++++++
 6.4.0/gentoo/README.history                        |  2 ++
 .../gentoo/99_all_x32-fix-eh-return-address.patch  | 37 ++++++++++++++++++++++
 7.3.0/gentoo/README.history                        |  1 +
 .../gentoo/101_all_x32-fix-eh-return-address.patch | 37 ++++++++++++++++++++++
 8.2.0/gentoo/README.history                        |  2 ++
 6 files changed, 116 insertions(+)
Comment 7 Sergei Trofimovich (RETIRED) gentoo-dev 2018-09-12 23:24:51 UTC
Queued patches for next gcc patchset. Thank you!
Comment 8 Larry the Git Cow gentoo-dev 2018-09-23 22:36:17 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=a8ff963554a3e28f8cd5905180b5c5428d1df7b8

commit a8ff963554a3e28f8cd5905180b5c5428d1df7b8
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2018-09-23 20:54:48 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2018-09-23 22:35:58 +0000

    sys-devel/gcc: cut 1.7 patchset for 6.4.0
    
    Two new patches:
    + 101_all_x32-fix-eh-return-address.patch
      fix exception handler ABI on x32
    + 102_all_respect-build-cxxflags.patch
      fix cross-build for arch-specific CXXFLAGS
    
    Bug: https://bugs.gentoo.org/664016
    Bug: https://bugs.gentoo.org/581406
    Package-Manager: Portage-2.3.49, Repoman-2.3.11

 sys-devel/gcc/Manifest            |  1 +
 sys-devel/gcc/gcc-6.4.0-r4.ebuild | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=fd1aa67de92c05d6a066e84ba5cd4236e637629e

commit fd1aa67de92c05d6a066e84ba5cd4236e637629e
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2018-09-23 20:48:40 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2018-09-23 22:35:58 +0000

    sys-devel/gcc: cut 1.7 patchset for 7.3.0
    
    Three new patches:
    + 98_all_msp430-partial-int.patch:
      fix msp430 code generator on complex types
    + 99_all_x32-fix-eh-return-address.patch
      fix exception handler ABI on x32
    + 100_all_respect-build-cxxflags.patch
      fix cross-build for arch-specific CXXFLAGS
    
    Bug: https://bugs.gentoo.org/664014
    Bug: https://bugs.gentoo.org/664016
    Bug: https://bugs.gentoo.org/581406
    Package-Manager: Portage-2.3.49, Repoman-2.3.11

 sys-devel/gcc/Manifest            |  1 +
 sys-devel/gcc/gcc-7.3.0-r5.ebuild | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=c7ec432dc64cefa8de88292efcc134cc8e9306c9

commit c7ec432dc64cefa8de88292efcc134cc8e9306c9
Author:     Sergei Trofimovich <slyfox@gentoo.org>
AuthorDate: 2018-09-23 20:36:46 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2018-09-23 22:35:58 +0000

    sys-devel/gcc: cut 1.3 patchset for 8.2.0
    
    Four new patches:
    + 101_all_x32-fix-eh-return-address.patch:
      fix exception handler ABI on x32
    + 102_all_respect-build-cxxflags.patch:
      fix cross-build for arch-specific CXXFLAGS
    + 103_all_mmix-fix-85666-p1.patch
    + 104_all_mmix-fix-85666-p2.patch
      backport mmix support
    
    Bug: https://bugs.gentoo.org/664016
    Bug: https://bugs.gentoo.org/581406
    Package-Manager: Portage-2.3.49, Repoman-2.3.11

 sys-devel/gcc/Manifest            |  1 +
 sys-devel/gcc/gcc-8.2.0-r3.ebuild | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)