During my investigations for making working Win2k3 csrsrv.dll (or other CSR servers) into ROS (to compare our behaviour with our own csrsrv.dll and Win2k3 one), I hit a problem: if I test a checked-build version of csrsrv (or other CSR servers), everything was fine when they were loaded, but if I use a release-build version (i.e. without any debug information), I systematically hit a memory access violation which was traced back to the moment when a CSR server's CsrInitialization entry point was called.
So I did the experiment, where I used our (debug-build) csrsrv with a free-build win2k3 CSR server dll (it was winsrv.dll, and I retested with basesrv.dll after). I hit the access violation. But if I took a debug-build version of winsrv.dll, everything was OK.
I then added in our csrsrv' server.c file the following line (around line 212 of the current file version):
DPRINT1("%s ; ServerDll->ValidTable = 0x%p ; ServerDll->NameTable = 0x%p ; ServerDll->SizeOfProcessData = %d ; ServerDll->ConnectCallback = 0x%p\n", DllString, ServerDll->ValidTable, ServerDll->NameTable, ServerDll->SizeOfProcessData, ServerDll->ConnectCallback);
and I saw that, when using a debug-build win2k3 CSR server, everything was fine (in particular the ServerDll->SizeOfProcessData member contained a reasonable value, e.g. a size of 88 bytes), whereas if I used a free-build version, I got an off-by-one problem, with the ServerDll->ValidTable pointer valid but the ServerDll->NameTable member being equal to 88 (i.e. invalid pointer) and the ServerDll->SizeOfProcessData member being equal to a very large value, which looked like a pointer value.
After more investigations, I saw that in debug-build CSR servers the list of API names were stored, whereas it was not the case in free-build versions. Therefore I concluded that the API names table was included *ONLY* in debug builds and not in release builds.
Hence, to be able to test in ROS either debug-builds or release-builds versions of Windows CSR servers in ROS (and vice-versa), I introduced a #define called CSR_DBG, which is defined only if the DBG macro is != 0, and which is not defined otherwise. When the CSR_DBG flag is defined, API names tables are added in CSR servers and otherwise, they are not.
Therefore, we are now able to test debug-build Windows CSR servers in ROS (the default possibility) or free-build versions of these CSR servers (but first, we have to build the other ones without the CSR_DBG flag, to avoid the off-by-one problem described above).
svn path=/trunk/; revision=60560
#ifndef _CSRSRV_H
#define _CSRSRV_H
+/*
+ * The CSR_DBG macro is defined for building CSR Servers
+ * with extended debugging information.
+ */
+#if DBG
+#define CSR_DBG
+#endif
+
#include "csrmsg.h"
ULONG HighestApiSupported;
PCSR_API_ROUTINE *DispatchTable;
PBOOLEAN ValidTable; // Table of booleans which describe whether or not a server function call is valid when it is called via CsrCallServerFromServer.
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
PCHAR *NameTable;
+#endif
+
ULONG SizeOfProcessData;
PCSR_CONNECT_CALLBACK ConnectCallback;
PCSR_DISCONNECT_CALLBACK DisconnectCallback;
ULONG Unknown2[3];
} CSR_SERVER_DLL, *PCSR_SERVER_DLL;
#ifndef _WIN64
-C_ASSERT(FIELD_OFFSET(CSR_SERVER_DLL, SharedSection) == 0x3C);
+ #ifdef CSR_DBG
+ C_ASSERT(FIELD_OFFSET(CSR_SERVER_DLL, SharedSection) == 0x3C);
+ #else
+ C_ASSERT(FIELD_OFFSET(CSR_SERVER_DLL, SharedSection) == 0x38);
+ #endif
#endif
typedef
TRUE, // BaseSrvNlsGetUserInfo
};
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
PCHAR BaseServerApiNameTable[BasepMaxApiNumber - BASESRV_FIRST_API_NUMBER] =
{
"BaseCreateProcess",
"BaseRegisterThread",
"BaseNlsGetUserInfo",
};
+#endif
/* FUNCTIONS ******************************************************************/
LoadedServerDll->HighestApiSupported = BasepMaxApiNumber;
LoadedServerDll->DispatchTable = BaseServerApiDispatchTable;
LoadedServerDll->ValidTable = BaseServerApiServerValidTable;
+#ifdef CSR_DBG
LoadedServerDll->NameTable = BaseServerApiNameTable;
+#endif
LoadedServerDll->SizeOfProcessData = 0;
LoadedServerDll->ConnectCallback = NULL;
LoadedServerDll->DisconnectCallback = NULL;
{
/* We are beyond the Maximum API ID, or it doesn't exist */
DPRINT1("API: %d\n", ApiId);
+#ifdef CSR_DBG
DPRINT1("CSRSS: %lx (%s) is invalid ApiTableIndex for %Z or is an "
"invalid API to call from the server.\n",
ApiId,
((ServerDll->NameTable) && (ServerDll->NameTable[ApiId])) ?
ServerDll->NameTable[ApiId] : "*** UNKNOWN ***",
&ServerDll->Name);
+#endif
// DbgBreakPoint();
ReplyMsg->Status = STATUS_ILLEGAL_FUNCTION;
return STATUS_ILLEGAL_FUNCTION;
}
}
+#ifdef CSR_DBG
if (CsrDebug & 2)
{
DPRINT1("CSRSS: %s Api Request received from server process\n",
ServerDll->NameTable[ApiId]);
}
+#endif
/* Validation complete, start SEH */
_SEH2_TRY
continue;
}
+#ifdef CSR_DBG
if (CsrDebug & 2)
{
DPRINT1("[%02x] CSRSS: [%02x,%02x] - %s Api called from %08x\n",
ServerDll->NameTable[ApiId],
NULL);
}
+#endif
/* Assume success */
ReceiveMsg.Status = STATUS_SUCCESS;
continue;
}
+#ifdef CSR_DBG
if (CsrDebug & 2)
{
DPRINT1("[%02x] CSRSS: [%02x,%02x] - %s Api called from %08x, Process %08x - %08x\n",
CsrThread->Process,
CsrProcess);
}
+#endif
/* Assume success */
ReplyMsg = &ReceiveMsg;
#define CSR_SERVER_DLL_MAX 4
+// Debug Flag
+extern ULONG CsrDebug;
+
extern HANDLE hBootstrapOk;
extern HANDLE CsrApiPort;
extern HANDLE CsrSmApiPort;
extern LIST_ENTRY CsrThreadHashTable[NUMBER_THREAD_HASH_BUCKETS];
extern PCSR_PROCESS CsrRootProcess;
extern UNICODE_STRING CsrDirectoryName;
-extern ULONG CsrDebug;
extern ULONG CsrTotalPerProcessDataLength;
extern SYSTEM_BASIC_INFORMATION CsrNtSysInfo;
extern HANDLE CsrHeap;
/* DATA ***********************************************************************/
+// Debug Flag
+ULONG CsrDebug = 0; // 0xFFFFFFFF;
+
HANDLE CsrHeap = NULL;
HANDLE CsrObjectDirectory = NULL;
UNICODE_STRING CsrDirectoryName;
HANDLE CsrSmApiPort = NULL;
HANDLE hSbApiPort = NULL;
HANDLE CsrApiPort = NULL;
-ULONG CsrDebug = 0; // 0xFFFFFFFF;
ULONG CsrMaxApiRequestThreads;
ULONG CsrTotalPerProcessDataLength;
ULONG SessionId;
/* DATA ***********************************************************************/
+PCSR_SERVER_DLL CsrLoadedServerDll[CSR_SERVER_DLL_MAX];
+PVOID CsrSrvSharedSectionHeap = NULL;
+PVOID CsrSrvSharedSectionBase = NULL;
+PVOID *CsrSrvSharedStaticServerData = NULL;
+ULONG CsrSrvSharedSectionSize = 0;
+HANDLE CsrSrvSharedSection = NULL;
+
PCSR_API_ROUTINE CsrServerApiDispatchTable[CsrpMaxApiNumber] =
{
CsrSrvClientConnect,
TRUE
};
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
PCHAR CsrServerApiNameTable[CsrpMaxApiNumber] =
{
"ClientConnect",
"IdentifyAlertableThread",
"SetPriorityClass"
};
-
-PCSR_SERVER_DLL CsrLoadedServerDll[CSR_SERVER_DLL_MAX];
-PVOID CsrSrvSharedSectionHeap = NULL;
-PVOID CsrSrvSharedSectionBase = NULL;
-PVOID *CsrSrvSharedStaticServerData = NULL;
-ULONG CsrSrvSharedSectionSize = 0;
-HANDLE CsrSrvSharedSection = NULL;
+#endif
/* PRIVATE FUNCTIONS **********************************************************/
LoadedServerDll->HighestApiSupported = CsrpMaxApiNumber;
LoadedServerDll->DispatchTable = CsrServerApiDispatchTable;
LoadedServerDll->ValidTable = CsrServerApiServerValidTable;
+#ifdef CSR_DBG
LoadedServerDll->NameTable = CsrServerApiNameTable;
+#endif
LoadedServerDll->SizeOfProcessData = 0;
LoadedServerDll->ConnectCallback = NULL;
LoadedServerDll->DisconnectCallback = NULL;
// FALSE, // SrvConsoleClientConnect,
};
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
PCHAR ConsoleServerApiNameTable[ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER] =
{
"OpenConsole",
// "SetScreenBufferInfo",
// "ConsoleClientConnect",
};
-
+#endif
/* FUNCTIONS ******************************************************************/
LoadedServerDll->HighestApiSupported = ConsolepMaxApiNumber;
LoadedServerDll->DispatchTable = ConsoleServerApiDispatchTable;
LoadedServerDll->ValidTable = ConsoleServerApiServerValidTable;
+#ifdef CSR_DBG
LoadedServerDll->NameTable = ConsoleServerApiNameTable;
+#endif
LoadedServerDll->SizeOfProcessData = sizeof(CONSOLE_PROCESS_DATA);
LoadedServerDll->ConnectCallback = ConSrvConnect;
LoadedServerDll->DisconnectCallback = ConSrvDisconnect;
// FALSE, // SrvGetSetShutdownBlockReason
};
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
PCHAR UserServerApiNameTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
{
"SrvExitWindowsEx",
// "SrvConsoleHandleOperation",
// "SrvGetSetShutdownBlockReason",
};
-
+#endif
/* FUNCTIONS ******************************************************************/
LoadedServerDll->HighestApiSupported = UserpMaxApiNumber;
LoadedServerDll->DispatchTable = UserServerApiDispatchTable;
LoadedServerDll->ValidTable = UserServerApiServerValidTable;
+#ifdef CSR_DBG
LoadedServerDll->NameTable = UserServerApiNameTable;
+#endif
LoadedServerDll->SizeOfProcessData = 0;
LoadedServerDll->ConnectCallback = NULL;
LoadedServerDll->DisconnectCallback = NULL;