summaryrefslogtreecommitdiffstats
path: root/ipc/mscom/StructStream.h
blob: 45a32c7687e39d4233ba68bd74be9ae90aa1b616 (plain)
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
/* -*- 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/. */

#ifndef mozilla_mscom_StructStream_h
#define mozilla_mscom_StructStream_h

#include "mozilla/Attributes.h"
#include "mozilla/UniquePtr.h"
#include "nscore.h"

#include <memory.h>
#include <midles.h>
#include <objidl.h>
#include <rpc.h>

/**
 * This code is used for (de)serializing data structures that have been
 * declared using midl, thus allowing us to use Microsoft RPC for marshaling
 * data for our COM handlers that may run in other processes that are not ours.
 */

namespace mozilla {
namespace mscom {

namespace detail {

typedef ULONG EncodedLenT;

}  // namespace detail

class MOZ_NON_TEMPORARY_CLASS StructToStream {
 public:
  /**
   * This constructor variant represents an empty/null struct to be serialized.
   */
  StructToStream()
      : mStatus(RPC_S_OK), mHandle(nullptr), mBuffer(nullptr), mEncodedLen(0) {}

  template <typename StructT>
  StructToStream(StructT& aSrcStruct, void (*aEncodeFnPtr)(handle_t, StructT*))
      : mStatus(RPC_X_INVALID_BUFFER),
        mHandle(nullptr),
        mBuffer(nullptr),
        mEncodedLen(0) {
    mStatus =
        ::MesEncodeDynBufferHandleCreate(&mBuffer, &mEncodedLen, &mHandle);
    if (mStatus != RPC_S_OK) {
      return;
    }

    MOZ_SEH_TRY { aEncodeFnPtr(mHandle, &aSrcStruct); }
#ifdef HAVE_SEH_EXCEPTIONS
    MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
      mStatus = ::RpcExceptionCode();
      return;
    }
#endif

    if (!mBuffer || !mEncodedLen) {
      mStatus = RPC_X_NO_MEMORY;
      return;
    }
  }

  ~StructToStream() {
    if (mHandle) {
      ::MesHandleFree(mHandle);
    }
    if (mBuffer) {
      // Bug 1440564: You'd think that MesHandleFree would free the buffer,
      // since it was created by RPC, but it doesn't.
      midl_user_free(mBuffer);
    }
  }

  static unsigned long GetEmptySize() { return sizeof(detail::EncodedLenT); }

  static HRESULT WriteEmpty(IStream* aDestStream) {
    StructToStream emptyStruct;
    return emptyStruct.Write(aDestStream);
  }

  explicit operator bool() const { return mStatus == RPC_S_OK; }

  bool IsEmpty() const { return mStatus == RPC_S_OK && !mEncodedLen; }

  unsigned long GetSize() const { return sizeof(mEncodedLen) + mEncodedLen; }

  HRESULT Write(IStream* aDestStream) {
    if (!aDestStream) {
      return E_INVALIDARG;
    }
    if (mStatus != RPC_S_OK) {
      return E_FAIL;
    }

    ULONG bytesWritten;
    HRESULT hr =
        aDestStream->Write(&mEncodedLen, sizeof(mEncodedLen), &bytesWritten);
    if (FAILED(hr)) {
      return hr;
    }
    if (bytesWritten != sizeof(mEncodedLen)) {
      return E_UNEXPECTED;
    }

    if (mBuffer && mEncodedLen) {
      hr = aDestStream->Write(mBuffer, mEncodedLen, &bytesWritten);
      if (FAILED(hr)) {
        return hr;
      }
      if (bytesWritten != mEncodedLen) {
        return E_UNEXPECTED;
      }
    }

    return hr;
  }

  StructToStream(const StructToStream&) = delete;
  StructToStream(StructToStream&&) = delete;
  StructToStream& operator=(const StructToStream&) = delete;
  StructToStream& operator=(StructToStream&&) = delete;

 private:
  RPC_STATUS mStatus;
  handle_t mHandle;
  char* mBuffer;
  detail::EncodedLenT mEncodedLen;
};

class MOZ_NON_TEMPORARY_CLASS StructFromStream {
  struct AlignedFreeDeleter {
    void operator()(void* aPtr) { ::_aligned_free(aPtr); }
  };

  static const detail::EncodedLenT kRpcReqdBufAlignment = 8;

 public:
  explicit StructFromStream(IStream* aStream)
      : mStatus(RPC_X_INVALID_BUFFER), mHandle(nullptr) {
    MOZ_ASSERT(aStream);

    // Read the length of the encoded data first
    detail::EncodedLenT encodedLen = 0;
    ULONG bytesRead = 0;
    HRESULT hr = aStream->Read(&encodedLen, sizeof(encodedLen), &bytesRead);
    if (FAILED(hr)) {
      return;
    }

    // NB: Some implementations of IStream return S_FALSE to indicate EOF,
    // other implementations return S_OK and set the number of bytes read to 0.
    // We must handle both.
    if (hr == S_FALSE || !bytesRead) {
      mStatus = RPC_S_OBJECT_NOT_FOUND;
      return;
    }

    if (bytesRead != sizeof(encodedLen)) {
      return;
    }

    if (!encodedLen) {
      mStatus = RPC_S_OBJECT_NOT_FOUND;
      return;
    }

    MOZ_ASSERT(encodedLen % kRpcReqdBufAlignment == 0);
    if (encodedLen % kRpcReqdBufAlignment) {
      return;
    }

    // This memory allocation is fallible
    mEncodedBuffer.reset(static_cast<char*>(
        ::_aligned_malloc(encodedLen, kRpcReqdBufAlignment)));
    if (!mEncodedBuffer) {
      return;
    }

    ULONG bytesReadFromStream = 0;
    hr = aStream->Read(mEncodedBuffer.get(), encodedLen, &bytesReadFromStream);
    if (FAILED(hr) || bytesReadFromStream != encodedLen) {
      return;
    }

    mStatus = ::MesDecodeBufferHandleCreate(mEncodedBuffer.get(), encodedLen,
                                            &mHandle);
  }

  ~StructFromStream() {
    if (mHandle) {
      ::MesHandleFree(mHandle);
    }
  }

  explicit operator bool() const { return mStatus == RPC_S_OK || IsEmpty(); }

  bool IsEmpty() const { return mStatus == RPC_S_OBJECT_NOT_FOUND; }

  template <typename StructT>
  bool Read(StructT* aDestStruct, void (*aDecodeFnPtr)(handle_t, StructT*)) {
    if (!aDestStruct || !aDecodeFnPtr || mStatus != RPC_S_OK) {
      return false;
    }

    // NB: Deserialization will fail with BSTRs unless the destination data
    //     is zeroed out!
    ZeroMemory(aDestStruct, sizeof(StructT));

    MOZ_SEH_TRY { aDecodeFnPtr(mHandle, aDestStruct); }
#ifdef HAVE_SEH_EXCEPTIONS
    MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
      mStatus = ::RpcExceptionCode();
      return false;
    }
#endif

    return true;
  }

  StructFromStream(const StructFromStream&) = delete;
  StructFromStream(StructFromStream&&) = delete;
  StructFromStream& operator=(const StructFromStream&) = delete;
  StructFromStream& operator=(StructFromStream&&) = delete;

 private:
  RPC_STATUS mStatus;
  handle_t mHandle;
  UniquePtr<char, AlignedFreeDeleter> mEncodedBuffer;
};

}  // namespace mscom
}  // namespace mozilla

#endif  // mozilla_mscom_StructStream_h