Bug 262977 - Preprocessor error in sys/contrib/zlib/crc32.c on arm64
Summary: Preprocessor error in sys/contrib/zlib/crc32.c on arm64
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: arm64 Any
: --- Affects Some People
Assignee: Xin LI
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-04-01 11:27 UTC by John F. Carr
Modified: 2022-07-04 23:25 UTC (History)
3 users (show)

See Also:


Attachments
define Z_U8 (646 bytes, patch)
2022-04-01 14:39 UTC, John F. Carr
no flags Details | Diff
Another possible patch for testing (366 bytes, patch)
2022-04-03 06:07 UTC, Xin LI
no flags Details | Diff
Another patch to define both Z_U4 and Z_U8 (375 bytes, patch)
2022-04-03 08:57 UTC, Peter Jeremy
no flags Details | Diff
Proposed patch (861 bytes, patch)
2022-04-03 19:03 UTC, Xin LI
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description John F. Carr 2022-04-01 11:27:52 UTC
On main (3256b7ca36e739a08f141a7ee52b9db9d995db85 and some earlier revisions) my kernel does not compile on arm64 because of the error below.  The macro W (word size) is not defined.

cc -target aarch64-unknown-freebsd14.0 --sysroot=/usr/obj/usr/home/jfc/freebsd/src/arm64.aarch64/tmp -B/usr/obj/usr/home/jfc/freebsd/src/arm64.aarch64/tmp/usr/bin  -O2 -pipe -fno-common -mcpu=cortex-a57  -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -DKLD_TIED -nostdinc   -DHAVE_KERNEL_OPTION_HEADERS -include /usr/obj/usr/home/jfc/freebsd/src/arm64.aarch64/sys/STRIATUS/opt_global.h -I. -I/usr/home/jfc/freebsd/src/sys -I/usr/home/jfc/freebsd/src/sys/contrib/ck/include -fno-common -g -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fPIC -fdebug-prefix-map=./machine=/usr/home/jfc/freebsd/src/sys/arm64/include -I/usr/obj/usr/home/jfc/freebsd/src/arm64.aarch64/sys/STRIATUS   -mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 -mstack-protector-guard-offset=0  -MD  -MF.depend.crc32.o -MTcrc32.o -mgeneral-regs-only -ffixed-x18 -ffreestanding -fwrapv -fstack-protector -Wall -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wcast-qual -Wundef -Wno-pointer-sign -D__printf__=__freebsd_kprintf__ -Wmissing-include-dirs -fdiagnostics-show-option -Wno-unknown-pragmas -Wno-error=tautological-compare -Wno-error=empty-body -Wno-error=parentheses-equality -Wno-error=unused-function -Wno-error=pointer-sign -Wno-error=shift-negative-value -Wno-address-of-packed-member -Wno-error=unused-but-set-variable -Wno-format-zero-length     -std=iso9899:1999 -c /usr/home/jfc/freebsd/src/sys/contrib/zlib/crc32.c -o crc32.o
/usr/home/jfc/freebsd/src/sys/contrib/zlib/crc32.c:106:61: error: 'W' is not defined, evaluates to 0 [-Werror,-Wundef]
#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
Comment 1 Ed Maste freebsd_committer freebsd_triage 2022-04-01 13:04:10 UTC
Can you give this (untested) patch a try:

diff --git a/sys/conf/files b/sys/conf/files
index 6639485509bc..335d240e0526 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -4073,7 +4073,8 @@ contrib/zlib/compress.c           optional crypto | geom_uzip | \
        mxge | ddb_ctf | gzio | zfs | zlib \
        compile-with "${NORMAL_C} -Wno-cast-qual"
 contrib/zlib/crc32.c           optional crypto | geom_uzip | \
-       mxge | ddb_ctf | gzio | zfs | zlib
+       mxge | ddb_ctf | gzio | zfs | zlib \
+       compile-with "${NORMAL_C} -Wno-undef"
 contrib/zlib/deflate.c         optional crypto | geom_uzip | \
        mxge | ddb_ctf | gzio | zfs | zlib \
        compile-with "${NORMAL_C} -Wno-cast-qual"

I'd like to submit a patch upstream to do this in a more elegant way than just turning off the warning but that will take more time.
Comment 2 John F. Carr 2022-04-01 14:39:55 UTC
Created attachment 232867 [details]
define Z_U8

My kernel builds with this patch and boots with ZFS root.  zconf.h might be a better place to define Z_U8.
Comment 3 Ed Maste freebsd_committer freebsd_triage 2022-04-01 16:51:55 UTC
(In reply to John F. Carr from comment #2)
In addition to or instead of the change in comment #1?
Comment 4 John F. Carr 2022-04-01 16:53:05 UTC
My kernel builds with my patch only, without the patch to add -Wno-undef.
Comment 5 Xin LI freebsd_committer freebsd_triage 2022-04-03 06:07:05 UTC
Created attachment 232914 [details]
Another possible patch for testing

Hi,

Unfortunately I wasn't able to reproduce this (both cross-compile in `make tinderbox` and native with a GENERIC configuration adding `option ZFS` to make it build zlib into the kernel).  Could you please test if the attached patch would help for your situation?
Comment 6 Peter Jeremy freebsd_committer freebsd_triage 2022-04-03 08:57:44 UTC
Created attachment 232915 [details]
Another patch to define both Z_U4 and Z_U8

This is my proposed patch to fix the problem reported in this bug.
Comment 7 John F. Carr 2022-04-03 09:11:38 UTC
To reproduce you need -mcpu=cortex-a57 in the compile command, i.e. CPUTYPE=cortex-a57 in the make command or in /etc/make.conf.
Comment 8 John F. Carr 2022-04-03 09:19:00 UTC
More specifically, the macro __ARM_FEATURE_CRC32 needs to be defined to trigger the compilation error, otherwise short circuit evaluation skips the test for W == 8.  Using -mcpu=cortex-a57 defines the macro.

#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
Comment 9 Peter Jeremy freebsd_committer freebsd_triage 2022-04-03 09:24:52 UTC
TL;DR: I suspect cd8822075a38 is resulting in suboptimal behaviour on all architectures but is only a build failure on arm64 with CRC32 extensions.

I'm seeing the same failure as John.  In my case, it's failing to build the zlib module (I have ZFS in my kernel but the compile is dying before it gets that far).  The relevant command line is:
cc -target aarch64-unknown-freebsd14.0 --sysroot=/usr/obj/usr/src/arm64.aarch64/tmp -B/usr/obj/usr/src/arm64.aarch64/tmp/usr/bin  -O2 -pipe -fno-common -mcpu=cortex-a53  -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -DKLD_TIED -nostdinc   -DHAVE_KERNEL_OPTION_HEADERS -include /usr/obj/usr/src/arm64.aarch64/sys/ROCK64/opt_global.h -I. -I/usr/src/sys -I/usr/src/sys/contrib/ck/include -fno-common -g -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fPIC -fdebug-prefix-map=./machine=/usr/src/sys/arm64/include -I/usr/obj/usr/src/arm64.aarch64/sys/ROCK64   -mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 -mstack-protector-guard-offset=0  -mgeneral-regs-only -ffixed-x18 -ffreestanding -fwrapv -fstack-protector -Wall -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wcast-qual -Wundef -Wno-pointer-sign -D__printf__=__freebsd_kprintf__ -Wmissing-include-dirs -fdiagnostics-show-option -Wno-unknown-pragmas -Wno-error=tautological-compare -Wno-error=empty-body -Wno-error=parentheses-equality -Wno-error=unused-function -Wno-error=pointer-sign -Wno-error=shift-negative-value -Wno-address-of-packed-member -Wno-error=unused-but-set-variable -Wno-format-zero-length     -std=iso9899:1999 -c /usr/src/sys/contrib/zlib/crc32.c -o crc32.o

and the only non-generic file is  /usr/obj/usr/src/arm64.aarch64/sys/ROCK64/opt_global.h, which contains:
#define AUDIT 1
#define COMPAT_FREEBSD11 1
#define COMPAT_FREEBSD13 1
#define KDTRACE_HOOKS 1
#define NEW_PCIB 1
#define INVARIANT_SUPPORT 1
#define SMP 1
#define RACCT 1
#define RCTL 1
#define INTRNG 1
#define LINUX_BOOT_ABI 1
#define COMPAT_FREEBSD32 1
#define PERTHREAD_SSP 1
#define VFP 1
#define ARM64 1
#define RACCT_DEFAULT_TO_DISABLED 1
#define WITNESS 1
#define INVARIANTS 1
#define DEBUG_VFS_LOCKS 1
#define MAC 1
#define COMPAT_FREEBSD12 1
#define CC_NEWRENO 1
#define KDB 1

Regarding the proposed patches:  My reading of the code is that Z_U4 should always be defined (as something like __uint32_t) and Z_U8 should be defined on 64-bit architectures (as something like __uint64_t).  As an experiment, I tried adding the following patch (without any of the suggested patches) and building GENERIC on arm64:
diff --git a/sys/contrib/zlib/crc32.c b/sys/contrib/zlib/crc32.c
index 451887bc7ce4..1fc12a212b8f 100644
--- a/sys/contrib/zlib/crc32.c
+++ b/sys/contrib/zlib/crc32.c
@@ -98,6 +98,16 @@
 #  endif
 #endif
 
+#ifndef Z_U8
+# warning Z_U8_ not defined
+#endif
+#ifndef Z_U4
+# warning Z_U4_ not defined
+#endif
+#ifndef W
+# warning W_ not defined
+#endif
+
 /* Local functions. */
 local z_crc_t multmodp OF((z_crc_t a, z_crc_t b));
 local z_crc_t x2nmodp OF((z_off64_t n, unsigned k));

Which gave the following output:
--- crc32.o ---
/tank/fbsd/src/main/sys/contrib/zlib/crc32.c:102:3: error: Z_U8_ not defined [-Werror,-W#warnings]
# warning Z_U8_ not defined
  ^
/tank/fbsd/src/main/sys/contrib/zlib/crc32.c:105:3: error: Z_U4_ not defined [-Werror,-W#warnings]
# warning Z_U4_ not defined
  ^
/tank/fbsd/src/main/sys/contrib/zlib/crc32.c:108:3: error: W_ not defined [-Werror,-W#warnings]
# warning W_ not defined
  ^

I've tried building GENERIC and got the same error as my kernel.

I've tried building using the patch from delphij@ with the same error and have attached my own proposal.

I agree that a default build on ref14-aarch64 succeeds but if you add "-mcpu=cortex-a53" (which adds an expection that the CRC32 instructions are present) then the build fails.  You can reproduce the problem on ref14-aarch64 with "MAKEOBJDIRPREFIX=/scratch/tmp/$USER/obj make -DKERNFAST CPUTYPE=cortex-a53 buildkernel".  My attached patch passes this test.
Comment 10 John F. Carr 2022-04-03 09:59:38 UTC
The patch "Another patch to define both Z_U4 and Z_U8" works on my system with CPUTYPE?=cortex-a57 in /etc/make.conf.
Comment 11 Xin LI freebsd_committer freebsd_triage 2022-04-03 19:03:18 UTC
Created attachment 232919 [details]
Proposed patch

Hi,

Thanks for the info and analysis!  These were very helpful.

It looks like zlib was depending on limits.h when deciding how to define Z_U4 and Z_U8 (the code assumed they were defined).  Since Z_U4 is defined in zconf.h, I think we should be defining them there.

I've tested this with both CPUTYPE=cortex-a53 and CPUTYPE=cortex-a57 and will perform some additional testing.  Could you please let me know if the result would work on actual hardware?

Thanks again for your help!
Comment 12 Peter Jeremy freebsd_committer freebsd_triage 2022-04-03 21:15:59 UTC
The latest proposed patch compiles successfully on my Rock64 (RK3328, Cortex-A53).  The resultant kernel runs successfully but I'm not using ZFS on that system.
Comment 13 commit-hook freebsd_committer freebsd_triage 2022-04-03 21:41:41 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0a21252adf11f7e839eabeb530e75cd1f9cd5386

commit 0a21252adf11f7e839eabeb530e75cd1f9cd5386
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-04-03 18:45:38 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2022-04-03 21:38:31 +0000

    sys/contrib/zlib: Always define Z_U8 and Z_U4

    This is a temporary hack for zlib to make sure that the library
    still builds when building with Z_SOLO (used in kernel and loader),
    as zlib is depending on limits.h which is only available in STDC
    case.

    PR:             kern/262977
    MFC after:      3 days

 sys/contrib/zlib/zconf.h | 5 +++++
 1 file changed, 5 insertions(+)
Comment 14 commit-hook freebsd_committer freebsd_triage 2022-04-04 15:58:38 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9587a10b420f9ef243f578b6938662ff6b558e86

commit 9587a10b420f9ef243f578b6938662ff6b558e86
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-04-03 18:45:38 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-04-04 15:58:10 +0000

    sys/contrib/zlib: Always define Z_U8 and Z_U4

    This is a temporary hack for zlib to make sure that the library
    still builds when building with Z_SOLO (used in kernel and loader),
    as zlib is depending on limits.h which is only available in STDC
    case.

    PR:             kern/262977
    MFC after:      3 days

    (cherry picked from commit 0a21252adf11f7e839eabeb530e75cd1f9cd5386)

    Approved by:    re (gjb, early MFC)

 sys/contrib/zlib/zconf.h | 5 +++++
 1 file changed, 5 insertions(+)
Comment 15 commit-hook freebsd_committer freebsd_triage 2022-04-04 17:24:52 UTC
A commit in branch releng/13.1 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=c022121396da13d667c00ac588947647eac5f658

commit c022121396da13d667c00ac588947647eac5f658
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-04-03 18:45:38 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-04-04 16:54:20 +0000

    sys/contrib/zlib: Always define Z_U8 and Z_U4

    This is a temporary hack for zlib to make sure that the library
    still builds when building with Z_SOLO (used in kernel and loader),
    as zlib is depending on limits.h which is only available in STDC
    case.

    PR:             kern/262977
    MFC after:      3 days

    (cherry picked from commit 0a21252adf11f7e839eabeb530e75cd1f9cd5386)

    Approved by:    re (gjb, early MFC)

    (cherry picked from commit 9587a10b420f9ef243f578b6938662ff6b558e86)

    Approved by:    re (gjb)

 sys/contrib/zlib/zconf.h | 5 +++++
 1 file changed, 5 insertions(+)
Comment 16 commit-hook freebsd_committer freebsd_triage 2022-04-05 19:44:23 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ca211d5f64999e5efe6ac3459d912aa6f8d0546e

commit ca211d5f64999e5efe6ac3459d912aa6f8d0546e
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-04-03 18:45:38 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-04-05 19:41:58 +0000

    sys/contrib/zlib: Always define Z_U8 and Z_U4

    This is a temporary hack for zlib to make sure that the library
    still builds when building with Z_SOLO (used in kernel and loader),
    as zlib is depending on limits.h which is only available in STDC
    case.

    PR:             kern/262977
    MFC after:      3 days

    (cherry picked from commit 0a21252adf11f7e839eabeb530e75cd1f9cd5386)

 sys/contrib/zlib/zconf.h | 5 +++++
 1 file changed, 5 insertions(+)