LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / source / runtime / dllmgr-x86.cxx
blob93e5c2bf533f03730ac92bb3ff2ff0a982e75650
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #if defined(_WIN32)
23 #include <prewin.h>
24 #include <postwin.h>
25 #endif
27 #include <algorithm>
28 #include <cstddef>
29 #include <map>
30 #include <vector>
32 #include <basic/sbx.hxx>
33 #include <basic/sbxvar.hxx>
34 #include "runtime.hxx"
35 #include <osl/thread.h>
36 #include <rtl/ref.hxx>
37 #include <rtl/string.hxx>
38 #include <rtl/ustring.hxx>
39 #include <sal/log.hxx>
40 #include <salhelper/simplereferenceobject.hxx>
41 #include <o3tl/char16_t2wchar_t.hxx>
43 #undef max
45 #include "dllmgr.hxx"
47 using namespace css;
48 using namespace css::uno;
50 /* Open issues:
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.
64 extern "C" {
66 int __stdcall DllMgr_call32(FARPROC, void const * stack, std::size_t size);
67 double __stdcall DllMgr_callFp(FARPROC, void const * stack, std::size_t size);
71 namespace {
73 char * address(std::vector< char > & blob) {
74 return blob.empty() ? 0 : &blob[0];
77 ErrCode convert(OUString const & source, OString * target) {
78 return
79 source.convertToString(
80 target, osl_getThreadTextEncoding(),
81 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
82 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
83 ? ERRCODE_NONE : ERRCODE_BASIC_BAD_ARGUMENT;
84 //TODO: more specific errcode?
87 ErrCode convert(char const * source, sal_Int32 length, OUString * target) {
88 return
89 rtl_convertStringToUString(
90 &target->pData, source, length, osl_getThreadTextEncoding(),
91 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
92 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
93 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
94 ? ERRCODE_NONE : ERRCODE_BASIC_BAD_ARGUMENT;
95 //TODO: more specific errcode?
98 struct UnmarshalData {
99 UnmarshalData(SbxVariable * theVariable, void * theBuffer):
100 variable(theVariable), buffer(theBuffer) {}
102 SbxVariable * variable;
103 void * buffer;
106 struct StringData: public UnmarshalData {
107 StringData(SbxVariable * theVariable, void * theBuffer, bool theSpecial):
108 UnmarshalData(theVariable, theBuffer), special(theSpecial) {}
110 bool special;
113 class MarshalData {
114 public:
115 MarshalData() = default;
116 MarshalData(const MarshalData&) = delete;
117 const MarshalData& operator=(const MarshalData&) = delete;
119 std::vector< char > * newBlob() {
120 blobs_.push_back(std::vector< char >());
121 return &blobs_.back();
124 std::vector< UnmarshalData > unmarshal;
126 std::vector< StringData > unmarshalStrings;
128 private:
129 std::vector< std::vector< char > > blobs_;
132 std::size_t align(std::size_t address, std::size_t alignment) {
133 // alignment = 2^k for some k >= 0
134 return (address + (alignment - 1)) & ~(alignment - 1);
137 char * align(
138 std::vector< char > & blob, std::size_t alignment, std::size_t offset,
139 std::size_t add)
141 std::vector< char >::size_type n = blob.size();
142 n = align(n - offset, alignment) + offset; //TODO: overflow in align()
143 blob.resize(n + add); //TODO: overflow
144 return address(blob) + n;
147 template< typename T > void add(
148 std::vector< char > & blob, T const & data, std::size_t alignment,
149 std::size_t offset)
151 *reinterpret_cast< T * >(align(blob, alignment, offset, sizeof (T))) = data;
154 std::size_t alignment(SbxVariable * variable) {
155 assert(variable != 0);
156 if ((variable->GetType() & SbxARRAY) == 0) {
157 switch (variable->GetType()) {
158 case SbxINTEGER:
159 return 2;
160 case SbxLONG:
161 case SbxSINGLE:
162 case SbxSTRING:
163 return 4;
164 case SbxDOUBLE:
165 return 8;
166 case SbxOBJECT:
168 std::size_t n = 1;
169 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
170 assert(pobj);
171 SbxArray* props = pobj->GetProperties();
172 for (sal_uInt32 i = 0; i < props->Count(); ++i)
174 n = std::max(n, alignment(props->Get(i)));
176 return n;
178 case SbxBOOL:
179 case SbxBYTE:
180 return 1;
181 default:
182 assert(false);
183 return 1;
185 } else {
186 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
187 assert(arr);
188 sal_Int32 dims = arr->GetDims();
189 std::vector< sal_Int32 > low(dims);
190 for (sal_Int32 i = 0; i < dims; ++i) {
191 sal_Int32 up;
192 arr->GetDim(i + 1, low[i], up);
194 return alignment(arr->Get(&low[0]));
198 ErrCode marshal(
199 bool outer, SbxVariable * variable, bool special,
200 std::vector< char > & blob, std::size_t offset, MarshalData & data);
202 ErrCode marshalString(
203 SbxVariable * variable, bool special, MarshalData & data, void ** buffer)
205 assert(variable != 0 && buffer != 0);
206 OString str;
207 ErrCode e = convert(variable->GetOUString(), &str);
208 if (e != ERRCODE_NONE) {
209 return e;
211 std::vector< char > * blob = data.newBlob();
212 blob->insert(
213 blob->begin(), str.getStr(), str.getStr() + str.getLength() + 1);
214 *buffer = address(*blob);
215 data.unmarshalStrings.push_back(StringData(variable, *buffer, special));
216 return ERRCODE_NONE;
219 ErrCode marshalStruct(
220 SbxVariable * variable, std::vector< char > & blob, std::size_t offset,
221 MarshalData & data)
223 assert(variable != 0);
224 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
225 assert(pobj);
226 SbxArray* props = pobj->GetProperties();
227 for (sal_uInt32 i = 0; i < props->Count(); ++i)
229 ErrCode e = marshal(false, props->Get(i), false, blob, offset, data);
230 if (e != ERRCODE_NONE) {
231 return e;
234 return ERRCODE_NONE;
237 ErrCode marshalArray(
238 SbxVariable * variable, std::vector< char > & blob, std::size_t offset,
239 MarshalData & data)
241 assert(variable != 0);
242 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
243 assert(arr);
244 sal_Int32 dims = arr->GetDims();
245 std::vector< sal_Int32 > low(dims);
246 std::vector< sal_Int32 > up(dims);
247 for (sal_Int32 i = 0; i < dims; ++i) {
248 arr->GetDim(i + 1, low[i], up[i]);
250 for (std::vector< sal_Int32 > idx = low;;) {
251 ErrCode e = marshal(false, arr->Get(&idx[0]), false, blob, offset, data);
252 if (e != ERRCODE_NONE) {
253 return e;
255 sal_Int32 i = dims - 1;
256 while (idx[i] == up[i]) {
257 idx[i] = low[i];
258 if (i == 0) {
259 return ERRCODE_NONE;
261 --i;
263 ++idx[i];
267 // 8-aligned structs are only 4-aligned on stack, so alignment of members in
268 // such structs must take that into account via "offset"
269 ErrCode marshal(
270 bool outer, SbxVariable * variable, bool special,
271 std::vector< char > & blob, std::size_t offset, MarshalData & data)
273 assert(variable != 0);
275 SbxDataType eVarType = variable->GetType();
276 bool bByVal = !(variable->GetFlags() & SbxFlagBits::Reference);
277 if( !bByVal && !SbiRuntime::isVBAEnabled() && eVarType == SbxSTRING )
278 bByVal = true;
280 if (bByVal) {
281 if ((eVarType & SbxARRAY) == 0) {
282 switch (eVarType) {
283 case SbxINTEGER:
284 add(blob, variable->GetInteger(), outer ? 4 : 2, offset);
285 break;
286 case SbxLONG:
287 add(blob, variable->GetLong(), 4, offset);
288 break;
289 case SbxSINGLE:
290 add(blob, variable->GetSingle(), 4, offset);
291 break;
292 case SbxDOUBLE:
293 add(blob, variable->GetDouble(), outer ? 4 : 8, offset);
294 break;
295 case SbxSTRING:
297 void * p;
298 ErrCode e = marshalString(variable, special, data, &p);
299 if (e != ERRCODE_NONE) {
300 return e;
302 add(blob, p, 4, offset);
303 break;
305 case SbxOBJECT:
307 align(blob, outer ? 4 : alignment(variable), offset, 0);
308 ErrCode e = marshalStruct(variable, blob, offset, data);
309 if (e != ERRCODE_NONE) {
310 return e;
312 break;
314 case SbxBOOL:
315 add(blob, variable->GetBool(), outer ? 4 : 1, offset);
316 break;
317 case SbxBYTE:
318 add(blob, variable->GetByte(), outer ? 4 : 1, offset);
319 break;
320 default:
321 assert(false);
322 break;
324 } else {
325 ErrCode e = marshalArray(variable, blob, offset, data);
326 if (e != ERRCODE_NONE) {
327 return e;
330 } else {
331 if ((eVarType & SbxARRAY) == 0) {
332 switch (eVarType) {
333 case SbxINTEGER:
334 case SbxLONG:
335 case SbxSINGLE:
336 case SbxDOUBLE:
337 case SbxBOOL:
338 case SbxBYTE:
339 add(blob, variable->data(), 4, offset);
340 break;
341 case SbxSTRING:
343 void * p;
344 ErrCode e = marshalString(variable, special, data, &p);
345 if (e != ERRCODE_NONE) {
346 return e;
348 std::vector< char > * blob2 = data.newBlob();
349 add(*blob2, p, 4, 0);
350 add(blob, address(*blob2), 4, offset);
351 break;
353 case SbxOBJECT:
355 std::vector< char > * blob2 = data.newBlob();
356 ErrCode e = marshalStruct(variable, *blob2, 0, data);
357 if (e != ERRCODE_NONE) {
358 return e;
360 void * p = address(*blob2);
361 if (outer) {
362 data.unmarshal.push_back(UnmarshalData(variable, p));
364 add(blob, p, 4, offset);
365 break;
367 default:
368 assert(false);
369 break;
371 } else {
372 std::vector< char > * blob2 = data.newBlob();
373 ErrCode e = marshalArray(variable, *blob2, 0, data);
374 if (e != ERRCODE_NONE) {
375 return e;
377 void * p = address(*blob2);
378 if (outer) {
379 data.unmarshal.push_back(UnmarshalData(variable, p));
381 add(blob, p, 4, offset);
384 return ERRCODE_NONE;
387 template< typename T > T read(void const ** pointer) {
388 T const * p = static_cast< T const * >(*pointer);
389 *pointer = static_cast< void const * >(p + 1);
390 return *p;
393 void const * unmarshal(SbxVariable * variable, void const * data) {
394 assert(variable != 0);
395 if ((variable->GetType() & SbxARRAY) == 0) {
396 switch (variable->GetType()) {
397 case SbxINTEGER:
398 variable->PutInteger(read< sal_Int16 >(&data));
399 break;
400 case SbxLONG:
401 variable->PutLong(read< sal_Int32 >(&data));
402 break;
403 case SbxSINGLE:
404 variable->PutSingle(read< float >(&data));
405 break;
406 case SbxDOUBLE:
407 variable->PutDouble(read< double >(&data));
408 break;
409 case SbxSTRING:
410 read< char * >(&data); // handled by unmarshalString
411 break;
412 case SbxOBJECT:
414 data = reinterpret_cast< void const * >(
415 align(
416 reinterpret_cast< sal_uIntPtr >(data),
417 alignment(variable)));
418 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
419 assert(pobj);
420 SbxArray* props = pobj->GetProperties();
421 for (sal_uInt32 i = 0; i < props->Count(); ++i)
423 data = unmarshal(props->Get(i), data);
425 break;
427 case SbxBOOL:
428 variable->PutBool(read< sal_Bool >(&data));
429 break;
430 case SbxBYTE:
431 variable->PutByte(read< sal_uInt8 >(&data));
432 break;
433 default:
434 assert(false);
435 break;
437 } else {
438 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
439 assert(arr);
440 sal_Int32 dims = arr->GetDims();
441 std::vector< sal_Int32 > low(dims);
442 std::vector< sal_Int32 > up(dims);
443 for (sal_Int32 i = 0; i < dims; ++i) {
444 arr->GetDim(i + 1, low[i], up[i]);
446 for (std::vector< sal_Int32 > idx = low;;) {
447 data = unmarshal(arr->Get(&idx[0]), data);
448 sal_Int32 i = dims - 1;
449 while (idx[i] == up[i]) {
450 idx[i] = low[i];
451 if (i == 0) {
452 goto done;
454 --i;
456 ++idx[i];
458 done:;
460 return data;
463 ErrCode unmarshalString(StringData const & data, SbxVariable & result) {
464 OUString str;
465 if (data.buffer != 0) {
466 char const * p = static_cast< char const * >(data.buffer);
467 sal_Int32 len;
468 if (data.special) {
469 len = static_cast< sal_Int32 >(result.GetULong());
470 if (len < 0) { // i.e., DWORD result >= 2^31
471 return ERRCODE_BASIC_BAD_ARGUMENT;
472 //TODO: more specific errcode?
474 } else {
475 len = rtl_str_getLength(p);
477 ErrCode e = convert(p, len, &str);
478 if (e != ERRCODE_NONE) {
479 return e;
482 data.variable->PutString(str);
483 return ERRCODE_NONE;
486 struct ProcData {
487 OString name;
488 FARPROC proc;
491 ErrCode call(
492 OUString const & dll, ProcData const & proc, SbxArray * arguments,
493 SbxVariable & result)
495 std::vector< char > stack;
496 MarshalData data;
497 // For DWORD GetLogicalDriveStringsA(DWORD nBufferLength, LPSTR lpBuffer)
498 // from kernel32, upon return, filled lpBuffer length is result DWORD, which
499 // requires special handling in unmarshalString; other functions might
500 // require similar treatment, too:
501 bool special = dll.equalsIgnoreAsciiCase("KERNEL32.DLL") &&
502 (proc.name == OString("GetLogicalDriveStringsA"));
503 for (sal_uInt32 i = 1; i < (arguments == 0 ? 0 : arguments->Count()); ++i)
505 ErrCode e = marshal(true, arguments->Get(i), special && i == 2, stack, stack.size(),
506 data);
507 if (e != ERRCODE_NONE) {
508 return e;
510 align(stack, 4, 0, 0);
512 switch (result.GetType()) {
513 case SbxEMPTY:
514 DllMgr_call32(proc.proc, address(stack), stack.size());
515 break;
516 case SbxINTEGER:
517 result.PutInteger(
518 static_cast< sal_Int16 >(
519 DllMgr_call32(proc.proc, address(stack), stack.size())));
520 break;
521 case SbxLONG:
522 result.PutLong(
523 static_cast< sal_Int32 >(
524 DllMgr_call32(proc.proc, address(stack), stack.size())));
525 break;
526 case SbxSINGLE:
527 result.PutSingle(
528 static_cast< float >(
529 DllMgr_callFp(proc.proc, address(stack), stack.size())));
530 break;
531 case SbxDOUBLE:
532 result.PutDouble(
533 DllMgr_callFp(proc.proc, address(stack), stack.size()));
534 break;
535 case SbxSTRING:
537 char const * s1 = reinterpret_cast< char const * >(
538 DllMgr_call32(proc.proc, address(stack), stack.size()));
539 OUString s2;
540 ErrCode e = convert(s1, rtl_str_getLength(s1), &s2);
541 if (e != ERRCODE_NONE) {
542 return e;
544 result.PutString(s2);
545 break;
547 case SbxOBJECT:
548 //TODO
549 DllMgr_call32(proc.proc, address(stack), stack.size());
550 break;
551 case SbxBOOL:
552 result.PutBool(
553 bool(DllMgr_call32(proc.proc, address(stack), stack.size())));
554 break;
555 case SbxBYTE:
556 result.PutByte(
557 static_cast< sal_uInt8 >(
558 DllMgr_call32(proc.proc, address(stack), stack.size())));
559 break;
560 default:
561 assert(false);
562 break;
564 for (sal_uInt32 i = 1; i < (arguments == 0 ? 0 : arguments->Count()); ++i)
566 arguments->Get(i)->ResetFlag(SbxFlagBits::Reference);
567 //TODO: skipped for errors?!?
569 for (auto& rUnmarshalData : data.unmarshal)
571 unmarshal(rUnmarshalData.variable, rUnmarshalData.buffer);
573 for (const auto& rStringData : data.unmarshalStrings)
575 ErrCode e = unmarshalString(rStringData, result);
576 if (e != ERRCODE_NONE) {
577 return e;
580 return ERRCODE_NONE;
583 ErrCode getProcData(HMODULE handle, OUString const & name, ProcData * proc)
585 assert(proc != 0);
586 if ( !name.isEmpty() && name[0] == '@' ) { //TODO: "@" vs. "#"???
587 sal_Int32 n = name.copy(1).toInt32(); //TODO: handle bad input
588 if (n <= 0 || n > 0xFFFF) {
589 return ERRCODE_BASIC_BAD_ARGUMENT; //TODO: more specific errcode?
591 FARPROC p = GetProcAddress(handle, reinterpret_cast< LPCSTR >(n));
592 if (p != 0) {
593 proc->name = OString("#") + OString::number(n);
594 proc->proc = p;
595 return ERRCODE_NONE;
597 } else {
598 OString name8;
599 ErrCode e = convert(name, &name8);
600 if (e != ERRCODE_NONE) {
601 return e;
603 FARPROC p = GetProcAddress(handle, name8.getStr());
604 if (p != 0) {
605 proc->name = name8;
606 proc->proc = p;
607 return ERRCODE_NONE;
609 sal_Int32 i = name8.indexOf('#');
610 if (i != -1) {
611 name8 = name8.copy(0, i);
612 p = GetProcAddress(handle, name8.getStr());
613 if (p != 0) {
614 proc->name = name8;
615 proc->proc = p;
616 return ERRCODE_NONE;
619 OString real(OString("_") + name8);
620 p = GetProcAddress(handle, real.getStr());
621 if (p != 0) {
622 proc->name = real;
623 proc->proc = p;
624 return ERRCODE_NONE;
626 real = name8 + OString("A");
627 p = GetProcAddress(handle, real.getStr());
628 if (p != 0) {
629 proc->name = real;
630 proc->proc = p;
631 return ERRCODE_NONE;
634 return ERRCODE_BASIC_PROC_UNDEFINED;
637 struct Dll: public salhelper::SimpleReferenceObject {
638 private:
639 typedef std::map< OUString, ProcData > Procs;
641 virtual ~Dll();
643 public:
644 Dll(): handle(0) {}
646 ErrCode getProc(OUString const & name, ProcData * proc);
648 HMODULE handle;
649 Procs procs;
652 Dll::~Dll() {
653 if (handle != 0 && !FreeLibrary(handle)) {
654 SAL_WARN("basic", "FreeLibrary(" << handle << ") failed with " << GetLastError());
658 ErrCode Dll::getProc(OUString const & name, ProcData * proc) {
659 Procs::iterator i(procs.find(name));
660 if (i != procs.end()) {
661 *proc = i->second;
662 return ERRCODE_NONE;
664 ErrCode e = getProcData(handle, name, proc);
665 if (e == ERRCODE_NONE) {
666 procs.emplace(name, *proc);
668 return e;
671 OUString fullDllName(OUString const & name) {
672 OUString full(name);
673 if (full.indexOf('.') == -1) {
674 full += ".DLL";
676 return full;
681 struct SbiDllMgr::Impl {
682 private:
683 typedef std::map< OUString, rtl::Reference< Dll > > Dlls;
685 public:
686 Impl() = default;
687 Impl(const Impl&) = delete;
688 const Impl& operator=(const Impl&) = delete;
690 Dll * getDll(OUString const & name);
692 Dlls dlls;
695 Dll * SbiDllMgr::Impl::getDll(OUString const & name) {
696 Dlls::iterator i(dlls.find(name));
697 if (i == dlls.end()) {
698 i = dlls.emplace(name, new Dll).first;
699 HMODULE h = LoadLibraryW(o3tl::toW(name.getStr()));
700 if (h == 0) {
701 dlls.erase(i);
702 return 0;
704 i->second->handle = h;
706 return i->second.get();
709 ErrCode SbiDllMgr::Call(
710 std::u16string_view function, std::u16string_view library,
711 SbxArray * arguments, SbxVariable & result, bool cdeclConvention)
713 if (cdeclConvention) {
714 return ERRCODE_BASIC_NOT_IMPLEMENTED;
716 OUString dllName(fullDllName(OUString(library)));
717 Dll * dll = impl_->getDll(dllName);
718 if (dll == 0) {
719 return ERRCODE_BASIC_BAD_DLL_LOAD;
721 ProcData proc;
722 ErrCode e = dll->getProc(OUString(function), &proc);
723 if (e != ERRCODE_NONE) {
724 return e;
726 return call(dllName, proc, arguments, result);
729 void SbiDllMgr::FreeDll(OUString const & library) {
730 impl_->dlls.erase(library);
733 SbiDllMgr::SbiDllMgr(): impl_(new Impl) {}
735 SbiDllMgr::~SbiDllMgr() {}
737 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */