[ros-diffs] [cwittich] 46292: [RPCRT4] partial rpcrt4 sync to wine 1.1.40 (fixes crashing cstub test)

cwittich at svn.reactos.org cwittich at svn.reactos.org
Sun Mar 21 11:53:19 CET 2010


Author: cwittich
Date: Sun Mar 21 11:53:17 2010
New Revision: 46292

URL: http://svn.reactos.org/svn/reactos?rev=46292&view=rev
Log:
[RPCRT4]
partial rpcrt4 sync to wine 1.1.40 (fixes crashing cstub test)

Modified:
    trunk/reactos/dll/win32/rpcrt4/cproxy.c
    trunk/reactos/dll/win32/rpcrt4/cpsf.c
    trunk/reactos/dll/win32/rpcrt4/cpsf.h
    trunk/reactos/dll/win32/rpcrt4/cstub.c
    trunk/reactos/dll/win32/rpcrt4/ndr_ole.c

Modified: trunk/reactos/dll/win32/rpcrt4/cproxy.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/rpcrt4/cproxy.c?rev=46292&r1=46291&r2=46292&view=diff
==============================================================================
--- trunk/reactos/dll/win32/rpcrt4/cproxy.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/rpcrt4/cproxy.c [iso-8859-1] Sun Mar 21 11:53:17 2010
@@ -2,6 +2,7 @@
  * COM proxy implementation
  *
  * Copyright 2001 Ove KÃ¥ven, TransGaming Technologies
+ * Copyright 2009 Alexandre Julliard
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,9 @@
  * TODO: Handle non-i386 architectures
  */
 
+#include "config.h"
+#include "wine/port.h"
+
 #include <stdarg.h>
 
 #define COBJMACROS
@@ -36,8 +40,6 @@
 #include "wine/debug.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(ole);
-
-struct StublessThunk;
 
 /* I don't know what MS's std proxy structure looks like,
    so this probably doesn't match, but that shouldn't matter */
@@ -45,13 +47,13 @@
   const IRpcProxyBufferVtbl *lpVtbl;
   LPVOID *PVtbl;
   LONG RefCount;
-  const MIDL_STUBLESS_PROXY_INFO *stubless;
   const IID* piid;
   LPUNKNOWN pUnkOuter;
+  IUnknown *base_object;  /* must be at offset 0x10 from PVtbl */
+  IRpcProxyBuffer *base_proxy;
   PCInterfaceName name;
   LPPSFACTORYBUFFER pPSFactory;
   LPRPCCHANNELBUFFER pChannel;
-  struct StublessThunk *thunks;
 } StdProxyImpl;
 
 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
@@ -62,66 +64,111 @@
 
 #include "pshpack1.h"
 
-struct StublessThunk {
+struct thunk {
   BYTE push;
   DWORD index;
-  BYTE call;
+  BYTE jmp;
   LONG handler;
-  BYTE ret;
-  WORD bytes;
-  BYTE pad[3];
 };
 
 #include "poppack.h"
 
-/* adjust the stack size since we don't use Windows's method */
-#define STACK_ADJUST sizeof(DWORD)
-
-#define FILL_STUBLESS(x,idx,stk) \
- x->push = 0x68; /* pushl [immediate] */ \
- x->index = (idx); \
- x->call = 0xe8; /* call [near] */ \
- x->handler = (char*)ObjectStubless - (char*)&x->ret; \
- x->ret = 0xc2; /* ret [immediate] */ \
- x->bytes = stk; \
- x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
- x->pad[1] = 0x76; \
- x->pad[2] = 0x00;
-
-static HRESULT WINAPI ObjectStubless(DWORD index)
-{
-  char *args = (char*)(&index + 2);
-  LPVOID iface = *(LPVOID*)args;
-
-  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
-
-  PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
-  unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
-  TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, bytes, *(DWORD*)(args+bytes));
-
-  return NdrClientCall2(This->stubless->pStubDesc, fs, args);
+extern void call_stubless_func(void);
+__ASM_GLOBAL_FUNC(call_stubless_func,
+                  "pushl %esp\n\t"  /* pointer to index */
+                  __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
+                  "call " __ASM_NAME("ObjectStubless") __ASM_STDCALL(4) "\n\t"
+                  "popl %edx\n\t"  /* args size */
+                  __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
+                  "movl (%esp),%ecx\n\t"  /* return address */
+                  "addl %edx,%esp\n\t"
+                  "jmp *%ecx" );
+
+HRESULT WINAPI ObjectStubless(DWORD *args)
+{
+    DWORD index = args[0];
+    void **iface = (void **)args[2];
+    const void **vtbl = (const void **)*iface;
+    const MIDL_STUBLESS_PROXY_INFO *stubless = *(const MIDL_STUBLESS_PROXY_INFO **)(vtbl - 2);
+    const PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[index];
+
+    /* store bytes to remove from stack */
+    args[0] = *(const WORD*)(fs + 8);
+    TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, args[0], args[1]);
+
+    return NdrClientCall2(stubless->pStubDesc, fs, args + 2);
+}
+
+#define BLOCK_SIZE 1024
+#define MAX_BLOCKS 64  /* 64k methods should be enough for anybody */
+
+static const struct thunk *method_blocks[MAX_BLOCKS];
+
+static const struct thunk *allocate_block( unsigned int num )
+{
+    unsigned int i;
+    struct thunk *prev, *block;
+
+    block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
+                          MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
+    if (!block) return NULL;
+
+    for (i = 0; i < BLOCK_SIZE; i++)
+    {
+        block[i].push    = 0x68; /* pushl */
+        block[i].index   = BLOCK_SIZE * num + i + 3;
+        block[i].jmp     = 0xe9; /* jmp */
+        block[i].handler = (char *)call_stubless_func - (char *)(&block[i].handler + 1);
+    }
+    VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
+    prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
+    if (prev) /* someone beat us to it */
+    {
+        VirtualFree( block, 0, MEM_RELEASE );
+        block = prev;
+    }
+    return block;
+}
+
+static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
+{
+    const void **entry = (const void **)(vtbl + 1);
+    DWORD i, j;
+
+    if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
+    {
+        FIXME( "%u methods not supported\n", num );
+        return FALSE;
+    }
+    for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
+    {
+        const struct thunk *block = method_blocks[i];
+        if (!block && !(block = allocate_block( i ))) return FALSE;
+        for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
+            if (*entry == (LPVOID)-1) *entry = &block[j];
+    }
+    return TRUE;
 }
 
 #else  /* __i386__ */
 
-/* can't do that on this arch */
-struct StublessThunk { int dummy; };
-#define FILL_STUBLESS(x,idx,stk) \
- ERR("stubless proxies are not supported on this architecture\n");
-#define STACK_ADJUST 0
+static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
+{
+    ERR("stubless proxies are not supported on this architecture\n");
+    return FALSE;
+}
 
 #endif  /* __i386__ */
 
-HRESULT WINAPI StdProxy_Construct(REFIID riid,
-                                 LPUNKNOWN pUnkOuter,
-                                 const ProxyFileInfo *ProxyInfo,
-                                 int Index,
-                                 LPPSFACTORYBUFFER pPSFactory,
-                                 LPRPCPROXYBUFFER *ppProxy,
-                                 LPVOID *ppvObj)
+HRESULT StdProxy_Construct(REFIID riid,
+                           LPUNKNOWN pUnkOuter,
+                           const ProxyFileInfo *ProxyInfo,
+                           int Index,
+                           LPPSFACTORYBUFFER pPSFactory,
+                           LPRPCPROXYBUFFER *ppProxy,
+                           LPVOID *ppvObj)
 {
   StdProxyImpl *This;
-  const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
   PCInterfaceName name = ProxyInfo->pNamesArray[Index];
   CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
 
@@ -129,13 +176,11 @@
 
   /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
   if (ProxyInfo->TableVersion > 1) {
-    stubless = *(const void **)vtbl;
+    ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
     vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
-    TRACE("stubless=%p\n", stubless);
-  }
-
-  TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
-  TRACE("vtbl=%p\n", vtbl->Vtbl);
+    TRACE("stubless vtbl %p: count=%d\n", vtbl->Vtbl, count );
+    fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
+  }
 
   if (!IsEqualGUID(vtbl->header.piid, riid)) {
     ERR("IID mismatch during proxy creation\n");
@@ -145,51 +190,37 @@
   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
   if (!This) return E_OUTOFMEMORY;
 
-  if (stubless) {
-    CInterfaceStubVtbl *svtbl = ProxyInfo->pStubVtblList[Index];
-    unsigned long i, count = svtbl->header.DispatchTableCount;
-    /* Maybe the original vtbl is just modified directly to point at
-     * ObjectStublessClientXXX thunks in real Windows, but I don't like it
-     */
-    TRACE("stubless thunks: count=%ld\n", count);
-    This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
-    This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
-    for (i=0; i<count; i++) {
-      struct StublessThunk *thunk = &This->thunks[i];
-      if (vtbl->Vtbl[i] == (LPVOID)-1) {
-        PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
-        unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
-        TRACE("method %ld: stacksize=%d\n", i, bytes);
-        FILL_STUBLESS(thunk, i, bytes)
-        This->PVtbl[i] = thunk;
-      }
-      else {
-        memset(thunk, 0, sizeof(struct StublessThunk));
-        This->PVtbl[i] = vtbl->Vtbl[i];
-      }
-    }
-  }
-  else 
-    This->PVtbl = vtbl->Vtbl;
-
+  if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
   This->lpVtbl = &StdProxy_Vtbl;
+  This->PVtbl = vtbl->Vtbl;
   /* one reference for the proxy */
   This->RefCount = 1;
-  This->stubless = stubless;
   This->piid = vtbl->header.piid;
+  This->base_object = NULL;
+  This->base_proxy = NULL;
   This->pUnkOuter = pUnkOuter;
   This->name = name;
   This->pPSFactory = pPSFactory;
   This->pChannel = NULL;
+
+  if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
+  {
+      HRESULT r = create_proxy( ProxyInfo->pDelegatedIIDs[Index], NULL,
+                                &This->base_proxy, (void **)&This->base_object );
+      if (FAILED(r))
+      {
+          HeapFree( GetProcessHeap(), 0, This );
+          return r;
+      }
+  }
+
   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
   *ppvObj = &This->PVtbl;
-  /* if there is no outer unknown then the caller will control the lifetime
-   * of the proxy object through the proxy buffer, so no need to increment the
-   * ref count of the proxy object */
-  if (pUnkOuter)
-    IUnknown_AddRef((IUnknown *)*ppvObj);
+  IUnknown_AddRef((IUnknown *)*ppvObj);
   IPSFactoryBuffer_AddRef(pPSFactory);
 
+  TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
+         debugstr_guid(riid), This, *ppProxy, *ppvObj, This->PVtbl, This->base_proxy, This->base_object );
   return S_OK;
 }
 
@@ -200,11 +231,10 @@
   if (This->pChannel)
     IRpcProxyBuffer_Disconnect(iface);
 
+  if (This->base_object) IUnknown_Release( This->base_object );
+  if (This->base_proxy) IRpcProxyBuffer_Release( This->base_proxy );
+
   IPSFactoryBuffer_Release(This->pPSFactory);
-  if (This->thunks) {
-    HeapFree(GetProcessHeap(),0,This->PVtbl);
-    HeapFree(GetProcessHeap(),0,This->thunks);
-  }
   HeapFree(GetProcessHeap(),0,This);
 }
 
@@ -259,6 +289,7 @@
 
   This->pChannel = pChannel;
   IRpcChannelBuffer_AddRef(pChannel);
+  if (This->base_proxy) IRpcProxyBuffer_Connect( This->base_proxy, pChannel );
   return S_OK;
 }
 
@@ -266,6 +297,8 @@
 {
   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
   TRACE("(%p)->Disconnect()\n",This);
+
+  if (This->base_proxy) IRpcProxyBuffer_Disconnect( This->base_proxy );
 
   IRpcChannelBuffer_Release(This->pChannel);
   This->pChannel = NULL;

Modified: trunk/reactos/dll/win32/rpcrt4/cpsf.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/rpcrt4/cpsf.c?rev=46292&r1=46291&r2=46292&view=diff
==============================================================================
--- trunk/reactos/dll/win32/rpcrt4/cpsf.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/rpcrt4/cpsf.c [iso-8859-1] Sun Mar 21 11:53:17 2010
@@ -145,6 +145,41 @@
   CStdPSFactory_CreateStub
 };
 
+
+static void init_psfactory( CStdPSFactoryBuffer *psfac, const ProxyFileInfo **file_list )
+{
+    DWORD i, j, k;
+
+    psfac->lpVtbl = &CStdPSFactory_Vtbl;
+    psfac->RefCount = 0;
+    psfac->pProxyFileList = file_list;
+    for (i = 0; file_list[i]; i++)
+    {
+        const PCInterfaceProxyVtblList *proxies = file_list[i]->pProxyVtblList;
+        const PCInterfaceStubVtblList *stubs = file_list[i]->pStubVtblList;
+
+        for (j = 0; j < file_list[i]->TableSize; j++)
+        {
+            /* FIXME: i think that different vtables should be copied for
+             * async interfaces */
+            void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
+            void **pRpcStubVtbl = (void **)&stubs[j]->Vtbl;
+
+            if (file_list[i]->pDelegatedIIDs && file_list[i]->pDelegatedIIDs[j])
+            {
+                void **vtbl = proxies[j]->Vtbl;
+                if (file_list[i]->TableVersion > 1) vtbl++;
+                fill_delegated_proxy_table( (IUnknownVtbl *)vtbl, stubs[j]->header.DispatchTableCount );
+                pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
+            }
+
+            for (k = 0; k < sizeof(IRpcStubBufferVtbl)/sizeof(void *); k++)
+                if (!pRpcStubVtbl[k]) pRpcStubVtbl[k] = pSrcRpcStubVtbl[k];
+        }
+    }
+}
+
+
 /***********************************************************************
  *           NdrDllGetClassObject [RPCRT4.@]
  */
@@ -158,35 +193,8 @@
     pPSFactoryBuffer);
 
   *ppv = NULL;
-  if (!pPSFactoryBuffer->lpVtbl) {
-    const ProxyFileInfo **pProxyFileList2;
-    DWORD max_delegating_vtbl_size = 0;
-    pPSFactoryBuffer->lpVtbl = &CStdPSFactory_Vtbl;
-    pPSFactoryBuffer->RefCount = 0;
-    pPSFactoryBuffer->pProxyFileList = pProxyFileList;
-    for (pProxyFileList2 = pProxyFileList; *pProxyFileList2; pProxyFileList2++) {
-      int i;
-      for (i = 0; i < (*pProxyFileList2)->TableSize; i++) {
-        /* FIXME: i think that different vtables should be copied for
-         * async interfaces */
-        void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
-        void **pRpcStubVtbl = (void **)&(*pProxyFileList2)->pStubVtblList[i]->Vtbl;
-        unsigned int j;
-
-        if ((*pProxyFileList2)->pDelegatedIIDs && (*pProxyFileList2)->pDelegatedIIDs[i]) {
-          pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
-          if ((*pProxyFileList2)->pStubVtblList[i]->header.DispatchTableCount > max_delegating_vtbl_size)
-            max_delegating_vtbl_size = (*pProxyFileList2)->pStubVtblList[i]->header.DispatchTableCount;
-        }
-
-        for (j = 0; j < sizeof(IRpcStubBufferVtbl)/sizeof(void *); j++)
-          if (!pRpcStubVtbl[j])
-            pRpcStubVtbl[j] = pSrcRpcStubVtbl[j];
-      }
-    }
-    if(max_delegating_vtbl_size > 0)
-      create_delegating_vtbl(max_delegating_vtbl_size);
-  }
+  if (!pPSFactoryBuffer->lpVtbl) init_psfactory( pPSFactoryBuffer, pProxyFileList );
+
   if (pclsid && IsEqualGUID(rclsid, pclsid))
     return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
   else {
@@ -207,7 +215,7 @@
  */
 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
 {
-  return !(pPSFactoryBuffer->RefCount);
+  return pPSFactoryBuffer->RefCount != 0 ? S_FALSE : S_OK;
 }
 
 
@@ -266,7 +274,7 @@
   if (len && len < sizeof(module)) {
       TRACE("registering CLSID %s => %s\n", debugstr_w(clsid), debugstr_w(module));
       if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
-          RegSetValueExW(subkey, NULL, 0, REG_SZ, (const BYTE *)psfactoryW, sizeof(psfactoryW));
+          RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)psfactoryW, sizeof(psfactoryW));
           if (RegCreateKeyW(key, inprocserverW, &subkey) == ERROR_SUCCESS) {
               RegSetValueExW(subkey, NULL, 0, REG_SZ, (LPBYTE)module, (strlenW(module)+1)*sizeof(WCHAR));
               RegSetValueExW(subkey, threadingmodelW, 0, REG_SZ, (const BYTE *)bothW, sizeof(bothW));

Modified: trunk/reactos/dll/win32/rpcrt4/cpsf.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/rpcrt4/cpsf.h?rev=46292&r1=46291&r2=46292&view=diff
==============================================================================
--- trunk/reactos/dll/win32/rpcrt4/cpsf.h [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/rpcrt4/cpsf.h [iso-8859-1] Sun Mar 21 11:53:17 2010
@@ -21,36 +21,25 @@
 #ifndef __WINE_CPSF_H
 #define __WINE_CPSF_H
 
-HRESULT WINAPI StdProxy_Construct(REFIID riid,
-				  LPUNKNOWN pUnkOuter,
-				  const ProxyFileInfo *ProxyInfo,
-				  int Index,
-				  LPPSFACTORYBUFFER pPSFactory,
-				  LPRPCPROXYBUFFER *ppProxy,
-				  LPVOID *ppvObj);
+HRESULT StdProxy_Construct(REFIID riid, LPUNKNOWN pUnkOuter, const ProxyFileInfo *ProxyInfo,
+                           int Index, LPPSFACTORYBUFFER pPSFactory, LPRPCPROXYBUFFER *ppProxy,
+                           LPVOID *ppvObj);
 
-HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
-					LPUNKNOWN pUnkServer,
-					PCInterfaceName name,
-					CInterfaceStubVtbl *vtbl,
-					LPPSFACTORYBUFFER pPSFactory,
-					LPRPCSTUBBUFFER *ppStub);
+HRESULT CStdStubBuffer_Construct(REFIID riid, LPUNKNOWN pUnkServer, PCInterfaceName name,
+                                 CInterfaceStubVtbl *vtbl, LPPSFACTORYBUFFER pPSFactory,
+                                 LPRPCSTUBBUFFER *ppStub);
 
-HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
-                                                   LPUNKNOWN pUnkServer,
-                                                   PCInterfaceName name,
-                                                   CInterfaceStubVtbl *vtbl,
-                                                   REFIID delegating_iid,
-                                                   LPPSFACTORYBUFFER pPSFactory,
-                                                   LPRPCSTUBBUFFER *ppStub);
+HRESULT CStdStubBuffer_Delegating_Construct(REFIID riid, LPUNKNOWN pUnkServer, PCInterfaceName name,
+                                            CInterfaceStubVtbl *vtbl, REFIID delegating_iid,
+                                            LPPSFACTORYBUFFER pPSFactory, LPRPCSTUBBUFFER *ppStub);
 
 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface);
 
 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl;
 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl;
 
