Commit ddd26d6b for xz

commit ddd26d6b0474b77ee2e0a131b03186b7eb96f42f
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Sat Oct 17 18:37:39 2026 +0300

    Tests: Add tests for the threaded encoder bugs

    test_bug1 results in invalid memory writes if the bug fix is reverted.
    It should consistently crash even without sanitizers.

    test_bug2 and test_bug3 depend on timing luck. They aren't guaranteed to
    fail every time or on every system if the bug fixes are reverted.
    Especially test_bug3 can be difficult to trigger, but I didn't want
    to increase the iteration count a lot because then the single test
    would consume most of the execution time of the entire test suite.

    Thanks-to: Mar <mardgez13@gmail.com>

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 889b767c..4843425a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -43,6 +43,7 @@ check_PROGRAMS = \
 	test_raw \
 	test_stream_buffer_decode \
 	test_stream_buffer_encode \
+	test_stream_encoder_mt \
 	test_stream_flags \
 	test_filter_flags \
 	test_filter_str \
@@ -61,6 +62,7 @@ TESTS = \
 	test_raw \
 	test_stream_buffer_decode \
 	test_stream_buffer_encode \
+	test_stream_encoder_mt \
 	test_stream_flags \
 	test_filter_flags \
 	test_filter_str \
diff --git a/tests/test_stream_encoder_mt.c b/tests/test_stream_encoder_mt.c
new file mode 100644
index 00000000..d4d47172
--- /dev/null
+++ b/tests/test_stream_encoder_mt.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: 0BSD
+
+///////////////////////////////////////////////////////////////////////////////
+//
+/// \file       test_stream_encoder_mt.c
+/// \brief      Test lzma_stream_encoder_mt()
+//
+//  Author:     Lasse Collin
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include "tests.h"
+#include "mythread.h"
+
+
+#if defined(MYTHREAD_ENABLED) && defined(HAVE_ENCODERS)
+static const lzma_options_lzma lzma2_opt = {
+	.dict_size = LZMA_DICT_SIZE_MIN,
+	.preset_dict = NULL,
+	.lc = LZMA_LC_DEFAULT,
+	.lp = LZMA_LP_DEFAULT,
+	.pb = LZMA_PB_DEFAULT,
+	.mode = LZMA_MODE_FAST,
+	.nice_len = 8,
+	.mf = LZMA_MF_HC4,
+	.depth = 0,
+};
+
+
+static const lzma_filter filters[] = {
+	{ LZMA_FILTER_LZMA2, (lzma_options_lzma *)&lzma2_opt },
+	{ LZMA_VLI_UNKNOWN, NULL }
+};
+
+
+static const uint8_t in[4096] = "Hello World!";
+
+static uint8_t out[4096];
+
+
+// This fails before the commit:
+// "liblzma: mt enc: Don't reuse threads on reinit if block size differs"
+static void
+test_bug1(void)
+{
+	// Start with a tiny block size.
+	lzma_mt mt = {
+		.flags = 0,
+		.threads = 1,
+		.block_size = 5,
+		.timeout = 0,
+		.filters = filters,
+		.check = LZMA_CHECK_CRC32,
+	};
+
+	lzma_stream strm = LZMA_STREAM_INIT;
+	assert_lzma_ret(lzma_stream_encoder_mt(&strm, &mt), LZMA_OK);
+
+	strm.next_in = in;
+	strm.avail_in = 12;
+	strm.next_out = out;
+	strm.avail_out = sizeof(out);
+	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
+
+	// Increase to big enough that it should crash without sanitizers.
+	mt.block_size = 4U << 20;
+	assert_lzma_ret(lzma_stream_encoder_mt(&strm, &mt), LZMA_OK);
+
+	lzma_ret ret;
+	do {
+		strm.next_in = in;
+		strm.avail_in = sizeof(in);
+		strm.next_out = out;
+		strm.avail_out = sizeof(out);
+
+		const lzma_action action = strm.total_in > 2 * mt.block_size
+				? LZMA_FINISH : LZMA_RUN;
+		ret = lzma_code(&strm, action);
+	} while (ret == LZMA_OK);
+
+	assert_lzma_ret(ret, LZMA_STREAM_END);
+
+	lzma_end(&strm);
+}
+
+
+// It depends on timing luck, but this might fail before the commit:
+// "liblzma: mt enc: Always return workers to coder->threads_free on THR_STOP"
+static void
+test_bug2(void)
+{
+	// Set a timeout so that lzma_code() will eventually return instead
+	// of hang if the first bug is present. The code shouldn't need to
+	// wait at all, so a short timeout must work even on slow systems.
+	lzma_mt mt = {
+		.flags = 0,
+		.threads = 1,
+		.block_size = 5,
+		.timeout = 1,
+		.filters = filters,
+		.check = LZMA_CHECK_CRC32,
+	};
+
+	lzma_stream strm = LZMA_STREAM_INIT;
+
+	const unsigned repeats = 100;
+	for (unsigned i = 0; i < 2 * repeats; ++i) {
+		assert_lzma_ret(lzma_stream_encoder_mt(&strm, &mt), LZMA_OK);
+		strm.total_in = 0;
+		strm.next_in = in;
+		strm.avail_in = 3;
+		strm.next_out = out;
+		strm.avail_out = sizeof(out);
+		assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_OK);
+		assert_uint_eq(strm.total_in, 3);
+	}
+
+	lzma_end(&strm);
+}
+
+
+// It depends on timing luck, but this might fail before the commit:
+// "liblzma: mt enc: Fix threads_stop(coder, true)"
+static void
+test_bug3(void)
+{
+	// Use a timeout to prevent lzma_code() from hanging if the bug
+	// tested by test_bug2() is present. The timeout needs to be large
+	// enough that at least one .xz Block will be encoded in that time
+	// even on a slow machine.
+	lzma_mt mt = {
+		.flags = 0,
+		.threads = 4,
+		.block_size = 5,
+		.timeout = 6000,
+		.filters = filters,
+		.check = LZMA_CHECK_CRC32,
+	};
+
+	// Once the encoder has accepted this much input, it must have
+	// encoded at least one .xz Block.
+	const size_t in_limit = mt.threads * mt.block_size + 2;
+
+	lzma_stream strm = LZMA_STREAM_INIT;
+
+	// NOTE: A million tries would trigger the bug more consistently,
+	// but it would slow down the test suite.
+	for (unsigned i = 0; i < 10000; ++i) {
+		assert_lzma_ret(lzma_stream_encoder_mt(&strm, &mt), LZMA_OK);
+		strm.total_in = 0;
+		strm.next_in = in;
+		strm.avail_in = in_limit;
+		strm.next_out = out;
+		strm.avail_out = sizeof(out);
+
+		// LZMA_PROG_ERROR here is a possible failure. It comes from
+		// lzma_index_append() that is called with invalid
+		// Unpadded Size when the bug has corrupted thr->outbuf->pos
+		// and thr->outbuf->finish in worker_start().
+		assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_OK);
+		assert_uint_eq(strm.total_in, in_limit);
+
+		// Another possible failure is invalid progress info.
+		uint64_t progress_in;
+		uint64_t progress_out;
+		lzma_get_progress(&strm, &progress_in, &progress_out);
+
+		// At least one .xz Block must have been encoded.
+		assert_uint(progress_in, >=, mt.block_size);
+
+		// Input progress cannot be more than the amount of input
+		// given to the encoder.
+		assert_uint(progress_in, <=, in_limit);
+	}
+
+	lzma_end(&strm);
+}
+#endif
+
+
+extern int
+main(int argc, char **argv)
+{
+	tuktest_start(argc, argv);
+
+#if !defined(MYTHREAD_ENABLED) || !defined(HAVE_ENCODERS)
+	tuktest_early_skip("Threading or encoder support is disabled");
+#else
+	tuktest_run(test_bug1);
+	tuktest_run(test_bug2);
+	tuktest_run(test_bug3);
+#endif
+
+	return tuktest_end();
+}
diff --git a/tests/tests.cmake b/tests/tests.cmake
index 868f9fbb..474a7254 100644
--- a/tests/tests.cmake
+++ b/tests/tests.cmake
@@ -65,6 +65,7 @@ if(BUILD_TESTING)
         test_raw
         test_stream_buffer_decode
         test_stream_buffer_encode
+        test_stream_encoder_mt
         test_stream_flags
         test_vli
     )