Skip to content

[clang] Wrong optimization when -D_FORTIFY_SOURCE is passed #63065

@ceseo

Description

@ceseo

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.

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions