LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / source / runtime / dllmgr-x64.cxx
blobb2ddbb890da13b55ce713d816270b970befeda88
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 <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>
44 #undef max
46 #include "dllmgr.hxx"
48 using namespace css;
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 namespace {
66 char * address(std::vector< char > & blob) {
67 return blob.empty() ? nullptr : blob.data();
70 ErrCode convert(OUString const & source, OString * target) {
71 return
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) {
81 return
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;
96 void * buffer;
99 struct StringData: public UnmarshalData {
100 StringData(SbxVariable * theVariable, void * theBuffer, bool theSpecial):
101 UnmarshalData(theVariable, theBuffer), special(theSpecial) {}
103 bool special;
106 class MarshalData {
107 public:
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;
121 private:
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);
130 char * align(
131 std::vector< char > & blob, std::size_t alignment, std::size_t offset,
132 std::size_t add)
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,
142 std::size_t offset)
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()) {
151 case SbxINTEGER:
152 return 2;
153 case SbxLONG:
154 case SbxSINGLE:
155 case SbxSTRING:
156 return 4;
157 case SbxDOUBLE:
158 return 8;
159 case SbxOBJECT:
161 std::size_t n = 1;
162 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
163 assert(pobj);
164 SbxArray* props = pobj->GetProperties();
165 for (sal_uInt32 i = 0; i < props->Count(); ++i)
167 n = std::max(n, alignment(props->Get(i)));
169 return n;
171 case SbxBOOL:
172 case SbxBYTE:
173 return 1;
174 default:
175 assert(false);
176 return 1;
178 } else {
179 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
180 assert(arr);
181 sal_Int32 dims = arr->GetDims();
182 std::vector< sal_Int32 > low(dims);
183 for (sal_Int32 i = 0; i < dims; ++i) {
184 sal_Int32 up;
185 arr->GetDim(i + 1, low[i], up);
187 return alignment(arr->Get(low.data()));
191 ErrCode marshal(
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);
199 OString str;
200 ErrCode e = convert(variable->GetOUString(), &str);
201 if (e != ERRCODE_NONE) {
202 return e;
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));
208 return ERRCODE_NONE;
211 ErrCode marshalStruct(
212 SbxVariable const * variable, std::vector< char > & blob, std::size_t offset,
213 MarshalData & data)
215 assert(variable != nullptr);
216 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
217 assert(pobj);
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) {
223 return e;
226 return ERRCODE_NONE;
229 ErrCode marshalArray(
230 SbxVariable const * variable, std::vector< char > & blob, std::size_t offset,
231 MarshalData & data)
233 assert(variable != nullptr);
234 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
235 assert(arr);
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) {
245 return e;
247 sal_Int32 i = dims - 1;
248 while (idx[i] == up[i]) {
249 idx[i] = low[i];
250 if (i == 0) {
251 return ERRCODE_NONE;
253 --i;
255 ++idx[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"
261 ErrCode marshal(
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 )
270 bByVal = true;
272 if (bByVal) {
273 if ((eVarType & SbxARRAY) == 0) {
274 switch (eVarType) {
275 case SbxINTEGER:
276 add(blob, variable->GetInteger(), outer ? 8 : 2, offset);
277 break;
278 case SbxLONG:
279 add(blob, variable->GetLong(), outer ? 8 : 4, offset);
280 break;
281 case SbxSINGLE:
282 add(blob, variable->GetSingle(), outer ? 8 : 4, offset);
283 break;
284 case SbxDOUBLE:
285 add(blob, variable->GetDouble(), 8, offset);
286 break;
287 case SbxSTRING:
289 void * p;
290 ErrCode e = marshalString(variable, special, data, &p);
291 if (e != ERRCODE_NONE) {
292 return e;
294 add(blob, p, 8, offset);
295 break;
297 case SbxOBJECT:
299 align(blob, outer ? 8 : alignment(variable), offset, 0);
300 ErrCode e = marshalStruct(variable, blob, offset, data);
301 if (e != ERRCODE_NONE) {
302 return e;
304 break;
306 case SbxBOOL:
307 add(blob, variable->GetBool(), outer ? 8 : 1, offset);
308 break;
309 case SbxBYTE:
310 add(blob, variable->GetByte(), outer ? 8 : 1, offset);
311 break;
312 default:
313 assert(false);
314 break;
316 } else {
317 ErrCode e = marshalArray(variable, blob, offset, data);
318 if (e != ERRCODE_NONE) {
319 return e;
322 } else {
323 if ((eVarType & SbxARRAY) == 0) {
324 switch (eVarType) {
325 case SbxINTEGER:
326 case SbxLONG:
327 case SbxSINGLE:
328 case SbxDOUBLE:
329 case SbxBOOL:
330 case SbxBYTE:
331 add(blob, variable->data(), 8, offset);
332 break;
333 case SbxSTRING:
335 void * p;
336 ErrCode e = marshalString(variable, special, data, &p);
337 if (e != ERRCODE_NONE) {
338 return e;
340 std::vector< char >* blob2 = data.newBlob();
341 add(*blob2, p, 8, 0);
342 add(blob, address(*blob2), 8, offset);
343 break;
345 case SbxOBJECT:
347 std::vector< char > * blob2 = data.newBlob();
348 ErrCode e = marshalStruct(variable, *blob2, 0, data);
349 if (e != ERRCODE_NONE) {
350 return e;
352 void * p = address(*blob2);
353 if (outer) {
354 data.unmarshal.push_back(UnmarshalData(variable, p));
356 add(blob, p, 8, offset);
357 break;
359 default:
360 assert(false);
361 break;
363 } else {
364 std::vector< char > * blob2 = data.newBlob();
365 ErrCode e = marshalArray(variable, *blob2, 0, data);
366 if (e != ERRCODE_NONE) {
367 return e;
369 void * p = address(*blob2);
370 if (outer) {
371 data.unmarshal.push_back(UnmarshalData(variable, p));
373 add(blob, p, 8, offset);
376 return ERRCODE_NONE;
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);
382 return *p;
385 void const * unmarshal(SbxVariable * variable, void const * data) {
386 assert(variable != nullptr);
387 if ((variable->GetType() & SbxARRAY) == 0) {
388 switch (variable->GetType()) {
389 case SbxINTEGER:
390 variable->PutInteger(read< sal_Int16 >(&data));
391 break;
392 case SbxLONG:
393 variable->PutLong(read< sal_Int32 >(&data));
394 break;
395 case SbxSINGLE:
396 variable->PutSingle(read< float >(&data));
397 break;
398 case SbxDOUBLE:
399 variable->PutDouble(read< double >(&data));
400 break;
401 case SbxSTRING:
402 read< char * >(&data); // handled by unmarshalString
403 break;
404 case SbxOBJECT:
406 data = reinterpret_cast< void const * >(
407 align(
408 reinterpret_cast< sal_uIntPtr >(data),
409 alignment(variable)));
410 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
411 assert(pobj);
412 SbxArray* props = pobj->GetProperties();
413 for (sal_uInt32 i = 0; i < props->Count(); ++i)
415 data = unmarshal(props->Get(i), data);
417 break;
419 case SbxBOOL:
420 variable->PutBool(read< sal_Bool >(&data));
421 break;
422 case SbxBYTE:
423 variable->PutByte(read< sal_uInt8 >(&data));
424 break;
425 default:
426 assert(false);
427 break;
429 } else {
430 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
431 assert(arr);
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]) {
442 idx[i] = low[i];
443 if (i == 0) {
444 goto done;
446 --i;
448 ++idx[i];
450 done:;
452 return data;
455 ErrCode unmarshalString(StringData const & data, SbxVariable const & result) {
456 OUString str;
457 if (data.buffer != nullptr) {
458 char const * p = static_cast< char const * >(data.buffer);
459 sal_Int32 len;
460 if (data.special) {
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?
466 } else {
467 len = rtl_str_getLength(p);
469 ErrCode e = convert(p, len, &str);
470 if (e != ERRCODE_NONE) {
471 return e;
474 data.variable->PutString(str);
475 return ERRCODE_NONE;
478 struct ProcData {
479 OString name;
480 FARPROC proc;
483 ErrCode call(
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;
491 MarshalData data;
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:
497 bool special =
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(),
503 data);
504 if (e != ERRCODE_NONE) {
505 return e;
507 align(stack, 8, 0, 0);
510 stack.resize(20*8);
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()) {
520 case SbxEMPTY:
521 case SbxINTEGER:
522 case SbxLONG:
523 case SbxSTRING:
524 case SbxOBJECT:
525 case SbxBOOL:
526 case SbxBYTE:
528 auto p = reinterpret_cast<sal_Int64 (*)(...)>(proc.proc);
529 auto const st = reinterpret_cast<double *>(stack.data());
530 iRetVal
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]);
533 break;
535 case SbxSINGLE:
536 case SbxDOUBLE:
538 auto p = reinterpret_cast<double (*)(...)>(proc.proc);
539 auto const st = reinterpret_cast<double*>(stack.data());
540 dRetVal
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]);
543 break;
545 default:
546 break;
549 switch (result.GetType()) {
550 case SbxEMPTY:
551 break;
552 case SbxINTEGER:
553 result.PutInteger(static_cast< sal_Int16 >(iRetVal));
554 break;
555 case SbxLONG:
556 result.PutLong(static_cast< sal_Int32 >(iRetVal));
557 break;
558 case SbxSINGLE:
559 result.PutSingle(static_cast< float >(dRetVal));
560 break;
561 case SbxDOUBLE:
562 result.PutDouble(dRetVal);
563 break;
564 case SbxSTRING:
566 char const * s1 = reinterpret_cast< char const * >(iRetVal);
567 OUString s2;
568 ErrCode e = convert(s1, rtl_str_getLength(s1), &s2);
569 if (e != ERRCODE_NONE) {
570 return e;
572 result.PutString(s2);
573 break;
575 case SbxOBJECT:
576 //TODO
577 break;
578 case SbxBOOL:
579 result.PutBool(bool(iRetVal));
580 break;
581 case SbxBYTE:
582 result.PutByte(static_cast< sal_uInt8 >(iRetVal));
583 break;
584 default:
585 assert(false);
586 break;
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) {
601 return e;
604 return 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));
616 if (p != nullptr) {
617 proc->name = "#" + OString::number(n);
618 proc->proc = p;
619 return ERRCODE_NONE;
621 } else {
622 OString name8;
623 ErrCode e = convert(name, &name8);
624 if (e != ERRCODE_NONE) {
625 return e;
627 FARPROC p = GetProcAddress(handle, name8.getStr());
628 if (p != nullptr) {
629 proc->name = name8;
630 proc->proc = p;
631 return ERRCODE_NONE;
633 sal_Int32 i = name8.indexOf('#');
634 if (i != -1) {
635 name8 = name8.copy(0, i);
636 p = GetProcAddress(handle, name8.getStr());
637 if (p != nullptr) {
638 proc->name = name8;
639 proc->proc = p;
640 return ERRCODE_NONE;
643 OString real("_" + name8);
644 p = GetProcAddress(handle, real.getStr());
645 if (p != nullptr) {
646 proc->name = real;
647 proc->proc = p;
648 return ERRCODE_NONE;
650 real = name8 + "A";
651 p = GetProcAddress(handle, real.getStr());
652 if (p != nullptr) {
653 proc->name = real;
654 proc->proc = p;
655 return ERRCODE_NONE;
658 return ERRCODE_BASIC_PROC_UNDEFINED;
661 struct Dll: public salhelper::SimpleReferenceObject {
662 private:
663 typedef std::map< OUString, ProcData > Procs;
665 virtual ~Dll() override;
667 public:
668 Dll(): handle(nullptr) {}
670 ErrCode getProc(OUString const & name, ProcData * proc);
672 HMODULE handle;
673 Procs procs;
676 Dll::~Dll() {
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()) {
685 *proc = i->second;
686 return ERRCODE_NONE;
688 ErrCode e = getProcData(handle, name, proc);
689 if (e == ERRCODE_NONE) {
690 procs.emplace(name, *proc);
692 return e;
695 OUString fullDllName(OUString const & name) {
696 OUString full(name);
697 if (full.indexOf('.') == -1) {
698 full += ".DLL";
700 return full;
705 struct SbiDllMgr::Impl{
706 private:
707 typedef std::map< OUString, ::rtl::Reference< Dll > > Dlls;
709 public:
710 Impl() = default;
711 Impl(const Impl&) = delete;
712 const Impl& operator=(const Impl&) = delete;
714 Dll * getDll(OUString const & name);
716 Dlls dlls;
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()));
724 if (h == nullptr) {
725 dlls.erase(i);
726 return nullptr;
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;
745 ProcData proc;
746 ErrCode e = dll->getProc(OUString(function), &proc);
747 if (e != ERRCODE_NONE) {
748 return e;
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: */