Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/bridges/source/cpp_uno/shared/cppinterfaceproxy.cxx
Line
Count
Source (jump to first uncovered line)
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
#include <cppinterfaceproxy.hxx>
21
22
#include <bridge.hxx>
23
#include <utility>
24
#include <vtablefactory.hxx>
25
26
#include <com/sun/star/uno/XInterface.hpp>
27
#include <typelib/typedescription.h>
28
29
#include <cstddef>
30
#include <memory>
31
#include <new>
32
33
namespace bridges::cpp_uno::shared {
34
35
void freeCppInterfaceProxy(uno_ExtEnvironment * pEnv, void * pInterface)
36
0
{
37
0
    CppInterfaceProxy * pThis = CppInterfaceProxy::castInterfaceToProxy(
38
0
        pInterface);
39
0
    if (pEnv != pThis->pBridge->getCppEnv()) {
40
0
        assert(false);
41
0
    }
42
43
0
    (*pThis->pBridge->getUnoEnv()->revokeInterface)(
44
0
        pThis->pBridge->getUnoEnv(), pThis->pUnoI );
45
0
    (*pThis->pUnoI->release)( pThis->pUnoI );
46
0
    ::typelib_typedescription_release(
47
0
        &pThis->pTypeDescr->aBase );
48
0
    pThis->pBridge->release();
49
50
#if OSL_DEBUG_LEVEL > 1
51
    *(int *)pInterface = 0xdeadbabe;
52
#endif
53
0
    pThis->~CppInterfaceProxy();
54
0
    delete[] reinterpret_cast< char * >(pThis);
55
0
}
56
57
css::uno::XInterface * CppInterfaceProxy::create(
58
    bridges::cpp_uno::shared::Bridge * pBridge, uno_Interface * pUnoI,
59
    typelib_InterfaceTypeDescription * pTypeDescr, OUString const & rOId)
60
0
{
61
0
    typelib_typedescription_complete(
62
0
        reinterpret_cast< typelib_TypeDescription ** >(&pTypeDescr));
63
0
    static bridges::cpp_uno::shared::VtableFactory factory;
64
0
    const bridges::cpp_uno::shared::VtableFactory::Vtables& rVtables(
65
0
        factory.getVtables(pTypeDescr));
66
0
    std::unique_ptr< char[] > pMemory(
67
0
        new char[
68
0
            sizeof (CppInterfaceProxy)
69
0
            + (rVtables.count - 1) * sizeof (void **)]);
70
0
    new(pMemory.get()) CppInterfaceProxy(pBridge, pUnoI, pTypeDescr, rOId);
71
0
    CppInterfaceProxy * pProxy = reinterpret_cast< CppInterfaceProxy * >(
72
0
        pMemory.release());
73
0
    for (sal_Int32 i = 0; i < rVtables.count; ++i) {
74
0
        pProxy->vtables[i] = VtableFactory::mapBlockToVtable(
75
0
            rVtables.blocks[i].start);
76
0
    }
77
    // coverity[leaked_storage : FALSE] - see freeCppInterfaceProxy
78
0
    return castProxyToInterface(pProxy);
79
0
}
80
81
void CppInterfaceProxy::acquireProxy()
82
0
{
83
0
    if (++nRef == 1)
84
0
    {
85
        // rebirth of proxy zombie
86
        // register at cpp env
87
0
        void * pThis = castProxyToInterface( this );
88
0
        (*pBridge->getCppEnv()->registerProxyInterface)(
89
0
            pBridge->getCppEnv(), &pThis, freeCppInterfaceProxy, oid.pData,
90
0
            pTypeDescr );
91
0
        assert(pThis == castProxyToInterface(this));
92
0
    }
93
0
}
94
95
void CppInterfaceProxy::releaseProxy()
96
0
{
97
0
    if (! --nRef ) // last release
98
0
    {
99
        // revoke from cpp env
100
0
        (*pBridge->getCppEnv()->revokeInterface)(
101
0
            pBridge->getCppEnv(), castProxyToInterface( this ) );
102
0
    }
103
0
}
104
105
CppInterfaceProxy::CppInterfaceProxy(
106
    bridges::cpp_uno::shared::Bridge * pBridge_, uno_Interface * pUnoI_,
107
    typelib_InterfaceTypeDescription * pTypeDescr_, OUString aOId_)
108
0
    : nRef( 1 )
109
0
    , pBridge( pBridge_ )
110
0
    , pUnoI( pUnoI_ )
111
0
    , pTypeDescr( pTypeDescr_ )
112
0
    , oid(std::move( aOId_ ))
113
0
{
114
0
    pBridge->acquire();
115
0
    ::typelib_typedescription_acquire( &pTypeDescr->aBase );
116
0
    (*pUnoI->acquire)( pUnoI );
117
0
    (*pBridge->getUnoEnv()->registerInterface)(
118
0
        pBridge->getUnoEnv(), reinterpret_cast< void ** >( &pUnoI ), oid.pData,
119
0
        pTypeDescr );
120
0
}
121
122
CppInterfaceProxy::~CppInterfaceProxy()
123
0
{}
124
125
css::uno::XInterface * CppInterfaceProxy::castProxyToInterface(
126
    CppInterfaceProxy * pProxy)
127
0
{
128
0
    return reinterpret_cast< css::uno::XInterface * >(
129
0
        &pProxy->vtables);
130
0
}
131
132
CppInterfaceProxy * CppInterfaceProxy::castInterfaceToProxy(void * pInterface)
133
0
{
134
    // pInterface == &pProxy->vtables (this emulated offsetof is not truly
135
    // portable):
136
0
    char const * const base = reinterpret_cast< char const * >(16);
137
0
    std::ptrdiff_t const offset = reinterpret_cast< char const * >(
138
0
        &reinterpret_cast< CppInterfaceProxy const * >(base)->vtables) - base;
139
0
    return reinterpret_cast< CppInterfaceProxy * >(
140
0
        static_cast< char * >(pInterface) - offset);
141
0
}
142
143
}
144
145
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */