Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 644492 - sys-libs/glibc SIGSEGV: stack pointer unalignment issue in __strpbrk_sse42
Summary: sys-libs/glibc SIGSEGV: stack pointer unalignment issue in __strpbrk_sse42
Status: RESOLVED NEEDINFO
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-01-13 20:50 UTC by vista
Modified: 2018-04-25 21:47 UTC (History)
1 user (show)

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


Attachments
screenshot (336314f4ab.png,240.03 KB, image/png)
2018-01-13 20:50 UTC, vista
Details
stable-glibc info (stable-glibc,5.50 KB, text/plain)
2018-01-13 21:28 UTC, vista
Details
unstable-glibc info (unstable-glibc,5.51 KB, text/plain)
2018-01-13 21:29 UTC, vista
Details

Note You need to log in before you can comment on or make changes to this bug.
Description vista 2018-01-13 20:50:15 UTC
Created attachment 514714 [details]
screenshot

Greetings all,

I have come across the following bug in glibc - I was running a 32-bit ELF when the SIGSEGV occured, I am not sure if this impacts 64-bit. I am also not sure whether this is a glibc or a gcc bug. I am running gcc-7.2.0

The __strpbrk_sse42 function uses the `movaps` SSE4.2 instruction, but the stack pointer is not guaranteed to be on a 16 byte alignment - as it was in my case.

Please see the attached screenshot with the bug in action. Please contact me via e-mail for a copy of the offending binary.

I have also attached `emerge --info` for the stable glibc (2.25-r9 as of writing) and the latest unstable one (2.26-r5 as of writing) as well; the bug surfaces in both releases.
stable glibc: https://paste.pound-python.org/show/DiHMM70ymdJn4J2Sfn8V/
unstable glibc: https://paste.pound-python.org/show/jIcMd8NJB5R1pmcrjHs3/

I was able to run the same program without crashing on another system with a newer generation CPU, this could perhaps be attributed to it using AVX instructions instead of SSE4.2.

A quite similar (if not the same) bug can be found on the Arch bug tracker: https://bugs.archlinux.org/task/541364
Comment 1 Sergei Trofimovich (RETIRED) gentoo-dev 2018-01-13 21:11:59 UTC
(In reply to vista from comment #0)
> Created attachment 514714 [details]
> screenshot
> 
> Greetings all,
> 
> I have come across the following bug in glibc - I was running a 32-bit ELF
> when the SIGSEGV occured, I am not sure if this impacts 64-bit. I am also
> not sure whether this is a glibc or a gcc bug. I am running gcc-7.2.0
> 
> The __strpbrk_sse42 function uses the `movaps` SSE4.2 instruction, but the
> stack pointer is not guaranteed to be on a 16 byte alignment - as it was in
> my case.
> 
> Please see the attached screenshot with the bug in action. Please contact me
> via e-mail for a copy of the offending binary.

Do you have a minimal source-based reproducer instead and attach it here? Or can you write one?
What does [esp] hold? I guess an unaligned pointer to the string you passed in.
Is it valid?

> I have also attached `emerge --info` for the stable glibc (2.25-r9 as of
> writing) and the latest unstable one (2.26-r5 as of writing) as well; the
> bug surfaces in both releases.
> stable glibc: https://paste.pound-python.org/show/DiHMM70ymdJn4J2Sfn8V/
> unstable glibc: https://paste.pound-python.org/show/jIcMd8NJB5R1pmcrjHs3/

Please attach emerge --info output to the bug.

> I was able to run the same program without crashing on another system with a
> newer generation CPU, this could perhaps be attributed to it using AVX
> instructions instead of SSE4.2.
> 
> A quite similar (if not the same) bug can be found on the Arch bug tracker:
> https://bugs.archlinux.org/task/541364

Says "task does not exist".
Comment 2 vista 2018-01-13 21:28:09 UTC
Created attachment 514716 [details]
stable-glibc info
Comment 3 Sergei Trofimovich (RETIRED) gentoo-dev 2018-01-13 21:29:37 UTC
glibc's implementation of __strpbrk_sse42 for 32/64-bit x86 should be here:

https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/multiarch/strcspn-c.c;h=857af10486032455a0cbfee0abb97b3c28911018;hb=HEAD#l82

Maybe you'll be able to trace it on your local system and spot what generated unaligned access.
Comment 4 vista 2018-01-13 21:29:44 UTC
Created attachment 514718 [details]
unstable-glibc info
Comment 5 vista 2018-01-13 21:38:54 UTC
(In reply to Sergei Trofimovich from comment #1)
> Do you have a minimal source-based reproducer instead and attach it here? Or
> can you write one?
> What does [esp] hold? I guess an unaligned pointer to the string you passed
> in.
> Is it valid?

I was trying to create a test-case, but I had no success in the end - the executable/binary I had problems with are "alien" to my system, they were compiled with with a gcc version that's more than a decade old. I had no problems running them on my newer system (which supports AVX/AVX2), though.

> Please attach emerge --info output to the bug.

Done.

> > A quite similar (if not the same) bug can be found on the Arch bug tracker:
> > https://bugs.archlinux.org/task/541364
> 
> Says "task does not exist".

There is indeed a typo in the URL, the correct one is: https://bugs.archlinux.org/task/54136
Comment 6 vista 2018-01-13 21:40:03 UTC
(In reply to Sergei Trofimovich from comment #3)
> glibc's implementation of __strpbrk_sse42 for 32/64-bit x86 should be here:
> 
> https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/multiarch/
> strcspn-c.c;h=857af10486032455a0cbfee0abb97b3c28911018;hb=HEAD#l82
> 
> Maybe you'll be able to trace it on your local system and spot what
> generated unaligned access.

I will try to trace it tomorrow
Comment 7 Sergei Trofimovich (RETIRED) gentoo-dev 2018-01-14 00:52:36 UTC
(In reply to vista from comment #5)
> I was trying to create a test-case, but I had no success in the end - the
> executable/binary I had problems with are "alien" to my system, they were
> compiled with with a gcc version that's more than a decade old.

Oh, that might might explain it.

GCC (before 4.5) used to assume stack alignment=16 on i386 when SSE was introduced and fixed since. See
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38496
for historical reference.

It might or might not be the issue you are experiencing. Depends on how callbacks into glibc and back happen in your program.

The workarounds to check the theory could be (any of):
- rebuild old binary with new compiler to apply according stack alignment
- rebuild glibc with -mstackrealign (might be tricky). That will cause every C function in glibc to autorealign stack.
Comment 8 vista 2018-01-14 15:57:57 UTC
(In reply to Sergei Trofimovich from comment #7)
> (In reply to vista from comment #5)
> > I was trying to create a test-case, but I had no success in the end - the
> > executable/binary I had problems with are "alien" to my system, they were
> > compiled with with a gcc version that's more than a decade old.
> 
> Oh, that might might explain it.
> 
> GCC (before 4.5) used to assume stack alignment=16 on i386 when SSE was
> introduced and fixed since. See
>     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
>     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38496
> for historical reference.
> 
> It might or might not be the issue you are experiencing. Depends on how
> callbacks into glibc and back happen in your program.
> 
> The workarounds to check the theory could be (any of):
> - rebuild old binary with new compiler to apply according stack alignment
> - rebuild glibc with -mstackrealign (might be tricky). That will cause every
> C function in glibc to autorealign stack.

I see. Sadly, this binary is one of those legacy applications where the vendor's not even around anymore.

I'll spin up a VM in the following week and test the latter solution.
Comment 9 Sergei Trofimovich (RETIRED) gentoo-dev 2018-01-25 21:43:11 UTC
Another thing to check is building glibc against recently added gcc-7.3.0.
I've encountered https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81481 when scrolling through changelog. Looks very similar as well.
Comment 10 Andreas K. Hüttel archtester gentoo-dev 2018-04-21 21:13:24 UTC
Any news here?
Comment 11 vista 2018-04-22 09:43:05 UTC
(In reply to Andreas K. Hüttel from comment #10)
> Any news here?

Hey Andreas,

I did not have time to properly debug this in the end, I managed to find a bit older version of the application which did not exhibit this behaviour.

Thanks,
vista