LibreOffice Module binaryurp (master) 1
reader.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <cassert>
23#include <exception>
24#include <memory>
25#include <vector>
26
27#include <com/sun/star/connection/XConnection.hpp>
28#include <com/sun/star/io/IOException.hpp>
29#include <com/sun/star/uno/Any.hxx>
30#include <com/sun/star/uno/Exception.hpp>
31#include <com/sun/star/uno/Reference.hxx>
32#include <com/sun/star/uno/RuntimeException.hpp>
33#include <com/sun/star/uno/Sequence.hxx>
34#include <com/sun/star/uno/Type.hxx>
35#include <com/sun/star/uno/XCurrentContext.hpp>
36#include <com/sun/star/uno/XInterface.hpp>
37#include <cppu/unotype.hxx>
38#include <o3tl/safeint.hxx>
39#include <rtl/byteseq.h>
40#include <rtl/ustring.hxx>
41#include <sal/log.hxx>
42#include <sal/types.h>
43#include <typelib/typeclass.h>
44#include <typelib/typedescription.h>
45#include <typelib/typedescription.hxx>
46
47#include "binaryany.hxx"
48#include "bridge.hxx"
49#include "incomingreply.hxx"
50#include "incomingrequest.hxx"
51#include "outgoingrequest.hxx"
52#include "reader.hxx"
54#include "unmarshal.hxx"
55
56namespace binaryurp {
57
58namespace {
59
60css::uno::Sequence< sal_Int8 > read(
61 css::uno::Reference< css::connection::XConnection > const & connection,
62 sal_uInt32 size, bool eofOk)
63{
64 assert(connection.is());
65 if (size > SAL_MAX_INT32) {
66 throw css::uno::RuntimeException(
67 "binaryurp::Reader: block size too large");
68 }
69 css::uno::Sequence< sal_Int8 > buf;
70 sal_Int32 n = connection->read(buf, static_cast< sal_Int32 >(size));
71 if (n == 0 && eofOk) {
72 return css::uno::Sequence< sal_Int8 >();
73 }
74 if (o3tl::make_unsigned(n) != size) {
75 throw css::io::IOException(
76 "binaryurp::Reader: premature end of input");
77 }
78 assert(o3tl::make_unsigned(buf.getLength()) == size);
79 return buf;
80}
81
82extern "C" void request(void * pThreadSpecificData) {
83 assert(pThreadSpecificData != nullptr);
84 std::unique_ptr< IncomingRequest >(
85 static_cast< IncomingRequest * >(pThreadSpecificData))->
86 execute();
87}
88
89}
90
92 Thread("binaryurpReader"), bridge_(bridge)
93{
94 assert(bridge.is());
95}
96
98
100 try {
101 bridge_->sendRequestChangeRequest();
102 css::uno::Reference< css::connection::XConnection > con(
103 bridge_->getConnection());
104 for (;;) {
105 css::uno::Sequence< sal_Int8 > s(read(con, 8, true));
106 if (!s.hasElements()) {
107 break;
108 }
110 sal_uInt32 size = header.read32();
111 sal_uInt32 count = header.read32();
112 header.done();
113 if (count == 0) {
114 throw css::io::IOException(
115 "binaryurp::Reader: block with zero message count received");
116 }
117 Unmarshal block(bridge_, state_, read(con, size, false));
118 for (sal_uInt32 i = 0; i != count; ++i) {
119 readMessage(block);
120 }
121 block.done();
122 }
123 } catch (const css::uno::Exception & e) {
124 SAL_WARN("binaryurp", "caught UNO exception '" << e << '\'');
125 } catch (const std::exception & e) {
126 SAL_WARN("binaryurp", "caught C++ exception '" << e.what() << '\'');
127 }
128 bridge_->terminate(false);
129 bridge_.clear();
130}
131
133 sal_uInt8 flags1 = unmarshal.read8();
134 bool newType;
135 bool newOid;
136 bool newTid;
137 bool forceSynchronous;
138 sal_uInt16 functionId;
139 if ((flags1 & 0x80) != 0) { // bit 7: LONGHEADER
140 if ((flags1 & 0x40) == 0) { // bit 6: REQUEST
141 readReplyMessage(unmarshal, flags1);
142 return;
143 }
144 newType = (flags1 & 0x20) != 0; // bit 5: NEWTYPE
145 newOid = (flags1 & 0x10) != 0; // bit 4: NEWOID
146 newTid = (flags1 & 0x08) != 0; // bit 3: NEWTID
147 if ((flags1 & 0x01) != 0) { // bit 0: MOREFLAGSS
148 sal_uInt8 flags2 = unmarshal.read8();
149 forceSynchronous = (flags2 & 0x80) != 0; // bit 7: MUSTREPLY
150 if (((flags2 & 0x40) != 0) != forceSynchronous) {
151 // bit 6: SYNCHRONOUS
152 throw css::uno::RuntimeException(
153 "URP: request message with MUSTREPLY != SYNCHRONOUS"
154 " received");
155 }
156 } else {
157 forceSynchronous = false;
158 }
159 functionId = ((flags1 & 0x04) != 0) // bit 2: FUNCTIONID16
160 ? unmarshal.read16() : unmarshal.read8();
161 } else {
162 newType = false;
163 newOid = false;
164 newTid = false;
165 forceSynchronous = false;
166 functionId = ((flags1 & 0x40) != 0) // bit 6: FUNCTIONID14
167 ? ((flags1 & 0x3F) << 8) | unmarshal.read8() : flags1 & 0x3F;
168 }
169 css::uno::TypeDescription type;
170 if (newType) {
171 type = unmarshal.readType();
172 lastType_ = type;
173 } else {
174 if (!lastType_.is()) {
175 throw css::uno::RuntimeException(
176 "URP: request message with NEWTYPE received when last"
177 " interface type has not yet been set");
178 }
179 type = lastType_;
180 }
181 OUString oid;
182 if (newOid) {
183 oid = unmarshal.readOid();
184 if (oid.isEmpty()) {
185 throw css::io::IOException(
186 "binaryurp::Unmarshal: empty OID");
187 }
188 lastOid_ = oid;
189 } else {
190 if (lastOid_.isEmpty()) {
191 throw css::uno::RuntimeException(
192 "URP: request message with NEWOID received when last OID has"
193 " not yet been set");
194 }
195 oid = lastOid_;
196 }
197 rtl::ByteSequence tid(getTid(unmarshal, newTid));
198 lastTid_ = tid;
199 type.makeComplete();
200 if (type.get()->eTypeClass != typelib_TypeClass_INTERFACE) {
201 throw css::uno::RuntimeException(
202 "URP: request message with non-interface interface type received");
203 }
204 typelib_InterfaceTypeDescription * itd =
205 reinterpret_cast< typelib_InterfaceTypeDescription * >(type.get());
206 if (functionId >= itd->nMapFunctionIndexToMemberIndex) {
207 throw css::uno::RuntimeException(
208 "URP: request message with unknown function ID received");
209 }
210 sal_Int32 memberId = itd->pMapFunctionIndexToMemberIndex[functionId];
211 css::uno::TypeDescription memberTd(itd->ppAllMembers[memberId]);
212 memberTd.makeComplete();
213 assert(memberTd.is());
214 bool protProps = bridge_->isProtocolPropertiesRequest(oid, type);
215 bool ccMode = !protProps && functionId != SPECIAL_FUNCTION_ID_RELEASE &&
216 bridge_->isCurrentContextMode();
217 css::uno::UnoInterfaceReference cc;
218 if (ccMode) {
219 css::uno::TypeDescription t(
221 cc.set(
222 *static_cast< uno_Interface ** >(
223 unmarshal.readValue(t).getValue(t)));
224 }
225 bool oneWay =
226 memberTd.get()->eTypeClass == typelib_TypeClass_INTERFACE_METHOD &&
227 (reinterpret_cast< typelib_InterfaceMethodTypeDescription * >(
228 memberTd.get())->
229 bOneWay);
231 !oneWay && forceSynchronous, "binaryurp",
232 ("superfluous MUSTREPLY/SYNCHRONOUS ignored in request message with"
233 " non-oneway function ID"));
234 bool synchronous = !oneWay || forceSynchronous;
235 bool bSetter = false;
236 std::vector< BinaryAny > inArgs;
237 switch (memberTd.get()->eTypeClass) {
238 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
239 bSetter = itd->pMapMemberIndexToFunctionIndex[memberId] != functionId;
240 // pMapMemberIndexToFunctionIndex contains function index of
241 // attribute getter
242 if (bSetter) {
243 inArgs.push_back(
244 unmarshal.readValue(
245 css::uno::TypeDescription(
246 reinterpret_cast<
247 typelib_InterfaceAttributeTypeDescription * >(
248 memberTd.get())->
249 pAttributeTypeRef)));
250 }
251 break;
252 case typelib_TypeClass_INTERFACE_METHOD:
253 {
254 typelib_InterfaceMethodTypeDescription * mtd =
255 reinterpret_cast< typelib_InterfaceMethodTypeDescription * >(
256 memberTd.get());
257 for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
258 if (mtd->pParams[i].bIn) {
259 inArgs.push_back(
260 unmarshal.readValue(
261 css::uno::TypeDescription(
262 mtd->pParams[i].pTypeRef)));
263 }
264 }
265 break;
266 }
267 default:
268 assert(false); // this cannot happen
269 break;
270 }
271 bridge_->incrementCalls(
272 !protProps && functionId != SPECIAL_FUNCTION_ID_RELEASE);
273 if (protProps) {
274 switch (functionId) {
276 bridge_->handleRequestChangeRequest(tid, inArgs);
277 break;
279 bridge_->handleCommitChangeRequest(tid, inArgs);
280 break;
281 default:
282 throw css::uno::RuntimeException(
283 "URP: request message with UrpProtocolProperties OID and"
284 " unknown function ID received");
285 }
286 } else {
287 css::uno::UnoInterfaceReference obj;
288 switch (functionId) {
290 obj = bridge_->findStub(oid, type);
291 if (!obj.is()) {
292 assert(
293 inArgs.size() == 1
294 && inArgs[0].getType().equals(
295 css::uno::TypeDescription(
297 if (!(type.equals(
298 css::uno::TypeDescription(
300 css::uno::Reference<
301 css::uno::XInterface > >::get()))
302 && (css::uno::TypeDescription(
303 *static_cast<
304 typelib_TypeDescriptionReference ** >(
305 inArgs[0].getValue(inArgs[0].getType()))).
306 equals(
307 css::uno::TypeDescription(
309 css::uno::Reference<
310 css::uno::XInterface > >::get())))))
311 {
312 throw css::uno::RuntimeException(
313 "URP: queryInterface request message with unknown OID '"
314 + oid + "' received");
315 }
316 }
317 break;
319 throw css::uno::RuntimeException(
320 "URP: request message with unknown function ID 1 received");
322 break;
323 default:
324 obj = bridge_->findStub(oid, type);
325 if (!obj.is()) {
326 throw css::uno::RuntimeException(
327 "URP: request message with unknown OID received");
328 }
329 break;
330 }
331 std::unique_ptr< IncomingRequest > req(
332 new IncomingRequest(
333 bridge_, tid, oid, obj, type, functionId, synchronous, memberTd,
334 bSetter, std::move(inArgs), ccMode, cc));
335 if (synchronous) {
336 bridge_->incrementActiveCalls();
337 }
339 bridge_->getThreadPool(), tid.getHandle(), req.get(), &request,
340 !synchronous);
341 // coverity[leaked_storage] - "request" destroys req when executed
342 req.release();
343 }
344}
345
347 rtl::ByteSequence tid(getTid(unmarshal, (flags1 & 0x08) != 0));
348 // bit 3: NEWTID
349 lastTid_ = tid;
350 OutgoingRequest req(bridge_->lastOutgoingRequest(tid));
351 bool exc = (flags1 & 0x20) != 0; // bit 5: EXCEPTION
352 BinaryAny ret;
353 std::vector< BinaryAny > outArgs;
354 if (exc) {
355 ret = unmarshal.readValue(
356 css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()));
358 (css::uno::TypeDescription(
360 get()),
361 ret.getType().get()))
362 {
363 sal_Int32 n = 0;
364 typelib_TypeDescriptionReference ** p = nullptr;
365 switch (req.member.get()->eTypeClass) {
366 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
367 {
368 typelib_InterfaceAttributeTypeDescription * atd =
369 reinterpret_cast<
370 typelib_InterfaceAttributeTypeDescription * >(
371 req.member.get());
372 n = req.setter ? atd->nSetExceptions : atd->nGetExceptions;
373 p = req.setter
374 ? atd->ppSetExceptions : atd->ppGetExceptions;
375 break;
376 }
377 case typelib_TypeClass_INTERFACE_METHOD:
378 {
379 typelib_InterfaceMethodTypeDescription * mtd =
380 reinterpret_cast<
381 typelib_InterfaceMethodTypeDescription * >(
382 req.member.get());
383 n = mtd->nExceptions;
384 p = mtd->ppExceptions;
385 break;
386 }
387 default:
388 assert(false); // this cannot happen
389 break;
390 }
391 bool bOk = false;
392 for (sal_Int32 i = 0; i != n; ++i) {
394 p[i],
395 reinterpret_cast< typelib_TypeDescriptionReference * >(
396 ret.getType().get())))
397 {
398 bOk = true;
399 break;
400 }
401 }
402 if (!bOk) {
403 throw css::uno::RuntimeException(
404 "URP: reply message with bad exception type received");
405 }
406 }
407 } else {
408 switch (req.member.get()->eTypeClass) {
409 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
410 if (!req.setter) {
411 ret = unmarshal.readValue(
412 css::uno::TypeDescription(
413 reinterpret_cast<
414 typelib_InterfaceAttributeTypeDescription * >(
415 req.member.get())->
416 pAttributeTypeRef));
417 }
418 break;
419 case typelib_TypeClass_INTERFACE_METHOD:
420 {
421 typelib_InterfaceMethodTypeDescription * mtd =
422 reinterpret_cast<
423 typelib_InterfaceMethodTypeDescription * >(
424 req.member.get());
425 ret = unmarshal.readValue(
426 css::uno::TypeDescription(mtd->pReturnTypeRef));
427 for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
428 if (mtd->pParams[i].bOut) {
429 outArgs.push_back(
430 unmarshal.readValue(
431 css::uno::TypeDescription(
432 mtd->pParams[i].pTypeRef)));
433 }
434 }
435 break;
436 }
437 default:
438 assert(false); // this cannot happen
439 break;
440 }
441 }
442 switch (req.kind) {
444 {
445 std::unique_ptr< IncomingReply > resp(
446 new IncomingReply(exc, ret, std::move(outArgs)));
448 bridge_->getThreadPool(), tid.getHandle(), resp.get(), nullptr,
449 false);
450 // coverity[leaked_storage] - "Bridge::makeCall" destroys resp when received
451 resp.release();
452 break;
453 }
455 assert(outArgs.empty());
456 bridge_->handleRequestChangeReply(exc, ret);
457 break;
459 assert(outArgs.empty());
460 bridge_->handleCommitChangeReply(exc, ret);
461 break;
462 default:
463 assert(false); // this cannot happen
464 break;
465 }
466}
467
468rtl::ByteSequence Reader::getTid(Unmarshal & unmarshal, bool newTid) const {
469 if (newTid) {
470 return unmarshal.readTid();
471 }
472 if (lastTid_.getLength() == 0) {
473 throw css::uno::RuntimeException(
474 "URP: message with NEWTID received when last TID has not yet been"
475 " set");
476 }
477 return lastTid_;
478}
479
480}
481
482/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_Int8 header[]
XPropertyListType t
com::sun::star::uno::TypeDescription getType() const noexcept
Definition: binaryany.cxx:97
void * getValue(com::sun::star::uno::TypeDescription const &type) const noexcept
Definition: binaryany.cxx:101
virtual void execute() override
Definition: reader.cxx:99
com::sun::star::uno::TypeDescription lastType_
Definition: reader.hxx:58
rtl::ByteSequence getTid(Unmarshal &unmarshal, bool newTid) const
Definition: reader.cxx:468
virtual ~Reader() override
Definition: reader.cxx:97
ReaderState state_
Definition: reader.hxx:61
OUString lastOid_
Definition: reader.hxx:59
rtl::Reference< Bridge > bridge_
Definition: reader.hxx:57
rtl::ByteSequence lastTid_
Definition: reader.hxx:60
void readReplyMessage(Unmarshal &unmarshal, sal_uInt8 flags1)
Definition: reader.cxx:346
Reader(rtl::Reference< Bridge > const &bridge)
Definition: reader.cxx:91
void readMessage(Unmarshal &unmarshal)
Definition: reader.cxx:132
rtl::ByteSequence readTid()
Definition: unmarshal.cxx:225
com::sun::star::uno::TypeDescription readType()
Definition: unmarshal.cxx:123
sal_uInt16 read16()
Definition: unmarshal.cxx:109
void done() const
Definition: unmarshal.cxx:356
BinaryAny readValue(com::sun::star::uno::TypeDescription const &type)
Definition: unmarshal.cxx:248
void * p
sal_Int64 n
#define SAL_INFO_IF(condition, area, stream)
#define SAL_WARN(area, stream)
css::uno::UnoInterfaceReference get()
@ SPECIAL_FUNCTION_ID_COMMIT_CHANGE
@ SPECIAL_FUNCTION_ID_REQUEST_CHANGE
@ SPECIAL_FUNCTION_ID_QUERY_INTERFACE
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
con
com::sun::star::uno::TypeDescription member
void SAL_CALL uno_threadpool_putJob(uno_ThreadPool hPool, sal_Sequence *pThreadId, void *pJob, void(SAL_CALL *doRequest)(void *pThreadSpecificData), sal_Bool bIsOneway) SAL_THROW_EXTERN_C()
sal_Bool SAL_CALL typelib_typedescription_isAssignableFrom(typelib_TypeDescription *pAssignable, typelib_TypeDescription *pFrom) SAL_THROW_EXTERN_C()
sal_Bool SAL_CALL typelib_typedescriptionreference_isAssignableFrom(typelib_TypeDescriptionReference *pAssignable, typelib_TypeDescriptionReference *pFrom) SAL_THROW_EXTERN_C()
unsigned char sal_uInt8
ResultType type