https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129262
Bug ID: 129262
Summary: -flto-incremental cache can link wrong .ltrans.o on
pruning
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: lto
Assignee: unassigned at gcc dot gnu.org
Reporter: luolongjuna at gmail dot com
Target Milestone: ---
While reading the LTRANS cache code, I found this problem. In the worst case,
it silently links the wrong object file (resulting in wrong code without any
warning).
The problem here is that lto-wrapper passes the cached .ltrans.o filename
directly to ld, and exits before ld reads it.
Consider several big projects building together and sharing the same cache
directory (which is explicitly allowed by the GCC manual). When the cache hits
the size limit, pruning deletes old files, and another build can reuse the same
filename before ld opens it.
Here is a reduced case:
mkdir cache slow
# delay returning to ld after the real lto-wrapper exits, widening the race
window
cat > slow/lto-wrapper <<EOF
#!/bin/sh
$(gcc -print-prog-name=lto-wrapper) "\$@"
sleep 2
EOF
chmod +x slow/lto-wrapper
echo 'int main(void) { return 1; }' > a.c
echo 'int main(void) { return 2; }' > b.c
echo 'int main(void) { return 3; }' > c.c
gcc -flto -c a.c b.c c.c
# link app_a, and meanwhile build app_b and app_c with the same cache
gcc -flto -flto-incremental=cache -flto-incremental-cache-size=1 -Bslow/ a.o -o
app_a &
sleep 0.5
gcc -flto -flto-incremental=cache -flto-incremental-cache-size=1 b.o -o app_b
gcc -flto -flto-incremental=cache -flto-incremental-cache-size=1 c.o -o app_c
wait
./app_a; echo $?
# -> 3 (expected: 1)
Reproduced on x86_64 Linux with GCC 17 trunk (cdb8a10e1a8).