[CSR]
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 6 Oct 2013 13:33:17 +0000 (13:33 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 6 Oct 2013 13:33:17 +0000 (13:33 +0000)
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

reactos/include/reactos/subsys/csr/csrsrv.h
reactos/subsystems/win/basesrv/init.c
reactos/subsystems/win32/csrsrv/api.c
reactos/subsystems/win32/csrsrv/api.h
reactos/subsystems/win32/csrsrv/init.c
reactos/subsystems/win32/csrsrv/server.c
reactos/win32ss/user/winsrv/consrv/init.c
reactos/win32ss/user/winsrv/usersrv/init.c

index 6c89d14..8634d3c 100644 (file)
 #ifndef _CSRSRV_H
 #define _CSRSRV_H
 
 #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"
 
 
 #include "csrmsg.h"
 
 
@@ -215,7 +223,14 @@ typedef struct _CSR_SERVER_DLL
     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.
     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;
     PCHAR *NameTable;
+#endif
+
     ULONG SizeOfProcessData;
     PCSR_CONNECT_CALLBACK ConnectCallback;
     PCSR_DISCONNECT_CALLBACK DisconnectCallback;
     ULONG SizeOfProcessData;
     PCSR_CONNECT_CALLBACK ConnectCallback;
     PCSR_DISCONNECT_CALLBACK DisconnectCallback;
@@ -226,7 +241,11 @@ typedef struct _CSR_SERVER_DLL
     ULONG Unknown2[3];
 } CSR_SERVER_DLL, *PCSR_SERVER_DLL;
 #ifndef _WIN64
     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
 #endif
 
 typedef
index 6489ca8..b08d6bc 100644 (file)
@@ -97,6 +97,11 @@ BOOLEAN BaseServerApiServerValidTable[BasepMaxApiNumber - BASESRV_FIRST_API_NUMB
     TRUE,   // BaseSrvNlsGetUserInfo
 };
 
     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",
 PCHAR BaseServerApiNameTable[BasepMaxApiNumber - BASESRV_FIRST_API_NUMBER] =
 {
     "BaseCreateProcess",
@@ -131,6 +136,7 @@ PCHAR BaseServerApiNameTable[BasepMaxApiNumber - BASESRV_FIRST_API_NUMBER] =
     "BaseRegisterThread",
     "BaseNlsGetUserInfo",
 };
     "BaseRegisterThread",
     "BaseNlsGetUserInfo",
 };
+#endif
 
 /* FUNCTIONS ******************************************************************/
 
 
 /* FUNCTIONS ******************************************************************/
 
@@ -567,7 +573,9 @@ CSR_SERVER_DLL_INIT(ServerDllInitialization)
     LoadedServerDll->HighestApiSupported = BasepMaxApiNumber;
     LoadedServerDll->DispatchTable = BaseServerApiDispatchTable;
     LoadedServerDll->ValidTable = BaseServerApiServerValidTable;
     LoadedServerDll->HighestApiSupported = BasepMaxApiNumber;
     LoadedServerDll->DispatchTable = BaseServerApiDispatchTable;
     LoadedServerDll->ValidTable = BaseServerApiServerValidTable;
+#ifdef CSR_DBG
     LoadedServerDll->NameTable = BaseServerApiNameTable;
     LoadedServerDll->NameTable = BaseServerApiNameTable;
+#endif
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;
index dee6bd7..55f8c9d 100644 (file)
@@ -77,23 +77,27 @@ CsrCallServerFromServer(IN PCSR_API_MESSAGE ReceiveMsg,
         {
             /* We are beyond the Maximum API ID, or it doesn't exist */
             DPRINT1("API: %d\n", ApiId);
         {
             /* 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);
             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;
         }
     }
 
             // 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]);
     }
     if (CsrDebug & 2)
     {
         DPRINT1("CSRSS: %s Api Request received from server process\n",
                 ServerDll->NameTable[ApiId]);
     }
+#endif
 
     /* Validation complete, start SEH */
     _SEH2_TRY
 
     /* Validation complete, start SEH */
     _SEH2_TRY
@@ -577,6 +581,7 @@ CsrApiRequestThread(IN PVOID Parameter)
                     continue;
                 }
 
                     continue;
                 }
 
+#ifdef CSR_DBG
                 if (CsrDebug & 2)
                 {
                     DPRINT1("[%02x] CSRSS: [%02x,%02x] - %s Api called from %08x\n",
                 if (CsrDebug & 2)
                 {
                     DPRINT1("[%02x] CSRSS: [%02x,%02x] - %s Api called from %08x\n",
@@ -586,6 +591,7 @@ CsrApiRequestThread(IN PVOID Parameter)
                             ServerDll->NameTable[ApiId],
                             NULL);
                 }
                             ServerDll->NameTable[ApiId],
                             NULL);
                 }
+#endif
 
                 /* Assume success */
                 ReceiveMsg.Status = STATUS_SUCCESS;
 
                 /* Assume success */
                 ReceiveMsg.Status = STATUS_SUCCESS;
@@ -781,6 +787,7 @@ CsrApiRequestThread(IN PVOID Parameter)
             continue;
         }
 
             continue;
         }
 
+#ifdef CSR_DBG
         if (CsrDebug & 2)
         {
             DPRINT1("[%02x] CSRSS: [%02x,%02x] - %s Api called from %08x, Process %08x - %08x\n",
         if (CsrDebug & 2)
         {
             DPRINT1("[%02x] CSRSS: [%02x,%02x] - %s Api called from %08x, Process %08x - %08x\n",
@@ -792,6 +799,7 @@ CsrApiRequestThread(IN PVOID Parameter)
                     CsrThread->Process,
                     CsrProcess);
         }
                     CsrThread->Process,
                     CsrProcess);
         }
+#endif
 
         /* Assume success */
         ReplyMsg = &ReceiveMsg;
 
         /* Assume success */
         ReplyMsg = &ReceiveMsg;
index 6bf2944..5171b09 100644 (file)
@@ -41,6 +41,9 @@ extern RTL_CRITICAL_SECTION CsrProcessLock, CsrWaitListsLock;
 #define CSR_SERVER_DLL_MAX  4
 
 
 #define CSR_SERVER_DLL_MAX  4
 
 
+// Debug Flag
+extern ULONG CsrDebug;
+
 extern HANDLE hBootstrapOk;
 extern HANDLE CsrApiPort;
 extern HANDLE CsrSmApiPort;
 extern HANDLE hBootstrapOk;
 extern HANDLE CsrApiPort;
 extern HANDLE CsrSmApiPort;
@@ -49,7 +52,6 @@ extern HANDLE CsrSbApiPort;
 extern LIST_ENTRY CsrThreadHashTable[NUMBER_THREAD_HASH_BUCKETS];
 extern PCSR_PROCESS CsrRootProcess;
 extern UNICODE_STRING CsrDirectoryName;
 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;
 extern ULONG CsrTotalPerProcessDataLength;
 extern SYSTEM_BASIC_INFORMATION CsrNtSysInfo;
 extern HANDLE CsrHeap;
index 4b08d5f..fb8b393 100644 (file)
@@ -16,6 +16,9 @@
 
 /* DATA ***********************************************************************/
 
 
 /* DATA ***********************************************************************/
 
+// Debug Flag
+ULONG CsrDebug = 0; // 0xFFFFFFFF;
+
 HANDLE CsrHeap = NULL;
 HANDLE CsrObjectDirectory = NULL;
 UNICODE_STRING CsrDirectoryName;
 HANDLE CsrHeap = NULL;
 HANDLE CsrObjectDirectory = NULL;
 UNICODE_STRING CsrDirectoryName;
@@ -25,7 +28,6 @@ PCSR_THREAD CsrSbApiRequestThreadPtr;
 HANDLE CsrSmApiPort = NULL;
 HANDLE hSbApiPort = NULL;
 HANDLE CsrApiPort = NULL;
 HANDLE CsrSmApiPort = NULL;
 HANDLE hSbApiPort = NULL;
 HANDLE CsrApiPort = NULL;
-ULONG CsrDebug = 0; // 0xFFFFFFFF;
 ULONG CsrMaxApiRequestThreads;
 ULONG CsrTotalPerProcessDataLength;
 ULONG SessionId;
 ULONG CsrMaxApiRequestThreads;
 ULONG CsrTotalPerProcessDataLength;
 ULONG SessionId;
index 213165a..5efeb0d 100644 (file)
 
 /* DATA ***********************************************************************/
 
 
 /* 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,
 PCSR_API_ROUTINE CsrServerApiDispatchTable[CsrpMaxApiNumber] =
 {
     CsrSrvClientConnect,
@@ -33,6 +40,11 @@ BOOLEAN CsrServerApiServerValidTable[CsrpMaxApiNumber] =
     TRUE
 };
 
     TRUE
 };
 
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
 PCHAR CsrServerApiNameTable[CsrpMaxApiNumber] =
 {
     "ClientConnect",
 PCHAR CsrServerApiNameTable[CsrpMaxApiNumber] =
 {
     "ClientConnect",
@@ -41,13 +53,7 @@ PCHAR CsrServerApiNameTable[CsrpMaxApiNumber] =
     "IdentifyAlertableThread",
     "SetPriorityClass"
 };
     "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 **********************************************************/
 
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
@@ -73,7 +79,9 @@ CSR_SERVER_DLL_INIT(CsrServerDllInitialization)
     LoadedServerDll->HighestApiSupported = CsrpMaxApiNumber;
     LoadedServerDll->DispatchTable = CsrServerApiDispatchTable;
     LoadedServerDll->ValidTable = CsrServerApiServerValidTable;
     LoadedServerDll->HighestApiSupported = CsrpMaxApiNumber;
     LoadedServerDll->DispatchTable = CsrServerApiDispatchTable;
     LoadedServerDll->ValidTable = CsrServerApiServerValidTable;
+#ifdef CSR_DBG
     LoadedServerDll->NameTable = CsrServerApiNameTable;
     LoadedServerDll->NameTable = CsrServerApiNameTable;
+#endif
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;
index 1052295..a8365f5 100644 (file)
@@ -218,6 +218,11 @@ BOOLEAN ConsoleServerApiServerValidTable[ConsolepMaxApiNumber - CONSRV_FIRST_API
     // FALSE,   // SrvConsoleClientConnect,
 };
 
     // 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",
 PCHAR ConsoleServerApiNameTable[ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER] =
 {
     "OpenConsole",
@@ -313,7 +318,7 @@ PCHAR ConsoleServerApiNameTable[ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER]
     // "SetScreenBufferInfo",
     // "ConsoleClientConnect",
 };
     // "SetScreenBufferInfo",
     // "ConsoleClientConnect",
 };
-
+#endif
 
 /* FUNCTIONS ******************************************************************/
 
 
 /* FUNCTIONS ******************************************************************/
 
@@ -532,7 +537,9 @@ CSR_SERVER_DLL_INIT(ConServerDllInitialization)
     LoadedServerDll->HighestApiSupported = ConsolepMaxApiNumber;
     LoadedServerDll->DispatchTable = ConsoleServerApiDispatchTable;
     LoadedServerDll->ValidTable = ConsoleServerApiServerValidTable;
     LoadedServerDll->HighestApiSupported = ConsolepMaxApiNumber;
     LoadedServerDll->DispatchTable = ConsoleServerApiDispatchTable;
     LoadedServerDll->ValidTable = ConsoleServerApiServerValidTable;
+#ifdef CSR_DBG
     LoadedServerDll->NameTable = ConsoleServerApiNameTable;
     LoadedServerDll->NameTable = ConsoleServerApiNameTable;
+#endif
     LoadedServerDll->SizeOfProcessData = sizeof(CONSOLE_PROCESS_DATA);
     LoadedServerDll->ConnectCallback = ConSrvConnect;
     LoadedServerDll->DisconnectCallback = ConSrvDisconnect;
     LoadedServerDll->SizeOfProcessData = sizeof(CONSOLE_PROCESS_DATA);
     LoadedServerDll->ConnectCallback = ConSrvConnect;
     LoadedServerDll->DisconnectCallback = ConSrvDisconnect;
index 7749d74..acffbc4 100644 (file)
@@ -57,6 +57,11 @@ BOOLEAN UserServerApiServerValidTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMB
     // FALSE,   // SrvGetSetShutdownBlockReason
 };
 
     // 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",
 PCHAR UserServerApiNameTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
 {
     "SrvExitWindowsEx",
@@ -73,7 +78,7 @@ PCHAR UserServerApiNameTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
     // "SrvConsoleHandleOperation",
     // "SrvGetSetShutdownBlockReason",
 };
     // "SrvConsoleHandleOperation",
     // "SrvGetSetShutdownBlockReason",
 };
-
+#endif
 
 /* FUNCTIONS ******************************************************************/
 
 
 /* FUNCTIONS ******************************************************************/
 
@@ -291,7 +296,9 @@ CSR_SERVER_DLL_INIT(UserServerDllInitialization)
     LoadedServerDll->HighestApiSupported = UserpMaxApiNumber;
     LoadedServerDll->DispatchTable = UserServerApiDispatchTable;
     LoadedServerDll->ValidTable = UserServerApiServerValidTable;
     LoadedServerDll->HighestApiSupported = UserpMaxApiNumber;
     LoadedServerDll->DispatchTable = UserServerApiDispatchTable;
     LoadedServerDll->ValidTable = UserServerApiServerValidTable;
+#ifdef CSR_DBG
     LoadedServerDll->NameTable = UserServerApiNameTable;
     LoadedServerDll->NameTable = UserServerApiNameTable;
+#endif
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;