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

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