1
0
mirror of https://github.com/lz4/lz4.git synced 2026-10-24 01:48:57 +03:00

109 Commits

Author SHA1 Message Date
Yann Collet
5d63c34ac0 tests: move the simple block unit tests from fuzzer.c to unittest
33 tests, in 2 new suites : lz4 (decoding of invalid input, one-shot and streaming compression)
and lz4hc (streaming, dictionaries, destSize). Tests depending on the HC level run on both levels
previously selected by fuzzer.

UT_sample(), KB, MB and ARRAY_SIZE() are now provided by unittest.h, for all suites.
2026-10-23 10:54:38 -07:00
Yann Collet
04c771f442 tests: add unittest, a unit test framework with autonomous tests
Each test is a function with its own context : resources are released at the end of the test,
even when it fails, and the random generator restarts from the same seed.
A failed assertion ends the test, the runner continues with the next one.

First suite : 18 lz4frame unit tests, moved from frametest.c
2026-10-22 15:52:14 -07:00
Yann Collet
5d0c796189 lib/Makefile: Windows DLL built through multiconf, so that changing MULTITHREAD rebuilds it
The DLL was compiled directly from sources, without the objects' cache keyed by flags :
after make lib-nomt, make lib-mt kept the DLL without threads (and conversely).
It now uses c_dynamic_library(), as the Unix shared library, with the multi-threading flags
in the cache key ; the resource object is an additional dependency. The import library
is the same for both variants (same exports). With BUILD_SHARED=no, liblz4 builds nothing, as before.
Checked with mingw-w64 : MULTITHREAD=0 / 1 / back to 1 (cached) switch the DLL's thread imports ;
a program linked to the multi-threaded DLL compresses with nbWorkers=4 under wine.
2026-10-22 12:58:19 -07:00
Yann Collet
88ed0b630e api: LZ4F_getMaxNbWorkers() reports multi-threading support ; CLI thread cap follows liblz4, lz4 -V reports multithreading (checkpoint)
LZ4F_getMaxNbWorkers() (static linking only, experimental like nbWorkers) :
0 when liblz4 is built without LZ4F_MULTITHREAD, otherwise the cap of nbWorkers,
now 1024 on 64-bit systems and 128 on 32-bit ones (was 200).
lz4 : build macro LZ4_NBWORKERS_MAX removed ; worker threads of frame and legacy
formats are capped by LZ4F_getMaxNbWorkers(), 1 without liblz4 multi-threading
(restores a cap for the legacy format on POSIX). lz4 -V reports "multithread"
only when both lz4 and liblz4 support it ; -T# warning and -v note accordingly.
VS2022 _test.bat checks the version line, catching a CLI built without LZ4F_MULTITHREAD
(same output, so no other observable difference).
frameMT : validateMaxNbWorkers ; test builds use LZ4F_NBWORKERS_MAX=128.
2026-10-22 12:58:19 -07:00
Yann Collet
d4a0782b47 review round 10: failed LZ4F_compressBegin() invalidates on allocation only, deferred emission of completed jobs, VS2022 x64 CLI multithreaded (checkpoint)
LZ4F_compressBegin*() validates its arguments first (dictSize included) :
rejecting them leaves a frame in progress untouched, jobs in flight included
(contract of #1817, tested by frametest). Past validation, the frame in progress
is abandoned before cctx is modified : an allocation failure invalidates it
(previously, cStage remained 1 with an inconsistent cctx, also with nbWorkers == 0).
lz4frame.h : LZ4F_srcStableUntilFlush lifetime and LZ4F_compressBegin() failures clarified.
Without autoFlush, LZ4F_compressUpdate() defers completed jobs not fitting into dst,
instead of failing : it only fails when output must be emitted (all job slots busy).
VS2022 : lz4.vcxproj x64 configurations define LZ4F_MULTITHREAD=1.
frameMT : validateDeferredEmission, validateBeginFailures.
2026-10-22 12:58:19 -07:00
Yann Collet
c6e5a8cf53 review round 9: destination too small invalidates MT frames (test), --rm keeps a renamed or replaced source, multithreaded CLI tests, MSan step (checkpoint) 2026-10-22 12:58:19 -07:00
Yann Collet
869ed8083f build: shared pthread probe within a private directory, also for programs (checkpoint)
Prototype, not intended for a PR as is.

programs/Makefile probed pthread with a fixed have_pthread.c in its directory
(pre-existing in dev) : concurrent invocations collided (24 / 192 concurrent probes
wrongly reported no threads, producing an lz4 CLI without multi-threading),
and read-only source trees made it fail.

- build/make/lz4defs.make : PTHREAD_PROBE, evaluated with the CC and ALLFLAGS of
  the invoking Makefile, within a private temporary directory (mktemp -d).
- lib/Makefile and programs/Makefile employ it (lib/'s copy from previous commit removed).

Validated : probes of lib/ and programs/ (192 concurrent, read-only trees), full tests
MT / no-MT, ASAN+UBSAN, TSAN, make test, wine, CLI build (lz4 multithread, lz4-nomt not),
build policies, install scenarios + test_install.sh.
2026-10-22 04:26:50 -07:00
Yann Collet
0852fddadc lz4frame: streaming multi-threaded compression, persistent pool, CLI uses it (checkpoint)
Prototype, not intended for a PR as is.

- persistent thread pool within LZ4F_cctx (pthread / Win32), replaces the per-call fork-join;
  nbWorkers is now the nb of worker threads (the calling thread also runs jobs while waiting).
- streaming : LZ4F_compressUpdate() / LZ4F_flush() / LZ4F_compressEnd() cut input into jobs
  (4 MB linked, 1 MB independent), at fixed positions of the content; outputs emitted in order;
  input read in place with stableSrc or autoFlush, copied otherwise; autoFlush respected;
  LZ4F_compressBound() covers jobs in flight; LZ4F_uncompressedUpdate() drains pending jobs.
- one-shot compression goes through the same engine.
- CLI : frame compression goes through LZ4F streaming with nbWorkers (output identical to
  previous CLI, and lz4 == lz4-nomt); custom block sizes fall back to single-thread.
- build systems compile the CLI's liblz4 sources with LZ4F_MULTITHREAD.
- tests/frameMT.c : streaming validation (slicing, stableSrc, flushes, autoFlush,
  uncompressed segments, exact bounds, abandoned frames).
2026-10-22 04:26:50 -07:00
Yann Collet
02916432ee lz4frame: experimental multi-threaded one-shot compression (checkpoint)
Prototype, not intended for a PR as is.

- LZ4F_preferences_t.nbWorkers (replaces reserved[0], ABI preserved):
  LZ4F_compressFrame() and LZ4F_compressFrame_usingCDict() cut input into jobs,
  compressed by up to nbWorkers threads (inline fork-join, threads created per call).
  Output only depends on parameters, never on nbWorkers (>= 1) nor scheduling;
  identical to single-threaded with independent blocks, identical to the CLI -T output.
- LZ4F_MULTITHREAD build macro (default 0) : without it, jobs run serially, same output.
- build policy (zstd-like) : dynamic library multi-threaded by default, static library not
  (make lib-mt / lib-nomt / MULTITHREAD=, cmake LZ4_SHARED/STATIC_MULTITHREAD,
  meson shared-/static-multithread, VS2022 liblz4-dll).
- internal LZ4F_compressBlocks_internal() extracted from LZ4F_compressUpdateImpl().
- tests/frameMT.c (make -C tests test-frameMT) : validation + bench mode.

Known gaps : Win32 thread path never compiled; no persistent thread pool; no streaming.
2026-10-22 04:26:50 -07:00
Yann Collet
f42fdc171d README: refresh text, links and build pointers
- grammar: "a lossless compression algorithm", "multi-core CPUs",
  "The LZ4 library ... the BSD 2-Clause license", "defined in"
- clarify that the acceleration factor tunes compression speed
- dictionary API / CLI links were pinned to v1.8.3:
  point to the in-tree lib/lz4frame.h and programs/lz4.1.md instead
- Zstandard dictionary builder link: v1.3.5 -> dev
- mention the GPL-2.0-or-later license of the CLI, as stated in LICENSE
- switch homepage, zlib, zstd and Silesia links to https
- point to build/ for CMake, Meson and Visual Studio builds
- vcpkg instructions: ./vcpkg, not ./vcpkg.exe, after bootstrap-vcpkg.sh
- list the API headers in the Documentation section

build/README.md: VS2022 solution outputs to build\VS2022\bin, not build\VS2010\bin
2026-10-17 17:57:45 -07:00
Yann Collet
aa787570e6 Windows CI: test lz4 -l on an empty file, NEWS for #1740
On Windows, TPool_jobsCompleted() only waited on an auto-reset event,
set when the pending job count drops to 0 :
- with no job submitted, the event is never set and the wait never ends.
  `lz4 -l` on an empty file submits no write job,
  and decoding the resulting stream (magic number only) submits no decoding job.
- ResetEvent() + increment in TPool_submitJob() race with
  decrement + SetEvent() in a worker : the event can stay set while a job
  is still pending, so the wait could end before the last write.
#1740 checks the pending job count before waiting, and after each wake up.

The Windows CI never ran lz4 on files :
_test.bat now compresses and decodes an empty file in legacy format.
2026-10-17 09:01:28 -07:00
Yann Collet
ff2b42fa5f cmake: enable multithreading in lz4 programs on pthread platforms
The Makefile and meson enable multithreading when pthread is available,
and Windows enables it by default (programs/lz4conf.h).
CMake never did : on Linux, macOS or BSD, its lz4 binary was single-threaded.

CMake now detects pthread (find_package(Threads)) for lz4 and lz4c.
The new option LZ4_BUILD_MULTITHREAD (default ON) disables it, Windows included.

silesia.tar, lz4 -1 : 0.89 s => 0.25 s, same output size.
2026-10-12 16:21:54 -07:00
Yann Collet
518f56a4bd Avoid resetting make jobserver in submakes 2026-05-28 20:48:18 -07:00
jlee303
3e231ebc2c Merge branch 'lz4:dev' into compress_bound_one 2026-02-23 22:30:41 -08:00
Jennifer Lee
3afa73236c fix test errors 2026-02-13 18:04:27 -08:00
Yann Collet
aa8694a11e make compilation uses multithreading by default 2026-01-25 10:59:16 -08:00
Tomas Lopez
79a80135dd Fix typo 2025-05-30 14:28:32 -04:00
Yann Collet
1079933be4 cmake: Add configurable LZ4_DEBUG_LEVEL (0-8) with CMake GUI support
- Replace fixed LZ4_DEBUG=1 for Debug builds with configurable levels
- Level 0: Disabled (default for Release builds)
- Level 1: Enable assert() statements (default for Debug builds)
- Levels 2-8: Enable debug traces with increasing verbosity
- Add CMake GUI dropdown with options 0-8 for easy configuration
- Maintain MSVC and GCC/Clang compiler compatibility
- Preserve CMake 3.5+ compatibility using add_definitions()
- Only apply macro when level > 0 to avoid unnecessary definitions
2025-05-25 03:18:01 +00:00
Yann Collet
a60fb3b5e7 cmake: Enable LZ4_DEBUG=1 in Debug builds
- Add conditional compilation flag for Debug builds
- Set LZ4_DEBUG=1 to enable assert() statements when debugging
- Use compiler-specific syntax: /DLZ4_DEBUG=1 for MSVC, -DLZ4_DEBUG=1 for GCC/Clang
- Ensure CMake 3.5+ compatibility using add_definitions()
- Apply globally to all targets for consistent debug behavior
2025-05-25 02:01:19 +00:00
Yann Collet
55061612bd cmake: Fix symlink creation race condition in parallel builds
Add missing DEPENDS lz4cli to symlink creation targets to ensure
the lz4cli executable is built before attempting to create symlinks.
This resolves 'No such file or directory' errors when using --parallel
builds where symlink targets could run before the executable target
was completed.

Fixes parallel build compatibility for Unix symlink creation.
2025-05-24 22:02:54 +00:00
Yann Collet
dc03bcc33b Add default build type configuration to CMakeLists.txt
- Set Release as default build type when none is specified
- Prevents undefined build behavior when running cmake without -DCMAKE_BUILD_TYPE
- Includes proper cache configuration with user-friendly description
- Provides dropdown options for cmake-gui users
2025-05-24 21:27:32 +00:00
Yann Collet
684ffb3f22 Update CMake version requirements to support broader compatibility
This commit:
- Maintains CMake compatibility with older systems by keeping the minimum version at 3.5
- Sets the maximum policy version to 4.0.2 to ensure consistent behavior with newer CMake versions
- Adds a comment explaining the version range specification purpose
- Ensures future-proofing as newer CMake versions will use the 4.0.2 policy set
2025-05-24 19:26:28 +00:00
Yann Collet
b4c20713d4 Improve CMakeLists.txt readability with better structure
This commit improves the readability and maintainability of the CMake build system by:
1. Adding clear section headers with descriptive comments
2. Fixing indentation consistency throughout the file
3. Adding labels to endif() statements to match their corresponding if statements
4. Using visual separators (dashed lines) for better visual organization

These changes make the file easier to navigate without changing its functionality.
2025-05-24 19:12:48 +00:00
Yann Collet
6a65138bd8 update "make lib" from tests/
to not override CFLAGS required adjustment (+fPIC)
2025-05-05 18:51:19 -07:00
Yann Collet
55341797c3 changed way to add -fPIC 2025-05-05 17:53:56 -07:00
Yann Collet
191efabe60 dynamic library built with multiconf.make
on posix.

Windows build is more complex.
It's doubtful if it's worth forcing its integration within multiconf.make.
2025-05-05 17:33:38 -07:00
Yann Collet
fe679bf80d update multiconf.make 2025-05-05 16:36:27 -07:00
Yann Collet
c9c9bf8c12 added multiconf doc 2025-05-01 16:53:52 -07:00
GeorgH93
7f71be01f2 Update cmake minimum required version (#1601)
Update cmake minimum required version to work with CMake v4 and shut up about the deprecation warning (for cmake_minimum_required < 3.10) on CMake v3.31+
2025-04-27 11:09:49 -07:00
Eli Schwartz
af1a58d71e meson: fix broken oss-fuzz support
Regression in original introduction in commit
198e532300.

Every meson file in the repository must have this variable set, to an
appropriate per-file value, in order to cater to the split buildsystem
design where the build files are stored in a subdirectory away from the
source code.

If ossfuzz is enabled, this file is processed and the missing value will
come from some other file instead, which may not be correct.

- Using default values, ossfuzz is enabled but no other subdirs are, other
  than of course lib, which has a coincidentally identical value, and
  everything works.

- If contrib is enabled, it will set the per-file variable to a value
  that is *not* coincidentally the same as ossfuzz.

- If examples is enabled, it will set the per-file variable to a value
  that is coincidentally the same as ossfuzz... and do so later than
  contrib.

So, if contrib is enabled but examples is not, then the ossfuzz
directory fails to process correctly and the build aborts. Fix this by
explicitly adding the correct per-file variable.

Fixes: https://github.com/lz4/lz4/issues/1559
2025-01-27 23:46:31 -05:00
Yann Collet
7887022e5d Merge pull request #1510 from ds-sloth/respect-global-pic
CMakeLists.txt: respect global CMAKE_POSITION_INDEPENDENT_CODE flag
2024-11-05 10:59:46 -08:00
Yann Collet
c612292343 Merge pull request #1524 from bgilbert/freestanding
build/meson: force `LZ4_DEBUG=0` for `tests/freestanding`
2024-10-06 14:37:03 -07:00
Benjamin Gilbert
b8f1cc1f07 build/meson: force LZ4_DEBUG=0 for tests/freestanding
Match the Makefile, which never sets LZ4_DEBUG for this test.  If
LZ4_DEBUG is non-zero, lib/lz4.c #includes <assert.h>, the musl version of
which has a conflicting definition for __assert_fail:

    https://www.openwall.com/lists/musl/2019/03/04/6

Fixes build failure:

    ../../../tests/freestanding.c:173:6: error: conflicting types for '__assert_fail'; have 'void(const char *, const char *, unsigned int,  const char *)'
      173 | void __assert_fail(const char * assertion, const char * file, unsigned int line, const char * function) {
          |      ^~~~~~~~~~~~~
    In file included from ../../../tests/../lib/lz4.c:270,
                     from ../../../tests/freestanding.c:49:
    /usr/include/assert.h:19:16: note: previous declaration of '__assert_fail' with type 'void(const char *, const char *, int,  const char *)'
       19 | _Noreturn void __assert_fail (const char *, const char *, int, const char *);
          |                ^~~~~~~~~~~~~
2024-10-06 04:11:36 -07:00
Benjamin Gilbert
6d17921245 build/meson: avoid passing long options to ln
On macOS, ln only supports short options.
2024-10-06 03:18:27 -07:00
Benjamin Gilbert
ac3e03c55c build/meson: update directory path in README 2024-10-06 03:15:38 -07:00
ds-sloth
0e44420380 CMakeLists.txt: use CMAKE_POSITION_INDEPENDENT_CODE to set default only 2024-09-16 19:21:35 -04:00
ds-sloth
80d4c6215a CMakeLists.txt: respect global CMAKE_POSITION_INDEPENDENT_CODE flag 2024-09-11 21:37:35 -04:00
Yann Collet
45fda541f0 Merge pull request #1485 from grobian/meson-no-stdc99
meson: do not force c99 mode
2024-08-03 23:05:47 -07:00
Yann Collet
3d946c68aa Merge pull request #1479 from heitbaum/cmake
add back lz4c target but default to OFF
2024-07-30 13:10:42 -07:00
Fabian Groffen
bce6a94c25 meson: do not force c99 mode
On Solaris/OpenIndiana hosts forcing (old) C99 means disabling POSIX
2001 functionality, resulting in errors like

In file included from ../../../net/ptah/export/gentoo/working-repos/lz4/programs/bench.c:39:
../../../net/ptah/export/gentoo/working-repos/lz4/programs/util.h: In function  UTIL_getOpenFileSize’:
../../../net/ptah/export/gentoo/working-repos/lz4/programs/util.h:156:23: error: implicit declaration of function ‘fileno’ [-Wimplicit-function-declaration]
  156 | #  define UTIL_fileno fileno
      |                       ^~~~~~
../../../net/ptah/export/gentoo/working-repos/lz4/programs/util.h:325:10: note: in expansion of macro ‘UTIL_fileno’
  325 |     fd = UTIL_fileno(file);
      |          ^~~~~~~~~~~

These can be fixed either by forcing a standard to be applied in programs/platform.h or by not forcing the compiler to use an old standard.

Since CMake and Makefile don't force C99 by default either, just drop it from
meson.build.

Signed-off-by: Fabian Groffen <grobian@gentoo.org>
2024-07-30 21:50:06 +02:00
Yann Collet
fe399506bd Merge pull request #1469 from hhoffstaette/no-sp
Fix building freestanding test with clang & enabled stack protector
2024-07-28 21:32:24 -07:00
Rudi Heitbaum
6969661757 add back lz4c target but default to OFF
Partially revert "removed lz4c target"
This reverts commit 65998fecaf.
2024-07-28 01:26:49 +00:00
Kai Pastor
ca197e67a0 Add an lz4::lz4 target to the exported package 2024-07-23 20:32:43 +02:00
Holger Hoffstätte
d08826c4fd Fix building freestanding test with clang & enabled stack protector
The freestanding test is special as it requires reimplementation of
runtime/toolchain functionality. When building with clang where the
compiler is configured to enable the stack-protector by default,
building fails due to missing __stack_chk_fail symbols.
Thefore disable stack protection specifically for this test.
2024-07-23 13:36:41 +02:00
Yann Collet
65998fecaf removed lz4c target
should no longer be necessary
2024-07-20 17:26:32 -07:00
Yann Collet
4cc04310c9 remove Visual Solutions
keep VS2022 for the time being,
as several tests still depend on its presence
2024-07-15 16:57:17 -07:00
Yann Collet
b20025fd03 minor readability refactor for version extraction logic 2024-07-12 21:21:44 -07:00
Yann Collet
f76c979f84 add lz4file.h to include list 2024-07-12 21:11:10 -07:00
Yann Collet
e5563ea26c update logic that determines LZ4_BUNDLED_MODE
smaller and hopefully clearer
2024-07-12 19:46:44 -07:00
Yann Collet
6ce6e6da6f improved logic to extract version number 2024-07-12 18:06:13 -07:00