nginx: build with zlib-ng in compatibility mode (#399565)

This commit is contained in:
Pol Dellaiera
2025-05-06 15:32:30 +02:00
committed by GitHub
3 changed files with 3 additions and 283 deletions
-12
View File
@@ -220,18 +220,6 @@ stdenv.mkDerivation {
./nix-etag-1.15.4.patch
./nix-skip-check-logs-path.patch
]
++
lib.optionals
(lib.elem pname [
"nginx"
"nginxQuic"
"tengine"
])
[
# https://github.com/NixOS/nixpkgs/issues/357522
# https://github.com/zlib-ng/patches/blob/5a036c0a00120c75ee573b27f4f44ade80d82ff2/nginx/README.md
./nginx-zlib-ng.patch
]
++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
(fetchpatch {
url = "https://raw.githubusercontent.com/openwrt/packages/c057dfb09c7027287c7862afab965a4cd95293a3/net/nginx/patches/102-sizeof_test_fix.patch";
-271
View File
@@ -1,271 +0,0 @@
--- a/auto/lib/zlib/conf
+++ b/auto/lib/zlib/conf
@@ -33,8 +33,8 @@ if [ $ZLIB != NONE ]; then
*)
have=NGX_ZLIB . auto/have
- LINK_DEPS="$LINK_DEPS $ZLIB/libz.a"
- CORE_LIBS="$CORE_LIBS $ZLIB/libz.a"
+ LINK_DEPS="$LINK_DEPS $ZLIB/libz-ng.a"
+ CORE_LIBS="$CORE_LIBS $ZLIB/libz-ng.a"
#CORE_LIBS="$CORE_LIBS -L $ZLIB -lz"
;;
@@ -50,10 +50,10 @@ else
ngx_feature="zlib library"
ngx_feature_name="NGX_ZLIB"
ngx_feature_run=no
- ngx_feature_incs="#include <zlib.h>"
+ ngx_feature_incs="#include <zlib-ng.h>"
ngx_feature_path=
- ngx_feature_libs="-lz"
- ngx_feature_test="z_stream z; deflate(&z, Z_NO_FLUSH)"
+ ngx_feature_libs="-lz-ng"
+ ngx_feature_test="zng_stream z; zng_deflate(&z, Z_NO_FLUSH)"
. auto/feature
diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h
index 1861be6..bd427b8 100644
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -141,5 +141,9 @@ typedef intptr_t ngx_flag_t;
#endif
+/* Force enable ZLIB-NG */
+#ifndef NGX_ZLIB_NG
+#define NGX_ZLIB_NG 1
+#endif
#endif /* _NGX_CONFIG_H_INCLUDED_ */
diff --git a/src/http/modules/ngx_http_gunzip_filter_module.c b/src/http/modules/ngx_http_gunzip_filter_module.c
index 5d170a1..d8dcc96 100644
--- a/src/http/modules/ngx_http_gunzip_filter_module.c
+++ b/src/http/modules/ngx_http_gunzip_filter_module.c
@@ -10,7 +10,14 @@
#include <ngx_core.h>
#include <ngx_http.h>
-#include <zlib.h>
+#if defined(NGX_ZLIB_NG)
+# include <zlib-ng.h>
+# define ZPREFIX(x) zng_ ## x
+# define z_stream zng_stream
+#elif defined(NGX_ZLIB)
+# include <zlib.h>
+# define ZPREFIX(x) x
+#endif
typedef struct {
@@ -312,7 +319,7 @@ ngx_http_gunzip_filter_inflate_start(ngx_http_request_t *r,
ctx->zstream.opaque = ctx;
/* windowBits +16 to decode gzip, zlib 1.2.0.4+ */
- rc = inflateInit2(&ctx->zstream, MAX_WBITS + 16);
+ rc = ZPREFIX(inflateInit2)(&ctx->zstream, MAX_WBITS + 16);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
@@ -435,7 +442,7 @@ ngx_http_gunzip_filter_inflate(ngx_http_request_t *r,
ctx->zstream.avail_in, ctx->zstream.avail_out,
ctx->flush, ctx->redo);
- rc = inflate(&ctx->zstream, ctx->flush);
+ rc = ZPREFIX(inflate)(&ctx->zstream, ctx->flush);
if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -533,7 +540,7 @@ ngx_http_gunzip_filter_inflate(ngx_http_request_t *r,
if (rc == Z_STREAM_END && ctx->zstream.avail_in > 0) {
- rc = inflateReset(&ctx->zstream);
+ rc = ZPREFIX(inflateReset)(&ctx->zstream);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
@@ -584,7 +591,7 @@ ngx_http_gunzip_filter_inflate_end(ngx_http_request_t *r,
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"gunzip inflate end");
- rc = inflateEnd(&ctx->zstream);
+ rc = ZPREFIX(inflateEnd)(&ctx->zstream);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
diff --git a/src/http/modules/ngx_http_gzip_filter_module.c b/src/http/modules/ngx_http_gzip_filter_module.c
index 7113df6..d6c2ea1 100644
--- a/src/http/modules/ngx_http_gzip_filter_module.c
+++ b/src/http/modules/ngx_http_gzip_filter_module.c
@@ -9,7 +9,14 @@
#include <ngx_core.h>
#include <ngx_http.h>
-#include <zlib.h>
+#if defined(NGX_ZLIB_NG)
+# include <zlib-ng.h>
+# define ZPREFIX(x) zng_ ## x
+# define z_stream zng_stream
+#elif defined(NGX_ZLIB)
+# include <zlib.h>
+# define ZPREFIX(x) x
+#endif
typedef struct {
@@ -454,7 +461,7 @@ failed:
ctx->done = 1;
if (ctx->preallocated) {
- deflateEnd(&ctx->zstream);
+ ZPREFIX(deflateEnd)(&ctx->zstream);
ngx_pfree(r->pool, ctx->preallocated);
}
@@ -527,10 +534,20 @@ ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
wbits = ngx_max(wbits, 13);
}
- ctx->allocated = 8192 + 16 + (1 << (wbits + 2))
- + 131072 + (5 << (memlevel + 6))
- + 4 * (64 + sizeof(void*));
ctx->zlib_ng = 1;
+ ctx->allocated = 6144 // State
+ + 65536 // Window
+ + 65536 // Prev
+ + 131072 // Head
+ + 163840 // Pending
+ + 56 + 8 // Alloc struct + padding
+#if (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
+ + 4096 // Required to fix allocation alignment
+#else
+ + 64 // Required to fix allocation alignment
+#endif
+ + 256; // Extra to allow for future changes
+
}
}
@@ -623,7 +640,7 @@ ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
ctx->zstream.zfree = ngx_http_gzip_filter_free;
ctx->zstream.opaque = ctx;
- rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
+ rc = ZPREFIX(deflateInit2)(&ctx->zstream, (int) conf->level, Z_DEFLATED,
ctx->wbits + 16, ctx->memlevel, Z_DEFAULT_STRATEGY);
if (rc != Z_OK) {
@@ -758,7 +775,7 @@ ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
ctx->zstream.avail_in, ctx->zstream.avail_out,
ctx->flush, ctx->redo);
- rc = deflate(&ctx->zstream, ctx->flush);
+ rc = ZPREFIX(deflate)(&ctx->zstream, ctx->flush);
if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
@@ -882,7 +899,7 @@ ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
ctx->zin = ctx->zstream.total_in;
ctx->zout = ctx->zstream.total_out;
- rc = deflateEnd(&ctx->zstream);
+ rc = ZPREFIX(deflateEnd)(&ctx->zstream);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c
index f7c4bd2..ad71d4d 100644
--- a/src/http/modules/ngx_http_log_module.c
+++ b/src/http/modules/ngx_http_log_module.c
@@ -9,8 +9,13 @@
#include <ngx_core.h>
#include <ngx_http.h>
-#if (NGX_ZLIB)
-#include <zlib.h>
+#if defined(NGX_ZLIB_NG)
+# include <zlib-ng.h>
+# define ZPREFIX(x) zng_ ## x
+# define z_stream zng_stream
+#elif defined(NGX_ZLIB)
+# include <zlib.h>
+# define ZPREFIX(x) x
#endif
@@ -634,7 +639,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
zstream.next_out = out;
zstream.avail_out = size;
- rc = deflateInit2(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel,
+ rc = ZPREFIX(deflateInit2)(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel,
Z_DEFAULT_STRATEGY);
if (rc != Z_OK) {
@@ -647,7 +652,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
zstream.next_in, zstream.next_out,
zstream.avail_in, zstream.avail_out);
- rc = deflate(&zstream, Z_FINISH);
+ rc = ZPREFIX(deflate)(&zstream, Z_FINISH);
if (rc != Z_STREAM_END) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
@@ -663,7 +668,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
size -= zstream.avail_out;
- rc = deflateEnd(&zstream);
+ rc = ZPREFIX(deflateEnd)(&zstream);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, log, 0, "deflateEnd() failed: %d", rc);
diff --git a/src/stream/ngx_stream_log_module.c b/src/stream/ngx_stream_log_module.c
index 0ff7f42..0b9d12c 100644
--- a/src/stream/ngx_stream_log_module.c
+++ b/src/stream/ngx_stream_log_module.c
@@ -9,8 +9,13 @@
#include <ngx_core.h>
#include <ngx_stream.h>
-#if (NGX_ZLIB)
-#include <zlib.h>
+#if defined(NGX_ZLIB_NG)
+# include <zlib-ng.h>
+# define ZPREFIX(x) zng_ ## x
+# define z_stream zng_stream
+#elif defined(NGX_ZLIB)
+# include <zlib.h>
+# define ZPREFIX(x) x
#endif
@@ -525,7 +530,7 @@ ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
zstream.next_out = out;
zstream.avail_out = size;
- rc = deflateInit2(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel,
+ rc = ZPREFIX(deflateInit2)(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel,
Z_DEFAULT_STRATEGY);
if (rc != Z_OK) {
@@ -538,7 +543,7 @@ ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
zstream.next_in, zstream.next_out,
zstream.avail_in, zstream.avail_out);
- rc = deflate(&zstream, Z_FINISH);
+ rc = ZPREFIX(deflate)(&zstream, Z_FINISH);
if (rc != Z_STREAM_END) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
@@ -554,7 +559,7 @@ ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
size -= zstream.avail_out;
- rc = deflateEnd(&zstream);
+ rc = ZPREFIX(deflateEnd)(&zstream);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, log, 0, "deflateEnd() failed: %d", rc);
+3
View File
@@ -10568,6 +10568,7 @@ with pkgs;
nginx = nginxStable;
nginxQuic = callPackage ../servers/http/nginx/quic.nix {
zlib-ng = zlib-ng.override { withZlibCompat = true; };
withPerl = false;
# We don't use `with` statement here on purpose!
# See https://github.com/NixOS/nixpkgs/pull/10474#discussion_r42369334
@@ -10581,6 +10582,7 @@ with pkgs;
};
nginxStable = callPackage ../servers/http/nginx/stable.nix {
zlib-ng = zlib-ng.override { withZlibCompat = true; };
withPerl = false;
# We don't use `with` statement here on purpose!
# See https://github.com/NixOS/nixpkgs/pull/10474#discussion_r42369334
@@ -10592,6 +10594,7 @@ with pkgs;
};
nginxMainline = callPackage ../servers/http/nginx/mainline.nix {
zlib-ng = zlib-ng.override { withZlibCompat = true; };
withKTLS = true;
withPerl = false;
# We don't use `with` statement here on purpose!