https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122895

            Bug ID: 122895
           Summary: Segfault with target_clones attribute when built with
                    -fprofile-generate
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: info at kleisauke dot nl
  Target Milestone: ---

This was reported downstream in libvips at
https://github.com/libvips/libvips/issues/4747.

`reduced.c`
```
__attribute__((target_clones("default,avx")))
static void
sum_private(const int *a, const int *b, int *result, int n) {
    for (int i = 0; i < n; i++)
        result[i] = a[i] + b[i];
}

void
sum(const int *a, const int *b, int *result, int n) {
    sum_private(a, b, result, n);
}
```

`main.c`
```
#include <stdio.h>

extern void sum(const int *a, const int *b, int *result, int n);

int main() {
    int n = 8;
    int a[8] = {1,2,3,4,5,6,7,8};
    int b[8] = {10,20,30,40,50,60,70,80};
    int result[8];

    sum(a, b, result, n);

    printf("Result: ");
    for (int i = 0; i < n; i++)
        printf("%d ", result[i]);
    printf("\n");

    return 0;
}
```

```
$ CFLAGS="-O0 -g -fprofile-generate"
$ gcc $CFLAGS -fPIC -c reduced.c -o reduced.o
$ gcc $CFLAGS -shared -o libfoo.so reduced.o
$ gcc $CFLAGS main.c -L. -lfoo
$ LD_LIBRARY_PATH=. ./a.out
Segmentation fault (core dumped)
$ coredumpctl debug
...
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007ff9543a9be7 in sum_private[resolver] () at reduced.c:11
11      }
$ gcc --version
gcc (GCC) 15.2.1 20251111 (Red Hat 15.2.1-4)
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

Interestingly, the issue seems to occur only when the `target_clones` attribute
is applied to a static function. For example, I could not reproduce this when
merging `sum_private` and `sum`.

Reply via email to