annotate src/capnproto-0.6.0/doc/faq.md @ 71:388bd4da45bf

Opus build for Windows (MinGW)
author Chris Cannam
date Fri, 25 Jan 2019 13:49:03 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 ---
cannam@62 2 layout: page
cannam@62 3 title: FAQ
cannam@62 4 ---
cannam@62 5
cannam@62 6 # FAQ
cannam@62 7
cannam@62 8 ## Design
cannam@62 9
cannam@62 10 ### Isn't I/O bandwidth more important than CPU usage? Is Cap'n Proto barking up the wrong tree?
cannam@62 11
cannam@62 12 It depends. What is your use case?
cannam@62 13
cannam@62 14 Are you communicating between two processes on the same machine? If so, you have unlimited
cannam@62 15 bandwidth, and you should be entirely concerned with CPU.
cannam@62 16
cannam@62 17 Are you communicating between two machines within the same datacenter? If so, it's unlikely that
cannam@62 18 you will saturate your network connection before your CPU. Possible, but unlikely.
cannam@62 19
cannam@62 20 Are you communicating across the general internet? In that case, bandwidth is probably your main
cannam@62 21 concern. Luckily, Cap'n Proto lets you choose to enable "packing" in this case, achieving similar
cannam@62 22 encoding size to Protocol Buffers while still being faster. And you can always add extra
cannam@62 23 compression on top of that.
cannam@62 24
cannam@62 25 ### Have you considered building the RPC system on ZeroMQ?
cannam@62 26
cannam@62 27 ZeroMQ (and its successor, Nanomsg) is a powerful technology for distributed computing. Its
cannam@62 28 design focuses on scenarios involving lots of stateless, fault-tolerant worker processes
cannam@62 29 communicating via various patterns, such as request/response, produce/consume, and
cannam@62 30 publish/subscribe. For big data processing where armies of stateless nodes make sense, pairing
cannam@62 31 Cap'n Proto with ZeroMQ would be an excellent choice -- and this is easy to do today, as ZeroMQ
cannam@62 32 is entirely serialization-agnostic.
cannam@62 33
cannam@62 34 That said, Cap'n Proto RPC takes a very different approach. Cap'n Proto's model focuses on
cannam@62 35 stateful servers interacting in complex, object-oriented ways. The model is better suited to
cannam@62 36 tasks involving applications with many heterogeneous components and interactions between
cannam@62 37 mutually-distrusting parties. Requests and responses can go in any direction. Objects have
cannam@62 38 state and so two calls to the same object had best go to the same machine. Load balancing and
cannam@62 39 fault tolerance is pushed up the stack, because without a large pool of homogeneous work there's
cannam@62 40 just no way to make them transparent at a low level.
cannam@62 41
cannam@62 42 Put concretely, you might build a search engine indexing pipeline on ZeroMQ, but an online
cannam@62 43 interactive spreadsheet editor would be better built on Cap'n Proto RPC.
cannam@62 44
cannam@62 45 (Actually, a distributed programming framework providing similar features to ZeroMQ could itself be
cannam@62 46 built on top of Cap'n Proto RPC.)
cannam@62 47
cannam@62 48 ### Aren't messages that contain pointers a huge security problem?
cannam@62 49
cannam@62 50 Not at all. Cap'n Proto bounds-checks each pointer when it is read and throws an exception or
cannam@62 51 returns a safe dummy value (your choice) if the pointer is out-of-bounds.
cannam@62 52
cannam@62 53 ### So it's not that you've eliminated parsing, you've just moved it to happen lazily?
cannam@62 54
cannam@62 55 No. Compared to Protobuf decoding, the time spent validating pointers while traversing a Cap'n
cannam@62 56 Proto message is negligible.
cannam@62 57
cannam@62 58 ### I think I heard somewhere that capability-based security doesn't work?
cannam@62 59
cannam@62 60 This was a popular myth in security circles way back in the 80's and 90's, based on an incomplete
cannam@62 61 understanding of what capabilities are and how to use them effectively. Read
cannam@62 62 [Capability Myths Demolished](http://zesty.ca/capmyths/usenix.pdf). (No really, read it;
cannam@62 63 it's awesome.)
cannam@62 64
cannam@62 65 ## Usage
cannam@62 66
cannam@62 67 ### How do I make a field "required", like in Protocol Buffers?
cannam@62 68
cannam@62 69 You don't. You may find this surprising, but the "required" keyword in Protocol Buffers turned
cannam@62 70 out to be a horrible mistake.
cannam@62 71
cannam@62 72 For background, in protocol buffers, a field could be marked "required" to indicate that parsing
cannam@62 73 should fail if the sender forgot to set the field before sending the message. Required fields were
cannam@62 74 encoded exactly the same as optional ones; the only difference was the extra validation.
cannam@62 75
cannam@62 76 The problem with this is, validation is sometimes more subtle than that. Sometimes, different
cannam@62 77 applications -- or different parts of the same application, or different versions of the same
cannam@62 78 application -- place different requirements on the same protocol. An application may want to
cannam@62 79 pass around partially-complete messages internally. A particular field that used to be required
cannam@62 80 might become optional. A new use case might call for almost exactly the same message type, minus
cannam@62 81 one field, at which point it may make more sense to reuse the type than to define a new one.
cannam@62 82
cannam@62 83 A field declared required, unfortunately, is required everywhere. The validation is baked into
cannam@62 84 the parser, and there's nothing you can do about it. Nothing, that is, except change the field
cannam@62 85 from "required" to "optional". But that's where the _real_ problems start.
cannam@62 86
cannam@62 87 Imagine a production environment in which two servers, Alice and Bob, exchange messages through a
cannam@62 88 message bus infrastructure running on a big corporate network. The message bus parses each message
cannam@62 89 just to examine the envelope and decide how to route it, without paying attention to any other
cannam@62 90 content. Often, messages from various applications are batched together and then split up again
cannam@62 91 downstream.
cannam@62 92
cannam@62 93 Now, at some point, Alice's developers decide that one of the fields in a deeply-nested message
cannam@62 94 commonly sent to Bob has become obsolete. To clean things up, they decide to remove it, so they
cannam@62 95 change the field from "required" to "optional". The developers aren't idiots, so they realize that
cannam@62 96 Bob needs to be updated as well. They make the changes to Bob, and just to be thorough they
cannam@62 97 run an integration test with Alice and Bob running in a test environment. The test environment
cannam@62 98 is always running the latest build of the message bus, but that's irrelevant anyway because the
cannam@62 99 message bus doesn't actually care about message contents; it only does routing. Protocols are
cannam@62 100 modified all the time without updating the message bus.
cannam@62 101
cannam@62 102 Satisfied with their testing, the devs push a new version of Alice to prod. Immediately,
cannam@62 103 everything breaks. And by "everything" I don't just mean Alice and Bob. Completely unrelated
cannam@62 104 servers are getting strange errors or failing to receive messages. The whole data center has
cannam@62 105 ground to a halt and the sysadmins are running around with their hair on fire.
cannam@62 106
cannam@62 107 What happened? Well, the message bus running in prod was still an older build from before the
cannam@62 108 protocol change. And even though the message bus doesn't care about message content, it _does_
cannam@62 109 need to parse every message just to read the envelope. And the protobuf parser checks the _entire_
cannam@62 110 message for missing required fields. So when Alice stopped sending that newly-optional field, the
cannam@62 111 whole message failed to parse, envelope and all. And to make matters worse, any other messages
cannam@62 112 that happened to be in the same batch _also_ failed to parse, causing errors in seemingly-unrelated
cannam@62 113 systems that share the bus.
cannam@62 114
cannam@62 115 Things like this have actually happened. At Google. Many times.
cannam@62 116
cannam@62 117 The right answer is for applications to do validation as-needed in application-level code. If you
cannam@62 118 want to detect when a client fails to set a particular field, give the field an invalid default
cannam@62 119 value and then check for that value on the server. Low-level infrastructure that doesn't care
cannam@62 120 about message content should not validate it at all.
cannam@62 121
cannam@62 122 Oh, and also, Cap'n Proto doesn't have any parsing step during which to check for required
cannam@62 123 fields. :)
cannam@62 124
cannam@62 125 ### How do I make a field optional?
cannam@62 126
cannam@62 127 Cap'n Proto has no notion of "optional" fields.
cannam@62 128
cannam@62 129 A primitive field always takes space on the wire whether you set it or not (although default-valued
cannam@62 130 fields will be compressed away if you enable packing). Such a field can be made semantically
cannam@62 131 optional by placing it in a union with a `Void` field:
cannam@62 132
cannam@62 133 {% highlight capnp %}
cannam@62 134 union {
cannam@62 135 age @0 :Int32;
cannam@62 136 ageUnknown @1 :Void;
cannam@62 137 }
cannam@62 138 {% endhighlight %}
cannam@62 139
cannam@62 140 However, this field still takes space on the wire, and in fact takes an extra 16 bits of space
cannam@62 141 for the union tag. A better approach may be to give the field a bogus default value and interpret
cannam@62 142 that value to mean "not present".
cannam@62 143
cannam@62 144 Pointer fields are a bit different. They start out "null", and you can check for nullness using
cannam@62 145 the `hasFoo()` accessor. You could use a null pointer to mean "not present". Note, though, that
cannam@62 146 calling `getFoo()` on a null pointer returns the default value, which is indistinguishable from a
cannam@62 147 legitimate value, so checking `hasFoo()` is in fact the _only_ way to detect nullness.
cannam@62 148
cannam@62 149 ### How do I resize a list?
cannam@62 150
cannam@62 151 Unfortunately, you can't. You have to know the size of your list upfront, before you initialize
cannam@62 152 any of the elements. This is an annoying side effect of arena allocation, which is a fundamental
cannam@62 153 part of Cap'n Proto's design: in order to avoid making a copy later, all of the pieces of the
cannam@62 154 message must be allocated in a tightly-packed segment of memory, with each new piece being added
cannam@62 155 to the end. If a previously-allocated piece is discarded, it leaves a hole, which wastes space.
cannam@62 156 Since Cap'n Proto lists are flat arrays, the only way to resize a list would be to discard the
cannam@62 157 existing list and allocate a new one, which would thus necessarily waste space.
cannam@62 158
cannam@62 159 In theory, a more complicated memory allocation algorithm could attempt to reuse the "holes" left
cannam@62 160 behind by discarded message pieces. However, it would be hard to make sure any new data inserted
cannam@62 161 into the space is exactly the right size. Fragmentation would result. And the allocator would
cannam@62 162 have to do a lot of extra bookkeeping that could be expensive. This would be sad, as arena
cannam@62 163 allocation is supposed to be cheap!
cannam@62 164
cannam@62 165 The only solution is to temporarily place your data into some other data structure (an
cannam@62 166 `std::vector`, perhaps) until you know how many elements you have, then allocate the list and copy.
cannam@62 167 On the bright side, you probably aren't losing much performance this way -- using vectors already
cannam@62 168 involves making copies every time the backing array grows. It's just annoying to code.
cannam@62 169
cannam@62 170 Keep in mind that you can use [orphans](cxx.html#orphans) to allocate sub-objects before you have
cannam@62 171 a place to put them. But, also note that you cannot allocate elements of a struct list as orphans
cannam@62 172 and then put them together as a list later, because struct lists are encoded as a flat array of
cannam@62 173 struct values, not an array of pointers to struct values. You can, however, allocate any inner
cannam@62 174 objects embedded within those structs as orphans.
cannam@62 175
cannam@62 176 ## Security
cannam@62 177
cannam@62 178 ### Is Cap'n Proto secure?
cannam@62 179
cannam@62 180 What is your threat model?
cannam@62 181
cannam@62 182 ### Sorry. Can Cap'n Proto be used to deserialize malicious messages?
cannam@62 183
cannam@62 184 Cap'n Proto's serialization layer is designed to be safe against malicious input. The Cap'n Proto implementation should never segfault, corrupt memory, leak secrets, execute attacker-specified code, consume excessive resources, etc. as a result of any sequence of input bytes. Moreover, the API is carefully designed to avoid putting app developers into situations where it is easy to write insecure code -- we consider it a bug in Cap'n Proto if apps commonly misuse it in a way that is a security problem.
cannam@62 185
cannam@62 186 With all that said, Cap'n Proto's C++ reference implementation has not yet undergone a formal security review. It may have bugs.
cannam@62 187
cannam@62 188 ### Is it safe to use Cap'n Proto RPC with a malicious peer?
cannam@62 189
cannam@62 190 Cap'n Proto's RPC layer is explicitly designed to be useful for interactions between mutually-distrusting parties. Its capability-based security model makes it easy to express complex interactions securely.
cannam@62 191
cannam@62 192 At this time, the RPC layer is not robust against resource exhaustion attacks, possibly allowing denials of service.
cannam@62 193
cannam@62 194 ### Is Cap'n Proto encrypted?
cannam@62 195
cannam@62 196 Cap'n Proto may be layered on top of an existing encrypted transport, such as TLS, but at this time it is the application's responsibility to add this layer. We plan to integrate this into the Cap'n Proto library proper in the future.
cannam@62 197
cannam@62 198 ### How do I report security bugs?
cannam@62 199
cannam@62 200 Please email [security@sandstorm.io](mailto:security@sandstorm.io).
cannam@62 201
cannam@62 202 ## Sandstorm
cannam@62 203
cannam@62 204 ### How does Cap'n Proto relate to Sandstorm.io?
cannam@62 205
cannam@62 206 [Sandstorm.io](https://sandstorm.io) is an Open Source project and startup founded by Kenton, the author of Cap'n Proto. Cap'n Proto is owned and developed by Sandstorm the company and heavily used in Sandstorm the project.
cannam@62 207
cannam@62 208 ### How does Sandstorm use Cap'n Proto?
cannam@62 209
cannam@62 210 See [this Sandstorm blog post](https://blog.sandstorm.io/news/2014-12-15-capnproto-0.5.html).
cannam@62 211