LibreOffice Module bridges (master) 1
msvc_win32_intel/except.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21#include <sal/config.h>
22#include <malloc.h>
23#include <typeinfo>
24#include <signal.h>
25
26#include <rtl/alloc.h>
27#include <rtl/strbuf.hxx>
28#include <rtl/ustrbuf.hxx>
29#include <sal/log.hxx>
30#include <osl/mutex.hxx>
31
32#include <com/sun/star/uno/Any.hxx>
33#include <unordered_map>
34#include <msvc/x86.hxx>
35#include <except.hxx>
36
37#pragma pack(push, 8)
38
39using namespace ::com::sun::star;
40
41void * ObjectFunction::operator new ( size_t nSize )
42{
43 void * pMem = std::malloc( nSize );
44 if (pMem != 0)
45 {
46 DWORD old_protect;
47 BOOL success =
48 VirtualProtect(pMem, nSize, PAGE_EXECUTE_READWRITE, &old_protect);
49 (void) success;
50 assert(success && "VirtualProtect() failed!");
51 }
52 return pMem;
53}
54
55void ObjectFunction::operator delete ( void * pMem )
56{
57 std::free( pMem );
58}
59
60ObjectFunction::ObjectFunction( typelib_TypeDescription * pTypeDescr, void * fpFunc ) throw ()
61 : _pTypeDescr( pTypeDescr )
62{
63 ::typelib_typedescription_acquire( _pTypeDescr );
64
65 unsigned char * pCode = (unsigned char *)somecode;
66 // a must be!
67 assert((void *)this == (void *)pCode);
68
69 // push ObjectFunction this
70 *pCode++ = 0x68;
71 *(void **)pCode = this;
72 pCode += sizeof(void *);
73 // jmp rel32 fpFunc
74 *pCode++ = 0xe9;
75 *(sal_Int32 *)pCode = ((unsigned char *)fpFunc) - pCode - sizeof(sal_Int32);
76}
77
79{
80 ::typelib_typedescription_release( _pTypeDescr );
81}
82
83static void * __cdecl __copyConstruct( void * pExcThis, void * pSource, ObjectFunction * pThis )
84 throw ()
85{
86 ::uno_copyData(pExcThis, pSource, pThis->_pTypeDescr, uno::cpp_acquire);
87 return pExcThis;
88}
89
90static void * __cdecl __destruct( void * pExcThis, ObjectFunction * pThis )
91 throw ()
92{
93 ::uno_destructData(pExcThis, pThis->_pTypeDescr, uno::cpp_release);
94 return pExcThis;
95}
96
97// these are non virtual object methods; there is no this ptr on stack => ecx supplies _this_ ptr
98
99static __declspec(naked) void copyConstruct() throw ()
100{
101 __asm
102 {
103 // ObjectFunction this already on stack
104 push [esp+8] // source exc object this
105 push ecx // exc object
106 call __copyConstruct
107 add esp, 12 // + ObjectFunction this
108 ret 4
109 }
110}
111
112static __declspec(naked) void destruct() throw ()
113{
114 __asm
115 {
116 // ObjectFunction this already on stack
117 push ecx // exc object
118 call __destruct
119 add esp, 8 // + ObjectFunction this
120 ret
121 }
122}
123
125 : _n0( 0 )
126 , _n1( 0 )
127 , _n2( -1 )
128 , _n3( 0 )
129 , _n4( pTypeDescr->nSize )
130 , _pCopyCtor( new ObjectFunction( pTypeDescr, copyConstruct ) )
131 , _n5( 0 )
132{
133 _pTypeInfo = RTTInfos::get(pTypeDescr->pTypeName);
134}
135
137{
138 delete _pCopyCtor;
139}
140
141RaiseInfo::RaiseInfo( typelib_TypeDescription * pTypeDescr ) throw ()
142 : _n0( 0 )
143 , _pDtor( new ObjectFunction( pTypeDescr, destruct ) )
144 , _n2( 0 )
145 , _n3( 0 )
146 , _n4( 0 )
147{
148 // a must be
149 static_assert(sizeof(sal_Int32) == sizeof(ExceptionType *), "### pointer size differs from sal_Int32!");
150
151 typelib_CompoundTypeDescription * pCompTypeDescr;
152
153 // info count
154 sal_Int32 nLen = 0;
155 for ( pCompTypeDescr = (typelib_CompoundTypeDescription*)pTypeDescr;
156 pCompTypeDescr; pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
157 {
158 ++nLen;
159 }
160
161 // info count accompanied by type info ptrs: type, base type, base base type, ...
162 _types = std::malloc( sizeof(sal_Int32) + (sizeof(ExceptionType *) * nLen) );
163 *(sal_Int32 *)_types = nLen;
164
165 ExceptionType ** ppTypes = (ExceptionType **)((sal_Int32 *)_types + 1);
166
167 sal_Int32 nPos = 0;
168 for ( pCompTypeDescr = (typelib_CompoundTypeDescription*)pTypeDescr;
169 pCompTypeDescr; pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
170 {
171 ppTypes[nPos++] = new ExceptionType( (typelib_TypeDescription *)pCompTypeDescr );
172 }
173}
174
176{
177 ExceptionType ** ppTypes = (ExceptionType **)((sal_Int32 *)_types + 1);
178 for ( sal_Int32 nTypes = *(sal_Int32 *)_types; nTypes--; )
179 {
180 delete ppTypes[nTypes];
181 }
182 std::free( _types );
183
184 delete _pDtor;
185}
186
187#pragma pack(pop)
188
189/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static type_info * get(OUString const &rUNOname, int *len=nullptr) noexcept
sal_uInt16 nPos
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
static void *__cdecl destruct(void *pExcThis, typelib_TypeDescription *pTD) noexcept
static void *__cdecl copyConstruct(void *pExcThis, void *pSource, typelib_TypeDescription *pTD) noexcept
static void *__cdecl __destruct(void *pExcThis, ObjectFunction *pThis)
static void *__cdecl __copyConstruct(void *pExcThis, void *pSource, ObjectFunction *pThis)
static __declspec(naked) void copyConstruct()
const wchar_t *typedef BOOL
sal_uInt32 _pCopyCtor
Definition: amd64.hxx:32
ExceptionType(unsigned char *pCode, sal_uInt64 pCodeBase, typelib_TypeDescription *pTD) noexcept
ObjectFunction(typelib_TypeDescription *pTypeDescr, void *fpFunc)
typelib_TypeDescription * _pTypeDescr
Definition: x86.hxx:29
sal_uInt32 _types
Definition: amd64.hxx:47
sal_uInt32 _pDtor
Definition: amd64.hxx:45
RaiseInfo(typelib_TypeDescription *pTD) noexcept