<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Cap'n Proto News</title>
		<description>Latest updates on Cap'n Proto</description>
		<link>https://capnproto.org</link>
		<atom:link href="/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Cap'n Proto 1.0</title>
				<description>&lt;div style=&quot;float: right&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;/install.html&quot;&gt;Get it now &amp;raquo;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;It’s been a little over ten years since the first release of Cap’n Proto, on April 1, 2013. Today I’m releasing version 1.0 of Cap’n Proto’s C++ reference implementation.&lt;/p&gt;

&lt;p&gt;Don’t get too excited! There’s not actually much new. Frankly, I should have declared 1.0 a long time ago – probably around version 0.6 (in 2017) or maybe even 0.5 (in 2014). I didn’t mostly because there were a few advanced features (like three-party handoff, or shared-memory RPC) that I always felt like I wanted to finish before 1.0, but they just kept not reaching the top of my priority list. But the reality is that Cap’n Proto has been relied upon in production for a long time. In fact, you are using Cap’n Proto right now, to view this site, which is served by Cloudflare, which uses Cap’n Proto extensively (and is also my employer, although they used Cap’n Proto before they hired me). Cap’n Proto is used to encode millions (maybe billions) of messages and gigabits (maybe terabits) of data every single second of every day. As for those still-missing features, the real world has seemingly proven that they aren’t actually that important. (I still do want to complete them though.)&lt;/p&gt;

&lt;p&gt;Ironically, the thing that finally motivated the 1.0 release is so that we can start working on 2.0. But again here, don’t get too excited! Cap’n Proto 2.0 is not slated to be a revolutionary change. Rather, there are a number of changes we (the Cloudflare Workers team) would like to make to Cap’n Proto’s C++ API, and its companion, the KJ C++ toolkit library. Over the ten years these libraries have been available, I have kept their APIs pretty stable, despite being 0.x versioned. But for 2.0, we want to make some sweeping backwards-incompatible changes, in order to fix some footguns and improve developer experience for those on our team.&lt;/p&gt;

&lt;p&gt;Some users probably won’t want to keep up with these changes. Hence, I’m releasing 1.0 now as a sort of “long-term support” release. We’ll backport bugfixes as appropriate to the 1.0 branch for the long term, so that people who aren’t interested in changes can just stick with it.&lt;/p&gt;

&lt;h2 id=&quot;whats-actually-new-in-10&quot;&gt;What’s actually new in 1.0?&lt;/h2&gt;

&lt;p&gt;Again, not a whole lot has changed since the last version, 0.10. But there are a few things worth mentioning:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;A number of optimizations were made to improve performance of Cap’n Proto RPC. These include reducing the amount of memory allocation done by the RPC implementation and KJ I/O framework, adding the ability to elide certain messages from the RPC protocol to reduce traffic, and doing better buffering of small messages that are sent and received together to reduce syscalls. These are incremental improvements.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Breaking change:&lt;/strong&gt; Previously, servers could opt into allowing RPC cancellation by calling &lt;code&gt;context.allowCancellation()&lt;/code&gt; after a call was delivered. In 1.0, opting into cancellation is instead accomplished using an annotation on the schema (the &lt;code&gt;allowCancellation&lt;/code&gt; annotation defined in &lt;code&gt;c++.capnp&lt;/code&gt;). We made this change after observing that in practice, we almost always wanted to allow cancellation, but we almost always forgot to do so. The schema-level annotation can be set on a whole file at a time, which is easier not to forget. Moreover, the dynamic opt-in required a lot of bookkeeping that had a noticeable performance impact in practice; switching to the annotation provided a performance boost. For users that never used &lt;code&gt;context.allowCancellation()&lt;/code&gt; in the first place, there’s no need to change anything when upgrading to 1.0 – cancellation is still disallowed by default. (If you are affected, you will see a compile error. If there’s no compile error, you have nothing to worry about.)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;KJ now uses &lt;code&gt;kqueue()&lt;/code&gt; to handle asynchronous I/O on systems that have it (MacOS and BSD derivatives). KJ has historically always used &lt;code&gt;epoll&lt;/code&gt; on Linux, but until now had used a slower &lt;code&gt;poll()&lt;/code&gt;-based approach on other Unix-like platforms.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;KJ’s HTTP client and server implementations now support the &lt;code&gt;CONNECT&lt;/code&gt; method.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/pull/1700&quot;&gt;A new class &lt;code&gt;capnp::RevocableServer&lt;/code&gt; was introduced&lt;/a&gt; to assist in exporting RPC wrappers around objects whose lifetimes are not controlled by the wrapper. Previously, avoiding use-after-free bugs in such scenarios was tricky.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Many, many smaller bug fixes and improvements. &lt;a href=&quot;https://github.com/capnproto/capnproto/pulls?q=is%3Apr+is%3Aclosed&quot;&gt;See the PR history&lt;/a&gt; for details.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;whats-planned-for-20&quot;&gt;What’s planned for 2.0?&lt;/h2&gt;

&lt;p&gt;The changes we have in mind for version 2.0 of Cap’n Proto’s C++ implementation are mostly NOT related to the protocol itself, but rather to the C++ API and especially to KJ, the C++ toolkit library that comes with Cap’n Proto. These changes are motivated by our experience building a large codebase on top of KJ: namely, the Cloudflare Workers runtime, &lt;a href=&quot;https://github.com/cloudflare/workerd&quot;&gt;&lt;code&gt;workerd&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;KJ is a C++ toolkit library, arguably comparable to things like Boost, Google’s Abseil, or Facebook’s Folly. I started building KJ at the same time as Cap’n Proto in 2013, at a time when C++11 was very new and most libraries were not really designing around it yet. The intent was never to create a new standard library, but rather to address specific needs I had at the time. But over many years, I ended up building a lot of stuff. By the time I joined Cloudflare and started the Workers Runtime, KJ already featured a powerful async I/O framework, HTTP implementation, TLS bindings, and more.&lt;/p&gt;

&lt;p&gt;Of course, KJ has nowhere near as much stuff as Boost or Abseil, and nowhere near as much engineering effort behind it. You might argue, therefore, that it would have been better to choose one of those libraries to build on. However, KJ had a huge advantage: that we own it, and can shape it to fit our specific needs, without having to fight with anyone to get those changes upstreamed.&lt;/p&gt;

&lt;p&gt;One example among many: KJ’s HTTP implementation features the ability to “suspend” the state of an HTTP connection, after receiving headers, and transfer it to a different thread or process to be resumed. This is an unusual thing to want, but is something we needed for resource management in the Workers Runtime. Implementing this required some deep surgery in KJ HTTP and definitely adds complexity. If we had been using someone else’s HTTP library, would they have let us upstream such a change?&lt;/p&gt;

&lt;p&gt;That said, even though we own KJ, we’ve still tried to avoid making any change that breaks third-party users, and this has held back some changes that would probably benefit Cloudflare Workers. We have therefore decided to “fork” it. Version 2.0 is that fork.&lt;/p&gt;

&lt;p&gt;Development of version 2.0 will take place on Cap’n Proto’s new &lt;code&gt;v2&lt;/code&gt; branch. The &lt;code&gt;master&lt;/code&gt; branch will become the 1.0 LTS branch, so that existing projects which track &lt;code&gt;master&lt;/code&gt; are not disrupted by our changes.&lt;/p&gt;

