llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang] Crash when using [[clang::preserve_none]]

Open philnik777 opened this issue 8 months ago • 2 comments

struct S {
  unsigned long long arr[3];
};

[[clang::preserve_none]] void call(S);

[[clang::preserve_none]] void func() {
  S s{};
  call(s);
}

Causes Clang to crash at -O1: https://godbolt.org/z/E77TdTn9P

philnik777 avatar Mar 27 '26 08:03 philnik777

Look like this started in clang-22

shafik avatar Mar 31 '26 22:03 shafik

Happened to run into this when doing some experiment. Looked into it a bit, as far as I can see this is X86 backend specific:

When using -O(>=1), the TRE pass runs which stage tail calls, and together with preserve_none with, IR looks like:

tail call preserve_nonecc void @_Z14func(ptr noundef nonnull byval(%struct.S) align 8 %1)

Now we have tailcall on a func with preserve_none cc, and a byval in the parameter.

When lowering to DAG, we will consider if this is a tailcall/sibling call. It slips through the isEligibleForSiblingCallOpt check and thinks this is a sibling call, because byval for preserve_none is passed as a register, not memory.

As a result, the lowering logic thinks this is a sibling call and does not reserve stack space for it. But preserve_none with byval would try copy outgoing args in the stack, corrupting the frame. This leads to a runtime crash.

The propose fix is to check for non-standard CC like preserve_none and disqualify it for sibling call. Will put up a PR.

kuroyukiasuna avatar Apr 15 '26 17:04 kuroyukiasuna