Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/net/NeckoMessageUtils.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef mozilla_net_NeckoMessageUtils_h
7
#define mozilla_net_NeckoMessageUtils_h
8
9
#include "mozilla/DebugOnly.h"
10
11
#include "ipc/IPCMessageUtils.h"
12
#include "nsExceptionHandler.h"
13
#include "nsPrintfCString.h"
14
#include "nsString.h"
15
#include "prio.h"
16
#include "mozilla/net/DNS.h"
17
#include "TimingStruct.h"
18
19
20
namespace IPC {
21
22
// nsIPermissionManager utilities
23
24
struct Permission
25
{
26
  nsCString origin, type;
27
  uint32_t capability, expireType;
28
  int64_t expireTime;
29
30
  Permission()
31
    : capability(0)
32
    , expireType(0)
33
    , expireTime(0)
34
0
  {}
35
36
  Permission(const nsCString& aOrigin,
37
             const nsCString& aType,
38
             const uint32_t aCapability,
39
             const uint32_t aExpireType,
40
             const int64_t aExpireTime) : origin(aOrigin),
41
                                          type(aType),
42
                                          capability(aCapability),
43
                                          expireType(aExpireType),
44
                                          expireTime(aExpireTime)
45
0
  {}
46
};
47
48
template<>
49
struct ParamTraits<Permission>
50
{
51
  static void Write(Message* aMsg, const Permission& aParam)
52
0
  {
53
0
    WriteParam(aMsg, aParam.origin);
54
0
    WriteParam(aMsg, aParam.type);
55
0
    WriteParam(aMsg, aParam.capability);
56
0
    WriteParam(aMsg, aParam.expireType);
57
0
    WriteParam(aMsg, aParam.expireTime);
58
0
  }
59
60
  static bool Read(const Message* aMsg, PickleIterator* aIter, Permission* aResult)
61
0
  {
62
0
    return ReadParam(aMsg, aIter, &aResult->origin) &&
63
0
           ReadParam(aMsg, aIter, &aResult->type) &&
64
0
           ReadParam(aMsg, aIter, &aResult->capability) &&
65
0
           ReadParam(aMsg, aIter, &aResult->expireType) &&
66
0
           ReadParam(aMsg, aIter, &aResult->expireTime);
67
0
  }
68
69
  static void Log(const Permission& p, std::wstring* l)
70
0
  {
71
0
    l->append(L"(");
72
0
    LogParam(p.origin, l);
73
0
    l->append(L", ");
74
0
    LogParam(p.capability, l);
75
0
    l->append(L", ");
76
0
    LogParam(p.expireTime, l);
77
0
    l->append(L", ");
78
0
    LogParam(p.expireType, l);
79
0
    l->append(L")");
80
0
  }
81
};
82
83
template<>
84
struct ParamTraits<mozilla::net::NetAddr>
85
{
86
  static void Write(Message* aMsg, const mozilla::net::NetAddr &aParam)
87
0
  {
88
0
    WriteParam(aMsg, aParam.raw.family);
89
0
    if (aParam.raw.family == AF_UNSPEC) {
90
0
      aMsg->WriteBytes(aParam.raw.data, sizeof(aParam.raw.data));
91
0
    } else if (aParam.raw.family == AF_INET) {
92
0
      WriteParam(aMsg, aParam.inet.port);
93
0
      WriteParam(aMsg, aParam.inet.ip);
94
0
    } else if (aParam.raw.family == AF_INET6) {
95
0
      WriteParam(aMsg, aParam.inet6.port);
96
0
      WriteParam(aMsg, aParam.inet6.flowinfo);
97
0
      WriteParam(aMsg, aParam.inet6.ip.u64[0]);
98
0
      WriteParam(aMsg, aParam.inet6.ip.u64[1]);
99
0
      WriteParam(aMsg, aParam.inet6.scope_id);
100
0
#if defined(XP_UNIX)
101
0
    } else if (aParam.raw.family == AF_LOCAL) {
102
0
      // Train's already off the rails:  let's get a stack trace at least...
103
0
      MOZ_CRASH("Error: please post stack trace to "
104
0
                      "https://bugzilla.mozilla.org/show_bug.cgi?id=661158");
105
0
      aMsg->WriteBytes(aParam.local.path, sizeof(aParam.local.path));
106
0
#endif
107
0
    } else {
108
0
      if (XRE_IsParentProcess()) {
109
0
        nsPrintfCString msg("%d", aParam.raw.family);
110
0
        CrashReporter::AnnotateCrashReport(
111
0
          CrashReporter::Annotation::UnknownNetAddrSocketFamily, msg);
112
0
      }
113
0
114
0
      MOZ_CRASH("Unknown socket family");
115
0
    }
116
0
  }
117
118
  static bool Read(const Message* aMsg, PickleIterator* aIter, mozilla::net::NetAddr* aResult)
119
0
  {
120
0
    if (!ReadParam(aMsg, aIter, &aResult->raw.family))
121
0
      return false;
122
0
123
0
    if (aResult->raw.family == AF_UNSPEC) {
124
0
      return aMsg->ReadBytesInto(aIter, &aResult->raw.data, sizeof(aResult->raw.data));
125
0
    } else if (aResult->raw.family == AF_INET) {
126
0
      return ReadParam(aMsg, aIter, &aResult->inet.port) &&
127
0
             ReadParam(aMsg, aIter, &aResult->inet.ip);
128
0
    } else if (aResult->raw.family == AF_INET6) {
129
0
      return ReadParam(aMsg, aIter, &aResult->inet6.port) &&
130
0
             ReadParam(aMsg, aIter, &aResult->inet6.flowinfo) &&
131
0
             ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[0]) &&
132
0
             ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[1]) &&
133
0
             ReadParam(aMsg, aIter, &aResult->inet6.scope_id);
134
0
#if defined(XP_UNIX)
135
0
    } else if (aResult->raw.family == AF_LOCAL) {
136
0
      return aMsg->ReadBytesInto(aIter, &aResult->local.path, sizeof(aResult->local.path));
137
0
#endif
138
0
    }
139
0
140
0
    /* We've been tricked by some socket family we don't know about! */
141
0
    return false;
142
0
  }
143
};
144
145
template<>
146
struct ParamTraits<mozilla::net::ResourceTimingStruct>
147
{
148
  static void Write(Message* aMsg, const mozilla::net::ResourceTimingStruct& aParam)
149
0
  {
150
0
    WriteParam(aMsg, aParam.domainLookupStart);
151
0
    WriteParam(aMsg, aParam.domainLookupEnd);
152
0
    WriteParam(aMsg, aParam.connectStart);
153
0
    WriteParam(aMsg, aParam.tcpConnectEnd);
154
0
    WriteParam(aMsg, aParam.secureConnectionStart);
155
0
    WriteParam(aMsg, aParam.connectEnd);
156
0
    WriteParam(aMsg, aParam.requestStart);
157
0
    WriteParam(aMsg, aParam.responseStart);
158
0
    WriteParam(aMsg, aParam.responseEnd);
159
0
160
0
    WriteParam(aMsg, aParam.fetchStart);
161
0
    WriteParam(aMsg, aParam.redirectStart);
162
0
    WriteParam(aMsg, aParam.redirectEnd);
163
0
164
0
    WriteParam(aMsg, aParam.transferSize);
165
0
    WriteParam(aMsg, aParam.encodedBodySize);
166
0
    WriteParam(aMsg, aParam.protocolVersion);
167
0
168
0
    WriteParam(aMsg, aParam.cacheReadStart);
169
0
    WriteParam(aMsg, aParam.cacheReadEnd);
170
0
  }
171
172
  static bool Read(const Message* aMsg, PickleIterator* aIter, mozilla::net::ResourceTimingStruct* aResult)
173
0
  {
174
0
    return ReadParam(aMsg, aIter, &aResult->domainLookupStart) &&
175
0
           ReadParam(aMsg, aIter, &aResult->domainLookupEnd) &&
176
0
           ReadParam(aMsg, aIter, &aResult->connectStart) &&
177
0
           ReadParam(aMsg, aIter, &aResult->tcpConnectEnd) &&
178
0
           ReadParam(aMsg, aIter, &aResult->secureConnectionStart) &&
179
0
           ReadParam(aMsg, aIter, &aResult->connectEnd) &&
180
0
           ReadParam(aMsg, aIter, &aResult->requestStart) &&
181
0
           ReadParam(aMsg, aIter, &aResult->responseStart) &&
182
0
           ReadParam(aMsg, aIter, &aResult->responseEnd) &&
183
0
           ReadParam(aMsg, aIter, &aResult->fetchStart) &&
184
0
           ReadParam(aMsg, aIter, &aResult->redirectStart) &&
185
0
           ReadParam(aMsg, aIter, &aResult->redirectEnd) &&
186
0
           ReadParam(aMsg, aIter, &aResult->transferSize) &&
187
0
           ReadParam(aMsg, aIter, &aResult->encodedBodySize) &&
188
0
           ReadParam(aMsg, aIter, &aResult->protocolVersion) &&
189
0
           ReadParam(aMsg, aIter, &aResult->cacheReadStart) &&
190
0
           ReadParam(aMsg, aIter, &aResult->cacheReadEnd);
191
0
  }
192
};
193
194
} // namespace IPC
195
196
#endif // mozilla_net_NeckoMessageUtils_h