&lt;p&gt;We don’t yet know all the changes we want to make as we’ve only just started thinking seriously about it. But, here’s some ideas we’ve had so far:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;We will require a compiler with support for C++20, or maybe even C++23. Cap’n Proto 1.0 only requires C++14.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;In particular, we will require a compiler that supports C++20 coroutines, as lots of KJ async code will be refactored to rely on coroutines. This should both make the code clearer and improve performance by reducing memory allocations. However, coroutine support is still spotty – as of this writing, GCC seems to ICE on KJ’s coroutine implementation.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Cap’n Proto’s RPC API, KJ’s HTTP APIs, and others are likely to be revised to make them more coroutine-friendly.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code&gt;kj::Maybe&lt;/code&gt; will become more ergonomic. It will no longer overload &lt;code&gt;nullptr&lt;/code&gt; to represent the absence of a value; we will introduce &lt;code&gt;kj::none&lt;/code&gt; instead. &lt;code&gt;KJ_IF_MAYBE&lt;/code&gt; will no longer produce a pointer, but instead a reference (a trick that becomes possible by utilizing C++17 features).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;We will drop support for compiling with exceptions disabled. KJ’s coding style uses exceptions as a form of software fault isolation, or “catchable panics”, such that errors can cause the “current task” to fail out without disrupting other tasks running concurrently. In practice, this ends up affecting every part of how KJ-style code is written. And yet, since the beginning, KJ and Cap’n Proto have been designed to accommodate environments where exceptions are turned off at compile time, using an elaborate system to fall back to callbacks and distinguish between fatal and non-fatal exceptions. In practice, maintaining this ability has been a drag on development – no-exceptions mode is constantly broken and must be tediously fixed before each release. Even when the tests are passing, it’s likely that a lot of KJ’s functionality realistically cannot be used in no-exceptions mode due to bugs and fragility. Today, I would strongly recommend against anyone using this mode except maybe for the most basic use of Cap’n Proto’s serialization layer. Meanwhile, though, I’m honestly not sure if anyone uses this mode at all! In theory I would expect many people do, since many people choose to use C++ with exceptions disabled, but I’ve never actually received a single question or bug report related to it. It seems very likely that this was wasted effort all along. By removing support, we can simplify a lot of stuff and probably do releases more frequently going forward.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Similarly, we’ll drop support for no-RTTI mode and other exotic modes that are a maintenance burden.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;We may revise KJ’s approach to reference counting, as the current design has proven to be unintuitive to many users.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;We will fix a longstanding design flaw in &lt;code&gt;kj::AsyncOutputStream&lt;/code&gt;, where EOF is currently signaled by destroying the stream. Instead, we’ll add an explicit &lt;code&gt;end()&lt;/code&gt; method that returns a Promise. Destroying the stream without calling &lt;code&gt;end()&lt;/code&gt; will signal an erroneous disconnect. (There are several other aesthetic improvements I’d like to make to the KJ stream APIs as well.)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;We may want to redesign several core I/O APIs to be a better fit for Linux’s new-ish io_uring event notification paradigm.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The RPC implementation may switch to allowing cancellation by default. As discussed above, this is opt-in today, but in practice I find it’s almost always desirable, and disallowing it can lead to subtle problems.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;And so on.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s worth noting that at present, there is no plan to make any backwards-incompatible changes to the serialization format or RPC protocol. The changes being discussed only affect the C++ API. Applications written in other languages are completely unaffected by all this.&lt;/p&gt;

&lt;p&gt;It’s likely that a formal 2.0 release will not happen for some time – probably a few years. I want to make sure we get through all the really big breaking changes we want to make, before we inflict update pain on most users. Of course, if you’re willing to accept breakages, you can always track the &lt;code&gt;v2&lt;/code&gt; branch. Cloudflare Workers releases from &lt;code&gt;v2&lt;/code&gt; twice a week, so it should always be in good working order.&lt;/p&gt;
</description>
				<pubDate>Fri, 28 Jul 2023 00:00:00 +0000</pubDate>
                                <link>/news/2023-07-28-capnproto-1.0.html</link>
                                <guid isPermaLink="true">/news/2023-07-28-capnproto-1.0.html</guid>
			</item>
		
			<item>
				<title>CVE-2022-46149: Possible out-of-bounds read related to list-of-pointers</title>
				<description>&lt;p&gt;David Renshaw, the author of the Rust implementation of Cap’n Proto, discovered a security vulnerability affecting both the C++ and Rust implementations of Cap’n Proto. The vulnerability was discovered using fuzzing. In theory, the vulnerability could lead to out-of-bounds reads which could cause crashes or perhaps exfiltration of memory.&lt;/p&gt;

&lt;p&gt;The vulnerability is exploitable only if an application performs a certain unusual set of actions. As of this writing, we are not aware of any applications that are actually affected. However, out of an abundance of caution, we are issuing a security advisory and advising everyone to patch.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/security-advisories/2022-11-30-0-pointer-list-bounds.md&quot;&gt;Our security advisory&lt;/a&gt; explains the impact of the bug, what an app must do to be affected, and where to find the fix.&lt;/p&gt;

&lt;p&gt;Check out &lt;a href=&quot;https://dwrensha.github.io/capnproto-rust/2022/11/30/out_of_bounds_memory_access_bug.html&quot;&gt;David’s blog post&lt;/a&gt; for an in-depth explanation of the bug itself, including some of the inner workings of Cap’n Proto.&lt;/p&gt;
</description>
				<pubDate>Wed, 30 Nov 2022 00:00:00 +0000</pubDate>
                                <link>/news/2022-11-30-CVE-2022-46149-security-advisory.html</link>
                                <guid isPermaLink="true">/news/2022-11-30-CVE-2022-46149-security-advisory.html</guid>
			</item>
		
			<item>
				<title>Cap'n Proto 0.10</title>
				<description>&lt;div style=&quot;float: right&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;/install.html&quot;&gt;Get it now &amp;raquo;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Today I’m releasing Cap’n Proto 0.10.&lt;/p&gt;

&lt;p&gt;Like last time, there’s no huge new features in this release, but there are many minor improvements and bug fixes. You can &lt;a href=&quot;https://github.com/capnproto/capnproto/pulls?q=is%3Apr+is%3Aclosed&quot;&gt;read the PR history&lt;/a&gt; to find out what has changed.&lt;/p&gt;
</description>
				<pubDate>Fri, 03 Jun 2022 00:00:00 +0000</pubDate>
                                <link>/news/2022-06-03-capnproto-0.10.html</link>
                                <guid isPermaLink="true">/news/2022-06-03-capnproto-0.10.html</guid>
			</item>
		
			<item>
				<title>Cap'n Proto 0.9</title>
				<description>&lt;div style=&quot;float: right&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;/install.html&quot;&gt;Get it now &amp;raquo;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Today I’m releasing Cap’n Proto 0.9.&lt;/p&gt;

&lt;p&gt;There’s no huge new features in this release, but there are many minor improvements and bug fixes. You can &lt;a href=&quot;https://github.com/capnproto/capnproto/pulls?q=is%3Apr+is%3Aclosed&quot;&gt;read the PR history&lt;/a&gt; to find out what has changed.&lt;/p&gt;

&lt;p&gt;Cap’n Proto development has continued to be primarily driven by the &lt;a href=&quot;https://workers.cloudflare.com/&quot;&gt;Cloudflare Workers&lt;/a&gt; project (of which I’m the lead engineer). As of the previous release, Cloudflare Workers primarily used the &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md&quot;&gt;KJ C++ toolkit&lt;/a&gt; that is developed with Cap’n Proto, but made only light use of Cap’n Proto serialization and RPC itself. That has now changed: the implementation of &lt;a href=&quot;https://blog.cloudflare.com/introducing-workers-durable-objects/&quot;&gt;Durable Objects&lt;/a&gt; makes heavy use of Cap’n Proto RPC for essentially all communication within the system.&lt;/p&gt;
</description>
				<pubDate>Sat, 14 Aug 2021 00:00:00 +0000</pubDate>
                                <link>/news/2021-08-14-capnproto-0.9.html</link>
                                <guid isPermaLink="true">/news/2021-08-14-capnproto-0.9.html</guid>
			</item>
		
			<item>
				<title>Cap'n Proto 0.8: Streaming flow control, HTTP-over-RPC, fibers, etc.</title>
				<description>&lt;div style=&quot;float: right&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;/install.html&quot;&gt;Get it now &amp;raquo;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Today I’m releasing Cap’n Proto 0.8.&lt;/p&gt;

