Bug 100380 - Segfault when using inline asm
Summary: Segfault when using inline asm
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: jit (show other bugs)
Version: 10.3.1
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords: inline-asm
Depends on:
Blocks:
 
Reported: 2021-05-02 01:51 UTC by Antoni
Modified: 2021-09-13 22:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-05-11 00:00:00


Attachments
Reproducer for the bug (913 bytes, text/x-csrc)
2021-05-02 01:51 UTC, Antoni
Details
Fixed reproducer (910 bytes, text/x-csrc)
2021-05-02 01:54 UTC, Antoni
Details
Working code (908 bytes, text/x-csrc)
2021-05-02 02:20 UTC, Antoni
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Antoni 2021-05-02 01:51:58 UTC
Created attachment 50729 [details]
Reproducer for the bug

Hi.
The attached example produce a segfault when trying to compile code using inline assembly.
Thanks.
Comment 1 Antoni 2021-05-02 01:54:47 UTC
Created attachment 50730 [details]
Fixed reproducer
Comment 2 Antoni 2021-05-02 02:20:27 UTC
Created attachment 50731 [details]
Working code

So, the segfault seems to happen when creating the variable after creating the extended asm expression.
Here's a working version of the code.
Comment 3 Martin Liška 2021-05-11 12:04:37 UTC
I'll take a look.
Comment 4 Antoni 2021-05-15 14:13:45 UTC
I just had a similar issue when developing a new feature for libgccjit and it might be the same problem. If it is (I haven't checked in this case), here's what's happening:

 * The asm is replayed.
 * The asm tries to access the replayed variable (which wasn't replayed yet because it was created after the asm).
 * Segfault (the rest is not executed, but is shown to explain what's happening)
 * The variable is replayed (too late because it was NULL when accessed by the asm).

Again it's to be verified, and I'm not sure what should be the solution to this problem because the mementos are replayed in the order they were created.
Comment 5 Antoni 2021-05-16 13:54:54 UTC
I can confirm that the problem is indeed what I described in my previous post.

One solution would be to check if the rvalue was replayed (and if not, replay it now), but that involves adding this check everywhere, so that seems very invasive.

Do you think there's a better solution?
Comment 6 Martin Liška 2021-08-16 11:31:48 UTC
Leaving to David..
Comment 7 Antoni 2021-09-13 22:31:42 UTC
Since then, I found a workaround to fix the similar segfault in my other feature.
It might work for solving this and goes like this:
instead of trying to access the rvalue when first replaying the asm, create an intermediate memento that does the work of add_output_operand (and most likely the other actions like add_input_operand).
It works since this memento will necessarily be created after both the asm and the variable and thus, both will have been replayed when it's time to replay the new 'add_output_operand' memento.