The proposed update to lang/node makes this irrelevant, but I thought I'd send
it anyway since it may come up elsewhere too.

I noticed that on one system, 'npm install less' would abort, logging
'node: backwards memcpy', but on another it worked fine.  Eventually I
figured out this was because the working one had packages built with llvm 11,
the other with llvm 13 packages, and llvm 13's memcpy optimiser was turning
a series of small memcpys in node's bundled zlib into one larger one, without
identifying that the src and dest of the larger memcpy could overlap.

Compiling the bundled zlib with -fno-builtin-memcpy prevents it from doing
that, which fixes npm.

This should only affect the zlib bundled in node and chromium (and possibly
elsewhere?).  The version in node and chromium has a supposedly optimised
inner loop using memcpy, while other versions use simpler byte copy loops,
which get turned into inline unrolled loops rather than memcpy calls.

The proposed update to node 16.13.2 also switches to using the system zlib,
so we probably don't need the diff below.


Index: Makefile
===================================================================
RCS file: /cvs/ports/lang/node/Makefile,v
retrieving revision 1.93
diff -u -p -u -p -r1.93 Makefile
--- Makefile    2 Nov 2021 00:01:17 -0000       1.93
+++ Makefile    24 Jan 2022 10:17:02 -0000
@@ -19,7 +19,7 @@ DISTFILES =           node-pledge-{}${PLEDGE_VER}
 
 DISTNAME =             node-${NODE_VERSION}
 PKGNAME =              ${DISTNAME:S/v//g}
-REVISION =             0
+REVISION =             1
 
 MASTER_SITES0 =                https://github.com/qbit/node-pledge/archive/
 
Index: patches/patch-deps_zlib_zlib_gyp
===================================================================
RCS file: /cvs/ports/lang/node/patches/patch-deps_zlib_zlib_gyp,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 patch-deps_zlib_zlib_gyp
--- patches/patch-deps_zlib_zlib_gyp    6 Sep 2021 13:49:44 -0000       1.2
+++ patches/patch-deps_zlib_zlib_gyp    24 Jan 2022 10:17:02 -0000
@@ -4,19 +4,24 @@ node has a patch to add SIMD etc optimiz
 disable on i386, it breaks build. it uses cpuid to detect features so
 _should_ work on amd64.
 
+clang 13 does not safely optimise the memcpy calls in the optimized
+zlib code, resulting in backwards memcpys and node processes aborting,
+so disable memcpy optimization for now.
+
 Index: deps/zlib/zlib.gyp
 --- deps/zlib/zlib.gyp.orig
 +++ deps/zlib/zlib.gyp
-@@ -64,7 +64,7 @@
+@@ -64,7 +64,8 @@
                  'USE_FILE32API'
                ],
              }],
 -            ['(target_arch in "ia32 x64 x32" and OS!="ios") or 
arm_fpu=="neon"', {
 +            ['(target_arch in "x64" and OS!="ios") or arm_fpu=="neon"', {
++              'cflags': [ '-fno-builtin-memcpy' ],
                'sources': [
                  'adler32_simd.c',
                  'adler32_simd.h',
-@@ -77,7 +77,7 @@
+@@ -77,7 +78,7 @@
                'sources': [ 'inflate.c', ],
              }],
              # Incorporate optimizations where possible

Reply via email to