Files
nt-lite/code/user/tests/async_cancel/async_cancel.c
T

152 lines
4.4 KiB
C

// User-mode regression test for async_cancel.
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ntlite/syscall.h>
#define EVENT_AUTO_RESET 0u
// Prints the requested operation.
static void print(const char* s) {
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) };
SysWriteConsole(&req);
}
// Opens the first available executable payload for async I/O.
// Tests are usually launched from /apps, so a bare "system/init.exe"
// resolves as /apps/system/init.exe and fails. Prefer the test binary itself,
// then fall back to absolute/drive-qualified paths.
static uint32_t open_test_file(uint32_t* handleOut, const char** pathOut) {
static const char* paths[] = {
"async_cancel.exe",
"/apps/async_cancel.exe",
"1:/apps/async_cancel.exe",
"0:/apps/async_cancel.exe",
"/system/init.exe",
"0:/system/init.exe",
"1:/system/init.exe",
"system/init.exe",
};
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
uint32_t h = 0;
SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(paths[i]), &h };
uint32_t s = SysOpenFile(&req);
if (s == SYS_STATUS_SUCCESS) {
*handleOut = h;
if (pathOut)
*pathOut = paths[i];
return SYS_STATUS_SUCCESS;
}
}
return SYS_STATUS_NOT_FOUND;
}
// Prints u64.
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 = buf[i];
do { SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(&c, 1) }; SysWriteConsole(&req); } while (0);
}
}
// Runs the requested operation.
int main(void) {
print("async_cancel: start\n");
uint32_t file = 0;
uint32_t event = 0;
uint32_t request = 0;
const char* openedPath = 0;
uint32_t s = open_test_file(&file, &openedPath);
if (s != SYS_STATUS_SUCCESS) {
print("async_cancel: open failed status="); print_u64(s); print("\n");
SysExitProcess(1);
}
print("async_cancel: using ");
print(openedPath ? openedPath : "<unknown>");
print("\n");
s = SysCreateEvent(EVENT_AUTO_RESET, 0, &event);
if (s != SYS_STATUS_SUCCESS) {
print("async_cancel: event create failed status="); print_u64(s); print("\n");
SysExitProcess(1);
}
uint8_t* buffer = (uint8_t*)malloc(512);
if (!buffer) {
print("async_cancel: alloc failed\n");
SysExitProcess(1);
}
memset(buffer, 0, 512);
SYS_IO_STATUS_BLOCK iosb;
iosb.Status = 0;
iosb.Information = 0;
SYS_ASYNC_FILE_REQUEST req;
memset(&req, 0, sizeof(req));
req.FileHandle = file;
req.EventHandle = event;
req.Offset = 0;
req.Buffer = buffer;
req.Size = 512;
req.Iosb = &iosb;
req.RequestHandleOut = &request;
s = SysReadFileAsync(&req);
if (s != SYS_STATUS_PENDING || request == 0) {
print("async_cancel: submit failed status="); print_u64(s);
print(" request="); print_u64(request); print("\n");
SysExitProcess(1);
}
uint32_t cs = SysCancelIo(request);
print("async_cancel: cancel returned "); print_u64(cs); print("\n");
while (iosb.Status == SYS_STATUS_PENDING) {
s = SysWait(event, SYS_WAIT_FLAG_ALERTABLE);
print("async_cancel: wait returned "); print_u64(s); print("\n");
if (s != SYS_STATUS_SUCCESS && s != SYS_STATUS_KERNEL_APC && s != SYS_STATUS_USER_APC && s != SYS_STATUS_ALERTED)
break;
}
print("async_cancel: iosb status="); print_u64(iosb.Status);
print(" info="); print_u64(iosb.Information); print("\n");
// If the worker had already started, cancellation is best-effort and the
// request may complete successfully. If it was still queued, it must report
// SYS_STATUS_CANCELLED. Both outcomes are valid; SYS_STATUS_PENDING is not.
uint32_t ok = (iosb.Status == SYS_STATUS_CANCELLED) ||
(iosb.Status == SYS_STATUS_SUCCESS && iosb.Information == 512);
SysCloseHandle(request);
SysCloseHandle(event);
SysCloseHandle(file);
free(buffer);
if (!ok) {
print("async_cancel: FAIL\n");
SysExitProcess(1);
}
print("async_cancel: PASS\n");
SysExitProcess(0);
return 0;
}