# Typed runtime service registry.
#
# Data only. `cargo run -p xenora-import-gen --bin xenora-runtime-gen` validates
# it and writes crates/loader/src/runtime_table.rs (typed Win64 adapters and
# static metadata) and docs/runtime/services.md (coverage counts).
#
#   service <library> <symbol>(<name>: <type>, ...) -> <type>
#       handler <Rust function in crates/loader/src/services.rs or crt.rs>
#       status  implemented | partial | stub
#       alias   <documented API set that forwards this symbol>
#       fails   <failure contract>
#       ref     <public header or document>
#       test    <test function in crates/loader/tests/runtime_services.rs>
#
# Types: u32 i32 u64 bool32 handle ptr out<N> f32 f64, and void as a result.
# A bool32 argument is true when nonzero. out<N> is a guest pointer to N bytes
# the service writes. Arguments are assigned by position (Microsoft x64 calling
# convention): RCX, RDX, R8, R9 or XMM0-XMM3 for floats, then the stack.
#
# "status" describes behaviour against the documented contract. A service needs
# at least one test before it may be implemented or partial.

service kernel32.dll SetProcessPriorityBoost(process: handle, disable: bool32) -> bool32
    handler set_process_priority_boost
    status partial
    alias api-ms-win-core-processthreads-l1-1-2.dll
    fails 0 with ERROR_INVALID_HANDLE
    ref processthreadsapi.h SetProcessPriorityBoost
    test priority_boost_round_trips_and_checks_its_handle

service kernel32.dll GetProcessPriorityBoost(process: handle, disabled: out<4>) -> bool32
    handler get_process_priority_boost
    status partial
    alias api-ms-win-core-processthreads-l1-1-2.dll
    fails 0 with ERROR_INVALID_HANDLE or ERROR_NOACCESS
    ref processthreadsapi.h GetProcessPriorityBoost
    test priority_boost_round_trips_and_checks_its_handle

service kernel32.dll GetCurrentProcess() -> handle
    handler get_current_process
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentProcess
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetProcessId(process: handle) -> u32
    handler get_process_id
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails 0 with ERROR_INVALID_HANDLE
    ref processthreadsapi.h GetProcessId
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetCurrentThread() -> handle
    handler get_current_thread
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentThread
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetCurrentProcessId() -> u32
    handler get_current_process_id
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentProcessId
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetCurrentThreadId() -> u32
    handler get_current_thread_id
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentThreadId
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll OpenThread(access: u32, inherit: bool32, thread: u32) -> handle
    handler open_thread
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails 0 with ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED or ERROR_NOT_ENOUGH_MEMORY
    ref processthreadsapi.h OpenThread
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll GetThreadPriority(thread: handle) -> i32
    handler get_thread_priority
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails THREAD_PRIORITY_ERROR_RETURN with ERROR_INVALID_HANDLE or ERROR_ACCESS_DENIED
    ref processthreadsapi.h GetThreadPriority
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll SetThreadPriority(thread: handle, priority: i32) -> bool32
    handler set_thread_priority
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails 0 with ERROR_INVALID_HANDLE, ERROR_ACCESS_DENIED, ERROR_NOT_SUPPORTED or ERROR_INVALID_PARAMETER
    ref processthreadsapi.h SetThreadPriority
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll CloseHandle(object: handle) -> bool32
    handler close_handle
    status partial
    alias api-ms-win-core-handle-l1-1-0.dll
    fails 0 with ERROR_INVALID_HANDLE
    ref handleapi.h CloseHandle
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll GetSystemTimeAsFileTime(time: out<8>) -> void
    handler get_system_time_as_file_time
    status implemented
    alias api-ms-win-core-sysinfo-l1-1-0.dll
    fails guest fault on an invalid pointer
    ref sysinfoapi.h GetSystemTimeAsFileTime
    test system_time_comes_from_the_injected_clock

# Standard streams and formatted output for a hosted C program. Output to
# stdout and stderr is captured; any other stream, input, floating-point and
# wide conversions stop the guest with the runtime-limit fault.

service ucrtbase.dll __acrt_iob_func(index: u32) -> ptr
    handler standard_stream
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails stops the guest for an index above 2 or when the streams cannot be allocated
    ref corecrt_wstdio.h __acrt_iob_func
    test standard_streams_are_three_stable_file_objects

service ucrtbase.dll __stdio_common_vfprintf(options: u64, stream: ptr, format: ptr, locale: ptr, arguments: ptr) -> i32
    handler vfprintf
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails -1 with errno EINVAL for a null stream or format or an invalid format; stops the guest for what is not modelled
    ref corecrt_stdio_config.h __stdio_common_vfprintf; Microsoft Learn, vfprintf and format specification syntax
    test formatted_output_reaches_the_captured_standard_streams

service ucrtbase.dll fwrite(buffer: ptr, size: u64, count: u64, stream: ptr) -> u64
    handler fwrite
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails 0 with errno EINVAL for a null buffer or stream; stops the guest for a stream other than stdout or stderr
    ref stdio.h fwrite
    test fwrite_and_fputs_write_to_the_captured_standard_streams

service ucrtbase.dll fputs(text: ptr, stream: ptr) -> i32
    handler fputs
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails EOF with errno EINVAL for a null string or stream; stops the guest for a stream other than stdout or stderr
    ref stdio.h fputs
    test fwrite_and_fputs_write_to_the_captured_standard_streams

service ucrtbase.dll fflush(stream: ptr) -> i32
    handler fflush
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails stops the guest for a stream that is not a standard stream
    ref stdio.h fflush
    test fflush_and_setvbuf_return_their_documented_results

service ucrtbase.dll setvbuf(stream: ptr, buffer: ptr, mode: i32, size: u64) -> i32
    handler setvbuf
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails -1 with errno EINVAL for a null stream, an unknown mode or a size outside 2 to INT_MAX; stops the guest for a stream that is not a standard stream
    ref stdio.h setvbuf
    test fflush_and_setvbuf_return_their_documented_results

service kernel32.dll GetLastError() -> u32
    handler get_last_error
    status partial
    alias api-ms-win-core-errorhandling-l1-1-0.dll
    fails never
    ref errhandlingapi.h GetLastError
    test last_error_is_set_by_failures_and_by_the_guest

service kernel32.dll SetLastError(code: u32) -> void
    handler set_last_error
    status partial
    alias api-ms-win-core-errorhandling-l1-1-0.dll
    fails never
    ref errhandlingapi.h SetLastError
    test last_error_is_set_by_failures_and_by_the_guest

# What a hosted C program's startup code hands to `main`. The three pointer
# functions and the two initialization functions are listed by Microsoft only
# by name; crates/loader/src/services/crt_arguments.rs says what is documented,
# inferred and Xenora's choice.

service ucrtbase.dll __p___argc() -> ptr
    handler argc_pointer
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails stops the guest when the variable cannot be allocated
    ref Microsoft Learn, C runtime library reference, __argc, __argv, __wargv
    test program_arguments_reach_the_guest_as_argc_and_argv

service ucrtbase.dll __p___argv() -> ptr
    handler argv_pointer
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails stops the guest when the variable cannot be allocated
    ref Microsoft Learn, C runtime library reference, __argc, __argv, __wargv
    test program_arguments_reach_the_guest_as_argc_and_argv

service ucrtbase.dll __p__environ() -> ptr
    handler environ_pointer
    status partial
    alias api-ms-win-crt-environment-l1-1-0.dll
    fails stops the guest when the variable cannot be allocated
    ref Microsoft Learn, C runtime library reference, _environ, _wenviron
    test the_environment_table_is_empty_and_ends_with_a_null_pointer

service ucrtbase.dll _configure_narrow_argv(mode: i32) -> i32
    handler configure_narrow_argv
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails EINVAL for a mode other than 0, 1 or 2; stops the guest when the table cannot be allocated
    ref corecrt_startup.h _configure_narrow_argv
    test program_arguments_reach_the_guest_as_argc_and_argv

service ucrtbase.dll _initialize_narrow_environment() -> i32
    handler initialize_narrow_environment
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails stops the guest when the table cannot be allocated
    ref corecrt_startup.h _initialize_narrow_environment
    test the_environment_table_is_empty_and_ends_with_a_null_pointer

service kernel32.dll VirtualAlloc(address: ptr, size: u64, kind: u32, protect: u32) -> ptr
    handler virtual_alloc
    status partial
    alias api-ms-win-core-memory-l1-1-0.dll
    fails 0 with ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED, ERROR_INVALID_ADDRESS or ERROR_NOT_ENOUGH_MEMORY
    ref memoryapi.h VirtualAlloc
    test virtual_memory_is_mapped_released_and_bounded

service kernel32.dll VirtualFree(address: ptr, size: u64, kind: u32) -> bool32
    handler virtual_free
    status partial
    alias api-ms-win-core-memory-l1-1-0.dll
    fails 0 with ERROR_NOT_SUPPORTED, ERROR_INVALID_PARAMETER or ERROR_INVALID_ADDRESS
    ref memoryapi.h VirtualFree
    test virtual_memory_is_mapped_released_and_bounded

# Stubs. A program built with the mingw-w64 runtime imports these three and
# does not call them on a normal run. They exist so that its imports bind. A
# call stops the guest with the runtime-limit fault and changes nothing.

service kernel32.dll VirtualProtect(address: ptr, size: u64, protect: u32, old: out<4>) -> bool32
    handler virtual_protect
    status stub
    alias api-ms-win-core-memory-l1-1-0.dll
    fails always: stops the guest with the runtime-limit fault; no protection changes
    ref memoryapi.h VirtualProtect
    test memory_and_exception_stubs_stop_the_guest_and_change_nothing

service kernel32.dll VirtualQuery(address: ptr, information: out<48>, length: u64) -> u64
    handler virtual_query
    status stub
    alias api-ms-win-core-memory-l1-1-0.dll
    fails always: stops the guest with the runtime-limit fault; nothing is written
    ref memoryapi.h VirtualQuery
    test memory_and_exception_stubs_stop_the_guest_and_change_nothing

service ucrtbase.dll __C_specific_handler(record: ptr, frame: ptr, context: ptr, dispatcher: ptr) -> i32
    handler c_specific_handler
    status stub
    alias api-ms-win-crt-private-l1-1-0.dll
    fails always: stops the guest with the runtime-limit fault; no exception is dispatched
    ref Microsoft Learn, x64 exception handling, language-specific handler
    test memory_and_exception_stubs_stop_the_guest_and_change_nothing

service ucrtbase.dll sqrt(x: f64) -> f64
    handler sqrt
    status partial
    alias api-ms-win-crt-math-l1-1-0.dll
    fails NaN for a negative argument; errno is not set
    ref math.h sqrt (ISO C, IEC 60559 squareRoot)
    test floating_arguments_and_results_use_xmm_registers

service ucrtbase.dll sqrtf(x: f32) -> f32
    handler sqrtf
    status partial
    alias api-ms-win-crt-math-l1-1-0.dll
    fails NaN for a negative argument; errno is not set
    ref math.h sqrtf (ISO C, IEC 60559 squareRoot)
    test floating_arguments_and_results_use_xmm_registers

service ucrtbase.dll fabs(x: f64) -> f64
    handler fabs
    status implemented
    alias api-ms-win-crt-math-l1-1-0.dll
    fails never
    ref math.h fabs (ISO C, IEC 60559 abs)
    test floating_arguments_and_results_use_xmm_registers

service ucrtbase.dll fabsf(x: f32) -> f32
    handler fabsf
    status implemented
    alias api-ms-win-crt-math-l1-1-0.dll
    fails never
    ref math.h fabsf (ISO C, IEC 60559 abs)
    test floating_arguments_and_results_use_xmm_registers

# What a C program's startup code configures before `main`. Each value is
# recorded and returned as documented; nothing in the runtime acts on it.

service ucrtbase.dll _set_app_type(kind: i32) -> void
    handler set_app_type
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails never
    ref corecrt_startup.h _set_app_type
    test crt_app_type_and_math_error_handler_are_recorded

service ucrtbase.dll _set_new_mode(mode: i32) -> i32
    handler set_new_mode
    status partial
    alias api-ms-win-crt-heap-l1-1-0.dll
    fails -1 with errno EINVAL for a mode other than 0 or 1
    ref new.h _set_new_mode
    test crt_startup_modes_return_the_previous_setting

service ucrtbase.dll _configthreadlocale(request: i32) -> i32
    handler configure_thread_locale
    status partial
    alias api-ms-win-crt-locale-l1-1-0.dll
    fails -1 with errno EINVAL for a request other than 0, 1 or 2
    ref locale.h _configthreadlocale
    test crt_startup_modes_return_the_previous_setting

service ucrtbase.dll __setusermatherr(handler: ptr) -> void
    handler set_user_math_error
    status partial
    alias api-ms-win-crt-math-l1-1-0.dll
    fails never
    ref corecrt_startup.h __setusermatherr
    test crt_app_type_and_math_error_handler_are_recorded

service ucrtbase.dll _set_invalid_parameter_handler(handler: ptr) -> ptr
    handler set_invalid_parameter_handler
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails never
    ref stdlib.h _set_invalid_parameter_handler
    test crt_startup_handlers_return_the_previous_handler

service ucrtbase.dll __p__fmode() -> ptr
    handler file_mode_pointer
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails stops the guest when the variable cannot be allocated
    ref Microsoft Learn, C runtime library reference, __p__fmode
    test crt_file_mode_variables_are_guest_memory

service ucrtbase.dll __p__commode() -> ptr
    handler commit_mode_pointer
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails stops the guest when the variable cannot be allocated
    ref Microsoft Learn, C runtime library reference, __p__commode
    test crt_file_mode_variables_are_guest_memory

service ucrtbase.dll signal(signal: i32, handler: ptr) -> ptr
    handler signal
    status partial
    alias api-ms-win-crt-runtime-l1-1-0.dll
    fails SIG_ERR with errno EINVAL for a signal the documentation does not list
    ref signal.h signal
    test crt_startup_handlers_return_the_previous_handler

service kernel32.dll SetUnhandledExceptionFilter(filter: ptr) -> ptr
    handler set_unhandled_exception_filter
    status partial
    alias api-ms-win-core-errorhandling-l1-1-0.dll
    fails never
    ref errhandlingapi.h SetUnhandledExceptionFilter
    test crt_startup_handlers_return_the_previous_handler