&lt;h3 id=&quot;whats-new&quot;&gt;What’s new?&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#multi-stream-flow-control&quot;&gt;Multi-stream Flow Control&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#http-over-capn-proto&quot;&gt;HTTP-over-Cap’n-Proto&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#kj-improvements&quot;&gt;KJ improvements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lots and lots of minor tweaks and fixes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;multi-stream-flow-control&quot;&gt;Multi-stream Flow Control&lt;/h4&gt;

&lt;p&gt;It is commonly believed, wrongly, that Cap’n Proto doesn’t support “streaming”, in the way that gRPC does. In fact, Cap’n Proto’s object-capability model and promise pipelining make it much more expressive than gRPC. In Cap’n Proto, “streaming” is just a pattern, not a built-in feature.&lt;/p&gt;

&lt;p&gt;Streaming is accomplished by introducing a temporary RPC object as part of a call. Each streamed message becomes a call to the temporary object. Think of this like providing a callback function in an object-oriented language.&lt;/p&gt;

&lt;p&gt;For instance, server -&amp;gt; client streaming (“returning multiple responses”) can look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-capnp&quot; data-lang=&quot;capnp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# NOT NEW: Server -&amp;gt; client streaming example.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyInterface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;{
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;streamingCall&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;(&lt;span class=&quot;n&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;:Callback&lt;/span&gt;)&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-&amp;gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;();

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Callback&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;{
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendChunk&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;(&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;:Data&lt;/span&gt;)&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-&amp;gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;();
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;}
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Or for client -&amp;gt; server streaming, the server returns a callback:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-capnp&quot; data-lang=&quot;capnp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# NOT NEW: Client -&amp;gt; Server streaming example.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyInterface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;{
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;streamingCall&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;()&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-&amp;gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;(&lt;span class=&quot;n&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;:Callback&lt;/span&gt;);

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Callback&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;{
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendChunk&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;(&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;:Data&lt;/span&gt;)&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-&amp;gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;();
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;}
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that the client -&amp;gt; server example relies on &lt;a href=&quot;https://capnproto.org/rpc.html#time-travel-promise-pipelining&quot;&gt;promise pipelining&lt;/a&gt;: When the client invokes &lt;code&gt;streamingCall()&lt;/code&gt;, it does NOT have to wait for the server to respond before it starts making calls to the &lt;code&gt;callback&lt;/code&gt;. Using promise pipelining (which has been a built-in feature of Cap’n Proto RPC since its first release in 2013), the client sends messages to the server that say: “Once my call to &lt;code&gt;streamingCall()&lt;/code&gt; call is finished, take the returned callback and call this on it.”&lt;/p&gt;

&lt;p&gt;Obviously, you can also combine the two examples to create bidirectional streams. You can also introduce “callback” objects that have multiple methods, methods that themselves return values (maybe even further streaming callbacks!), etc. You can send and receive multiple new RPC objects in a single call. Etc.&lt;/p&gt;

&lt;p&gt;But there has been one problem that arises in the context of streaming specifically: flow control. Historically, if an app wanted to stream faster than the underlying network connection would allow, then it could end up queuing messages in memory. Worse, if other RPC calls were happening on the same connection concurrently, they could end up blocked behind these queued streaming calls.&lt;/p&gt;

&lt;p&gt;In order to avoid such problems, apps needed to implement some sort of flow control strategy. An easy strategy was to wait for each &lt;code&gt;sendChunk()&lt;/code&gt; call to return before starting the next call, but this would incur an unnecessary network round trip for each chunk. A better strategy was for apps to allow multiple concurrent calls, but only up to some limit before waiting for in-flight calls to return. For example, an app could limit itself to four in-flight stream calls at a time, or to 64kB worth of chunks.&lt;/p&gt;

&lt;p&gt;This sort of worked, but there were two problems. First, this logic could get pretty complicated, distracting from the app’s business logic. Second, the “N-bytes-in-flight-at-a-time” strategy only works well if the value of N is close to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Bandwidth-delay_product&quot;&gt;bandwidth-delay product (BDP)&lt;/a&gt; of the connection. If N was chosen too low, the connection would be under-utilized. If too high, it would increase queuing latency for all users of the connection.&lt;/p&gt;

