llvm / llvm/llvm-project

[X86] Generated Binary Crash Unless `-disable-postra-machine-sink` Specified

Open
#224,802 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backend:X86 crash-on-valid
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Given this code:

```cpp
#include
#include
#include

struct Parser
{
virtual int Put(std::span, char *)
{
return 0;
}

virtual void Finish()
{
throw 1;
}

virtual bool Complete() const noexcept
{
return false;
}

virtual bool Status() const
{
return true;
}

std::string Decode(std::span input);
};

std::string Parser::Decode(std::span input)
{
char chunk[1]{};
std::string body;
unsigned offset = 0;
while (true)
{
if (offset == input.size())
{
Finish();
}
else if (!Complete())
{
const auto progress = Put(input.subspan(offset), chunk);
offset += progress;
body.append(chunk, 1);
}
const auto status = Status();
if (!status)
{
continue;
}
return body;
}
}

int main()
{
Parser parser;
// Keep the virtual calls in Decode opaque without compiler-specific attributes.
try
{
volatile auto decode = &Parser::Decode;
(void)(parser.*decode)({});
}
catch (int error)
{
std::printf("Caught %d\n", error);
return error == 1 ? 0 : 1;
}
return 2;
}
```

When compiled for `x86` at `-O2`, the generated binary segfaults: https://godbolt.org/z/z4Mq4jnqW

These configurations are known **not** to fail:
- Other architectures (`x86_64`, `aarch64`) at `-O2`.
- `x86` at `-O0`.
- `x86` at `-O2`, but with `-mllvm -disable-postra-machine-sink`.
- `x86` at `-O2`, but with Clang 14.0.0: https://godbolt.org/z/cTTbdrrvM

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the x86 -O2 crash with the provided C++ program and compare it with -disable-postra-machine-sink, other architectures, and Clang 14. Investigate the post-RA machine-sink pass and its x86-specific behavior. Done means the generated x86 binary no longer segfaults without disabling the pass, while the listed non-failing configurations remain valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.