layout.h 41.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Kenton Varda's avatar
Kenton Varda committed
24 25
// This file is NOT intended for use by clients, except in generated code.
//
Kenton Varda's avatar
Kenton Varda committed
26 27 28 29
// This file defines low-level, non-type-safe classes for traversing the Cap'n Proto memory layout
// (which is also its wire format).  Code generated by the Cap'n Proto compiler uses these classes,
// as does other parts of the Cap'n proto library which provide a higher-level interface for
// dynamic introspection.
Kenton Varda's avatar
Kenton Varda committed
30

Kenton Varda's avatar
Kenton Varda committed
31 32
#ifndef CAPNP_LAYOUT_H_
#define CAPNP_LAYOUT_H_
33

Kenton Varda's avatar
Kenton Varda committed
34
#include <kj/common.h>
35
#include <kj/memory.h>
36
#include "common.h"
Kenton Varda's avatar
Kenton Varda committed
37
#include "blob.h"
38
#include "endian.h"
39

40
namespace capnp {
41

42
class ClientHook;
43

44
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
45

46 47
class PointerBuilder;
class PointerReader;
48 49 50 51
class StructBuilder;
class StructReader;
class ListBuilder;
class ListReader;
52
class OrphanBuilder;
53
struct WirePointer;
54
struct WireHelpers;
55 56
class SegmentReader;
class SegmentBuilder;
57
class Arena;
58
class BuilderArena;
59 60
class ImbuedReaderArena;
class ImbuedBuilderArena;
61

62
// =============================================================================
63 64

enum class FieldSize: uint8_t {
65
  // TODO(cleanup):  Rename to FieldLayout or maybe ValueLayout.
66

67 68 69 70 71
  // Notice that each member of this enum, when representing a list element size, represents a
  // size that is greater than or equal to the previous members, since INLINE_COMPOSITE is used
  // only for multi-word structs.  This is important because it allows us to compare FieldSize
  // values for the purpose of deciding when we need to upgrade a list.

72 73 74 75 76 77 78
  VOID = 0,
  BIT = 1,
  BYTE = 2,
  TWO_BYTES = 3,
  FOUR_BYTES = 4,
  EIGHT_BYTES = 5,

79
  POINTER = 6,  // Indicates that the field lives in the pointer section, not the data section.
80 81 82 83

  INLINE_COMPOSITE = 7
  // A composite type of fixed width.  This serves two purposes:
  // 1) For lists of composite types where all the elements would have the exact same width,
84
  //    allocating a list of pointers which in turn point at the elements would waste space.  We
85 86 87
  //    can avoid a layer of indirection by placing all the elements in a flat sequence, and only
  //    indicating the element properties (e.g. field count for structs) once.
  //
88 89
  //    Specifically, a list pointer indicating INLINE_COMPOSITE element size actually points to
  //    a "tag" describing one element.  This tag is formatted like a wire pointer, but the
90
  //    "offset" instead stores the element count of the list.  The flat list of elements appears
91
  //    immediately after the tag.  In the list pointer itself, the element count is replaced with
92 93 94
  //    a word count for the whole list (excluding tag).  This allows the tag and elements to be
  //    precached in a single step rather than two sequential steps.
  //
95
  //    It is NOT intended to be possible to substitute an INLINE_COMPOSITE list for a POINTER
96 97 98 99 100 101 102 103 104 105
  //    list or vice-versa without breaking recipients.  Recipients expect one or the other
  //    depending on the message definition.
  //
  //    However, it IS allowed to substitute an INLINE_COMPOSITE list -- specifically, of structs --
  //    when a list was expected, or vice versa, with the assumption that the first field of the
  //    struct (field number zero) correspond to the element type.  This allows a list of
  //    primitives to be upgraded to a list of structs, avoiding the need to use parallel arrays
  //    when you realize that you need to attach some extra information to each element of some
  //    primitive list.
  //
106 107
  // 2) At one point there was a notion of "inline" struct fields, but it was deemed too much of
  //    an implementation burden for too little gain, and so was deleted.
108 109 110
};

typedef decltype(BITS / ELEMENTS) BitsPerElement;
111
typedef decltype(POINTERS / ELEMENTS) PointersPerElement;
112

Kenton Varda's avatar
Kenton Varda committed
113 114 115 116 117 118 119 120 121 122
static constexpr BitsPerElement BITS_PER_ELEMENT_TABLE[8] = {
    0 * BITS / ELEMENTS,
    1 * BITS / ELEMENTS,
    8 * BITS / ELEMENTS,
    16 * BITS / ELEMENTS,
    32 * BITS / ELEMENTS,
    64 * BITS / ELEMENTS,
    0 * BITS / ELEMENTS,
    0 * BITS / ELEMENTS
};
123

124
inline constexpr BitsPerElement dataBitsPerElement(FieldSize size) {
125
  return _::BITS_PER_ELEMENT_TABLE[static_cast<int>(size)];
126 127
}

128
inline constexpr PointersPerElement pointersPerElement(FieldSize size) {
129
  return size == FieldSize::POINTER ? 1 * POINTERS / ELEMENTS : 0 * POINTERS / ELEMENTS;
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
template <size_t size> struct ElementSizeForByteSize;
template <> struct ElementSizeForByteSize<1> { static constexpr FieldSize value = FieldSize::BYTE; };
template <> struct ElementSizeForByteSize<2> { static constexpr FieldSize value = FieldSize::TWO_BYTES; };
template <> struct ElementSizeForByteSize<4> { static constexpr FieldSize value = FieldSize::FOUR_BYTES; };
template <> struct ElementSizeForByteSize<8> { static constexpr FieldSize value = FieldSize::EIGHT_BYTES; };

template <typename T> struct ElementSizeForType {
  static constexpr FieldSize value =
      // Primitive types that aren't special-cased below can be determined from sizeof().
      kind<T>() == Kind::PRIMITIVE ? ElementSizeForByteSize<sizeof(T)>::value :
      kind<T>() == Kind::ENUM ? FieldSize::TWO_BYTES :
      kind<T>() == Kind::STRUCT ? FieldSize::INLINE_COMPOSITE :

      // Everything else is a pointer.
      FieldSize::POINTER;
147 148
};

149 150 151
// Void and bool are special.
template <> struct ElementSizeForType<Void> { static constexpr FieldSize value = FieldSize::VOID; };
template <> struct ElementSizeForType<bool> { static constexpr FieldSize value = FieldSize::BIT; };
152

153 154 155 156 157 158 159 160 161 162
// Lists and blobs are pointers, not structs.
template <typename T, bool b> struct ElementSizeForType<List<T, b>> {
  static constexpr FieldSize value = FieldSize::POINTER;
};
template <> struct ElementSizeForType<Text> {
  static constexpr FieldSize value = FieldSize::POINTER;
};
template <> struct ElementSizeForType<Data> {
  static constexpr FieldSize value = FieldSize::POINTER;
};
163

164 165 166 167 168
template <typename T>
inline constexpr FieldSize elementSizeForType() {
  return ElementSizeForType<T>::value;
}

169
}  // namespace _ (private)
170 171 172

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

173
namespace _ {  // private
174

175 176 177 178 179 180 181 182 183
template <int wordCount>
union AlignedData {
  // Useful for declaring static constant data blobs as an array of bytes, but forcing those
  // bytes to be word-aligned.

  uint8_t bytes[wordCount * sizeof(word)];
  word words[wordCount];
};

184 185
struct StructSize {
  WordCount16 data;
186
  WirePointerCount16 pointers;
187

188 189 190 191
  FieldSize preferredListEncoding;
  // Preferred size to use when encoding a list of this struct.  This is INLINE_COMPOSITE if and
  // only if the struct is larger than one word; otherwise the struct list can be encoded more
  // efficiently by encoding it as if it were some primitive type.
192

193
  inline constexpr WordCount total() const { return data + pointers * WORDS_PER_POINTER; }
194 195

  StructSize() = default;
196
  inline constexpr StructSize(WordCount data, WirePointerCount pointers,
197 198
                              FieldSize preferredListEncoding)
      : data(data), pointers(pointers), preferredListEncoding(preferredListEncoding) {}
199 200
};

201
template <typename T> struct StructSize_;
202 203
// Specialized for every struct type with member:  static constexpr StructSize value"

204
template <typename T>
205
inline constexpr StructSize structSize() {
206
  return StructSize_<T>::value;
207
}
208

209 210
// -------------------------------------------------------------------
// Masking of default values
211

212 213 214 215 216
template <typename T, Kind kind = kind<T>()> struct Mask_;
template <typename T> struct Mask_<T, Kind::PRIMITIVE> { typedef T Type; };
template <typename T> struct Mask_<T, Kind::ENUM> { typedef uint16_t Type; };
template <> struct Mask_<float, Kind::PRIMITIVE> { typedef uint32_t Type; };
template <> struct Mask_<double, Kind::PRIMITIVE> { typedef uint64_t Type; };
217

218
template <typename T> struct Mask_<T, Kind::UNKNOWN> {
219 220 221
  // Union discriminants end up here.
  static_assert(sizeof(T) == 2, "Don't know how to mask this type.");
  typedef uint16_t Type;
222 223
};

224
template <typename T>
225
using Mask = typename Mask_<T>::Type;
226 227

template <typename T>
228
KJ_ALWAYS_INLINE(Mask<T> mask(T value, Mask<T> mask));
229
template <typename T>
230
KJ_ALWAYS_INLINE(T unmask(Mask<T> value, Mask<T> mask));
231 232

template <typename T>
233 234
inline Mask<T> mask(T value, Mask<T> mask) {
  return static_cast<Mask<T> >(value) ^ mask;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}

template <>
inline uint32_t mask<float>(float value, uint32_t mask) {
  uint32_t i;
  static_assert(sizeof(i) == sizeof(value), "float is not 32 bits?");
  memcpy(&i, &value, sizeof(value));
  return i ^ mask;
}

template <>
inline uint64_t mask<double>(double value, uint64_t mask) {
  uint64_t i;
  static_assert(sizeof(i) == sizeof(value), "double is not 64 bits?");
  memcpy(&i, &value, sizeof(value));
  return i ^ mask;
}

template <typename T>
254
inline T unmask(Mask<T> value, Mask<T> mask) {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  return static_cast<T>(value ^ mask);
}

template <>
inline float unmask<float>(uint32_t value, uint32_t mask) {
  value ^= mask;
  float result;
  static_assert(sizeof(result) == sizeof(value), "float is not 32 bits?");
  memcpy(&result, &value, sizeof(value));
  return result;
}

template <>
inline double unmask<double>(uint64_t value, uint64_t mask) {
  value ^= mask;
  double result;
  static_assert(sizeof(result) == sizeof(value), "double is not 64 bits?");
  memcpy(&result, &value, sizeof(value));
  return result;
}

276 277
// -------------------------------------------------------------------

278 279 280 281 282 283
class PointerBuilder: public kj::DisallowConstCopy {
  // Represents a single pointer, usually embedded in a struct or a list.

public:
  inline PointerBuilder(): segment(nullptr), pointer(nullptr) {}

Kenton Varda's avatar
Kenton Varda committed
284 285 286 287
  static inline PointerBuilder getRoot(SegmentBuilder* segment, word* location);
  // Get a PointerBuilder representing a message root located in the given segment at the given
  // location.

288 289 290
  bool isNull();

  StructBuilder getStruct(StructSize size, const word* defaultValue);
291
  ListBuilder getList(FieldSize elementSize, const word* defaultValzue);
292 293
  ListBuilder getStructList(StructSize elementSize, const word* defaultValue);
  template <typename T> typename T::Builder getBlob(const void* defaultValue,ByteCount defaultSize);
Kenton Varda's avatar
Kenton Varda committed
294
  kj::Own<const ClientHook> getCapability();
295 296 297 298 299 300 301 302 303 304 305 306 307 308
  // Get methods:  Get the value.  If it is null, initialize it to a copy of the default value.
  // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a
  // simple byte array for blobs.

  StructBuilder initStruct(StructSize size);
  ListBuilder initList(FieldSize elementSize, ElementCount elementCount);
  ListBuilder initStructList(ElementCount elementCount, StructSize size);
  template <typename T> typename T::Builder initBlob(ByteCount size);
  // Init methods:  Initialize the pointer to a newly-allocated object, discarding the existing
  // object.

  void setStruct(const StructReader& value);
  void setList(const ListReader& value);
  template <typename T> void setBlob(typename T::Reader value);
Kenton Varda's avatar
Kenton Varda committed
309
  void setCapability(kj::Own<const ClientHook>&& cap);
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  // Set methods:  Initialize the pointer to a newly-allocated copy of the given value, discarding
  // the existing object.

  void adopt(OrphanBuilder&& orphan);
  // Set the pointer to point at the given orphaned value.

  OrphanBuilder disown();
  // Set the pointer to null and return its previous value as an orphan.

  void clear();
  // Clear the pointer to null, discarding its previous value.

  void transferFrom(PointerBuilder other);
  // Equivalent to `adopt(other.disown())`.

  void copyFrom(PointerReader other);
  // Equivalent to `set(other.get())`.

328 329
  PointerReader asReader() const;

330
  BuilderArena* getArena() const;
331 332 333 334 335
  // Get the arena containing this pointer.

  PointerBuilder imbue(ImbuedBuilderArena& newArena) const;
  // Imbue the pointer with a capability context, returning the imbued pointer.

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
private:
  SegmentBuilder* segment;     // Memory segment in which the pointer resides.
  WirePointer* pointer;        // Pointer to the pointer.

  inline PointerBuilder(SegmentBuilder* segment, WirePointer* pointer)
      : segment(segment), pointer(pointer) {}

  friend class StructBuilder;
  friend class ListBuilder;
};

class PointerReader {
public:
  inline PointerReader(): segment(nullptr), pointer(nullptr), nestingLimit(0x7fffffff) {}

Kenton Varda's avatar
Kenton Varda committed
351 352 353 354 355 356 357
  static PointerReader getRoot(SegmentReader* segment, const word* location, int nestingLimit);
  // Get a PointerReader representing a message root located in the given segment at the given
  // location.

  static inline PointerReader getRootUnchecked(const word* location);
  // Get a PointerReader for an unchecked message.

Kenton Varda's avatar
Kenton Varda committed
358 359 360 361 362 363 364
  WordCount64 targetSize() const;
  // Return the total size of the target object and everything to which it points.  Does not count
  // far pointer overhead.  This is useful for deciding how much space is needed to copy the object
  // into a flat array.  However, the caller is advised NOT to treat this value as secure.  Instead,
  // use the result as a hint for allocating the first segment, do the copy, and then throw an
  // exception if it overruns.

365 366 367 368 369 370
  bool isNull() const;

  StructReader getStruct(const word* defaultValue) const;
  ListReader getList(FieldSize expectedElementSize, const word* defaultValue) const;
  template <typename T>
  typename T::Reader getBlob(const void* defaultValue, ByteCount defaultSize) const;
371
  kj::Own<const ClientHook> getCapability() const;
372 373 374 375 376 377 378 379 380
  // Get methods:  Get the value.  If it is null, return the default value instead.
  // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a
  // simple byte array for blobs.

  const word* getUnchecked() const;
  // If this is an unchecked message, get a word* pointing at the location of the pointer.  This
  // word* can actually be passed to readUnchecked() to read the designated sub-object later.  If
  // this isn't an unchecked message, throws an exception.

381 382 383 384 385 386
  kj::Maybe<Arena&> getArena() const;
  // Get the arena containing this pointer.

  PointerReader imbue(ImbuedReaderArena& newArena) const;
  // Imbue the pointer with a capability context, returning the imbued pointer.

387 388 389 390 391 392 393 394 395 396 397 398 399
private:
  SegmentReader* segment;      // Memory segment in which the pointer resides.
  const WirePointer* pointer;  // Pointer to the pointer.  null = treat as null pointer.

  int nestingLimit;
  // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
  // Once this reaches zero, further pointers will be pruned.

  inline PointerReader(SegmentReader* segment, const WirePointer* pointer, int nestingLimit)
      : segment(segment), pointer(pointer), nestingLimit(nestingLimit) {}

