systemd / systemd

The systemd System and Service Manager
https://systemd.io
GNU General Public License v2.0
13.1k stars 3.75k forks source link

systemd seems to be failing to compile with gcc-10, -O3 and -flto #16224

Closed evverx closed 4 years ago

evverx commented 4 years ago

I'm not sure why it hasn't been caught by the "build test" workflow (where systemd is built with gcc-10, -O3 and -flto)

$ gcc --version
gcc (GCC) 10.1.1 20200507 (Red Hat 10.1.1-1)
Copyright (C) 2020 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.

$ CFLAGS='-Wall -Werror' meson   --optimization=3 -Db_lto=true   build

$ ninja ./build
...
[1050/1636] Linking target systemd-networkd
FAILED: systemd-networkd
cc  -o systemd-networkd 'systemd-networkd@exe/src_network_networkd.c.o' -flto -Wl,--as-needed -Wl,--no-undefined -Wl,-z,relro -Wl,-z,now -fstack-protector -Wl,--gc-sections -Wall -Werror -Wl,--start-group src/network/libnetworkd-core.a src/shared/libsystemd-shared-245.so src/libsystemd-network/libsystemd-network.a src/udev/libudev_static.a src/udev/libudev-basic.a -Wl,--end-group -pthread '-Wl,-rpath,$ORIGIN/src/network:$ORIGIN/src/shared:$ORIGIN/src/libsystemd-network:$ORIGIN/src/udev' -Wl,-rpath-link,/home/vagrant/systemd/build/src/network -Wl,-rpath-link,/home/vagrant/systemd/build/src/shared -Wl,-rpath-link,/home/vagrant/systemd/build/src/libsystemd-network -Wl,-rpath-link,/home/vagrant/systemd/build/src/udev
In function ‘dhcp6_assign_delegated_prefix’,
    inlined from ‘dhcp6_pd_prefix_assign’ at ../src/network/networkd-dhcp6.c:203:21,
    inlined from ‘dhcp6_pd_prefix_distribute.isra’ at ../src/network/networkd-dhcp6.c:333:21:
../src/network/networkd-dhcp6.c:1068:17: error: writing 8 bytes into a region of size 0 [-Werror=stringop-overflow=]
 1068 |                 memcpy(&address->in_addr.in6.s6_addr + 8, &link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
      |                 ^
../src/network/networkd-dhcp6.c: In function ‘dhcp6_pd_prefix_distribute.isra’:
../src/basic/alloc-util.h:96:16: note: at offset 196 to an object with size 0 allocated by ‘malloc’ here
   96 |         return malloc(size * need ?: 1);
      |                ^
lto1: all warnings being treated as errors
lto-wrapper: fatal error: cc returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
[1052/1636] Linking target test-network
FAILED: test-network
cc  -o test-network 'test-network@exe/src_network_test-network.c.o' -flto -Wl,--as-needed -Wl,--no-undefined -Wl,-z,relro -Wl,-z,now -fstack-protector -Wl,--gc-sections -Wall -Werror -Wl,--start-group src/network/libnetworkd-core.a src/shared/libsystemd-shared-245.so src/udev/libudev_static.a src/udev/libudev-basic.a src/libsystemd-network/libsystemd-network.a -Wl,--end-group -pthread '-Wl,-rpath,$ORIGIN/src/network:$ORIGIN/src/shared:$ORIGIN/src/udev:$ORIGIN/src/libsystemd-network' -Wl,-rpath-link,/home/vagrant/systemd/build/src/network -Wl,-rpath-link,/home/vagrant/systemd/build/src/shared -Wl,-rpath-link,/home/vagrant/systemd/build/src/udev -Wl,-rpath-link,/home/vagrant/systemd/build/src/libsystemd-network
In function ‘dhcp6_assign_delegated_prefix’,
    inlined from ‘dhcp6_pd_prefix_assign’ at ../src/network/networkd-dhcp6.c:203:21,
    inlined from ‘dhcp6_pd_prefix_distribute.isra’ at ../src/network/networkd-dhcp6.c:333:21:
../src/network/networkd-dhcp6.c:1068:17: error: writing 8 bytes into a region of size 0 [-Werror=stringop-overflow=]
 1068 |                 memcpy(&address->in_addr.in6.s6_addr + 8, &link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
      |                 ^
../src/network/networkd-dhcp6.c: In function ‘dhcp6_pd_prefix_distribute.isra’:
../src/basic/alloc-util.h:96:16: note: at offset 196 to an object with size 0 allocated by ‘malloc’ here
   96 |         return malloc(size * need ?: 1);
      |                ^
lto1: all warnings being treated as errors
lto-wrapper: fatal error: cc returned 1 exit status
compilation terminated.
evverx commented 4 years ago

FWIW apparently this issue was also reported by Coverity three days ago:

Hi,

Please find the latest report on new defect(s) introduced to systemd/systemd found with Coverity Scan.

2 new defect(s) introduced to systemd/systemd found with Coverity Scan.

New defect(s) Reported-by: Coverity Scan
Showing 2 of 2 defect(s)

** CID 1429928:  Memory - corruptions  (OVERRUN)
/src/network/networkd-dhcp6.c: 1068 in dhcp6_assign_delegated_prefix()

________________________________________________________________________________________________________
*** CID 1429928:  Memory - corruptions  (OVERRUN)
/src/network/networkd-dhcp6.c: 1068 in dhcp6_assign_delegated_prefix()
1062             if (r < 0)
1063                     return log_link_error_errno(link, r, "Failed to allocate address for DHCPv6 delegated prefix: %m");
1064     
1065             address->in_addr.in6 = *prefix;
1066     
1067             if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_delegation_prefix_token))

     CID 1429928:  Memory - corruptions  (OVERRUN)
     Overrunning buffer pointed to by "&address->in_addr.in6.__in6_u.__u6_addr8 + 8" of 16 bytes by passing it to a function which accesses it at byte offset 135 using argument "8UL".
1068                     memcpy(&address->in_addr.in6.s6_addr + 8, &link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
1069             else {
1070                     r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1071                     if (r < 0)
1072                             return log_link_warning_errno(link, r, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
1073             }

** CID 1429927:  Memory - corruptions  (OVERRUN)
/src/network/networkd-dhcp6.c: 1068 in dhcp6_assign_delegated_prefix()

________________________________________________________________________________________________________
*** CID 1429927:  Memory - corruptions  (OVERRUN)
/src/network/networkd-dhcp6.c: 1068 in dhcp6_assign_delegated_prefix()
1062             if (r < 0)
1063                     return log_link_error_errno(link, r, "Failed to allocate address for DHCPv6 delegated prefix: %m");
1064     
1065             address->in_addr.in6 = *prefix;
1066     
1067             if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_delegation_prefix_token))

Показать цитату целикомПоказать всю переписку
1068                     memcpy(&address->in_addr.in6.s6_addr + 8, &link->network->dhcp6_delegation_prefix_token.in6.s6_addr + 8, 8);
1069             else {
1070                     r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1071                     if (r < 0)
1072                             return log_link_warning_errno(link, r, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
1073             }
evverx commented 4 years ago

Since both gcc and Coverity are complaining about this I'll go ahead and add the "bug" label and the "246" milestone.

cc @yuwata @ssahani

ssahani commented 4 years ago

https://github.com/systemd/systemd/commit/845d784e544303ad486e7f18006559abad46716f and https://github.com/systemd/systemd/pull/16067/commits/a3d816199ba3ba9acefc7c95414a938e13f81723#diff-30d862b9251b785c8ee19db9d7a0321aR1068

here we need the casting

ssahani commented 4 years ago

@evverx I hope https://github.com/systemd/systemd/pull/16227 will fix this.

evverx commented 4 years ago

@ssahani could you cherry-pick the commit from https://github.com/systemd/systemd/pull/16225 just to make sure gcc no longer complains?

ssahani commented 4 years ago

Ok done.