-void create_delegating_vtbl(DWORD num_methods);
-
+BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num);
+HRESULT create_proxy(REFIID iid, IUnknown *pUnkOuter, IRpcProxyBuffer **pproxy, void **ppv);
 HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub);
 
 #endif  /* __WINE_CPSF_H */

Modified: trunk/reactos/dll/win32/rpcrt4/cstub.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/rpcrt4/cstub.c?rev=46292&r1=46291&r2=46292&view=diff
==============================================================================
--- trunk/reactos/dll/win32/rpcrt4/cstub.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/rpcrt4/cstub.c [iso-8859-1] Sun Mar 21 11:53:17 2010
@@ -2,6 +2,7 @@
  * COM stub (CStdStubBuffer) implementation
  *
  * Copyright 2001 Ove KÃ¥ven, TransGaming Technologies
+ * Copyright 2009 Alexandre Julliard
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -61,12 +62,12 @@
     return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
 }
 
-HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
-                                       LPUNKNOWN pUnkServer,
-                                       PCInterfaceName name,
-                                       CInterfaceStubVtbl *vtbl,
-                                       LPPSFACTORYBUFFER pPSFactory,
-                                       LPRPCSTUBBUFFER *ppStub)
+HRESULT CStdStubBuffer_Construct(REFIID riid,
+                                 LPUNKNOWN pUnkServer,
+                                 PCInterfaceName name,
+                                 CInterfaceStubVtbl *vtbl,
+                                 LPPSFACTORYBUFFER pPSFactory,
+                                 LPRPCSTUBBUFFER *ppStub)
 {
   CStdStubBuffer *This;
   IUnknown *pvServer;
@@ -113,20 +114,16 @@
 {
     DWORD ref;
     DWORD size;
-    void **methods;
     IUnknownVtbl vtbl;
     /* remaining entries in vtbl */
 } ref_counted_vtbl;
 
-static struct
-{
-    ref_counted_vtbl *table;
-} current_vtbl;
+static ref_counted_vtbl *current_vtbl;
 
 
 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
 {
-    *ppv = (void *)pUnk;
+    *ppv = pUnk;
     return S_OK;
 }
 
@@ -161,87 +158,137 @@
 } vtbl_method_t;
 #include "poppack.h"
 
-static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
-{
-    vtbl_method_t *method;
-    void **entry;
-    DWORD i;
-
+#define BLOCK_SIZE 1024
+#define MAX_BLOCKS 64  /* 64k methods should be enough for anybody */
+
+static const vtbl_method_t *method_blocks[MAX_BLOCKS];
+
+static const vtbl_method_t *allocate_block( unsigned int num )
+{
+    unsigned int i;
+    vtbl_method_t *prev, *block;
+
+    block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
+                          MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
+    if (!block) return NULL;
+
+    for (i = 0; i < BLOCK_SIZE; i++)
+    {
+        block[i].mov1 = 0x0424448b;
+        block[i].mov2 = 0x408b;
+        block[i].sixteen = 0x10;
+        block[i].mov3 = 0x04244489;
+        block[i].mov4 = 0x008b;
+        block[i].mov5 = 0x808b;
+        block[i].offset = (BLOCK_SIZE * num + i + 3) << 2;
+        block[i].jmp = 0xe0ff;
+        block[i].pad[0] = 0x8d;
+        block[i].pad[1] = 0x76;
+        block[i].pad[2] = 0x00;
+    }
+    VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
+    prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
+    if (prev) /* someone beat us to it */
+    {
+        VirtualFree( block, 0, MEM_RELEASE );
+        block = prev;
+    }
+    return block;
+}
+
+static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
+{
+    const void **entry = (const void **)(vtbl + 1);
+    DWORD i, j;
+
+    if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
+    {
+        FIXME( "%u methods not supported\n", num );
+        return FALSE;
+    }
     vtbl->QueryInterface = delegating_QueryInterface;
     vtbl->AddRef = delegating_AddRef;
     vtbl->Release = delegating_Release;
-
-    method = (vtbl_method_t*)methods;
-    entry = (void**)(vtbl + 1);
-
-    for(i = 3; i < num; i++)
-    {
-        *entry = method;
-        method->mov1 = 0x0424448b;
-        method->mov2 = 0x408b;
-        method->sixteen = 0x10;
-        method->mov3 = 0x04244489;
-        method->mov4 = 0x008b;
-        method->mov5 = 0x808b;
-        method->offset = i << 2;
-        method->jmp = 0xe0ff;
-        method->pad[0] = 0x8d;
-        method->pad[1] = 0x76;
-        method->pad[2] = 0x00;
-
-        method++;
-        entry++;
-    }
+    for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
+    {
+        const vtbl_method_t *block = method_blocks[i];
+        if (!block && !(block = allocate_block( i ))) return FALSE;
+        for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++) *entry++ = &block[j];
+    }
+    return TRUE;
+}
+
+BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
+{
+    const void **entry = (const void **)(vtbl + 1);
+    DWORD i, j;
+
+    if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
+    {
+        FIXME( "%u methods not supported\n", num );
+        return FALSE;
+    }
+    vtbl->QueryInterface = IUnknown_QueryInterface_Proxy;
+    vtbl->AddRef = IUnknown_AddRef_Proxy;
+    vtbl->Release = IUnknown_Release_Proxy;
+    for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
+    {
+        const vtbl_method_t *block = method_blocks[i];
+        if (!block && !(block = allocate_block( i ))) return FALSE;
+        for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
+            if (!*entry) *entry = &block[j];
+    }
+    return TRUE;
 }
 
 #else  /* __i386__ */
 
