llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.65k stars 11.84k forks source link

riscv64 apparent miscompilation causing segfault #113488

Open bscarlet opened 3 hours ago

bscarlet commented 3 hours ago

The following program segfaults for me when compiled and run:

#include <cstdarg>

struct Foo {
  char *buf0;
  char *buf1;
};

void (*x)(Foo *);

static void x_impl(Foo *) {}

void vfoo(char *buf0, const char *, va_list ap) {
  char buf1[80];

  Foo f{.buf0 = buf0, .buf1 = buf1};

  x = x_impl;

  va_list ap2;
  va_copy(ap2, ap);
  x(&f);
  va_end(ap2);
}

void foo(const char *format, ...) {
  va_list ap;
  va_start(ap, format);

  char buffer[4096];
  vfoo(buffer, format, ap);

  va_end(ap);
}

int main(int argc, char **argv) {
  foo("");
  return 0;
}

Using clang ... -O1 -mrvv-vector-bits=512 -target riscv64-unknown-linux -march=rv64gcv1p0 ...

(... includes flags I don't think are relevant, like paths about my environment etc, and - among other things - examining the output assembly)

This problem started for me at the merge of https://github.com/llvm/llvm-project/pull/110809

This tarball contains the source above, and the assembly I get with and without the problem. riscv-varargs-crash.tgz

The differences in the assembly do look to me like probably results of the above merge.

llvmbot commented 3 hours ago

@llvm/issue-subscribers-backend-risc-v

Author: Benjamin S. Scarlet (bscarlet)

The following program segfaults for me when compiled and run: ``` #include <cstdarg> struct Foo { char *buf0; char *buf1; }; void (*x)(Foo *); static void x_impl(Foo *) {} void vfoo(char *buf0, const char *, va_list ap) { char buf1[80]; Foo f{.buf0 = buf0, .buf1 = buf1}; x = x_impl; va_list ap2; va_copy(ap2, ap); x(&f); va_end(ap2); } void foo(const char *format, ...) { va_list ap; va_start(ap, format); char buffer[4096]; vfoo(buffer, format, ap); va_end(ap); } int main(int argc, char **argv) { foo(""); return 0; } ``` Using `clang ... -O1 -mrvv-vector-bits=512 -target riscv64-unknown-linux -march=rv64gcv1p0 ...` (... includes flags I don't think are relevant, like paths about my environment etc, and - among other things - examining the output assembly) This problem started for me at the merge of https://github.com/llvm/llvm-project/pull/110809 This tarball contains the source above, and the assembly I get with and without the problem. [riscv-varargs-crash.tgz](https://github.com/user-attachments/files/17497474/riscv-varargs-crash.tgz) The differences in the assembly do look to me like probably results of the above merge.