Files
libreoffice/basic/source/classes/errobject.cxx
Mike Kaganski 3ebf6a090b Avoid crash in shutdown when accessing already destroyed BASIC_DLL
... as seen in https://ci.libreoffice.org/job/gerrit_linux_gcc_release/48425/console

The problem was that SbxErrObject::getErrObject created a static object
which called GetSbxData_Impl() in its destructor, which in turn accessed
BASIC_DLL, which was invalidated by deleting BasicDLL objects either in
SfxApplication::~SfxApplication or in MacroSnippet::~MacroSnippet, thus
already invalid when static was destroyed.

So fix this by creating the global error object in the SbxAppData, not
as function-local static, so that its lifetime is limited to the data.

The crash happened also on Windows, but was somehow masked (possibly by
custom error handler?), with this call stack:

sblo.dll!std::unique_ptr<SbxAppData,std::default_delete<SbxAppData>>::operator*() Line 1886
	at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\memory(1886)
sblo.dll!GetSbxData_Impl() Line 110
	at C:\lo\src\core\basic\source\runtime\basrdll.cxx(110)
sblo.dll!SbxBase::GetError() Line 96
	at C:\lo\src\core\basic\source\sbx\sbxbase.cxx(96)
sblo.dll!SbxValue::Put(const SbxValues & rVal) Line 414
	at C:\lo\src\core\basic\source\sbx\sbxvalue.cxx(414)
sblo.dll!SbxValue::Clear() Line 179
	at C:\lo\src\core\basic\source\sbx\sbxvalue.cxx(179)
sblo.dll!SbxValue::~SbxValue() Line 139
	at C:\lo\src\core\basic\source\sbx\sbxvalue.cxx(139)
sblo.dll!SbxVariable::~SbxVariable() Line 125
	at C:\lo\src\core\basic\source\sbx\sbxvar.cxx(125)
sblo.dll!SbxProperty::~SbxProperty() Line 861
	at C:\lo\src\core\basic\source\sbx\sbxobj.cxx(861)
sblo.dll!SbUnoProperty::~SbUnoProperty() Line 2591
	at C:\lo\src\core\basic\source\classes\sbunoobj.cxx(2591)
sblo.dll!SbUnoProperty::`vbase destructor'()
sblo.dll!SbUnoProperty::`scalar deleting destructor'(unsigned int)
tllo.dll!SvRefBase::ReleaseRef() Line 163
	at C:\lo\src\core\include\tools\ref.hxx(163)
sblo.dll!tools::SvRef<SbxVariable>::~SvRef<SbxVariable>() Line 56
	at C:\lo\src\core\include\tools\ref.hxx(56)
