1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include <utility>
#if defined(ACCESSIBILITY)
# include "HandlerData.h"
# include "mozilla/a11y/Platform.h"
# include "mozilla/mscom/ActivationContext.h"
#endif // defined(ACCESSIBILITY)
#include "mozilla/mscom/EnsureMTA.h"
#include "mozilla/mscom/ProxyStream.h"
#include "mozilla/mscom/Utils.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/mscom/Objref.h"
#include "nsExceptionHandler.h"
#include "nsPrintfCString.h"
#include "RegistrationAnnotator.h"
#include <windows.h>
#include <objbase.h>
#include <shlwapi.h>
namespace mozilla {
namespace mscom {
ProxyStream::ProxyStream()
: mGlobalLockedBuf(nullptr),
mHGlobal(nullptr),
mBufSize(0),
mPreserveStream(false) {}
// GetBuffer() fails with this variant, but that's okay because we're just
// reconstructing the stream from a buffer anyway.
ProxyStream::ProxyStream(REFIID aIID, const BYTE* aInitBuf,
const int aInitBufSize, Environment* aEnv)
: mGlobalLockedBuf(nullptr),
mHGlobal(nullptr),
mBufSize(aInitBufSize),
mPreserveStream(false) {
CrashReporter::Annotation kCrashReportKey =
CrashReporter::Annotation::ProxyStreamUnmarshalStatus;
if (!aInitBufSize) {
CrashReporter::AnnotateCrashReport(kCrashReportKey, "!aInitBufSize"_ns);
// We marshaled a nullptr. Nothing else to do here.
return;
}
HRESULT createStreamResult =
CreateStream(aInitBuf, aInitBufSize, getter_AddRefs(mStream));
if (FAILED(createStreamResult)) {
nsPrintfCString hrAsStr("0x%08X", createStreamResult);
CrashReporter::AnnotateCrashReport(kCrashReportKey, hrAsStr);
return;
}
// NB: We can't check for a null mStream until after we have checked for
// the zero aInitBufSize above. This is because InitStream will also fail
// in that case, even though marshaling a nullptr is allowable.
MOZ_ASSERT(mStream);
if (!mStream) {
CrashReporter::AnnotateCrashReport(kCrashReportKey, "!mStream"_ns);
return;
}
#if defined(ACCESSIBILITY)
const uint32_t expectedStreamLen = GetOBJREFSize(WrapNotNull(mStream));
nsAutoCString strActCtx;
nsAutoString manifestPath;
#endif // defined(ACCESSIBILITY)
HRESULT unmarshalResult = S_OK;
// We need to convert to an interface here otherwise we mess up const
// correctness with IPDL. We'll request an IUnknown and then QI the
// actual interface later.
#if defined(ACCESSIBILITY)
auto marshalFn = [this, &strActCtx, &manifestPath, &unmarshalResult, &aIID,
aEnv]() -> void
#else
auto marshalFn = [this, &unmarshalResult, &aIID, aEnv]() -> void
#endif // defined(ACCESSIBILITY)
{
if (aEnv) {
bool pushOk = aEnv->Push();
MOZ_DIAGNOSTIC_ASSERT(pushOk);
if (!pushOk) {
return;
}
}
auto popEnv = MakeScopeExit([aEnv]() -> void {
if (!aEnv) {
return;
}
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
bool popOk =
#endif
aEnv->Pop();
MOZ_DIAGNOSTIC_ASSERT(popOk);
});
#if defined(ACCESSIBILITY)
auto curActCtx = ActivationContext::GetCurrent();
if (curActCtx.isOk()) {
strActCtx.AppendPrintf("0x%p", curActCtx.unwrap());
} else {
strActCtx.AppendPrintf("HRESULT 0x%08X", curActCtx.unwrapErr());
}
ActivationContext::GetCurrentManifestPath(manifestPath);
#endif // defined(ACCESSIBILITY)
unmarshalResult = ::CoUnmarshalInterface(mStream, aIID,
getter_AddRefs(mUnmarshaledProxy));
MOZ_ASSERT(SUCCEEDED(unmarshalResult));
};
if (XRE_IsParentProcess()) {
// We'll marshal this stuff directly using the current thread, therefore its
// proxy will reside in the same apartment as the current thread.
marshalFn();
} else {
// When marshaling in child processes, we want to force the MTA.
EnsureMTA mta(marshalFn);
}
mStream = nullptr;
if (FAILED(unmarshalResult) || !mUnmarshaledProxy) {
nsPrintfCString hrAsStr("0x%08X", unmarshalResult);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::CoUnmarshalInterfaceResult, hrAsStr);
AnnotateInterfaceRegistration(aIID);
if (!mUnmarshaledProxy) {
CrashReporter::AnnotateCrashReport(kCrashReportKey,
"!mUnmarshaledProxy"_ns);
}
#if defined(ACCESSIBILITY)
AnnotateClassRegistration(CLSID_AccessibleHandler);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::UnmarshalActCtx, strActCtx);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::UnmarshalActCtxManifestPath,
NS_ConvertUTF16toUTF8(manifestPath));
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::A11yHandlerRegistered,
a11y::IsHandlerRegistered() ? "true"_ns : "false"_ns);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::ExpectedStreamLen, expectedStreamLen);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::ActualStreamLen, aInitBufSize);
#endif // defined(ACCESSIBILITY)
}
}
ProxyStream::ProxyStream(ProxyStream&& aOther)
: mGlobalLockedBuf(nullptr),
mHGlobal(nullptr),
mBufSize(0),
mPreserveStream(false) {
*this = std::move(aOther);
}
ProxyStream& ProxyStream::operator=(ProxyStream&& aOther) {
if (mHGlobal && mGlobalLockedBuf) {
DebugOnly<BOOL> result = ::GlobalUnlock(mHGlobal);
MOZ_ASSERT(!result && ::GetLastError() == NO_ERROR);
}
mStream = std::move(aOther.mStream);
mGlobalLockedBuf = aOther.mGlobalLockedBuf;
aOther.mGlobalLockedBuf = nullptr;
// ::GlobalFree() was called implicitly when mStream was replaced.
mHGlobal = aOther.mHGlobal;
aOther.mHGlobal = nullptr;
mBufSize = aOther.mBufSize;
aOther.mBufSize = 0;
mUnmarshaledProxy = std::move(aOther.mUnmarshaledProxy);
mPreserveStream = aOther.mPreserveStream;
return *this;
}
ProxyStream::~ProxyStream() {
if (mHGlobal && mGlobalLockedBuf) {
DebugOnly<BOOL> result = ::GlobalUnlock(mHGlobal);
MOZ_ASSERT(!result && ::GetLastError() == NO_ERROR);
// ::GlobalFree() is called implicitly when mStream is released
}
// If this assert triggers then we will be leaking a marshaled proxy!
// Call GetPreservedStream to obtain a preservable stream and then save it
// until the proxy is no longer needed.
MOZ_ASSERT(!mPreserveStream);
}
const BYTE* ProxyStream::GetBuffer(int& aReturnedBufSize) const {
aReturnedBufSize = 0;
if (!mStream) {
return nullptr;
}
if (!mGlobalLockedBuf) {
return nullptr;
}
aReturnedBufSize = mBufSize;
return mGlobalLockedBuf;
}
PreservedStreamPtr ProxyStream::GetPreservedStream() {
MOZ_ASSERT(mStream);
MOZ_ASSERT(mHGlobal);
if (!mStream || !mPreserveStream) {
return nullptr;
}
// Clone the stream so that the result has a distinct seek pointer.
RefPtr<IStream> cloned;
HRESULT hr = mStream->Clone(getter_AddRefs(cloned));
if (FAILED(hr)) {
return nullptr;
}
// Ensure the stream is rewound. We do this because CoReleaseMarshalData needs
// the stream to be pointing to the beginning of the marshal data.
LARGE_INTEGER pos;
pos.QuadPart = 0LL;
hr = cloned->Seek(pos, STREAM_SEEK_SET, nullptr);
if (FAILED(hr)) {
return nullptr;
}
mPreserveStream = false;
return ToPreservedStreamPtr(std::move(cloned));
}
bool ProxyStream::GetInterface(void** aOutInterface) {
// We should not have a locked buffer on this side
MOZ_ASSERT(!mGlobalLockedBuf);
MOZ_ASSERT(aOutInterface);
if (!aOutInterface) {
return false;
}
*aOutInterface = mUnmarshaledProxy.release();
return true;
}
ProxyStream::ProxyStream(REFIID aIID, IUnknown* aObject, Environment* aEnv,
ProxyStreamFlags aFlags)
: mGlobalLockedBuf(nullptr),
mHGlobal(nullptr),
mBufSize(0),
mPreserveStream(aFlags & ProxyStreamFlags::ePreservable) {
if (!aObject) {
return;
}
RefPtr<IStream> stream;
HGLOBAL hglobal = NULL;
int streamSize = 0;
DWORD mshlFlags = mPreserveStream ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_NORMAL;
HRESULT createStreamResult = S_OK;
HRESULT marshalResult = S_OK;
HRESULT statResult = S_OK;
HRESULT getHGlobalResult = S_OK;
nsAutoString manifestPath;
auto marshalFn = [&aIID, aObject, mshlFlags, &stream, &streamSize, &hglobal,
&createStreamResult, &marshalResult, &statResult,
&getHGlobalResult, aEnv, &manifestPath]() -> void {
if (aEnv) {
bool pushOk = aEnv->Push();
MOZ_DIAGNOSTIC_ASSERT(pushOk);
if (!pushOk) {
return;
}
}
auto popEnv = MakeScopeExit([aEnv]() -> void {
if (!aEnv) {
return;
}
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
bool popOk =
#endif
aEnv->Pop();
MOZ_DIAGNOSTIC_ASSERT(popOk);
});
createStreamResult =
::CreateStreamOnHGlobal(nullptr, TRUE, getter_AddRefs(stream));
if (FAILED(createStreamResult)) {
return;
}
#if defined(ACCESSIBILITY)
ActivationContext::GetCurrentManifestPath(manifestPath);
#endif // defined(ACCESSIBILITY)
marshalResult = ::CoMarshalInterface(stream, aIID, aObject, MSHCTX_LOCAL,
nullptr, mshlFlags);
MOZ_ASSERT(marshalResult != E_INVALIDARG);
if (FAILED(marshalResult)) {
return;
}
STATSTG statstg;
statResult = stream->Stat(&statstg, STATFLAG_NONAME);
if (SUCCEEDED(statResult)) {
streamSize = static_cast<int>(statstg.cbSize.LowPart);
} else {
return;
}
getHGlobalResult = ::GetHGlobalFromStream(stream, &hglobal);
MOZ_ASSERT(SUCCEEDED(getHGlobalResult));
};
if (XRE_IsParentProcess()) {
// We'll marshal this stuff directly using the current thread, therefore its
// stub will reside in the same apartment as the current thread.
marshalFn();
} else {
// When marshaling in child processes, we want to force the MTA.
EnsureMTA mta(marshalFn);
}
if (FAILED(createStreamResult)) {
nsPrintfCString hrAsStr("0x%08X", createStreamResult);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::CreateStreamOnHGlobalFailure, hrAsStr);
}
if (FAILED(marshalResult)) {
AnnotateInterfaceRegistration(aIID);
nsPrintfCString hrAsStr("0x%08X", marshalResult);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::CoMarshalInterfaceFailure, hrAsStr);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::MarshalActCtxManifestPath,
NS_ConvertUTF16toUTF8(manifestPath));
}
if (FAILED(statResult)) {
nsPrintfCString hrAsStr("0x%08X", statResult);
CrashReporter::AnnotateCrashReport(CrashReporter::Annotation::StatFailure,
hrAsStr);
}
if (FAILED(getHGlobalResult)) {
nsPrintfCString hrAsStr("0x%08X", getHGlobalResult);
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::GetHGlobalFromStreamFailure, hrAsStr);
}
mStream = std::move(stream);
if (streamSize) {
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::ProxyStreamSizeFrom, "IStream::Stat"_ns);
mBufSize = streamSize;
}
if (!hglobal) {
return;
}
mGlobalLockedBuf = reinterpret_cast<BYTE*>(::GlobalLock(hglobal));
mHGlobal = hglobal;
// If we couldn't get the stream size directly from mStream, we may use
// the size of the memory block allocated by the HGLOBAL, though it might
// be larger than the actual stream size.
if (!streamSize) {
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::ProxyStreamSizeFrom, "GlobalSize"_ns);
mBufSize = static_cast<int>(::GlobalSize(hglobal));
}
CrashReporter::AnnotateCrashReport(CrashReporter::Annotation::ProxyStreamSize,
mBufSize);
}
} // namespace mscom
} // namespace mozilla
|