236 lines
8.6 KiB
C
236 lines
8.6 KiB
C
// Page-cache write/truncate coherency regression test.
|
|
// Verifies cached pages stay coherent after file write and cached tails are dropped after truncate.
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <ntlite/syscall.h>
|
|
#include <ntlite/path.h>
|
|
|
|
#define TEST_SIZE 8192u
|
|
#define TRUNC_SIZE 1024u
|
|
|
|
static uint8_t gOriginal[TEST_SIZE];
|
|
static uint8_t gReplacement[4096];
|
|
static uint8_t gReadback[TEST_SIZE];
|
|
|
|
static void print(const char* s) {
|
|
SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) };
|
|
SysWriteConsole(&req);
|
|
}
|
|
|
|
static void print_u64(uint64_t v) {
|
|
char buf[32];
|
|
int i = 0;
|
|
if (v == 0) { print("0"); return; }
|
|
while (v && i < (int)sizeof(buf)) {
|
|
buf[i++] = (char)('0' + (v % 10));
|
|
v /= 10;
|
|
}
|
|
while (i--) {
|
|
char c[2] = { buf[i], 0 };
|
|
print(c);
|
|
}
|
|
}
|
|
|
|
static void exit_now(int code) {
|
|
while (true) SysExitProcess(code);
|
|
}
|
|
|
|
static void fail(const char* msg) {
|
|
print("pcache_invalidate: ");
|
|
print(msg);
|
|
print("\n");
|
|
exit_now(1);
|
|
}
|
|
|
|
static void fail_status(const char* what, KSTATUS status) {
|
|
print("pcache_invalidate: ");
|
|
print(what);
|
|
print(" failed status=");
|
|
print_u64(status);
|
|
print("\n");
|
|
exit_now(1);
|
|
}
|
|
|
|
static uint32_t query_cache(SYS_FILE_CACHE_STATS* out) {
|
|
memset(out, 0, sizeof(*out));
|
|
return SysQueryKernelStats(SYS_KERNEL_STATS_FILE_CACHE, out, sizeof(*out), 0);
|
|
}
|
|
|
|
static void delete_path(const char* path) {
|
|
(void)NtlDeleteFile(SysSvFromCstr(path));
|
|
}
|
|
|
|
static KSTATUS create_temp(uint32_t* fileOut, const char** pathOut) {
|
|
static const char* paths[] = {
|
|
"pcache_invalidate.tmp",
|
|
};
|
|
|
|
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
|
|
uint32_t h = 0;
|
|
delete_path(paths[i]);
|
|
KSTATUS s = NtlOpenFile(SysSvFromCstr(paths[i]),
|
|
SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_WRITE | SYS_FILE_ACCESS_QUERY,
|
|
SYS_FILE_CREATE_NEW,
|
|
SYS_FILE_FLAG_NONE,
|
|
&h);
|
|
if (s == NTLSTATUS_SUCCESS) {
|
|
*fileOut = h;
|
|
if (pathOut) *pathOut = paths[i];
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|
|
}
|
|
|
|
return NTLSTATUS_NOT_FOUND;
|
|
}
|
|
|
|
static void fill_patterns(void) {
|
|
for (size_t i = 0; i < TEST_SIZE; ++i)
|
|
gOriginal[i] = (uint8_t)(0x30u + (uint8_t)(i * 17u + (i >> 7)));
|
|
for (size_t i = 0; i < sizeof(gReplacement); ++i)
|
|
gReplacement[i] = (uint8_t)(0xA0u + (uint8_t)(i * 29u + (i >> 5)));
|
|
}
|
|
|
|
static void checked_write(uint32_t file, const void* buffer, size_t size, const char* what) {
|
|
size_t done = 0;
|
|
SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(file, buffer, size, &done);
|
|
KSTATUS s = SysWriteFile(&req);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status(what, s);
|
|
if (done != size) fail("short write");
|
|
}
|
|
|
|
static void checked_read(uint32_t file, void* buffer, size_t size, size_t* gotOut, const char* what) {
|
|
size_t got = 0;
|
|
memset(buffer, 0xCC, size);
|
|
SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(file, buffer, size, &got);
|
|
KSTATUS s = SysReadFile(&req);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status(what, s);
|
|
if (gotOut) *gotOut = got;
|
|
}
|
|
|
|
int main(void) {
|
|
print("pcache_invalidate: start\n");
|
|
fill_patterns();
|
|
|
|
uint32_t file = 0;
|
|
const char* path = 0;
|
|
uint32_t s = create_temp(&file, &path);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("create temp", s);
|
|
|
|
print("pcache_invalidate: using ");
|
|
print(path ? path : "<unknown>");
|
|
print("\n");
|
|
|
|
checked_write(file, gOriginal, sizeof(gOriginal), "write original");
|
|
|
|
s = SysSeekFile(file, 0);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("seek fill", s);
|
|
|
|
SYS_FILE_CACHE_STATS before;
|
|
SYS_FILE_CACHE_STATS afterFill;
|
|
SYS_FILE_CACHE_STATS afterSecond;
|
|
SYS_FILE_CACHE_STATS afterWrite;
|
|
SYS_FILE_CACHE_STATS afterRewriteRead;
|
|
SYS_FILE_CACHE_STATS afterTruncate;
|
|
if (query_cache(&before) != NTLSTATUS_SUCCESS) fail("initial stats query failed");
|
|
|
|
size_t got = 0;
|
|
checked_read(file, gReadback, sizeof(gReadback), &got, "first read");
|
|
if (got != sizeof(gOriginal)) fail("first read size mismatch");
|
|
if (memcmp(gReadback, gOriginal, sizeof(gOriginal)) != 0) fail("first read data mismatch");
|
|
|
|
if (query_cache(&afterFill) != NTLSTATUS_SUCCESS) fail("fill stats query failed");
|
|
if (afterFill.Loads <= before.Loads && afterFill.Hits <= before.Hits)
|
|
fail("initial read did not touch cache");
|
|
|
|
s = SysSeekFile(file, 0);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("seek second", s);
|
|
checked_read(file, gReadback, sizeof(gReadback), &got, "second read");
|
|
if (got != sizeof(gOriginal)) fail("second read size mismatch");
|
|
if (memcmp(gReadback, gOriginal, sizeof(gOriginal)) != 0) fail("second read data mismatch");
|
|
|
|
if (query_cache(&afterSecond) != NTLSTATUS_SUCCESS) fail("second stats query failed");
|
|
// Do not require this second read to hit: the cache is intentionally
|
|
// bounded and pcache_pressure.exe may have left it at the soft limit, so
|
|
// the newly-populated temp pages are eligible for immediate pressure
|
|
// eviction. This test is about write/truncate freshness; mmap/read reuse
|
|
// is covered by pcache_mmap_read_reuse.exe.
|
|
|
|
s = SysSeekFile(file, 0);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("seek rewrite", s);
|
|
checked_write(file, gReplacement, sizeof(gReplacement), "rewrite first page");
|
|
|
|
if (query_cache(&afterWrite) != NTLSTATUS_SUCCESS) fail("post-write stats query failed");
|
|
if (afterWrite.DirtyMarks <= afterSecond.DirtyMarks) fail("rewrite did not dirty cached page");
|
|
|
|
uint32_t mapping = 0;
|
|
SYS_CREATE_FILE_MAPPING_REQUEST createMapping = {
|
|
sizeof(SYS_CREATE_FILE_MAPPING_REQUEST), file, SYS_VM_READ,
|
|
SYS_MAP_FLAG_NONE, sizeof(gReplacement), &mapping
|
|
};
|
|
s = SysCreateFileMapping(&createMapping);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("create mapping after rewrite", s);
|
|
|
|
void* mappedBase = 0;
|
|
SYS_MAP_FILE_REQUEST mapFile = {
|
|
sizeof(SYS_MAP_FILE_REQUEST), mapping, SYS_MAP_ACCESS_READ,
|
|
SYS_MAP_FLAG_NONE, 0, sizeof(gReplacement), &mappedBase
|
|
};
|
|
s = SysMapFile(&mapFile);
|
|
SysCloseHandle(mapping);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("map after rewrite", s);
|
|
if (!mappedBase) fail("map after rewrite returned null");
|
|
|
|
volatile uint8_t* mappedView = (volatile uint8_t*)mappedBase;
|
|
(void)mappedView[0];
|
|
(void)mappedView[sizeof(gReplacement) - 1];
|
|
if (memcmp((const void*)mappedBase, gReplacement, sizeof(gReplacement)) != 0) {
|
|
SysFreeVm(mappedBase);
|
|
fail("stale mmap data after write invalidation");
|
|
}
|
|
s = SysFreeVm(mappedBase);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("unmap after rewrite", s);
|
|
|
|
s = SysSeekFile(file, 0);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("seek verify rewrite", s);
|
|
checked_read(file, gReadback, sizeof(gReadback), &got, "read after rewrite");
|
|
if (got != sizeof(gOriginal)) fail("rewrite read size mismatch");
|
|
if (memcmp(gReadback, gReplacement, sizeof(gReplacement)) != 0) fail("stale data after write invalidation");
|
|
if (memcmp(gReadback + sizeof(gReplacement), gOriginal + sizeof(gReplacement),
|
|
sizeof(gOriginal) - sizeof(gReplacement)) != 0) fail("unwritten page changed after rewrite");
|
|
|
|
if (query_cache(&afterRewriteRead) != NTLSTATUS_SUCCESS) fail("rewrite-read stats query failed");
|
|
|
|
s = SysTruncateFile(file, TRUNC_SIZE);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("truncate", s);
|
|
uint64_t truncatedSize = 0;
|
|
s = SysGetFileSize(file, &truncatedSize);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("get truncated size", s);
|
|
if (truncatedSize != TRUNC_SIZE) fail("truncate size mismatch");
|
|
|
|
if (query_cache(&afterTruncate) != NTLSTATUS_SUCCESS) fail("truncate stats query failed");
|
|
if (afterTruncate.Invalidations <= afterRewriteRead.Invalidations) fail("truncate did not invalidate cached tail");
|
|
|
|
s = SysSeekFile(file, 0);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("seek truncated", s);
|
|
checked_read(file, gReadback, sizeof(gReadback), &got, "read truncated");
|
|
if (got != TRUNC_SIZE) fail("truncated read length mismatch");
|
|
if (memcmp(gReadback, gReplacement, TRUNC_SIZE) != 0) fail("truncated data mismatch");
|
|
|
|
SysCloseHandle(file);
|
|
delete_path(path);
|
|
|
|
print("pcache_invalidate: invalidations+=");
|
|
print_u64(afterTruncate.Invalidations - before.Invalidations);
|
|
print(" loads+=");
|
|
print_u64(afterTruncate.Loads - before.Loads);
|
|
print(" hits+=");
|
|
print_u64(afterTruncate.Hits - before.Hits);
|
|
print("\n");
|
|
|
|
print("pcache_invalidate: PASS\n");
|
|
exit_now(0);
|
|
return 0;
|
|
}
|