1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
32 #include <basic/sbx.hxx>
33 #include <basic/sbxvar.hxx>
34 #include <runtime.hxx>
35 #include <osl/thread.h>
36 #include <osl/diagnose.h>
37 #include <rtl/ref.hxx>
38 #include <rtl/string.hxx>
39 #include <rtl/ustring.hxx>
40 #include <sal/log.hxx>
41 #include <salhelper/simplereferenceobject.hxx>
42 #include <o3tl/char16_t2wchar_t.hxx>
52 Missing support for functions returning structs (see TODO in call()).
54 Missing support for additional data types (64 bit integers, Any, ...; would
55 trigger assert(false) in various switches).
57 It is assumed that the variables passed into SbiDllMgr::Call to represent
58 the arguments and return value have types that exactly match the Declare
59 statement; it would be better if this code had access to the function
60 signature from the Declare statement, so that it could convert the passed
61 variables accordingly.
66 char * address(std::vector
< char > & blob
) {
67 return blob
.empty() ? nullptr : blob
.data();
70 ErrCode
convert(OUString
const & source
, OString
* target
) {
72 source
.convertToString(
73 target
, osl_getThreadTextEncoding(),
74 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
75 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
))
76 ? ERRCODE_NONE
: ERRCODE_BASIC_BAD_ARGUMENT
;
77 //TODO: more specific errcode?
80 ErrCode
convert(char const * source
, sal_Int32 length
, OUString
* target
) {
82 rtl_convertStringToUString(
83 &target
->pData
, source
, length
, osl_getThreadTextEncoding(),
84 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
|
85 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
|
86 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR
))
87 ? ERRCODE_NONE
: ERRCODE_BASIC_BAD_ARGUMENT
;
88 //TODO: more specific errcode?
91 struct UnmarshalData
{
92 UnmarshalData(SbxVariable
* theVariable
, void * theBuffer
):
93 variable(theVariable
), buffer(theBuffer
) {}
95 SbxVariable
* variable
;
99 struct StringData
: public UnmarshalData
{
100 StringData(SbxVariable
* theVariable
, void * theBuffer
, bool theSpecial
):
101 UnmarshalData(theVariable
, theBuffer
), special(theSpecial
) {}
108 MarshalData() = default;
109 MarshalData(const MarshalData
&) = delete;
110 const MarshalData
& operator=(const MarshalData
&) = delete;
112 std::vector
< char > * newBlob() {
113 blobs_
.push_back(std::vector
< char >());
114 return &blobs_
.back();
117 std::vector
< UnmarshalData
> unmarshal
;
119 std::vector
< StringData
> unmarshalStrings
;
122 std::vector
< std::vector
< char > > blobs_
;
125 std::size_t align(std::size_t address
, std::size_t alignment
) {
126 // alignment = 2^k for some k >= 0
127 return (address
+ (alignment
- 1)) & ~(alignment
- 1);
131 std::vector
< char > & blob
, std::size_t alignment
, std::size_t offset
,
134 std::vector
< char >::size_type n
= blob
.size();
135 n
= align(n
- offset
, alignment
) + offset
; //TODO: overflow in align()
136 blob
.resize(n
+ add
); //TODO: overflow
137 return address(blob
) + n
;
140 template< typename T
> void add(
141 std::vector
< char > & blob
, T
const & data
, std::size_t alignment
,
144 *reinterpret_cast< T
* >(align(blob
, alignment
, offset
, sizeof (T
))) = data
;
147 std::size_t alignment(SbxVariable
const * variable
) {
148 assert(variable
!= nullptr);
149 if ((variable
->GetType() & SbxARRAY
) == 0) {
150 switch (variable
->GetType()) {
162 SbxObject
* pobj
= dynamic_cast<SbxObject
*>(variable
->GetObject());
164 SbxArray
* props
= pobj
->GetProperties();
165 for (sal_uInt32 i
= 0; i
< props
->Count(); ++i
)
167 n
= std::max(n
, alignment(props
->Get(i
)));
179 SbxDimArray
* arr
= dynamic_cast<SbxDimArray
*>( variable
->GetObject() );
181 sal_Int32 dims
= arr
->GetDims();
182 std::vector
< sal_Int32
> low(dims
);
183 for (sal_Int32 i
= 0; i
< dims
; ++i
) {
185 arr
->GetDim(i
+ 1, low
[i
], up
);
187 return alignment(arr
->Get(low
.data()));
192 bool outer
, SbxVariable
* variable
, bool special
,
193 std::vector
< char > & blob
, std::size_t offset
, MarshalData
& data
);
195 ErrCode
marshalString(
196 SbxVariable
* variable
, bool special
, MarshalData
& data
, void ** buffer
)
198 assert(variable
!= nullptr && buffer
!= nullptr);
200 ErrCode e
= convert(variable
->GetOUString(), &str
);
201 if (e
!= ERRCODE_NONE
) {
204 std::vector
< char > * blob
= data
.newBlob();
205 blob
->insert(blob
->begin(), str
.getStr(), str
.getStr() + str
.getLength() + 1);
206 *buffer
= address(*blob
);
207 data
.unmarshalStrings
.push_back(StringData(variable
, *buffer
, special
));
211 ErrCode
marshalStruct(
212 SbxVariable
const * variable
, std::vector
< char > & blob
, std::size_t offset
,
215 assert(variable
!= nullptr);
216 SbxObject
* pobj
= dynamic_cast<SbxObject
*>(variable
->GetObject());
218 SbxArray
* props
= pobj
->GetProperties();
219 for (sal_uInt32 i
= 0; i
< props
->Count(); ++i
)
221 ErrCode e
= marshal(false, props
->Get(i
), false, blob
, offset
, data
);
222 if (e
!= ERRCODE_NONE
) {
229 ErrCode
marshalArray(
230 SbxVariable
const * variable
, std::vector
< char > & blob
, std::size_t offset
,
233 assert(variable
!= nullptr);
234 SbxDimArray
* arr
= dynamic_cast<SbxDimArray
*>( variable
->GetObject() );
236 sal_Int32 dims
= arr
->GetDims();
237 std::vector
< sal_Int32
> low(dims
);
238 std::vector
< sal_Int32
> up(dims
);
239 for (sal_Int32 i
= 0; i
< dims
; ++i
) {
240 arr
->GetDim(i
+ 1, low
[i
], up
[i
]);
242 for (std::vector
< sal_Int32
> idx
= low
;;) {
243 ErrCode e
= marshal(false, arr
->Get(idx
.data()), false, blob
, offset
, data
);
244 if (e
!= ERRCODE_NONE
) {
247 sal_Int32 i
= dims
- 1;
248 while (idx
[i
] == up
[i
]) {
259 // 8-aligned structs are only 4-aligned on stack, so alignment of members in
260 // such structs must take that into account via "offset"
262 bool outer
, SbxVariable
* variable
, bool special
,
263 std::vector
< char > & blob
, std::size_t offset
, MarshalData
& data
)
265 assert(variable
!= nullptr);
267 SbxDataType eVarType
= variable
->GetType();
268 bool bByVal
= !(variable
->GetFlags() & SbxFlagBits::Reference
);
269 if( !bByVal
&& !SbiRuntime::isVBAEnabled() && eVarType
== SbxSTRING
)
273 if ((eVarType
& SbxARRAY
) == 0) {
276 add(blob
, variable
->GetInteger(), outer
? 8 : 2, offset
);
279 add(blob
, variable
->GetLong(), outer
? 8 : 4, offset
);
282 add(blob
, variable
->GetSingle(), outer
? 8 : 4, offset
);
285 add(blob
, variable
->GetDouble(), 8, offset
);
290 ErrCode e
= marshalString(variable
, special
, data
, &p
);
291 if (e
!= ERRCODE_NONE
) {
294 add(blob
, p
, 8, offset
);
299 align(blob
, outer
? 8 : alignment(variable
), offset
, 0);
300 ErrCode e
= marshalStruct(variable
, blob
, offset
, data
);
301 if (e
!= ERRCODE_NONE
) {
307 add(blob
, variable
->GetBool(), outer
? 8 : 1, offset
);
310 add(blob
, variable
->GetByte(), outer
? 8 : 1, offset
);
317 ErrCode e
= marshalArray(variable
, blob
, offset
, data
);
318 if (e
!= ERRCODE_NONE
) {
323 if ((eVarType
& SbxARRAY
) == 0) {
331 add(blob
, variable
->data(), 8, offset
);
336 ErrCode e
= marshalString(variable
, special
, data
, &p
);
337 if (e
!= ERRCODE_NONE
) {
340 std::vector
< char >* blob2
= data
.newBlob();
341 add(*blob2
, p
, 8, 0);
342 add(blob
, address(*blob2
), 8, offset
);
347 std::vector
< char > * blob2
= data
.newBlob();
348 ErrCode e
= marshalStruct(variable
, *blob2
, 0, data
);
349 if (e
!= ERRCODE_NONE
) {
352 void * p
= address(*blob2
);
354 data
.unmarshal
.push_back(UnmarshalData(variable
, p
));
356 add(blob
, p
, 8, offset
);
364 std::vector
< char > * blob2
= data
.newBlob();
365 ErrCode e
= marshalArray(variable
, *blob2
, 0, data
);
366 if (e
!= ERRCODE_NONE
) {
369 void * p
= address(*blob2
);
371 data
.unmarshal
.push_back(UnmarshalData(variable
, p
));
373 add(blob
, p
, 8, offset
);
379 template< typename T
> T
read(void const ** pointer
) {
380 T
const * p
= static_cast< T
const * >(*pointer
);
381 *pointer
= static_cast< void const * >(p
+ 1);
385 void const * unmarshal(SbxVariable
* variable
, void const * data
) {
386 assert(variable
!= nullptr);
387 if ((variable
->GetType() & SbxARRAY
) == 0) {
388 switch (variable
->GetType()) {
390 variable
->PutInteger(read
< sal_Int16
>(&data
));
393 variable
->PutLong(read
< sal_Int32
>(&data
));
396 variable
->PutSingle(read
< float >(&data
));
399 variable
->PutDouble(read
< double >(&data
));
402 read
< char * >(&data
); // handled by unmarshalString
406 data
= reinterpret_cast< void const * >(
408 reinterpret_cast< sal_uIntPtr
>(data
),
409 alignment(variable
)));
410 SbxObject
* pobj
= dynamic_cast<SbxObject
*>(variable
->GetObject());
412 SbxArray
* props
= pobj
->GetProperties();
413 for (sal_uInt32 i
= 0; i
< props
->Count(); ++i
)
415 data
= unmarshal(props
->Get(i
), data
);
420 variable
->PutBool(read
< sal_Bool
>(&data
));
423 variable
->PutByte(read
< sal_uInt8
>(&data
));
430 SbxDimArray
* arr
= dynamic_cast<SbxDimArray
*>( variable
->GetObject() );
432 sal_Int32 dims
= arr
->GetDims();
433 std::vector
< sal_Int32
> low(dims
);
434 std::vector
< sal_Int32
> up(dims
);
435 for (sal_Int32 i
= 0; i
< dims
; ++i
) {
436 arr
->GetDim(i
+ 1, low
[i
], up
[i
]);
438 for (std::vector
< sal_Int32
> idx
= low
;;) {
439 data
= unmarshal(arr
->Get(idx
.data()), data
);
440 sal_Int32 i
= dims
- 1;
441 while (idx
[i
] == up
[i
]) {
455 ErrCode
unmarshalString(StringData
const & data
, SbxVariable
const & result
) {
457 if (data
.buffer
!= nullptr) {
458 char const * p
= static_cast< char const * >(data
.buffer
);
461 len
= static_cast< sal_Int32
>(result
.GetULong());
462 if (len
< 0) { // i.e., DWORD result >= 2^31
463 return ERRCODE_BASIC_BAD_ARGUMENT
;
464 //TODO: more specific errcode?
467 len
= rtl_str_getLength(p
);
469 ErrCode e
= convert(p
, len
, &str
);
470 if (e
!= ERRCODE_NONE
) {
474 data
.variable
->PutString(str
);
484 OUString
const & dll
, ProcData
const & proc
, SbxArray
* arguments
,
485 SbxVariable
& result
)
487 if (arguments
&& arguments
->Count() > 20)
488 return ERRCODE_BASIC_NOT_IMPLEMENTED
;
490 std::vector
< char > stack
;
493 // For DWORD GetLogicalDriveStringsA(DWORD nBufferLength, LPSTR lpBuffer)
494 // from kernel32, upon return, filled lpBuffer length is result DWORD, which
495 // requires special handling in unmarshalString; other functions might
496 // require similar treatment, too:
498 dll
.equalsIgnoreAsciiCase("KERNEL32.DLL") &&
499 (proc
.name
== "GetLogicalDriveStringsA");
500 for (sal_uInt32 i
= 1; i
< (arguments
== nullptr ? 0 : arguments
->Count()); ++i
)
502 ErrCode e
= marshal(true, arguments
->Get(i
), special
&& i
== 2, stack
, stack
.size(),
504 if (e
!= ERRCODE_NONE
) {
507 align(stack
, 8, 0, 0);
512 // We fake all calls as being to a varargs function,
513 // as this means any floating-point argument among the first four
514 // ones will end up in a XMM register where the callee expects it.
516 sal_Int64 iRetVal
= 0;
517 double dRetVal
= 0.0;
519 switch (result
.GetType()) {
528 auto p
= reinterpret_cast<sal_Int64 (*)(...)>(proc
.proc
);
529 auto const st
= reinterpret_cast<double *>(stack
.data());
531 = p(st
[0], st
[1], st
[2], st
[3], st
[4], st
[5], st
[6], st
[7], st
[8], st
[9], st
[10],
532 st
[11], st
[12], st
[13], st
[14], st
[15], st
[16], st
[17], st
[18], st
[19]);
538 auto p
= reinterpret_cast<double (*)(...)>(proc
.proc
);
539 auto const st
= reinterpret_cast<double*>(stack
.data());
541 = p(st
[0], st
[1], st
[2], st
[3], st
[4], st
[5], st
[6], st
[7], st
[8], st
[9], st
[10],
542 st
[11], st
[12], st
[13], st
[14], st
[15], st
[16], st
[17], st
[18], st
[19]);
549 switch (result
.GetType()) {
553 result
.PutInteger(static_cast< sal_Int16
>(iRetVal
));
556 result
.PutLong(static_cast< sal_Int32
>(iRetVal
));
559 result
.PutSingle(static_cast< float >(dRetVal
));
562 result
.PutDouble(dRetVal
);
566 char const * s1
= reinterpret_cast< char const * >(iRetVal
);
568 ErrCode e
= convert(s1
, rtl_str_getLength(s1
), &s2
);
569 if (e
!= ERRCODE_NONE
) {
572 result
.PutString(s2
);
579 result
.PutBool(bool(iRetVal
));
582 result
.PutByte(static_cast< sal_uInt8
>(iRetVal
));
588 for (sal_uInt32 i
= 1; i
< (arguments
== nullptr ? 0 : arguments
->Count()); ++i
)
590 arguments
->Get(i
)->ResetFlag(SbxFlagBits::Reference
);
591 //TODO: skipped for errors?!?
593 for (auto const& elem
: data
.unmarshal
)
595 unmarshal(elem
.variable
, elem
.buffer
);
597 for (auto const& elem
: data
.unmarshalStrings
)
599 ErrCode e
= unmarshalString(elem
, result
);
600 if (e
!= ERRCODE_NONE
) {
607 ErrCode
getProcData(HMODULE handle
, OUString
const & name
, ProcData
* proc
)
609 assert(proc
!= nullptr);
610 if (name
.getLength() != 0 && name
[0] == '@') { //TODO: "@" vs. "#"???
611 sal_Int32 n
= name
.copy(1).toInt32(); //TODO: handle bad input
612 if (n
<= 0 || n
> 0xFFFF) {
613 return ERRCODE_BASIC_BAD_ARGUMENT
; //TODO: more specific errcode?
615 FARPROC p
= GetProcAddress(handle
, reinterpret_cast< LPCSTR
>(n
));
617 proc
->name
= "#" + OString::number(n
);
623 ErrCode e
= convert(name
, &name8
);
624 if (e
!= ERRCODE_NONE
) {
627 FARPROC p
= GetProcAddress(handle
, name8
.getStr());
633 sal_Int32 i
= name8
.indexOf('#');
635 name8
= name8
.copy(0, i
);
636 p
= GetProcAddress(handle
, name8
.getStr());
643 OString
real("_" + name8
);
644 p
= GetProcAddress(handle
, real
.getStr());
651 p
= GetProcAddress(handle
, real
.getStr());
658 return ERRCODE_BASIC_PROC_UNDEFINED
;
661 struct Dll
: public salhelper::SimpleReferenceObject
{
663 typedef std::map
< OUString
, ProcData
> Procs
;
665 virtual ~Dll() override
;
668 Dll(): handle(nullptr) {}
670 ErrCode
getProc(OUString
const & name
, ProcData
* proc
);
677 if (handle
!= nullptr && !FreeLibrary(handle
)) {
678 SAL_WARN("basic", "FreeLibrary(" << handle
<< ") failed with " << GetLastError());
682 ErrCode
Dll::getProc(OUString
const & name
, ProcData
* proc
) {
683 Procs::iterator
i(procs
.find(name
));
684 if (i
!= procs
.end()) {
688 ErrCode e
= getProcData(handle
, name
, proc
);
689 if (e
== ERRCODE_NONE
) {
690 procs
.emplace(name
, *proc
);
695 OUString
fullDllName(OUString
const & name
) {
697 if (full
.indexOf('.') == -1) {
705 struct SbiDllMgr::Impl
{
707 typedef std::map
< OUString
, ::rtl::Reference
< Dll
> > Dlls
;
711 Impl(const Impl
&) = delete;
712 const Impl
& operator=(const Impl
&) = delete;
714 Dll
* getDll(OUString
const & name
);
719 Dll
* SbiDllMgr::Impl::getDll(OUString
const & name
) {
720 Dlls::iterator
i(dlls
.find(name
));
721 if (i
== dlls
.end()) {
722 i
= dlls
.emplace(name
, new Dll
).first
;
723 HMODULE h
= LoadLibraryW(o3tl::toW(name
.getStr()));
728 i
->second
->handle
= h
;
730 return i
->second
.get();
733 ErrCode
SbiDllMgr::Call(
734 std::u16string_view function
, std::u16string_view library
,
735 SbxArray
* arguments
, SbxVariable
& result
, bool cdeclConvention
)
737 if (cdeclConvention
) {
738 return ERRCODE_BASIC_NOT_IMPLEMENTED
;
740 OUString
dllName(fullDllName(OUString(library
)));
741 Dll
* dll
= impl_
->getDll(dllName
);
742 if (dll
== nullptr) {
743 return ERRCODE_BASIC_BAD_DLL_LOAD
;
746 ErrCode e
= dll
->getProc(OUString(function
), &proc
);
747 if (e
!= ERRCODE_NONE
) {
750 return call(dllName
, proc
, arguments
, result
);
753 void SbiDllMgr::FreeDll(OUString
const & library
) {
754 impl_
->dlls
.erase(library
);
757 SbiDllMgr::SbiDllMgr(): impl_(new Impl
) {}
759 SbiDllMgr::~SbiDllMgr() {}
761 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */