Shared Source CLI Platform Adaptation Layer Specification

Contents

Overview

This document and the C header file (%ROTOR_DIR%\pal\rotor_pal.h) along with the relevant portions of the Microsoft® MSDN on-line documentation (msdn.microsoft.com), forms the specification for the Shared Source CLI (Rotor) Platform Adaptation Layer (PAL). The PAL has been specified such that correct implementation will enable the portable subset of the .Net Frameworks to be run on a variety of non-Microsoft® Windows® operating systems.

Note: The PAL alone is not sufficient to accomplish a port of the SSCLI implementation - the recompilation of the portable subset along with the construction of an appropriate just-in-time (JIT) compiler are required as well.

The document consists of the following sections:


Host Machine and Operating System Requirements

This section describes the features that the underlying platform is expected to support and programmatically expose to the PAL.

Host Machine

Host Operating System

Compilation Environment


Basic Requirements


Assumptions

In constructing this specification, the following assumptions were used:

PAL-specific Entrypoints

PAL_Initialize

This is to be called by all clients of the PAL, to allow the PAL to perform any required initialization. It returns 0 on success and a nonzero errno value on failure. In the failure case, the PAL should display a meaningful error message if possible, since the host application will be unable to call any PAL APIs to print messages. The PAL must support the exit() and ExitProcess() functions even if PAL_Initialize returns a failure code. This routine may be called multiple times if multiple PAL clients are loaded into the same process. The PAL should keep an internal count of the number of PAL_Initialize calls.

The caller must pass its argument list (argc/argv), in order to support GetCommandLine on systems which do not otherwise have a system call to fetch back the command-line arguments. Calls after the first should ignore argc/argv.

On return, the floating-point environment must be set up to be compatible with the default Microsoft Win32® environment:

All new threads created via CreateThread() must also have the floating-point environment initialized to these settings.

PAL_Terminate

PAL clients must call this if they have previously called PAL_Initialize and it has succeeded. After this call, if the count of outstanding PAL clients drops to 0, then the only PAL APIs that must continue to function are exit() and ExitProcess().

PAL_GetUserConfigurationDirectoryW

The PAL should return back the directory that the host should use to store per-user configuration data in. On Windows, this should be located under the per-user directory returned from SHGetSpecialFolderPath() for CSIDL_PROFILE (that is, the %userprofile% directory). On Unix systems, it should be under the user's home directory, and should be prefixed by "." so it is hidden. To support multiple different Rotor implementations running concurrently, the user configuration directory should be unique per-PAL, as determined by examining the full path to the PAL .dll/.so file. See Side-by-side support for more information.

The PAL must create the directory upon the first call to this API.

PAL_RegisterLibrary PAL_UnregisterLibrary

Called to register/unregister statically linked libraries. See DllMain chapter for more details.

PAL_GetPALDirectoryW

Returns the fully qualified directory name where the PAL DLL was loaded from, including a trailing path separator. On failure, it returns FALSE and sets the LastError value to a meaningful error code. For success, if the PAL was loaded from, for example, "D:\rotor\v1.x86chk.rotor\rotor_pal.dll", the returned path should be "D:\rotor\v1.x86chk.rotor\". This API will be used to form path names that point back to the directory that Rotor's binaries are installed in.

PAL_Random

Source of a good randomness implementation. A correct implementation gathers randomness from multiple non deterministic sources like interrupt and network latencies. The lpBuffer is filled with dwLen random bytes. Optionally, the caller can populate the buffer with data to use as an auxiliary random seed before calling the function. On Windows, this function should be implemented using CryptGenRandom. On Unix platforms, this function should be implemented by reading from /dev/random or /dev/urandom.

PAL_get_stdout PAL_get_stdin PAL_get_stderr

These functions are called from the PAL-defined "stdout", "stdin", and "stderr" macros.

PAL_va_start

Implements the C runtime va_start macro.

PAL_errno

Returns a pointer to the per-thread errno value

PAL_LocalHandleToRemote

Converts a HANDLE valid in the current process into a "cookie" which can be passed to another PAL-based process on the same machine, and converted into a HANDLE valid in that process. Conversion from remote RHANDLE to local HANDLE is accomplished by calling PAL_RemoteHandleToLocal.

The return value is INVALID_HANDLE_VALUE on failure, due to out-of-memory or invalid HANDLE value or type.

See DuplicateHandle for more details on local and remote HANDLEs.

PAL_RemoteHandleToLocal

Creates a local HANDLE that corresponds to a HANDLE in a remote process, where the remote HANDLE has been marshalled via PAL_LocalHandleToRemote.

The return value is INVALID_HANDLE_VALUE on failure, due to out-of-memory, etc. The results are undefined if the RHANDLE was not created via PAL_LocalHandleToRemote().


Win32 Entrypoints

User32.dll

CharNextA

CharNextExA

wsprintfW

The format string must support %x, %s, %d, and other C runtime standard printf formatters, plus %ws.

wsprintfA

MessageBoxW

The implementation of MessageBox should not show any UI to the user. Instead, it should take the text and any other relevant process/thread information and log it to the host operating system's event logging mechanism.

The return value will be:

Kernel32.dll

File I/O

CreateFileA CreateFileW

The filename will never be of the form \\.\*. This excludes named pipes, mailslots, disk devices, and tape devices. The file name will be either a fully qualified pathname (such as "c:\windows\myfile.txt" on Win32 or "/etc/rotor.conf" on Unix-style systems), or a partially qualified pathname (such as "myfile.txt" or "windows\myfile.txt").

CreateFile does not need to support filenames starting with "\\.\". Therefore, named pipes, mailslots, disk devices, and tape devices are not supported, since all of those names start with "\\.\". Files and directories should be supported, though not with the Windows-specific "\\.\C:" format. Communications resources and consoles are not supported via the Windows NT names such as CON:, COM1:, CONIN$, and so on. CreateFile should only support opening names within the filesystem - files, directories, and on Unix-like systems, devices mounted in the filesystem, such as "/dev/tty". CreateFile does not need to map the built-in DOS device names to underlying operating system device names.

The rationale for not mapping the DOS device names to the Unix names is that for many standard DOS device names the devices require additional APIs for configuring the device, and these APIs are not exposed via the PAL API.  For example, COMx: support in CreateFile doesn't make sense since there is no way for the code calling the PAL CreateFile to configure the baud rate, control flow, and other parameters.

dwShareMode & FILE_SHARE_DELETE. If passed in subsequent calls to CreateFile don't require DELETE access to be specified for the open to succeed.

FILE_FLAG_BACKUP_SEMANTICS is specified when the caller wishes to open a directory as a file. The directory HANDLE is used only in GetFileTime/SetFileTime calls, so the caller will request write access. However, Unix systems allow directories to be opened only for read, though the directory's lastaccess and lastwrite time may be altered via that file descriptor, so CreateFileA may choose to ignore the dwDesiredAccess value if FILE_FLAG_BACKUP_SEMANTICS is specified.

AreFileApisANSI

CopyFileA CopyFileW

DeleteFileA DeleteFileW

MoveFileW (MoveFileA not required)

MoveFileExW (MoveFileExA not required)

CreateDirectoryA CreateDirectoryW

RemoveDirectoryW (RemoveDirectoryA not required)

FindFirstFileA FindFirstFileW FindNextFileW FindNextFileA FindClose

Note: The HANDLE return type on Win32 is not allocated from the same pool as other HANDLEs like events and files. It can never be passed to CloseHandle() - only to FindNextFileA/W() and FindClose().

GetFileAttributesA GetFileAttributesW

To support the a CLI implementation, the return value must support FILE_ATTRIBUTE_DIRECTORY, -1 (for file-not-found), and FILE_ATTRIBUTE_READONLY. To support System.IO.File.GetAttributes, it needs to implement as much functionality as feasible.

GetFileAttributesExW (GetFileAttributesExA not required)

SetFileAttributesA SetFileAttributesW

To correctly support System.IO.File.SetAttributes in a CLI implementation, it needs to implement as much functionality as feasible. Attributes which cannot be supported may be silently ignored by the PAL, such as FILE_ATTRIBUTE_HIDDEN on Unix.

WriteFile

GetStdHandle

The handles returned from GetStdHandle may be passed to CreateProcess in the STARTUPINFO structure.

SetEndOfFile

SetFilePointer

ReadFile

GetFileSize

CompareFileTime

SetFileTime

GetFileTime

FlushFileBuffers

FileTimeToLocalFileTime

FileTimeToDosDateTime

Note that the range of allowable dates may differ from the range available on Win32. Implementations must allow dates up to year 2037.

DosDateTimeToFileTime

GetSystemTimeAsFileTime

LocalFileTimeToFileTime

GetFileType

The return value must be FILE_TYPE_DISK for HANDLEs that represent files. It is really used to determine if the HANDLE supports seek operations or not.

GetConsoleCP

GetConsoleOutputCP

GetFullPathNameW GetFullPathNameA

GetLongPathNameW

On filesystems which don't support long vs. short pathnames, or if the short path does not exist, it should return 0 without altering the contents of lpszLongPath.

GetTempFileNameA GetTempFileNameW

GetTempPathA GetTempPathW

GetCurrentDirectoryA GetCurrentDirectoryW

SetCurrentDirectoryA SetCurrentDirectoryW

LockFile

UnlockFile

GetDiskFreeSpaceW

The Rotor managed assembly location implementation uses this API to round file sizes up to their actual size on-disk based on the BytesPerSector*SectorsPerCluster, which must equal the minimum allocation granularity for files stored under lpRootPathName. This value is known as the "cluster size" on Windows, and the "frag-size" on Unix.

SearchPathA SearchPathW

Threads and Processes

CreateSemaphoreA CreateSemaphoreW ReleaseSemaphore

CreateEventA CreateEventW

SetEvent

ResetEvent

OpenEventW (OpenEventA not required)

CreateMutexW (CreateMutexA not required)

ReleaseMutex

GetCurrentProcessId

CreateProcessA CreateProcessW

The CreateProcess implementation must probe the type of the binary pointed to by the lpCommandLine parameter. If it is an IL-only PE/COFF image, then CreateProcess must prepend the fully-qualified path to "clix " (found in the PAL_GetPALDirectoryW() directory) to the lpCommandLine parameter before launching the child process, so that the IL-only binary is launched within a new Rotor process. Otherwise, the lpCommandLine should be considered to be a native system executable and launched as such.

WaitForSingleObject

GetExitCodeProcess

The return code from the child process may be truncated on some platforms, to the size of that platform's process exit code data type. The PAL GetExitCodeProcess will sign-extend the native platform's process exit code data type up to 32 bits and return as a DWORD. For PAL APIs which exit the process and specify a return code, if the return value is outside of the valid range for the host platform, the PAL will truncate the return value to fit. In other words, platforms where the return value is a signed char, return values less than -128 will become 128, and return values greater than 127 will become 127.

DuplicateHandle

If hSourceProcessHandle is not the current process, then hSourceHandle is a local handle created via PAL_RemoteHandleToLocal. Similarly, if hTargetProcessHandle is not the current process, then lpTargetHandle should contain a HANDLE value suitable for passing to PAL_LocalHandleToRemote for remoting.

For cases where either source or destination is not the current process, DuplicateHandle() can be implemented as a simple copy of hSourceHandle to *lpTargetHandle, if PAL_LocalHandleToRemote() and PAL_RemoteHandleToLocal() are used to remote HANDLEs. Alternatively, if the DuplicateHandle() can perform cross-process HANDLE duplication, then PAL_LocalHandleToRemote() and PAL_RemoteHandleToLocal() can be implemented as no-ops, returning the HANDLE value unmodified.

GetCurrentProcess

GetCurrentThreadId

Sleep

SleepEx

CreateThread

See PAL_Initialize() for information on the initial floating-point environment for the newly-created thread.

Note that the lpThreadId pointer must be updated by the CreateThread() implementation before the newly-created thread is allowed to run. ie. if the lpThreadId points to a global variable, the new thread must be able to confirm that its GetCurrentThreadId() value matches the contents of the global.

SuspendThread

ResumeThread

QueueUserAPC

GetThreadContext

Must support CONTEXT_CONTROL and CONTEXT_INTEGER. It will only be called on suspended threads in the case of cordbg, and only on the current thread in the case of an in-process call within the common language runtime.

SetThreadContext

GetCurrentThread

GetThreadPriority

SetThreadPriority

WaitForMultipleObjectsEx

This API may be called with any handle type(s) which is supported in the PAL and is documented as waitable in the Win32 API documentation.

WaitForMultipleObjects

TerminateProcess

ExitThread

GetProcessTimes

TlsGetValue

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

TlsSetValue

TlsFree

TlsAlloc

The PAL must guarantee that at least 32 TLS slots are available to the calling application.

EnterCriticalSection TryEnterCriticalSection

These need to support recursion on enter.

LeaveCriticalSection

DeleteCriticalSection

InitializeCriticalSection

SetErrorMode

The PAL may implement this function as a no-op, ignoring the input parameter, and always returning 0.

ExitProcess

WriteProcessMemory

The PAL does not need to enforce memory protection: attempts to write to read-only pages in the destination process may either succeed by altering the page to be read/write, or may cause WriteProcessMemory to return a failure code.

OpenProcess

This API is used by cordbg's "attach" command, to attach the debugger to the specified process.

Memory Management

CreateFileMappingA CreateFileMappingW

OpenFileMappingA OpenFileMappingW

MapViewOfFile

UnmapViewOfFile

LoadLibraryA LoadLibraryW

These APIs will be used only to load and call unmanaged dynamic libraries. There is no requirement that they support loading of IL-only PE/COFF files. Like Win32 LoadLibrary, the PAL must first search the directory that the application was loaded from, but beyond that, it may search whatever directories a native shared-library loader would use, in whatever order it would search. It need not use the PATH environment variable.

FreeLibrary

Calls to FreeLibrary made after PAL_Terminate() or ExitProcess() must be ignored and treated as success regardless of the validity of the module handle. The application may call FreeLibrary() from within a DllMain() routine that is handling a DLL_PROCESS_DETACH message.

FreeLibraryAndExitThread

GetProcAddress

GetProcAddress must support binding to both functions and data by name. Binding by ordinal is not required, though some implementations may support it.

DisableThreadLibraryCalls

Calls to DisableThreadLibraryCalls made after PAL_Terminate() or ExitProcess() must be ignored and treated as success regardless of the validity of the module handle. The application may call DisableThreadLibraryCalls() from within a DllMain() routine that is handling a DLL_PROCESS_DETACH message.

GetModuleFileNameA GetModuleFileNameW

VirtualAlloc

VirtualFree

VirtualProtect

VirtualQuery

If the address to query isn't contained within an allocation created via VirtualAlloc, then VirtualQuery may return MEM_FREE, even if the address isn't available for use by VirtualAlloc.  If the memory is in use as a DLL, or heap or mapped file, VirtualQuery may return MEM_FREE rather than MEM_COMMIT. This is compatible with the usage patterns in clr\src\vm\gcsmp.cpp and clr\src\utilcode\util.cpp.

In the event of a return of MEM_FREE for the unknown region, the returned MEMORY_BASIC_INFORMATION.RegionSize should be zero.

ReadProcessMemory

GetProcessHeap

This can return any value - the return value is passed to HeapAlloc/HeapFree only as a cookie.

HeapAlloc

HeapReAlloc

HeapFree

LocalAlloc

LocalFree

GlobalMemoryStatus

The dwMemoryLoad and dwTotalPhys are the only field that must be returned. The caller will compare the load for >= 90 and >= 95 in order to make decisions about garbage collection.

FlushInstructionCache

RtlMoveMemory

CopyMemory

Locale Information

GetStringTypeExW

CompareStringW

GetLocaleInfoW

GetACP

GetCPInfo

IsValidCodePage

WideCharToMultiByte

MultiByteToWideChar

If passed in CP_UTF8 for the codepage and the input string contains an invalid UTF8 character, the character is ignored.

GetSystemDefaultLangID

GetUserDefaultLangID

SetThreadLocale

GetThreadLocale

GetUserDefaultLCID

GetTimeZoneInformation

IsValidLocale

GetConsoleOutputCP

If the console codepage isn't the same as GetACP(), the C# compiler tries to remap its output strings by first converting to Unicode, then into ANSI using the console's codepage.

IsDbcsLeadByteExW

GetCalendarInfoW

If this API fails by returning 0, the NLS code will fall back to a set of default values compatible with the common language runtime when running on Windows 95.

GetDateFormatW

This API will only be called only from within System.Globalization.DateTimeFormatInfo when the calendar is CAL_TAIWAN.

Miscellaneous

OutputDebugStringA OutputDebugStringW

If the PAL can determine whether a native debugger is present:

If the PAL cannot determine whether a native debugger is present:

DebugBreak

lstrcatW

lstrcpyW

lstrlenA lstrlenW

lstrcpynW

GetEnvironmentVariableA GetEnvironmentVariableW

SetEnvironmentVariableA SetEnvironmentVariableW

CloseHandle

RaiseException

GetTickCount

QueryPerformanceCounter

QueryPerformanceFrequency

SetUnhandledExceptionFilter

InterlockedExchange

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

InterlockedExchangePointer

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

InterlockedDecrement

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

InterlockedIncrement

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

InterlockedCompareExchange

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

InterlockedCompareExchangePointer

This API should not be logged, in order to avoid the performance hit: this API is called frequently.

IsBadReadPtr

IsBadWritePtr

IsBadCodePtr

GetSystemTime

FormatMessageW (FormatMessageA not required)

FORMAT_MESSAGE_FROM_SYSTEM may be implemented by dynamically loading the PAL runtime (PAL RT) and calling the satellite APIs (See Localized Resource Strings) to map the Win32 error codes to text. Because the PAL is built and run before the PAL RT has been built, the PAL may not statically link to the PAL RT and must be robust against failures to load it. Appendix A has information on how to generate a rotor_pal.satellite file containing the error-to-text mappings using a Windows operating system.

Note: Windows 2000 and Windows XP have different behavior for format strings that use the printf-style format specifier "lS". For example, "%3!lS!". Use of the "lS" specifier in the PAL FormatMessageW is disallowed; the implementation is PAL-specific.

GetLastError

The error codes supported by the PAL are listed in rotor_pal.h.

SetLastError

FreeEnvironmentStringsW (FreeEnvironmentStringsA not required)

GetEnvironmentStringsW (GetEnvironmentStrings not required)

GetCommandLineW (GetCommandLineA not required)

GetVersionExA GetVersionExW)

Only the dwMajorVersion, dwMinorVersion, and dwPlatformID need to be set. On Unix, dwPlatformId should be VER_PLATFORM_UNIX.

GetSystemInfo

SetConsoleCtrlHandler

Note that the PAL must notify the callback routine of CTRL+C events (or whatever key sequence signals a process to exit), but the PAL may not notify the callback routine of other events such as window-close or user logoff. The intent is to allow command-line tools to clean up any temporary output files in the event

GenerateConsoleCtrlEvent

Raise a CTRL+C or CTRL+Break event in the current process, used to test the SetConsoleCtrlHandler() implementation. The current process, or the current process and its sub-group may receive the event, depending on the PAL implementation.

CreatePipe

C Runtime

memcmp

memset

memcpy

memmove

strlen

wcstol

wcscpy

wcslen

wcsrchr

wcscat

wcsncmp

wcschr

_wcsnicmp

swprintf

malloc

free

_alloca

operator new

operator delete

qsort

_fdopen

_close

fclose

wcspbrk

sprintf

feof

ferror

fread

fwrite

_swab

_stricmp

fgets

fputs

_mbslen

_mbsinc

_mbsninc

_mbsdec

isspace

iswspace

isprint

vprintf

fprintf

fwprintf

vsprintf

_vsnprintf

_snprintf

vswprintf

_vsnwprintf

_snwprintf

sscanf

strstr

_strlwr

wcsstr

_wcslwr

_putw

fseek

ftell

_getw

iswdigit

isxdigit

iswxdigit

bsearch

_finite

_isnan

realloc

memchr

strcmp

strncmp

_strnicmp

strcat

strncat

strcpy

strncpy

strchr

strrchr

strpbrk

atoi

atol

tolower

toupper

towupper

wcscmp

wcsncat

wcsncpy

_itow

_i64tow

_ui64tow

iswupper

iswprint

towlower

fflush

isalpha

isalnum

isupper

islower

__iscsym

strtok

strspn

strtoul

wcstok

strcspn

getenv

_putenv

_rotl

_rotr

abs

log

log10

exp

pow

acos

asin

atan

atan2

cos

sin

tan

cosh

sinh

tanh

fmod

floor

fabs

getc

ungetc

_ecvt

modf

ctime

rand

srand

exit

The exit code may be truncated to a signed char - see GetExitCodeProcess for details.

strtod wcstod

If an overflow occurs, MSDN states that these routines return +/-HUGE_VAL. On the PAL, the value of HUGE_VAL is undefined. Thus on overflow strtod and wcstod must return a valid double-precision value, but the value does not need to be the same across different PAL implementations. There is no symbolic name "HUGE_VAL" in the PAL.

atof

The 'nnne+nnn' format will not be supported - calls must only use 'nnnd+nnn'.

_splitpath _wsplitpath _makepath _wmakepath

On host operating systems that don't support drive letters, the "drive" parameter must always be either NULL or a pointer to an empty string. By supporting "" as a drive string, _makepath can be used on the output of a previous _splitpath call.

_fullpath

swscanf

Scan string may be "%[^,],%[^,],%s" or "%u%n" or "%[^,],%[^,],%[^,],%c,%c"

time

localtime

mktime

_gcvt

_open_osfhandle

wcstoul

On overflow or underflow, it must return ULONG_MAX and set errno to ERANGE.

errno

The only value that must be supported is ERANGE, in the event that wcstoul() fails due to overflow. Other values of errno are allowable, but no constants are defined - the PAL is free to return just 0 and ERANGE, or 0, ERANGE, and some other nonzero values. In the PAL header file, errno is a macro which calls PAL_errno() to get the pointer to the per-thread errno value, then references that pointer.

fopen

_wfopen

Wsock32.dll, Ws2_32.dll

gethostbyname

gethostbyaddr

gethostname

inet_addr

getpeername

getsockopt

getsockopt must support the following level/optname combinations: The PAL may support more level and optname values, these are the minimum set. The PAL may return WSAEINVAL if the level is unknown, or WSAENOPROTOOPT if the option is unsupported by the protocol family.

setsockopt

setsockopt must support the following level/optname combinations: The PAL may support more level and optname values, these are the minimum set. The PAL may return WSAEINVAL if the level is unknown, or WSAENOPROTOOPT if the option is unsupported by the protocol family.

connect

If the 'namelen' parameter is invalid and the API fails, the exact failure code returned from GetLastError() is implementation-dependent.

send

recv

closesocket

accept

listen

bind

shutdown

sendto

If the socket is connectionless (ie. DGRAM), there are several implementation-specific options: If the socket is connection-oriented but not connected, the behavior is implementation-defined: PAL may either fail the call or may connect the socket.

The PAL need not validate the namelen parameter.

recvfrom

getsockname

select

socket

If multiple parameters are incorrect, precisely which error is reported is implementation-defined. Ie. different implementations may validate parameters in a different order, affecting which error code is returned.

WSAStartup

WSACleanup

WSASend

WSASendTo

WSARecv

WSARecvFrom

WSAEventSelect

WSASocketA

WSAIoctl

The PAL need only implement ioctls which reasonably map on to the host operating system's socket code. There are no functional requirements from within Rotor itself: the API is never called, except on behalf of a managed application calling System.Net.Sockets.IOControl.

WSAEnumNetworkEvents

ioctlsocket


Implementation Details

API Call Tracing

The PAL should include API tracing in the debug build. API trace logs will be an invaluable debugging tool, for understanding bugs in both the CLR and the PAL.

Tracing should be controlled by an environment variable which is read during PAL per-process initialization. If the variable, PAL_API_TRACING is set, it is either a file name to write to, or "stdout" or "stderr". If the name is "stdout" or "stderr" then logging should be performed to the C runtime stdout or stderr file. Otherwise, the PAL should open the file, truncate it if it exists, and write the trace information to that file.

Ideally, each API should log the following information:

Care must be taken to format the entire line to be logged into one buffer before being written to the log file, so concurrent API calls on different threads don't have logging interleaved together within a single line.

Example:

000000ba CopyFileW(lpExistingFileName=001259a0 L"foo.txt", lpNewFileName=00126762 L"bar.txt", bFailIfExists=0)
000000c0 FindClose(hFindFile=10a3cdb0)
000000c0 FindClose BOOL 1
000000ba CopyFileW BOOL 1

File and Path Names

On Unix-like filesystems, any PAL APIs operating on file/path names must be prepared to accept DOS/Win32-style pathnames and translate them into paths acceptable for the host filesystem. In particular: However, the PAL does not need to implement case insensitivity for compatibility with Win32. If the underlying filesystem is case-sensitive, then the PAL filesystem APIs should also be case-sensitive.

On Win32, CreateFile, fopen, and other file-open APIs all fail if the string parameter refers to a directory instead of a file.

On filesystems that support symbolic links, the PAL APIs must always open the destination of the link (ie. follow the link and operate on what the link references), rather than operate on the link itself.

PAL file I/O APIs do not need to support filenames starting with "\\.\" or names of DOS devices. Therefore, named pipes, mailslots, disk devices, and tape devices are not supported, since all of those names start with "\\.\", and names of DOS devices such as "lpt1", "aux", "com1" need not be supported. The PAL does not need to map the built-in DOS device names to underlying operating system device names.

The rationale for not mapping the DOS device names to the Unix names is that many standard DOS device names the devices require additional APIs for configuring the device, and these APIs are not exposed via the PAL API.  Thus COMx: support in CreateFile doesn't make sense since there is no way for the code calling the PAL CreateFile to configure the baud rate, control flow, and other parameters.

HANDLEs

The allocator used to manage HANDLE types should not need to be robust against mis-use such as double-close of a HANDLE or use of a HANDLE after it was closed.  However, it will likely be worthwhile to code some robustness into the free version and add some more expensive validation code into the checked and fastchecked versions.

 

On 64-bit systems, sizeof(HANDLE)==8, and consumers of HANDLEs will never truncate. On Microsoft Win64™, sizeof(HANDLE)==8, but the upper 4 bytes can be truncated, and then the 32-bit value can be sign-extended to recreate a HANDLE.

LastError

The PAL must accept following error codes as SetLastError values and may set the lasterror value to any of these codes as the result of an error encountered during execution of a PAL API.. The full list can be found in rotor_pal.h - search for "winerror.h codes" and "Socket error codes".

Printf Format Specifiers

All printf-style routines may share a single format specification list. The specification list should follow Ansi-C, with the following exceptions and notes:

Use of Resource After It Has Been Freed

If the calling application allocates a resource (HANDLE, socket, heap allocation, etc.), uses the resource, then frees it (via CloseHandle, close, free, and so on), the behavior of subsequent API calls is undefined, if the calling application continues to pass the now-freed resource handle. The PAL may crash, assert, terminate the process, corrupt memory, falsely report success, or choose any other kind of behavior. Where reasonable, the PAL should be robust against such errors in the calling application, but it is not a requirement.

Case Sensitivity in Environment Variables

Environment-variable APIs such as getenv, putenv, GetEnvironmentVariableA, etc. are case-insensitive on Win32. The PAL may implement them as case-sensitive if the host operating system's environment variables are case-sensitive.

Side-by-Side: Named Shared Objects and the User Configuration Directory

In order to support multiple different Rotor processes running side-by-side under the same user logon account, each PAL implementation must use its own isolated namespace for named shared objects. Named shared objects can be created via APIs such as CreateEventW(), CreateMutexW(), and CreateFileMappingA().

Isolation should be per-PAL, as determined by examining the full path to the PAL .dll/.so file. Therefore, if multiple processes use /usr/bin/librotor_pal.so, then those processes should share one namespace, while processes using ~/experimental/librotor_pal.so should share a separate namespace. One possible implementation is to hash the fully-qualifed pathname to the PAL .dll/.so into a short string which can then be merged with the shared object name.

The per-user configuration directory returned by PAL_GetUserConfigurationDirectoryW() should also be per-PAL.


General Issues

I/O Completion Ports and Async I/O

These do not map well onto Unix platforms. Async I/O has very limited support in general, and the APIs to use it are not standardized. For example, Posix 4 aio_* APIs exist on Linux, but each async file open creates a new thread, causing very poor performance if a large number of files are open. I/O completion ports and async I/O will not be exposed via the PAL.

The BCL exposes async I/O via the base Stream.BeginRead and BeginWrite methods, and FileStream derives from Stream. The implementation of FileStream's BeginRead and BeginWrite will automatically fall back to synchronous reads and writes if async is not available.

Out-of-Stack

On Win32, the common language runtime can use the guard page at the bottom of the stack to understand where the stack bounds are and avoid hitting the end of stack.

Exceptions

On non-Win32 platforms, Win32 structured exception handling (SEH) is not supported. Instead, the PAL implements a subset of SEH via a combination of compile-time macros and runtime help. The goal is to replace code like:
	... local variable declarations
	try {
	        ... code which references locals from above
	} except ( ExceptionFilter() ) {
	        ...  code which references locals from above            
	}
with:
	... local variable declarations
	PAL_TRY {
		... code which references locals from above
	} PAL_EXCEPT_FILTER(ExceptionFilter) {
	        ...  code which references locals from above   
	} PAL_ENDTRY
and cause no additional code expansion in the Win32 build.

SEH Macros

PAL_TRY

PAL_EXCEPT_FILTER(pfnFilter, pvParameter)

PAL_EXCEPT_FILTER_EX(LabelName, pfnFilter, pvParameter)

PAL_EXCEPT (disposition)

PAL_EXCEPT_EX(LabelName, disposition)

PAL_FINALLY

PAL_FINALLY_EX(LabelName)

PAL_LEAVE

PAL_LEAVE_EX(LabelName)

PAL_ENDTRY

On non-Win32, SEH is implemented via wrappers on top of setjmp/longjmp, so both SEH setup and exception dispatching will likely be larger and slower than x86 Win32 or even RISC Win32. Use sparingly.

SEH for Jitted Code

Every entry into jitted code is enclosed in a try/catch block. The handler associated with this try/catch block calls the appropriate jitted exception handlers in case exception is thrown, thus the jitted code itself does not know about the implementation details of exception handling (EH).

Unfortunately, the try/catch block around the jitted code cannot be the regular C/C++ SEH try block. It is the native EH record plugged directly into the fs:[0] chain in the Windows i386 version.

The main feature difference between the Win32 native EH and C/C++ SEH is in stack state at invocation of the body of catch or finally clauses during second pass:

It should be clear now why it is now enough for jitted code to be enclosed in the regular C/C++ SEH try block: it won't be possible to invoke the jitted second pass handlers because of the stack they depend on would be gone.

It is also not a good idea to invoke both first and second pass jitted handlers in the first pass of C/C++ SEH. The execution engine depends on the interop between the native and managed exceptions. The exception search often flies through multiple floors of jitted and execution engine code before the exception is caught and second pass starts. Invoking both first and second pass jitted handlers in the first pass would change the order in which are the jitted handlers executed.

Thus it is necessary to emulate the Win32 native EH in addition to C/C++ SEH in the PAL. The differences between Win32 native EH and the C/C++ SEH are:

SEH Implementation

The following routines must be implemented by PALs on non-Win32 platforms:

PAL_TryHelper

PAL_EndTryHelper

PAL_GetBottommostRegistration

PAL_SetBottommostRegistration

PAL_GetBottommostRegistrationPtr

Localized Resource Strings

Several components within the common language runtime use LoadLibrary then LoadString in order to fetch localizable text resources from satellite DLLs. Rather than duplicating this PE/COFF resource structure within a Unix shared-library, a better solution is to switch to a generic text-based system. This solution is implemented in the PAL runtime, on top of the PAL APIs, so it can be shared between all platforms. However, it is documented here, as it may be useful in the PAL's implementation of FormatMessage.

LoadLibrary/LoadLibraryEx calls used to load satellite DLLs will be replaced by:

        typedef HANDLE HSATELLITE;

        PALIMPORT
        HSATELLITE
        PALAPI
        PAL_LoadSatelliteResourceW(LPCWSTR SatelliteResourceFileName);
        #ifdef UNICODE
        #define LoadSatelliteResource LoadSatelliteResourceW
        #endif
LoadString calls to fetch strings from the satellite DLL will be replaced by:
	PALIMPORT
	int
	PALAPI
	PAL_LoadSatelliteStringW(HSATELLITE SatelliteResource,
		UINT uID,
		LPWSTR lpBuffer,
		int nBufferMax);
	PALIMPORT
	int
	PALAPI
	PAL_LoadSatelliteStringA(HSATELLITE SatelliteResource,
		UINT uID,
		LPSTR lpBuffer,
		int nBufferMax);
	#ifdef UNICODE
	#define PAL_LoadSatelliteResource PAL_LoadSatelliteResourceW
	#else
	#define PAL_LoadSatelliteResource PAL_LoadSatelliteResourceA
	#endif
FreeLibrary calls to release satellite DLLs will be replaced by:
	PALIMPORT
	BOOL
	PALAPI
	PAL_FreeSatelliteResource(HSATELLITE SatelliteResource);

The format of the satellite DLL will be a UTF-8 encoded file containing a list of pairs of decimal numbers and quoted string pairs, with a whitespace between. The maximum string length is 511 bytes, not including the required quotes.  For example, the following three messages are valid:

1 "This is a message"
2 "This is a message"
     39 "This is
another message"

But the following are not valid:

A "This is a message"
1 2

The LoadSatelliteString APIs will scan through the table by reading in each message number and quoted string pair, one at a time until the desired message number is found or EOF is hit. If an illegal message/string pair is found, then the APIs should immediately return failure. Note - checks for duplicated message IDs are not required; the API behavior is undefined.

The satellite message files will be generated via a tool, resourcecompiler.html, which functions like the Win32 Resource compiler, converting STRINGTABLE resources in the .rc file into the new satellite message file format. The conversion tool should check that no duplicated message ids are present, and either emit a file containing all legal ID/string pairs, or error out. Rotor includes an implementation of the new resource compiler tool which produces the satellite files. Note that resource IDs in both the source .rc file and as parameters to LoadSatelliteResource are both truncated to 16-bit values, for compatibility with Win32 resource IDs.

The satellite resource data will be reference-counted - multiple calls to LoadSatelliteResource() to load the same resource file name are permitted, and each call must have a matching FreeSatelliteResource() call. Satellite resource files are considered to be the same only if their fully qualified path names are identical. Note: The file or path name passed to LoadSatelliteResource() may be a partial path that must be fully qualified by the PAL implementation.

It would be great to re-use the managed resource format (System.Resources.ResourceManager), but this will add a significant bootstrapping dependency since so many unmanaged binaries depend on these for core functionality.

This routine will be implemented within the core URT tree, not within the PAL. It can easily be built on top of the PAL APIs.

Security

The PAL must avoid introducing security holes that can be exploited from managed code. In particular, the sequence of CreateFile/ReadFile/CreateFileMapping/MapViewOfFile must guarantee that if the file was opened with deny-write, that the file that was initially opened is exactly the same data is read from the file via both ReadFile and MapViewOfFile. In other words, the implementation of CreateFileMapping or MapViewOfFile cannot be implemented using an additional file-open API call using the file name, since that could potentially open a different file due to another process renaming the first file and creating a new file with the name of the first file.

The PAL must also avoid buffer overruns when dealing with strings such as file and directory names.

DLLMain

LoadLibrary will call DllMain if there is one on PROCESS_ATTACH / PROCESS_DETACH / THREAD_ATTACH / THREAD_DETACH. DisableThreadLibraryCalls will work as usual. The Win32 DllMain thread safety invariant (only one DllMain is called on one thread at a time) holds.

If DLL is statically linked to other PAL DLLs, it has to call PAL_RegisterLibrary for all DLLs it depends on in its PROCESS_ATTACH handler and PAL_UnregisterLibrary in its PROCESS_DETACH handler.

In the Unix PAL implementation, PAL_Register/PAL_Unregister calls are identical to LoadLibrary / FreeLibrary. In the Win32 PAL implementation, these calls are no-ops.

Calls to DllMain from LoadLibrary should be protected against cyclic references between DLLs.

This solution minimizes changes in the Win32 code. The only change necessary is a couple of calls to PAL_RegisterLibrary/PAL_UnregisterLibrary under #ifdef FEATURE_PAL. It has also a very simple implementation in the Win32 PAL.

Unexpected Termination

In some circumstances, the PAL may acquire and use resources shared between several cooperating processes. If the process running the PAL terminates or blocks unexpectedly due to an asynchronous event, and the PAL is currently executing code within a PAL API, then the state of the shared resources may be inconsistent.

For example, if the PAL is currently executing CreateEvent() and the event is named, and at the precise moment that the event name is being copied into memory shared between all PAL processes, the user kills the process with 'kill -f', then the shared memory might be locked forever, or the contents might contain only a fragment of the event's name. In this case, it is acceptable for other PAL processes to block forever or crash, if they access the PAL's shared memory.

The PAL should not acquire and hold locks when no PAL APIs are executing, and should not hold locks across long-running APIs such as WaitForSingleObject().

An alternative solution for this problem is to create a common, trusted, helper process to store the shared state, and PAL client processes would request reads and/or writes via LRPC calls. The server could be robust against errant clients, and reduce the points of failure down to just the trusted helper.

Appendix A: Building rotor_pal.satellite

In the current implementation of the Shared Source CLI there is tools support for generating a rotor_pal.satellite file which is a string resource file as described above in Localized String Resources.

In %ROTOR_DIR%\pal\unix, there is a rotor_pal.satellite file, which contains the mapping from Win32 LastError codes to English strings. This mapping is used by the PAL implementation of FormatMessage. The rotor_pal.satellite is machine-generated by building tools\palsatellite on a computor running Windows, and executing it while the current directory is set to %ROTOR_DIR%\pal:

This will create a new rotor_pal.satellite file in %ROTOR_DIR%\pal, which needs to be manually moved to %ROTOR_DIR%\pal\unix.

The tool works by scanning rotor_pal.h for lines starting with "#define ERROR_".  The tool captures the error number constant, then calls Win32 FormatMessage, captures the output, and then formats it into a Rotor Satellite file.  After running the tool, the LastError mapping prints exactly the same messages on both the Windows and FreeBSD Rotor PAL implementation.

This tool needs to be re-run whenever new ERROR_ constants are added to rotor_pal.h. They are a cut and pasted from the Win32 Platform SDK file, winerror.h.

Copyright (c) 2002 Microsoft Corporation