&lt;p&gt;Cap’n Proto 0.8 introduces a built-in feature to manage flow control. Now, you can declare your streaming calls like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-capnp&quot; data-lang=&quot;capnp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyInterface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;{
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;streamingCall&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;(&lt;span class=&quot;n&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;:Callback&lt;/span&gt;)&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-&amp;gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;();

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Callback&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;{
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# NEW: This streaming call features flow control!&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendChunk&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;(&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;:Data&lt;/span&gt;)&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-&amp;gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;();
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;}
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Methods declared with &lt;code&gt;-&amp;gt; stream&lt;/code&gt; behave like methods with empty return types (&lt;code&gt;-&amp;gt; ()&lt;/code&gt;), but with special behavior when the call is sent over a network connection. Instead of waiting for the remote site to respond to the call, the Cap’n Proto client library will act as if the call has “returned” as soon as it thinks the app should send the next call. So, now the app can use a simple loop that calls &lt;code&gt;sendChunk()&lt;/code&gt;, waits for it to “complete”, then sends the next chunk. Each call will appear to “return immediately” until such a time as Cap’n Proto thinks the connection is fully-utilized, and then each call will block until space frees up.&lt;/p&gt;

&lt;p&gt;When using streaming, it is important that apps be aware that error handling works differently. Since the client side may indicate completion of the call before the call has actually executed on the server, any exceptions thrown on the server side obviously cannot propagate to the client. Instead, we introduce a new rule: If a streaming call ends up throwing an exception, then all later method invocations on the same object (streaming or not) will also throw the same exception. You’ll notice that we added a &lt;code&gt;done()&lt;/code&gt; method to the callback interface above. After completing all streaming calls, the caller &lt;em&gt;must&lt;/em&gt; call &lt;code&gt;done()&lt;/code&gt; to check for errors. If any previous streaming call failed, then &lt;code&gt;done()&lt;/code&gt; will fail too.&lt;/p&gt;

&lt;p&gt;Under the hood, Cap’n Proto currently implements flow control using a simple hack: it queries the send buffer size of the underlying network socket, and sets that as the “window size” for each stream. The operating system will typically increase the socket buffer as needed to match the TCP congestion window, and Cap’n Proto’s streaming window size will increase to match. This is not a very good implementation for a number of reasons. The biggest problem is that it doesn’t account for proxying: with Cap’n Proto it is common to pass objects through multiple nodes, which automatically arranges for calls to the object to be proxied though the middlemen. But, the TCP socket buffer size only approximates the BDP of the first hop. A better solution would measure the end-to-end BDP using an algorithm like &lt;a href=&quot;https://queue.acm.org/detail.cfm?id=3022184&quot;&gt;BBR&lt;/a&gt;. Expect future versions of Cap’n Proto to improve on this.&lt;/p&gt;

&lt;p&gt;Note that this new feature does not come with any change to the underlying RPC protocol! The flow control behavior is implemented entirely on the client side. The &lt;code&gt;-&amp;gt; stream&lt;/code&gt; declaration in the schema is merely a hint to the client that it should use this behavior. Methods declared with &lt;code&gt;-&amp;gt; stream&lt;/code&gt; are wire-compatible with methods declared with &lt;code&gt;-&amp;gt; ()&lt;/code&gt;. Currently, flow control is only implemented in the C++ library. RPC implementations in other languages will treat &lt;code&gt;-&amp;gt; stream&lt;/code&gt; the same as &lt;code&gt;-&amp;gt; ()&lt;/code&gt; until they add explicit support for it. Apps in those languages will need to continue doing their own flow control in the meantime, as they did before this feature was added.&lt;/p&gt;

&lt;h4 id=&quot;http-over-capn-proto&quot;&gt;HTTP-over-Cap’n-Proto&lt;/h4&gt;

&lt;p&gt;Cap’n Proto 0.8 defines &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/capnp/compat/http-over-capnp.capnp&quot;&gt;a protocol for tunnelling HTTP calls over Cap’n Proto RPC&lt;/a&gt;, along with an &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/capnp/compat/http-over-capnp.h&quot;&gt;adapter library&lt;/a&gt; adapting it to the &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/compat/http.h&quot;&gt;KJ HTTP API&lt;/a&gt;. Thus, programs written to send or receive HTTP requests using KJ HTTP can easily be adapted to communicate over Cap’n Proto RPC instead. It’s also easy to build a proxy that converts regular HTTP protocol into Cap’n Proto RPC and vice versa.&lt;/p&gt;

&lt;p&gt;In principle, http-over-capnp can achieve similar advantages to HTTP/2: Multiple calls can multiplex over the same connection with arbitrary ordering. But, unlike HTTP/2, calls can be initiated in either direction, can be addressed to multiple virtual endpoints (without relying on URL-based routing), and of course can be multiplexed with non-HTTP Cap’n Proto traffic.&lt;/p&gt;

&lt;p&gt;In practice, however, http-over-capnp is new, and should not be expected to perform as well as mature HTTP/2 implementations today. More work is needed.&lt;/p&gt;

&lt;p&gt;We use http-over-capnp in &lt;a href=&quot;https://workers.cloudflare.com/&quot;&gt;Cloudflare Workers&lt;/a&gt; to communicate HTTP requests between components of the system, especially into and out of sandboxes. Using this protocol, instead of plain HTTP or HTTP/2, allows us to communicate routing and metadata out-of-band (rather than e.g. stuffing it into private headers). It also allows us to design component APIs using an &lt;a href=&quot;http://erights.org/elib/capability/ode/ode-capabilities.html&quot;&gt;object-capability model&lt;/a&gt;, which turns out to be an excellent choice when code needs to be securely sandboxed.&lt;/p&gt;

&lt;p&gt;Today, our use of this protocol is fairly experimental, but we plan to use it more heavily as the code matures.&lt;/p&gt;

&lt;h4 id=&quot;kj-improvements&quot;&gt;KJ improvements&lt;/h4&gt;

&lt;p&gt;KJ is the C++ toolkit library developed together with Cap’n Proto’s C++ implementation. Ironically, most of the development in the Cap’n Proto repo these days is actually improvements to KJ, in part because it is used heavily in the implementation of &lt;a href=&quot;https://workers.cloudflare.com/&quot;&gt;Cloudflare Workers&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The KJ Promise API now supports fibers. Fibers allow you to execute code in a synchronous style within a thread driven by an asynchronous event loop. The synchronous code runs on an alternate call stack. The code can synchronously wait on a promise, at which point the thread switches back to the main stack and runs the event loop. We generally recommend that new code be written in asynchronous style rather than using fibers, but fibers can be useful in cases where you want to call a synchronous library, and then perform asynchronous tasks in callbacks from said library. &lt;a href=&quot;https://github.com/capnproto/capnproto/pull/913&quot;&gt;See the pull request for more details.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;New API &lt;code&gt;kj::Executor&lt;/code&gt; can be used to communicate directly between event loops on different threads. You can use it to execute an arbitrary lambda on a different thread’s event loop. Previously, it was necessary to use some OS construct like a pipe, signal, or eventfd to wake up the receiving thread.&lt;/li&gt;
  &lt;li&gt;KJ’s mutex API now supports conditional waits, meaning you can unlock a mutex and sleep until such a time as a given lambda function, applied to the mutex’s protected state, evaluates to true.&lt;/li&gt;
  &lt;li&gt;The KJ HTTP library has continued to be developed actively for its use in &lt;a href=&quot;https://workers.cloudflare.com/&quot;&gt;Cloudflare Workers&lt;/a&gt;. This library now handles millions of requests per second worldwide, both as a client and as a server (since most Workers are proxies), for a wide variety of web sites big and small.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;towards-10&quot;&gt;Towards 1.0&lt;/h3&gt;

&lt;p&gt;Cap’n Proto has now been around for seven years, with many huge production users (such as Cloudflare). But, we’re still on an 0.x release? What gives?&lt;/p&gt;

&lt;p&gt;Well, to be honest, there are still a lot of missing features that I feel like are critical to Cap’n Proto’s vision, the most obvious one being three-party handoff. But, so far I just haven’t had a real production need to implement those features. Clearly, I should stop waiting for perfection.&lt;/p&gt;

&lt;p&gt;Still, there are a couple smaller things I want to do for an upcoming 1.0 release:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Properly document KJ, independent of Cap’n Proto. KJ has evolved into an extremely useful general-purpose C++ toolkit library.&lt;/li&gt;
  &lt;li&gt;Fix a mistake in the design of KJ’s &lt;code&gt;AsyncOutputStream&lt;/code&gt; interface. The interface currently does not have a method to write EOF; instead, EOF is implied by the destructor. This has proven to be the wrong design. Since fixing it will be a breaking API change for anyone using this interface, I want to do it before declaring 1.0.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I aim to get these done sometime this summer…&lt;/p&gt;
</description>
				<pubDate>Thu, 23 Apr 2020 00:00:00 +0000</pubDate>
                                <link>/news/2020-04-23-capnproto-0.8.html</link>
                                <guid isPermaLink="true">/news/2020-04-23-capnproto-0.8.html</guid>
			</item>
		
			<item>
				<title>Cap'n Proto 0.7 Released</title>
				<description>&lt;div style=&quot;float: right&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;/install.html&quot;&gt;Get it now &amp;raquo;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Today we’re releasing Cap’n Proto 0.7.&lt;/p&gt;

&lt;h3 id=&quot;as-used-in-cloudflare-workers&quot;&gt;As used in Cloudflare Workers&lt;/h3&gt;

&lt;p&gt;The biggest high-level development in Cap’n Proto since the last release is its use in the implementation of &lt;a href=&quot;https://blog.cloudflare.com/cloudflare-workers-unleashed/&quot;&gt;Cloudflare Workers&lt;/a&gt; (of which I am the tech lead).&lt;/p&gt;

&lt;p&gt;Cloudflare operates a global network of 152 datacenters and growing, and Cloudflare Workers allows you to deploy “serveless” JavaScript to all of those locations in under 30 seconds. Your code is written against the W3C standard &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API&quot;&gt;Service Workers API&lt;/a&gt; and handles HTTP traffic for your web site.&lt;/p&gt;

&lt;p&gt;The Cloudflare Workers runtime implementation is written in C++, leveraging the V8 JavaScript engine and libKJ, the C++ toolkit library distributed with Cap’n Proto.&lt;/p&gt;

&lt;p&gt;Cloudflare Workers are all about handling HTTP traffic, and the runtime uses KJ’s HTTP library to do it. This means the KJ HTTP library is now battle-tested in production. Every package downloaded from &lt;a href=&quot;https://npmjs.org&quot;&gt;npm&lt;/a&gt;, for example, passes through KJ’s HTTP client and server libraries on the way (since npm uses Workers).&lt;/p&gt;

&lt;p&gt;The Workers runtime makes heavy use of KJ, but so far only makes light use of Cap’n Proto serialization. Cap’n Proto is used as a format for distributing configuration as well as (ironically) to handle JSON. We anticipate, however, making deeper use of Cap’n Proto in the future, including RPC.&lt;/p&gt;

&lt;h3 id=&quot;what-else-is-new&quot;&gt;What else is new?&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;The C++ library now requires C++14 or newer. It requires GCC 4.9+, Clang 3.6+, or Microsoft Visual Studio 2017. This change allows us to make faster progress and provide cleaner APIs by utilizing newer language features.&lt;/li&gt;
  &lt;li&gt;The JSON parser now supports &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/capnp/compat/json.capnp&quot;&gt;annotations to customize conversion behavior&lt;/a&gt;. These allow you to override field names (e.g. to use underscores instead of camelCase), flatten sub-objects, and express unions in various more-idiomatic ways.&lt;/li&gt;
  &lt;li&gt;The KJ HTTP library supports WebSockets, and has generally become much higher-quality as it has been battle-tested in Cloudflare Workers.&lt;/li&gt;
  &lt;li&gt;KJ now offers its own &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/map.h&quot;&gt;hashtable- and b-tree-based container implementations&lt;/a&gt;. &lt;code&gt;kj::HashMap&lt;/code&gt; is significantly faster and more memory-efficient than &lt;code&gt;std::unordered_map&lt;/code&gt;, with more optimizations coming. &lt;code&gt;kj::TreeMap&lt;/code&gt; is somewhat slower than &lt;code&gt;std::map&lt;/code&gt;, but uses less memory and has a smaller code footprint. Both are implemented on top of &lt;code&gt;kj::Table&lt;/code&gt;, a building block that can also support multi-maps. Most importantly, all these interfaces are cleaner and more modern than their ancient STL counterparts.&lt;/li&gt;
  &lt;li&gt;KJ now includes &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/compat/tls.h&quot;&gt;TLS bindings&lt;/a&gt;. &lt;code&gt;libkj-tls&lt;/code&gt; wraps OpenSSL or BoringSSL and provides a simple, hard-to-mess-up API integrated with the KJ event loop.&lt;/li&gt;
  &lt;li&gt;KJ now includes &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/compat/gzip.h&quot;&gt;gzip bindings&lt;/a&gt;, which wrap zlib in KJ stream interfaces (sync and async).&lt;/li&gt;
  &lt;li&gt;KJ now includes &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/encoding.h&quot;&gt;helpers for encoding/decoding Unicode (UTF-8/UTF-16/UTF-32), base64, hex, URI-encoding, and C-escaped text&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/compat/url.h&quot;&gt;&lt;code&gt;kj::Url&lt;/code&gt; helper class&lt;/a&gt; is provided to parse and compose URLs.&lt;/li&gt;
  &lt;li&gt;KJ now includes &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/filesystem.h&quot;&gt;a filesystem API&lt;/a&gt; which is designed to be resistant to path injection attacks, is dependency-injection-friendly to ease unit testing, is cross-platform (Unix and Windows), makes atomic file replacement easy, makes mmap easy, and &lt;a href=&quot;https://github.com/capnproto/capnproto/pull/384&quot;&gt;other neat features&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;capnp&lt;/code&gt; tool now has a &lt;code&gt;convert&lt;/code&gt; command which can be used to convert between all known message encodings, such as binary, packed, text, JSON, canonical, etc. This obsoletes the old &lt;code&gt;encode&lt;/code&gt; and &lt;code&gt;decode&lt;/code&gt; commands.&lt;/li&gt;
  &lt;li&gt;Many smaller tweaks and bug fixes.&lt;/li&gt;
&lt;/ul&gt;

&lt;div style=&quot;text-align: center&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff; width: 45%&quot; href=&quot;/install.html&quot;&gt;Download &amp;raquo;&lt;/a&gt;&lt;/div&gt;
</description>
				<pubDate>Tue, 28 Aug 2018 00:00:00 +0000</pubDate>
                                <link>/news/2018-08-28-capnproto-0.7.html</link>
                                <guid isPermaLink="true">/news/2018-08-28-capnproto-0.7.html</guid>
			</item>
		
			<item>
				<title>Cap'n Proto 0.6 Released: Two and a half years of improvements</title>
				<description>&lt;div style=&quot;float: right&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;/install.html&quot;&gt;Get it now &amp;raquo;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Today we’re releasing Cap’n Proto 0.6, the first major Cap’n Proto release in nearly 2.5 years.&lt;/p&gt;

&lt;p&gt;Cap’n Proto has been under active development the entire time, as part of its parent project, &lt;a href=&quot;https://sandstorm.io&quot;&gt;Sandstorm.io&lt;/a&gt;. The lack of releases did not indicate a lack of development, but rather a lack of keeping the code running on every platform it supports – especially Windows. Without a working Windows build, we couldn’t do a release. But as Sandstorm didn’t need Windows, it was hard to prioritize – that is, until contributors stepped up!&lt;/p&gt;

&lt;p&gt;Note that this release encompasses the core tools and the C++ reference implementation. Implementations in other languages have their own release schedules, but it’s likely that several will be updated soon to integrate new language features.&lt;/p&gt;

&lt;h3 id=&quot;brought-to-you-by-cloudflare&quot;&gt;Brought to you by Cloudflare&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://sandstorm.io/news/2017-03-13-joining-cloudflare&quot;&gt;As announced on the Sandstorm blog&lt;/a&gt;, most of the Sandstorm team (including myself) now work for &lt;a href=&quot;https://cloudflare.com&quot;&gt;Cloudflare&lt;/a&gt;. Cloudflare is one of the largest users of Cap’n Proto, &lt;a href=&quot;https://youtu.be/LA-gNoxSLCE?t=12m47s&quot;&gt;as described in this talk by John-Graham Cumming&lt;/a&gt;, and as such maintaining Cap’n Proto is part of my job at Cloudflare.&lt;/p&gt;

&lt;div style=&quot;text-align: center&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff&quot; href=&quot;https://www.meetup.com/Sandstorm-SF-Bay-Area/events/239341254/&quot;&gt;Come to our release party May 18 at Cloudflare SF&lt;/a&gt;&lt;/div&gt;

&lt;h3 id=&quot;whats-new&quot;&gt;What’s New?&lt;/h3&gt;

&lt;h4 id=&quot;full-windows--visual-studio-support&quot;&gt;Full Windows / Visual Studio Support&lt;/h4&gt;

&lt;p&gt;With this release, all of Cap’n Proto’s functionality now works on Windows with Visual Studio 2015 and 2017. That includes the serialization, dynamic API, schema parser, async I/O framework (using I/O completion ports), RPC, and tools. This is a huge step up from 0.5, in which Cap’n Proto could only be built in “lite mode”, which supported only basic serialization.&lt;/p&gt;

&lt;p&gt;Most of the work to make this happen was contributed by &lt;a href=&quot;https://github.com/harrishancock&quot;&gt;&lt;strong&gt;Harris Hancock&lt;/strong&gt;&lt;/a&gt; (with some help from &lt;a href=&quot;https://github.com/gordonmcshane&quot;&gt;Gordon McShane&lt;/a&gt;, &lt;a href=&quot;https://github.com/mark-grimes&quot;&gt;Mark Grimes&lt;/a&gt;, myself, and others). It was no small feat: Visual Studio’s C++ compiler is still quite buggy, so lots of work-arounds were needed. Meanwhile, the Cap’n Proto developers working on Linux were continuously introducing new issues with their changes. Harris sorted it all out and delivered a beautiful series of patches. He also helped get us set up with &lt;a href=&quot;https://ci.appveyor.com/project/kentonv/capnproto&quot;&gt;continuous integration on AppVeyor&lt;/a&gt;, so that we can stay on top of these issues going forward.&lt;/p&gt;

&lt;h4 id=&quot;security-hardening&quot;&gt;Security Hardening&lt;/h4&gt;

&lt;p&gt;The 0.6 release includes a number of measures designed to harden Cap’n Proto’s C++ implementation against possible security bugs. These include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The core pointer validation code has been refactored to detect possible integer overflows at compile time using C++ template metaprogramming, as &lt;a href=&quot;https://capnproto.org/news/2015-03-02-security-advisory-and-integer-overflow-protection.html&quot;&gt;described in this old blog post&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;The core test suite – which runs when you type &lt;code&gt;make check&lt;/code&gt; – now includes a targeted fuzz test of the pointer validation code.&lt;/li&gt;
  &lt;li&gt;We additionally tested this release using &lt;a href=&quot;http://lcamtuf.coredump.cx/afl/&quot;&gt;American Fuzzy Lop&lt;/a&gt;, running several different test cases for over three days each.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;json-converter&quot;&gt;JSON converter&lt;/h4&gt;

&lt;p&gt;Cap’n Proto messages can now be converted to and from JSON using &lt;code&gt;libcapnp-json&lt;/code&gt;. This makes it easy to integrate your JSON front-end API with your Cap’n Proto back-end.&lt;/p&gt;

&lt;p&gt;See the &lt;code&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/capnp/compat/json.h&quot;&gt;capnp/compat/json.h&lt;/a&gt;&lt;/code&gt; header for API details.&lt;/p&gt;

&lt;p&gt;This library was primarily built by &lt;a href=&quot;https://github.com/kamalmarhubi&quot;&gt;&lt;strong&gt;Kamal Marhubi&lt;/strong&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/katreniak&quot;&gt;&lt;strong&gt;Branislav Katreniak&lt;/strong&gt;&lt;/a&gt;, using Cap’n Proto’s &lt;a href=&quot;/cxx.html#dynamic-reflection&quot;&gt;dynamic API&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;http-library&quot;&gt;HTTP library&lt;/h4&gt;

&lt;p&gt;KJ (the C++ framework library bundled with Cap’n Proto) now ships with a minimalist HTTP library, &lt;code&gt;libkj-http&lt;/code&gt;. The library is based on the KJ asynchronous I/O framework and covers both client-side and server-side use cases. Although functional and used in production today, the library should be considered a work in progress – expect improvements in future releases, such as client connection pooling and TLS support.&lt;/p&gt;

&lt;p&gt;See the &lt;code&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/blob/master/c++/src/kj/compat/http.h&quot;&gt;kj/compat/http.h&lt;/a&gt;&lt;/code&gt; header for API details.&lt;/p&gt;

&lt;h4 id=&quot;smaller-things&quot;&gt;Smaller things&lt;/h4&gt;

&lt;p&gt;With two years of development, there are far too many changes to list, but here are some more things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;KJ now offers its own unit test framework under &lt;code&gt;kj/test.h&lt;/code&gt;, as well as a compatibility shim with Google Test under &lt;code&gt;kj/compat/gtest.h&lt;/code&gt;. The KJ and Cap’n Proto tests no longer depend on Google Test.&lt;/li&gt;
  &lt;li&gt;New API &lt;code&gt;capnp::TextCodec&lt;/code&gt; in &lt;code&gt;capnp/serialize-text.h&lt;/code&gt; provides direct access to parse text-format Cap’n Proto messages (requires &lt;code&gt;libcapnpc&lt;/code&gt;, the schema parser library). (Contributed by: &lt;a href=&quot;https://github.com/pqu&quot;&gt;&lt;strong&gt;Philip Quinn&lt;/strong&gt;&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;It is possible to compare Cap’n Proto messages for equality (with correct handling of unknown fields, something Protocol Buffers struggled with) using APIs in &lt;code&gt;capnp/any.h&lt;/code&gt;. (Contributed by: &lt;a href=&quot;https://github.com/joshuawarner32&quot;&gt;&lt;strong&gt;Joshua Warner&lt;/strong&gt;&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;A function &lt;code&gt;capnp::canonicalize()&lt;/code&gt; has been added which returns the canonical serialization of a given struct. (Contributed by: &lt;a href=&quot;https://github.com/maurer&quot;&gt;&lt;strong&gt;Matthew Maurer&lt;/strong&gt;&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;AnyPointer&lt;/code&gt; fields can now be assigned in constant values, by referencing another named constant (which itself is defined with a specific type).&lt;/li&gt;
  &lt;li&gt;In addition to &lt;code&gt;AnyPointer&lt;/code&gt;, the types &lt;code&gt;AnyStruct&lt;/code&gt;, &lt;code&gt;AnyList&lt;/code&gt;, and &lt;code&gt;Capability&lt;/code&gt; can now be used in schemas.&lt;/li&gt;
  &lt;li&gt;New class &lt;code&gt;capnp::CapabilityServerSet&lt;/code&gt; in &lt;code&gt;capnp/capability.h&lt;/code&gt; allows an RPC server to detect when capabilities to its own local objects are passed back to it and allows it to “unwrap” them to get at the underlying native object.&lt;/li&gt;
  &lt;li&gt;A membrane framework library was added (header &lt;code&gt;capnp/membrane.h&lt;/code&gt;). This makes it easy to set up a MITM layer between RPC actors, e.g. to implement revocability, transformations, and many other useful capability patterns.&lt;/li&gt;
  &lt;li&gt;Basic flow control can now be applied to an RPC connection, preventing new messages from being accepted if outstanding calls exceed a certain watermark, which helps prevent excessive buffering / malicious resource exhaustion. See &lt;code&gt;RpcSystem::setFlowLimit()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;KJ’s networking API now includes datagram protocols (UDP).&lt;/li&gt;
  &lt;li&gt;In &lt;code&gt;.capnp&lt;/code&gt; syntax, all comma-delimited lists can now have a trailing comma. (Contributed by: &lt;a href=&quot;https://github.com/zarvox&quot;&gt;&lt;strong&gt;Drew Fisher&lt;/strong&gt;&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Hundreds more small feature additions and bug fixes.&lt;/li&gt;
&lt;/ul&gt;

&lt;div style=&quot;text-align: center&quot;&gt;&lt;a class=&quot;block_link&quot; style=&quot;color: #fff; width: 45%&quot; href=&quot;/install.html&quot;&gt;Download &amp;raquo;&lt;/a&gt; &lt;a class=&quot;block_link&quot; style=&quot;color: #fff; width: 45%&quot; href=&quot;https://www.meetup.com/Sandstorm-SF-Bay-Area/events/239341254/&quot;&gt;Release Party &amp;raquo;&lt;/a&gt;&lt;/div&gt;
</description>
				<pubDate>Mon, 01 May 2017 00:00:00 +0000</pubDate>
                                <link>/news/2017-05-01-capnproto-0.6-msvc-json-http-more.html</link>
                                <guid isPermaLink="true">/news/2017-05-01-capnproto-0.6-msvc-json-http-more.html</guid>
			</item>
		
			<item>
				<title>Another security advisory -- Additional CPU amplification case</title>
				<description>&lt;p&gt;Unfortunately, it turns out that our fix for one of &lt;a href=&quot;2015-03-02-security-advisory-and-integer-overflow-protection.html&quot;&gt;the security advisories issued on Monday&lt;/a&gt; was not complete.&lt;/p&gt;

&lt;p&gt;Fortunately, the incomplete fix is for the non-critical vulnerability. The worst case is that an attacker could consume excessive CPU time.&lt;/p&gt;

&lt;p&gt;Nevertheless, we’ve issued &lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories/2015-03-05-0-c++-addl-cpu-amplification.md&quot;&gt;a new advisory&lt;/a&gt; and pushed a new release:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Release 0.5.1.2: &lt;a href=&quot;https://capnproto.org/capnproto-c++-0.5.1.2.tar.gz&quot;&gt;source&lt;/a&gt;, &lt;a href=&quot;https://capnproto.org/capnproto-c++-win32-0.5.1.2.zip&quot;&gt;win32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Release 0.4.1.2: &lt;a href=&quot;https://capnproto.org/capnproto-c++-0.4.1.2.tar.gz&quot;&gt;source&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sorry for the rapid repeated releases, but we don’t like sitting on security bugs.&lt;/p&gt;
</description>
				<pubDate>Thu, 05 Mar 2015 00:00:00 +0000</pubDate>
                                <link>/news/2015-03-05-another-cpu-amplification.html</link>
                                <guid isPermaLink="true">/news/2015-03-05-another-cpu-amplification.html</guid>
			</item>
		
			<item>
				<title>Security Advisory -- And how to catch integer overflows with template metaprogramming</title>
				<description>&lt;p&gt;As the installation page has always stated, I do not yet recommend using Cap’n Proto’s C++ library for handling possibly-malicious input, and will not recommend it until it undergoes a formal security review. That said, security is obviously a high priority for the project. The security of Cap’n Proto is in fact essential to the security of &lt;a href=&quot;https://sandstorm.io&quot;&gt;Sandstorm.io&lt;/a&gt;, Cap’n Proto’s parent project, in which sandboxed apps communicate with each other and the platform via Cap’n Proto RPC.&lt;/p&gt;

&lt;p&gt;A few days ago, the first major security bugs were found in Cap’n Proto C++ – two by security guru &lt;a href=&quot;http://en.wikipedia.org/wiki/Ben_Laurie&quot;&gt;Ben Laurie&lt;/a&gt; and one by myself during subsequent review (see below). You can read details about each bug in our new &lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories&quot;&gt;security advisories directory&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories/2015-03-02-0-c++-integer-overflow.md&quot;&gt;Integer overflow in pointer validation.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories/2015-03-02-1-c++-integer-underflow.md&quot;&gt;Integer underflow in pointer validation.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories/2015-03-02-2-all-cpu-amplification.md&quot;&gt;CPU usage amplification attack.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have backported the fixes to the last two release branches – 0.5 and 0.4:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Release 0.5.1.1: &lt;a href=&quot;https://capnproto.org/capnproto-c++-0.5.1.1.tar.gz&quot;&gt;source&lt;/a&gt;, &lt;a href=&quot;https://capnproto.org/capnproto-c++-win32-0.5.1.1.zip&quot;&gt;win32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Release 0.4.1.1: &lt;a href=&quot;https://capnproto.org/capnproto-c++-0.4.1.1.tar.gz&quot;&gt;source&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that we added a “nano” component to the version number (rather than use 0.5.2/0.4.2) to indicate that this release is ABI-compatible with the previous release. If you are linking Cap’n Proto as a shared library, you only need to update the library, not re-compile your app.&lt;/p&gt;

&lt;p&gt;To be clear, the first two bugs affect only the C++ implementation of Cap’n Proto; implementations in other languages are likely safe. The third bug probably affects all languages, and as of this writing only the C++ implementation (and wrappers around it) is fixed. However, this third bug is not as serious as the other two.&lt;/p&gt;

&lt;h3 id=&quot;preventative-measures&quot;&gt;Preventative Measures&lt;/h3&gt;

&lt;p&gt;It is our policy that any time a security problem is found, we will not only fix the problem, but also implement new measures to prevent the class of problems from occurring again. To that end, here’s what we’re doing doing to avoid problems like these in the future:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;A fuzz test of each pointer type has been added to the standard unit test
suite.&lt;/li&gt;
  &lt;li&gt;We will additionally add fuzz testing with American Fuzzy Lop to our
extended test suite.&lt;/li&gt;
  &lt;li&gt;In parallel, we will extend our use of template metaprogramming for
compile-time unit analysis (kj::Quantity in kj/units.h) to also cover
overflow detection (by tracking the maximum size of an integer value across
arithmetic expressions and raising an error when it overflows). More on this
below.&lt;/li&gt;
  &lt;li&gt;We will continue to require that all tests (including the new fuzz test) run
cleanly under Valgrind before each release.&lt;/li&gt;
  &lt;li&gt;We will commission a professional security review before any 1.0 release.
Until that time, we continue to recommend against using Cap’n Proto to
interpret data from potentially-malicious sources.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am pleased to report that measures 1, 2, and 3 all detected both integer overflow/underflow problems, and AFL additionally detected the CPU amplification problem.&lt;/p&gt;

&lt;h3 id=&quot;integer-overflow-is-hard&quot;&gt;Integer Overflow is Hard&lt;/h3&gt;

&lt;p&gt;Integer overflow is a nasty problem.&lt;/p&gt;

&lt;p&gt;In the past, C and C++ code has been plagued by buffer overrun bugs, but these days, systems engineers have mostly learned to avoid them by simply never using static-sized buffers for dynamically-sized content. If we don’t see proof that a buffer is the size of the content we’re putting in it, our “spidey sense” kicks in.&lt;/p&gt;

&lt;p&gt;But developing a similar sense for integer overflow is hard. We do arithmetic in code all the time, and the vast majority of it isn’t an issue. The few places where overflow can happen all too easily go unnoticed.&lt;/p&gt;

&lt;p&gt;And by the way, integer overflow affects many memory-safe languages too! Java and C# don’t protect against overflow. Python does, using slow arbitrary-precision integers. JavaScript doesn’t use integers, and is instead succeptible to loss-of-precision bugs, which can have similar (but more subtle) consequences.&lt;/p&gt;

&lt;p&gt;While writing Cap’n Proto, I made sure to think carefully about overflow and managed to correct for it most of the time. On learning that I missed a case, I immediately feared that I might have missed many more, and wondered how I might go about systematically finding them.&lt;/p&gt;

&lt;p&gt;Fuzz testing – e.g. using &lt;a href=&quot;http://lcamtuf.coredump.cx/afl/&quot;&gt;American Fuzzy Lop&lt;/a&gt; – is one approach, and is indeed how Ben found the two bugs he reported. As mentioned above, we will make AFL part of our release process in the future. However, AFL cannot really &lt;em&gt;prove&lt;/em&gt; anything – it can only try lots of possibilities. I want my compiler to refuse to compile arithmetic which might overflow.&lt;/p&gt;

&lt;h3 id=&quot;proving-safety-through-template-metaprogramming&quot;&gt;Proving Safety Through Template Metaprogramming&lt;/h3&gt;

&lt;p&gt;C++ Template Metaprogramming is powerful – many would say &lt;em&gt;too&lt;/em&gt; powerful. As it turns out, it’s powerful enough to do what we want.&lt;/p&gt;

&lt;p&gt;I defined a new type:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c--&quot; data-lang=&quot;c++&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Guarded&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// Wraps T (a basic integer type) and statically guarantees&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// that the value can be no more than `maxN` and no less than&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// zero.&lt;/span&gt;

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;possible overflow detected&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// If maxN is not representable in type T, we can no longer&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// guarantee no overflows.&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;OtherT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Guarded&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Guarded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OtherT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// You cannot construct a Guarded from another Guarded&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// with a higher maximum.&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;possible overflow detected&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;otherT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Guarded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guardedAdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;                           &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Guarded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// Addition operator also computes the new maximum.&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// (`guardedAdd` is a constexpr template that adds two&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// constants while detecting overflow.)&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Guarded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guardedAdd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherMax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;                   &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsafe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So, a &lt;code&gt;Guarded&amp;lt;10, int&amp;gt;&lt;/code&gt; represents a &lt;code&gt;int&lt;/code&gt; which is statically guaranteed to hold a non-negative value no greater than 10. If you add a &lt;code&gt;Guarded&amp;lt;10, int&amp;gt;&lt;/code&gt; to &lt;code&gt;Guarded&amp;lt;15, int&amp;gt;&lt;/code&gt;, the result is a &lt;code&gt;Guarded&amp;lt;25, int&amp;gt;&lt;/code&gt;. If you try to initialize a &lt;code&gt;Guarded&amp;lt;10, int&amp;gt;&lt;/code&gt; from a &lt;code&gt;Guarded&amp;lt;25, int&amp;gt;&lt;/code&gt;, you’ll trigger a &lt;code&gt;static_assert&lt;/code&gt; – the compiler will complain. You can, however, initialize a &lt;code&gt;Guarded&amp;lt;25, int&amp;gt;&lt;/code&gt; from a &lt;code&gt;Guarded&amp;lt;10, int&amp;gt;&lt;/code&gt; with no problem.&lt;/p&gt;

&lt;p&gt;Moreover, because all of &lt;code&gt;Guarded&lt;/code&gt;’s operators are inline and &lt;code&gt;constexpr&lt;/code&gt;, a good optimizing compiler will be able to optimize &lt;code&gt;Guarded&lt;/code&gt; down to the underlying primitive integer type. So, in theory, using &lt;code&gt;Guarded&lt;/code&gt; has no runtime overhead. (I have not yet verified that real compilers get this right, but I suspect they do.)&lt;/p&gt;

&lt;p&gt;Of course, the full implementation is considerably more complicated than this. The code has not been merged into the Cap’n Proto tree yet as we need to do more analysis to make sure it has no negative impact. For now, you can find it in the &lt;a href=&quot;https://github.com/capnproto/capnproto/tree/overflow-safe&quot;&gt;overflow-safe&lt;/a&gt; branch, specifically in the second half of &lt;a href=&quot;https://github.com/capnproto/capnproto/blob/overflow-safe/c++/src/kj/units.h&quot;&gt;kj/units.h&lt;/a&gt;. (This header also contains metaprogramming for compile-time unit analysis, which Cap’n Proto has been using since its first release.)&lt;/p&gt;

&lt;h3 id=&quot;results&quot;&gt;Results&lt;/h3&gt;

&lt;p&gt;I switched Cap’n Proto’s core pointer validation code (&lt;code&gt;capnp/layout.c++&lt;/code&gt;) over to &lt;code&gt;Guarded&lt;/code&gt;. In the process, I found:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Several overflows that could be triggered by the application calling methods with invalid parameters, but not by a remote attacker providing invalid message data. We will change the code to check these in the future, but they are not critical security problems.&lt;/li&gt;
  &lt;li&gt;The overflow that Ben had already reported (&lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories/2015-03-02-0-c++-integer-overflow.md&quot;&gt;2015-03-02-0&lt;/a&gt;). I had intentionally left this unfixed during my analysis to verify that &lt;code&gt;Guarded&lt;/code&gt; would catch it.&lt;/li&gt;
  &lt;li&gt;One otherwise-undiscovered integer underflow (&lt;a href=&quot;https://github.com/capnproto/capnproto/tree/master/security-advisories/2015-03-02-1-c++-integer-underflow.md&quot;&gt;2015-03-02-1&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on these results, I conclude that &lt;code&gt;Guarded&lt;/code&gt; is in fact effective at finding overflow bugs, and that such bugs are thankfully &lt;em&gt;not&lt;/em&gt; endemic in Cap’n Proto’s code.&lt;/p&gt;

&lt;p&gt;With that said, it does not seem practical to change every integer throughout the Cap’n Proto codebase to use &lt;code&gt;Guarded&lt;/code&gt; – using it in the API would create too much confusion and cognitive overhead for users, and would force application code to be more verbose. Therefore, this approach unfortunately will not be able to find all integer overflows throughout the entire library, but fortunately the most sensitive parts are covered in &lt;code&gt;layout.c++&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;why-dont-programming-languages-do-this&quot;&gt;Why don’t programming languages do this?&lt;/h3&gt;

&lt;p&gt;Anything that can be implemented in C++ templates can obviously be implemented by the compiler directly. So, why have so many languages settled for either modular arithmetic or slow arbitrary-precision integers?&lt;/p&gt;

&lt;p&gt;Languages could even do something which my templates cannot: allow me to declare relations between variables. For example, I would like to be able to declare an integer whose value is less than the size of some array. Then I know that the integer is a safe index for the array, without any run-time check.&lt;/p&gt;

&lt;p&gt;Obviously, I’m not the first to think of this. “Dependent types” have been researched for decades, but we have yet to see a practical language supporting them. Apparently, something about them is complicated, even though the rules look like they should be simple enough from where I’m standing.&lt;/p&gt;

&lt;p&gt;Some day, I would like to design a language that gets this right. But for the moment, I remain focused on &lt;a href=&quot;https://sandstorm.io&quot;&gt;Sandstorm.io&lt;/a&gt;. Hopefully someone will beat me to it. Hint hint.&lt;/p&gt;
</description>
				<pubDate>Mon, 02 Mar 2015 00:00:00 +0000</pubDate>
                                <link>/news/2015-03-02-security-advisory-and-integer-overflow-protection.html</link>
                                <guid isPermaLink="true">/news/2015-03-02-security-advisory-and-integer-overflow-protection.html</guid>
			</item>
		
			<item>
				<title>Cap'n Proto 0.5.1: Bugfixes</title>
				<description>&lt;p&gt;Cap’n Proto 0.5.1 has just been released with some bug fixes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;On Windows, the &lt;code&gt;capnp&lt;/code&gt; tool would crash when it tried to generate an ID, e.g. when using &lt;code&gt;capnp id&lt;/code&gt; or when compiling a file that was missing the file ID, because it tried to get random bytes from &lt;code&gt;/dev/urandom&lt;/code&gt;, which of course doesn’t exist on Windows. Oops. Now it uses &lt;code&gt;CryptGenRandom()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Declaring a generic method (with method-specific type parameters) inside a generic interface generated code that didn’t compile.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;joinPromises()&lt;/code&gt; didn’t work on an array of &lt;code&gt;Promise&amp;lt;void&amp;gt;&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Unnecessary error messages were being printed to the console when RPC clients disconnected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sorry about the bugs.&lt;/p&gt;

&lt;p&gt;In other news, as you can see, the Cap’n Proto web site now lives at &lt;code&gt;capnproto.org&lt;/code&gt;. Additionally, the Github repo has been moved to the &lt;a href=&quot;https://github.com/capnproto&quot;&gt;Sandstorm.io organization&lt;/a&gt;. Both moves have left behind redirects so that old links / repository references should continue to work.&lt;/p&gt;
</description>
				<pubDate>Fri, 23 Jan 2015 00:00:00 +0000</pubDate>
                                <link>/news/2015-01-23-capnproto-0.5.1-bugfixes.html</link>
                                <guid isPermaLink="true">/news/2015-01-23-capnproto-0.5.1-bugfixes.html</guid>
			</item>
		
	</channel>
</rss>