sblo.dll!SbxVarEntry::~SbxVarEntry()
sblo.dll!SbxVarEntry::`scalar deleting destructor'(unsigned int)
sblo.dll!std::_Default_allocator_traits<std::allocator<SbxVarEntry>>::destroy<SbxVarEntry>(std::allocator<SbxVarEntry> & __formal, SbxVarEntry * const _Ptr) Line 677
	at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory(677)
sblo.dll!std::_Destroy_range<std::allocator<SbxVarEntry>>(SbxVarEntry * _First, SbxVarEntry * const _Last, std::allocator<SbxVarEntry> & _Al) Line 951
	at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory(951)
sblo.dll!std::vector<SbxVarEntry,std::allocator<SbxVarEntry>>::_Destroy(SbxVarEntry * _First, SbxVarEntry * _Last) Line 1616
	at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\vector(1616)
sblo.dll!std::vector<SbxVarEntry,std::allocator<SbxVarEntry>>::_Tidy() Line 1698
	at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\vector(1698)
sblo.dll!std::vector<SbxVarEntry,std::allocator<SbxVarEntry>>::~vector<SbxVarEntry,std::allocator<SbxVarEntry>>() Line 674
	at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\vector(674)
sblo.dll!SbxArray::~SbxArray() Line 75
	at C:\lo\src\core\basic\source\sbx\sbxarray.cxx(75)
sblo.dll!SbxArray::`vbase destructor'()
sblo.dll!SbxArray::`vector deleting destructor'(unsigned int)
tllo.dll!SvRefBase::ReleaseRef() Line 163
	at C:\lo\src\core\include\tools\ref.hxx(163)
sblo.dll!tools::SvRef<SbxArray>::~SvRef<SbxArray>() Line 56
	at C:\lo\src\core\include\tools\ref.hxx(56)
sblo.dll!SbxObject::~SbxObject() Line 111
	at C:\lo\src\core\basic\source\sbx\sbxobj.cxx(111)
sblo.dll!SbUnoObject::~SbUnoObject() Line 2387
	at C:\lo\src\core\basic\source\classes\sbunoobj.cxx(2387)
sblo.dll!SbxErrObject::~SbxErrObject() Line 183
	at C:\lo\src\core\basic\source\classes\errobject.cxx(183)
sblo.dll!SbxErrObject::`vbase destructor'()
sblo.dll!SbxErrObject::`scalar deleting destructor'(unsigned int)
tllo.dll!SvRefBase::ReleaseRef() Line 163
	at C:\lo\src\core\include\tools\ref.hxx(163)
sblo.dll!tools::SvRef<SbxVariable>::~SvRef<SbxVariable>() Line 56
	at C:\lo\src\core\include\tools\ref.hxx(56)
sblo.dll!`SbxErrObject::getErrObject'::`2'::`dynamic atexit destructor for 'pGlobErr''()
ucrtbased.dll!_execute_onexit_table::__l2::<lambda>() Line 206
	at minkernel\crts\ucrt\src\appcrt\startup\onexit.cpp(206)
ucrtbased.dll!__crt_seh_guarded_call<int>::operator()<void <lambda>(void),int <lambda>(void) &,void <lambda>(void)>(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, _execute_onexit_table::__l2::int <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 204
	at vccrt\vcruntime\inc\internal_shared.h(204)
ucrtbased.dll!__acrt_lock_and_call<int <lambda>(void)>(const __acrt_lock_id lock_id, _execute_onexit_table::__l2::int <lambda>(void) && action) Line 975
	at minkernel\crts\ucrt\inc\corecrt_internal.h(975)
ucrtbased.dll!_execute_onexit_table(_onexit_table_t * table) Line 231
	at minkernel\crts\ucrt\src\appcrt\startup\onexit.cpp(231)
sblo.dll!__scrt_dllmain_uninitialize_c() Line 399
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\utility\utility.cpp(399)
sblo.dll!dllmain_crt_process_detach(const bool is_terminating) Line 182
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\dll_dllmain.cpp(182)
sblo.dll!dllmain_crt_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 220
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\dll_dllmain.cpp(220)
sblo.dll!dllmain_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 293
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\dll_dllmain.cpp(293)
sblo.dll!_DllMainCRTStartup(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 335
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\dll_dllmain.cpp(335)
ntdll.dll!LdrpCallInitRoutine()
ntdll.dll!LdrpProcessDetachNode()
ntdll.dll!LdrpUnloadNode()
ntdll.dll!LdrpUnloadNode()
ntdll.dll!LdrpUnloadNode()
ntdll.dll!LdrpDecrementModuleLoadCountEx()
ntdll.dll!LdrUnloadDll()
KernelBase.dll!FreeLibrary()
cppunitd_dll.dll!CppUnit::DynamicLibraryManager::doReleaseLibrary() Line 30
	at C:\lo\src\core\workdir\UnpackedTarball\cppunit\src\cppunit\Win32DynamicLibraryManager.cpp(30)
cppunitd_dll.dll!CppUnit::DynamicLibraryManager::releaseLibrary() Line 69
	at C:\lo\src\core\workdir\UnpackedTarball\cppunit\src\cppunit\DynamicLibraryManager.cpp(69)
cppunitd_dll.dll!CppUnit::DynamicLibraryManager::~DynamicLibraryManager() Line 19
	at C:\lo\src\core\workdir\UnpackedTarball\cppunit\src\cppunit\DynamicLibraryManager.cpp(19)
cppunitd_dll.dll!CppUnit::DynamicLibraryManager::`scalar deleting destructor'(unsigned int)
cppunitd_dll.dll!CppUnit::PlugInManager::unload(CppUnit::PlugInManager::PlugInInfo & plugIn) Line 83
	at C:\lo\src\core\workdir\UnpackedTarball\cppunit\src\cppunit\PlugInManager.cpp(83)
cppunitd_dll.dll!CppUnit::PlugInManager::~PlugInManager() Line 24
	at C:\lo\src\core\workdir\UnpackedTarball\cppunit\src\cppunit\PlugInManager.cpp(24)
cppunittester.exe!`anonymous namespace'::ProtectedFixtureFunctor::run() Line 327
	at C:\lo\src\core\sal\cppunittester\cppunittester.cxx(327)
cppunittester.exe!sal_main() Line 466
	at C:\lo\src\core\sal\cppunittester\cppunittester.cxx(466)
cppunittester.exe!main(int argc, char * * argv) Line 373
	at C:\lo\src\core\sal\cppunittester\cppunittester.cxx(373)
cppunittester.exe!invoke_main() Line 79
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(79)
cppunittester.exe!__scrt_common_main_seh() Line 288
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288)
cppunittester.exe!__scrt_common_main() Line 331
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(331)
cppunittester.exe!mainCRTStartup() Line 17
	at d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp(17)
kernel32.dll!BaseThreadInitThunk()
ntdll.dll!RtlUserThreadStart()

Change-Id: I597dc242f71d220bbb42aef2611e4fd7e20a7be6
Reviewed-on: https://gerrit.libreoffice.org/85586
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
2019-12-21 08:12:38 +01:00

216 lines
6.0 KiB
C++

/* -*- 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 <errobject.hxx>
#include <sbxbase.hxx>
#include <cppuhelper/implbase.hxx>
#include <com/sun/star/script/XDefaultProperty.hpp>
#include <sbintern.hxx>
#include <runtime.hxx>
using namespace ::com::sun::star;
using namespace ::ooo;
class ErrObject : public ::cppu::WeakImplHelper< vba::XErrObject,
script::XDefaultProperty >
{
OUString m_sHelpFile;
OUString m_sSource;
OUString m_sDescription;
sal_Int32 m_nNumber;
sal_Int32 m_nHelpContext;
public:
ErrObject();
// Attributes
virtual ::sal_Int32 SAL_CALL getNumber() override;
virtual void SAL_CALL setNumber( ::sal_Int32 _number ) override;
virtual ::sal_Int32 SAL_CALL getHelpContext() override;
virtual void SAL_CALL setHelpContext( ::sal_Int32 _helpcontext ) override;
virtual OUString SAL_CALL getHelpFile() override;
virtual void SAL_CALL setHelpFile( const OUString& _helpfile ) override;
virtual OUString SAL_CALL getDescription() override;
virtual void SAL_CALL setDescription( const OUString& _description ) override;
virtual OUString SAL_CALL getSource() override;
virtual void SAL_CALL setSource( const OUString& _source ) override;
// Methods
virtual void SAL_CALL Clear( ) override;
virtual void SAL_CALL Raise( const uno::Any& Number, const uno::Any& Source, const uno::Any& Description, const uno::Any& HelpFile, const uno::Any& HelpContext ) override;
// XDefaultProperty
virtual OUString SAL_CALL getDefaultPropertyName( ) override;
// Helper method
/// @throws css::uno::RuntimeException
void setData( const uno::Any& Number, const uno::Any& Source, const uno::Any& Description,
const uno::Any& HelpFile, const uno::Any& HelpContext );
};
ErrObject::ErrObject() : m_nNumber(0), m_nHelpContext(0)
{
}
sal_Int32 SAL_CALL
ErrObject::getNumber()
{
return m_nNumber;
}
void SAL_CALL
ErrObject::setNumber( ::sal_Int32 _number )
{
GetSbData()->pInst->setErrorVB( _number );
OUString _description = GetSbData()->pInst->GetErrorMsg();
setData( uno::Any( _number ), uno::Any(), uno::Any( _description ), uno::Any(), uno::Any() );
}
::sal_Int32 SAL_CALL
ErrObject::getHelpContext()
{
return m_nHelpContext;
}
void SAL_CALL
ErrObject::setHelpContext( ::sal_Int32 _helpcontext )
{
m_nHelpContext = _helpcontext;
}
OUString SAL_CALL
ErrObject::getHelpFile()
{
return m_sHelpFile;
}
void SAL_CALL
ErrObject::setHelpFile( const OUString& _helpfile )
{
m_sHelpFile = _helpfile;
}
OUString SAL_CALL
ErrObject::getDescription()
{
return m_sDescription;
}
void SAL_CALL
ErrObject::setDescription( const OUString& _description )
{
m_sDescription = _description;
}
OUString SAL_CALL
ErrObject::getSource()
{
return m_sSource;
}
void SAL_CALL
ErrObject::setSource( const OUString& _source )
{
m_sSource = _source;
}
// Methods
void SAL_CALL
ErrObject::Clear( )
{
m_sHelpFile.clear();
m_sSource = m_sHelpFile;
m_sDescription = m_sSource;
m_nNumber = 0;
m_nHelpContext = 0;
}
void SAL_CALL
ErrObject::Raise( const uno::Any& Number, const uno::Any& Source, const uno::Any& Description, const uno::Any& HelpFile, const uno::Any& HelpContext )
{
setData( Number, Source, Description, HelpFile, HelpContext );
if ( m_nNumber )
GetSbData()->pInst->ErrorVB( m_nNumber, m_sDescription );
}
// XDefaultProperty
OUString SAL_CALL
ErrObject::getDefaultPropertyName( )
{
return "Number";
}
void ErrObject::setData( const uno::Any& Number, const uno::Any& Source, const uno::Any& Description, const uno::Any& HelpFile, const uno::Any& HelpContext )
{
if ( !Number.hasValue() )
throw uno::RuntimeException("Missing Required Parameter" );
Number >>= m_nNumber;
Description >>= m_sDescription;
Source >>= m_sSource;
HelpFile >>= m_sHelpFile;
HelpContext >>= m_nHelpContext;
}
// SbxErrObject
SbxErrObject::SbxErrObject( const OUString& rName, const uno::Any& rUnoObj )
: SbUnoObject( rName, rUnoObj )
, m_pErrObject( nullptr )
{
rUnoObj >>= m_xErr;
if ( m_xErr.is() )
{
SetDfltProperty( uno::Reference< script::XDefaultProperty >( m_xErr, uno::UNO_QUERY_THROW )->getDefaultPropertyName() ) ;
m_pErrObject = static_cast< ErrObject* >( m_xErr.get() );
}
}
SbxErrObject::~SbxErrObject()
{
}
uno::Reference< vba::XErrObject > const &
SbxErrObject::getUnoErrObject()
{
SbxErrObject* pGlobErr = static_cast< SbxErrObject* >( getErrObject().get() );
return pGlobErr->m_xErr;
}
SbxVariableRef const &
SbxErrObject::getErrObject()
{
SbxVariableRef& rGlobErr = GetSbxData_Impl().m_aGlobErr;
{
static osl::Mutex aMutex;
osl::MutexGuard aGuard(aMutex);
if (!rGlobErr)
rGlobErr = new SbxErrObject("Err",
uno::Any(uno::Reference<vba::XErrObject>(new ErrObject())));
}
return rGlobErr;
}
void SbxErrObject::setNumberAndDescription( ::sal_Int32 _number, const OUString& _description )
{
if( m_pErrObject != nullptr )
{
m_pErrObject->setData( uno::Any( _number ), uno::Any(), uno::Any( _description ), uno::Any(), uno::Any() );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */