Commit Graph

209 Commits

Author SHA1 Message Date
Yann Collet
41ad238bf9
Merge pull request #515 from svpv/refactorDec
lz4.c: refactor the decoding routines
2018-04-29 07:41:35 -07:00
Yann Collet
47d70e755e
Merge pull request #519 from lz4/fdParser
Faster decoding speed
2018-04-27 11:46:29 -07:00
Yann Collet
d294dd7fc6 ensure favorDecSpeed is properly initialized
also :
- fix a potential malloc error
- proper use of ALLOC macro inside lz4hc
- update html API doc
2018-04-27 09:04:09 -07:00
Alexey Tourbin
d81a434c3d lz4.c: fixed the LZ4_decompress_fast_continue case
The change is very similar to that of the LZ4_decompress_safe_continue
case.  The only reason a make this a separate change is to ensure that
the fuzzer, after it's been enhanced, can detect the flaw in
LZ4_decompress_fast_continue, and that the change indeed fixes the flaw.
2018-04-27 15:10:12 +03:00
W. Felix Handte
0858362f28 Merge _destSize Compress Variant into LZ4_compress_generic() 2018-04-26 18:01:08 -04:00
Alexey Tourbin
5603d30f81 lz4.c: fixed the LZ4_decompress_safe_continue case
The previous change broke decoding with a ring buffer.  That's because
I didn't realize that the "double dictionary mode" was possible, i.e.
that the decoding routine can look both at the first part of the
dictionary passed as prefix and the second part passed via dictStart+dictSize.

So this change introduces the LZ4_decompress_safe_doubleDict helper,
which handles this "double dictionary" situation.  (This is a bit of
a misnomer, there is only one dictionary, but I can't think of a better
name, and perhaps the designation is not all too bad.)  The helper is
used only once, in LZ4_decompress_safe_continue, it should be inlined
with LZ4_FORCE_O2_GCC_PPC64LE attached to LZ4_decompress_safe_continue.

(Also, in the helper functions, I change the dictStart parameter type
to "const void*", to avoid a cast when calling helpers.  In the helpers,
the upcast to "BYTE*" is still required, for compatibility with C++.)

So this fixes the case of LZ4_decompress_safe_continue, and I'm
surprised by the fact that the fuzzer is now happy and does not detect
a similar problem with LZ4_decompress_fast_continue.  So before fixing
LZ4_decompress_fast_continue, the next logical step is to enhance
the fuzzer.
2018-04-26 08:23:54 +03:00
Alexey Tourbin
b4eda8d08f lz4.c: refactor the decoding routines
I noticed that LZ4_decompress_generic is sometimes instantiated with
identical set of parameters, or (what's worse) with a subtly different
sets of parameters.  For example, LZ4_decompress_fast_withPrefix64k is
instantiated as follows:

    return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize,
		full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB);

while the equivalent withPrefix64k call in LZ4_decompress_usingDict_generic
passes 0 for the last argument instead of 64 KB.  It turns out that there
is no difference in this case: if you change 64 KB to 0 KB in
LZ4_decompress_fast_withPrefix64k, you get the same binary code.

Moreover, because it's been clarified that LZ4_decompress_fast doesn't
check match offsets, it is now obvious that both of these fast/withPrefix64k
instantiations are simply redundant.  Exactly because LZ4_decompress_fast
doesn't check offsets, it serves well with any prefixed dictionary.

There's a difference, though, with LZ4_decompress_safe_withPrefix64k.
It also passes 64 KB as the last argument, and if you change that to 0,
as in LZ4_decompress_usingDict_generic, you get a completely different
binary code.  It seems that passing 0 enables offset checking:

    const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));

However, the resulting code seems to run a bit faster.  How come
enabling extra checks can make the code run faster?  Curiouser and
curiouser!  This needs extra study.  Currently I take the view that
the dictSize should be set to non-zero when nothing else will do,
i.e. when passing the external dictionary via dictStart.  Otherwise,
lowPrefix betrays just enough information about the dictionary.

    * * *

Anyway, with this change, I instantiate all the necessary cases as
functions with distinctive names, which also take fewer arguments and
are therefore less error-prone.  I also make the functions non-inline.
(The compiler won't inline the functions because they are used more than
once.  Hence I attach LZ4_FORCE_O2_GCC_PPC64LE to the instances while
removing from the callers.)  The number of instances is now is reduced
from 18 (safe+fast+partial+4*continue+4*prefix+4*dict+2*prefix64+forceExtDict)
down to 7 (safe+fast+partial+2*prefix+2*dict).  The size of the code is
not the only issue here.  Separate helper function are much more
amenable to profile-guided optimization: it is enough to profile only
a few basic functions, while the other less-often used functions, such
as LZ4_decompress_*_continue, will benefit automatically.

This is the list of LZ4_decompress* functions in liblz4.so, sorted by size.
Exported functions are marked with a capital T.

$ nm -S lib/liblz4.so |grep -wi T |grep LZ4_decompress |sort -k2
0000000000016260 0000000000000005 T LZ4_decompress_fast_withPrefix64k
0000000000016dc0 0000000000000025 T LZ4_decompress_fast_usingDict
0000000000016d80 0000000000000040 T LZ4_decompress_safe_usingDict
0000000000016d10 000000000000006b T LZ4_decompress_fast_continue
0000000000016c70 000000000000009f T LZ4_decompress_safe_continue
00000000000156c0 000000000000059c T LZ4_decompress_fast
0000000000014a90 00000000000005fa T LZ4_decompress_safe
0000000000015c60 00000000000005fa T LZ4_decompress_safe_withPrefix64k
0000000000002280 00000000000005fa t LZ4_decompress_safe_withSmallPrefix
0000000000015090 000000000000062f T LZ4_decompress_safe_partial
0000000000002880 00000000000008ea t LZ4_decompress_fast_extDict
0000000000016270 0000000000000993 t LZ4_decompress_safe_forceExtDict
2018-04-25 13:18:06 +03:00
Yann Collet
8c6ca6283d
Merge pull request #511 from lz4/decFast
Fixed performance issue with LZ4_decompress_fast()
2018-04-24 11:25:57 -07:00
Cyan4973
44bff3fd3b re-ordered parenthesis
to avoid mixing && and &
as suggested by @terrelln
2018-04-23 19:26:02 -07:00
Yann Collet
0c2ae72ba8
Merge pull request #507 from lz4/clangPerf
fixed lz4_fast clang performance
2018-04-23 15:55:56 -07:00
Cyan4973
cd0663456f disable shortcut for LZ4_decompress_fast()
improving speed
2018-04-23 15:47:08 -07:00
Nick Terrell
672799e814 Fix compilation error and assert. 2018-04-23 14:21:02 -07:00
Nick Terrell
bb83cad98f Fix input size validation edge cases
The bug is a read up to 2 bytes past the end of the buffer.
There are three cases for this bug, one for each test case added.

* An empty input causes `token = *ip++` to read one byte too far.
* A one byte input with `(token >> ML_BITS) == RUN_MASK` causes
  one extra byte to be read without validation. This could be
  combined with the first bug to cause 2 extra bytes to be read.
* The case pointed out in issue #508, where `ip == iend` at the
  beginning of the loop after taking the shortcut.

Benchmarks show no regressions on clang or gcc-7 on both my mac
and devserver.

Fixes #508.
2018-04-23 13:34:18 -07:00
Cyan4973
d1f21883d6 fixed incorrect comment 2018-04-21 00:11:51 -07:00
Yann Collet
a8a5dfd426 fixed clang performance in lz4_fast
The simple change from
`matchIndex+MAX_DISTANCE < current`
towards
`current - matchIndex > MAX_DISTANCE`

is enough to generate a 10% performance drop under clang.
Quite massive.
(I missed as my eyes were concentrated on gcc performance at that time).

The second version is more robust, because it also survives a situation where
`matchIndex > current`
due to overflows.

The first version requires matchIndex to not overflow.
Hence were added `assert()` conditions.

The only case where this can happen is with dictCtx compression,
in the case where the dictionary context is not initialized before loading the dictionary.
So it's enough to always initialize the context while loading the dictionary.
2018-04-20 18:09:51 -07:00
Yann Collet
62d7cdcc74
Merge pull request #503 from lz4/l120
minor length reduction of several large lines
2018-04-19 11:50:20 -07:00
Yann Collet
dede47f13b
Merge pull request #502 from lhacc1/dev
Wrap likely/unlikely macroses with #ifndef
2018-04-19 10:52:48 -07:00
Yann Collet
46058d71aa modified indentation for consistency 2018-04-19 10:50:40 -07:00
Yann Collet
4785bd6a35 minor length reduction of several large lines 2018-04-18 16:49:27 -07:00
Dmitrii Rodionov
ea6ed46fc2 Wrap likely/unlikely macroses with #ifndef
It prevent redefine error when project using lz4 has its own likely/unlikely
macroses.
2018-04-18 12:20:56 +03:00
Yann Collet
5ad4599c5a fixed LZ4_compress_fast_extState_fastReset() in 32-bit mode 2018-04-17 16:47:56 -07:00
Yann Collet
88cca1723e fix dictDelta setting error
wrong test
2018-04-17 16:18:37 -07:00
Yann Collet
1520642183 fix matchIndex overflow
can happen with dictCtx
2018-04-17 15:29:17 -07:00
Yann Collet
ce78d10c1f Merge branch 'dev' into lowAddr 2018-04-17 12:07:22 -07:00
W. Felix Handte
aedc447804 Always Bump Offset by 64 KB in LZ4_loadDict()
This actually ensures the guarantee referred to in the comment in
LZ4_compress_fast_continue().
2018-04-17 14:09:00 -04:00
Yann Collet
da3b5ba6f0 fixed dictCtx compression 2018-04-16 23:59:42 -07:00
Yann Collet
444211d259 edited a few traces for debugging 2018-04-16 17:16:08 -07:00
Yann Collet
a3aeb34184 fixed minor format warnings 2018-04-16 16:54:03 -07:00
Yann Collet
e928064797 fixed gcc performance regression 2018-04-16 15:11:28 -07:00
Yann Collet
d2bcfa31f5 fixed minor unused variable warning 2018-04-13 02:45:32 -07:00
Yann Collet
c40bac31d3 added comment on variables required after _next_match 2018-04-13 02:26:14 -07:00
Yann Collet
54ec83ce1f fixed potential ptrdiff_t overflow (32-bits mode)
Also removed pointer comparison, which should solve #485
2018-04-13 02:10:53 -07:00
Cyan4973
57afa36795 compatibility with gcc-4.4 string.h version
Someone found it would be a great idea to define there a global variable under the very generic name "index".
Cause problem with shadow warnings, so no variable can be named "index" now ...

Also : automatically update API manual
2018-04-13 01:01:54 -07:00
test4973
db9aa785c5 fixed : counting matches which overlap extDict and prefix 2018-04-12 16:12:21 -07:00
test4973
8af32ce6f7 modified a few traces for debug 2018-04-12 13:35:19 -07:00
test4973
1838803948 fixed LZ4_compress_fast_extState_fastReset() 2018-04-11 16:49:40 -07:00
test4973
b183066793 Merge branch 'dev' into lowAddr 2018-04-11 16:45:19 -07:00
W. Felix Handte
056ea63215 Fix Silly Warning (const-ness in declaration has no effect on value types!) 2018-04-11 18:42:09 -04:00
W. Felix Handte
51a56c47c0 Minor Fixes 2018-04-11 18:06:48 -04:00
W. Felix Handte
3a0c571272 Add a LZ4_STATIC_LINKING_ONLY Macro to Guard Experimental APIs 2018-04-11 18:06:10 -04:00
W. Felix Handte
afa52c9b95 Expose dictCtx Functionality in LZ4 2018-04-11 16:28:56 -04:00
W. Felix Handte
21f0c9700b Rename _extState_noReset -> _extState_fastReset and Edit Comments 2018-04-11 15:13:01 -04:00
W. Felix Handte
c18bff933b Remove Extraneous Assignment (clearedTable == 0) 2018-04-11 15:12:34 -04:00
W. Felix Handte
59c7d95121 Expose a Faster Stream Reset Function 2018-04-10 13:26:17 -04:00
test4973
cf2f06a6c5 fixed minor conversion warning
ptr diff -> U32
2018-04-09 17:08:17 -07:00
test4973
b28abb9f18 Merge branch 'dev' into lowAddr 2018-04-09 16:23:39 -07:00
W. Felix Handte
f88dc90055 Avoid Calling LZ4_prepareTable() in LZ4_compress_fast_continue() 2018-04-09 16:17:33 -04:00
W. Felix Handte
5622c276e1 Return to Allowing Early Returns in LZ4_compress_generic()
Or: `goto` Considered Harmful

Or: https://xkcd.com/292/
2018-04-06 19:28:08 -04:00
test4973
f9992fa37f noticed a bug when re-using hash table
./fuzzer -vv -s4217 -t7518
2018-04-05 19:09:24 -07:00
test4973
f4e06e28e6 fixed byPtr mode
switch to byU32 when src address is < 64K

note : byPtr is still useful in 32-bits, as it's about ~10% faster
2018-04-05 18:29:42 -07:00