📄 cstub.c
字号:
/* * COM stub (CStdStubBuffer) implementation * * Copyright 2001 Ove K鍁en, TransGaming Technologies * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */#include <stdarg.h>#define COBJMACROS#include "windef.h"#include "winbase.h"#include "winerror.h"#include "excpt.h"#include "objbase.h"#include "rpcproxy.h"#include "wine/debug.h"#include "wine/exception.h"#include "cpsf.h"WINE_DEFAULT_DEBUG_CHANNEL(ole);#define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])static WINE_EXCEPTION_FILTER(stub_filter){ if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION) return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_EXECUTE_HANDLER;}typedef struct{ IUnknownVtbl *base_obj; IRpcStubBuffer *base_stub; CStdStubBuffer stub_buffer;} cstdstubbuffer_delegating_t;static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface ){ 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){ CStdStubBuffer *This; IUnknown *pvServer; HRESULT r; TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name); TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid)); TRACE("vtbl=%p\n", &vtbl->Vtbl); if (!IsEqualGUID(vtbl->header.piid, riid)) { ERR("IID mismatch during stub creation\n"); return RPC_E_UNEXPECTED; } r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer); if(FAILED(r)) return r; This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer)); if (!This) { IUnknown_Release(pvServer); return E_OUTOFMEMORY; } This->lpVtbl = &vtbl->Vtbl; This->RefCount = 1; This->pvServerObject = pvServer; This->pPSFactory = pPSFactory; *ppStub = (LPRPCSTUBBUFFER)This; IPSFactoryBuffer_AddRef(pPSFactory); return S_OK;}static CRITICAL_SECTION delegating_vtbl_section;static CRITICAL_SECTION_DEBUG critsect_debug ={ 0, 0, &delegating_vtbl_section, { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList }, 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }};static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };typedef struct{ DWORD ref; IUnknownVtbl vtbl;} ref_counted_vtbl;static struct{ ref_counted_vtbl *table; DWORD size;} current_vtbl;static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv){ *ppv = (void *)pUnk; return S_OK;}static ULONG WINAPI delegating_AddRef(IUnknown *pUnk){ return 1;}static ULONG WINAPI delegating_Release(IUnknown *pUnk){ return 1;}#if defined(__i386__)/* The idea here is to replace the first param on the stack ie. This (which will point to cstdstubbuffer_delegating_t) with This->stub_buffer.pvServerObject and then jump to the relevant offset in This->stub_buffer.pvServerObject's vtbl.*/#include "pshpack1.h"typedef struct { DWORD mov1; /* mov 0x4(%esp), %eax 8b 44 24 04 */ WORD mov2; /* mov 0x10(%eax), %eax 8b 40 */ BYTE sixteen; /* 10 */ DWORD mov3; /* mov %eax, 0x4(%esp) 89 44 24 04 */ WORD mov4; /* mov (%eax), %eax 8b 00 */ WORD mov5; /* mov offset(%eax), %eax 8b 80 */ DWORD offset; /* xx xx xx xx */ WORD jmp; /* jmp *%eax ff e0 */ BYTE pad[3]; /* lea 0x0(%esi), %esi 8d 76 00 */} vtbl_method_t;#include "poppack.h"static void fill_table(IUnknownVtbl *vtbl, DWORD num){ vtbl_method_t *method; void **entry; DWORD i; vtbl->QueryInterface = delegating_QueryInterface; vtbl->AddRef = delegating_AddRef; vtbl->Release = delegating_Release; method = (vtbl_method_t*)((void **)vtbl + num); 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++; }}#else /* __i386__ */typedef struct {int dummy;} vtbl_method_t;static void fill_table(IUnknownVtbl *vtbl, DWORD num){ ERR("delegated stubs are not supported on this architecture\n");}#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; } EnterCriticalSection(&delegating_vtbl_section); if(num_methods > current_vtbl.size) { DWORD size; if(current_vtbl.table && current_vtbl.table->ref == 0) { TRACE("freeing old table\n"); HeapFree(GetProcessHeap(), 0, current_vtbl.table); } size = sizeof(DWORD) + num_methods * sizeof(void*) + (num_methods - 3) * sizeof(vtbl_method_t); current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, size); fill_table(¤t_vtbl.table->vtbl, num_methods); current_vtbl.table->ref = 0; current_vtbl.size = num_methods; } LeaveCriticalSection(&delegating_vtbl_section);}static IUnknownVtbl *get_delegating_vtbl(void){ IUnknownVtbl *ret; EnterCriticalSection(&delegating_vtbl_section); current_vtbl.table->ref++; ret = ¤t_vtbl.table->vtbl; LeaveCriticalSection(&delegating_vtbl_section); return ret;}static void release_delegating_vtbl(IUnknownVtbl *vtbl){ ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1); EnterCriticalSection(&delegating_vtbl_section); table->ref--; TRACE("ref now %d\n", table->ref); if(table->ref == 0 && table != current_vtbl.table) { TRACE("... and we're not current so free'ing\n"); 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){ cstdstubbuffer_delegating_t *This; IUnknown *pvServer; HRESULT r; TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name); TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid)); TRACE("vtbl=%p\n", &vtbl->Vtbl); if (!IsEqualGUID(vtbl->header.piid, riid)) { ERR("IID mismatch during stub creation\n"); return RPC_E_UNEXPECTED; } r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer); if(FAILED(r)) return r; This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This)); if (!This) { IUnknown_Release(pvServer); return E_OUTOFMEMORY; } This->base_obj = get_delegating_vtbl(); r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub); if(FAILED(r)) { release_delegating_vtbl(This->base_obj); HeapFree(GetProcessHeap(), 0, This); IUnknown_Release(pvServer); return r;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -