1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <config_features.h>

#include <rtl/instance.hxx>
#include <osl/diagnose.h>
#include <osl/doublecheckedlocking.h>
#include <sal/log.hxx>
#include <uno/dispatcher.hxx>
#include <uno/lbnames.h>
#include <uno/mapping.hxx>
#include <cppuhelper/detail/XExceptionThrower.hpp>
#include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
#include <com/sun/star/ucb/NameClashException.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>

#include <cppuhelper/exc_hlp.hxx>

using namespace ::osl;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

namespace
{

using cppuhelper::detail::XExceptionThrower;


struct ExceptionThrower : public uno_Interface, XExceptionThrower
{
    ExceptionThrower();

    virtual ~ExceptionThrower() {}

    static Type const & getCppuType()
    {
        return cppu::UnoType<XExceptionThrower>::get();
    }

    // XInterface
    virtual Any SAL_CALL queryInterface( Type const & type ) override;
    virtual void SAL_CALL acquire() throw () override;
    virtual void SAL_CALL release() throw () override;

    // XExceptionThrower
    virtual void SAL_CALL throwException( Any const & exc ) override;
    virtual void SAL_CALL rethrowException() override;
};

extern "C"
{


void ExceptionThrower_acquire_release_nop(
    SAL_UNUSED_PARAMETER uno_Interface * )
{}


void ExceptionThrower_dispatch(
    uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
    void * pReturn, void * pArgs [], uno_Any ** ppException )
{
    OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );

    switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
                const_cast< typelib_TypeDescription * >( pMemberType ) )->
            nPosition)
    {
    case 0: // queryInterface()
    {
        Type const & rType_demanded =
            *static_cast< Type const * >( pArgs[ 0 ] );
        if (rType_demanded.equals( cppu::UnoType<XInterface>::get() ) ||
            rType_demanded.equals( ExceptionThrower::getCppuType() ))
        {
            typelib_TypeDescription * pTD = nullptr;
            TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
            uno_any_construct(
                static_cast< uno_Any * >( pReturn ), &pUnoI, pTD, nullptr );
            TYPELIB_DANGER_RELEASE( pTD );
        }
        else
        {
            uno_any_construct(
                static_cast< uno_Any * >( pReturn ), nullptr, nullptr, nullptr );
        }
        *ppException = nullptr;
        break;
    }
    case 1: // acquire()
    case 2: // release()
        *ppException = nullptr;
        break;
    case 3: // throwException()
    {
        uno_Any * pAny = static_cast< uno_Any * >( pArgs[ 0 ] );
        OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
        uno_type_any_construct( *ppException, pAny->pData, pAny->pType, nullptr );
        break;
    }
    default:
    {
        OSL_ASSERT( false );
        RuntimeException exc( "not implemented!" );
        uno_type_any_construct(
            *ppException, &exc, cppu::UnoType<decltype(exc)>::get().getTypeLibType(), nullptr );
        break;
    }
    }
}

} // extern "C"


Any ExceptionThrower::queryInterface( Type const & type )
{
    if (type.equals( cppu::UnoType<XInterface>::get() ) ||
        type.equals( ExceptionThrower::getCppuType() ))
    {
        XExceptionThrower * that = this;
        return Any( &that, type );
    }
    return Any();
}


void ExceptionThrower::acquire() throw ()
{
}

void ExceptionThrower::release() throw ()
{
}


void ExceptionThrower::throwException( Any const & exc )
{
    OSL_FAIL( "unexpected!" );
    cppu::throwException( exc );
}


void ExceptionThrower::rethrowException()<--- The function 'rethrowException' is never used.
{
    throw;
}


ExceptionThrower::ExceptionThrower()
{
    uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
    uno_Interface::release = ExceptionThrower_acquire_release_nop;
    uno_Interface::pDispatcher = ExceptionThrower_dispatch;
}

class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};

#if defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
// In the native iOS / Android app, where we don't have any Java, Python,
// BASIC, or other scripting, the only thing that would use the C++/UNO bridge
// functionality that invokes codeSnippet() was cppu::throwException().
//
// codeSnippet() is part of what corresponds to the code that uses
// run-time-generated machine code on other platforms. We can't generate code
// at run-time on iOS, that has been known forever.
//
// Instead of digging in and trying to understand what is wrong, another
// solution was chosen. It turns out that the number of types of exception
// objects thrown by cppu::throwException() is fairly small. During startup of
// the LibreOffice code, and loading of an .odt document, only one kind of
// exception is thrown this way... (The lovely
// css::ucb:InteractiveAugmentedIOException.)
//
// So we can simply have code that checks what the type of object being thrown
// is, and explicitly throws such an object then with a normal C++ throw
// statement. Seems to work.
template <class E> void tryThrow(css::uno::Any const& aException)
{
    E aSpecificException;
    if (aException >>= aSpecificException)
        throw aSpecificException;
}

void lo_mobile_throwException(css::uno::Any const& aException)
{
    assert(aException.getValueTypeClass() == css::uno::TypeClass_EXCEPTION);

    tryThrow<css::ucb::InteractiveAugmentedIOException>(aException);
    tryThrow<css::ucb::NameClashException>(aException);
    tryThrow<css::uno::RuntimeException>(aException);

    SAL_WARN("cppuhelper", "lo_mobile_throwException: Unhandled exception type: " << aException.getValueTypeName());

    assert(false);
}
#endif // defined(IOS) || (defined(__aarch64__) && defined(ANDROID))

} // anonymous namespace


namespace cppu
{


void SAL_CALL throwException( Any const & exc )
{
    if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
    {
        throw RuntimeException(
            "no UNO exception given "
            "(must be derived from com::sun::star::uno::Exception)!" );
    }

#if defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
    lo_mobile_throwException(exc);
#else
    Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
    if (! uno2cpp.is())
    {
        throw RuntimeException(
            "cannot get binary UNO to C++ mapping!" );
    }

    Reference< XExceptionThrower > xThrower;
    uno2cpp.mapInterface(
        reinterpret_cast< void ** >( &xThrower ),
        static_cast< uno_Interface * >( &theExceptionThrower::get() ),
        ExceptionThrower::getCppuType() );
    OSL_ASSERT( xThrower.is() );
    xThrower->throwException( exc );
#endif
}


Any SAL_CALL getCaughtException()
{
#if defined(__aarch64__) && defined(ANDROID)
    // FIXME This stuff works on 32bit ARM, let's use the shortcut only for
    // the 64bit ARM.
    return Any();
#else
    Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
    if (! cpp2uno.is())
    {
        throw RuntimeException(
            "cannot get C++ to binary UNO mapping!" );
    }
    Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
    if (! uno2cpp.is())
    {
        throw RuntimeException(
            "cannot get binary UNO to C++ mapping!" );
    }

    typelib_TypeDescription * pTD = nullptr;
    TYPELIB_DANGER_GET(
        &pTD, ExceptionThrower::getCppuType().getTypeLibType() );

    UnoInterfaceReference unoI;
    cpp2uno.mapInterface(
        reinterpret_cast< void ** >( &unoI.m_pUnoI ),
        static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
    OSL_ASSERT( unoI.is() );

    typelib_TypeDescription * pMemberTD = nullptr;
    TYPELIB_DANGER_GET(
        &pMemberTD,
        reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
        ppMembers[ 1 ] /* rethrowException() */ );

    uno_Any exc_mem;
    uno_Any * exc = &exc_mem;
    unoI.dispatch( pMemberTD, nullptr, nullptr, &exc );

    TYPELIB_DANGER_RELEASE( pMemberTD );
    TYPELIB_DANGER_RELEASE( pTD );

    if (exc == nullptr)
    {
        throw RuntimeException( "rethrowing C++ exception failed!" );
    }

    Any ret;
    uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
    uno_type_any_constructAndConvert(
        &ret, exc->pData, exc->pType, uno2cpp.get() );
    uno_any_destruct( exc, nullptr );
    return ret;
#endif
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */