2016-07-27 20:44:29 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2017-02-27 21:55:42 +03:00
|
|
|
#include "mozilla/Move.h"
|
2016-07-27 20:44:29 +03:00
|
|
|
#include "mozilla/mscom/EnsureMTA.h"
|
|
|
|
#include "mozilla/mscom/ProxyStream.h"
|
2016-08-30 04:55:37 +03:00
|
|
|
#include "mozilla/mscom/Utils.h"
|
2017-03-03 21:33:13 +03:00
|
|
|
|
2017-05-05 00:41:35 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
#include "InterfaceRegistrationAnnotator.h"
|
2017-02-27 21:55:42 +03:00
|
|
|
#include "nsExceptionHandler.h"
|
2017-05-12 00:22:19 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2017-03-03 21:33:13 +03:00
|
|
|
#endif
|
2016-07-27 20:44:29 +03:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <objbase.h>
|
|
|
|
#include <shlwapi.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
|
|
|
ProxyStream::ProxyStream()
|
|
|
|
: mGlobalLockedBuf(nullptr)
|
|
|
|
, mHGlobal(nullptr)
|
|
|
|
, mBufSize(0)
|
2017-08-17 01:31:07 +03:00
|
|
|
, mPreserveStream(false)
|
2016-07-27 20:44:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuffer() fails with this variant, but that's okay because we're just
|
|
|
|
// reconstructing the stream from a buffer anyway.
|
2017-05-19 22:59:49 +03:00
|
|
|
ProxyStream::ProxyStream(REFIID aIID, const BYTE* aInitBuf,
|
|
|
|
const int aInitBufSize)
|
2017-08-22 19:48:12 +03:00
|
|
|
: mGlobalLockedBuf(nullptr)
|
2016-07-27 20:44:29 +03:00
|
|
|
, mHGlobal(nullptr)
|
|
|
|
, mBufSize(aInitBufSize)
|
2017-08-17 01:31:07 +03:00
|
|
|
, mPreserveStream(false)
|
2016-07-27 20:44:29 +03:00
|
|
|
{
|
2017-08-10 00:07:11 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kCrashReportKey, "ProxyStreamUnmarshalStatus");
|
|
|
|
#endif
|
|
|
|
|
2017-08-22 19:48:12 +03:00
|
|
|
HRESULT createStreamResult = CreateStream(aInitBuf, aInitBufSize,
|
|
|
|
getter_AddRefs(mStream));
|
|
|
|
if (FAILED(createStreamResult)) {
|
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
nsPrintfCString hrAsStr("0x%08X", createStreamResult);
|
|
|
|
CrashReporter::AnnotateCrashReport(kCrashReportKey, hrAsStr);
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
if (!aInitBufSize) {
|
2017-08-10 00:07:11 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
CrashReporter::AnnotateCrashReport(kCrashReportKey,
|
|
|
|
NS_LITERAL_CSTRING("!aInitBufSize"));
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
2016-07-27 20:44:29 +03:00
|
|
|
// We marshaled a nullptr. Nothing else to do here.
|
|
|
|
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) {
|
2017-08-10 00:07:11 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
CrashReporter::AnnotateCrashReport(kCrashReportKey,
|
|
|
|
NS_LITERAL_CSTRING("!mStream"));
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
2016-07-27 20:44:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-19 22:59:49 +03:00
|
|
|
HRESULT unmarshalResult = S_OK;
|
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
auto marshalFn = [&]() -> void
|
|
|
|
{
|
|
|
|
// OK to forget mStream when calling into this function because the stream
|
|
|
|
// gets released even if the unmarshaling part fails.
|
2017-05-19 22:59:49 +03:00
|
|
|
unmarshalResult =
|
2017-07-13 00:00:27 +03:00
|
|
|
::CoGetInterfaceAndReleaseStream(mStream.forget().take(), aIID,
|
2017-03-15 03:42:24 +03:00
|
|
|
getter_AddRefs(mUnmarshaledProxy));
|
2017-05-19 22:59:49 +03:00
|
|
|
MOZ_ASSERT(SUCCEEDED(unmarshalResult));
|
2016-07-27 20:44:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-02-27 21:55:42 +03:00
|
|
|
|
2017-03-15 03:42:24 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
2017-05-19 22:59:49 +03:00
|
|
|
if (FAILED(unmarshalResult)) {
|
|
|
|
nsPrintfCString hrAsStr("0x%08X", unmarshalResult);
|
2017-02-27 21:55:42 +03:00
|
|
|
CrashReporter::AnnotateCrashReport(
|
|
|
|
NS_LITERAL_CSTRING("CoGetInterfaceAndReleaseStreamFailure"), hrAsStr);
|
2017-05-19 22:59:49 +03:00
|
|
|
AnnotateInterfaceRegistration(aIID);
|
2017-02-27 21:55:42 +03:00
|
|
|
}
|
2017-03-15 03:42:24 +03:00
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
2016-07-27 20:44:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ProxyStream::ProxyStream(ProxyStream&& aOther)
|
2017-08-10 00:07:11 +03:00
|
|
|
: mGlobalLockedBuf(nullptr)
|
|
|
|
, mHGlobal(nullptr)
|
|
|
|
, mBufSize(0)
|
2017-08-17 01:31:07 +03:00
|
|
|
, mPreserveStream(false)
|
2016-07-27 20:44:29 +03:00
|
|
|
{
|
|
|
|
*this = mozilla::Move(aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxyStream&
|
|
|
|
ProxyStream::operator=(ProxyStream&& aOther)
|
|
|
|
{
|
2017-08-10 00:07:11 +03:00
|
|
|
if (mHGlobal && mGlobalLockedBuf) {
|
|
|
|
DebugOnly<BOOL> result = ::GlobalUnlock(mHGlobal);
|
|
|
|
MOZ_ASSERT(!result && ::GetLastError() == NO_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-03-15 03:42:24 +03:00
|
|
|
mStream = Move(aOther.mStream);
|
2017-08-10 00:07:11 +03:00
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
mGlobalLockedBuf = aOther.mGlobalLockedBuf;
|
|
|
|
aOther.mGlobalLockedBuf = nullptr;
|
2017-08-10 00:07:11 +03:00
|
|
|
|
|
|
|
// ::GlobalFree() was called implicitly when mStream was replaced.
|
2016-07-27 20:44:29 +03:00
|
|
|
mHGlobal = aOther.mHGlobal;
|
|
|
|
aOther.mHGlobal = nullptr;
|
2017-08-10 00:07:11 +03:00
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
mBufSize = aOther.mBufSize;
|
|
|
|
aOther.mBufSize = 0;
|
2017-08-10 00:07:11 +03:00
|
|
|
|
2017-03-15 03:42:24 +03:00
|
|
|
mUnmarshaledProxy = Move(aOther.mUnmarshaledProxy);
|
2017-08-17 01:31:07 +03:00
|
|
|
|
|
|
|
mPreserveStream = aOther.mPreserveStream;
|
2016-07-27 20:44:29 +03:00
|
|
|
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
|
|
|
|
}
|
2017-08-17 01:31:07 +03:00
|
|
|
|
|
|
|
// 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);
|
2016-07-27 20:44:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const BYTE*
|
|
|
|
ProxyStream::GetBuffer(int& aReturnedBufSize) const
|
|
|
|
{
|
|
|
|
aReturnedBufSize = 0;
|
|
|
|
if (!mStream) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!mGlobalLockedBuf) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
aReturnedBufSize = mBufSize;
|
|
|
|
return mGlobalLockedBuf;
|
|
|
|
}
|
|
|
|
|
2017-08-17 01:31:07 +03:00
|
|
|
PreservedStreamPtr
|
|
|
|
ProxyStream::GetPreservedStream()
|
2017-07-19 21:07:45 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStream);
|
|
|
|
MOZ_ASSERT(mHGlobal);
|
|
|
|
|
2017-08-17 01:31:07 +03:00
|
|
|
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)) {
|
2017-07-19 21:07:45 +03:00
|
|
|
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;
|
2017-08-17 01:31:07 +03:00
|
|
|
hr = cloned->Seek(pos, STREAM_SEEK_SET, nullptr);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-19 21:07:45 +03:00
|
|
|
|
2017-08-17 01:31:07 +03:00
|
|
|
mPreserveStream = false;
|
|
|
|
return ToPreservedStreamPtr(Move(cloned));
|
2017-07-19 21:07:45 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
bool
|
2017-07-13 00:00:27 +03:00
|
|
|
ProxyStream::GetInterface(void** aOutInterface)
|
2016-07-27 20:44:29 +03:00
|
|
|
{
|
|
|
|
// We should not have a locked buffer on this side
|
|
|
|
MOZ_ASSERT(!mGlobalLockedBuf);
|
|
|
|
MOZ_ASSERT(aOutInterface);
|
|
|
|
|
|
|
|
if (!aOutInterface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-10 00:07:11 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
if (!mUnmarshaledProxy) {
|
|
|
|
CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("ProxyStreamUnmarshalStatus"),
|
|
|
|
NS_LITERAL_CSTRING("!mUnmarshaledProxy"));
|
|
|
|
}
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
|
|
|
|
2017-07-13 00:00:27 +03:00
|
|
|
*aOutInterface = mUnmarshaledProxy.release();
|
|
|
|
return true;
|
2016-07-27 20:44:29 +03:00
|
|
|
}
|
|
|
|
|
2017-08-17 01:31:07 +03:00
|
|
|
ProxyStream::ProxyStream(REFIID aIID, IUnknown* aObject,
|
|
|
|
ProxyStreamFlags aFlags)
|
2016-07-27 20:44:29 +03:00
|
|
|
: mGlobalLockedBuf(nullptr)
|
|
|
|
, mHGlobal(nullptr)
|
|
|
|
, mBufSize(0)
|
2017-08-17 01:31:07 +03:00
|
|
|
, mPreserveStream(aFlags & ProxyStreamFlags::ePreservable)
|
2016-07-27 20:44:29 +03:00
|
|
|
{
|
2017-03-15 03:42:24 +03:00
|
|
|
if (!aObject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
RefPtr<IStream> stream;
|
|
|
|
HGLOBAL hglobal = NULL;
|
2017-03-15 03:42:24 +03:00
|
|
|
int streamSize = 0;
|
2017-08-17 01:31:07 +03:00
|
|
|
DWORD mshlFlags = mPreserveStream ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_NORMAL;
|
2016-07-27 20:44:29 +03:00
|
|
|
|
2017-08-10 00:07:11 +03:00
|
|
|
HRESULT createStreamResult = S_OK;
|
2017-02-27 21:55:42 +03:00
|
|
|
HRESULT marshalResult = S_OK;
|
2017-08-10 00:07:11 +03:00
|
|
|
HRESULT statResult = S_OK;
|
|
|
|
HRESULT getHGlobalResult = S_OK;
|
2017-02-27 21:55:42 +03:00
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
auto marshalFn = [&]() -> void
|
|
|
|
{
|
2017-08-10 00:07:11 +03:00
|
|
|
createStreamResult = ::CreateStreamOnHGlobal(nullptr, TRUE, getter_AddRefs(stream));
|
|
|
|
if (FAILED(createStreamResult)) {
|
2016-07-27 20:44:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-10 00:07:11 +03:00
|
|
|
marshalResult = ::CoMarshalInterface(stream, aIID, aObject, MSHCTX_LOCAL,
|
2017-08-17 01:31:07 +03:00
|
|
|
nullptr, mshlFlags);
|
2017-08-10 00:07:11 +03:00
|
|
|
if (FAILED(marshalResult)) {
|
2016-07-27 20:44:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-15 03:42:24 +03:00
|
|
|
STATSTG statstg;
|
2017-08-10 00:07:11 +03:00
|
|
|
statResult = stream->Stat(&statstg, STATFLAG_NONAME);
|
|
|
|
if (SUCCEEDED(statResult)) {
|
2017-03-15 03:42:24 +03:00
|
|
|
streamSize = static_cast<int>(statstg.cbSize.LowPart);
|
2017-08-10 00:07:11 +03:00
|
|
|
} else {
|
|
|
|
return;
|
2017-03-15 03:42:24 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 00:07:11 +03:00
|
|
|
getHGlobalResult = ::GetHGlobalFromStream(stream, &hglobal);
|
|
|
|
MOZ_ASSERT(SUCCEEDED(getHGlobalResult));
|
2016-07-27 20:44:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-15 03:42:24 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
2017-08-10 00:07:11 +03:00
|
|
|
if (FAILED(createStreamResult)) {
|
|
|
|
nsPrintfCString hrAsStr("0x%08X", createStreamResult);
|
|
|
|
CrashReporter::AnnotateCrashReport(
|
|
|
|
NS_LITERAL_CSTRING("CreateStreamOnHGlobalFailure"),
|
|
|
|
hrAsStr);
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:55:42 +03:00
|
|
|
if (FAILED(marshalResult)) {
|
2017-05-05 00:41:35 +03:00
|
|
|
AnnotateInterfaceRegistration(aIID);
|
2017-02-27 21:55:42 +03:00
|
|
|
nsPrintfCString hrAsStr("0x%08X", marshalResult);
|
|
|
|
CrashReporter::AnnotateCrashReport(
|
|
|
|
NS_LITERAL_CSTRING("CoMarshalInterfaceFailure"), hrAsStr);
|
|
|
|
}
|
2017-08-10 00:07:11 +03:00
|
|
|
|
|
|
|
if (FAILED(statResult)) {
|
|
|
|
nsPrintfCString hrAsStr("0x%08X", statResult);
|
|
|
|
CrashReporter::AnnotateCrashReport(
|
|
|
|
NS_LITERAL_CSTRING("StatFailure"),
|
|
|
|
hrAsStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(getHGlobalResult)) {
|
|
|
|
nsPrintfCString hrAsStr("0x%08X", getHGlobalResult);
|
|
|
|
CrashReporter::AnnotateCrashReport(
|
|
|
|
NS_LITERAL_CSTRING("GetHGlobalFromStreamFailure"),
|
|
|
|
hrAsStr);
|
|
|
|
}
|
2017-03-15 03:42:24 +03:00
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
2017-02-27 21:55:42 +03:00
|
|
|
|
2016-07-27 20:44:29 +03:00
|
|
|
mStream = mozilla::Move(stream);
|
2017-03-15 03:42:24 +03:00
|
|
|
|
2017-08-10 00:07:11 +03:00
|
|
|
if (streamSize) {
|
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("ProxyStreamSizeFrom"),
|
|
|
|
NS_LITERAL_CSTRING("IStream::Stat"));
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
|
|
|
mBufSize = streamSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hglobal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGlobalLockedBuf = reinterpret_cast<BYTE*>(::GlobalLock(hglobal));
|
|
|
|
mHGlobal = hglobal;
|
2017-03-15 03:42:24 +03:00
|
|
|
|
2017-08-10 00:07:11 +03:00
|
|
|
// 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) {
|
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("ProxyStreamSizeFrom"),
|
|
|
|
NS_LITERAL_CSTRING("GlobalSize"));
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
|
|
|
mBufSize = static_cast<int>(::GlobalSize(hglobal));
|
2016-07-27 20:44:29 +03:00
|
|
|
}
|
2017-08-10 00:07:11 +03:00
|
|
|
|
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
nsAutoCString strBufSize;
|
|
|
|
strBufSize.AppendInt(mBufSize);
|
|
|
|
|
|
|
|
CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("ProxyStreamSize"),
|
|
|
|
strBufSize);
|
|
|
|
#endif // defined(MOZ_CRASHREPORTER)
|
2016-07-27 20:44:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|
|
|
|
|