cannam@132: // Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors cannam@132: // Licensed under the MIT License: cannam@132: // cannam@132: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@132: // of this software and associated documentation files (the "Software"), to deal cannam@132: // in the Software without restriction, including without limitation the rights cannam@132: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@132: // copies of the Software, and to permit persons to whom the Software is cannam@132: // furnished to do so, subject to the following conditions: cannam@132: // cannam@132: // The above copyright notice and this permission notice shall be included in cannam@132: // all copies or substantial portions of the Software. cannam@132: // cannam@132: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@132: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@132: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@132: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@132: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@132: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@132: // THE SOFTWARE. cannam@132: cannam@132: // This file is NOT intended for use by clients, except in generated code. cannam@132: // cannam@132: // This file defines low-level, non-type-safe classes for traversing the Cap'n Proto memory layout cannam@132: // (which is also its wire format). Code generated by the Cap'n Proto compiler uses these classes, cannam@132: // as does other parts of the Cap'n proto library which provide a higher-level interface for cannam@132: // dynamic introspection. cannam@132: cannam@132: #ifndef CAPNP_LAYOUT_H_ cannam@132: #define CAPNP_LAYOUT_H_ cannam@132: cannam@132: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@132: #pragma GCC system_header cannam@132: #endif cannam@132: cannam@132: #include cannam@132: #include cannam@132: #include "common.h" cannam@132: #include "blob.h" cannam@132: #include "endian.h" cannam@132: cannam@132: #if (defined(__mips__) || defined(__hppa__)) && !defined(CAPNP_CANONICALIZE_NAN) cannam@132: #define CAPNP_CANONICALIZE_NAN 1 cannam@132: // Explicitly detect NaNs and canonicalize them to the quiet NaN value as would be returned by cannam@132: // __builtin_nan("") on systems implementing the IEEE-754 recommended (but not required) NaN cannam@132: // signalling/quiet differentiation (such as x86). Unfortunately, some architectures -- in cannam@132: // particular, MIPS -- represent quiet vs. signalling nans differently than the rest of the world. cannam@132: // Canonicalizing them makes output consistent (which is important!), but hurts performance cannam@132: // slightly. cannam@132: // cannam@132: // Note that trying to convert MIPS NaNs to standard NaNs without losing data doesn't work. cannam@132: // Signaling vs. quiet is indicated by a bit, with the meaning being the opposite on MIPS vs. cannam@132: // everyone else. It would be great if we could just flip that bit, but we can't, because if the cannam@132: // significand is all-zero, then the value is infinity rather than NaN. This means that on most cannam@132: // machines, where the bit indicates quietness, there is one more quiet NaN value than signalling cannam@132: // NaN value, whereas on MIPS there is one more sNaN than qNaN, and thus there is no isomorphic cannam@132: // mapping that properly preserves quietness. Instead of doing something hacky, we just give up cannam@132: // and blow away NaN payloads, because no one uses them anyway. cannam@132: #endif cannam@132: cannam@132: namespace capnp { cannam@132: cannam@132: #if !CAPNP_LITE cannam@132: class ClientHook; cannam@132: #endif // !CAPNP_LITE cannam@132: cannam@132: namespace _ { // private cannam@132: cannam@132: class PointerBuilder; cannam@132: class PointerReader; cannam@132: class StructBuilder; cannam@132: class StructReader; cannam@132: class ListBuilder; cannam@132: class ListReader; cannam@132: class OrphanBuilder; cannam@132: struct WirePointer; cannam@132: struct WireHelpers; cannam@132: class SegmentReader; cannam@132: class SegmentBuilder; cannam@132: class Arena; cannam@132: class BuilderArena; cannam@132: cannam@132: // ============================================================================= cannam@132: cannam@132: typedef decltype(BITS / ELEMENTS) BitsPerElement; cannam@132: typedef decltype(POINTERS / ELEMENTS) PointersPerElement; cannam@132: cannam@132: static constexpr BitsPerElement BITS_PER_ELEMENT_TABLE[8] = { cannam@132: 0 * BITS / ELEMENTS, cannam@132: 1 * BITS / ELEMENTS, cannam@132: 8 * BITS / ELEMENTS, cannam@132: 16 * BITS / ELEMENTS, cannam@132: 32 * BITS / ELEMENTS, cannam@132: 64 * BITS / ELEMENTS, cannam@132: 0 * BITS / ELEMENTS, cannam@132: 0 * BITS / ELEMENTS cannam@132: }; cannam@132: cannam@132: inline KJ_CONSTEXPR() BitsPerElement dataBitsPerElement(ElementSize size) { cannam@132: return _::BITS_PER_ELEMENT_TABLE[static_cast(size)]; cannam@132: } cannam@132: cannam@132: inline constexpr PointersPerElement pointersPerElement(ElementSize size) { cannam@132: return size == ElementSize::POINTER ? 1 * POINTERS / ELEMENTS : 0 * POINTERS / ELEMENTS; cannam@132: } cannam@132: cannam@132: template struct ElementSizeForByteSize; cannam@132: template <> struct ElementSizeForByteSize<1> { static constexpr ElementSize value = ElementSize::BYTE; }; cannam@132: template <> struct ElementSizeForByteSize<2> { static constexpr ElementSize value = ElementSize::TWO_BYTES; }; cannam@132: template <> struct ElementSizeForByteSize<4> { static constexpr ElementSize value = ElementSize::FOUR_BYTES; }; cannam@132: template <> struct ElementSizeForByteSize<8> { static constexpr ElementSize value = ElementSize::EIGHT_BYTES; }; cannam@132: cannam@132: template struct ElementSizeForType { cannam@132: static constexpr ElementSize value = cannam@132: // Primitive types that aren't special-cased below can be determined from sizeof(). cannam@132: CAPNP_KIND(T) == Kind::PRIMITIVE ? ElementSizeForByteSize::value : cannam@132: CAPNP_KIND(T) == Kind::ENUM ? ElementSize::TWO_BYTES : cannam@132: CAPNP_KIND(T) == Kind::STRUCT ? ElementSize::INLINE_COMPOSITE : cannam@132: cannam@132: // Everything else is a pointer. cannam@132: ElementSize::POINTER; cannam@132: }; cannam@132: cannam@132: // Void and bool are special. cannam@132: template <> struct ElementSizeForType { static constexpr ElementSize value = ElementSize::VOID; }; cannam@132: template <> struct ElementSizeForType { static constexpr ElementSize value = ElementSize::BIT; }; cannam@132: cannam@132: // Lists and blobs are pointers, not structs. cannam@132: template struct ElementSizeForType> { cannam@132: static constexpr ElementSize value = ElementSize::POINTER; cannam@132: }; cannam@132: template <> struct ElementSizeForType { cannam@132: static constexpr ElementSize value = ElementSize::POINTER; cannam@132: }; cannam@132: template <> struct ElementSizeForType { cannam@132: static constexpr ElementSize value = ElementSize::POINTER; cannam@132: }; cannam@132: cannam@132: template cannam@132: inline constexpr ElementSize elementSizeForType() { cannam@132: return ElementSizeForType::value; cannam@132: } cannam@132: cannam@132: struct MessageSizeCounts { cannam@132: WordCount64 wordCount; cannam@132: uint capCount; cannam@132: cannam@132: MessageSizeCounts& operator+=(const MessageSizeCounts& other) { cannam@132: wordCount += other.wordCount; cannam@132: capCount += other.capCount; cannam@132: return *this; cannam@132: } cannam@132: cannam@132: MessageSize asPublic() { cannam@132: return MessageSize { wordCount / WORDS, capCount }; cannam@132: } cannam@132: }; cannam@132: cannam@132: // ============================================================================= cannam@132: cannam@132: template cannam@132: union AlignedData { cannam@132: // Useful for declaring static constant data blobs as an array of bytes, but forcing those cannam@132: // bytes to be word-aligned. cannam@132: cannam@132: uint8_t bytes[wordCount * sizeof(word)]; cannam@132: word words[wordCount]; cannam@132: }; cannam@132: cannam@132: struct StructSize { cannam@132: WordCount16 data; cannam@132: WirePointerCount16 pointers; cannam@132: cannam@132: inline constexpr WordCount total() const { return data + pointers * WORDS_PER_POINTER; } cannam@132: cannam@132: StructSize() = default; cannam@132: inline constexpr StructSize(WordCount data, WirePointerCount pointers) cannam@132: : data(data), pointers(pointers) {} cannam@132: }; cannam@132: cannam@132: template cannam@132: inline constexpr StructSize structSize() { cannam@132: return StructSize(CapnpPrivate::dataWordSize * WORDS, CapnpPrivate::pointerCount * POINTERS); cannam@132: } cannam@132: cannam@132: template > cannam@132: inline constexpr StructSize minStructSizeForElement() { cannam@132: // If T is a struct, return its struct size. Otherwise return the minimum struct size big enough cannam@132: // to hold a T. cannam@132: cannam@132: return StructSize(CapnpPrivate::dataWordSize * WORDS, CapnpPrivate::pointerCount * POINTERS); cannam@132: } cannam@132: cannam@132: template > cannam@132: inline constexpr StructSize minStructSizeForElement() { cannam@132: // If T is a struct, return its struct size. Otherwise return the minimum struct size big enough cannam@132: // to hold a T. cannam@132: cannam@132: return StructSize( cannam@132: dataBitsPerElement(elementSizeForType()) * ELEMENTS > 0 * BITS ? 1 * WORDS : 0 * WORDS, cannam@132: pointersPerElement(elementSizeForType()) * ELEMENTS); cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: // Masking of default values cannam@132: cannam@132: template struct Mask_; cannam@132: template struct Mask_ { typedef T Type; }; cannam@132: template struct Mask_ { typedef uint16_t Type; }; cannam@132: template <> struct Mask_ { typedef uint32_t Type; }; cannam@132: template <> struct Mask_ { typedef uint64_t Type; }; cannam@132: cannam@132: template struct Mask_ { cannam@132: // Union discriminants end up here. cannam@132: static_assert(sizeof(T) == 2, "Don't know how to mask this type."); cannam@132: typedef uint16_t Type; cannam@132: }; cannam@132: cannam@132: template cannam@132: using Mask = typename Mask_::Type; cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(Mask mask(T value, Mask mask)); cannam@132: template cannam@132: KJ_ALWAYS_INLINE(T unmask(Mask value, Mask mask)); cannam@132: cannam@132: template cannam@132: inline Mask mask(T value, Mask mask) { cannam@132: return static_cast >(value) ^ mask; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline uint32_t mask(float value, uint32_t mask) { cannam@132: #if CAPNP_CANONICALIZE_NAN cannam@132: if (value != value) { cannam@132: return 0x7fc00000u ^ mask; cannam@132: } cannam@132: #endif cannam@132: cannam@132: uint32_t i; cannam@132: static_assert(sizeof(i) == sizeof(value), "float is not 32 bits?"); cannam@132: memcpy(&i, &value, sizeof(value)); cannam@132: return i ^ mask; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline uint64_t mask(double value, uint64_t mask) { cannam@132: #if CAPNP_CANONICALIZE_NAN cannam@132: if (value != value) { cannam@132: return 0x7ff8000000000000ull ^ mask; cannam@132: } cannam@132: #endif cannam@132: cannam@132: uint64_t i; cannam@132: static_assert(sizeof(i) == sizeof(value), "double is not 64 bits?"); cannam@132: memcpy(&i, &value, sizeof(value)); cannam@132: return i ^ mask; cannam@132: } cannam@132: cannam@132: template cannam@132: inline T unmask(Mask value, Mask mask) { cannam@132: return static_cast(value ^ mask); cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline float unmask(uint32_t value, uint32_t mask) { cannam@132: value ^= mask; cannam@132: float result; cannam@132: static_assert(sizeof(result) == sizeof(value), "float is not 32 bits?"); cannam@132: memcpy(&result, &value, sizeof(value)); cannam@132: return result; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline double unmask(uint64_t value, uint64_t mask) { cannam@132: value ^= mask; cannam@132: double result; cannam@132: static_assert(sizeof(result) == sizeof(value), "double is not 64 bits?"); cannam@132: memcpy(&result, &value, sizeof(value)); cannam@132: return result; cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: class CapTableReader { cannam@132: public: cannam@132: #if !CAPNP_LITE cannam@132: virtual kj::Maybe> extractCap(uint index) = 0; cannam@132: // Extract the capability at the given index. If the index is invalid, returns null. cannam@132: #endif // !CAPNP_LITE cannam@132: }; cannam@132: cannam@132: class CapTableBuilder: public CapTableReader { cannam@132: public: cannam@132: #if !CAPNP_LITE cannam@132: virtual uint injectCap(kj::Own&& cap) = 0; cannam@132: // Add the capability to the message and return its index. If the same ClientHook is injected cannam@132: // twice, this may return the same index both times, but in this case dropCap() needs to be cannam@132: // called an equal number of times to actually remove the cap. cannam@132: cannam@132: virtual void dropCap(uint index) = 0; cannam@132: // Remove a capability injected earlier. Called when the pointer is overwritten or zero'd out. cannam@132: #endif // !CAPNP_LITE cannam@132: }; cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: class PointerBuilder: public kj::DisallowConstCopy { cannam@132: // Represents a single pointer, usually embedded in a struct or a list. cannam@132: cannam@132: public: cannam@132: inline PointerBuilder(): segment(nullptr), capTable(nullptr), pointer(nullptr) {} cannam@132: cannam@132: static inline PointerBuilder getRoot( cannam@132: SegmentBuilder* segment, CapTableBuilder* capTable, word* location); cannam@132: // Get a PointerBuilder representing a message root located in the given segment at the given cannam@132: // location. cannam@132: cannam@132: inline bool isNull() { return getPointerType() == PointerType::NULL_; } cannam@132: PointerType getPointerType(); cannam@132: cannam@132: StructBuilder getStruct(StructSize size, const word* defaultValue); cannam@132: ListBuilder getList(ElementSize elementSize, const word* defaultValue); cannam@132: ListBuilder getStructList(StructSize elementSize, const word* defaultValue); cannam@132: ListBuilder getListAnySize(const word* defaultValue); cannam@132: template typename T::Builder getBlob(const void* defaultValue,ByteCount defaultSize); cannam@132: #if !CAPNP_LITE cannam@132: kj::Own getCapability(); cannam@132: #endif // !CAPNP_LITE cannam@132: // Get methods: Get the value. If it is null, initialize it to a copy of the default value. cannam@132: // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a cannam@132: // simple byte array for blobs. cannam@132: cannam@132: StructBuilder initStruct(StructSize size); cannam@132: ListBuilder initList(ElementSize elementSize, ElementCount elementCount); cannam@132: ListBuilder initStructList(ElementCount elementCount, StructSize size); cannam@132: template typename T::Builder initBlob(ByteCount size); cannam@132: // Init methods: Initialize the pointer to a newly-allocated object, discarding the existing cannam@132: // object. cannam@132: cannam@132: void setStruct(const StructReader& value, bool canonical = false); cannam@132: void setList(const ListReader& value, bool canonical = false); cannam@132: template void setBlob(typename T::Reader value); cannam@132: #if !CAPNP_LITE cannam@132: void setCapability(kj::Own&& cap); cannam@132: #endif // !CAPNP_LITE cannam@132: // Set methods: Initialize the pointer to a newly-allocated copy of the given value, discarding cannam@132: // the existing object. cannam@132: cannam@132: void adopt(OrphanBuilder&& orphan); cannam@132: // Set the pointer to point at the given orphaned value. cannam@132: cannam@132: OrphanBuilder disown(); cannam@132: // Set the pointer to null and return its previous value as an orphan. cannam@132: cannam@132: void clear(); cannam@132: // Clear the pointer to null, discarding its previous value. cannam@132: cannam@132: void transferFrom(PointerBuilder other); cannam@132: // Equivalent to `adopt(other.disown())`. cannam@132: cannam@132: void copyFrom(PointerReader other, bool canonical = false); cannam@132: // Equivalent to `set(other.get())`. cannam@132: // If you set the canonical flag, it will attempt to lay the target out cannam@132: // canonically, provided enough space is available. cannam@132: cannam@132: PointerReader asReader() const; cannam@132: cannam@132: BuilderArena* getArena() const; cannam@132: // Get the arena containing this pointer. cannam@132: cannam@132: CapTableBuilder* getCapTable(); cannam@132: // Gets the capability context in which this object is operating. cannam@132: cannam@132: PointerBuilder imbue(CapTableBuilder* capTable); cannam@132: // Return a copy of this builder except using the given capability context. cannam@132: cannam@132: private: cannam@132: SegmentBuilder* segment; // Memory segment in which the pointer resides. cannam@132: CapTableBuilder* capTable; // Table of capability indexes. cannam@132: WirePointer* pointer; // Pointer to the pointer. cannam@132: cannam@132: inline PointerBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* pointer) cannam@132: : segment(segment), capTable(capTable), pointer(pointer) {} cannam@132: cannam@132: friend class StructBuilder; cannam@132: friend class ListBuilder; cannam@132: friend class OrphanBuilder; cannam@132: }; cannam@132: cannam@132: class PointerReader { cannam@132: public: cannam@132: inline PointerReader() cannam@132: : segment(nullptr), capTable(nullptr), pointer(nullptr), nestingLimit(0x7fffffff) {} cannam@132: cannam@132: static PointerReader getRoot(SegmentReader* segment, CapTableReader* capTable, cannam@132: const word* location, int nestingLimit); cannam@132: // Get a PointerReader representing a message root located in the given segment at the given cannam@132: // location. cannam@132: cannam@132: static inline PointerReader getRootUnchecked(const word* location); cannam@132: // Get a PointerReader for an unchecked message. cannam@132: cannam@132: MessageSizeCounts targetSize() const; cannam@132: // Return the total size of the target object and everything to which it points. Does not count cannam@132: // far pointer overhead. This is useful for deciding how much space is needed to copy the object cannam@132: // into a flat array. However, the caller is advised NOT to treat this value as secure. Instead, cannam@132: // use the result as a hint for allocating the first segment, do the copy, and then throw an cannam@132: // exception if it overruns. cannam@132: cannam@132: inline bool isNull() const { return getPointerType() == PointerType::NULL_; } cannam@132: PointerType getPointerType() const; cannam@132: cannam@132: StructReader getStruct(const word* defaultValue) const; cannam@132: ListReader getList(ElementSize expectedElementSize, const word* defaultValue) const; cannam@132: ListReader getListAnySize(const word* defaultValue) const; cannam@132: template cannam@132: typename T::Reader getBlob(const void* defaultValue, ByteCount defaultSize) const; cannam@132: #if !CAPNP_LITE cannam@132: kj::Own getCapability() const; cannam@132: #endif // !CAPNP_LITE cannam@132: // Get methods: Get the value. If it is null, return the default value instead. cannam@132: // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a cannam@132: // simple byte array for blobs. cannam@132: cannam@132: const word* getUnchecked() const; cannam@132: // If this is an unchecked message, get a word* pointing at the location of the pointer. This cannam@132: // word* can actually be passed to readUnchecked() to read the designated sub-object later. If cannam@132: // this isn't an unchecked message, throws an exception. cannam@132: cannam@132: kj::Maybe getArena() const; cannam@132: // Get the arena containing this pointer. cannam@132: cannam@132: CapTableReader* getCapTable(); cannam@132: // Gets the capability context in which this object is operating. cannam@132: cannam@132: PointerReader imbue(CapTableReader* capTable) const; cannam@132: // Return a copy of this reader except using the given capability context. cannam@132: cannam@132: bool isCanonical(const word **readHead); cannam@132: // Validate this pointer's canonicity, subject to the conditions: cannam@132: // * All data to the left of readHead has been read thus far (for pointer cannam@132: // ordering) cannam@132: // * All pointers in preorder have already been checked cannam@132: // * This pointer is in the first and only segment of the message cannam@132: cannam@132: private: cannam@132: SegmentReader* segment; // Memory segment in which the pointer resides. cannam@132: CapTableReader* capTable; // Table of capability indexes. cannam@132: const WirePointer* pointer; // Pointer to the pointer. null = treat as null pointer. cannam@132: cannam@132: int nestingLimit; cannam@132: // Limits the depth of message structures to guard against stack-overflow-based DoS attacks. cannam@132: // Once this reaches zero, further pointers will be pruned. cannam@132: cannam@132: inline PointerReader(SegmentReader* segment, CapTableReader* capTable, cannam@132: const WirePointer* pointer, int nestingLimit) cannam@132: : segment(segment), capTable(capTable), pointer(pointer), nestingLimit(nestingLimit) {} cannam@132: cannam@132: friend class StructReader; cannam@132: friend class ListReader; cannam@132: friend class PointerBuilder; cannam@132: friend class OrphanBuilder; cannam@132: }; cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: class StructBuilder: public kj::DisallowConstCopy { cannam@132: public: cannam@132: inline StructBuilder(): segment(nullptr), capTable(nullptr), data(nullptr), pointers(nullptr) {} cannam@132: cannam@132: inline word* getLocation() { return reinterpret_cast(data); } cannam@132: // Get the object's location. Only valid for independently-allocated objects (i.e. not list cannam@132: // elements). cannam@132: cannam@132: inline BitCount getDataSectionSize() const { return dataSize; } cannam@132: inline WirePointerCount getPointerSectionSize() const { return pointerCount; } cannam@132: inline kj::ArrayPtr getDataSectionAsBlob(); cannam@132: inline _::ListBuilder getPointerSectionAsList(); cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset)); cannam@132: // Return true if the field is set to something other than its default value. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(T getDataField(ElementCount offset)); cannam@132: // Gets the data field value of the given type at the given offset. The offset is measured in cannam@132: // multiples of the field size, determined by the type. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(T getDataField(ElementCount offset, Mask mask)); cannam@132: // Like getDataField() but applies the given XOR mask to the data on load. Used for reading cannam@132: // fields with non-zero default values. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(void setDataField( cannam@132: ElementCount offset, kj::NoInfer value)); cannam@132: // Sets the data field value at the given offset. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(void setDataField( cannam@132: ElementCount offset, kj::NoInfer value, Mask mask)); cannam@132: // Like setDataField() but applies the given XOR mask before storing. Used for writing fields cannam@132: // with non-zero default values. cannam@132: cannam@132: KJ_ALWAYS_INLINE(PointerBuilder getPointerField(WirePointerCount ptrIndex)); cannam@132: // Get a builder for a pointer field given the index within the pointer section. cannam@132: cannam@132: void clearAll(); cannam@132: // Clear all pointers and data. cannam@132: cannam@132: void transferContentFrom(StructBuilder other); cannam@132: // Adopt all pointers from `other`, and also copy all data. If `other`'s sections are larger cannam@132: // than this, the extra data is not transferred, meaning there is a risk of data loss when cannam@132: // transferring from messages built with future versions of the protocol. cannam@132: cannam@132: void copyContentFrom(StructReader other); cannam@132: // Copy content from `other`. If `other`'s sections are larger than this, the extra data is not cannam@132: // copied, meaning there is a risk of data loss when copying from messages built with future cannam@132: // versions of the protocol. cannam@132: cannam@132: StructReader asReader() const; cannam@132: // Gets a StructReader pointing at the same memory. cannam@132: cannam@132: BuilderArena* getArena(); cannam@132: // Gets the arena in which this object is allocated. cannam@132: cannam@132: CapTableBuilder* getCapTable(); cannam@132: // Gets the capability context in which this object is operating. cannam@132: cannam@132: StructBuilder imbue(CapTableBuilder* capTable); cannam@132: // Return a copy of this builder except using the given capability context. cannam@132: cannam@132: private: cannam@132: SegmentBuilder* segment; // Memory segment in which the struct resides. cannam@132: CapTableBuilder* capTable; // Table of capability indexes. cannam@132: void* data; // Pointer to the encoded data. cannam@132: WirePointer* pointers; // Pointer to the encoded pointers. cannam@132: cannam@132: BitCount32 dataSize; cannam@132: // Size of data section. We use a bit count rather than a word count to more easily handle the cannam@132: // case of struct lists encoded with less than a word per element. cannam@132: cannam@132: WirePointerCount16 pointerCount; // Size of the pointer section. cannam@132: cannam@132: inline StructBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, cannam@132: void* data, WirePointer* pointers, cannam@132: BitCount dataSize, WirePointerCount pointerCount) cannam@132: : segment(segment), capTable(capTable), data(data), pointers(pointers), cannam@132: dataSize(dataSize), pointerCount(pointerCount) {} cannam@132: cannam@132: friend class ListBuilder; cannam@132: friend struct WireHelpers; cannam@132: friend class OrphanBuilder; cannam@132: }; cannam@132: cannam@132: class StructReader { cannam@132: public: cannam@132: inline StructReader() cannam@132: : segment(nullptr), capTable(nullptr), data(nullptr), pointers(nullptr), dataSize(0), cannam@132: pointerCount(0), nestingLimit(0x7fffffff) {} cannam@132: inline StructReader(kj::ArrayPtr data) cannam@132: : segment(nullptr), capTable(nullptr), data(data.begin()), pointers(nullptr), cannam@132: dataSize(data.size() * WORDS * BITS_PER_WORD), pointerCount(0), nestingLimit(0x7fffffff) {} cannam@132: cannam@132: const void* getLocation() const { return data; } cannam@132: cannam@132: inline BitCount getDataSectionSize() const { return dataSize; } cannam@132: inline WirePointerCount getPointerSectionSize() const { return pointerCount; } cannam@132: inline kj::ArrayPtr getDataSectionAsBlob(); cannam@132: inline _::ListReader getPointerSectionAsList(); cannam@132: cannam@132: kj::Array canonicalize(); cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset) const); cannam@132: // Return true if the field is set to something other than its default value. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(T getDataField(ElementCount offset) const); cannam@132: // Get the data field value of the given type at the given offset. The offset is measured in cannam@132: // multiples of the field size, determined by the type. Returns zero if the offset is past the cannam@132: // end of the struct's data section. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE( cannam@132: T getDataField(ElementCount offset, Mask mask) const); cannam@132: // Like getDataField(offset), but applies the given XOR mask to the result. Used for reading cannam@132: // fields with non-zero default values. cannam@132: cannam@132: KJ_ALWAYS_INLINE(PointerReader getPointerField(WirePointerCount ptrIndex) const); cannam@132: // Get a reader for a pointer field given the index within the pointer section. If the index cannam@132: // is out-of-bounds, returns a null pointer. cannam@132: cannam@132: MessageSizeCounts totalSize() const; cannam@132: // Return the total size of the struct and everything to which it points. Does not count far cannam@132: // pointer overhead. This is useful for deciding how much space is needed to copy the struct cannam@132: // into a flat array. However, the caller is advised NOT to treat this value as secure. Instead, cannam@132: // use the result as a hint for allocating the first segment, do the copy, and then throw an cannam@132: // exception if it overruns. cannam@132: cannam@132: CapTableReader* getCapTable(); cannam@132: // Gets the capability context in which this object is operating. cannam@132: cannam@132: StructReader imbue(CapTableReader* capTable) const; cannam@132: // Return a copy of this reader except using the given capability context. cannam@132: cannam@132: bool isCanonical(const word **readHead, const word **ptrHead, cannam@132: bool *dataTrunc, bool *ptrTrunc); cannam@132: // Validate this pointer's canonicity, subject to the conditions: cannam@132: // * All data to the left of readHead has been read thus far (for pointer cannam@132: // ordering) cannam@132: // * All pointers in preorder have already been checked cannam@132: // * This pointer is in the first and only segment of the message cannam@132: // cannam@132: // If this function returns false, the struct is non-canonical. If it cannam@132: // returns true, then: cannam@132: // * If it is a composite in a list, it is canonical if at least one struct cannam@132: // in the list outputs dataTrunc = 1, and at least one outputs ptrTrunc = 1 cannam@132: // * If it is derived from a struct pointer, it is canonical if cannam@132: // dataTrunc = 1 AND ptrTrunc = 1 cannam@132: cannam@132: private: cannam@132: SegmentReader* segment; // Memory segment in which the struct resides. cannam@132: CapTableReader* capTable; // Table of capability indexes. cannam@132: cannam@132: const void* data; cannam@132: const WirePointer* pointers; cannam@132: cannam@132: BitCount32 dataSize; cannam@132: // Size of data section. We use a bit count rather than a word count to more easily handle the cannam@132: // case of struct lists encoded with less than a word per element. cannam@132: cannam@132: WirePointerCount16 pointerCount; // Size of the pointer section. cannam@132: cannam@132: int nestingLimit; cannam@132: // Limits the depth of message structures to guard against stack-overflow-based DoS attacks. cannam@132: // Once this reaches zero, further pointers will be pruned. cannam@132: // TODO(perf): Limit to 16 bits for better packing? cannam@132: cannam@132: inline StructReader(SegmentReader* segment, CapTableReader* capTable, cannam@132: const void* data, const WirePointer* pointers, cannam@132: BitCount dataSize, WirePointerCount pointerCount, int nestingLimit) cannam@132: : segment(segment), capTable(capTable), data(data), pointers(pointers), cannam@132: dataSize(dataSize), pointerCount(pointerCount), cannam@132: nestingLimit(nestingLimit) {} cannam@132: cannam@132: friend class ListReader; cannam@132: friend class StructBuilder; cannam@132: friend struct WireHelpers; cannam@132: }; cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: class ListBuilder: public kj::DisallowConstCopy { cannam@132: public: cannam@132: inline explicit ListBuilder(ElementSize elementSize) cannam@132: : segment(nullptr), capTable(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS), cannam@132: step(0 * BITS / ELEMENTS), structDataSize(0 * BITS), structPointerCount(0 * POINTERS), cannam@132: elementSize(elementSize) {} cannam@132: cannam@132: inline word* getLocation() { cannam@132: // Get the object's location. cannam@132: cannam@132: if (elementSize == ElementSize::INLINE_COMPOSITE && ptr != nullptr) { cannam@132: return reinterpret_cast(ptr) - POINTER_SIZE_IN_WORDS; cannam@132: } else { cannam@132: return reinterpret_cast(ptr); cannam@132: } cannam@132: } cannam@132: cannam@132: inline ElementSize getElementSize() const { return elementSize; } cannam@132: cannam@132: inline ElementCount size() const; cannam@132: // The number of elements in the list. cannam@132: cannam@132: Text::Builder asText(); cannam@132: Data::Builder asData(); cannam@132: // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(T getDataElement(ElementCount index)); cannam@132: // Get the element of the given type at the given index. cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(void setDataElement( cannam@132: ElementCount index, kj::NoInfer value)); cannam@132: // Set the element at the given index. cannam@132: cannam@132: KJ_ALWAYS_INLINE(PointerBuilder getPointerElement(ElementCount index)); cannam@132: cannam@132: StructBuilder getStructElement(ElementCount index); cannam@132: cannam@132: ListReader asReader() const; cannam@132: // Get a ListReader pointing at the same memory. cannam@132: cannam@132: BuilderArena* getArena(); cannam@132: // Gets the arena in which this object is allocated. cannam@132: cannam@132: CapTableBuilder* getCapTable(); cannam@132: // Gets the capability context in which this object is operating. cannam@132: cannam@132: ListBuilder imbue(CapTableBuilder* capTable); cannam@132: // Return a copy of this builder except using the given capability context. cannam@132: cannam@132: private: cannam@132: SegmentBuilder* segment; // Memory segment in which the list resides. cannam@132: CapTableBuilder* capTable; // Table of capability indexes. cannam@132: cannam@132: byte* ptr; // Pointer to list content. cannam@132: cannam@132: ElementCount elementCount; // Number of elements in the list. cannam@132: cannam@132: decltype(BITS / ELEMENTS) step; cannam@132: // The distance between elements. cannam@132: cannam@132: BitCount32 structDataSize; cannam@132: WirePointerCount16 structPointerCount; cannam@132: // The struct properties to use when interpreting the elements as structs. All lists can be cannam@132: // interpreted as struct lists, so these are always filled in. cannam@132: cannam@132: ElementSize elementSize; cannam@132: // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE cannam@132: // from other types when the overall size is exactly zero or one words. cannam@132: cannam@132: inline ListBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, void* ptr, cannam@132: decltype(BITS / ELEMENTS) step, ElementCount size, cannam@132: BitCount structDataSize, WirePointerCount structPointerCount, cannam@132: ElementSize elementSize) cannam@132: : segment(segment), capTable(capTable), ptr(reinterpret_cast(ptr)), cannam@132: elementCount(size), step(step), structDataSize(structDataSize), cannam@132: structPointerCount(structPointerCount), elementSize(elementSize) {} cannam@132: cannam@132: friend class StructBuilder; cannam@132: friend struct WireHelpers; cannam@132: friend class OrphanBuilder; cannam@132: }; cannam@132: cannam@132: class ListReader { cannam@132: public: cannam@132: inline explicit ListReader(ElementSize elementSize) cannam@132: : segment(nullptr), capTable(nullptr), ptr(nullptr), elementCount(0), cannam@132: step(0 * BITS / ELEMENTS), structDataSize(0), structPointerCount(0), cannam@132: elementSize(elementSize), nestingLimit(0x7fffffff) {} cannam@132: cannam@132: inline ElementCount size() const; cannam@132: // The number of elements in the list. cannam@132: cannam@132: inline ElementSize getElementSize() const { return elementSize; } cannam@132: cannam@132: Text::Reader asText(); cannam@132: Data::Reader asData(); cannam@132: // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized. cannam@132: cannam@132: kj::ArrayPtr asRawBytes(); cannam@132: cannam@132: template cannam@132: KJ_ALWAYS_INLINE(T getDataElement(ElementCount index) const); cannam@132: // Get the element of the given type at the given index. cannam@132: cannam@132: KJ_ALWAYS_INLINE(PointerReader getPointerElement(ElementCount index) const); cannam@132: cannam@132: StructReader getStructElement(ElementCount index) const; cannam@132: cannam@132: CapTableReader* getCapTable(); cannam@132: // Gets the capability context in which this object is operating. cannam@132: cannam@132: ListReader imbue(CapTableReader* capTable) const; cannam@132: // Return a copy of this reader except using the given capability context. cannam@132: cannam@132: bool isCanonical(const word **readHead); cannam@132: // Validate this pointer's canonicity, subject to the conditions: cannam@132: // * All data to the left of readHead has been read thus far (for pointer cannam@132: // ordering) cannam@132: // * All pointers in preorder have already been checked cannam@132: // * This pointer is in the first and only segment of the message cannam@132: cannam@132: private: cannam@132: SegmentReader* segment; // Memory segment in which the list resides. cannam@132: CapTableReader* capTable; // Table of capability indexes. cannam@132: cannam@132: const byte* ptr; // Pointer to list content. cannam@132: cannam@132: ElementCount elementCount; // Number of elements in the list. cannam@132: cannam@132: decltype(BITS / ELEMENTS) step; cannam@132: // The distance between elements. cannam@132: cannam@132: BitCount32 structDataSize; cannam@132: WirePointerCount16 structPointerCount; cannam@132: // The struct properties to use when interpreting the elements as structs. All lists can be cannam@132: // interpreted as struct lists, so these are always filled in. cannam@132: cannam@132: ElementSize elementSize; cannam@132: // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE cannam@132: // from other types when the overall size is exactly zero or one words. cannam@132: cannam@132: int nestingLimit; cannam@132: // Limits the depth of message structures to guard against stack-overflow-based DoS attacks. cannam@132: // Once this reaches zero, further pointers will be pruned. cannam@132: cannam@132: inline ListReader(SegmentReader* segment, CapTableReader* capTable, const void* ptr, cannam@132: ElementCount elementCount, decltype(BITS / ELEMENTS) step, cannam@132: BitCount structDataSize, WirePointerCount structPointerCount, cannam@132: ElementSize elementSize, int nestingLimit) cannam@132: : segment(segment), capTable(capTable), ptr(reinterpret_cast(ptr)), cannam@132: elementCount(elementCount), step(step), structDataSize(structDataSize), cannam@132: structPointerCount(structPointerCount), elementSize(elementSize), cannam@132: nestingLimit(nestingLimit) {} cannam@132: cannam@132: friend class StructReader; cannam@132: friend class ListBuilder; cannam@132: friend struct WireHelpers; cannam@132: friend class OrphanBuilder; cannam@132: }; cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: class OrphanBuilder { cannam@132: public: cannam@132: inline OrphanBuilder(): segment(nullptr), capTable(nullptr), location(nullptr) { cannam@132: memset(&tag, 0, sizeof(tag)); cannam@132: } cannam@132: OrphanBuilder(const OrphanBuilder& other) = delete; cannam@132: inline OrphanBuilder(OrphanBuilder&& other) noexcept; cannam@132: inline ~OrphanBuilder() noexcept(false); cannam@132: cannam@132: static OrphanBuilder initStruct(BuilderArena* arena, CapTableBuilder* capTable, StructSize size); cannam@132: static OrphanBuilder initList(BuilderArena* arena, CapTableBuilder* capTable, cannam@132: ElementCount elementCount, ElementSize elementSize); cannam@132: static OrphanBuilder initStructList(BuilderArena* arena, CapTableBuilder* capTable, cannam@132: ElementCount elementCount, StructSize elementSize); cannam@132: static OrphanBuilder initText(BuilderArena* arena, CapTableBuilder* capTable, ByteCount size); cannam@132: static OrphanBuilder initData(BuilderArena* arena, CapTableBuilder* capTable, ByteCount size); cannam@132: cannam@132: static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, StructReader copyFrom); cannam@132: static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, ListReader copyFrom); cannam@132: static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, PointerReader copyFrom); cannam@132: static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, Text::Reader copyFrom); cannam@132: static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, Data::Reader copyFrom); cannam@132: #if !CAPNP_LITE cannam@132: static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, cannam@132: kj::Own copyFrom); cannam@132: #endif // !CAPNP_LITE cannam@132: cannam@132: static OrphanBuilder concat(BuilderArena* arena, CapTableBuilder* capTable, cannam@132: ElementSize expectedElementSize, StructSize expectedStructSize, cannam@132: kj::ArrayPtr lists); cannam@132: cannam@132: static OrphanBuilder referenceExternalData(BuilderArena* arena, Data::Reader data); cannam@132: cannam@132: OrphanBuilder& operator=(const OrphanBuilder& other) = delete; cannam@132: inline OrphanBuilder& operator=(OrphanBuilder&& other); cannam@132: cannam@132: inline bool operator==(decltype(nullptr)) const { return location == nullptr; } cannam@132: inline bool operator!=(decltype(nullptr)) const { return location != nullptr; } cannam@132: cannam@132: StructBuilder asStruct(StructSize size); cannam@132: // Interpret as a struct, or throw an exception if not a struct. cannam@132: cannam@132: ListBuilder asList(ElementSize elementSize); cannam@132: // Interpret as a list, or throw an exception if not a list. elementSize cannot be cannam@132: // INLINE_COMPOSITE -- use asStructList() instead. cannam@132: cannam@132: ListBuilder asStructList(StructSize elementSize); cannam@132: // Interpret as a struct list, or throw an exception if not a list. cannam@132: cannam@132: Text::Builder asText(); cannam@132: Data::Builder asData(); cannam@132: // Interpret as a blob, or throw an exception if not a blob. cannam@132: cannam@132: StructReader asStructReader(StructSize size) const; cannam@132: ListReader asListReader(ElementSize elementSize) const; cannam@132: #if !CAPNP_LITE cannam@132: kj::Own asCapability() const; cannam@132: #endif // !CAPNP_LITE cannam@132: Text::Reader asTextReader() const; cannam@132: Data::Reader asDataReader() const; cannam@132: cannam@132: bool truncate(ElementCount size, bool isText) KJ_WARN_UNUSED_RESULT; cannam@132: // Resize the orphan list to the given size. Returns false if the list is currently empty but cannam@132: // the requested size is non-zero, in which case the caller will need to allocate a new list. cannam@132: cannam@132: void truncate(ElementCount size, ElementSize elementSize); cannam@132: void truncate(ElementCount size, StructSize elementSize); cannam@132: void truncateText(ElementCount size); cannam@132: // Versions of truncate() that know how to allocate a new list if needed. cannam@132: cannam@132: private: cannam@132: static_assert(1 * POINTERS * WORDS_PER_POINTER == 1 * WORDS, cannam@132: "This struct assumes a pointer is one word."); cannam@132: word tag; cannam@132: // Contains an encoded WirePointer representing this object. WirePointer is defined in cannam@132: // layout.c++, but fits in a word. cannam@132: // cannam@132: // This may be a FAR pointer. Even in that case, `location` points to the eventual destination cannam@132: // of that far pointer. The reason we keep the far pointer around rather than just making `tag` cannam@132: // represent the final destination is because if the eventual adopter of the pointer is not in cannam@132: // the target's segment then it may be useful to reuse the far pointer landing pad. cannam@132: // cannam@132: // If `tag` is not a far pointer, its offset is garbage; only `location` points to the actual cannam@132: // target. cannam@132: cannam@132: SegmentBuilder* segment; cannam@132: // Segment in which the object resides. cannam@132: cannam@132: CapTableBuilder* capTable; cannam@132: // Table of capability indexes. cannam@132: cannam@132: word* location; cannam@132: // Pointer to the object, or nullptr if the pointer is null. For capabilities, we make this cannam@132: // 0x1 just so that it is non-null for operator==, but it is never used. cannam@132: cannam@132: inline OrphanBuilder(const void* tagPtr, SegmentBuilder* segment, cannam@132: CapTableBuilder* capTable, word* location) cannam@132: : segment(segment), capTable(capTable), location(location) { cannam@132: memcpy(&tag, tagPtr, sizeof(tag)); cannam@132: } cannam@132: cannam@132: inline WirePointer* tagAsPtr() { return reinterpret_cast(&tag); } cannam@132: inline const WirePointer* tagAsPtr() const { return reinterpret_cast(&tag); } cannam@132: cannam@132: void euthanize(); cannam@132: // Erase the target object, zeroing it out and possibly reclaiming the memory. Called when cannam@132: // the OrphanBuilder is being destroyed or overwritten and it is non-null. cannam@132: cannam@132: friend struct WireHelpers; cannam@132: }; cannam@132: cannam@132: // ======================================================================================= cannam@132: // Internal implementation details... cannam@132: cannam@132: // These are defined in the source file. cannam@132: template <> typename Text::Builder PointerBuilder::initBlob(ByteCount size); cannam@132: template <> void PointerBuilder::setBlob(typename Text::Reader value); cannam@132: template <> typename Text::Builder PointerBuilder::getBlob(const void* defaultValue, ByteCount defaultSize); cannam@132: template <> typename Text::Reader PointerReader::getBlob(const void* defaultValue, ByteCount defaultSize) const; cannam@132: cannam@132: template <> typename Data::Builder PointerBuilder::initBlob(ByteCount size); cannam@132: template <> void PointerBuilder::setBlob(typename Data::Reader value); cannam@132: template <> typename Data::Builder PointerBuilder::getBlob(const void* defaultValue, ByteCount defaultSize); cannam@132: template <> typename Data::Reader PointerReader::getBlob(const void* defaultValue, ByteCount defaultSize) const; cannam@132: cannam@132: inline PointerBuilder PointerBuilder::getRoot( cannam@132: SegmentBuilder* segment, CapTableBuilder* capTable, word* location) { cannam@132: return PointerBuilder(segment, capTable, reinterpret_cast(location)); cannam@132: } cannam@132: cannam@132: inline PointerReader PointerReader::getRootUnchecked(const word* location) { cannam@132: return PointerReader(nullptr, nullptr, cannam@132: reinterpret_cast(location), 0x7fffffff); cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: inline kj::ArrayPtr StructBuilder::getDataSectionAsBlob() { cannam@132: return kj::ArrayPtr(reinterpret_cast(data), dataSize / BITS_PER_BYTE / BYTES); cannam@132: } cannam@132: cannam@132: inline _::ListBuilder StructBuilder::getPointerSectionAsList() { cannam@132: return _::ListBuilder(segment, capTable, pointers, 1 * POINTERS * BITS_PER_POINTER / ELEMENTS, cannam@132: pointerCount * (1 * ELEMENTS / POINTERS), cannam@132: 0 * BITS, 1 * POINTERS, ElementSize::POINTER); cannam@132: } cannam@132: cannam@132: template cannam@132: inline bool StructBuilder::hasDataField(ElementCount offset) { cannam@132: return getDataField>(offset) != 0; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline bool StructBuilder::hasDataField(ElementCount offset) { cannam@132: return false; cannam@132: } cannam@132: cannam@132: template cannam@132: inline T StructBuilder::getDataField(ElementCount offset) { cannam@132: return reinterpret_cast*>(data)[offset / ELEMENTS].get(); cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline bool StructBuilder::getDataField(ElementCount offset) { cannam@132: BitCount boffset = offset * (1 * BITS / ELEMENTS); cannam@132: byte* b = reinterpret_cast(data) + boffset / BITS_PER_BYTE; cannam@132: return (*reinterpret_cast(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline Void StructBuilder::getDataField(ElementCount offset) { cannam@132: return VOID; cannam@132: } cannam@132: cannam@132: template cannam@132: inline T StructBuilder::getDataField(ElementCount offset, Mask mask) { cannam@132: return unmask(getDataField >(offset), mask); cannam@132: } cannam@132: cannam@132: template cannam@132: inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer value) { cannam@132: reinterpret_cast*>(data)[offset / ELEMENTS].set(value); cannam@132: } cannam@132: cannam@132: #if CAPNP_CANONICALIZE_NAN cannam@132: // Use mask() on floats and doubles to make sure we canonicalize NaNs. cannam@132: template <> cannam@132: inline void StructBuilder::setDataField(ElementCount offset, float value) { cannam@132: setDataField(offset, mask(value, 0)); cannam@132: } cannam@132: template <> cannam@132: inline void StructBuilder::setDataField(ElementCount offset, double value) { cannam@132: setDataField(offset, mask(value, 0)); cannam@132: } cannam@132: #endif cannam@132: cannam@132: template <> cannam@132: inline void StructBuilder::setDataField(ElementCount offset, bool value) { cannam@132: BitCount boffset = offset * (1 * BITS / ELEMENTS); cannam@132: byte* b = reinterpret_cast(data) + boffset / BITS_PER_BYTE; cannam@132: uint bitnum = boffset % BITS_PER_BYTE / BITS; cannam@132: *reinterpret_cast(b) = (*reinterpret_cast(b) & ~(1 << bitnum)) cannam@132: | (static_cast(value) << bitnum); cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline void StructBuilder::setDataField(ElementCount offset, Void value) {} cannam@132: cannam@132: template cannam@132: inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer value, Mask m) { cannam@132: setDataField >(offset, mask(value, m)); cannam@132: } cannam@132: cannam@132: inline PointerBuilder StructBuilder::getPointerField(WirePointerCount ptrIndex) { cannam@132: // Hacky because WirePointer is defined in the .c++ file (so is incomplete here). cannam@132: return PointerBuilder(segment, capTable, reinterpret_cast( cannam@132: reinterpret_cast(pointers) + ptrIndex * WORDS_PER_POINTER)); cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: inline kj::ArrayPtr StructReader::getDataSectionAsBlob() { cannam@132: return kj::ArrayPtr(reinterpret_cast(data), dataSize / BITS_PER_BYTE / BYTES); cannam@132: } cannam@132: cannam@132: inline _::ListReader StructReader::getPointerSectionAsList() { cannam@132: return _::ListReader(segment, capTable, pointers, pointerCount * (1 * ELEMENTS / POINTERS), cannam@132: 1 * POINTERS * BITS_PER_POINTER / ELEMENTS, 0 * BITS, 1 * POINTERS, cannam@132: ElementSize::POINTER, nestingLimit); cannam@132: } cannam@132: cannam@132: template cannam@132: inline bool StructReader::hasDataField(ElementCount offset) const { cannam@132: return getDataField>(offset) != 0; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline bool StructReader::hasDataField(ElementCount offset) const { cannam@132: return false; cannam@132: } cannam@132: cannam@132: template cannam@132: inline T StructReader::getDataField(ElementCount offset) const { cannam@132: if ((offset + 1 * ELEMENTS) * capnp::bitsPerElement() <= dataSize) { cannam@132: return reinterpret_cast*>(data)[offset / ELEMENTS].get(); cannam@132: } else { cannam@132: return static_cast(0); cannam@132: } cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline bool StructReader::getDataField(ElementCount offset) const { cannam@132: BitCount boffset = offset * (1 * BITS / ELEMENTS); cannam@132: if (boffset < dataSize) { cannam@132: const byte* b = reinterpret_cast(data) + boffset / BITS_PER_BYTE; cannam@132: return (*reinterpret_cast(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0; cannam@132: } else { cannam@132: return false; cannam@132: } cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline Void StructReader::getDataField(ElementCount offset) const { cannam@132: return VOID; cannam@132: } cannam@132: cannam@132: template cannam@132: T StructReader::getDataField(ElementCount offset, Mask mask) const { cannam@132: return unmask(getDataField >(offset), mask); cannam@132: } cannam@132: cannam@132: inline PointerReader StructReader::getPointerField(WirePointerCount ptrIndex) const { cannam@132: if (ptrIndex < pointerCount) { cannam@132: // Hacky because WirePointer is defined in the .c++ file (so is incomplete here). cannam@132: return PointerReader(segment, capTable, reinterpret_cast( cannam@132: reinterpret_cast(pointers) + ptrIndex * WORDS_PER_POINTER), nestingLimit); cannam@132: } else{ cannam@132: return PointerReader(); cannam@132: } cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: inline ElementCount ListBuilder::size() const { return elementCount; } cannam@132: cannam@132: template cannam@132: inline T ListBuilder::getDataElement(ElementCount index) { cannam@132: return reinterpret_cast*>(ptr + index * step / BITS_PER_BYTE)->get(); cannam@132: cannam@132: // TODO(perf): Benchmark this alternate implementation, which I suspect may make better use of cannam@132: // the x86 SIB byte. Also use it for all the other getData/setData implementations below, and cannam@132: // the various non-inline methods that look up pointers. cannam@132: // Also if using this, consider changing ptr back to void* instead of byte*. cannam@132: // return reinterpret_cast*>(ptr)[ cannam@132: // index / ELEMENTS * (step / capnp::bitsPerElement())].get(); cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline bool ListBuilder::getDataElement(ElementCount index) { cannam@132: // Ignore step for bit lists because bit lists cannot be upgraded to struct lists. cannam@132: BitCount bindex = index * (1 * BITS / ELEMENTS); cannam@132: byte* b = ptr + bindex / BITS_PER_BYTE; cannam@132: return (*reinterpret_cast(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline Void ListBuilder::getDataElement(ElementCount index) { cannam@132: return VOID; cannam@132: } cannam@132: cannam@132: template cannam@132: inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer value) { cannam@132: reinterpret_cast*>(ptr + index * step / BITS_PER_BYTE)->set(value); cannam@132: } cannam@132: cannam@132: #if CAPNP_CANONICALIZE_NAN cannam@132: // Use mask() on floats and doubles to make sure we canonicalize NaNs. cannam@132: template <> cannam@132: inline void ListBuilder::setDataElement(ElementCount index, float value) { cannam@132: setDataElement(index, mask(value, 0)); cannam@132: } cannam@132: template <> cannam@132: inline void ListBuilder::setDataElement(ElementCount index, double value) { cannam@132: setDataElement(index, mask(value, 0)); cannam@132: } cannam@132: #endif cannam@132: cannam@132: template <> cannam@132: inline void ListBuilder::setDataElement(ElementCount index, bool value) { cannam@132: // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists. cannam@132: BitCount bindex = index * (1 * BITS / ELEMENTS); cannam@132: byte* b = ptr + bindex / BITS_PER_BYTE; cannam@132: uint bitnum = bindex % BITS_PER_BYTE / BITS; cannam@132: *reinterpret_cast(b) = (*reinterpret_cast(b) & ~(1 << bitnum)) cannam@132: | (static_cast(value) << bitnum); cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline void ListBuilder::setDataElement(ElementCount index, Void value) {} cannam@132: cannam@132: inline PointerBuilder ListBuilder::getPointerElement(ElementCount index) { cannam@132: return PointerBuilder(segment, capTable, cannam@132: reinterpret_cast(ptr + index * step / BITS_PER_BYTE)); cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: inline ElementCount ListReader::size() const { return elementCount; } cannam@132: cannam@132: template cannam@132: inline T ListReader::getDataElement(ElementCount index) const { cannam@132: return reinterpret_cast*>(ptr + index * step / BITS_PER_BYTE)->get(); cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline bool ListReader::getDataElement(ElementCount index) const { cannam@132: // Ignore step for bit lists because bit lists cannot be upgraded to struct lists. cannam@132: BitCount bindex = index * (1 * BITS / ELEMENTS); cannam@132: const byte* b = ptr + bindex / BITS_PER_BYTE; cannam@132: return (*reinterpret_cast(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0; cannam@132: } cannam@132: cannam@132: template <> cannam@132: inline Void ListReader::getDataElement(ElementCount index) const { cannam@132: return VOID; cannam@132: } cannam@132: cannam@132: inline PointerReader ListReader::getPointerElement(ElementCount index) const { cannam@132: return PointerReader(segment, capTable, cannam@132: reinterpret_cast(ptr + index * step / BITS_PER_BYTE), nestingLimit); cannam@132: } cannam@132: cannam@132: // ------------------------------------------------------------------- cannam@132: cannam@132: inline OrphanBuilder::OrphanBuilder(OrphanBuilder&& other) noexcept cannam@132: : segment(other.segment), capTable(other.capTable), location(other.location) { cannam@132: memcpy(&tag, &other.tag, sizeof(tag)); // Needs memcpy to comply with aliasing rules. cannam@132: other.segment = nullptr; cannam@132: other.location = nullptr; cannam@132: } cannam@132: cannam@132: inline OrphanBuilder::~OrphanBuilder() noexcept(false) { cannam@132: if (segment != nullptr) euthanize(); cannam@132: } cannam@132: cannam@132: inline OrphanBuilder& OrphanBuilder::operator=(OrphanBuilder&& other) { cannam@132: // With normal smart pointers, it's important to handle the case where the incoming pointer cannam@132: // is actually transitively owned by this one. In this case, euthanize() would destroy `other` cannam@132: // before we copied it. This isn't possible in the case of `OrphanBuilder` because it only cannam@132: // owns message objects, and `other` is not itself a message object, therefore cannot possibly cannam@132: // be transitively owned by `this`. cannam@132: cannam@132: if (segment != nullptr) euthanize(); cannam@132: segment = other.segment; cannam@132: capTable = other.capTable; cannam@132: location = other.location; cannam@132: memcpy(&tag, &other.tag, sizeof(tag)); // Needs memcpy to comply with aliasing rules. cannam@132: other.segment = nullptr; cannam@132: other.location = nullptr; cannam@132: return *this; cannam@132: } cannam@132: cannam@132: } // namespace _ (private) cannam@132: } // namespace capnp cannam@132: cannam@132: #endif // CAPNP_LAYOUT_H_