From 095e72eac9a857d610e7bed9df7685b9bc80f94d Mon Sep 17 00:00:00 2001 From: FHaggs Date: Thu, 2 Oct 2025 16:11:15 +0200 Subject: [PATCH] Add short write to pwrite in faulty_libc. --- testing/unreliable-libc/file.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/testing/unreliable-libc/file.c b/testing/unreliable-libc/file.c index 9a580b513..ab6d934f7 100644 --- a/testing/unreliable-libc/file.c +++ b/testing/unreliable-libc/file.c @@ -13,6 +13,8 @@ static double probabilities[] = { [EIO] = 0.01, }; +static double short_write_probability = 0.05; // 5% chance of a short write + static bool chance(double probability) { double event = drand48(); @@ -37,6 +39,13 @@ ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) if (libc_pwrite == NULL) { libc_pwrite = dlsym(RTLD_NEXT, "pwrite"); } + + if (count > 1 && chance(short_write_probability)) { + size_t short_count = 1 + (lrand48() % (count - 1)); + printf("%s: injecting fault SHORT WRITE (requesting %zu instead of %zu)\n", __func__, short_count, count); + return libc_pwrite(fd, buf, short_count, offset); + } + if (inject_fault(ENOSPC)) { printf("%s: injecting fault NOSPC\n", __func__); return -1;