XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
// Host-only native contract tests compile the production Extras and Stdio files.
// Device substitutions below exist solely in this executable; none are shipped.
#include "../../Source/XFESystem.NativeRuntime/Native/SystemNativeContracts.h"
#include "../../Source/XFESystem.NativeRuntime/NativePal.h"

extern "C" __declspec(dllimport) void __stdcall ExitProcess(uint32_t);
extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(uint32_t);
extern "C" __declspec(dllimport) char* __stdcall GetCommandLineA();
extern "C" __declspec(dllimport) int __stdcall WriteFile(void*,const void*,uint32_t,uint32_t*,void*);
extern "C" int _fltused = 0;
static int test_errno, failures, checks;
static uint64_t written, filetime, sleeps;
static bool oversized_write;
static uint8_t memory[8192];
static size_t memory_used;
extern "C" int* __errno_location() { return &test_errno; }
extern "C" void* malloc(size_t count) {
    size_t aligned = (count + 15) & ~size_t(15);
    if(aligned < count || aligned > sizeof(memory) - memory_used) return nullptr;
    void* result = memory + memory_used; memory_used += aligned; return result;
}
extern "C" size_t strlen(const char* value) { size_t count = 0; while(value[count]) count++; return count; }
extern "C" void* memset(void* target, int value, size_t count) {
    auto* bytes = (uint8_t*)target; for(size_t i = 0; i < count; i++) bytes[i] = (uint8_t)value; return target;
}
extern "C" void* memcpy(void* target, const void* source, size_t count) {
    auto* bytes = (uint8_t*)target; auto* input = (const uint8_t*)source;
    for(size_t i = 0; i < count; i++) bytes[i] = input[i]; return target;
}
extern "C" void xfe_runtime_write(const char*, uint64_t count) {
    if(count > 1048576) oversized_write = true; written += count;
}
extern "C" uint64_t xfe_runtime_utc_filetime() { return filetime; }
extern "C" void xfe_runtime_halt(const char* reason) {
    uint32_t count; WriteFile(GetStdHandle((uint32_t)-11),reason,(uint32_t)strlen(reason),&count,nullptr);
    WriteFile(GetStdHandle((uint32_t)-11),"\r\n",2,&count,nullptr); ExitProcess(254);
}
static void sleep_test(uint32_t count) { sleeps += count; }
extern "C" const XfeRuntimePal* XfeRuntimeGetPal() {
    static XfeRuntimePal runtime = {}; runtime.sleep_milliseconds = sleep_test; return &runtime;
}
extern "C" int snprintf(char*,size_t,const char*,...);
extern "C" int xfe_stdio_selftest();
static bool same(const char* a,const char* b) {
    if(!a || !b) return a == b; while(*a && *a == *b) { a++; b++; } return *a == *b;
}
static void check(bool success,const char* label) {
    checks++;
    if(!success) {
        failures++; uint32_t count;
        WriteFile(GetStdHandle((uint32_t)-11),label,(uint32_t)strlen(label),&count,nullptr);
        WriteFile(GetStdHandle((uint32_t)-11),"\r\n",2,&count,nullptr);
    }
}
extern "C" void main() {
    const char* command=GetCommandLineA();
    for(const char* position=command; *position; position++) {
        if(same(position,"--rtc-unavailable")) { filetime=0; SystemNative_GetSystemTimeAsTicks(); ExitProcess(253); }
        if(same(position,"--rtc-overflow")) {
            filetime=116444736000000000ULL + uint64_t(INT64_MAX) + 1;
            SystemNative_GetSystemTimeAsTicks(); ExitProcess(253);
        }
    }
    check(SystemNative_ConvertErrorPlatformToPal(38)==Error_ENOSYS,"errno ENOSYS mapping");
    check(SystemNative_ConvertErrorPalToPlatform(Error_EBADF)==9,"PAL EBADF mapping");
    check(SystemNative_ConvertErrorPalToPlatform(Error_EOPNOTSUPP)==95,"PAL errno aliases");
    check(SystemNative_ConvertErrorPlatformToPal(123456)==Error_ENONSTANDARD,"unknown platform error");
    check(SystemNative_ConvertErrorPalToPlatform(0x70000)==-1,"unknown PAL error cannot be success");
    for(int code=0;code<=140;code++) {
        int pal=SystemNative_ConvertErrorPlatformToPal(code);
        if(pal!=Error_ENONSTANDARD) check(SystemNative_ConvertErrorPalToPlatform(pal)==code,"errno roundtrip");
    }
    test_errno=77;
    check(SystemNative_ConvertErrorPalToPlatform(Error_EHOSTNOTFOUND)==-0x20001 && test_errno==77,"lookup and errno preservation");
    char text[64];
    check(same(SystemNative_StrErrorR(38,text,sizeof(text)),"Function not implemented"),"real unsupported error text");
    char small[4];
    check(SystemNative_StrErrorR(9,small,sizeof(small))==nullptr && same(small,"Bad"),"error message truncation");
    check(same(SystemNative_StrErrorR(9,nullptr,0),"Bad file descriptor"),"borrowed error message");
    intptr_t output=SystemNative_Dup(1), other=SystemNative_Dup(output);
    check(output>=3 && other>output,"distinct output aliases");
    check(SystemNative_Close(output)==0 && SystemNative_Write(output,"x",1)==-1 && test_errno==9,"closed descriptor rejection");
    check(SystemNative_Write(other,"abc",3)==3 && written==3,"dup retains actual write endpoint");
    static char large[1048577];
    check(SystemNative_Write(other,large,sizeof(large))==(int32_t)sizeof(large) && !oversized_write && written==1048580,"HAL write bound and no truncation");
    check(SystemNative_Write(other,nullptr,0)==0,"zero write with null buffer");
    check(SystemNative_Write(other,nullptr,1)==-1 && test_errno==14,"invalid write buffer");
    check(SystemNative_Write(0,"x",1)==-1 && test_errno==9,"stdin not writable");
    check(SystemNative_Read(0,text,1)==-1 && test_errno==38,"missing stdin must not pretend EOF");
    check(SystemNative_Read(0,nullptr,0)==0,"zero stdin read");
    FileStatus status;
    check(SystemNative_FStat(other,&status)==0 && status.Mode==0x2000 && status.Size==0,"real character endpoint status");
    check(SystemNative_FStat(999,&status)==-1 && test_errno==9 && status.Mode==0,"invalid status is cleared");
    check(SystemNative_Open("/missing",0,0)==-1 && test_errno==2,"absent native file");
    check(SystemNative_Open("/new",0x20|1,0)==-1 && test_errno==38,"native storage create unsupported");
    check(SystemNative_Stat("/",&status)==-1 && test_errno==38,"logical root is not fake filesystem");
    DirectoryEntry entry;
    test_errno=77;
    check(SystemNative_ReadDir(nullptr,&entry)==9 && test_errno==77 && entry.Name==nullptr,"ReadDir positive errno contract");
    check(SystemNative_PRead(other,text,1,0)==-1 && test_errno==29,"serial seek rejected");
    check(SystemNative_FLock(other,8)==-1 && test_errno==38,"valid unlock flag is unsupported, not malformed");
    check(SystemNative_FLock(other,0)==-1 && test_errno==22,"invalid lock flag");
    test_errno=77;
    check(SystemNative_PosixFAdvise(other,0,1,0)==38 && test_errno==77,"advice raw errno without mutation");
    PollEvent poll[3]={{(int32_t)other,4,0},{999,1,0},{-1,1,99}}; uint32_t triggered=999;
    check(SystemNative_Poll(poll,3,0,&triggered)==Error_SUCCESS && triggered==2 && poll[0].TriggeredEvents==4 && poll[1].TriggeredEvents==32 && poll[2].TriggeredEvents==0,"poll actual output and invalid descriptors");
    poll[0]={0,1,0};
    check(SystemNative_Poll(poll,1,0,&triggered)==Error_ENOSYS && triggered==0,"poll unsupported stdin is PAL ENOSYS");
    check(SystemNative_Poll(nullptr,0,7,&triggered)==Error_SUCCESS && triggered==0 && sleeps==7,"empty poll actual scheduler timeout");
    check(SystemNative_Poll(nullptr,0,0,nullptr)==Error_EFAULT,"poll null result ABI");
    WinSize window={1,2,3,4};
    check(SystemNative_IsATty(other)==0 && test_errno==25,"serial endpoint not a POSIX tty");
    check(SystemNative_GetWindowSize(other,&window)==-1 && test_errno==25 && window.Row==0,"no fake terminal geometry");
    check(SystemNative_InitializeTerminalAndSignalHandling()==0 && test_errno==38,"terminal failure boolean");
    check(same(SystemNative_GetCwd(text,sizeof(text)),"/"),"logical root path");
    check(SystemNative_GetCwd(text,1)==nullptr && test_errno==34,"cwd insufficient buffer");
    check(same(SystemNative_GetCwd(nullptr,0),"/"),"allocated cwd contract");
    check(same(SystemNative_GetProcessPath(),"/System/Kernel.elf"),"allocated actual application path");
    Passwd pwd; test_errno=77;
    check(SystemNative_GetPwUidR(0,&pwd,text,sizeof(text))==38 && test_errno==77 && pwd.Name==nullptr,"account lookup returns positive errno");
    check(SystemNative_GetEUid()==UINT32_MAX && test_errno==38,"no fake root account");
    check(SystemNative_InterfaceNameToIndex(text)==0 && test_errno==38,"no fake network index");
    check(SystemNative_LoadLibrary("anything")==nullptr && test_errno==38,"dynamic loading rejected");
    check(same((char*)SystemNative_GetLoadLibraryError(),"Dynamic loading is unavailable in the RAM-only native guest"),"load error borrowed string");
    filetime=116444736000000000ULL - 123456789;
    check(SystemNative_GetSystemTimeAsTicks()==-123456789,"RTC pre-1970 negative Unix ticks");
    filetime=116444736000000000ULL + 123456789;
    check(SystemNative_GetSystemTimeAsTicks()==123456789,"RTC Unix 100ns epoch conversion");
    check(SystemNative_SNPrintF_1S(text,sizeof(text),"hello %s","native")==12 && same(text,"hello native"),"production string formatter");
    check(SystemNative_SNPrintF_1I(small,sizeof(small),"%d",12345)==5 && same(small,"123"),"production integer formatter truncation");
    check(xfe_stdio_selftest()==1,"production stdio own regressions");
    check(SystemNative_Close(other)==0 && SystemNative_Close(other)==-1 && test_errno==9,"duplicate close rejected");
    char result[96]; uint32_t count;
    int length=snprintf(result,sizeof(result),"SystemNativeExtras checks=%d failures=%d\r\n",checks,failures);
    WriteFile(GetStdHandle((uint32_t)-11),result,(uint32_t)length,&count,nullptr);
    ExitProcess(failures ? 1 : 0);
}