Bug 67265 - [x86] 'asm' operand has impossible constraints with -fstack-check
Summary: [x86] 'asm' operand has impossible constraints with -fstack-check
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 5.2.0
: P3 normal
Target Milestone: 4.9.4
Assignee: Eric Botcazou
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-08-19 00:32 UTC by Johannes Dewender
Modified: 2023-10-27 17:57 UTC (History)
2 users (show)

See Also:
Host: i686-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-10-28 00:00:00


Attachments
minimal working example (C file) (91 bytes, text/plain)
2015-08-19 00:32 UTC, Johannes Dewender
Details
Makefile with failing, working and workaround flags (74 bytes, text/plain)
2015-08-19 00:38 UTC, Johannes Dewender
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Johannes Dewender 2015-08-19 00:32:41 UTC
Created attachment 36209 [details]
minimal working example (C file)

When compiling a 32 bit build of libx264 (on a 64 bit machine) it works in general, even with PIC.
However the build breaks when I add -fstack-check to CFLAGS.

The failing code is:

int a, b, c, d, e;
int fn1() {
  __asm__("" : "+r"(c), "+r"(e), "+r"(d), "+r"(a) : ""(b), "mg"(fn1), "mm"(c));
}

(attached as me.c)

This works:
 gcc -O3 -m32 -fPIC -c me.c

This fails (and should work)
 gcc -O3 -m32 -fstack-check -fPIC -c me.c

This also works (as a workaround):
 gcc -O3 -m32 -fstack-check -c me.c


The error message is:
me.c: In function ‘fn1’:
me.c:3:3: error: ‘asm’ operand has impossible constraints
   __asm__("" : "+r"(c), "+r"(e), "+r"(d), "+r"(a) : ""(b), "mg"(fn1), "mm"(c));
   ^


The related discussion for x264 is here:
https://mailman.videolan.org/pipermail/x264-devel/2015-August/011246.html

The flag -fstack-check is added by default on Arch Linux when building packages, but also additionally using hardened wrapper, where I also mentioned the problem:
https://github.com/thestinger/hardening-wrapper/issues/6

I am the maintainer of lib32-libx264 on Arch Linux where the problem came up originally:
https://aur.archlinux.org/packages/lib32-libx264/
Comment 1 Johannes Dewender 2015-08-19 00:38:37 UTC
Created attachment 36210 [details]
Makefile with failing, working and workaround flags

I found the bug using gcc 5.2.0 (Arch Linux), but I also tested gcc 5.1.0 and gcc 4.9.2.
So this is not a brand new bug. Not sure if it did work as expected in versions before that.
Comment 2 Andrew Pinski 2015-08-19 00:45:20 UTC
X86 is very much under huge register pressure and that inline-asm uses 4 registers as input/output and two memory ones. -fastback-check forces one more register not to be available really.
Comment 3 Daniel Micay 2015-08-19 02:08:11 UTC
It only temporarily needs a register to perform a write. The implementation might currently have to reserve it but it *shouldn't* have to. It should be free other than guaranteeing a one byte write per page.
Comment 4 Eric Botcazou 2015-10-28 17:11:16 UTC
The implementation currently reserves the frame pointer to be able to unwind the stack and recover from the stack overflow.
Comment 5 Eric Botcazou 2015-10-28 17:37:19 UTC
For the minimal testcase we can do:

Index: ira.c
===================================================================
--- ira.c       (revision 229402)
+++ ira.c       (working copy)
@@ -2273,7 +2273,9 @@ ira_setup_eliminable_regset (void)
        || (cfun->calls_alloca && EXIT_IGNORE_STACK)
        /* We need the frame pointer to catch stack overflow exceptions
          if the stack pointer is moving.  */
-       || (flag_stack_check && STACK_CHECK_MOVING_SP)
+       || (flag_stack_check
+          && STACK_CHECK_MOVING_SP
+          && (!crtl->is_leaf || cfun->calls_alloca))
        || crtl->accesses_prior_frames
        || (SUPPORTS_STACK_ALIGNMENT && crtl->stack_realign_needed)
        /* We need a frame pointer for all Cilk Plus functions that use

i.e. do not reserve the frame pointer if the function is leaf and doesn't call alloca, but this might not be sufficient for the original testcase.
Comment 6 Eric Botcazou 2015-10-28 17:40:52 UTC
Let's try to do something.
Comment 7 Eric Botcazou 2015-11-11 14:23:16 UTC
Author: ebotcazou
Date: Wed Nov 11 14:22:43 2015
New Revision: 230168

URL: https://gcc.gnu.org/viewcvs?rev=230168&root=gcc&view=rev
Log:
	PR target/67265
	* ira.c (ira_setup_eliminable_regset): Do not necessarily create the
	frame pointer for stack checking if non-call exceptions aren't used.
	* config/i386/i386.c (ix86_finalize_stack_realign_flags): Likewise.

Added:
    trunk/gcc/testsuite/gcc.target/i386/pr67265.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/ira.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Eric Botcazou 2015-11-11 14:25:11 UTC
Author: ebotcazou
Date: Wed Nov 11 14:24:39 2015
New Revision: 230170

URL: https://gcc.gnu.org/viewcvs?rev=230170&root=gcc&view=rev
Log:
	PR target/67265
	* config/i386/i386.c (ix86_finalize_stack_realign_flags): Likewise.

Modified:
    trunk/gcc/config/i386/i386.c
Comment 9 Eric Botcazou 2015-11-11 14:56:49 UTC
Author: ebotcazou
Date: Wed Nov 11 14:56:17 2015
New Revision: 230176

URL: https://gcc.gnu.org/viewcvs?rev=230176&root=gcc&view=rev
Log:
	PR target/67265
	* ira.c (ira_setup_eliminable_regset): Do not necessarily create the
	frame pointer for stack checking if non-call exceptions aren't used.
	* config/i386/i386.c (ix86_finalize_stack_realign_flags): Likewise.

Added:
    branches/gcc-5-branch/gcc/testsuite/gcc.target/i386/pr67265.c
      - copied unchanged from r230168, trunk/gcc/testsuite/gcc.target/i386/pr67265.c
Modified:
    branches/gcc-5-branch/gcc/ChangeLog
    branches/gcc-5-branch/gcc/config/i386/i386.c
    branches/gcc-5-branch/gcc/ira.c
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
Comment 10 Eric Botcazou 2015-11-11 16:05:06 UTC
Author: ebotcazou
Date: Wed Nov 11 16:04:34 2015
New Revision: 230179

URL: https://gcc.gnu.org/viewcvs?rev=230179&root=gcc&view=rev
Log:
	PR target/67265
	* ira.c (ira_setup_eliminable_regset): Do not necessarily create the
	frame pointer for stack checking if non-call exceptions aren't used.
	* config/i386/i386.c (ix86_finalize_stack_realign_flags): Likewise.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/gcc.target/i386/pr67265.c
      - copied unchanged from r230177, trunk/gcc/testsuite/gcc.target/i386/pr67265.c
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/config/i386/i386.c
    branches/gcc-4_9-branch/gcc/ira.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
Comment 11 Eric Botcazou 2015-11-11 16:10:28 UTC
Fixed on all active branches.
Comment 12 Eric Botcazou 2015-11-11 16:11:09 UTC
Fixed on all active branches.
Comment 13 Eric Botcazou 2015-11-12 11:55:42 UTC
Author: ebotcazou
Date: Thu Nov 12 11:55:11 2015
New Revision: 230245

URL: https://gcc.gnu.org/viewcvs?rev=230245&root=gcc&view=rev
Log:
	PR target/67265
	* config/i386/i386.c (ix86_adjust_stack_and_probe): Remove obsolete
	assertion on the CFA register.

Added:
    trunk/gcc/testsuite/gcc.target/i386/pr67265-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/i386.c
    trunk/gcc/testsuite/ChangeLog
Comment 14 Eric Botcazou 2015-11-12 11:59:54 UTC
Author: ebotcazou
Date: Thu Nov 12 11:59:23 2015
New Revision: 230247

URL: https://gcc.gnu.org/viewcvs?rev=230247&root=gcc&view=rev
Log:
	PR target/67265
	* config/i386/i386.c (ix86_adjust_stack_and_probe): Remove obsolete
	assertion on the CFA register.

Added:
    branches/gcc-5-branch/gcc/testsuite/gcc.target/i386/pr67265-2.c
      - copied unchanged from r230245, trunk/gcc/testsuite/gcc.target/i386/pr67265-2.c
Modified:
    branches/gcc-5-branch/gcc/ChangeLog
    branches/gcc-5-branch/gcc/config/i386/i386.c
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
Comment 15 Eric Botcazou 2015-11-12 12:02:11 UTC
Author: ebotcazou
Date: Thu Nov 12 12:01:40 2015
New Revision: 230249

URL: https://gcc.gnu.org/viewcvs?rev=230249&root=gcc&view=rev
Log:
	PR target/67265
	* config/i386/i386.c (ix86_adjust_stack_and_probe): Remove obsolete
	assertion on the CFA register.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/gcc.target/i386/pr67265-2.c
      - copied unchanged from r230247, trunk/gcc/testsuite/gcc.target/i386/pr67265-2.c
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/config/i386/i386.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog