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