erofs-utils: lib: reset hc to avoid 32-bit overflow of kite-deflate
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.
--- include/erofs/defs.h | 3 +++ lib/kite_deflate.c | 12 ++++++++++++ 2 files changed, 15 insertions(+)
include/erofs/defs.h +3 -0
@@ -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
@@ -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
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.Reported-by Yifan Zhao <[email protected]>Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")Signed-off-by Gao Xiang <[email protected]>--- include/erofs/defs.h | 3 +++ lib/kite_deflate.c | 12 ++++++++++++ 2 files changed, 15 insertions(+)include/erofs/defs.h +3
-0@@ -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@@ -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
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.
Reported-by Yifan Zhao <[email protected]>Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")Signed-off-by Gao Xiang <[email protected]>---
include/erofs/defs.h| 3 +++
lib/kite_deflate.c | 12 ++++++++++++2 files changed, 15 insertions(+)include/erofs/defs.h +3
-0@@ -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 } #endiflib/kite_deflate.c +3
-0@@ -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;Tested-by Yifan Zhao <[email protected]>
Thanks!
Thanks,
Gao Xiang
Quoted 5 lines
Yifan Zhao
Thanks
Yifan Zhao
Tested-byQuoted 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.
Reported-by Yifan Zhao <[email protected]>Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")Signed-off-by Gao Xiang <[email protected]>---
include/erofs/defs.h| 3 +++
lib/kite_deflate.c | 12 ++++++++++++2 files changed, 15 insertions(+)include/erofs/defs.h +3
-0@@ -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 } #endiflib/kite_deflate.c +2
-0@@ -859,6 +859,18 @@ static void kite_mf_reset(structkite_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
Tested-by Yifan Zhao <[email protected]>Thanks!
Thanks,
Gao XiangQuoted 5 lines
Yifan Zhao
Thanks
Yes, it still works for me.
Thanks,
Yifan Zhao
Gao Xiang
AppliedQuoted 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.
Reported-by Yifan Zhao <[email protected]>Fixes 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")Signed-off-by Gao Xiang <[email protected]>---
include/erofs/defs.h| 3 +++
lib/kite_deflate.c | 12 ++++++++++++2 files changed, 15 insertions(+)include/erofs/defs.h +3
-0@@ -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 } #endiflib/kite_deflate.c +3
-0@@ -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;Tested-by Yifan Zhao <[email protected]>Thanks!
Thanks,
Gao XiangQuoted 2 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Yifan Zhao
Thanks
Yes, it still works for me.
Tested-by Yifan Zhao <[email protected]>
Applied, thanks!
Thanks,
Gao Xiang
Quoted 5 lines
Thanks,
Yifan Zhao