cannam@147
|
1 // Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors
|
cannam@147
|
2 // Licensed under the MIT License:
|
cannam@147
|
3 //
|
cannam@147
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@147
|
5 // of this software and associated documentation files (the "Software"), to deal
|
cannam@147
|
6 // in the Software without restriction, including without limitation the rights
|
cannam@147
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@147
|
8 // copies of the Software, and to permit persons to whom the Software is
|
cannam@147
|
9 // furnished to do so, subject to the following conditions:
|
cannam@147
|
10 //
|
cannam@147
|
11 // The above copyright notice and this permission notice shall be included in
|
cannam@147
|
12 // all copies or substantial portions of the Software.
|
cannam@147
|
13 //
|
cannam@147
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@147
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@147
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@147
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@147
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@147
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@147
|
20 // THE SOFTWARE.
|
cannam@147
|
21
|
cannam@147
|
22 #include <kj/common.h>
|
cannam@147
|
23 #include <kj/memory.h>
|
cannam@147
|
24 #include <kj/mutex.h>
|
cannam@147
|
25 #include <kj/debug.h>
|
cannam@147
|
26 #include "common.h"
|
cannam@147
|
27 #include "layout.h"
|
cannam@147
|
28 #include "any.h"
|
cannam@147
|
29
|
cannam@147
|
30 #ifndef CAPNP_MESSAGE_H_
|
cannam@147
|
31 #define CAPNP_MESSAGE_H_
|
cannam@147
|
32
|
cannam@147
|
33 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
|
cannam@147
|
34 #pragma GCC system_header
|
cannam@147
|
35 #endif
|
cannam@147
|
36
|
cannam@147
|
37 namespace capnp {
|
cannam@147
|
38
|
cannam@147
|
39 namespace _ { // private
|
cannam@147
|
40 class ReaderArena;
|
cannam@147
|
41 class BuilderArena;
|
cannam@147
|
42 }
|
cannam@147
|
43
|
cannam@147
|
44 class StructSchema;
|
cannam@147
|
45 class Orphanage;
|
cannam@147
|
46 template <typename T>
|
cannam@147
|
47 class Orphan;
|
cannam@147
|
48
|
cannam@147
|
49 // =======================================================================================
|
cannam@147
|
50
|
cannam@147
|
51 struct ReaderOptions {
|
cannam@147
|
52 // Options controlling how data is read.
|
cannam@147
|
53
|
cannam@147
|
54 uint64_t traversalLimitInWords = 8 * 1024 * 1024;
|
cannam@147
|
55 // Limits how many total words of data are allowed to be traversed. Traversal is counted when
|
cannam@147
|
56 // a new struct or list builder is obtained, e.g. from a get() accessor. This means that calling
|
cannam@147
|
57 // the getter for the same sub-struct multiple times will cause it to be double-counted. Once
|
cannam@147
|
58 // the traversal limit is reached, an error will be reported.
|
cannam@147
|
59 //
|
cannam@147
|
60 // This limit exists for security reasons. It is possible for an attacker to construct a message
|
cannam@147
|
61 // in which multiple pointers point at the same location. This is technically invalid, but hard
|
cannam@147
|
62 // to detect. Using such a message, an attacker could cause a message which is small on the wire
|
cannam@147
|
63 // to appear much larger when actually traversed, possibly exhausting server resources leading to
|
cannam@147
|
64 // denial-of-service.
|
cannam@147
|
65 //
|
cannam@147
|
66 // It makes sense to set a traversal limit that is much larger than the underlying message.
|
cannam@147
|
67 // Together with sensible coding practices (e.g. trying to avoid calling sub-object getters
|
cannam@147
|
68 // multiple times, which is expensive anyway), this should provide adequate protection without
|
cannam@147
|
69 // inconvenience.
|
cannam@147
|
70 //
|
cannam@147
|
71 // The default limit is 64 MiB. This may or may not be a sensible number for any given use case,
|
cannam@147
|
72 // but probably at least prevents easy exploitation while also avoiding causing problems in most
|
cannam@147
|
73 // typical cases.
|
cannam@147
|
74
|
cannam@147
|
75 int nestingLimit = 64;
|
cannam@147
|
76 // Limits how deeply-nested a message structure can be, e.g. structs containing other structs or
|
cannam@147
|
77 // lists of structs.
|
cannam@147
|
78 //
|
cannam@147
|
79 // Like the traversal limit, this limit exists for security reasons. Since it is common to use
|
cannam@147
|
80 // recursive code to traverse recursive data structures, an attacker could easily cause a stack
|
cannam@147
|
81 // overflow by sending a very-deeply-nested (or even cyclic) message, without the message even
|
cannam@147
|
82 // being very large. The default limit of 64 is probably low enough to prevent any chance of
|
cannam@147
|
83 // stack overflow, yet high enough that it is never a problem in practice.
|
cannam@147
|
84 };
|
cannam@147
|
85
|
cannam@147
|
86 class MessageReader {
|
cannam@147
|
87 // Abstract interface for an object used to read a Cap'n Proto message. Subclasses of
|
cannam@147
|
88 // MessageReader are responsible for reading the raw, flat message content. Callers should
|
cannam@147
|
89 // usually call `messageReader.getRoot<MyStructType>()` to get a `MyStructType::Reader`
|
cannam@147
|
90 // representing the root of the message, then use that to traverse the message content.
|
cannam@147
|
91 //
|
cannam@147
|
92 // Some common subclasses of `MessageReader` include `SegmentArrayMessageReader`, whose
|
cannam@147
|
93 // constructor accepts pointers to the raw data, and `StreamFdMessageReader` (from
|
cannam@147
|
94 // `serialize.h`), which reads the message from a file descriptor. One might implement other
|
cannam@147
|
95 // subclasses to handle things like reading from shared memory segments, mmap()ed files, etc.
|
cannam@147
|
96
|
cannam@147
|
97 public:
|
cannam@147
|
98 MessageReader(ReaderOptions options);
|
cannam@147
|
99 // It is suggested that subclasses take ReaderOptions as a constructor parameter, but give it a
|
cannam@147
|
100 // default value of "ReaderOptions()". The base class constructor doesn't have a default value
|
cannam@147
|
101 // in order to remind subclasses that they really need to give the user a way to provide this.
|
cannam@147
|
102
|
cannam@147
|
103 virtual ~MessageReader() noexcept(false);
|
cannam@147
|
104
|
cannam@147
|
105 virtual kj::ArrayPtr<const word> getSegment(uint id) = 0;
|
cannam@147
|
106 // Gets the segment with the given ID, or returns null if no such segment exists. This method
|
cannam@147
|
107 // will be called at most once for each segment ID.
|
cannam@147
|
108
|
cannam@147
|
109 inline const ReaderOptions& getOptions();
|
cannam@147
|
110 // Get the options passed to the constructor.
|
cannam@147
|
111
|
cannam@147
|
112 template <typename RootType>
|
cannam@147
|
113 typename RootType::Reader getRoot();
|
cannam@147
|
114 // Get the root struct of the message, interpreting it as the given struct type.
|
cannam@147
|
115
|
cannam@147
|
116 template <typename RootType, typename SchemaType>
|
cannam@147
|
117 typename RootType::Reader getRoot(SchemaType schema);
|
cannam@147
|
118 // Dynamically interpret the root struct of the message using the given schema (a StructSchema).
|
cannam@147
|
119 // RootType in this case must be DynamicStruct, and you must #include <capnp/dynamic.h> to
|
cannam@147
|
120 // use this.
|
cannam@147
|
121
|
cannam@147
|
122 bool isCanonical();
|
cannam@147
|
123 // Returns whether the message encoded in the reader is in canonical form.
|
cannam@147
|
124
|
cannam@147
|
125 private:
|
cannam@147
|
126 ReaderOptions options;
|
cannam@147
|
127
|
cannam@147
|
128 // Space in which we can construct a ReaderArena. We don't use ReaderArena directly here
|
cannam@147
|
129 // because we don't want clients to have to #include arena.h, which itself includes a bunch of
|
cannam@147
|
130 // big STL headers. We don't use a pointer to a ReaderArena because that would require an
|
cannam@147
|
131 // extra malloc on every message which could be expensive when processing small messages.
|
cannam@147
|
132 void* arenaSpace[15 + sizeof(kj::MutexGuarded<void*>) / sizeof(void*)];
|
cannam@147
|
133 bool allocatedArena;
|
cannam@147
|
134
|
cannam@147
|
135 _::ReaderArena* arena() { return reinterpret_cast<_::ReaderArena*>(arenaSpace); }
|
cannam@147
|
136 AnyPointer::Reader getRootInternal();
|
cannam@147
|
137 };
|
cannam@147
|
138
|
cannam@147
|
139 class MessageBuilder {
|
cannam@147
|
140 // Abstract interface for an object used to allocate and build a message. Subclasses of
|
cannam@147
|
141 // MessageBuilder are responsible for allocating the space in which the message will be written.
|
cannam@147
|
142 // The most common subclass is `MallocMessageBuilder`, but other subclasses may be used to do
|
cannam@147
|
143 // tricky things like allocate messages in shared memory or mmap()ed files.
|
cannam@147
|
144 //
|
cannam@147
|
145 // Creating a new message ususually means allocating a new MessageBuilder (ideally on the stack)
|
cannam@147
|
146 // and then calling `messageBuilder.initRoot<MyStructType>()` to get a `MyStructType::Builder`.
|
cannam@147
|
147 // That, in turn, can be used to fill in the message content. When done, you can call
|
cannam@147
|
148 // `messageBuilder.getSegmentsForOutput()` to get a list of flat data arrays containing the
|
cannam@147
|
149 // message.
|
cannam@147
|
150
|
cannam@147
|
151 public:
|
cannam@147
|
152 MessageBuilder();
|
cannam@147
|
153 virtual ~MessageBuilder() noexcept(false);
|
cannam@147
|
154 KJ_DISALLOW_COPY(MessageBuilder);
|
cannam@147
|
155
|
cannam@147
|
156 struct SegmentInit {
|
cannam@147
|
157 kj::ArrayPtr<word> space;
|
cannam@147
|
158
|
cannam@147
|
159 size_t wordsUsed;
|
cannam@147
|
160 // Number of words in `space` which are used; the rest are free space in which additional
|
cannam@147
|
161 // objects may be allocated.
|
cannam@147
|
162 };
|
cannam@147
|
163
|
cannam@147
|
164 explicit MessageBuilder(kj::ArrayPtr<SegmentInit> segments);
|
cannam@147
|
165 // Create a MessageBuilder backed by existing memory. This is an advanced interface that most
|
cannam@147
|
166 // people should not use. THIS METHOD IS INSECURE; see below.
|
cannam@147
|
167 //
|
cannam@147
|
168 // This allows a MessageBuilder to be constructed to modify an in-memory message without first
|
cannam@147
|
169 // making a copy of the content. This is especially useful in conjunction with mmap().
|
cannam@147
|
170 //
|
cannam@147
|
171 // The contents of each segment must outlive the MessageBuilder, but the SegmentInit array itself
|
cannam@147
|
172 // only need outlive the constructor.
|
cannam@147
|
173 //
|
cannam@147
|
174 // SECURITY: Do not use this in conjunction with untrusted data. This constructor assumes that
|
cannam@147
|
175 // the input message is valid. This constructor is designed to be used with data you control,
|
cannam@147
|
176 // e.g. an mmap'd file which is owned and accessed by only one program. When reading data you
|
cannam@147
|
177 // do not trust, you *must* load it into a Reader and then copy into a Builder as a means of
|
cannam@147
|
178 // validating the content.
|
cannam@147
|
179 //
|
cannam@147
|
180 // WARNING: It is NOT safe to initialize a MessageBuilder in this way from memory that is
|
cannam@147
|
181 // currently in use by another MessageBuilder or MessageReader. Other readers/builders will
|
cannam@147
|
182 // not observe changes to the segment sizes nor newly-allocated segments caused by allocating
|
cannam@147
|
183 // new objects in this message.
|
cannam@147
|
184
|
cannam@147
|
185 virtual kj::ArrayPtr<word> allocateSegment(uint minimumSize) = 0;
|
cannam@147
|
186 // Allocates an array of at least the given number of words, throwing an exception or crashing if
|
cannam@147
|
187 // this is not possible. It is expected that this method will usually return more space than
|
cannam@147
|
188 // requested, and the caller should use that extra space as much as possible before allocating
|
cannam@147
|
189 // more. The returned space remains valid at least until the MessageBuilder is destroyed.
|
cannam@147
|
190 //
|
cannam@147
|
191 // Cap'n Proto will only call this once at a time, so the subclass need not worry about
|
cannam@147
|
192 // thread-safety.
|
cannam@147
|
193
|
cannam@147
|
194 template <typename RootType>
|
cannam@147
|
195 typename RootType::Builder initRoot();
|
cannam@147
|
196 // Initialize the root struct of the message as the given struct type.
|
cannam@147
|
197
|
cannam@147
|
198 template <typename Reader>
|
cannam@147
|
199 void setRoot(Reader&& value);
|
cannam@147
|
200 // Set the root struct to a deep copy of the given struct.
|
cannam@147
|
201
|
cannam@147
|
202 template <typename RootType>
|
cannam@147
|
203 typename RootType::Builder getRoot();
|
cannam@147
|
204 // Get the root struct of the message, interpreting it as the given struct type.
|
cannam@147
|
205
|
cannam@147
|
206 template <typename RootType, typename SchemaType>
|
cannam@147
|
207 typename RootType::Builder getRoot(SchemaType schema);
|
cannam@147
|
208 // Dynamically interpret the root struct of the message using the given schema (a StructSchema).
|
cannam@147
|
209 // RootType in this case must be DynamicStruct, and you must #include <capnp/dynamic.h> to
|
cannam@147
|
210 // use this.
|
cannam@147
|
211
|
cannam@147
|
212 template <typename RootType, typename SchemaType>
|
cannam@147
|
213 typename RootType::Builder initRoot(SchemaType schema);
|
cannam@147
|
214 // Dynamically init the root struct of the message using the given schema (a StructSchema).
|
cannam@147
|
215 // RootType in this case must be DynamicStruct, and you must #include <capnp/dynamic.h> to
|
cannam@147
|
216 // use this.
|
cannam@147
|
217
|
cannam@147
|
218 template <typename T>
|
cannam@147
|
219 void adoptRoot(Orphan<T>&& orphan);
|
cannam@147
|
220 // Like setRoot() but adopts the orphan without copying.
|
cannam@147
|
221
|
cannam@147
|
222 kj::ArrayPtr<const kj::ArrayPtr<const word>> getSegmentsForOutput();
|
cannam@147
|
223 // Get the raw data that makes up the message.
|
cannam@147
|
224
|
cannam@147
|
225 Orphanage getOrphanage();
|
cannam@147
|
226
|
cannam@147
|
227 bool isCanonical();
|
cannam@147
|
228 // Check whether the message builder is in canonical form
|
cannam@147
|
229
|
cannam@147
|
230 private:
|
cannam@147
|
231 void* arenaSpace[22];
|
cannam@147
|
232 // Space in which we can construct a BuilderArena. We don't use BuilderArena directly here
|
cannam@147
|
233 // because we don't want clients to have to #include arena.h, which itself includes a bunch of
|
cannam@147
|
234 // big STL headers. We don't use a pointer to a BuilderArena because that would require an
|
cannam@147
|
235 // extra malloc on every message which could be expensive when processing small messages.
|
cannam@147
|
236
|
cannam@147
|
237 bool allocatedArena = false;
|
cannam@147
|
238 // We have to initialize the arena lazily because when we do so we want to allocate the root
|
cannam@147
|
239 // pointer immediately, and this will allocate a segment, which requires a virtual function
|
cannam@147
|
240 // call on the MessageBuilder. We can't do such a call in the constructor since the subclass
|
cannam@147
|
241 // isn't constructed yet. This is kind of annoying because it means that getOrphanage() is
|
cannam@147
|
242 // not thread-safe, but that shouldn't be a huge deal...
|
cannam@147
|
243
|
cannam@147
|
244 _::BuilderArena* arena() { return reinterpret_cast<_::BuilderArena*>(arenaSpace); }
|
cannam@147
|
245 _::SegmentBuilder* getRootSegment();
|
cannam@147
|
246 AnyPointer::Builder getRootInternal();
|
cannam@147
|
247 };
|
cannam@147
|
248
|
cannam@147
|
249 template <typename RootType>
|
cannam@147
|
250 typename RootType::Reader readMessageUnchecked(const word* data);
|
cannam@147
|
251 // IF THE INPUT IS INVALID, THIS MAY CRASH, CORRUPT MEMORY, CREATE A SECURITY HOLE IN YOUR APP,
|
cannam@147
|
252 // MURDER YOUR FIRST-BORN CHILD, AND/OR BRING ABOUT ETERNAL DAMNATION ON ALL OF HUMANITY. DO NOT
|
cannam@147
|
253 // USE UNLESS YOU UNDERSTAND THE CONSEQUENCES.
|
cannam@147
|
254 //
|
cannam@147
|
255 // Given a pointer to a known-valid message located in a single contiguous memory segment,
|
cannam@147
|
256 // returns a reader for that message. No bounds-checking will be done while traversing this
|
cannam@147
|
257 // message. Use this only if you have already verified that all pointers are valid and in-bounds,
|
cannam@147
|
258 // and there are no far pointers in the message.
|
cannam@147
|
259 //
|
cannam@147
|
260 // To create a message that can be passed to this function, build a message using a MallocAllocator
|
cannam@147
|
261 // whose preferred segment size is larger than the message size. This guarantees that the message
|
cannam@147
|
262 // will be allocated as a single segment, meaning getSegmentsForOutput() returns a single word
|
cannam@147
|
263 // array. That word array is your message; you may pass a pointer to its first word into
|
cannam@147
|
264 // readMessageUnchecked() to read the message.
|
cannam@147
|
265 //
|
cannam@147
|
266 // This can be particularly handy for embedding messages in generated code: you can
|
cannam@147
|
267 // embed the raw bytes (using AlignedData) then make a Reader for it using this. This is the way
|
cannam@147
|
268 // default values are embedded in code generated by the Cap'n Proto compiler. E.g., if you have
|
cannam@147
|
269 // a message MyMessage, you can read its default value like so:
|
cannam@147
|
270 // MyMessage::Reader reader = Message<MyMessage>::readMessageUnchecked(MyMessage::DEFAULT.words);
|
cannam@147
|
271 //
|
cannam@147
|
272 // To sanitize a message from an untrusted source such that it can be safely passed to
|
cannam@147
|
273 // readMessageUnchecked(), use copyToUnchecked().
|
cannam@147
|
274
|
cannam@147
|
275 template <typename Reader>
|
cannam@147
|
276 void copyToUnchecked(Reader&& reader, kj::ArrayPtr<word> uncheckedBuffer);
|
cannam@147
|
277 // Copy the content of the given reader into the given buffer, such that it can safely be passed to
|
cannam@147
|
278 // readMessageUnchecked(). The buffer's size must be exactly reader.totalSizeInWords() + 1,
|
cannam@147
|
279 // otherwise an exception will be thrown. The buffer must be zero'd before calling.
|
cannam@147
|
280
|
cannam@147
|
281 template <typename RootType>
|
cannam@147
|
282 typename RootType::Reader readDataStruct(kj::ArrayPtr<const word> data);
|
cannam@147
|
283 // Interprets the given data as a single, data-only struct. Only primitive fields (booleans,
|
cannam@147
|
284 // numbers, and enums) will be readable; all pointers will be null. This is useful if you want
|
cannam@147
|
285 // to use Cap'n Proto as a language/platform-neutral way to pack some bits.
|
cannam@147
|
286 //
|
cannam@147
|
287 // The input is a word array rather than a byte array to enforce alignment. If you have a byte
|
cannam@147
|
288 // array which you know is word-aligned (or if your platform supports unaligned reads and you don't
|
cannam@147
|
289 // mind the performance penalty), then you can use `reinterpret_cast` to convert a byte array into
|
cannam@147
|
290 // a word array:
|
cannam@147
|
291 //
|
cannam@147
|
292 // kj::arrayPtr(reinterpret_cast<const word*>(bytes.begin()),
|
cannam@147
|
293 // reinterpret_cast<const word*>(bytes.end()))
|
cannam@147
|
294
|
cannam@147
|
295 template <typename BuilderType>
|
cannam@147
|
296 typename kj::ArrayPtr<const word> writeDataStruct(BuilderType builder);
|
cannam@147
|
297 // Given a struct builder, get the underlying data section as a word array, suitable for passing
|
cannam@147
|
298 // to `readDataStruct()`.
|
cannam@147
|
299 //
|
cannam@147
|
300 // Note that you may call `.toBytes()` on the returned value to convert to `ArrayPtr<const byte>`.
|
cannam@147
|
301
|
cannam@147
|
302 template <typename Type>
|
cannam@147
|
303 static typename Type::Reader defaultValue();
|
cannam@147
|
304 // Get a default instance of the given struct or list type.
|
cannam@147
|
305 //
|
cannam@147
|
306 // TODO(cleanup): Find a better home for this function?
|
cannam@147
|
307
|
cannam@147
|
308 // =======================================================================================
|
cannam@147
|
309
|
cannam@147
|
310 class SegmentArrayMessageReader: public MessageReader {
|
cannam@147
|
311 // A simple MessageReader that reads from an array of word arrays representing all segments.
|
cannam@147
|
312 // In particular you can read directly from the output of MessageBuilder::getSegmentsForOutput()
|
cannam@147
|
313 // (although it would probably make more sense to call builder.getRoot().asReader() in that case).
|
cannam@147
|
314
|
cannam@147
|
315 public:
|
cannam@147
|
316 SegmentArrayMessageReader(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments,
|
cannam@147
|
317 ReaderOptions options = ReaderOptions());
|
cannam@147
|
318 // Creates a message pointing at the given segment array, without taking ownership of the
|
cannam@147
|
319 // segments. All arrays passed in must remain valid until the MessageReader is destroyed.
|
cannam@147
|
320
|
cannam@147
|
321 KJ_DISALLOW_COPY(SegmentArrayMessageReader);
|
cannam@147
|
322 ~SegmentArrayMessageReader() noexcept(false);
|
cannam@147
|
323
|
cannam@147
|
324 virtual kj::ArrayPtr<const word> getSegment(uint id) override;
|
cannam@147
|
325
|
cannam@147
|
326 private:
|
cannam@147
|
327 kj::ArrayPtr<const kj::ArrayPtr<const word>> segments;
|
cannam@147
|
328 };
|
cannam@147
|
329
|
cannam@147
|
330 enum class AllocationStrategy: uint8_t {
|
cannam@147
|
331 FIXED_SIZE,
|
cannam@147
|
332 // The builder will prefer to allocate the same amount of space for each segment with no
|
cannam@147
|
333 // heuristic growth. It will still allocate larger segments when the preferred size is too small
|
cannam@147
|
334 // for some single object. This mode is generally not recommended, but can be particularly useful
|
cannam@147
|
335 // for testing in order to force a message to allocate a predictable number of segments. Note
|
cannam@147
|
336 // that you can force every single object in the message to be located in a separate segment by
|
cannam@147
|
337 // using this mode with firstSegmentWords = 0.
|
cannam@147
|
338
|
cannam@147
|
339 GROW_HEURISTICALLY
|
cannam@147
|
340 // The builder will heuristically decide how much space to allocate for each segment. Each
|
cannam@147
|
341 // allocated segment will be progressively larger than the previous segments on the assumption
|
cannam@147
|
342 // that message sizes are exponentially distributed. The total number of segments that will be
|
cannam@147
|
343 // allocated for a message of size n is O(log n).
|
cannam@147
|
344 };
|
cannam@147
|
345
|
cannam@147
|
346 constexpr uint SUGGESTED_FIRST_SEGMENT_WORDS = 1024;
|
cannam@147
|
347 constexpr AllocationStrategy SUGGESTED_ALLOCATION_STRATEGY = AllocationStrategy::GROW_HEURISTICALLY;
|
cannam@147
|
348
|
cannam@147
|
349 class MallocMessageBuilder: public MessageBuilder {
|
cannam@147
|
350 // A simple MessageBuilder that uses malloc() (actually, calloc()) to allocate segments. This
|
cannam@147
|
351 // implementation should be reasonable for any case that doesn't require writing the message to
|
cannam@147
|
352 // a specific location in memory.
|
cannam@147
|
353
|
cannam@147
|
354 public:
|
cannam@147
|
355 explicit MallocMessageBuilder(uint firstSegmentWords = SUGGESTED_FIRST_SEGMENT_WORDS,
|
cannam@147
|
356 AllocationStrategy allocationStrategy = SUGGESTED_ALLOCATION_STRATEGY);
|
cannam@147
|
357 // Creates a BuilderContext which allocates at least the given number of words for the first
|
cannam@147
|
358 // segment, and then uses the given strategy to decide how much to allocate for subsequent
|
cannam@147
|
359 // segments. When choosing a value for firstSegmentWords, consider that:
|
cannam@147
|
360 // 1) Reading and writing messages gets slower when multiple segments are involved, so it's good
|
cannam@147
|
361 // if most messages fit in a single segment.
|
cannam@147
|
362 // 2) Unused bytes will not be written to the wire, so generally it is not a big deal to allocate
|
cannam@147
|
363 // more space than you need. It only becomes problematic if you are allocating many messages
|
cannam@147
|
364 // in parallel and thus use lots of memory, or if you allocate so much extra space that just
|
cannam@147
|
365 // zeroing it out becomes a bottleneck.
|
cannam@147
|
366 // The defaults have been chosen to be reasonable for most people, so don't change them unless you
|
cannam@147
|
367 // have reason to believe you need to.
|
cannam@147
|
368
|
cannam@147
|
369 explicit MallocMessageBuilder(kj::ArrayPtr<word> firstSegment,
|
cannam@147
|
370 AllocationStrategy allocationStrategy = SUGGESTED_ALLOCATION_STRATEGY);
|
cannam@147
|
371 // This version always returns the given array for the first segment, and then proceeds with the
|
cannam@147
|
372 // allocation strategy. This is useful for optimization when building lots of small messages in
|
cannam@147
|
373 // a tight loop: you can reuse the space for the first segment.
|
cannam@147
|
374 //
|
cannam@147
|
375 // firstSegment MUST be zero-initialized. MallocMessageBuilder's destructor will write new zeros
|
cannam@147
|
376 // over any space that was used so that it can be reused.
|
cannam@147
|
377
|
cannam@147
|
378 KJ_DISALLOW_COPY(MallocMessageBuilder);
|
cannam@147
|
379 virtual ~MallocMessageBuilder() noexcept(false);
|
cannam@147
|
380
|
cannam@147
|
381 virtual kj::ArrayPtr<word> allocateSegment(uint minimumSize) override;
|
cannam@147
|
382
|
cannam@147
|
383 private:
|
cannam@147
|
384 uint nextSize;
|
cannam@147
|
385 AllocationStrategy allocationStrategy;
|
cannam@147
|
386
|
cannam@147
|
387 bool ownFirstSegment;
|
cannam@147
|
388 bool returnedFirstSegment;
|
cannam@147
|
389
|
cannam@147
|
390 void* firstSegment;
|
cannam@147
|
391
|
cannam@147
|
392 struct MoreSegments;
|
cannam@147
|
393 kj::Maybe<kj::Own<MoreSegments>> moreSegments;
|
cannam@147
|
394 };
|
cannam@147
|
395
|
cannam@147
|
396 class FlatMessageBuilder: public MessageBuilder {
|
cannam@147
|
397 // THIS IS NOT THE CLASS YOU'RE LOOKING FOR.
|
cannam@147
|
398 //
|
cannam@147
|
399 // If you want to write a message into already-existing scratch space, use `MallocMessageBuilder`
|
cannam@147
|
400 // and pass the scratch space to its constructor. It will then only fall back to malloc() if
|
cannam@147
|
401 // the scratch space is not large enough.
|
cannam@147
|
402 //
|
cannam@147
|
403 // Do NOT use this class unless you really know what you're doing. This class is problematic
|
cannam@147
|
404 // because it requires advance knowledge of the size of your message, which is usually impossible
|
cannam@147
|
405 // to determine without actually building the message. The class was created primarily to
|
cannam@147
|
406 // implement `copyToUnchecked()`, which itself exists only to support other internal parts of
|
cannam@147
|
407 // the Cap'n Proto implementation.
|
cannam@147
|
408
|
cannam@147
|
409 public:
|
cannam@147
|
410 explicit FlatMessageBuilder(kj::ArrayPtr<word> array);
|
cannam@147
|
411 KJ_DISALLOW_COPY(FlatMessageBuilder);
|
cannam@147
|
412 virtual ~FlatMessageBuilder() noexcept(false);
|
cannam@147
|
413
|
cannam@147
|
414 void requireFilled();
|
cannam@147
|
415 // Throws an exception if the flat array is not exactly full.
|
cannam@147
|
416
|
cannam@147
|
417 virtual kj::ArrayPtr<word> allocateSegment(uint minimumSize) override;
|
cannam@147
|
418
|
cannam@147
|
419 private:
|
cannam@147
|
420 kj::ArrayPtr<word> array;
|
cannam@147
|
421 bool allocated;
|
cannam@147
|
422 };
|
cannam@147
|
423
|
cannam@147
|
424 // =======================================================================================
|
cannam@147
|
425 // implementation details
|
cannam@147
|
426
|
cannam@147
|
427 inline const ReaderOptions& MessageReader::getOptions() {
|
cannam@147
|
428 return options;
|
cannam@147
|
429 }
|
cannam@147
|
430
|
cannam@147
|
431 template <typename RootType>
|
cannam@147
|
432 inline typename RootType::Reader MessageReader::getRoot() {
|
cannam@147
|
433 return getRootInternal().getAs<RootType>();
|
cannam@147
|
434 }
|
cannam@147
|
435
|
cannam@147
|
436 template <typename RootType>
|
cannam@147
|
437 inline typename RootType::Builder MessageBuilder::initRoot() {
|
cannam@147
|
438 return getRootInternal().initAs<RootType>();
|
cannam@147
|
439 }
|
cannam@147
|
440
|
cannam@147
|
441 template <typename Reader>
|
cannam@147
|
442 inline void MessageBuilder::setRoot(Reader&& value) {
|
cannam@147
|
443 getRootInternal().setAs<FromReader<Reader>>(value);
|
cannam@147
|
444 }
|
cannam@147
|
445
|
cannam@147
|
446 template <typename RootType>
|
cannam@147
|
447 inline typename RootType::Builder MessageBuilder::getRoot() {
|
cannam@147
|
448 return getRootInternal().getAs<RootType>();
|
cannam@147
|
449 }
|
cannam@147
|
450
|
cannam@147
|
451 template <typename T>
|
cannam@147
|
452 void MessageBuilder::adoptRoot(Orphan<T>&& orphan) {
|
cannam@147
|
453 return getRootInternal().adopt(kj::mv(orphan));
|
cannam@147
|
454 }
|
cannam@147
|
455
|
cannam@147
|
456 template <typename RootType, typename SchemaType>
|
cannam@147
|
457 typename RootType::Reader MessageReader::getRoot(SchemaType schema) {
|
cannam@147
|
458 return getRootInternal().getAs<RootType>(schema);
|
cannam@147
|
459 }
|
cannam@147
|
460
|
cannam@147
|
461 template <typename RootType, typename SchemaType>
|
cannam@147
|
462 typename RootType::Builder MessageBuilder::getRoot(SchemaType schema) {
|
cannam@147
|
463 return getRootInternal().getAs<RootType>(schema);
|
cannam@147
|
464 }
|
cannam@147
|
465
|
cannam@147
|
466 template <typename RootType, typename SchemaType>
|
cannam@147
|
467 typename RootType::Builder MessageBuilder::initRoot(SchemaType schema) {
|
cannam@147
|
468 return getRootInternal().initAs<RootType>(schema);
|
cannam@147
|
469 }
|
cannam@147
|
470
|
cannam@147
|
471 template <typename RootType>
|
cannam@147
|
472 typename RootType::Reader readMessageUnchecked(const word* data) {
|
cannam@147
|
473 return AnyPointer::Reader(_::PointerReader::getRootUnchecked(data)).getAs<RootType>();
|
cannam@147
|
474 }
|
cannam@147
|
475
|
cannam@147
|
476 template <typename Reader>
|
cannam@147
|
477 void copyToUnchecked(Reader&& reader, kj::ArrayPtr<word> uncheckedBuffer) {
|
cannam@147
|
478 FlatMessageBuilder builder(uncheckedBuffer);
|
cannam@147
|
479 builder.setRoot(kj::fwd<Reader>(reader));
|
cannam@147
|
480 builder.requireFilled();
|
cannam@147
|
481 }
|
cannam@147
|
482
|
cannam@147
|
483 template <typename RootType>
|
cannam@147
|
484 typename RootType::Reader readDataStruct(kj::ArrayPtr<const word> data) {
|
cannam@147
|
485 return typename RootType::Reader(_::StructReader(data));
|
cannam@147
|
486 }
|
cannam@147
|
487
|
cannam@147
|
488 template <typename BuilderType>
|
cannam@147
|
489 typename kj::ArrayPtr<const word> writeDataStruct(BuilderType builder) {
|
cannam@147
|
490 auto bytes = _::PointerHelpers<FromBuilder<BuilderType>>::getInternalBuilder(kj::mv(builder))
|
cannam@147
|
491 .getDataSectionAsBlob();
|
cannam@147
|
492 return kj::arrayPtr(reinterpret_cast<word*>(bytes.begin()),
|
cannam@147
|
493 reinterpret_cast<word*>(bytes.end()));
|
cannam@147
|
494 }
|
cannam@147
|
495
|
cannam@147
|
496 template <typename Type>
|
cannam@147
|
497 static typename Type::Reader defaultValue() {
|
cannam@147
|
498 return typename Type::Reader(_::StructReader());
|
cannam@147
|
499 }
|
cannam@147
|
500
|
cannam@147
|
501 template <typename T>
|
cannam@147
|
502 kj::Array<word> canonicalize(T&& reader) {
|
cannam@147
|
503 return _::PointerHelpers<FromReader<T>>::getInternalReader(reader).canonicalize();
|
cannam@147
|
504 }
|
cannam@147
|
505
|
cannam@147
|
506 } // namespace capnp
|
cannam@147
|
507
|
cannam@147
|
508 #endif // CAPNP_MESSAGE_H_
|