Blame SOURCES/zlib-1.2.11-IBM-DFLTCC-compression-level-switching-issues.patch

08b07c
Subject: [PATCH] Fixed DFLTCC compression level switching issues
08b07c
08b07c
---
08b07c
 configure                     |  4 +--
08b07c
 contrib/s390/dfltcc.c         | 52 ++++++++++++++++++++++++++++++-----
08b07c
 contrib/s390/dfltcc_deflate.h |  2 ++
08b07c
 deflate.c                     | 12 ++++----
08b07c
 test/infcover.c               |  2 +-
08b07c
 5 files changed, 57 insertions(+), 15 deletions(-)
08b07c
08b07c
diff --git a/configure b/configure
08b07c
index bfe4386..70ed86b 100755
08b07c
--- a/configure
08b07c
+++ b/configure
08b07c
@@ -139,7 +139,7 @@ case "$1" in
08b07c
     -w* | --warn) warn=1; shift ;;
08b07c
     -d* | --debug) debug=1; shift ;;
08b07c
     --dfltcc)
08b07c
-	    CFLAGS="$CFLAGS -DDFLTCC"
08b07c
+	    CFLAGS="$CFLAGS -DDFLTCC -DDFLTCC_LEVEL_MASK=0x7e"
08b07c
       OBJC="$OBJC dfltcc.o"
08b07c
 	    PIC_OBJC="$PIC_OBJC dfltcc.lo"
08b07c
       shift
08b07c
@@ -838,7 +838,7 @@ cat > $test.c << EOF
08b07c
 #include <sys/sdt.h>
08b07c
 int main() { return 0; }
08b07c
 EOF
08b07c
-if try ${CC} ${CFLAGS} $test.c; then
08b07c
+ if try $CC -c $CFLAGS $test.c; then
08b07c
     echo "Checking for sys/sdt.h ... Yes." | tee -a configure.log
08b07c
     CFLAGS="$CFLAGS -DHAVE_SYS_SDT_H"
08b07c
     SFLAGS="$SFLAGS -DHAVE_SYS_SDT_H"
08b07c
diff --git a/contrib/s390/dfltcc.c b/contrib/s390/dfltcc.c
08b07c
index d88a0d6..94a196f 100644
08b07c
--- a/contrib/s390/dfltcc.c
08b07c
+++ b/contrib/s390/dfltcc.c
08b07c
@@ -350,8 +350,12 @@ int ZLIB_INTERNAL dfltcc_deflate(strm, flush, result)
08b07c
     int soft_bcc;
08b07c
     int no_flush;
08b07c
 
08b07c
-    if (!dfltcc_can_deflate(strm))
08b07c
+    if (!dfltcc_can_deflate(strm)) {
08b07c
+        /* Clear history. */
08b07c
+        if (flush == Z_FULL_FLUSH)
08b07c
+            param->hl = 0;
08b07c
         return 0;
08b07c
+    }
08b07c
 
08b07c
 again:
08b07c
     masked_avail_in = 0;
08b07c
@@ -376,7 +380,8 @@ again:
08b07c
         /* Clear history. */
08b07c
         if (flush == Z_FULL_FLUSH)
08b07c
             param->hl = 0; 
08b07c
-        *result = need_more;
08b07c
+        /* Trigger block post-processing if necessary. */
08b07c
+        *result = no_flush ? need_more : block_done;
08b07c
         return 1;
08b07c
     }
08b07c
 
08b07c
@@ -403,13 +408,18 @@ again:
08b07c
             param->bcf = 0;
08b07c
             dfltcc_state->block_threshold =
08b07c
                 strm->total_in + dfltcc_state->block_size;
08b07c
-            if (strm->avail_out == 0) {
08b07c
-                *result = need_more;
08b07c
-                return 1;
08b07c
-            }
08b07c
         }
08b07c
     }
08b07c
 
08b07c
+    /* No space for compressed data. If we proceed, dfltcc_cmpr() will return
08b07c
+     * DFLTCC_CC_OP1_TOO_SHORT without buffering header bits, but we will still
08b07c
+     * set BCF=1, which is wrong. Avoid complications and return early.
08b07c
+     */
08b07c
+    if (strm->avail_out == 0) {
08b07c
+        *result = need_more;
08b07c
+        return 1;
08b07c
+    }
08b07c
+
08b07c
     /* The caller gave us too much data. Pass only one block worth of
08b07c
      * uncompressed data to DFLTCC and mask the rest, so that on the next
08b07c
      * iteration we start a new block.
08b07c
@@ -737,10 +747,15 @@ __attribute__((constructor)) local void init_globals(void)
08b07c
      * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
08b07c
      * is 64-bit, it's always z/Architecture mode at runtime.
08b07c
      */
08b07c
-    __asm__ volatile(".machinemode push\n"
08b07c
+     __asm__ volatile(
08b07c
+#ifndef __clang__
08b07c
+                     ".machinemode push\n"
08b07c
                      ".machinemode zarch\n"
08b07c
+#endif
08b07c
                      "stfle %[facilities]\n"
08b07c
+#ifndef __clang__
08b07c
                      ".machinemode pop\n"
08b07c
+#endif
08b07c
                      : [facilities] "=Q" (cpu_facilities)
08b07c
                      , [r0] "+r" (r0)
08b07c
                      :
08b07c
@@ -872,6 +887,28 @@ int ZLIB_INTERNAL dfltcc_deflate_params(strm, level, strategy, flush)
08b07c
     return Z_OK; 
08b07c
 }
08b07c
 
08b07c
+int ZLIB_INTERNAL dfltcc_deflate_done(strm, flush)
08b07c
+    z_streamp strm;
08b07c
+    int flush;
08b07c
+{
08b07c
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
08b07c
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
08b07c
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
08b07c
+
08b07c
+    /* When deflate(Z_FULL_FLUSH) is called with small avail_out, it might
08b07c
+     * close the block without resetting the compression state. Detect this
08b07c
+     * situation and return that deflation is not done.
08b07c
+     */
08b07c
+    if (flush == Z_FULL_FLUSH && strm->avail_out == 0)
08b07c
+        return 0;
08b07c
+
08b07c
+    /* Return that deflation is not done if DFLTCC is used and either it
08b07c
+     * buffered some data (Continuation Flag is set), or has not written EOBS
08b07c
+     * yet (Block-Continuation Flag is set).
08b07c
+     */
08b07c
+    return !dfltcc_can_deflate(strm) || (!param->cf && !param->bcf);
08b07c
+}
08b07c
+
08b07c
 /*
08b07c
    Preloading history.
08b07c
 */
08b07c
@@ -925,6 +962,7 @@ int ZLIB_INTERNAL dfltcc_deflate_set_dictionary(strm, dictionary, dict_length)
08b07c
 
08b07c
     append_history(param, state->window, dictionary, dict_length);
08b07c
     state->strstart = 1; /* Add FDICT to zlib header */
08b07c
+    state->block_start = state->strstart; /* Make deflate_stored happy */
08b07c
     return Z_OK;
08b07c
 }
08b07c
 
08b07c
diff --git a/contrib/s390/dfltcc_deflate.h b/contrib/s390/dfltcc_deflate.h
08b07c
index de36784..914daa4 100644
08b07c
--- a/contrib/s390/dfltcc_deflate.h
08b07c
+++ b/contrib/s390/dfltcc_deflate.h
08b07c
@@ -11,6 +11,7 @@ int ZLIB_INTERNAL dfltcc_deflate_params OF((z_streamp strm,
08b07c
                                             int level,
08b07c
                                             int strategy,
08b07c
                                             int *flush)); 
08b07c
+int ZLIB_INTERNAL dfltcc_deflate_done OF((z_streamp strm, int flush));
08b07c
 int ZLIB_INTERNAL dfltcc_deflate_set_dictionary OF((z_streamp strm,
08b07c
                                                     const Bytef *dictionary,
08b07c
                                                     uInt dict_length));
08b07c
@@ -41,6 +42,7 @@ int ZLIB_INTERNAL dfltcc_deflate_get_dictionary OF((z_streamp strm,
08b07c
         if (err == Z_STREAM_ERROR) \
08b07c
             return err; \
08b07c
     } while (0)
08b07c
+#define DEFLATE_DONE dfltcc_deflate_done
08b07c
 #define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, source_len) \
08b07c
     do { \
08b07c
         if (dfltcc_can_deflate((strm))) \
08b07c
diff --git a/deflate.c b/deflate.c
08b07c
index d907a1b..085abbe 100644
08b07c
--- a/deflate.c
08b07c
+++ b/deflate.c
08b07c
@@ -75,6 +75,7 @@ const char deflate_copyright[] =
08b07c
 #define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
08b07c
 #define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
08b07c
 #define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) do {} while (0)
08b07c
+#define DEFLATE_DONE(strm, flush) 1
08b07c
 #define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
08b07c
 #define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
08b07c
 #define DEFLATE_HOOK(strm, flush, bstate) 0
08b07c
@@ -605,14 +606,15 @@ int ZEXPORT deflateParams(strm, level, strategy)
08b07c
     DEFLATE_PARAMS_HOOK(strm, level, strategy, &hook_flush);
08b07c
     func = configuration_table[s->level].func;
08b07c
 
08b07c
-    if ((strategy != s->strategy || func != configuration_table[level].func ||
08b07c
-        hook_flush != Z_NO_FLUSH) && s->last_flush != -2) {
08b07c
+    if (((strategy != s->strategy || func != configuration_table[level].func) &&
08b07c
+        s->last_flush != -2) || hook_flush != Z_NO_FLUSH) {
08b07c
         /* Flush the last buffer: */
08b07c
-        int err = deflate(strm, RANK(hook_flush) > RANK(Z_BLOCK) ?
08b07c
-                          hook_flush : Z_BLOCK); 
08b07c
+        int flush = RANK(hook_flush) > RANK(Z_BLOCK) ? hook_flush : Z_BLOCK;
08b07c
+        int err = deflate(strm, flush);
08b07c
         if (err == Z_STREAM_ERROR)
08b07c
             return err;
08b07c
-        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
08b07c
+        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead ||
08b07c
+            !DEFLATE_DONE(strm, flush))
08b07c
             return Z_BUF_ERROR;
08b07c
     }
08b07c
     if (s->level != level) {
08b07c
diff --git a/test/infcover.c b/test/infcover.c
08b07c
index a34cd17..a208219 100644
08b07c
--- a/test/infcover.c
08b07c
+++ b/test/infcover.c
08b07c
@@ -373,7 +373,7 @@ local void cover_support(void)
08b07c
     mem_setup(&strm;;
08b07c
     strm.avail_in = 0;
08b07c
     strm.next_in = Z_NULL;
08b07c
-    ret = inflateInit_(&strm, ZLIB_VERSION - 1, (int)sizeof(z_stream));
08b07c
+    ret = inflateInit_(&strm, &ZLIB_VERSION[1], (int)sizeof(z_stream));
08b07c
                                                 assert(ret == Z_VERSION_ERROR);
08b07c
     mem_done(&strm, "wrong version");
08b07c
 
08b07c
-- 
08b07c
2.26.0
08b07c