This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-10-quiet-head
in repository terminology.
View the commit online.
commit 942b4e9c6e05b8ee17236ab37aa0461c442ecd44
Author: Boris Faure <[email protected]>
AuthorDate: Tue Nov 3 17:50:30 2026 +0000
tyfuzz: take the read size and a repeat from a 3-byte header
Two kinds of bugs need more than the raw byte stream: those at read
boundaries only show with small reads, and those in the buffering of
sequences that span reads need megabytes of unterminated input, more
than mutators grow an input to. An input now starts with a 3-byte
header: how to split the body into reads, and how many times to repeat
a span of it, up to 4 MiB. The format is documented in tytest_common.c,
and a unit test pins the expansion down so that a crash file replays as
it was fuzzed.
A file argument is fed as one test case, in the AFL++ build too:
afl-cmin, afl-showmap and saved crash files pass the input that way,
outside the fork server's shared memory.
---
src/bin/tyfuzz.c | 53 +++++++++++++++++++++++++----
src/bin/tytest.c | 52 +++++++++++++++++++++++++++++
src/bin/tytest_common.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/tytest_common.h | 7 ++++
src/bin/unit_tests.h | 1 +
5 files changed, 196 insertions(+), 6 deletions(-)
diff --git a/src/bin/tyfuzz.c b/src/bin/tyfuzz.c
index b7fce990..4f9062b3 100644
--- a/src/bin/tyfuzz.c
+++ b/src/bin/tyfuzz.c
@@ -21,6 +21,38 @@ int _log_domain = -1;
__AFL_FUZZ_INIT();
#endif
+/* Feed a file as one test case. afl-showmap and afl-cmin with @@ pass the
+ * input this way, and so do saved crash and hang files; the fork server's
+ * shared memory test case does not exist in those runs, so it has to be
+ * read from the file regardless of the AFL build. Returns 0 on success. */
+static int
+_feed_file(const char *path)
+{
+ unsigned char *data;
+ struct stat st;
+ ssize_t len;
+ int fd = open(path, O_RDONLY);
+
+ if ((fd < 0) || (fstat(fd, &st) != 0) || (st.st_size < 0))
+ return 1;
+
+ data = ""
+ assert(data);
+ len = 0;
+ while (len < st.st_size)
+ {
+ ssize_t res = read(fd, data + len, st.st_size - len);
+
+ if (res <= 0) break;
+ len += res;
+ }
+ close(fd);
+
+ tytest_common_feed_input(data, len);
+ free(data);
+ return 0;
+}
+
int
main(int argc, char **argv)
{
@@ -37,6 +69,19 @@ main(int argc, char **argv)
* between iterations. Necessarily single-threaded state (the feedback
* map) comes from the AFL runtime. */
__AFL_INIT();
+#endif
+
+ if (argc > 1)
+ {
+ int res = _feed_file(argv[1]);
+
+ tytest_common_shutdown();
+ ecore_shutdown();
+ eina_shutdown();
+ return res;
+ }
+
+#if defined(__AFL_HAVE_MANUAL_CONTROL)
{
unsigned char *buf = __AFL_FUZZ_TESTCASE_BUF;
@@ -47,15 +92,11 @@ main(int argc, char **argv)
if (len <= 0) continue;
tytest_common_reset();
- tytest_common_feed_chunked((const char *)buf, len, 4096);
+ tytest_common_feed_input(buf, len);
}
}
#else
- if (argc > 1)
- {
- tytest_common_set_fd(open(argv[1], O_RDONLY));
- }
-
+ tytest_common_set_fd(STDIN_FILENO);
tytest_common_main_loop();
tytest_common_shutdown();
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 78e9f786..99e55f86 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -116,6 +116,7 @@ static struct {
{ "palette_slots", tytest_palette_slots},
{ "truecolor_cache_generation", tytest_truecolor_cache_generation},
{ "reset", tytest_reset},
+ { "fuzz_header", tytest_fuzz_header},
{ NULL, NULL},
};
@@ -707,6 +708,57 @@ _tytest_checksum(Termpty *ty)
printf("%s", md5out);
}
+/* The fuzzers' test case format: a 3-byte header then the body. Feed the
+ * header'd cases and check what reaches the parser: the expansion decides
+ * what a fuzzer input turns into, so a wrong expansion would silently fuzz
+ * something else than the test case says. */
+int
+tytest_fuzz_header(void)
+{
+ static const char body[] = "ABCDEFGHIJKLMNOP";
+ unsigned char data[TY_FUZZ_HEADER_SIZE + sizeof(body) - 1];
+ const char *stream;
+ int len;
+
+ /* feed_input() feeds, and feeding needs the shared Termpty */
+ tytest_common_init();
+
+ memcpy(data + TY_FUZZ_HEADER_SIZE, body, sizeof(body) - 1);
+
+ /* The seeds' default header, 0x07 0x00 0x00: 4096-byte reads, no
+ * repeat. The stream is the body as it stands. */
+ data[0] = 0x07;
+ data[1] = 0x00;
+ data[2] = 0x00;
+ tytest_common_feed_input(data, sizeof(data));
+ stream = tytest_common_stream_get(&len);
+ assert(len == (int)sizeof(body) - 1);
+ assert(stream && memcmp(stream, body, sizeof(body) - 1) == 0);
+
+ /* Byte 2 selects where the repeated span starts: 255 puts it on the
+ * last byte, and byte 1 = 2 feeds that byte 2^2 = 4 times. */
+ data[0] = 0x07;
+ data[1] = 0x02;
+ data[2] = 0xff;
+ tytest_common_feed_input(data, sizeof(data));
+ stream = tytest_common_stream_get(&len);
+ assert(len == 19);
+ assert(stream && memcmp(stream, "ABCDEFGHIJKLMNO" "PPPP", 19) == 0);
+
+ /* Byte 1 is an exponent: 2^20 additions of the 16-byte span overflow
+ * the 4 MiB stream cap, so the stream is truncated at exactly that. */
+ data[0] = 0x07;
+ data[1] = 20;
+ data[2] = 0x00;
+ tytest_common_feed_input(data, sizeof(data));
+ stream = tytest_common_stream_get(&len);
+ assert(len == TY_FUZZ_STREAM_MAX);
+ assert(stream && memcmp(stream, body, sizeof(body) - 1) == 0);
+
+ tytest_common_shutdown();
+ return 0;
+}
+
/* A reset state is a fresh state: feed sequences that allocate (title,
* hyperlink, synchronized-update snapshot, OSC 52, backlog rows), reset,
* and check the checksum equals the one right after a fresh
diff --git a/src/bin/tytest_common.c b/src/bin/tytest_common.c
index bf8edf7d..02c793b3 100644
--- a/src/bin/tytest_common.c
+++ b/src/bin/tytest_common.c
@@ -821,6 +821,93 @@ tytest_common_set_chunk(int chunk)
if (chunk > 0) _read_chunk = chunk;
}
+/* Test case format for the fuzzers: a 3-byte header, then the body.
+ *
+ * byte 0, low nibble: how the body is split into reads, an index into
+ * _chunk_sizes. 4096 takes several entries because it is what Terminology
+ * really reads; index 12 is "the whole body in one read", which is what a
+ * 64-read burst approximates. High nibble: reserved for a SIMD path
+ * switch.
+ * byte 1: repeat exponent k: the span from byte 2's offset to the end of
+ * the body is fed 2^(k mod 21) times. A small test case, ESC ] 0 ; once
+ * then A a million times, thereby expands into the megabytes the
+ * buffering paths need, sizes mutators do not grow inputs to by
+ * themselves.
+ * byte 2: start of the span, as byte * body_len / 256.
+ *
+ * The header is three plain bytes, so CmpLog and the mutators reach every
+ * value. Seeds from the escape-codes runs carry 0x07 0x00 0x00: 4096-byte
+ * reads, no repeat. The persistent and one-shot fuzzers share the format,
+ * so a crash file replays through the non-persistent build as-is. */
+
+static const int _chunk_sizes[16] = {
+ 1, 2, 3, 7, 64, 1024, 4095, 4096, 4097, 8192, 65536, 262144,
+ 0 /* whole body in one read */, 4096, 4096, 4096
+};
+
+/* The expanded stream. Grown once and kept across persistent iterations:
+ * expanding into a fresh allocation per input would put the allocator back
+ * on the fuzzer's critical path. */
+static char *_stream = NULL;
+static int _stream_len = 0;
+
+/* Expand data (header + body) into the _stream buffer. */
+static void
+_stream_expand(const unsigned char *data, int len)
+{
+ const char *body = (const char *)data + TY_FUZZ_HEADER_SIZE;
+ int body_len = len - TY_FUZZ_HEADER_SIZE;
+ int start = ((int)data[2] * body_len) / 256;
+ int span_len = body_len - start;
+ int repeat = 1 << (data[1] % 21);
+ int stream_len, i, room;
+
+ if (_stream == NULL)
+ {
+ _stream = malloc(TY_FUZZ_STREAM_MAX + 1);
+ assert(_stream);
+ }
+
+ memcpy(_stream, body, start);
+ stream_len = start;
+ for (i = 0; (i < repeat) && (stream_len < TY_FUZZ_STREAM_MAX); i++)
+ {
+ int add = span_len;
+
+ room = TY_FUZZ_STREAM_MAX - stream_len;
+ if (add > room) add = room;
+ memcpy(_stream + stream_len, body + start, add);
+ stream_len += add;
+ }
+ _stream_len = stream_len;
+}
+
+/* Feed one fuzzing test case: expand the header'd body, then hand it to the
+ * parser in the reads the header asks for. */
+void
+tytest_common_feed_input(const unsigned char *data, int len)
+{
+ int chunk;
+
+ if (len <= TY_FUZZ_HEADER_SIZE) return;
+
+ _stream_expand(data, len);
+ if (_stream_len <= 0) return;
+
+ chunk = _chunk_sizes[data[0] & 0x0f];
+ tytest_common_feed_chunked(_stream, _stream_len, chunk);
+}
+
+#if defined(BINARY_TYTEST)
+/* The expanded stream, for the fuzz-header unit test. */
+const char *
+tytest_common_stream_get(int *len)
+{
+ if (len) *len = _stream_len;
+ return _stream;
+}
+#endif
+
/* Feed len bytes in chunks of chunk bytes, as if read() kept returning that
* size. chunk <= 0 hands everything over in one call, which is what a burst
* of up to 64 reads in one wakeup approximates. */
@@ -962,6 +1049,8 @@ tytest_common_shutdown(void)
_termpty_release(&_ty);
free(_feed_buf);
_feed_buf = NULL;
+ free(_stream);
+ _stream = NULL;
free(_feed_codepoint);
_feed_codepoint = NULL;
_feed_size = 0;
diff --git a/src/bin/tytest_common.h b/src/bin/tytest_common.h
index c7486627..28b9aafa 100644
--- a/src/bin/tytest_common.h
+++ b/src/bin/tytest_common.h
@@ -9,9 +9,15 @@
#define TY_OFFSET_Y 1
#define TY_BACKSIZE 50
+/* The fuzzers' test case format: a 3-byte header expands into the stream
+ * that is fed, capped at TY_FUZZ_STREAM_MAX bytes. See tytest_common.c. */
+#define TY_FUZZ_HEADER_SIZE 3
+#define TY_FUZZ_STREAM_MAX (4 * 1024 * 1024)
+
void tytest_common_main_loop(void);
void tytest_common_feed(const char *data, int datalen);
void tytest_common_feed_chunked(const char *data, int len, int chunk);
+void tytest_common_feed_input(const unsigned char *data, int len);
void tytest_common_reset(void);
void tytest_common_set_chunk(int chunk);
void tytest_common_init(void);
@@ -22,6 +28,7 @@ const char *tytest_cursor_shape_get(void);
void tytest_notifications_clear(void);
const char *tytest_notifications_get(void);
Termpty *tytest_termpty_get(void);
+const char *tytest_common_stream_get(int *len);
#endif
#endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index b8aed87e..3c8a6e44 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -85,6 +85,7 @@ int tytest_xterm_parse_color(void);
int tytest_palette_slots(void);
int tytest_truecolor_cache_generation(void);
int tytest_reset(void);
+int tytest_fuzz_header(void);
#endif
int tytest_keyin_legacy(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.