Skip to contentSkip to conversation

erofs-utils: lib: reset hc to avoid 32-bit overflow of kite-deflate

PATCH In review Gao Xiang 2 participants 5 messages Jan 24, 2024 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: reset hc to avoid 32-bit overflow of kite-deflate

Yifan reported a "segmentation fault (core dumped)" error days ago
with a large dataset (enwik9 x 5). Let's fix it.

Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")
---
 include/erofs/defs.h |  3 +++
 lib/kite_deflate.c   | 12 ++++++++++++
 2 files changed, 15 insertions(+)
include/erofs/defs.h +3 -0
index e7384a1..4ea9a55 100644
@@ -343,6 +343,9 @@ unsigned long __roundup_pow_of_two(unsigned long n)
 #define ST_MTIM_NSEC(stbuf) 0
 #endif
 
+#define likely(x)      __builtin_expect(!!(x), 1)
+#define unlikely(x)    __builtin_expect(!!(x), 0)
+
 #ifdef __cplusplus
 }
 #endif
lib/kite_deflate.c +12 -0
index 8667954..cb81626 100644
@@ -859,6 +859,18 @@ static void kite_mf_reset(struct kite_matchfinder *mf,
 	 */
 	mf->base += mf->offset + kHistorySize32 + 1;
 
+	/*
+	 * Unlike other LZ encoders like liblzma [1], we simply reset the hash
+	 * chain instead of normalization.  This avoids extra complexity, as we
+	 * don't consider extreme large input buffers in one go.
+	 *
+	 * [1] https://github.com/tukaani-project/xz/blob/v5.4.0/src/liblzma/lz/lz_encoder_mf.c#L94
+	 */
+	if (unlikely(mf->base > ((typeof(mf->base))-1) >> 1)) {
+		mf->base = kHistorySize32 + 1;
+		memset(mf->hash, 0, 0x10000 * sizeof(mf->hash[0]));
+		memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);
+	}
 	mf->offset = 0;
 	mf->cyclic_pos = 0;
 
Signature · 1 line
2.39.3
Tested-by on v1 1/1
#1 lore raw
Quoted 38 lines · Gao Xiang

Yifan reported a "segmentation fault (core dumped)" error days ago
with a large dataset (enwik9 x 5). Let's fix it.

Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")
---
  include/erofs/defs.h |  3 +++
  lib/kite_deflate.c   | 12 ++++++++++++
  2 files changed, 15 insertions(+)
include/erofs/defs.h +3 -0
index e7384a1..4ea9a55 100644
@@ -343,6 +343,9 @@ unsigned long __roundup_pow_of_two(unsigned long n)
  #define ST_MTIM_NSEC(stbuf) 0
  #endif
 
+#define likely(x)      __builtin_expect(!!(x), 1)
+#define unlikely(x)    __builtin_expect(!!(x), 0)
+
  #ifdef __cplusplus
  }
  #endif
lib/kite_deflate.c +5 -0
index 8667954..cb81626 100644
@@ -859,6 +859,18 @@ static void kite_mf_reset(struct kite_matchfinder *mf,
  	 */
  	mf->base += mf->offset + kHistorySize32 + 1;
 
+	/*
+	 * Unlike other LZ encoders like liblzma [1], we simply reset the hash
+	 * chain instead of normalization.  This avoids extra complexity, as we
+	 * don't consider extreme large input buffers in one go.
+	 *

lib/kite_deflate.c static void kite_mf_reset(struct kite_matchfinder *mf, · line 876

Quoted 10 lines
lib/kite_deflate.c +7 -0
+	 * [1] https://github.com/tukaani-project/xz/blob/v5.4.0/src/liblzma/lz/lz_encoder_mf.c#L94
+	 */
+	if (unlikely(mf->base > ((typeof(mf->base))-1) >> 1)) {
+		mf->base = kHistorySize32 + 1;
+		memset(mf->hash, 0, 0x10000 * sizeof(mf->hash[0]));
+		memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);
+	}
  	mf->offset = 0;
  	mf->cyclic_pos = 0;
 

Yifan Zhao

Thanks

author
#2 lore raw

Hi Yifan,

Quoted 36 lines · Yifan Zhao
Quoted 34 lines · Gao Xiang

Yifan reported a "segmentation fault (core dumped)" error days ago with a large dataset (enwik9 x 5).   Let's fix it.

Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")

---
  include/erofs/defs.h |  3 +++
  lib/kite_deflate.c   | 12 ++++++++++++

  2 files changed, 15 insertions(+)
include/erofs/defs.h +3 -0
index e7384a1..4ea9a55 100644
@@ -343,6 +343,9 @@ unsigned long __roundup_pow_of_two(unsigned long n)
   #define ST_MTIM_NSEC(stbuf) 0
   #endif
+#define likely(x)      __builtin_expect(!!(x), 1)
+#define unlikely(x)    __builtin_expect(!!(x), 0)
+
   #ifdef __cplusplus
   }
   #endif
lib/kite_deflate.c +3 -0
index 8667954..cb81626 100644
@@ -859,6 +859,18 @@ static void kite_mf_reset(struct kite_matchfinder *mf,
        */
       mf->base += mf->offset + kHistorySize32 + 1;
+    /*
+     * Unlike other LZ encoders like liblzma [1], we simply reset the hash
+     * chain instead of normalization.  This avoids extra complexity, as we
Quoted 8 lines

lib/kite_deflate.c static void kite_mf_reset(struct kite_matchfinder *mf, · line 872

Quoted 8 lines
lib/kite_deflate.c +8 -0
+     * don't consider extreme large input buffers in one go.
+     *
+     * [1] https://github.com/tukaani-project/xz/blob/v5.4.0/src/liblzma/lz/lz_encoder_mf.c#L94
+     */
+    if (unlikely(mf->base > ((typeof(mf->base))-1) >> 1)) {
+        mf->base = kHistorySize32 + 1;
+        memset(mf->hash, 0, 0x10000 * sizeof(mf->hash[0]));
+        memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);

This line

memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);

