NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: toolchain/60289 (NetBSD 11 gcc mis-optimises some NULL checks)



Synopsis: NetBSD 11 gcc mis-optimises some NULL checks

State-Changed-From-To: open->analyzed
State-Changed-By: riastradh%NetBSD.org@localhost
State-Changed-When: Sat, 23 May 2026 21:59:11 +0000
State-Changed-Why:
This actually may be a gcc bug, because we compile the kernel with
-fno-delete-null-pointer-checks, which is supposed to prevent gcc from
eliminating these checks:

https://nxr.netbsd.org/xref/src/sys/conf/Makefile.kern.inc?r=1.306#108
https://gcc.gnu.org/onlinedocs/gcc-14.3.0/gcc/Optimize-Options.html#index-fdelete-null-pointer-checks

It's not NetBSD-specific -- here's a godbolt.org example with 14.3.0,
presumably not on NetBSD:

https://godbolt.org/z/aT6xqdMer

.LC0:
        .string "A "
...
        mov     edi, OFFSET FLAT:.LC0
        call    printf
        test    BYTE PTR [rbx+16], 1
        je      .L9

So I think someone should file a bug with gcc upstream.

In the mean time, we should also fix this idiom and avoid evaluating
&barp->foo when barp might be null -- we did nix some cases of it when
clang found them, but they were slightly different, like this:

https://mail-index.netbsd.org/source-changes/2014/11/06/msg060206.html

> Fix little C issues in i915drmkms hindering the Clang build.
> 
> - Test `x == NULL', not `&container_of(x, t, base)->base == NULL'.
> ...

-       obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
-       if (&obj->base == NULL)
+       gobj = drm_gem_object_lookup(dev, file, args->handle);
+       if (gobj == NULL)
                return -ENOENT;
+       obj = to_intel_bo(gobj);

The null pointer check in _this_ case vanished some time between gcc10
(as shipped in netbsd-10) and gcc11:

https://godbolt.org/z/fT6P66KT4 (gcc 10.5)

        mov     edi, OFFSET FLAT:.LC0
        call    printf
        test    rbx, rbx
        je      .L3
        test    BYTE PTR [rbx+16], 1
        jne     .L4

https://godbolt.org/z/Ee8hW3z37 (gcc 11.5)

        mov     edi, OFFSET FLAT:.LC0
        call    printf
        test    BYTE PTR [rbx+16], 1
        je      .L9






Home | Main Index | Thread Index | Old Index