<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="http://jroelofs.com/feed.xml" rel="self" type="application/atom+xml" /><link href="http://jroelofs.com/" rel="alternate" type="text/html" /><updated>2020-02-03T02:23:20+00:00</updated><id>http://jroelofs.com/feed.xml</id><title type="html">Jon Roelofs</title><subtitle>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
</subtitle><author><name>Jon Roelofs</name><email>jroelofs@jroelofs.com</email></author><entry><title type="html">Unconventional Calling</title><link href="http://jroelofs.com/general/2020/02/02/unconventional-calling/" rel="alternate" type="text/html" title="Unconventional Calling" /><published>2020-02-02T00:00:00+00:00</published><updated>2020-02-02T00:00:00+00:00</updated><id>http://jroelofs.com/general/2020/02/02/unconventional-calling</id><content type="html" xml:base="http://jroelofs.com/general/2020/02/02/unconventional-calling/">&lt;p&gt;&lt;img src=&quot;/images/jenga.jpg&quot; class=&quot;fit image&quot; /&gt;&lt;/p&gt;
&lt;div align=&quot;right&quot;&gt;
  &lt;h6&gt;
    JRoelofs Image
  &lt;/h6&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Recently a colleague and I were discussing some of the nuances of the ARM
Procedure Call Standard, related to some work I did adding armv4t support to
LLVM.&lt;/p&gt;

&lt;p&gt;First, let’s take a step back and review what a “Calling Convention” is, and
lay the ground rules for them on more modern ARM architectures before jumping
into the weeds with armv4t. Calling conventions are a low-level contract
describing how arguments may be passed to function calls, how return values are
passed back, and set the division of responsibilities between caller and callee
over saving and restoring registers before/after/during function calls.&lt;/p&gt;

&lt;p&gt;ARM EABI has a particularly straightforward calling convention for C functions
that denotes (ignoring VFP registers):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r0&lt;/code&gt;-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r3&lt;/code&gt; as caller-saved argument / scratch registers, with results passed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r0&lt;/code&gt;-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r1&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r4&lt;/code&gt;-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r8&lt;/code&gt; as callee-saved registers&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r9&lt;/code&gt; is either caller or callee saved, depending on the target platform&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r10&lt;/code&gt;-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r11&lt;/code&gt; are callee-saved&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r12&lt;/code&gt;-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r15&lt;/code&gt; are special-purpose registers (ip, fp, lr, pc)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Callee-saved registers must be saved by the called function, and caller-saved
registers must be saved by the calling function. This particular allocation of
a small number of caller-saved registers, with the bulk of the register set
allocated as callee-saved registers favors small functions with fewer live
ranges (and therefore less register pressure).  This seems to be a good
tradeoff since it allows small functions to avoid touching the stack to spill
live ranges, and leaves a big set of registers for use in large functions where
the cost of saving &amp;amp; restoring them is better amortized over the life of the
function.&lt;/p&gt;

&lt;p&gt;In practice, a C function like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;int melanger(int lhs, int rhs) {
    return lhs + rhs
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;will most likely get lowered to something like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;melanger:
    add r0, r0, r1
    bx lr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of note here is that the compiler passed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lhs&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rhs&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r1&lt;/code&gt;
respectively, and the result was returned in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r0&lt;/code&gt;. What’s also interesting is
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bx&lt;/code&gt; instruction used for the jump back to the caller in that it allows
“interworking” between the two instruction sets, Arm and Thumb, that most
32-bit ARM cores have on them. At branch time, depending on the parity of the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pc&lt;/code&gt; stored in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lr&lt;/code&gt;, the chip works out whether it needs to switch modes
between Arm and Thumb to correctly decode the instruction stream. A fun
side-effect of that is that all Thumb functions have odd addresses, and Arm
functions have even addresses.&lt;/p&gt;

&lt;p&gt;The call sites of this function will look something like this in the case of a
direct call:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    mov r0, ...
    mov r1, ...
    blx melanger
    mov ..., r0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Indirect calls (ie. through a function pointer) look a bit different:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    mov r0, #1
    mov r1, #2
    ldr r3, .funptr
    ldr r3, [r3]
    blx r3
    mov ..., r0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And that brings me to one of the first weirditudes of armv4: there is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blx&lt;/code&gt;
instruction, so indirect calls must use some other sequence. The most obvious
candidate would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov lr, pc; blx rN&lt;/code&gt;, but that has a subtle incompatiblity
if you attempt to run such code on a newer ARM core, since the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov&lt;/code&gt;
“magically” drops the parity bit, causing the instructions after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bx&lt;/code&gt; to
be interpreted as Arm instructions… which whole rest of the toolchain isn’t
prepared for.&lt;/p&gt;

&lt;p&gt;Instead, we take advantage of the fact that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bl&lt;/code&gt; instruction saves off the
link register, and create a sort of “branch island” to take care
of the interworking bits:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    mov r0, #1
    mov r1, #2
    ldr r3, .funptr
    bl .island
    mov ..., r0

    ...

.island:
    bx r3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This avoids the broken sequence, and maximizes compatibility with a bunch of
early ARM architecture variants. Iain Sandoe and I fixed that in
&lt;a href=&quot;https://github.com/llvm/llvm-project/commit/300d8ffdf2b07c77a6ffb30fe9b347f430d09df5&quot;&gt;r223380&lt;/a&gt;
if you’d like to take a look at the implementation details.&lt;/p&gt;

&lt;p&gt;Another place that armv4 gets weird is during function prologues, which would
normally leverage &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop {..., pc}&lt;/code&gt; to restore various callee-saved registers,
and jump back to the caller. Unfortunately on armv4, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; instruction
doesn’t restore the parity bit, so interworking through it is not permitted.
Instead, we have to return through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bx&lt;/code&gt; like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    pop {r3}
    add sp, #offset      # pop off whatever other stack we used
    bx r3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Things get even more complicated if the function needs to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r3&lt;/code&gt; as part of
the returned values, since now that hinders our ability to use that register to
help with interworking.  It is further complicated by the fact that there
aren’t many other registers that we can even use for this.  Any callee-saved
register is obviously out of the question, since we need to use it &lt;em&gt;after&lt;/em&gt;
we’ve restored them.  That limits us to a caller-saved one, or one of those
special-purpose registers mentioned earlier.  The best candidate is therefore
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ip&lt;/code&gt; register, i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r12&lt;/code&gt;, which is reserved for the linker as a scratch
register for use between function calls, and in that case we lower the prologue as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    mov ip, r3           # shuffle part of return value into ip
    pop {r3}             # lr from the call site
    add sp, #offset      # pop off whatever other stack we used
    mov lr, r3           # shuffle return address into lr
    mov r3, ip           # restore r3 as part of return value
    bx lr                # return to caller
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For those curious on the implementation details, I implemented this workaround
in &lt;a href=&quot;https://github.com/llvm/llvm-project/commit/f5fad3767b5e094b86c1495beae34252d21b1028&quot;&gt;r214881&lt;/a&gt;.&lt;/p&gt;</content><author><name>Jon Roelofs</name><email>jroelofs@jroelofs.com</email></author><summary type="html">JRoelofs Image</summary></entry><entry><title type="html">Anatomy of a SOW</title><link href="http://jroelofs.com/general/contracts/2019/12/15/anatomy-of-a-sow/" rel="alternate" type="text/html" title="Anatomy of a SOW" /><published>2019-12-15T00:00:00+00:00</published><updated>2019-12-15T00:00:00+00:00</updated><id>http://jroelofs.com/general/contracts/2019/12/15/anatomy-of-a-sow</id><content type="html" xml:base="http://jroelofs.com/general/contracts/2019/12/15/anatomy-of-a-sow/">&lt;p&gt;&lt;img src=&quot;/images/cuts-of-pork.png&quot; class=&quot;fit image&quot; /&gt;&lt;/p&gt;
&lt;div align=&quot;right&quot;&gt;
  &lt;h6&gt;
    &lt;a href=&quot;https://www.shutterstock.com/image-vector/cut-meat-set-poster-butcher-diagram-415805845?src=wtA8RwT4-7TikNqj595qdw-1-13&quot;&gt;Shutterstock Image&lt;/a&gt;
  &lt;/h6&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Writing good Statements of Work, or “SOW”s is one of the most important keys to
success in any long term project.  The structure it provides is often unseen
until well into projects that have gone off the rails in one way or another.
In this article, I’m going to walk through some of the things I’ve learned
about writing them over the course of several projects for different clients.&lt;/p&gt;

&lt;p&gt;In the early stages of a project, well before a SOW is even drafted, you’ll
often go through a “design study” phase.  During that period, the main ideas
for the What, How, and Why of the project get fleshed out in broad terms, but
with enough detail that someone familiar with the field could read it and get
up to speed quickly on your project.&lt;/p&gt;

&lt;p&gt;SOWs are similar to design studies and whitepapers, but more contractual,
and slightly less technical.  They are usually meant as a bridge between you
and your client to lay out the terms of what you’re going to build.  While you
may get along well with them now, the document you’re writing should be a clear
guide for the off-chance that something goes wrong with either their
expectations, or yours.  If you’re prepared for that, by having written a good
SOW, it’ll be much easier to manage those expectations, and keep everyone on
track.&lt;/p&gt;

&lt;h2 id=&quot;authoring&quot;&gt;Authoring&lt;/h2&gt;

&lt;p&gt;Like your project, it is important to have a good skeleton laid out in advance,
as that guides you through writing it.  SOWs have a pretty standard structure,
and once you’ve written one, you have a template for all of the rest of them
you’ll ever write.&lt;/p&gt;

&lt;h3 id=&quot;sections&quot;&gt;Sections&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#definitions&quot;&gt;Definitions&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#assumptions&quot;&gt;Assumptions&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#abstract&quot;&gt;Abstract&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#purpose&quot;&gt;Purpose&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deliverables&quot;&gt;Deliverables&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#milestones&quot;&gt;Milestones&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#acceptance&quot;&gt;Acceptance&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#budget&quot;&gt;Budget&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#agreement&quot;&gt;Agreement&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#appendix&quot;&gt;Appendix&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;definitions&quot;&gt;Definitions&lt;/h4&gt;

&lt;p&gt;Defining your terms helps to make sure that everyone is on the same page.
Often with technical work, there is quite a lot of jargon, with subjective
waffly meanings.  By writing down what you mean, things become objective and
the intent is clarified.  That way you, and your client can speak on even
terms, with a reduced chance of misunderstanding.&lt;/p&gt;

&lt;h4 id=&quot;assumptions&quot;&gt;Assumptions&lt;/h4&gt;

&lt;p&gt;Writing down your assumptions helps to limit the scope of whatever you didn’t
expect.  And surprises always show up when you least expect them (Ha! Imagine
that!) so it’s best to prepare for them as much as you can.  By preparing for
the unexpected, you’ll be giving yourself more room to negotiate later.&lt;/p&gt;

&lt;h4 id=&quot;abstract&quot;&gt;Abstract&lt;/h4&gt;

&lt;p&gt;Like any academic paper, it is useful to have a quick preliminary summary of
what the project is about, and why it’s going to get built.  This explains to
the non-technical managers, sales-people, and finance-critters the Big Picture
™ of what you’re trying to accomplish.  You’ll want this section to be
short and concise in order to keep from losing their attention, but general
enough to give them a working understanding of the project.  Think of what
you’d say in an Elevator Pitch, and put that in this section.  Use keywords
from your Definitions section, but don’t get too technical.&lt;/p&gt;

&lt;h4 id=&quot;purpose&quot;&gt;Purpose&lt;/h4&gt;

&lt;p&gt;This section is a good place to put the summary of the technical details. If
the Abstract was the view from the plane at 10km staring down at the top of
Everest, you’ll want this section to be right there at 8848m giving concrete
details.&lt;/p&gt;

&lt;h4 id=&quot;deliverables&quot;&gt;Deliverables&lt;/h4&gt;

&lt;p&gt;Deliverables are the bacon of a SOW, the &lt;em&gt;raison d’être&lt;/em&gt; of a project.  Often
they are intermediate steps along the way to completion of a project,
representing some modest amount of progress.  Sometimes they are a finished
product, other times they are a snapshot of development, or even a significant
event like publishing a website or achieving successful bringup on a new piece
of hardware.&lt;/p&gt;

&lt;p&gt;Each deliverable should be something self-contained, with a clear purpose.  If
you can put a simple name on it, even better, because you’re going to be
referring to it a lot.&lt;/p&gt;

&lt;h4 id=&quot;milestones&quot;&gt;Milestones&lt;/h4&gt;

&lt;p&gt;Milestones lay out the schedule of what is going to be built when, and by whom.&lt;/p&gt;

&lt;p&gt;For a fixed-fee structured contract, it is a really good idea to structure the
delivery dates of each milestone such that they’re relative to the other
milestones they depend on.  For example, if a farmer has two milestones, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M0&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M1&lt;/code&gt;, say, for raising a litter of piglets and delivering them to the butcher
once they’ve matured, then his &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M1&lt;/code&gt; depends on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M0&lt;/code&gt;.  Since he’s done this many
times before, he knows it takes about 4 months to raise a piglet to maturity.
Instead of writing down in his SOW that he expects his Sow to give birth in
January, with delivery to the butcher in May, he should give an estimated time
for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M0&lt;/code&gt;, and a relative time of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M0 + 4mo&lt;/code&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M1&lt;/code&gt;s delivery date.  This
protects him from something he doesn’t have a lot of control over: when the Sow
delivers.&lt;/p&gt;

&lt;p&gt;In a services organization, it isn’t advisable to promise too much about who
specifically is going to be doing what work.  Engineers are mostly
interchangable, and frankly, your client shouldn’t care.  On the other hand, it
is good to clear up which parts of the project you will be implementing, and
which parts of it your client needs to do.  The Milestones table is a good
place for that.&lt;/p&gt;

&lt;h4 id=&quot;acceptance&quot;&gt;Acceptance&lt;/h4&gt;

&lt;p&gt;Acceptance criteria are the rubrics by which you measure completion of a
project.  It is critical that they be well defined, and objective so that you
and your client can agree when Done is Done.&lt;/p&gt;

&lt;p&gt;Sometimes projects call for the Acceptance Tests to be determined later in the
project, instead of them being decided up front.  That usually comes up when the
experience of building each of the deliverables is thought to give more clarity
on how to actually define “Done”.  Be careful writing such an agreement
however, since it lets some subjectivity and wiggle room in through the back
door.  While it may allow you more flexibility later, it comes with the price
of allowing your customer to force scope-creep.  Usually the latter overshadows
any benefit of the former unless you’ve already developed a good working
relationship with them.&lt;/p&gt;

&lt;h4 id=&quot;budget&quot;&gt;Budget&lt;/h4&gt;

&lt;p&gt;The budget section of a SOW describes the payment schedule over the course of
the project.  Usually payments are contingent upon achieving milestones, and
tied to making successful deliveries.  Occasionally you might find a project
where a “Pay for Performance” style of incentive is warranted.  In those cases,
it’s a good idea to negotiate up front what the limits are, so that neither
party to the contract is surprised by having to pay too much / recieve too
little respectively.&lt;/p&gt;

&lt;h4 id=&quot;agreement&quot;&gt;Agreement&lt;/h4&gt;

&lt;p&gt;Finally, the Agreement section is where the SOW becomes a contract, since this
is where the signatures go.  It’s important to get everyone on board with the
all of the details before signing, so don’t sign your portion of it before
you’ve achieved consensus with your client.  A good way to protect yourself
during this period, is to lock the document &amp;amp; track the changes.  While Git is
a good tool for that as far as developers are concerned, the finance and
legal-critters tend to get scared off by it; better just use the tools in
Microsoft Word to do that, it’ll make them happier.  After all, a happy
salesperson is a good ally to have: they will go to bat for you when you need
them.&lt;/p&gt;

&lt;h4 id=&quot;appendix&quot;&gt;Appendix&lt;/h4&gt;

&lt;p&gt;Appended to the end of your SOW should be any supplemental materials that don’t
fit well in the structure above.  Refer to these in your descriptions where
they provide useful insight that would otherwise take several pages to explain.&lt;/p&gt;</content><author><name>Jon Roelofs</name><email>jroelofs@jroelofs.com</email></author><summary type="html">Shutterstock Image Writing good Statements of Work, or “SOW”s is one of the most important keys to success in any long term project. The structure it provides is often unseen until well into projects that have gone off the rails in one way or another. In this article, I’m going to walk through some of the things I’ve learned about writing them over the course of several projects for different clients.</summary></entry><entry><title type="html">About</title><link href="http://jroelofs.com/2017/11/24/about-me/" rel="alternate" type="text/html" title="About" /><published>2017-11-24T00:00:00+00:00</published><updated>2017-11-24T00:00:00+00:00</updated><id>http://jroelofs.com/2017/11/24/about-me</id><content type="html" xml:base="http://jroelofs.com/2017/11/24/about-me/">&lt;p&gt;For organizations that need to develop secure, cryptocurrency solutions in a
rapidly developing marketplace, my unique approach to embedded systems
engineering coupled with my expertise in compiler development ensures that your
products and services will be secure, efficient, and innovative.  Furthermore; my
experience in open source development processes will enable development of
collaborative relationships within the complex cryptocurrency markets ensuring
a role of leadership, innovation, and trust.&lt;/p&gt;

&lt;h2 id=&quot;what-next&quot;&gt;What Next&lt;/h2&gt;
&lt;p&gt;For the past seven years I have been a team player on a variety of cutting edge
technology projects for Intel, AMD, and Siemens.  I am ready for an
engineering-focused leadership role on my next technology development project.
I am excited about cryptocurrencies, and have a strong background in compilers;
combining the two wold be amazing.&lt;/p&gt;

&lt;h2 id=&quot;whats-important&quot;&gt;What’s Important&lt;/h2&gt;
&lt;p&gt;Being able to grow and lead a high performing team.&lt;br /&gt;
Creating innovative technological designs and elegant solutions.&lt;br /&gt;
Working closely with technology enthusiasts and early adopters.&lt;br /&gt;
Supporting business development with my technical expertise.&lt;br /&gt;
Building efficient, honest and collaborative teamwork.&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;competency-categories&quot;&gt;Competency Categories&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Systems Design
    &lt;ul&gt;
      &lt;li&gt;Define customer requirements&lt;/li&gt;
      &lt;li&gt;Translate business requirements into development specs&lt;/li&gt;
      &lt;li&gt;Scope, draft, and negotiate statements of work&lt;/li&gt;
      &lt;li&gt;Concise and clear verbal &amp;amp; written communication skills&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Software/Firmware Architecture
    &lt;ul&gt;
      &lt;li&gt;Architect optimizing compilers for modern embedded processors&lt;/li&gt;
      &lt;li&gt;Expert in SoC/GPU/FPGA acceleration of image processing algorithms&lt;/li&gt;
      &lt;li&gt;Strong understanding of hardware development lifecycles&lt;/li&gt;
      &lt;li&gt;Deep knowledge of blockchain technologies and their respective markets&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Progamming
    &lt;ul&gt;
      &lt;li&gt;Expert-level C/C++/ObjC programming skills and standards knowledge&lt;/li&gt;
      &lt;li&gt;Experienced in cross-platform development&lt;/li&gt;
      &lt;li&gt;Excellent embedded debug and problem solving skills&lt;/li&gt;
      &lt;li&gt;Well versed in compiler tooling for static and dynamic analysis&lt;/li&gt;
      &lt;li&gt;Proficient in Python, Bash, Javascript, and HTML&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Leadership &amp;amp; Innovation
    &lt;ul&gt;
      &lt;li&gt;See the big picture&lt;/li&gt;
      &lt;li&gt;Help people create innovative solutions&lt;/li&gt;
      &lt;li&gt;Respected member of the open source community&lt;/li&gt;
      &lt;li&gt;Proven track record of informative, patient, and accurate support&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Project Management
    &lt;ul&gt;
      &lt;li&gt;Passion for building high quality software on time &amp;amp; on budget&lt;/li&gt;
      &lt;li&gt;Tracks priorities, motivates others&lt;/li&gt;
      &lt;li&gt;Self-starting, self-motivated, self-directed, and self-sufficient&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Jon Roelofs</name><email>jroelofs@jroelofs.com</email></author><summary type="html">For organizations that need to develop secure, cryptocurrency solutions in a rapidly developing marketplace, my unique approach to embedded systems engineering coupled with my expertise in compiler development ensures that your products and services will be secure, efficient, and innovative. Furthermore; my experience in open source development processes will enable development of collaborative relationships within the complex cryptocurrency markets ensuring a role of leadership, innovation, and trust.</summary></entry><entry><title type="html">Private Keys, Dragons, and C++: Part 1</title><link href="http://jroelofs.com/programming/compilers/2017/11/22/keys-dragons-and-cpp-part-1/" rel="alternate" type="text/html" title="Private Keys, Dragons, and C++: Part 1" /><published>2017-11-22T00:00:00+00:00</published><updated>2017-11-22T00:00:00+00:00</updated><id>http://jroelofs.com/programming/compilers/2017/11/22/keys-dragons-and-cpp-part-1</id><content type="html" xml:base="http://jroelofs.com/programming/compilers/2017/11/22/keys-dragons-and-cpp-part-1/">&lt;p&gt;&lt;img src=&quot;/images/llvm_wyvern.png&quot; class=&quot;fit image&quot; /&gt;&lt;/p&gt;
&lt;div align=&quot;right&quot;&gt;
  &lt;h6&gt;LLVM Wyvern, used with permission from
    &lt;a href=&quot;https://llvm.org/Logo.html&quot;&gt;llvm.org&lt;/a&gt;
  &lt;/h6&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Managing the lifetime of private keys in C++ is a difficult task.  From memory
management to compiler optimizations and undefined behavior, everywhere you
turn, there are dragons waiting to break the security of even the most
well-written standards-conforming code, and steal the keys to your castle.  In
this series, I describe common pitfalls when securing data while resident in
memory and the industry-standard strategies for mitigating them.&lt;/p&gt;

&lt;p&gt;In the first article of this series, we’ll examine an example from
&lt;a href=&quot;https://github.com/monero-project&quot;&gt;Monero&lt;/a&gt;. In Monero, one of the ways private
keys are represented is with the following class hierarchy:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_scalar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_scalar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;public_key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_scalar&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;These POD or “Plain Old Data” structs are used all over the codebase as stack
variables. A good representative example is where Monero’s wallet derives the
private key from the user’s Electrum recovery sentence:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WalletImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recover&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;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&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;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;clearStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;m_errorString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_errorString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Electrum seed is empty&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;LOG_ERROR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_errorString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Status_Error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;m_recoveringFromSeed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;old_language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ElectrumWords&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_to_bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;old_language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_errorString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Electrum-style word list failed verification&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Status_Error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_wallet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_seed_language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old_language&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_wallet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&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;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Status_Critical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m_errorString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;what&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m_status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Status_Ok&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;Here the code does some error checking, converts the recovery sentence to
binary form stored in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;crypto::secret_key&lt;/code&gt;, and attempts to generate
the wallet from that. If generation fails and an exception is thrown, it is
handled and the error is logged.&lt;/p&gt;

&lt;p&gt;From a key security standpoint, there are a few different areas where this
code leaks the data of the private keys, in a way that other vulnerabilities
might be able to take advantage of it. Mind you, I’m only describing best
practices here, not an actual exploited vulnerability, so don’t get all doom
and gloom on me.&lt;/p&gt;

&lt;p&gt;The main leakages come from stale data on the stack.  This happens as a result
of C++’s mantra that one only pays in performance for the features they use.
In this case, that relates to the fact that most C++ programs are not so
hypersensitive to security issues, as those that handle financial transactions,
like cryptocurrency wallets.  Optimizing compilers, on the other hand, are very
sensitive to performance concerns, with their goal being to make
standards-comforming code as fast as possible.  The difference in goals here
means that the programmer has to take special care to manage the lifetime of
their data.&lt;/p&gt;

&lt;p&gt;The upside however, is that these are solvable problems.  Let’s talk about the
stack. Distilling the above example down, we have this roughly equivalent
snippet:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;words_to_bytes&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;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;recover&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;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_to_bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FAIL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FAIL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SUCCESS&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;Consider what the stack might look like just after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;words_to_bytes()&lt;/code&gt; call:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;      &lt;span class=&quot;o&quot;&gt;+------------------------------+&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;words_to_bytes&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;+------------------------------+&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;recovery_key&lt;/span&gt;               &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;recover&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'s'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'e'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'k'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'r'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'i'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'t'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;' '&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'i'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'s'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&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;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'4'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'2'&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;heap&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caller&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;       &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-------------------------&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;\&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;+------------------------------+&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;                       &lt;span class=&quot;o&quot;&gt;|&amp;lt;---/&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;caller&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;---------------------------/&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt;                 &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&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;o&quot;&gt;|&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;o&quot;&gt;|&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate()&lt;/code&gt; fails, and we return to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recover()&lt;/code&gt;s caller, the secret data in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recovery_key&lt;/code&gt; will be left as garbage on the stack.  This is undesriable
because it leaves the data exposed to exploits that inspect the stack.  Ideally,
the sensitive data in the keys should be resident in memory for precisely as
long as it is needed, and no longer.&lt;/p&gt;

&lt;p&gt;A similar issue arises in the original example in the thrown path.  There, the
unwinder walks down the stack, perforing any cleanups along the way (i.e.
destructors), leaving everything else untouched for performance reasons.  Since,
as platform maintainers, we want our user’s programs to be quick and responsive,
this is usually a good tradeoff.&lt;/p&gt;

&lt;p&gt;Luckily, we’re writing c++, and we can write a destructor that scrubs the key’s
memory on scope exit, or during one of those unwinder cleanups.  Doing so in C
is still possible, but takes a bit more manual management of the data.  Our
first attempt might look something like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crypto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_scalar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_scalar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;memset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&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;Unfortunately, the compiler decides to be “helpful” and
&lt;a href=&quot;https://godbolt.org/g/n8BLKt&quot;&gt;optimize out the memset&lt;/a&gt;.  Careful readers will
have noticed that I had to defeat the optimizer once already, in order to keep it
from optimizing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doit()&lt;/code&gt; all the way to a nop.  The trick there was to call
through a volatile function pointer.  Since volatile acts as a sort of
optimization barrier as far as the compiler is concerned, it keeps the dead
code elimination pass from touching our call.  In other words, volatile scares
off dragons.  If we use the same trick again, it forces the compiler to
&lt;a href=&quot;https://godbolt.org/g/e1HVTV&quot;&gt;keep the memset call&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This pattern is needed often enough that the standards body for C11 added it
under the name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memset_s&lt;/code&gt;.  On BSD platforms, there is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;explicit_bzero&lt;/code&gt;, and
likewise Windows has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SecureZeroMemory&lt;/code&gt;.  They’re all written with slightly
different tricks, but all with the purpose of forcing the optimizer to leave
seemingly dead code alone.&lt;/p&gt;

&lt;p&gt;As with all such dragons’ dens, tread carefully and ask the advice of your
local compiler expert before writing such code.  It’s really tricky to get
right, and there are many traps you can fall into.  In the second article in
this series, I’ll tell you all about page tables and memory being written to
disk behind your back.&lt;/p&gt;

&lt;p&gt;Stay tuned ‘till next time.&lt;/p&gt;</content><author><name>Jon Roelofs</name><email>jroelofs@jroelofs.com</email></author><summary type="html">LLVM Wyvern, used with permission from llvm.org Managing the lifetime of private keys in C++ is a difficult task. From memory management to compiler optimizations and undefined behavior, everywhere you turn, there are dragons waiting to break the security of even the most well-written standards-conforming code, and steal the keys to your castle. In this series, I describe common pitfalls when securing data while resident in memory and the industry-standard strategies for mitigating them.</summary></entry></feed>