is also unnecessary, let me remove it when applying.

I think it still works on your side.

Quoted 5 lines

lib/kite_deflate.c static void kite_mf_reset(struct kite_matchfinder *mf, · line 875

Quoted 3 lines
lib/kite_deflate.c +1 -0
+    }
       mf->offset = 0;
       mf->cyclic_pos = 0;

Thanks!

Thanks,
Gao Xiang

Quoted 5 lines

Yifan Zhao

Thanks

Tested-by
#3 lore raw
Quoted 65 lines · Gao Xiang

Hi Yifan,

Quoted 40 lines · Yifan Zhao
Quoted 38 lines · Gao Xiang

Yifan reported a "segmentation fault (core dumped)" error days ago with a large dataset (enwik9 x 5).   Let's fix it.

Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")

---
  include/erofs/defs.h |  3 +++
  lib/kite_deflate.c   | 12 ++++++++++++

  2 files changed, 15 insertions(+)
include/erofs/defs.h +3 -0
index e7384a1..4ea9a55 100644
@@ -343,6 +343,9 @@ unsigned long __roundup_pow_of_two(unsigned long n)
   #define ST_MTIM_NSEC(stbuf) 0
   #endif
+#define likely(x)      __builtin_expect(!!(x), 1)
+#define unlikely(x)    __builtin_expect(!!(x), 0)
+
   #ifdef __cplusplus
   }
   #endif
lib/kite_deflate.c +2 -0
index 8667954..cb81626 100644
@@ -859,6 +859,18 @@ static void kite_mf_reset(struct 
 kite_matchfinder *mf,
        */
       mf->base += mf->offset + kHistorySize32 + 1;
+    /*
+     * Unlike other LZ encoders like liblzma [1], we simply reset 

the hash
+     * chain instead of normalization.  This avoids extra complexity, as we
+     * don't consider extreme large input buffers in one go.

Quoted 8 lines
Quoted 8 lines

+     *
+     * [1]
https://github.com/tukaani-project/xz/blob/v5.4.0/src/liblzma/lz/lz_encoder_mf.c#L94
+     */
+    if (unlikely(mf->base > ((typeof(mf->base))-1) >> 1)) { +        mf->base = kHistorySize32 + 1;
+        memset(mf->hash, 0, 0x10000 * sizeof(mf->hash[0])); +        memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);

This line

memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);

is also unnecessary, let me remove it when applying.

I think it still works on your side.

Quoted 4 lines
Quoted 3 lines

+    }
      mf->offset = 0;
      mf->cyclic_pos = 0;

Quoted 12 lines
Quoted 1 line

Thanks!

Thanks,
Gao Xiang

Quoted 5 lines

Yifan Zhao

Thanks

Yes, it still works for me.

Thanks,

Yifan Zhao

author Applied
#4 lore raw
Quoted 72 lines · Yifan Zhao
Quoted 70 lines · Gao Xiang

Hi Yifan,

Quoted 36 lines · Yifan Zhao
Quoted 34 lines · Gao Xiang

Yifan reported a "segmentation fault (core dumped)" error days ago with a large dataset (enwik9 x 5).   Let's fix it.

Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")

---
  include/erofs/defs.h |  3 +++
  lib/kite_deflate.c   | 12 ++++++++++++

  2 files changed, 15 insertions(+)
include/erofs/defs.h +3 -0
index e7384a1..4ea9a55 100644
@@ -343,6 +343,9 @@ unsigned long __roundup_pow_of_two(unsigned long n)
   #define ST_MTIM_NSEC(stbuf) 0
   #endif
+#define likely(x)      __builtin_expect(!!(x), 1)
+#define unlikely(x)    __builtin_expect(!!(x), 0)
+
   #ifdef __cplusplus
   }
   #endif
lib/kite_deflate.c +3 -0
index 8667954..cb81626 100644
@@ -859,6 +859,18 @@ static void kite_mf_reset(struct kite_matchfinder *mf,
        */
       mf->base += mf->offset + kHistorySize32 + 1;
+    /*
+     * Unlike other LZ encoders like liblzma [1], we simply reset the hash
+     * chain instead of normalization.  This avoids extra complexity, as we
Quoted 8 lines

lib/kite_deflate.c static void kite_mf_reset(struct kite_matchfinder *mf, · line 872

Quoted 8 lines
lib/kite_deflate.c +8 -0
+     * don't consider extreme large input buffers in one go.
+     *
+     * [1] https://github.com/tukaani-project/xz/blob/v5.4.0/src/liblzma/lz/lz_encoder_mf.c#L94
+     */
+    if (unlikely(mf->base > ((typeof(mf->base))-1) >> 1)) {
+        mf->base = kHistorySize32 + 1;
+        memset(mf->hash, 0, 0x10000 * sizeof(mf->hash[0]));
+        memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);

This line

memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);

is also unnecessary, let me remove it when applying.

I think it still works on your side.

Quoted 5 lines

lib/kite_deflate.c static void kite_mf_reset(struct kite_matchfinder *mf, · line 875

Quoted 3 lines
lib/kite_deflate.c +1 -0
+    }
       mf->offset = 0;
       mf->cyclic_pos = 0;

Thanks!

Thanks,
Gao Xiang

Quoted 2 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

Yifan Zhao

Thanks

Yes, it still works for me.

Applied, thanks!

Thanks,
Gao Xiang

Quoted 5 lines

Thanks,

Yifan Zhao