  friend class StructReader;
  friend class ListReader;
400
  friend class PointerBuilder;
401
  friend class OrphanBuilder;
402 403
};

404
// -------------------------------------------------------------------
Kenton Varda's avatar
Kenton Varda committed
405

406
class StructBuilder: public kj::DisallowConstCopy {
407
public:
408
  inline StructBuilder(): segment(nullptr), data(nullptr), pointers(nullptr), bit0Offset(0) {}
409

410 411 412 413
  inline word* getLocation() { return reinterpret_cast<word*>(data); }
  // Get the object's location.  Only valid for independently-allocated objects (i.e. not list
  // elements).

414
  inline BitCount getDataSectionSize() const { return dataSize; }
415
  inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
416 417
  inline Data::Builder getDataSectionAsBlob();

418 419 420 421
  template <typename T>
  KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset));
  // Return true if the field is set to something other than its default value.

422
  template <typename T>
423
  KJ_ALWAYS_INLINE(T getDataField(ElementCount offset));
424
  // Gets the data field value of the given type at the given offset.  The offset is measured in
425 426
  // multiples of the field size, determined by the type.

427
  template <typename T>
428
  KJ_ALWAYS_INLINE(T getDataField(ElementCount offset, Mask<T> mask));
429 430 431
  // Like getDataField() but applies the given XOR mask to the data on load.  Used for reading
  // fields with non-zero default values.

432
  template <typename T>
433
  KJ_ALWAYS_INLINE(void setDataField(
434
      ElementCount offset, kj::NoInfer<T> value));
435 436
  // Sets the data field value at the given offset.

437
  template <typename T>
438
  KJ_ALWAYS_INLINE(void setDataField(
439
      ElementCount offset, kj::NoInfer<T> value, Mask<T> mask));
440 441 442
  // Like setDataField() but applies the given XOR mask before storing.  Used for writing fields
  // with non-zero default values.

443 444
  KJ_ALWAYS_INLINE(PointerBuilder getPointerField(WirePointerCount ptrIndex));
  // Get a builder for a pointer field given the index within the pointer section.
445

446 447 448
  void clearAll();
  // Clear all pointers and data.

449 450 451 452 453
  void transferContentFrom(StructBuilder other);
  // Adopt all pointers from `other`, and also copy all data.  If `other`'s sections are larger
  // than this, the extra data is not transferred, meaning there is a risk of data loss when
  // transferring from messages built with future versions of the protocol.

454 455 456 457 458
  void copyContentFrom(StructReader other);
  // Copy content from `other`.  If `other`'s sections are larger than this, the extra data is not
  // copied, meaning there is a risk of data loss when copying from messages built with future
  // versions of the protocol.

459
  StructReader asReader() const;
460
  // Gets a StructReader pointing at the same memory.
461

462 463 464
  BuilderArena* getArena();
  // Gets the arena in which this object is allocated.

465 466 467 468
  void unimbue();
  // Removes the capability context from the builder.  This means replacing the segment pointer --
  // which is assumed to point to an ImbuedSegmentBuilder -- with the non-imbued base segment.

469
private:
470
  SegmentBuilder* segment;     // Memory segment in which the struct resides.
Kenton Varda's avatar
Kenton Varda committed
471
  void* data;                  // Pointer to the encoded data.
472
  WirePointer* pointers;   // Pointer to the encoded pointers.
473

474
  BitCount32 dataSize;
475
  // Size of data section.  We use a bit count rather than a word count to more easily handle the
476 477
  // case of struct lists encoded with less than a word per element.

478
  WirePointerCount16 pointerCount;  // Size of the pointer section.
479 480 481 482 483 484

  BitCount8 bit0Offset;
  // A special hack:  If dataSize == 1 bit, then bit0Offset is the offset of that bit within the
  // byte pointed to by `data`.  In all other cases, this is zero.  This is needed to implement
  // struct lists where each struct is one bit.

485 486 487 488
  inline StructBuilder(SegmentBuilder* segment, void* data, WirePointer* pointers,
                       BitCount dataSize, WirePointerCount pointerCount, BitCount8 bit0Offset)
      : segment(segment), data(data), pointers(pointers),
        dataSize(dataSize), pointerCount(pointerCount), bit0Offset(bit0Offset) {}
489

490
  friend class ListBuilder;
491
  friend struct WireHelpers;
492
  friend class OrphanBuilder;
493 494
};

495
class StructReader {
496
public:
497
  inline StructReader()
498
      : segment(nullptr), data(nullptr), pointers(nullptr), dataSize(0),
499
        pointerCount(0), bit0Offset(0), nestingLimit(0x7fffffff) {}
500

Kenton Varda's avatar
Kenton Varda committed
501 502
  const void* getLocation() const { return data; }

503
  inline BitCount getDataSectionSize() const { return dataSize; }
504
  inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
505 506
  inline Data::Reader getDataSectionAsBlob();

507 508 509 510
  template <typename T>
  KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset) const);
  // Return true if the field is set to something other than its default value.

511
  template <typename T>
512
  KJ_ALWAYS_INLINE(T getDataField(ElementCount offset) const);
513
  // Get the data field value of the given type at the given offset.  The offset is measured in
514
  // multiples of the field size, determined by the type.  Returns zero if the offset is past the
515
  // end of the struct's data section.
516 517

  template <typename T>
518
  KJ_ALWAYS_INLINE(
519
      T getDataField(ElementCount offset, Mask<T> mask) const);
520 521
  // Like getDataField(offset), but applies the given XOR mask to the result.  Used for reading
  // fields with non-zero default values.
522

523 524 525
  KJ_ALWAYS_INLINE(PointerReader getPointerField(WirePointerCount ptrIndex) const);
  // Get a reader for a pointer field given the index within the pointer section.  If the index
  // is out-of-bounds, returns a null pointer.
526

527 528 529 530 531 532 533
  WordCount64 totalSize() const;
  // Return the total size of the struct and everything to which it points.  Does not count far
  // pointer overhead.  This is useful for deciding how much space is needed to copy the struct
  // into a flat array.  However, the caller is advised NOT to treat this value as secure.  Instead,
  // use the result as a hint for allocating the first segment, do the copy, and then throw an
  // exception if it overruns.

534 535 536 537
  void unimbue();
  // Removes the capability context from the reader.  This means replacing the segment pointer --
  // which is assumed to point to an ImbuedSegmentReader -- with the non-imbued base segment.

538
private:
Kenton Varda's avatar
Kenton Varda committed
539
  SegmentReader* segment;  // Memory segment in which the struct resides.
540

541
  const void* data;
542
  const WirePointer* pointers;
543

544
  BitCount32 dataSize;
545
  // Size of data section.  We use a bit count rather than a word count to more easily handle the
546 547
  // case of struct lists encoded with less than a word per element.

548
  WirePointerCount16 pointerCount;  // Size of the pointer section.
549

550 551 552 553 554 555 556 557 558 559
  BitCount8 bit0Offset;
  // A special hack:  If dataSize == 1 bit, then bit0Offset is the offset of that bit within the
  // byte pointed to by `data`.  In all other cases, this is zero.  This is needed to implement
  // struct lists where each struct is one bit.
  //
  // TODO(someday):  Consider packing this together with dataSize, since we have 10 extra bits
  //   there doing nothing -- or arguably 12 bits, if you consider that 2-bit and 4-bit sizes
  //   aren't allowed.  Consider that we could have a method like getDataSizeIn<T>() which is
  //   specialized to perform the correct shifts for each size.

560
  int nestingLimit;
561 562
  // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
  // Once this reaches zero, further pointers will be pruned.
563
  // TODO(perf):  Limit to 8 bits for better alignment?
564

565 566
  inline StructReader(SegmentReader* segment, const void* data, const WirePointer* pointers,
                      BitCount dataSize, WirePointerCount pointerCount, BitCount8 bit0Offset,
567
                      int nestingLimit)
568 569
      : segment(segment), data(data), pointers(pointers),
        dataSize(dataSize), pointerCount(pointerCount), bit0Offset(bit0Offset),
570
        nestingLimit(nestingLimit) {}
571

572 573
  friend class ListReader;
  friend class StructBuilder;
574 575 576 577 578
  friend struct WireHelpers;
};

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

579
class ListBuilder: public kj::DisallowConstCopy {
580
public:
581
  inline ListBuilder()
582
      : segment(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS),
583
        step(0 * BITS / ELEMENTS) {}
584

585 586 587 588 589 590 591 592 593 594 595
  inline word* getLocation() {
    // Get the object's location.  Only valid for independently-allocated objects (i.e. not list
    // elements).

    if (step * ELEMENTS <= BITS_PER_WORD * WORDS) {
      return reinterpret_cast<word*>(ptr);
    } else {
      return reinterpret_cast<word*>(ptr) - POINTER_SIZE_IN_WORDS;
    }
  }

596
  inline ElementCount size() const;
597 598
  // The number of elements in the list.

599 600 601 602
  Text::Builder asText();
  Data::Builder asData();
  // Reinterpret the list as a blob.  Throws an exception if the elements are not byte-sized.

603
  template <typename T>
604
  KJ_ALWAYS_INLINE(T getDataElement(ElementCount index));
605 606 607
  // Get the element of the given type at the given index.

  template <typename T>
608
  KJ_ALWAYS_INLINE(void setDataElement(
609
      ElementCount index, kj::NoInfer<T> value));
610
  // Set the element at the given index.
611

612
  KJ_ALWAYS_INLINE(PointerBuilder getPointerElement(ElementCount index));
Kenton Varda's avatar
Kenton Varda committed
613

614
  StructBuilder getStructElement(ElementCount index);
615

616 617
  ListReader asReader() const;
  // Get a ListReader pointing at the same memory.
618

619 620 621
  BuilderArena* getArena();
  // Gets the arena in which this object is allocated.

622
private:
Kenton Varda's avatar
Kenton Varda committed
623
  SegmentBuilder* segment;  // Memory segment in which the list resides.
624

625
  byte* ptr;  // Pointer to list content.
626

627
  ElementCount elementCount;  // Number of elements in the list.
628

629
  decltype(BITS / ELEMENTS) step;
630
  // The distance between elements.
631 632

  BitCount32 structDataSize;
633
  WirePointerCount16 structPointerCount;
634 635
  // The struct properties to use when interpreting the elements as structs.  All lists can be
  // interpreted as struct lists, so these are always filled in.
636

637
  inline ListBuilder(SegmentBuilder* segment, void* ptr,
638
                     decltype(BITS / ELEMENTS) step, ElementCount size,
639
                     BitCount structDataSize, WirePointerCount structPointerCount)
640
      : segment(segment), ptr(reinterpret_cast<byte*>(ptr)),
641
        elementCount(size), step(step), structDataSize(structDataSize),
642
        structPointerCount(structPointerCount) {}
643

644
  friend class StructBuilder;
645
  friend struct WireHelpers;
646
  friend class OrphanBuilder;
647 648
};

649
class ListReader {
650
public:
651
  inline ListReader()
652
      : segment(nullptr), ptr(nullptr), elementCount(0), step(0 * BITS / ELEMENTS),
653
        structDataSize(0), structPointerCount(0), nestingLimit(0x7fffffff) {}
654

655
  inline ElementCount size() const;
656 657
  // The number of elements in the list.

658 659 660 661
  Text::Reader asText();
  Data::Reader asData();
  // Reinterpret the list as a blob.  Throws an exception if the elements are not byte-sized.

662
  template <typename T>
663
  KJ_ALWAYS_INLINE(T getDataElement(ElementCount index) const);
664 665
  // Get the element of the given type at the given index.

666
  KJ_ALWAYS_INLINE(PointerReader getPointerElement(ElementCount index) const);
667

668
  StructReader getStructElement(ElementCount index) const;
669

670
private:
Kenton Varda's avatar
Kenton Varda committed
671
  SegmentReader* segment;  // Memory segment in which the list resides.
672

673
  const byte* ptr;  // Pointer to list content.
674

675
  ElementCount elementCount;  // Number of elements in the list.
676

677
  decltype(BITS / ELEMENTS) step;
678
  // The distance between elements.
679

680
  BitCount32 structDataSize;
681
  WirePointerCount16 structPointerCount;
682 683
  // The struct properties to use when interpreting the elements as structs.  All lists can be
  // interpreted as struct lists, so these are always filled in.
684

685
  int nestingLimit;
686 687 688
  // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
  // Once this reaches zero, further pointers will be pruned.

689
  inline ListReader(SegmentReader* segment, const void* ptr,
690
                    ElementCount elementCount, decltype(BITS / ELEMENTS) step,
691
                    BitCount structDataSize, WirePointerCount structPointerCount,
692 693
                    int nestingLimit)
      : segment(segment), ptr(reinterpret_cast<const byte*>(ptr)), elementCount(elementCount),
694
        step(step), structDataSize(structDataSize),
695
        structPointerCount(structPointerCount), nestingLimit(nestingLimit) {}
696

697 698
  friend class StructReader;
  friend class ListBuilder;
699
  friend struct WireHelpers;
700
  friend class OrphanBuilder;
701 702
};

703 704
// -------------------------------------------------------------------

705 706 707 708
class OrphanBuilder {
public:
  inline OrphanBuilder(): segment(nullptr), location(nullptr) { memset(&tag, 0, sizeof(tag)); }
  OrphanBuilder(const OrphanBuilder& other) = delete;
709
  inline OrphanBuilder(OrphanBuilder&& other) noexcept;
710
  inline ~OrphanBuilder() noexcept(false);
711 712 713 714 715 716 717 718 719 720 721

  static OrphanBuilder initStruct(BuilderArena* arena, StructSize size);
  static OrphanBuilder initList(BuilderArena* arena, ElementCount elementCount,
                                FieldSize elementSize);
  static OrphanBuilder initStructList(BuilderArena* arena, ElementCount elementCount,
                                      StructSize elementSize);
  static OrphanBuilder initText(BuilderArena* arena, ByteCount size);
  static OrphanBuilder initData(BuilderArena* arena, ByteCount size);

  static OrphanBuilder copy(BuilderArena* arena, StructReader copyFrom);
  static OrphanBuilder copy(BuilderArena* arena, ListReader copyFrom);
722
  static OrphanBuilder copy(BuilderArena* arena, PointerReader copyFrom);
723 724
  static OrphanBuilder copy(BuilderArena* arena, Text::Reader copyFrom);
  static OrphanBuilder copy(BuilderArena* arena, Data::Reader copyFrom);
725
  static OrphanBuilder copy(BuilderArena* arena, kj::Own<const ClientHook> copyFrom);
726 727 728 729

  OrphanBuilder& operator=(const OrphanBuilder& other) = delete;
  inline OrphanBuilder& operator=(OrphanBuilder&& other);

730 731
  inline bool operator==(decltype(nullptr)) const { return location == nullptr; }
  inline bool operator!=(decltype(nullptr)) const { return location != nullptr; }
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

  StructBuilder asStruct(StructSize size);
  // Interpret as a struct, or throw an exception if not a struct.

  ListBuilder asList(FieldSize elementSize);
  // Interpret as a list, or throw an exception if not a list.  elementSize cannot be
  // INLINE_COMPOSITE -- use asStructList() instead.

  ListBuilder asStructList(StructSize elementSize);
  // Interpret as a struct list, or throw an exception if not a list.

  Text::Builder asText();
  Data::Builder asData();
  // Interpret as a blob, or throw an exception if not a blob.

747 748
  StructReader asStructReader(StructSize size) const;
  ListReader asListReader(FieldSize elementSize) const;
749
  kj::Own<const ClientHook> asCapability() const;
750 751 752
  Text::Reader asTextReader() const;
  Data::Reader asDataReader() const;

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
private:
  static_assert(1 * POINTERS * WORDS_PER_POINTER == 1 * WORDS,
                "This struct assumes a pointer is one word.");
  word tag;
  // Contains an encoded WirePointer representing this object.  WirePointer is defined in
  // layout.c++, but fits in a word.
  //
  // If the pointer is a FAR pointer, then the tag is a complete pointer, `location` is null, and
  // `segment` is any arbitrary segment in the message.  Otherwise, the tag's offset is garbage,
  // `location` points at the actual object, and `segment` points at the segment where `location`
  // resides.

  SegmentBuilder* segment;
  // Segment in which the object resides, or an arbitrary segment in the message if the tag is a
  // FAR pointer.

  word* location;
770
  // Pointer to the object, or nullptr if the pointer is null.
771 772 773 774 775 776 777

  inline OrphanBuilder(const void* tagPtr, SegmentBuilder* segment, word* location)
      : segment(segment), location(location) {
    memcpy(&tag, tagPtr, sizeof(tag));
  }

  inline WirePointer* tagAsPtr() { return reinterpret_cast<WirePointer*>(&tag); }
778
  inline const WirePointer* tagAsPtr() const { return reinterpret_cast<const WirePointer*>(&tag); }
779 780 781 782 783 784 785 786

  void euthanize();
  // Erase the target object, zeroing it out and possibly reclaiming the memory.  Called when
  // the OrphanBuilder is being destroyed or overwritten and it is non-null.

  friend struct WireHelpers;
};

787 788 789
// =======================================================================================
// Internal implementation details...

790 791 792 793 794 795 796 797 798 799 800
// These are defined in the source file.
template <> typename Text::Builder PointerBuilder::initBlob<Text>(ByteCount size);
template <> void PointerBuilder::setBlob<Text>(typename Text::Reader value);
template <> typename Text::Builder PointerBuilder::getBlob<Text>(const void* defaultValue, ByteCount defaultSize);
template <> typename Text::Reader PointerReader::getBlob<Text>(const void* defaultValue, ByteCount defaultSize) const;

template <> typename Data::Builder PointerBuilder::initBlob<Data>(ByteCount size);
template <> void PointerBuilder::setBlob<Data>(typename Data::Reader value);
template <> typename Data::Builder PointerBuilder::getBlob<Data>(const void* defaultValue, ByteCount defaultSize);
template <> typename Data::Reader PointerReader::getBlob<Data>(const void* defaultValue, ByteCount defaultSize) const;

Kenton Varda's avatar
Kenton Varda committed
801 802 803 804 805 806 807 808
inline PointerBuilder PointerBuilder::getRoot(SegmentBuilder* segment, word* location) {
  return PointerBuilder(segment, reinterpret_cast<WirePointer*>(location));
}

inline PointerReader PointerReader::getRootUnchecked(const word* location) {
  return PointerReader(nullptr, reinterpret_cast<const WirePointer*>(location), 0x7fffffff);
}

809 810
// -------------------------------------------------------------------

811
inline Data::Builder StructBuilder::getDataSectionAsBlob() {
812
  return Data::Builder(reinterpret_cast<byte*>(data), dataSize / BITS_PER_BYTE / BYTES);
813 814
}

815 816 817 818 819 820 821 822 823 824
template <typename T>
inline bool StructBuilder::hasDataField(ElementCount offset) {
  return getDataField<Mask<T>>(offset) != 0;
}

template <>
inline bool StructBuilder::hasDataField<Void>(ElementCount offset) {
  return false;
}

825
template <typename T>
826
inline T StructBuilder::getDataField(ElementCount offset) {
827
  return reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].get();
828 829 830
}

template <>
831
inline bool StructBuilder::getDataField<bool>(ElementCount offset) {
832 833 834
  // This branch should be compiled out whenever this is inlined with a constant offset.
  BitCount boffset = (offset == 0 * ELEMENTS) ?
      BitCount(bit0Offset) : offset * (1 * BITS / ELEMENTS);
835
  byte* b = reinterpret_cast<byte*>(data) + boffset / BITS_PER_BYTE;
836
  return (*reinterpret_cast<uint8_t*>(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0;
837 838
}

839
template <>
840
inline Void StructBuilder::getDataField<Void>(ElementCount offset) {
841
  return VOID;
842 843
}

844
template <typename T>
845
inline T StructBuilder::getDataField(ElementCount offset, Mask<T> mask) {
846
  return unmask<T>(getDataField<Mask<T> >(offset), mask);
847 848
}

849
template <typename T>
850
inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value) {
851
  reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].set(value);
852 853 854
}

template <>
855
inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) {
856 857 858
  // This branch should be compiled out whenever this is inlined with a constant offset.
  BitCount boffset = (offset == 0 * ELEMENTS) ?
      BitCount(bit0Offset) : offset * (1 * BITS / ELEMENTS);
859
  byte* b = reinterpret_cast<byte*>(data) + boffset / BITS_PER_BYTE;
860 861 862
  uint bitnum = boffset % BITS_PER_BYTE / BITS;
  *reinterpret_cast<uint8_t*>(b) = (*reinterpret_cast<uint8_t*>(b) & ~(1 << bitnum))
                                 | (static_cast<uint8_t>(value) << bitnum);
863 864
}

865
template <>
866
inline void StructBuilder::setDataField<Void>(ElementCount offset, Void value) {}
867

868
template <typename T>
869
inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value, Mask<T> m) {
870
  setDataField<Mask<T> >(offset, mask<T>(value, m));
871 872
}

873 874 875 876 877 878
inline PointerBuilder StructBuilder::getPointerField(WirePointerCount ptrIndex) {
  // Hacky because WirePointer is defined in the .c++ file (so is incomplete here).
  return PointerBuilder(segment, reinterpret_cast<WirePointer*>(
      reinterpret_cast<word*>(pointers) + ptrIndex * WORDS_PER_POINTER));
}

879 880
// -------------------------------------------------------------------

881
inline Data::Reader StructReader::getDataSectionAsBlob() {
882
  return Data::Reader(reinterpret_cast<const byte*>(data), dataSize / BITS_PER_BYTE / BYTES);
883 884
}

885
template <typename T>
886 887 888 889 890 891 892 893 894 895 896
inline bool StructReader::hasDataField(ElementCount offset) const {
  return getDataField<Mask<T>>(offset) != 0;
}

template <>
inline bool StructReader::hasDataField<Void>(ElementCount offset) const {
  return false;
}

template <typename T>
inline T StructReader::getDataField(ElementCount offset) const {
897
  if ((offset + 1 * ELEMENTS) * capnp::bitsPerElement<T>() <= dataSize) {
898
    return reinterpret_cast<const WireValue<T>*>(data)[offset / ELEMENTS].get();
899
  } else {
900
    return static_cast<T>(0);
901
  }
902 903 904
}

template <>
905
inline bool StructReader::getDataField<bool>(ElementCount offset) const {
906
  BitCount boffset = offset * (1 * BITS / ELEMENTS);
907 908 909 910 911
  if (boffset < dataSize) {
    // This branch should be compiled out whenever this is inlined with a constant offset.
    if (offset == 0 * ELEMENTS) {
      boffset = bit0Offset;
    }
912
    const byte* b = reinterpret_cast<const byte*>(data) + boffset / BITS_PER_BYTE;
913 914
    return (*reinterpret_cast<const uint8_t*>(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0;
  } else {
915
    return false;
916
  }
917 918
}

919
template <>
920
inline Void StructReader::getDataField<Void>(ElementCount offset) const {
921
  return VOID;
922 923
}

924
template <typename T>
925 926
T StructReader::getDataField(ElementCount offset, Mask<T> mask) const {
  return unmask<T>(getDataField<Mask<T> >(offset), mask);
927 928
}

929 930 931 932 933 934 935 936 937 938
inline PointerReader StructReader::getPointerField(WirePointerCount ptrIndex) const {
  if (ptrIndex < pointerCount) {
    // Hacky because WirePointer is defined in the .c++ file (so is incomplete here).
    return PointerReader(segment, reinterpret_cast<const WirePointer*>(
        reinterpret_cast<const word*>(pointers) + ptrIndex * WORDS_PER_POINTER), nestingLimit);
  } else{
    return PointerReader();
  }
}

939 940
// -------------------------------------------------------------------

941
inline ElementCount ListBuilder::size() const { return elementCount; }
942 943

template <typename T>
944
inline T ListBuilder::getDataElement(ElementCount index) {
945 946
  return reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();

Kenton Varda's avatar
Kenton Varda committed
947
  // TODO(perf):  Benchmark this alternate implementation, which I suspect may make better use of
948 949 950 951
  //   the x86 SIB byte.  Also use it for all the other getData/setData implementations below, and
  //   the various non-inline methods that look up pointers.
  //   Also if using this, consider changing ptr back to void* instead of byte*.
//  return reinterpret_cast<WireValue<T>*>(ptr)[
952
//      index / ELEMENTS * (step / capnp::bitsPerElement<T>())].get();
953 954 955
}

template <>
956
inline bool ListBuilder::getDataElement<bool>(ElementCount index) {
957
  // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
958
  BitCount bindex = index * step;
959
  byte* b = ptr + bindex / BITS_PER_BYTE;
960
  return (*reinterpret_cast<uint8_t*>(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0;
961 962
}

963
template <>
964
inline Void ListBuilder::getDataElement<Void>(ElementCount index) {
965
  return VOID;
966 967
}

968
template <typename T>
969
inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer<T> value) {
970
  reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->set(value);
971 972 973
}

template <>
974
inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) {
975 976
  // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
  BitCount bindex = index * (1 * BITS / ELEMENTS);
977
  byte* b = ptr + bindex / BITS_PER_BYTE;
978 979 980
  uint bitnum = bindex % BITS_PER_BYTE / BITS;
  *reinterpret_cast<uint8_t*>(b) = (*reinterpret_cast<uint8_t*>(b) & ~(1 << bitnum))
                                 | (static_cast<uint8_t>(value) << bitnum);
981 982
}

983
template <>
984
inline void ListBuilder::setDataElement<Void>(ElementCount index, Void value) {}
985

986 987 988 989 990
inline PointerBuilder ListBuilder::getPointerElement(ElementCount index) {
  return PointerBuilder(segment,
      reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE));
}

991 992
// -------------------------------------------------------------------

993
inline ElementCount ListReader::size() const { return elementCount; }
994 995

template <typename T>
996
inline T ListReader::getDataElement(ElementCount index) const {
997
  return reinterpret_cast<const WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();
998 999 1000
}

template <>
1001
inline bool ListReader::getDataElement<bool>(ElementCount index) const {
1002
  // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
1003
  BitCount bindex = index * step;
1004
  const byte* b = ptr + bindex / BITS_PER_BYTE;
1005
  return (*reinterpret_cast<const uint8_t*>(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0;
1006 1007
}

1008 1009
template <>
inline Void ListReader::getDataElement<Void>(ElementCount index) const {
1010
  return VOID;
1011 1012
}

1013 1014 1015 1016
inline PointerReader ListReader::getPointerElement(ElementCount index) const {
  return PointerReader(segment,
      reinterpret_cast<const WirePointer*>(ptr + index * step / BITS_PER_BYTE), nestingLimit);
}
1017

1018 1019
// -------------------------------------------------------------------

1020
inline OrphanBuilder::OrphanBuilder(OrphanBuilder&& other) noexcept
1021 1022 1023 1024 1025 1026
    : segment(other.segment), location(other.location) {
  memcpy(&tag, &other.tag, sizeof(tag));  // Needs memcpy to comply with aliasing rules.
  other.segment = nullptr;
  other.location = nullptr;
}

1027
inline OrphanBuilder::~OrphanBuilder() noexcept(false) {
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
  if (segment != nullptr) euthanize();
}

inline OrphanBuilder& OrphanBuilder::operator=(OrphanBuilder&& other) {
  // With normal smart pointers, it's important to handle the case where the incoming pointer
  // is actually transitively owned by this one.  In this case, euthanize() would destroy `other`
  // before we copied it.  This isn't possible in the case of `OrphanBuilder` because it only
  // owns message objects, and `other` is not itself a message object, therefore cannot possibly
  // be transitively owned by `this`.

  if (segment != nullptr) euthanize();
  segment = other.segment;
  location = other.location;
  memcpy(&tag, &other.tag, sizeof(tag));  // Needs memcpy to comply with aliasing rules.
  other.segment = nullptr;
  other.location = nullptr;
  return *this;
}

1047
}  // namespace _ (private)
1048
}  // namespace capnp
1049

Kenton Varda's avatar
Kenton Varda committed
1050
#endif  // CAPNP_LAYOUT_H_