serialize.c++ 9.93 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
Kenton Varda's avatar
Kenton Varda committed
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
Kenton Varda's avatar
Kenton Varda committed
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
Kenton Varda's avatar
Kenton Varda committed
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Kenton Varda's avatar
Kenton Varda committed
21 22

#include "serialize.h"
23
#include "layout.h"
Kenton Varda's avatar
Kenton Varda committed
24
#include <kj/debug.h>
25
#include <exception>
Kenton Varda's avatar
Kenton Varda committed
26

27
namespace capnp {
Kenton Varda's avatar
Kenton Varda committed
28

29 30
FlatArrayMessageReader::FlatArrayMessageReader(
    kj::ArrayPtr<const word> array, ReaderOptions options)
31
    : MessageReader(options), end(array.end()) {
Kenton Varda's avatar
Kenton Varda committed
32 33 34 35 36
  if (array.size() < 1) {
    // Assume empty message.
    return;
  }

37 38
  const _::WireValue<uint32_t>* table =
      reinterpret_cast<const _::WireValue<uint32_t>*>(array.begin());
Kenton Varda's avatar
Kenton Varda committed
39

40
  uint segmentCount = table[0].get() + 1;
Kenton Varda's avatar
Kenton Varda committed
41 42
  size_t offset = segmentCount / 2u + 1u;

43
  KJ_REQUIRE(array.size() >= offset, "Message ends prematurely in segment table.") {
Kenton Varda's avatar
Kenton Varda committed
44 45 46 47
    return;
  }

  if (segmentCount == 0) {
48
    end = array.begin() + offset;
Kenton Varda's avatar
Kenton Varda committed
49 50 51
    return;
  }

52 53
  {
    uint segmentSize = table[1].get();
Kenton Varda's avatar
Kenton Varda committed
54

55 56 57 58
    KJ_REQUIRE(array.size() >= offset + segmentSize,
               "Message ends prematurely in first segment.") {
      return;
    }
Kenton Varda's avatar
Kenton Varda committed
59

60 61 62
    segment0 = array.slice(offset, offset + segmentSize);
    offset += segmentSize;
  }
Kenton Varda's avatar
Kenton Varda committed
63 64

  if (segmentCount > 1) {
Kenton Varda's avatar
Kenton Varda committed
65
    moreSegments = kj::heapArray<kj::ArrayPtr<const word>>(segmentCount - 1);
Kenton Varda's avatar
Kenton Varda committed
66 67 68 69

    for (uint i = 1; i < segmentCount; i++) {
      uint segmentSize = table[i + 1].get();

70
      KJ_REQUIRE(array.size() >= offset + segmentSize, "Message ends prematurely.") {
Kenton Varda's avatar
Kenton Varda committed
71 72 73 74 75 76 77 78
        moreSegments = nullptr;
        return;
      }

      moreSegments[i - 1] = array.slice(offset, offset + segmentSize);
      offset += segmentSize;
    }
  }
79 80

  end = array.begin() + offset;
Kenton Varda's avatar
Kenton Varda committed
81 82
}

83
kj::ArrayPtr<const word> FlatArrayMessageReader::getSegment(uint id) {
Kenton Varda's avatar
Kenton Varda committed
84 85 86 87 88 89 90 91 92
  if (id == 0) {
    return segment0;
  } else if (id <= moreSegments.size()) {
    return moreSegments[id - 1];
  } else {
    return nullptr;
  }
}

93 94 95 96 97 98 99
kj::ArrayPtr<const word> initMessageBuilderFromFlatArrayCopy(
    kj::ArrayPtr<const word> array, MessageBuilder& target, ReaderOptions options) {
  FlatArrayMessageReader reader(array, options);
  target.setRoot(reader.getRoot<AnyPointer>());
  return kj::arrayPtr(reader.getEnd(), array.end());
}

100
kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
101
  kj::Array<word> result = kj::heapArray<word>(computeSerializedSizeInWords(segments));
Kenton Varda's avatar
Kenton Varda committed
102

103 104
  _::WireValue<uint32_t>* table =
      reinterpret_cast<_::WireValue<uint32_t>*>(result.begin());
Kenton Varda's avatar
Kenton Varda committed
105

106 107 108
  // We write the segment count - 1 because this makes the first word zero for single-segment
  // messages, improving compression.  We don't bother doing this with segment sizes because
  // one-word segments are rare anyway.
109
  table[0].set(segments.size() - 1);
Kenton Varda's avatar
Kenton Varda committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  for (uint i = 0; i < segments.size(); i++) {
    table[i + 1].set(segments[i].size());
  }

  if (segments.size() % 2 == 0) {
    // Set padding byte.
    table[segments.size() + 1].set(0);
  }

  word* dst = result.begin() + segments.size() / 2 + 1;

  for (auto& segment: segments) {
    memcpy(dst, segment.begin(), segment.size() * sizeof(word));
    dst += segment.size();
  }

127
  KJ_DASSERT(dst == result.end(), "Buffer overrun/underrun bug in code above.");
Kenton Varda's avatar
Kenton Varda committed
128

Kenton Varda's avatar
Kenton Varda committed
129
  return kj::mv(result);
Kenton Varda's avatar
Kenton Varda committed
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143
size_t computeSerializedSizeInWords(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
  KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message.");

  size_t totalSize = segments.size() / 2 + 1;

  for (auto& segment: segments) {
    totalSize += segment.size();
  }

  return totalSize;
}

Kenton Varda's avatar
Kenton Varda committed
144 145
// =======================================================================================

146
InputStreamMessageReader::InputStreamMessageReader(
147
    kj::InputStream& inputStream, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
148
    : MessageReader(options), inputStream(inputStream), readPos(nullptr) {
149
  _::WireValue<uint32_t> firstWord[2];
Kenton Varda's avatar
Kenton Varda committed
150

Kenton Varda's avatar
Kenton Varda committed
151
  inputStream.read(firstWord, sizeof(firstWord));
Kenton Varda's avatar
Kenton Varda committed
152

153
  uint segmentCount = firstWord[0].get() + 1;
154
  uint segment0Size = segmentCount == 0 ? 0 : firstWord[1].get();
Kenton Varda's avatar
Kenton Varda committed
155

156
  size_t totalWords = segment0Size;
Kenton Varda's avatar
Kenton Varda committed
157

158
  // Reject messages with too many segments for security reasons.
159
  KJ_REQUIRE(segmentCount < 512, "Message has too many segments.") {
160 161
    segmentCount = 1;
    segment0Size = 1;
162
    break;
163
  }
164

165
  // Read sizes for all segments except the first.  Include padding if necessary.
166
  KJ_STACK_ARRAY(_::WireValue<uint32_t>, moreSizes, segmentCount & ~1, 16, 64);
167
  if (segmentCount > 1) {
168
    inputStream.read(moreSizes.begin(), moreSizes.size() * sizeof(moreSizes[0]));
169 170
    for (uint i = 0; i < segmentCount - 1; i++) {
      totalWords += moreSizes[i].get();
Kenton Varda's avatar
Kenton Varda committed
171 172 173
    }
  }

174 175 176
  // Don't accept a message which the receiver couldn't possibly traverse without hitting the
  // traversal limit.  Without this check, a malicious client could transmit a very large segment
  // size to make the receiver allocate excessive space and possibly crash.
177 178
  KJ_REQUIRE(totalWords <= options.traversalLimitInWords,
             "Message is too large.  To increase the limit on the receiving end, see "
179
             "capnp::ReaderOptions.") {
180
    segmentCount = 1;
181
    segment0Size = kj::min(segment0Size, options.traversalLimitInWords);
182
    totalWords = segment0Size;
183
    break;
184
  }
185

186
  if (scratchSpace.size() < totalWords) {
187 188
    // TODO(perf):  Consider allocating each segment as a separate chunk to reduce memory
    //   fragmentation.
Kenton Varda's avatar
Kenton Varda committed
189
    ownedSpace = kj::heapArray<word>(totalWords);
190
    scratchSpace = ownedSpace;
Kenton Varda's avatar
Kenton Varda committed
191 192
  }

193
  segment0 = scratchSpace.slice(0, segment0Size);
Kenton Varda's avatar
Kenton Varda committed
194

195
  if (segmentCount > 1) {
Kenton Varda's avatar
Kenton Varda committed
196
    moreSegments = kj::heapArray<kj::ArrayPtr<const word>>(segmentCount - 1);
197
    size_t offset = segment0Size;
Kenton Varda's avatar
Kenton Varda committed
198

199 200 201 202
    for (uint i = 0; i < segmentCount - 1; i++) {
      uint segmentSize = moreSizes[i].get();
      moreSegments[i] = scratchSpace.slice(offset, offset + segmentSize);
      offset += segmentSize;
Kenton Varda's avatar
Kenton Varda committed
203 204 205
    }
  }

206
  if (segmentCount == 1) {
Kenton Varda's avatar
Kenton Varda committed
207
    inputStream.read(scratchSpace.begin(), totalWords * sizeof(word));
208
  } else if (segmentCount > 1) {
209
    readPos = scratchSpace.asBytes().begin();
210
    readPos += inputStream.read(readPos, segment0Size * sizeof(word), totalWords * sizeof(word));
Kenton Varda's avatar
Kenton Varda committed
211
  }
212
}
Kenton Varda's avatar
Kenton Varda committed
213

214
InputStreamMessageReader::~InputStreamMessageReader() noexcept(false) {
215
  if (readPos != nullptr) {
216 217 218 219
    unwindDetector.catchExceptionsIfUnwinding([&]() {
      // Note that lazy reads only happen when we have multiple segments, so moreSegments.back() is
      // valid.
      const byte* allEnd = reinterpret_cast<const byte*>(moreSegments.back().end());
Kenton Varda's avatar
Kenton Varda committed
220
      inputStream.skip(allEnd - readPos);
221
    });
Kenton Varda's avatar
Kenton Varda committed
222 223 224
  }
}

225
kj::ArrayPtr<const word> InputStreamMessageReader::getSegment(uint id) {
Kenton Varda's avatar
Kenton Varda committed
226 227 228 229
  if (id > moreSegments.size()) {
    return nullptr;
  }

230
  kj::ArrayPtr<const word> segment = id == 0 ? segment0 : moreSegments[id - 1];
Kenton Varda's avatar
Kenton Varda committed
231

232 233 234 235 236 237 238 239
  if (readPos != nullptr) {
    // May need to lazily read more data.
    const byte* segmentEnd = reinterpret_cast<const byte*>(segment.end());
    if (readPos < segmentEnd) {
      // Note that lazy reads only happen when we have multiple segments, so moreSegments.back() is
      // valid.
      const byte* allEnd = reinterpret_cast<const byte*>(moreSegments.back().end());
      readPos += inputStream.read(readPos, segmentEnd - readPos, allEnd - readPos);
Kenton Varda's avatar
Kenton Varda committed
240 241 242
    }
  }

243
  return segment;
Kenton Varda's avatar
Kenton Varda committed
244 245
}

246 247 248 249 250 251
void readMessageCopy(kj::InputStream& input, MessageBuilder& target,
                     ReaderOptions options, kj::ArrayPtr<word> scratchSpace) {
  InputStreamMessageReader message(input, options, scratchSpace);
  target.setRoot(message.getRoot<AnyPointer>());
}

Kenton Varda's avatar
Kenton Varda committed
252 253
// -------------------------------------------------------------------

254
void writeMessage(kj::OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
255
  KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message.");
256

257
  KJ_STACK_ARRAY(_::WireValue<uint32_t>, table, (segments.size() + 2) & ~size_t(1), 16, 64);
Kenton Varda's avatar
Kenton Varda committed
258

259 260 261
  // We write the segment count - 1 because this makes the first word zero for single-segment
  // messages, improving compression.  We don't bother doing this with segment sizes because
  // one-word segments are rare anyway.
262
  table[0].set(segments.size() - 1);
Kenton Varda's avatar
Kenton Varda committed
263 264 265 266 267 268 269 270
  for (uint i = 0; i < segments.size(); i++) {
    table[i + 1].set(segments[i].size());
  }
  if (segments.size() % 2 == 0) {
    // Set padding byte.
    table[segments.size() + 1].set(0);
  }

271
  KJ_STACK_ARRAY(kj::ArrayPtr<const byte>, pieces, segments.size() + 1, 4, 32);
272
  pieces[0] = table.asBytes();
Kenton Varda's avatar
Kenton Varda committed
273

274
  for (uint i = 0; i < segments.size(); i++) {
275
    pieces[i + 1] = segments[i].asBytes();
Kenton Varda's avatar
Kenton Varda committed
276
  }
277

Kenton Varda's avatar
Kenton Varda committed
278
  output.write(pieces);
Kenton Varda's avatar
Kenton Varda committed
279 280 281
}

// =======================================================================================
282

283
StreamFdMessageReader::~StreamFdMessageReader() noexcept(false) {}
Kenton Varda's avatar
Kenton Varda committed
284

285
void writeMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
286
  kj::FdOutputStream stream(fd);
Kenton Varda's avatar
Kenton Varda committed
287 288 289
  writeMessage(stream, segments);
}

290 291 292 293 294 295
void readMessageCopyFromFd(int fd, MessageBuilder& target,
                           ReaderOptions options, kj::ArrayPtr<word> scratchSpace) {
  kj::FdInputStream stream(fd);
  readMessageCopy(stream, target, options, scratchSpace);
}

296
}  // namespace capnp