-typedef struct {int dummy;} vtbl_method_t;
-static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
+static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
 {
     ERR("delegated stubs are not supported on this architecture\n");
+    return FALSE;
+}
+
+BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
+{
+    ERR("delegated proxies are not supported on this architecture\n");
+    return FALSE;
 }
 
 #endif  /* __i386__ */
 
-void create_delegating_vtbl(DWORD num_methods)
-{
-    TRACE("%d\n", num_methods);
-    if(num_methods <= 3)
-    {
-        ERR("should have more than %d methods\n", num_methods);
-        return;
-    }
+static IUnknownVtbl *get_delegating_vtbl(DWORD num_methods)
+{
+    IUnknownVtbl *ret;
+
+    if (num_methods < 256) num_methods = 256;  /* avoid frequent reallocations */
 
     EnterCriticalSection(&delegating_vtbl_section);
-    if(!current_vtbl.table || num_methods > current_vtbl.table->size)
-    {
-        DWORD size;
-        DWORD old_protect;
-        if(current_vtbl.table && current_vtbl.table->ref == 0)
+
+    if(!current_vtbl || num_methods > current_vtbl->size)
+    {
+        ref_counted_vtbl *table = HeapAlloc(GetProcessHeap(), 0,
+                                            FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
+        if (!table)
+        {
+            LeaveCriticalSection(&delegating_vtbl_section);
+            return NULL;
+        }
+
+        table->ref = 0;
+        table->size = num_methods;
+        fill_delegated_stub_table(&table->vtbl, num_methods);
+
+        if (current_vtbl && current_vtbl->ref == 0)
         {
             TRACE("freeing old table\n");
-            VirtualFree(current_vtbl.table->methods, 0, MEM_RELEASE);
-            HeapFree(GetProcessHeap(), 0, current_vtbl.table);
+            HeapFree(GetProcessHeap(), 0, current_vtbl);
         }
-        size = (num_methods - 3) * sizeof(vtbl_method_t);
-        current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
-        current_vtbl.table->ref = 0;
-        current_vtbl.table->size = num_methods;
-        current_vtbl.table->methods = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
-        fill_table(&current_vtbl.table->vtbl, current_vtbl.table->methods, num_methods);
-        VirtualProtect(current_vtbl.table->methods, size, PAGE_EXECUTE_READ, &old_protect);
-    }
-    LeaveCriticalSection(&delegating_vtbl_section);
-}
-
-static IUnknownVtbl *get_delegating_vtbl(void)
-{
-    IUnknownVtbl *ret;
-
-    EnterCriticalSection(&delegating_vtbl_section);
-    current_vtbl.table->ref++;
-    ret = &current_vtbl.table->vtbl;
+        current_vtbl = table;
+    }
+
+    current_vtbl->ref++;
+    ret = &current_vtbl->vtbl;
     LeaveCriticalSection(&delegating_vtbl_section);
     return ret;
 }
@@ -253,22 +300,21 @@
     EnterCriticalSection(&delegating_vtbl_section);
     table->ref--;
     TRACE("ref now %d\n", table->ref);
-    if(table->ref == 0 && table != current_vtbl.table)
+    if(table->ref == 0 && table != current_vtbl)
     {
         TRACE("... and we're not current so free'ing\n");
-        VirtualFree(current_vtbl.table->methods, 0, MEM_RELEASE);
         HeapFree(GetProcessHeap(), 0, table);
     }
     LeaveCriticalSection(&delegating_vtbl_section);
 }
 
-HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
-                                                   LPUNKNOWN pUnkServer,
-                                                   PCInterfaceName name,
-                                                   CInterfaceStubVtbl *vtbl,
-                                                   REFIID delegating_iid,
-                                                   LPPSFACTORYBUFFER pPSFactory,
-                                                   LPRPCSTUBBUFFER *ppStub)
+HRESULT CStdStubBuffer_Delegating_Construct(REFIID riid,
+                                            LPUNKNOWN pUnkServer,
+                                            PCInterfaceName name,
+                                            CInterfaceStubVtbl *vtbl,
+                                            REFIID delegating_iid,
+                                            LPPSFACTORYBUFFER pPSFactory,
+                                            LPRPCSTUBBUFFER *ppStub)
 {
     cstdstubbuffer_delegating_t *This;
     IUnknown *pvServer;
@@ -294,7 +340,7 @@
         return E_OUTOFMEMORY;
     }
 
