<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63065>63065</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Wrong optimization when -D_FORTIFY_SOURCE is passed
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ceseo
      </td>
    </tr>
</table>

<pre>
    When compiling a program that calls `bcopy`, clang replaces it by a `memmove` call (as per `llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp`). This is not taking into account that it should generate a `__memmove_chk` when any level of `-D_FORTIFY_SOURCE` is passed to the compiler.

I think the issue is caused by the fact that this `bcopy` to `memmove` conversion should be done somewhere else in the code (probably in `SemaChecking.cpp`).

This issue was seen while building the glibc testsuite (`debug/tst-fortify*.c`), in our work to add llvm/clang support for it upstream.  gcc builds these tests correctly.

To reproduce:

The branch can be accessed [here](https://github.com/zatrazz/glibc/tree/azanella/clang).

Configure glibc with:

```
AR="${PATH_TO_LLVM}/llvm-ar" \
AS="${PATH_TO_LLVM}/llvm-as" \
NM="${PATH_TO_LLVM}/llvm-nm" \
OBJDUMP="${PATH_TO_LLVM}/llvm-objdump" \
OBJCOPY="${PATH_TO_LLVM}/llvm-objcopy" \
RANLIB="${PATH_TO_LLVM}/llvm-ranlib" \
READELF="${PATH_TO_LLVM}/llvm-readelf" \
STRIP="${PATH_TO_LLVM}/llvm-strip" \
CC="${PATH_TO_LLVM}/clang -target ${arch}-linux-gnu -fuse-ld=lld -Wno-unused-command-line-argument" \
CXX="${PATH_TO_LLVM}/clang++ -target ${arch}-linux-gnu -fuse-ld=lld -Wno-unused-command-line-argument" \
LD="${PATH_TO_LLVM}/ld.lld" \
CFLAGS="-O2 -g" \
CXXFLAGS="${CFLAGS}" \
${GLIBCSRC}/configure \
            --prefix=/usr \
 --host=${arch}-linux-gnu \
 --with-binutils="${PATH_TO_LLVM}" \
 --enable-stack-protector=all \
            --enable-tunables=yes \
 --enable-bind-now=yes \
 --enable-profile=no
```

then `make` and `make check`

Failures will be inside the `debug` directory. Screen output is in `tst-fortify-cc-default-*.out` and binaries will be in `tst-fortify-cc-default-*o`.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vk1v27oS_TX0ZiBDoWw5XnhhW1VfHtymiNPXdmVQ1EhiQ5ECP-I6v_6Bkt3aubm53twgkCFpDufMETlnmLWiVogLMl2RaTZi3jXaLDha1KNCl4fFtwYVcN12QgpVA4PO6NqwFlzDHHAmpQWSxgXX3YGkMaFr4JKpGgx2knG0IBwUB2AhqsW21c9I0rhHAqG3zEKHJryU8rklNJeiIDR_NEzZSpvWEpp_dUKG361oOymqw0YU65B4zLuuzzkfw2MjLAgLSjtw7ClwFcppYJxrr9xAVziwjfayhBoVGuZw4LXbHZntePMU2O1D1UwdQOIzStBViIqyXX7_8HiX_9ht778-rD-ESGGhY9ZiCU6Da_CoFZoxiTMSL4frHbhGqKc-QFjrwxU48wFXHPrHFeNHli6UcqZpWPmVeFo9o7FCq1M9BUKpFYLVLe4bNAgoLYJQR04lBrE7owtWyEN4TtJ4iy1bN8iDWmdanhM_yhoY75kFi6hg3wiJUHghyyBzSFBLUXBwaJ31wvW5SBqXWPia0NxZF1XaOFEdCF2O-ZAnbBWhQHsDe22eQpWsLOG4DYZNZH3XaeOg0iZ8PN9ZZ5C1Y4Ca84GCDQQsDsmBa2OQO3m4rEKH7Wh06TmSZHlZIEJhmOINcKaCjoxz7D8oma6CkmSaEXrbONfZgKU5oXktXOOLMdeB6gtzhr28hMdBhlCwQSQ0Zy9MoZTsVM5rcddaVaL25qTfXrjmFb0g1fDf3y4fSJIRSgmdkNnqy_LxP7vH-91m879PZJaFwyOf24gZQimQ6fqI2V6DsReYz5-uwKj2AnO_-m_29dOXK4C6-Fn6tnuNXt9_-XEduj8Y5-iH5efN3eoKsGGqbzDn2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