arena.h 19.4 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:
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:
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.
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.
21

Kenton Varda's avatar
Kenton Varda committed
22 23
#ifndef CAPNP_ARENA_H_
#define CAPNP_ARENA_H_
24

25
#if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
26 27 28
#pragma GCC system_header
#endif

Kenton Varda's avatar
Kenton Varda committed
29
#ifndef CAPNP_PRIVATE
30 31
#error "This header is only meant to be included by Cap'n Proto's own source code."
#endif
32

Kenton Varda's avatar
Kenton Varda committed
33
#include <kj/common.h>
34
#include <kj/mutex.h>
35
#include <kj/exception.h>
36
#include <kj/vector.h>
37
#include <kj/units.h>
38
#include "common.h"
39
#include "message.h"
40
#include "layout.h"
41
#include <unordered_map>
42 43

#if !CAPNP_LITE
44
#include "capability.h"
45
#endif  // !CAPNP_LITE
46

47
namespace capnp {
48

49
#if !CAPNP_LITE
50
class ClientHook;
51
#endif  // !CAPNP_LITE
52

53
namespace _ {  // private
54 55 56 57 58 59 60

class SegmentReader;
class SegmentBuilder;
class Arena;
class BuilderArena;
class ReadLimiter;

Kenton Varda's avatar
Kenton Varda committed
61
class Segment;
62
typedef kj::Id<uint32_t, Segment> SegmentId;
Kenton Varda's avatar
Kenton Varda committed
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
class ReadLimiter {
  // Used to keep track of how much data has been processed from a message, and cut off further
  // processing if and when a particular limit is reached.  This is primarily intended to guard
  // against maliciously-crafted messages which contain cycles or overlapping structures.  Cycles
  // and overlapping are not permitted by the Cap'n Proto format because in many cases they could
  // be used to craft a deceptively small message which could consume excessive server resources to
  // process, perhaps even sending it into an infinite loop.  Actually detecting overlaps would be
  // time-consuming, so instead we just keep track of how many words worth of data structures the
  // receiver has actually dereferenced and error out if this gets too high.
  //
  // This counting takes place as you call getters (for non-primitive values) on the message
  // readers.  If you call the same getter twice, the data it returns may be double-counted.  This
  // should not be a big deal in most cases -- just set the read limit high enough that it will
  // only trigger in unreasonable cases.
78 79 80 81
  //
  // This class is "safe" to use from multiple threads for its intended use case.  Threads may
  // overwrite each others' changes to the counter, but this is OK because it only means that the
  // limit is enforced a bit less strictly -- it will still kick in eventually.
82 83 84 85 86

public:
  inline explicit ReadLimiter();                     // No limit.
  inline explicit ReadLimiter(WordCount64 limit);    // Limit to the given number of words.

87 88
  inline void reset(WordCount64 limit);

89
  KJ_ALWAYS_INLINE(bool canRead(WordCount64 amount, Arena* arena));
90

91 92 93 94
  void unread(WordCount64 amount);
  // Adds back some words to the limit.  Useful when the caller knows they are double-reading
  // some data.

95
private:
96 97 98 99
  volatile uint64_t limit;
  // Current limit, decremented each time catRead() is called.  Volatile because multiple threads
  // could be trying to modify it at once.  (This is not real thread-safety, but good enough for
  // the purpose of this class.  See class comment.)
100

101
  KJ_DISALLOW_COPY(ReadLimiter);
102 103
};

104
#if !CAPNP_LITE
105 106 107 108 109 110
class BrokenCapFactory {
  // Callback for constructing broken caps.  We use this so that we can avoid arena.c++ having a
  // link-time dependency on capability code that lives in libcapnp-rpc.

public:
  virtual kj::Own<ClientHook> newBrokenCap(kj::StringPtr description) = 0;
111
  virtual kj::Own<ClientHook> newNullCap() = 0;
112
};
113
#endif  // !CAPNP_LITE
114

115 116
class SegmentReader {
public:
117
  inline SegmentReader(Arena* arena, SegmentId id, const word* ptr, SegmentWordCount size,
118 119
                       ReadLimiter* readLimiter);

120 121 122 123 124 125 126 127 128 129 130 131 132 133
  KJ_ALWAYS_INLINE(const word* checkOffset(const word* from, ptrdiff_t offset));
  // Adds the given offset to the given pointer, checks that it is still within the bounds of the
  // segment, then returns it. Note that the "end" pointer of the segment (which technically points
  // to the word after the last in the segment) is considered in-bounds for this purpose, so you
  // can't necessarily dereference it. You must call checkObject() next to check that the object
  // you want to read is entirely in-bounds.
  //
  // If `from + offset` is out-of-range, this returns a pointer to the end of the segment. Thus,
  // any non-zero-sized object will fail `checkObject()`. We do this instead of throwing to save
  // some code footprint.

  KJ_ALWAYS_INLINE(bool checkObject(const word* start, WordCountN<31> size));
  // Assuming that `start` is in-bounds for this segment (probably checked using `checkOffset()`),
  // check that `start + size` is also in-bounds, and hence the whole area in-between is valid.
134

135 136 137 138 139
  KJ_ALWAYS_INLINE(bool amplifiedRead(WordCount virtualAmount));
  // Indicates that the reader should pretend that `virtualAmount` additional data was read even
  // though no actual pointer was traversed. This is used e.g. when reading a struct list pointer
  // where the element sizes are zero -- the sender could set the list size arbitrarily high and
  // cause the receiver to iterate over this list even though the message itself is small, so we
Andrew Murray's avatar
Andrew Murray committed
140
  // need to defend against DoS attacks based on this.
141

142 143 144 145
  inline Arena* getArena();
  inline SegmentId getSegmentId();

  inline const word* getStartPtr();
146 147
  inline SegmentWordCount getOffsetTo(const word* ptr);
  inline SegmentWordCount getSize();
148

149
  inline kj::ArrayPtr<const word> getArray();
150

151 152 153
  inline void unread(WordCount64 amount);
  // Add back some words to the ReadLimiter.

154 155 156
private:
  Arena* arena;
  SegmentId id;
157
  kj::ArrayPtr<const word> ptr;  // size guaranteed to fit in SEGMENT_WORD_COUNT_BITS bits
158 159
  ReadLimiter* readLimiter;

160
  KJ_DISALLOW_COPY(SegmentReader);
161 162

  friend class SegmentBuilder;
163 164 165

  static void abortCheckObjectFault();
  // Called in debug mode in cases that would segfault in opt mode. (Should be impossible!)
166 167 168 169
};

class SegmentBuilder: public SegmentReader {
public:
170 171 172
  inline SegmentBuilder(BuilderArena* arena, SegmentId id, word* ptr, SegmentWordCount size,
                        ReadLimiter* readLimiter, SegmentWordCount wordsUsed = ZERO * WORDS);
  inline SegmentBuilder(BuilderArena* arena, SegmentId id, const word* ptr, SegmentWordCount size,
173 174 175
                        ReadLimiter* readLimiter);
  inline SegmentBuilder(BuilderArena* arena, SegmentId id, decltype(nullptr),
                        ReadLimiter* readLimiter);
176

177
  KJ_ALWAYS_INLINE(word* allocate(SegmentWordCount amount));
178 179 180 181

  KJ_ALWAYS_INLINE(void checkWritable());
  // Throw an exception if the segment is read-only (meaning it is a reference to external data).

182
  KJ_ALWAYS_INLINE(word* getPtrUnchecked(SegmentWordCount offset));
183 184
  // Get a writable pointer into the segment.  Throws an exception if the segment is read-only (i.e.
  // a reference to external immutable data).
185 186 187

  inline BuilderArena* getArena();

188
  inline kj::ArrayPtr<const word> currentlyAllocated();
189

190 191
  inline void reset();

192 193
  inline bool isWritable() { return !readOnly; }

194 195 196 197
  inline void tryTruncate(word* from, word* to);
  // If `from` points just past the current end of the segment, then move the end back to `to`.
  // Otherwise, do nothing.

198 199 200 201 202
  inline bool tryExtend(word* from, word* to);
  // If `from` points just past the current end of the segment, and `to` is within the segment
  // boundaries, then move the end up to `to` and return true. Otherwise, do nothing and return
  // false.

203
private:
204
  word* pos;
205
  // Pointer to a pointer to the current end point of the segment, i.e. the location where the
206
  // next object should be allocated.
207

208 209 210 211
  bool readOnly;

  void throwNotWritable();

212
  KJ_DISALLOW_COPY(SegmentBuilder);
213 214
};

215 216
class Arena {
public:
217
  virtual ~Arena() noexcept(false);
218 219 220 221 222

  virtual SegmentReader* tryGetSegment(SegmentId id) = 0;
  // Gets the segment with the given ID, or return nullptr if no such segment exists.

  virtual void reportReadLimitReached() = 0;
223
  // Called to report that the read limit has been reached.  See ReadLimiter, below.  This invokes
224
  // the VALIDATE_INPUT() macro which may throw an exception; if it returns normally, the caller
225
  // will need to continue with default values.
226 227
};

228
class ReaderArena final: public Arena {
229
public:
230
  explicit ReaderArena(MessageReader* message);
231 232 233
  ~ReaderArena() noexcept(false);
  KJ_DISALLOW_COPY(ReaderArena);

234 235 236 237 238
  // implements Arena ------------------------------------------------
  SegmentReader* tryGetSegment(SegmentId id) override;
  void reportReadLimitReached() override;

private:
239
  MessageReader* message;
240
  ReadLimiter readLimiter;
241 242 243 244

  // Optimize for single-segment messages so that small messages are handled quickly.
  SegmentReader segment0;

245
  typedef std::unordered_map<uint, kj::Own<SegmentReader>> SegmentMap;
246
  kj::MutexGuarded<kj::Maybe<kj::Own<SegmentMap>>> moreSegments;
247 248 249 250 251 252 253
  // We need to mutex-guard the segment map because we lazily initialize segments when they are
  // first requested, but a Reader is allowed to be used concurrently in multiple threads.  Luckily
  // this only applies to large messages.
  //
  // TODO(perf):  Thread-local thing instead?  Some kind of lockless map?  Or do sharing of data
  //   in a different way, where you have to construct a new MessageReader in each thread (but
  //   possibly backed by the same data)?
254 255 256

  ReaderArena(MessageReader* message, kj::ArrayPtr<const word> firstSegment);
  ReaderArena(MessageReader* message, const word* firstSegment, SegmentWordCount firstSegmentSize);
257 258
};

259 260
class BuilderArena final: public Arena {
  // A BuilderArena that does not allow the injection of capabilities.
261

262
public:
263 264
  explicit BuilderArena(MessageBuilder* message);
  BuilderArena(MessageBuilder* message, kj::ArrayPtr<MessageBuilder::SegmentInit> segments);
265 266
  ~BuilderArena() noexcept(false);
  KJ_DISALLOW_COPY(BuilderArena);
267

268
  inline SegmentBuilder* getRootSegment() { return &segment0; }
269

270 271 272 273
  kj::ArrayPtr<const kj::ArrayPtr<const word>> getSegmentsForOutput();
  // Get an array of all the segments, suitable for writing out.  This only returns the allocated
  // portion of each segment, whereas tryGetSegment() returns something that includes
  // not-yet-allocated space.
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  inline CapTableBuilder* getLocalCapTable() {
    // Return a CapTableBuilder that merely implements local loopback. That is, you can set
    // capabilities, then read the same capabilities back, but there is no intent ever to transmit
    // these capabilities. A MessageBuilder that isn't imbued with some other CapTable uses this
    // by default.
    //
    // TODO(cleanup): It's sort of a hack that this exists. In theory, perhaps, unimbued
    //   MessageBuilders should throw exceptions on any attempt to access capability fields, like
    //   unimbued MessageReaders do. However, lots of code exists which uses MallocMessageBuilder
    //   as a temporary holder for data to be copied in and out (without being serialized), and it
    //   is expected that such data can include capabilities, which is admittedly reasonable.
    //   Therefore, all MessageBuilders must have a cap table by default. Arguably we should
    //   deprecate this usage and instead define a new helper type for this exact purpose.

    return &localCapTable;
  }
291

292
  SegmentBuilder* getSegment(SegmentId id);
293 294
  // Get the segment with the given id.  Crashes or throws an exception if no such segment exists.

295 296 297 298 299
  struct AllocateResult {
    SegmentBuilder* segment;
    word* words;
  };

300
  AllocateResult allocate(SegmentWordCount amount);
301 302 303 304
  // Find a segment with at least the given amount of space available and allocate the space.
  // Note that allocating directly from a particular segment is much faster, but allocating from
  // the arena is guaranteed to succeed.  Therefore callers should try to allocate from a specific
  // segment first if there is one, then fall back to the arena.
305

306 307 308 309 310 311 312 313 314 315 316
  SegmentBuilder* addExternalSegment(kj::ArrayPtr<const word> content);
  // Add a new segment to the arena which points to some existing memory region.  The segment is
  // assumed to be completley full; the arena will never allocate from it.  In fact, the segment
  // is considered read-only.  Any attempt to get a Builder pointing into this segment will throw
  // an exception.  Readers are allowed, however.
  //
  // This can be used to inject some external data into a message without a copy, e.g. embedding a
  // large mmap'd file into a message as `Data` without forcing that data to actually be read in
  // from disk (until the message itself is written out).  `Orphanage` provides the public API for
  // this feature.

317 318 319 320 321
  // implements Arena ------------------------------------------------
  SegmentReader* tryGetSegment(SegmentId id) override;
  void reportReadLimitReached() override;

private:
322
  MessageBuilder* message;
323
  ReadLimiter dummyLimiter;
324 325

  class LocalCapTable: public CapTableBuilder {
326
#if !CAPNP_LITE
327 328 329 330 331 332 333
  public:
    kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) override;
    uint injectCap(kj::Own<ClientHook>&& cap) override;
    void dropCap(uint index) override;

  private:
    kj::Vector<kj::Maybe<kj::Own<ClientHook>>> capTable;
334
#endif // ! CAPNP_LITE
335 336 337
  };

  LocalCapTable localCapTable;
338

339
  SegmentBuilder segment0;
340
  kj::ArrayPtr<const word> segment0ForOutput;
341

342
  struct MultiSegmentState {
343
    kj::Vector<kj::Own<SegmentBuilder>> builders;
344
    kj::Vector<kj::ArrayPtr<const word>> forOutput;
345
  };
346
  kj::Maybe<kj::Own<MultiSegmentState>> moreSegments;
347 348 349 350 351 352 353 354

  SegmentBuilder* segmentWithSpace = nullptr;
  // When allocating, look for space in this segment first before resorting to allocating a new
  // segment.  This is not necessarily the last segment because addExternalSegment() may add a
  // segment that is already-full, in which case we don't update this pointer.

  template <typename T>  // Can be `word` or `const word`.
  SegmentBuilder* addSegmentInternal(kj::ArrayPtr<T> content);
355 356 357 358 359
};

// =======================================================================================

inline ReadLimiter::ReadLimiter()
360
    : limit(kj::maxValue) {}
361

362
inline ReadLimiter::ReadLimiter(WordCount64 limit): limit(unbound(limit / WORDS)) {}
363

364
inline void ReadLimiter::reset(WordCount64 limit) { this->limit = unbound(limit / WORDS); }
365

366
inline bool ReadLimiter::canRead(WordCount64 amount, Arena* arena) {
367 368 369
  // Be careful not to store an underflowed value into `limit`, even if multiple threads are
  // decrementing it.
  uint64_t current = limit;
370
  if (KJ_UNLIKELY(unbound(amount / WORDS) > current)) {
371 372 373
    arena->reportReadLimitReached();
    return false;
  } else {
374
    limit = current - unbound(amount / WORDS);
375 376 377 378 379 380
    return true;
  }
}

// -------------------------------------------------------------------

381 382
inline SegmentReader::SegmentReader(Arena* arena, SegmentId id, const word* ptr,
                                    SegmentWordCount size, ReadLimiter* readLimiter)
383
    : arena(arena), id(id), ptr(kj::arrayPtr(ptr, unbound(size / WORDS))),
384
      readLimiter(readLimiter) {}
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
inline const word* SegmentReader::checkOffset(const word* from, ptrdiff_t offset) {
  ptrdiff_t min = ptr.begin() - from;
  ptrdiff_t max = ptr.end() - from;
  if (offset >= min && offset <= max) {
    return from + offset;
  } else {
    return ptr.end();
  }
}

inline bool SegmentReader::checkObject(const word* start, WordCountN<31> size) {
  auto startOffset = intervalLength(ptr.begin(), start, MAX_SEGMENT_WORDS);
#ifdef KJ_DEBUG
  if (startOffset > bounded(ptr.size()) * WORDS) {
    abortCheckObjectFault();
  }
#endif
  return startOffset + size <= bounded(ptr.size()) * WORDS &&
      readLimiter->canRead(size, arena);
405 406
}

407 408 409 410
inline bool SegmentReader::amplifiedRead(WordCount virtualAmount) {
  return readLimiter->canRead(virtualAmount, arena);
}

411 412 413
inline Arena* SegmentReader::getArena() { return arena; }
inline SegmentId SegmentReader::getSegmentId() { return id; }
inline const word* SegmentReader::getStartPtr() { return ptr.begin(); }
414
inline SegmentWordCount SegmentReader::getOffsetTo(const word* ptr) {
415 416
  KJ_IREQUIRE(this->ptr.begin() <= ptr && ptr <= this->ptr.end());
  return intervalLength(this->ptr.begin(), ptr, MAX_SEGMENT_WORDS);
417 418 419
}
inline SegmentWordCount SegmentReader::getSize() {
  return assumeBits<SEGMENT_WORD_COUNT_BITS>(ptr.size()) * WORDS;
420
}
421
inline kj::ArrayPtr<const word> SegmentReader::getArray() { return ptr; }
422
inline void SegmentReader::unread(WordCount64 amount) { readLimiter->unread(amount); }
423 424 425 426

// -------------------------------------------------------------------

inline SegmentBuilder::SegmentBuilder(
427 428 429 430
    BuilderArena* arena, SegmentId id, word* ptr, SegmentWordCount size,
    ReadLimiter* readLimiter, SegmentWordCount wordsUsed)
    : SegmentReader(arena, id, ptr, size, readLimiter),
      pos(ptr + wordsUsed), readOnly(false) {}
431
inline SegmentBuilder::SegmentBuilder(
432 433 434
    BuilderArena* arena, SegmentId id, const word* ptr, SegmentWordCount size,
    ReadLimiter* readLimiter)
    : SegmentReader(arena, id, ptr, size, readLimiter),
435 436
      // const_cast is safe here because the member won't ever be dereferenced because it appears
      // to point to the end of the segment anyway.
437
      pos(const_cast<word*>(ptr + size)), readOnly(true) {}
438 439
inline SegmentBuilder::SegmentBuilder(BuilderArena* arena, SegmentId id, decltype(nullptr),
                                      ReadLimiter* readLimiter)
440 441
    : SegmentReader(arena, id, nullptr, ZERO * WORDS, readLimiter),
      pos(nullptr), readOnly(false) {}
442

443
inline word* SegmentBuilder::allocate(SegmentWordCount amount) {
444
  if (intervalLength(pos, ptr.end(), MAX_SEGMENT_WORDS) < amount) {
445
    // Not enough space in the segment for this allocation.
446 447
    return nullptr;
  } else {
448
    // Success.
449 450
    word* result = pos;
    pos = pos + amount;
451 452 453 454
    return result;
  }
}

455 456 457 458
inline void SegmentBuilder::checkWritable() {
  if (KJ_UNLIKELY(readOnly)) throwNotWritable();
}

459
inline word* SegmentBuilder::getPtrUnchecked(SegmentWordCount offset) {
460 461 462 463 464 465 466 467 468
  return const_cast<word*>(ptr.begin() + offset);
}

inline BuilderArena* SegmentBuilder::getArena() {
  // Down-cast safe because SegmentBuilder's constructor always initializes its SegmentReader base
  // class with an Arena pointer that actually points to a BuilderArena.
  return static_cast<BuilderArena*>(arena);
}

469
inline kj::ArrayPtr<const word> SegmentBuilder::currentlyAllocated() {
470
  return kj::arrayPtr(ptr.begin(), pos - ptr.begin());
471 472
}

473
inline void SegmentBuilder::reset() {
474
  word* start = getPtrUnchecked(ZERO * WORDS);
475 476
  memset(start, 0, (pos - start) * sizeof(word));
  pos = start;
477 478
}

479 480 481 482
inline void SegmentBuilder::tryTruncate(word* from, word* to) {
  if (pos == from) pos = to;
}

483 484 485 486 487 488 489 490 491 492
inline bool SegmentBuilder::tryExtend(word* from, word* to) {
  // Careful about overflow.
  if (pos == from && to <= ptr.end() && to >= from) {
    pos = to;
    return true;
  } else {
    return false;
  }
}

493
}  // namespace _ (private)
494
}  // namespace capnp
495

Kenton Varda's avatar
Kenton Varda committed
496
#endif  // CAPNP_ARENA_H_