225 lines
7.5 KiB
C
225 lines
7.5 KiB
C
// Shared writable mmap regression test.
|
|
// Verifies that writable mapped-file views dirty the coherent file cache and flush through the normal file path.
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <ntlite/syscall.h>
|
|
|
|
#define PAGE_SIZE_BYTES 4096u
|
|
#define TEST_SIZE (PAGE_SIZE_BYTES * 3u)
|
|
|
|
static uint8_t gInitial[TEST_SIZE];
|
|
static uint8_t gExpected[TEST_SIZE];
|
|
static uint8_t gReadback[TEST_SIZE];
|
|
|
|
static void print(const char* s) {
|
|
SYS_WRITE_CONSOLE_REQUEST req = { 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) {
|
|
for (;;) SysExitProcess(code);
|
|
}
|
|
|
|
static void fail(const char* msg) {
|
|
print("shared_mmap: ");
|
|
print(msg);
|
|
print("\n");
|
|
exit_now(1);
|
|
}
|
|
|
|
static void fail_status(const char* what, uint32_t status) {
|
|
print("shared_mmap: ");
|
|
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) {
|
|
SYS_PATH_REQUEST req = { SysSvFromCstr(path) };
|
|
(void)SysDeleteFile(&req);
|
|
}
|
|
|
|
static uint32_t create_temp(uint32_t* fileOut, const char** pathOut) {
|
|
static const char* paths[] = {
|
|
"shared_mmap.tmp",
|
|
"/shared_mmap.tmp",
|
|
"1:/shared_mmap.tmp",
|
|
"0:/shared_mmap.tmp",
|
|
};
|
|
|
|
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
|
|
uint32_t h = 0;
|
|
delete_path(paths[i]);
|
|
SYS_OPEN_FILE_REQUEST req = SysMakeOpenFileRequest(SysSvFromCstr(paths[i]),
|
|
SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_WRITE | SYS_FILE_ACCESS_QUERY,
|
|
SYS_FILE_CREATE_NEW,
|
|
SYS_FILE_FLAG_NONE,
|
|
&h);
|
|
uint32_t s = SysOpenFile(&req);
|
|
if (s == NTLSTATUS_SUCCESS) {
|
|
*fileOut = h;
|
|
if (pathOut) *pathOut = paths[i];
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|
|
}
|
|
return NTLSTATUS_NOT_FOUND;
|
|
}
|
|
|
|
static uint32_t open_path(const char* path, uint32_t* fileOut) {
|
|
uint32_t h = 0;
|
|
SYS_OPEN_FILE_REQUEST req = SysMakeOpenFileRequest(SysSvFromCstr(path),
|
|
SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_WRITE | SYS_FILE_ACCESS_QUERY,
|
|
SYS_FILE_OPEN_EXISTING,
|
|
SYS_FILE_FLAG_NONE,
|
|
&h);
|
|
uint32_t s = SysOpenFile(&req);
|
|
if (s == NTLSTATUS_SUCCESS)
|
|
*fileOut = h;
|
|
return s;
|
|
}
|
|
|
|
static void checked_seek(uint32_t file, uint64_t offset, const char* what) {
|
|
uint32_t s = SysSeekFile(file, offset);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status(what, s);
|
|
}
|
|
|
|
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);
|
|
uint32_t 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);
|
|
uint32_t s = SysReadFile(&req);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status(what, s);
|
|
if (gotOut) *gotOut = got;
|
|
}
|
|
|
|
static void fill_initial(void) {
|
|
for (size_t i = 0; i < TEST_SIZE; ++i) {
|
|
gInitial[i] = (uint8_t)(0x20u + (uint8_t)(i * 7u + (i >> 5)));
|
|
gExpected[i] = gInitial[i];
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
print("shared_mmap: start\n");
|
|
fill_initial();
|
|
|
|
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("shared_mmap: creating temporary file ");
|
|
print(path ? path : "<unknown>");
|
|
print("\n");
|
|
|
|
checked_write(file, gInitial, TEST_SIZE, "initial write");
|
|
s = SysFlushFile(file);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("initial flush", s);
|
|
|
|
uint32_t mapping = 0;
|
|
s = SysCreateFileMapping(file, SYS_VM_READWRITE, SYS_MAP_FLAG_NONE, TEST_SIZE, &mapping);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("create mapping", s);
|
|
|
|
uint8_t* view = 0;
|
|
s = SysMapFile(mapping, 0, TEST_SIZE, SYS_MAP_ACCESS_READ | SYS_MAP_ACCESS_WRITE,
|
|
SYS_MAP_FLAG_NONE, (void**)&view);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("map shared write", s);
|
|
|
|
for (size_t i = 128; i < 128 + 3000; ++i) {
|
|
view[i] = (uint8_t)(0xA0u + (uint8_t)i);
|
|
gExpected[i] = view[i];
|
|
}
|
|
for (size_t i = PAGE_SIZE_BYTES + 17; i < PAGE_SIZE_BYTES + 17 + 2048; ++i) {
|
|
view[i] = (uint8_t)(0x50u + (uint8_t)(i >> 1));
|
|
gExpected[i] = view[i];
|
|
}
|
|
|
|
SYS_FILE_CACHE_STATS beforeFlush;
|
|
SYS_FILE_CACHE_STATS afterFlush;
|
|
if (query_cache(&beforeFlush) != NTLSTATUS_SUCCESS) fail("pre-flush stats failed");
|
|
|
|
s = SysFlushFile(file);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("flush mapped writes", s);
|
|
|
|
if (query_cache(&afterFlush) != NTLSTATUS_SUCCESS) fail("post-flush stats failed");
|
|
if (afterFlush.MappedDirtyPages <= beforeFlush.MappedDirtyPages)
|
|
fail("mapped dirty pages were not harvested");
|
|
if (afterFlush.WritebackBytes <= beforeFlush.WritebackBytes)
|
|
fail("mapped writes did not write back");
|
|
|
|
checked_seek(file, 0, "seek after mapped flush");
|
|
size_t got = 0;
|
|
checked_read(file, gReadback, TEST_SIZE, &got, "read after mapped flush");
|
|
if (got != TEST_SIZE) fail("read after mapped flush size mismatch");
|
|
if (memcmp(gReadback, gExpected, TEST_SIZE) != 0) fail("mapped flush data mismatch");
|
|
|
|
for (size_t i = PAGE_SIZE_BYTES * 2 + 3; i < PAGE_SIZE_BYTES * 2 + 900; ++i) {
|
|
view[i] = (uint8_t)(0xC0u ^ (uint8_t)i);
|
|
gExpected[i] = view[i];
|
|
}
|
|
|
|
s = SysFreeVm(view);
|
|
view = 0;
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("unmap", s);
|
|
|
|
s = SysCloseHandle(mapping);
|
|
mapping = 0;
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("close mapping", s);
|
|
|
|
s = SysCloseHandle(file);
|
|
file = 0;
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("close file", s);
|
|
|
|
s = open_path(path, &file);
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("reopen", s);
|
|
|
|
checked_read(file, gReadback, TEST_SIZE, &got, "read after close");
|
|
if (got != TEST_SIZE) fail("read after close size mismatch");
|
|
if (memcmp(gReadback, gExpected, TEST_SIZE) != 0) fail("unmap/close data mismatch");
|
|
|
|
s = SysCloseHandle(file);
|
|
file = 0;
|
|
if (s != NTLSTATUS_SUCCESS) fail_status("close reopened", s);
|
|
delete_path(path);
|
|
|
|
print("shared_mmap: mapped-dirty-pages+=");
|
|
print_u64(afterFlush.MappedDirtyPages - beforeFlush.MappedDirtyPages);
|
|
print(" writeback-bytes+=");
|
|
print_u64(afterFlush.WritebackBytes - beforeFlush.WritebackBytes);
|
|
print("\n");
|
|
print("shared_mmap: PASS\n");
|
|
exit_now(0);
|
|
return 0;
|
|
}
|