-    This->base_obj = get_delegating_vtbl();
+    This->base_obj = get_delegating_vtbl( vtbl->header.DispatchTableCount );
     r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
     if(FAILED(r))
     {

Modified: trunk/reactos/dll/win32/rpcrt4/ndr_ole.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/rpcrt4/ndr_ole.c?rev=46292&r1=46291&r2=46292&view=diff
==============================================================================
--- trunk/reactos/dll/win32/rpcrt4/ndr_ole.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/rpcrt4/ndr_ole.c [iso-8859-1] Sun Mar 21 11:53:17 2010
@@ -370,10 +370,10 @@
 }
 
 /***********************************************************************
- * Helper function to create a stub.
- * This probably looks very much like NdrpCreateStub.
- */
-HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
+ * Helper function to create a proxy.
+ * Probably similar to NdrpCreateProxy.
+ */
+HRESULT create_proxy(REFIID iid, IUnknown *pUnkOuter, IRpcProxyBuffer **pproxy, void **ppv)
 {
     CLSID clsid;
     IPSFactoryBuffer *psfac;
@@ -387,8 +387,32 @@
     r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
     if(FAILED(r)) return r;
 
-    r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);
+    r = IPSFactoryBuffer_CreateProxy(psfac, pUnkOuter, iid, pproxy, ppv);
 
     IPSFactoryBuffer_Release(psfac);
     return r;
 }
+
+/***********************************************************************
+ * Helper function to create a stub.
+ * This probably looks very much like NdrpCreateStub.
+ */
+HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
+{
+    CLSID clsid;
+    IPSFactoryBuffer *psfac;
+    HRESULT r;
+
+    if(!LoadCOM()) return E_FAIL;
+
+    r = COM_GetPSClsid( iid, &clsid );
+    if(FAILED(r)) return r;
+
+    r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
+    if(FAILED(r)) return r;
+
+    r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);
+
+    IPSFactoryBuffer_Release(psfac);
+    return r;
+}




More information about the Ros-diffs mailing list