<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Aaron Klotz's Software Blog]]></title>
  <link href="https://dblohm7.ca/atom.xml" rel="self"/>
  <link href="https://dblohm7.ca/"/>
  <updated>2023-06-30T14:17:29-06:00</updated>
  <id>https://dblohm7.ca/</id>
  <author>
    <name><![CDATA[Aaron Klotz]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[DnsQueryEx Needs Love]]></title>
    <link href="https://dblohm7.ca/blog/2022/05/06/dnsqueryex-needs-love/"/>
    <updated>2022-05-06T17:40:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2022/05/06/dnsqueryex-needs-love</id>
    <content type="html"><![CDATA[<p>Recently I&rsquo;ve been doing some work with <a href="https://web.archive.org/web/20220107041650/https://docs.microsoft.com/en-us/windows/win32/api/windns/nf-windns-dnsqueryex"><code>DnsQueryEx</code></a>,
but unfortunately this has been less than pleasant. Not only are there errors in its documentation,
but the API itself contains a bug that IMHO should never have made it to release.</p>

<p>Like many other Win32 APIs, <code>DnsQueryEx</code> is an asynchronous interface that also
supports being called synchronously. Whether their completion mechanism uses an
event object, an APC, an I/O Completion Port, or some other technique,
asynchronous Win32 APIs consistently employ a common convention:</p>

<p>When a caller invokes the API, and that API is able to execute asynchronously,
it returns <code>ERROR_IO_PENDING</code>. On the other hand, when the API fails, the API is
able to immediately satisfy the request, or the API was invoked synchronously,
the function immediately returns the final error code.</p>

<p>For emphasis: <strong>In Win32, most asynchronous APIs reserve the right to complete
synchronously if they are able to immediately satisfy a request.</strong></p>

<p>Enter <code>DnsQueryEx</code>: while its internal implementation follows this convention,
the implementation of its public interface does not!</p>

<p>This is really easy to reproduce (on a fully-updated Windows 10 21H1, at least)
by setting up an asynchronous call to <code>DnsQueryEx</code>, and querying for <code>"localhost"</code>.
The caller must populate the <code>pQueryCompletionCallback</code> field in the <a href="https://web.archive.org/web/20220107051448/https://docs.microsoft.com/en-us/windows/win32/api/windns/ns-windns-dns_query_request"><code>DNS_QUERY_REQUEST</code></a>
structure.</p>

<p><code>DnsQueryEx</code> returns <code>ERROR_SUCCESS</code>. Great, the asynchronous API was able to
immediately fulfill the request!</p>

<p>Everything works according to plan until we examine the <code>pQueryRecords</code> field of
the <a href="https://web.archive.org/web/20220106224652/https://docs.microsoft.com/en-us/windows/win32/api/windns/ns-windns-dns_query_result"><code>DNS_QUERY_RESULT</code></a>
structure. That field is <code>NULL</code>! Every other output from this function points to
a successful query, and yet we receive no results!</p>

<p>I spent several hours pouring over the documentation and attempting different
permutations of the <code>localhost</code> query, however the only way that I could coerce
<code>DnsQueryEx</code> to actually produce the expected output is if I invoked it
synchronously.</p>

<p>I finally determined that this poking around was becoming futile and decided to
examine the disassembly. Here&rsquo;s some (highly-simplified) pseudocode of what I found:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'>  <span class="c1">// Inside DnsQueryEx</span>
</span><span class='line'>  <span class="kt">bool</span> <span class="n">isSynchronous</span> <span class="o">=</span> <span class="n">pQueryRequest</span><span class="o">-&gt;</span><span class="n">pQueryCompletionCallback</span> <span class="o">==</span> <span class="k">nullptr</span><span class="p">;</span>
</span><span class='line'>  <span class="n">PDNS_QUERY_RESULT</span> <span class="n">internalDnsQueryResult</span> <span class="o">=</span> <span class="cm">/*&lt;make private copy of pQueryResults&gt;*/</span><span class="p">;</span>
</span><span class='line'>  <span class="c1">// Call internal implementation. It returns the same error codes as DnsQueryEx</span>
</span><span class='line'>  <span class="n">DWORD</span> <span class="n">win32ErrorCode</span> <span class="o">=</span> <span class="n">Query_PrivateExW</span><span class="p">(</span><span class="n">pQueryRequest</span><span class="p">,</span> <span class="n">internalDnsQueryResult</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">isSynchronous</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">memcpy</span><span class="p">(</span><span class="n">pQueryResult</span><span class="p">,</span> <span class="n">internalDnsQueryResult</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">DNS_QUERY_RESULT</span><span class="p">));</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">win32ErrorCode</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="c1">// Otherwise we&#39;re executing asynchronously, continue on that path...</span>
</span></code></pre></td></tr></table></div></figure>


<p>Based on the background that I outlined above, do you see the bug?</p>

<p>I&rsquo;ll give you a hint: <code>ERROR_IO_PENDING</code>.</p>

<p>See it now?</p>

<p>Okay, here goes: <code>isSynchronous</code> is the wrong condition for determining
whether to copy the internal records to <code>pQueryResult</code> and immediately
return! In fact, I would argue that <code>isSynchronous</code> should not be checked at
all: instead, <code>DnsQueryEx</code> should be checking that <code>win32ErrorCode != ERROR_IO_PENDING</code>!</p>

<p>To add insult to injury, <code>Query_PrivateExW</code> correctly allocates the output
records from the heap, so <code>DnsQueryEx</code> is effectively leaking them.</p>

<p>I&rsquo;m going to try reporting this issue via Feedback Hub, but if any Microsofties
see this, I&rsquo;d appreciate it if you could flag the maintainer of <code>dnsapi.dll</code> and
get this fixed.</p>

<p>I suppose one workaround is to look for a successful call to <code>DnsQueryEx</code> with
<code>NULL</code> records, and then fall back to invoking it synchronously. On the other
hand, that doesn&rsquo;t help with the memory leak.</p>

<p>Another gross, hacky option could be to manually check for special queries like
<code>localhost</code> prior to calling the API, but this isn&rsquo;t exhaustive: there could
be other reasons that <code>Query_PrivateExW</code> decides to execute synchronously.</p>

<p>As you can see, this is a pretty trivial test case, which is why I find this
bug to be so disappointing. I am a big proponent of attributing bugs to an OS
until I have proof otherwise, but the disassembly I encountered was pretty
damning.</p>

<p>Hopefully this gets fixed. Until next time&hellip;</p>

<p><strong>UPDATE:</strong> Microsoft&rsquo;s Tommy Jensen <a href="https://twitter.com/tommyatms/status/1523732124343304193">noted</a>
that this bug has been fixed in Windows 11, but unfortunately <a href="https://twitter.com/tommyatms/status/1523735004009820165">will not</a>
be backported to Windows 10. Thanks to Brad Fitzpatrick for amplifying this post on Twitter.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[All Good Things...]]></title>
    <link href="https://dblohm7.ca/blog/2021/08/13/all-good-things/"/>
    <updated>2021-08-13T14:00:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2021/08/13/all-good-things</id>
    <content type="html"><![CDATA[<p>Today is my final day as an employee of Mozilla Corporation.</p>

<p>My first patch landed in Firefox 19, and my final patch as an employee has
landed in Nightly for Firefox 93.</p>

<p>I&rsquo;ll be moving on to something new in a few weeks&#8217; time, but for now, I&rsquo;d just
like to say this:</p>

<p>My time at Mozilla has made me into a better software developer, a better
leader, and more importantly, a better person.</p>

<p>I&rsquo;d like to thank all the Mozillians whom I have interacted with over the years
for their contributions to making that happen.</p>

<p>I will continue to update this blog with catch-up posts describing my Mozilla
work, though I am unsure what content I will be able to contribute beyond that.
Time will tell!</p>

<p>Until next time&hellip;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2019 Roundup: Part 1 - Porting the DLL Interceptor to AArch64]]></title>
    <link href="https://dblohm7.ca/blog/2021/03/01/2019-roundup-part-1/"/>
    <updated>2021-03-01T12:50:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2021/03/01/2019-roundup-part-1</id>
    <content type="html"><![CDATA[<p>In my continuing efforts to get caught up on discussing my work, I am now
commencing a roundup for 2019. I think I am going to structure this one
slightly differently from the last one: I am going to try to segment this
roundup by project.</p>

<p>Here is an index of all the entries in this series:</p>

<ul>
<li><a href="https://dblohm7.ca/blog/2021/03/01/2019-roundup-part-1/">Part 1 - Porting the DLL Interceptor to AArch64</a> (this post)</li>
</ul>


<h2>Porting the DLL Interceptor to AArch64</h2>

<p>During early 2019, Mozilla was working to port Firefox to run on the new
AArch64 builds of Windows. At our December 2018 all-hands, I brought up the
necessity of including the DLL Interceptor in our porting efforts. Since no deed
goes unpunished, I was put in charge of doing the work! [<em>I&rsquo;m actually kidding
here; this project was right up my alley and I was happy to do it! &ndash; Aaron</em>]</p>

<p>Before continuing, you might want to review my <a href="https://dblohm7.ca/blog/2019/01/23/2018-roundup-q2-part1/">previous entry</a>
describing the Great Interceptor Refactoring of 2018, as this post revisits some
of the concepts introduced there.</p>

<p>Let us review some DLL Interceptor terminology:</p>

<ul>
<li>The <em>target function</em> is the function we want to hook (Note that this is a
distinct concept from a <em>branch target</em>, which is also discussed in this post);</li>
<li>The <em>hook function</em> is our function that we want the intercepted target function
to invoke;</li>
<li>The <em>trampoline</em> is a small chunk of executable code generated by the DLL
interceptor that facilitates calling the target function&rsquo;s original implementation.</li>
</ul>


<p>On more than one occasion I had to field questions about why this work was
even necessary for AArch64: there aren&rsquo;t going to be many injected DLLs in a
Win32 ecosystem running on a shiny new processor architecture! In fact, the DLL
Interceptor is used for more than just facilitating the blocking of injected
DLLs; we also use it for other purposes.</p>

<p>Not all of this work was done in one bug: some tasks were more urgent than
others. I began this project by enumerating our extant uses of the interceptor to
determine which instances were relevant to the new AArch64 port. I threw a record
of each instance into a colour-coded spreadsheet, which proved to be very useful
for tracking progress: Reds were &ldquo;must fix&rdquo; instances, yellows were &ldquo;nice to have&rdquo;
instances, and greens were &ldquo;fixed&rdquo; instances. Coordinating with the milestones
laid out by program management, I was able to assign each instance to a bucket
which would help determine a total ordering for the various fixes. I landed the
first set of changes in <a title="nsWindowsDllInterceptor porting to aarch64" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1526383">bug 1526383</a>, and the second set in <a title="ARM64: nsWindowsDllInterceptor support for Milestone 4 (accessibility)" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1532470">bug 1532470</a>.</p>

<p>It was now time to sit down, download some AArch64 programming manuals, and
take a look at what I was dealing with. While I have been messing around with
x86 assembly since I was a teenager, my first exposure to RISC architectures was
via the <a href="https://en.wikipedia.org/wiki/DLX">DLX architecture</a> introduced by
Hennessy and Patterson in their textbooks. While DLX was crafted specifically
for educational purposes, it served for me as a great point of reference. When
I was a student taking CS 241 at the University of Waterloo, we had to write a
toy compiler that generated DLX code. That experience ended up saving me a lot
of time when looking into AArch64! While the latter is definitely more
sophisticated, I could clearly recognize analogs between the two architectures.</p>

<p>In some ways, targeting a RISC architecture greatly simplifies things: The
DLL Interceptor only needs to concern itself with a small subset of the AArch64
instruction set: loads and branches. In fact, the DLL Interceptor&rsquo;s AArch64
disassembler only looks for <a href="https://searchfox.org/mozilla-central/rev/362676fcadac37f9f585141a244a9a640948794a/mozglue/misc/interceptor/Arm64.cpp#53">nine distinct instructions</a>!
As a bonus, since the instruction length is fixed, we can easily copy over
verbatim any instructions that are not loads or branches!</p>

<p>On the other hand, one thing that <em>increased</em> complexity of the port is that
some branch instructions to relative addresses have maximum offsets. If we must
branch farther than that maximum, we must take alternate measures. For example,
in AArch64, an unconditional branch with an immediate offset must land in the
range of &plusmn;128 MiB from the current program counter.</p>

<p>Why is this a problem, you ask? Well, Detours-style interception must overwrite
the first several instructions of the target function. To write an absolute jump,
we require at least 16 bytes: 4 for an <code>LDR</code> instruction, 4 for a <code>BR</code>
instruction, and another 8 for the 64-bit absolute branch target address.</p>

<p>Unfortunately, target functions may be <em>really short</em>! Some of the target
functions that we need to patch consist only of a single 4-byte instruction!</p>

<p>In this case, our only option for patching the target is to use an immediate <code>B</code>
instruction, but that only works if our hook function falls within that &plusmn;128MiB
limit. If it does not, we need to construct a <em>veneer</em>. A veneer is a special
trampoline whose location falls within the target range of a branch instruction.
Its sole purpose is to provide an unconditional jump to the &ldquo;real&rdquo; desired
branch target that lies outside of the range of the original branch. Using
veneers, we can successfully hook a target function even if it is only one
instruction (ie, 4 bytes) in length, and the hook function lies more than 128MiB
away from it. The AArch64 Procedure Call Standard specifies <code>X16</code> as a volatile
register that is explicitly intended for use by veneers: veneers load an
absolute target address into <code>X16</code> (without needing to worry about whether or
not they&rsquo;re clobbering anything), and then unconditionally jump to it.</p>

<h3>Measuring Target Function Instruction Length</h3>

<p>To determine how many instructions the target function has for us to work with,
we make two passes over the target function&rsquo;s code. The first pass simply counts
how many instructions are available for patching (up to the 4 instruction
maximum needed for absolute branches; we don&rsquo;t really care beyond that).</p>

<p>The second pass actually populates the trampoline, builds the veneer (if
necessary), and patches the target function.</p>

<h3>Veneer Support</h3>

<p>Since the DLL interceptor is already well-equipped to build trampolines, it did
not take much effort to add support for <a href="https://searchfox.org/mozilla-central/rev/362676fcadac37f9f585141a244a9a640948794a/mozglue/misc/interceptor/Arm64.h#193">constructing veneers</a>.
However, <em>where</em> to write out a veneer is just as important as <em>what</em> to write
to a veneer.</p>

<p>Recall that we need our veneer to reside within &plusmn;128 MiB of an immediate
branch. Therefore, we need to be able to exercise some control over where
the trampoline memory for veneers is allocated. Until this point, our trampoline
allocator had no need to care about this; I had to add this capability.</p>

<h4>Adding Range-Aware VM Allocation</h4>

<p>Firstly, I needed to make the <code>MMPolicy</code> classes range-aware: we need to be able
to allocate trampoline space within acceptable distances from branch instructions.</p>

<p>Consider that, as described above, a branch instruction may have limits on the
extents of its target. As data, this is easily formatted as a <em>pivot</em> (ie, the
PC at the location where the branch instruction is encoutered), and a maximum
<em>distance</em> in either direction from that pivot.</p>

<p>On the other hand, range-constrained memory allocation tends to work in terms
of lower and upper bounds. I wrote a conversion method, <code>MMPolicyBase::SpanFromPivotAndDistance</code>,
to convert between the two formats. In addition to format conversion, this method
also constrains resulting bounds such that they are above the 1MiB mark of the
process&#8217; address space (to avoid reserving memory in VM regions that are
sensitive to compatibility concerns), as well as below the maximum allowable
user-mode VM address.</p>

<p>Another issue with range-aware VM allocation is determining the location, within
the allowable range, for the actual VM reservation. Ideally we would like the
kernel&rsquo;s memory manager to choose the best location for us: its holistic view of
existing VM layout (not to mention ASLR) across all processes will provide
superior VM reservations. On the other hand, the Win32 APIs that facilitate this
are specific to Windows 10. When available, <code>MMPolicyInProcess</code> uses <a href="https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc2"><code>VirtualAlloc2</code></a>
and <code>MMPolicyOutOfProcess</code> uses <a href="https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile3"><code>MapViewOfFile3</code></a>.
When we&rsquo;re running on Windows versions where those APIs are not yet available,
we need to fall back to finding and reserving our own range. The
<code>MMPolicyBase::FindRegion</code> method handles this for us.</p>

<p>All of this logic is wrapped up in the <code>MMPolicyBase::Reserve</code> method. In
addition to the desired VM size and range, the method also accepts two functors
that wrap the OS APIs for reserving VM. <code>Reserve</code> uses those functors when
available, otherwise it falls back to <code>FindRegion</code> to manually locate a suitable
reservation.</p>

<p>Now that our memory management primatives were range-aware, I needed to shift my
focus over to our VM sharing policies.</p>

<p>One impetus for the Great Interceptor Refactoring was to enable separate
Interceptor instances to share a unified pool of VM for trampoline memory.
To make this range-aware, I needed to make some additional changes to
<code>VMSharingPolicyShared</code>. It would no longer be sufficient to assume that we
could just share a single block of trampoline VM &mdash; we now needed to make the
shared VM policy capable of potentially allocating multiple blocks of VM.</p>

<p><code>VMSharingPolicyShared</code> now contains a mapping of ranges to VM blocks. If we
request a reservation which an existing block satisfies, we re-use that block.
On the other hand, if we require a range that is yet unsatisfied, then we need to
allocate a new one. I admit that I kind of half-assed the implementation of the
data structure we use for the mapping; I was too lazy to implement a fully-fledged
interval tree. The current implementation is probably &ldquo;good enough,&rdquo; however
it&rsquo;s probably worth fixing at some point.</p>

<p>Finally, I added a new generic class, <code>TrampolinePool</code>, that acts as an
abstraction of a reserved block of VM address space. The main interceptor code
requests a pool by calling the VM sharing policy&rsquo;s <code>Reserve</code> method, then it
uses the pool to retrieve new <code>Trampoline</code> instances to be populated.</p>

<h3>AArch64 Trampolines</h3>

<p>It is much simpler to generate trampolines for AArch64 than it is for x86(-64).
The most noteworthy addition to the <code>Trampoline</code> class is the <code>WriteLoadLiteral</code>
method, which writes an absolute address into the trampoline&rsquo;s literal pool,
followed by writing an <code>LDR</code> instruction referencing that literal into the
trampoline.</p>

<hr />

<p>Thanks for reading! Coming up next time: My Untrusted Modules Opus.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2018 Roundup: H2 - Preparing to Enable the Launcher Process by Default]]></title>
    <link href="https://dblohm7.ca/blog/2021/02/24/2018-roundup-h2/"/>
    <updated>2021-02-24T17:30:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2021/02/24/2018-roundup-h2</id>
    <content type="html"><![CDATA[<p><em>This is the fifth post in my &ldquo;2018 Roundup&rdquo; series. For an index of all entries, please see my
blog entry for <a href="https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1/">Q1</a>.</em></p>

<p>Yes, you are reading the dates correctly: I am posting this over two years after I began this series.
I am trying to get caught up on documenting my past work!</p>

<h3>CI and Developer Tooling</h3>

<p>Given that the launcher process completely changes how our Win32 Firefox builds
start, I needed to update both our CI harnesses, as well as the launcher process
itself. I didn&rsquo;t do much that was particularly noteworthy from a technical
standpoint, but I will mention some important points:</p>

<p>During normal use, the launcher process usually exits immediately after the
browser process is confirmed to have started. This was a deliberate design
decision that I made. Having the launcher process wait for the browser process
to terminate would not do any harm, however I did not want the launcher process
hanging around in Task Manager and being misunderstood by users who are checking
their browser&rsquo;s resource usage.</p>

<p>On the other hand, such a design completely breaks scripts that expect to start
Firefox and be able to synchronously wait for the browser to exit before
continuing! Clearly I needed to provide an opt-in for the latter case, so I added
the <code>--wait-for-browser</code> command-line option. The launcher process also implicitly
enables this mode under a few <a href="https://searchfox.org/mozilla-central/rev/31a3457890b5698af1277413ee9d9bd6c5955183/browser/app/winlauncher/LauncherProcessWin.cpp#92">other scenarios</a>.</p>

<p>Secondly, there is the issue of debugging. Developers were previously used to
attaching to the first <code>firefox.exe</code> process they see and expecting to be debugging
the browser process. With the launcher process enabled by default, this is no
longer the case.</p>

<p>There are few options here:</p>

<ul>
<li>Visual Studio users may install the <a href="https://devblogs.microsoft.com/devops/introducing-the-child-process-debugging-power-tool/">Child Process Debugging Power Tool</a>,
which enables the VS debugger to attach to child processes;</li>
<li>WinDbg users may start their debugger with the <code>-o</code> command-line flag,
or use the <code>Debug child processes also</code> checkbox in the GUI;</li>
<li>I added support for a <code>MOZ_DEBUG_BROWSER_PAUSE</code> environment variable, which
allows developers to set a timeout (in seconds) for the browser process to
print its pid to <code>stdout</code> and wait for a debugger attachment.</li>
</ul>


<h3>Performance Testing</h3>

<p>As I have alluded to in previous posts, I needed to measure the effect of adding
an additional process to the critical path of Firefox startup. Since in-process
testing will not work in this case, I needed to use something that could provide
a holistic view across both launcher and browser processes. I decided to enhance
our existing <code>xperf</code> suite in Talos to support my use case.</p>

<p>I already had prior experience with <code>xperf</code>; I spent a significant part of 2013
working with Joel Maher to put the <code>xperf</code> Talos suite into production. I also
knew that the existing code was not sufficiently generic to be able to handle my
use case.</p>

<p>I threw together a rudimentary <a href="https://github.com/dblohm7/xperf">analysis framework</a>
for working with CSV-exported xperf data. Then, after Joel&rsquo;s review, I vendored
it into <code>mozilla-central</code> and used it to construct an analysis for startup time.
[<em>While a more thorough discussion of this framework is definitely warranted, I
also feel that it is tangential to the discussion at hand; I&rsquo;ll write a dedicated
blog entry about this topic in the future. &ndash; Aaron</em>]</p>

<p>In essence, the analysis considers the following facts when processing an xperf recording:</p>

<ul>
<li>The launcher process will be the first <code>firefox.exe</code> process that runs;</li>
<li>The browser process will be started by the launcher process;</li>
<li>The browser process will fire a <a href="https://searchfox.org/mozilla-central/source/toolkit/components/startup/mozprofilerprobe.mof">session store window restored</a> event.</li>
</ul>


<p>For our analysis, we needed to do the following:</p>

<ol>
<li>Find the event showing the first <code>firefox.exe</code> process being created;</li>
<li>Find the session store window restored event from the second <code>firefox.exe</code> process;</li>
<li>Output the time interval between the two events.</li>
</ol>


<p><a href="https://searchfox.org/mozilla-central/rev/31a3457890b5698af1277413ee9d9bd6c5955183/testing/talos/talos/xtalos/parse_xperf.py#36">This block of code</a>
demonstrates how that analysis is specified using my analyzer framework.</p>

<p>Overall, these test results were quite positive. We saw a very slight but
imperceptible increase in startup time on machines with solid-state drives,
however the security benefits from the launcher process outweigh this very small
regression.</p>

<p>Most interestingly, we saw a signficant <em>improvement</em> in startup time on Windows
10 machines with magnetic hard disks! As I mentioned in Q2 Part 3, I believe
this improvement is due to reduced hard disk seeking thanks to the launcher
process forcing <code>\windows\system32</code> to the front of the dynamic linker&rsquo;s search
path.</p>

<h3>Error and Experimentation Readiness</h3>

<p>By Q3 I had the launcher process in a state where it was built by default into
Firefox, but it was still opt-in. As I have written previously, we needed the
launcher process to gracefully fail even without having the benefit of various
Gecko services such as preferences and the crash reporter.</p>

<h4>Error Propagation</h4>

<p>Firstly, I created a new class, <a href="https://searchfox.org/mozilla-central/rev/31a3457890b5698af1277413ee9d9bd6c5955183/widget/windows/WinHeaderOnlyUtils.h#73"><code>WindowsError</code></a>,
that encapsulates all types of Windows error codes. As an aside, I would strongly
encourage all Gecko developers who are writing new code that invokes Windows APIs
to use this class in your error handling.</p>

<p><code>WindowsError</code> is currently able to store Win32 <code>DWORD</code> error codes, <code>NTSTATUS</code>
error codes, and <code>HRESULT</code> error codes. Internally the code is stored as an
<code>HRESULT</code>, since that type has encodings to support the other two. <code>WindowsError</code>
also provides a method to convert its error code to a localized string for
human-readable output.</p>

<p>As for the launcher process itself, nearly every function in the launcher
process returns a <code>mozilla::Result</code>-based type. In case of error, we return a
<code>LauncherResult</code>, which [<em>as of 2018; this has changed more recently &ndash; Aaron</em>]
is a structure containing the error&rsquo;s source file, line number, and <code>WindowsError</code>
describing the failure.</p>

<h4>Detecting Browser Process Failures</h4>

<p>While all <code>Result</code>s in the launcher process may be indicating a successful
start, we may not yet be out of the woods! Consider the possibility that the
various interventions taken by the launcher process might have somehow impaired
the browser process&#8217; ability to start!</p>

<p>To deal with this situation, the launcher process and the browser process share
code that tracks whether both processes successfully started in sequence.</p>

<p>When the launcher process is started, it checks information recorded about the
previous run. If the browser process previously failed to start correctly, the
launcher process disables itself and proceeds to start the browser process
without any of its typical interventions.</p>

<p>Once the browser has successfully started, it reflects the launcher process
state into telemetry, preferences, and <code>about:support</code>.</p>

<p>Future attempts to start Firefox will bypass the launcher process until the
next time the installation&rsquo;s binaries are updated, at which point we reset and
attempt once again to start with the launcher process. We do this in the hope
that whatever was failing in version <em>n</em> might be fixed in version <em>n + 1</em>.</p>

<p>Note that this update behaviour implies that there is no way to forcibly and
permanently disable the launcher process. This is by design: the error detection
feature is designed to prevent the browser from becoming unusable, not to provide
configurability. The launcher process is a security feature and not something
that we should want users adjusting any more than we would want users to be
disabling the capability system or some other important security mitigation. In
fact, my original roadmap for InjectEject called for eventually removing the
failure detection code if the launcher failure rate ever reached zero.</p>

<h4>Experimentation and Emergency</h4>

<p>The pref reflection built into the failure detection system is bi-directional.
This allowed us to ship a release where we ran a study with a fraction of users
running with the launcher process enabled by default.</p>

<p>Once we rolled out the launcher process at 100%, this pref also served as a
useful &ldquo;emergency kill switch&rdquo; that we could have flipped if necessary.</p>

<p>Fortunately our experiments were successful and we rolled the launcher process
out to release at 100% without ever needing the kill switch!</p>

<p>At this point, this pref should probably be removed, as we no longer need nor
want to control launcher process deployment in this way.</p>

<h4>Error Reporting</h4>

<p>When telemetry is enabled, the launcher process is able to convert its
<code>LauncherResult</code> into a ping which is sent in the background by <code>ping-sender</code>.
When telemetry is disabled, we perform a last-ditch effort to surface the error
by logging details about the <code>LauncherResult</code> failure in the Windows Event Log.</p>

<h3>In Conclusion</h3>

<p>Thanks for reading! This concludes my 2018 Roundup series! There is so much more
work from 2018 that I did for this project that I wish I could discuss, but for
security reasons I must refrain. Nonetheless, I hope you enjoyed this series.
Stay tuned for more roundups in the future!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2018 Roundup: Q2, Part 3 - Fleshing Out the Launcher Process]]></title>
    <link href="https://dblohm7.ca/blog/2021/01/05/2018-roundup-q2-part3/"/>
    <updated>2021-01-05T09:45:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2021/01/05/2018-roundup-q2-part3</id>
    <content type="html"><![CDATA[<p><em>This is the fourth post in my &ldquo;2018 Roundup&rdquo; series. For an index of all entries, please see my
blog entry for <a href="https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1/">Q1</a>.</em></p>

<p>Yes, you are reading the dates correctly: I am posting this nearly two years after I began this series.
I am trying to get caught up on documenting my past work!</p>

<p>Once I had landed the <a href="https://dblohm7.ca/blog/2021/01/04/2018-roundup-q2-part2/">skeletal implementation</a>
of the launcher process, it was time to start making it do useful things.</p>

<h3>Ensuring Medium Integrity</h3>

<p>[<em>For an overview of Windows integrity levels, check out <a href="https://docs.microsoft.com/en-us/windows/win32/secauthz/mandatory-integrity-control">this MSDN page</a> &ndash; Aaron</em>]</p>

<p>Since Windows Vista, security tokens for standard users have run at a medium integrity level (IL) by default.
When UAC is enabled, members of the <code>Administrators</code> group also run as a standard user with a medium IL, with
the additional ability of being able to &ldquo;elevate&rdquo; themselves to a high IL. When UAC is disabled, an administrator
receives a token that always runs at the high integrity level.</p>

<p>Running a process at a high IL is something that is not to be taken lightly: at that level, the process may
alter system settings and access files that would otherwise be restricted by the OS.</p>

<p>While our sandboxed content processes always run at a low IL, I believed that defense-in-depth called for ensuring
that the browser process did not run at a high IL. In particular, I was concerned about cases where elevation
might be accidental. Consider, for example, a hypothetical scenario where a system administrator is running two
open command prompts, one elevated and one not, and they accidentally start Firefox from the one that is elevated.</p>

<p>This was a perfect use case for the launcher process: it detects whether it is running at high IL, and if so,
it launches the browser with medium integrity.</p>

<p>Unfortunately some users prefer to configure their accounts to run at all times as <code>Administrator</code> with high integrity!
This is <em>terrible</em> idea from a security perspective, but it is what it is; in my experience, most users who
run with this configuration do so deliberately, and they have no interest in being lectured about it.</p>

<p>Unfortunately, users running under this account configuration will experience side-effects of the Firefox browser
process running at medium IL. Specifically, a medium IL process is unable to initiate IPC connections with a process
running at a higher IL. This will break features such as drag-and-drop, since even the administrator&rsquo;s shell processes are running
at a higher IL than Firefox.</p>

<p>Being acutely aware of this issue, I included an escape hatch for these users: I implemented a command line option
that prevents the launcher process from de-elevating when running with a high IL. I hate that I needed to do this,
but moral suasion was not going to be an effective technique for solving this problem.</p>

<h3>Process Mitigation Policies</h3>

<p>Another tool that the launcher process enables us to utilize is process mitigation options. Introduced in Windows 8,
the kernel provides several opt-in flags that allows us to add prophylactic policies to our processes in an effort to
harden them against attacks.</p>

<p>Additional flags have been added over time, so we must be careful to only set flags that are supported by the version
of Windows on which we&rsquo;re running.</p>

<p>We could have set some of these policies by calling the
<a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setprocessmitigationpolicy"><code>SetProcessMitigationPolicy</code></a> API.
Unfortunately this API is designed for a process to use on itself once it is already running. This implies that there
is a window of time between process creation and the time that the process enables its mitigations where an attack could occur.</p>

<p>Fortunately, Windows provides a second avenue for setting process mitigation flags: These flags may be set as part of
an attribute list in the <a href="https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-startupinfoexw"><code>STARTUPINFOEX</code></a>
structure that we pass into <code>CreateProcess</code>.</p>

<p>Perhaps you can now see where I am going with this: The launcher process enables us to specify process mitigation flags
for the browser process <em>at the time of browser process creation</em>, thus preventing the aforementioned window of opportunity
for attacks to occur!</p>

<p>While there are other flags that we could support in the future, the initial mitigation policy that I added was the
<a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute"><code>PROCESS_CREATION_MITIGATION_POLICY_IMAGE_LOAD_PREFER_SYSTEM32_ALWAYS_ON</code></a>
flag. [<em>Note that I am only discussing flags applied to the browser process; sandboxed processes receive additional mitigations. &ndash; Aaron</em>]
This flag forces the Windows loader to always use the Windows <code>system32</code> directory as the first directory in its search path,
which prevents library preload attacks. Using this mitigation also gave us an unexpected performance gain on devices with
magnetic hard drives: most of our DLL dependencies are either loaded using absolute paths, or reside in <code>system32</code>. With
<code>system32</code> at the front of the loader&rsquo;s search path, the resulting reduction in hard disk seek times produced a slight but
meaningful decrease in browser startup time! How I made these measurements is addressed in a future post.</p>

<h3>Next Time</h3>

<p>This concludes the Q2 topics that I wanted to discuss. Thanks for reading! Coming up in <a href="https://dblohm7.ca/blog/2021/02/24/2018-roundup-h2/">H2</a>: Preparing to Enable the Launcher Process by Default.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2018 Roundup: Q2, Part 2 - Implementing a Skeletal Launcher Process]]></title>
    <link href="https://dblohm7.ca/blog/2021/01/04/2018-roundup-q2-part2/"/>
    <updated>2021-01-04T15:45:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2021/01/04/2018-roundup-q2-part2</id>
    <content type="html"><![CDATA[<p><em>This is the third post in my &ldquo;2018 Roundup&rdquo; series. For an index of all entries, please see my
blog entry for <a href="https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1/">Q1</a>.</em></p>

<p>Yes, you are reading the dates correctly: I am posting this nearly two years after I began this series.
I am trying to get caught up on documenting my past work!</p>

<p>One of the things I added to Firefox for Windows was a new process called the &ldquo;launcher process.&rdquo;
&ldquo;Bootstrap process&rdquo; would be a better name, but we already used the term &ldquo;bootstrap&rdquo;
for our XPCOM initialization code. Instead of overloading that term and adding potential confusion,
I opted for using &ldquo;launcher process&rdquo; instead.</p>

<p>The launcher process is intended to be the first process that runs when the user starts
Firefox. Its sole purpose is to create the &ldquo;real&rdquo; browser process in a suspended state, set various
attributes on the browser process, resume the browser process, and then self-terminate.</p>

<p>In <a title="Skeletal bootstrap process" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1454745">bug 1454745</a> I implemented an initial skeletal (and opt-in) implementation of the
launcher process.</p>

<p>This seems like pretty straightforward code, right? Na&iuml;vely, one could just rip a <code>CreateProcess</code>
sample off of MSDN and call it day. The actual launcher process implementation is more complicated than
that, for reasons that I will outline in the following sections.</p>

<h3>Built into <code>firefox.exe</code></h3>

<p>I wanted the launcher process to exist as a special &ldquo;mode&rdquo; of <code>firefox.exe</code>, as opposed to a distinct
executable.</p>

<h3>Performance</h3>

<p>By definition, the launcher process lies on the critical path to browser startup. I needed to be very
conscious of how we affect overall browser startup time.</p>

<p>Since the launcher process is built into <code>firefox.exe</code>, I needed to examine that executable&rsquo;s existing
dependencies to ensure that it is not loading any dependent libraries that are not actually needed
by the launcher process. Other than the essential Win32 DLLs <code>kernel32.dll</code> and <code>advapi32.dll</code> (and their
dependencies), I did not want anything else to load. In particular, I wanted to avoid loading <code>user32.dll</code>
and/or <code>gdi32.dll</code>, as this would trigger the initialization of Windows&#8217; GUI facilities, which would be a
huge performance killer. For that reason, most browser-mode library dependencies of <code>firefox.exe</code>
are either delay-loaded or are explicitly loaded via <code>LoadLibrary</code>.</p>

<h3>Safe Mode</h3>

<p>We wanted the launcher process to both respect Firefox&rsquo;s safe mode, as well as alter its behaviour
as necessary when safe mode is requested.</p>

<p>There are multiple mechanisms used by Firefox to detect safe mode. The launcher process detects
all of them except for one: Testing whether the user is holding the shift key. Retrieving keyboard
state would trigger loading of <code>user32.dll</code>, which would harm performance as I described above.</p>

<p>This is not too severe an issue in practice: The browser process itself would still detect the
shift key. Furthermore, while the launcher process may in theory alter its behaviour depending on
whether or not safe mode is requested, none of its behaviour changes are significant enough to
materially affect the browser&rsquo;s ability to start in safe mode.</p>

<p>Also note that, for serious cases where the browser is repeatedly unable to start,
the browser triggers a restart in safe mode via environment variable, which <em>is</em> a mechanism that
the launcher process honours.</p>

<h3>Testing and Automation</h3>

<p>We wanted the launcher process to behave well with respect to automated testing.</p>

<p>The skeletal launcher process that I landed in Q2 included code to pass its console handles
on to the browser process, but there was more work necessary to completely handle this case.
These capabilities were not yet an issue because the launcher process was opt-in at the time.</p>

<h3>Error Recovery</h3>

<p>We wanted the launcher process to gracefully handle failures even though, also by definition, it does not
have access to facilities that internal Gecko code has, such as preferences and the crash reporter.</p>

<p>The skeletal launcher process that I landed in Q2 did not yet utilize any special error handling
code, but this was also not yet an issue because the launcher process was opt-in at this point.</p>

<h3>Next Time</h3>

<p>Thanks for reading! Coming up in <a href="https://dblohm7.ca/blog/2021/01/05/2018-roundup-q2-part3/">Q2, Part 3</a>: Fleshing Out the Launcher Process</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Coming Around Full Circle]]></title>
    <link href="https://dblohm7.ca/blog/2019/09/30/coming-around-full-circle/"/>
    <updated>2019-09-30T10:30:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2019/09/30/coming-around-full-circle</id>
    <content type="html"><![CDATA[<p>One thing about me that most Mozillians don&rsquo;t know is that, when I first applied to work at MoCo,
I had applied to work on the mobile platform. When all was said and done, it was decided at the time
that I would be a better fit for an opening on Taras Glek&rsquo;s platform performance team.</p>

<p>My first day at Mozilla was October 15, 2012 &mdash; I will be celebrating my seventh anniversary at MoCo in
just a couple short weeks! Some people with similar tenures have suggested to me that we are now
&ldquo;old guard,&rdquo; but I&rsquo;m not sure that I feel that way! Anyway, I digress.</p>

<p>The platform performance team eventually evolved into a desktop-focused performance team by late 2013.
By the end of 2015 I had decided that it was time for a change, and by March 2016 I had moved over to
work for Jim Mathies, focusing on Gecko integration with Windows. I ended up spending the next twenty
or so months helping the accessibility team port their Windows implementation over to multiprocess.</p>

<p>Once Firefox Quantum 57 hit the streets, I scoped out and provided technical leadership for the
InjectEject project, whose objective was to tackle some of the root problems with DLL injection that
were causing us grief in Windows-land.</p>

<p>I am proud to say that, over the past three years on Jim&rsquo;s team, I have done the best work of my career.
I&rsquo;d like to thank Brad Lassey (now at Google) for his willingness to bring me over to his group, as well as
Jim, and David Bolter (a11y manager at the time) for their confidence in me. As somebody who had spent most
of his adult life having no confidence in his work whatsoever, their willingness to entrust me with
taking on those risks and responsibilities made an enormous difference in my self esteem and my
professional life.</p>

<p>Over the course of H1 2019, I began to feel restless again. I knew it was time for another change. What
I did not expect was that the agent of that change would be James Willcox, aka Snorp. In Whistler, Snorp
planted the seed in my head that I might want to come over to work with him on GeckoView, within the
mobile group which David was now managing.</p>

<p>The timing seemed perfect, so I made the decision to move to GeckoView. I had to finish tying up some
loose ends with InjectEject, so all the various stakeholders agreed that I&rsquo;d move over at the end of Q3 2019.</p>

<p>Which brings me to this week, when I officially join the GeckoView team, working for Emily Toop. I find
it somewhat amusing that I am now joining the team that evolved from the team that I had originally applied
for back in 2012. I have truly come full circle in my career at Mozilla!</p>

<p>So, what&rsquo;s next?</p>

<ul>
<li><p>I have a couple of InjectEject bugs that are pretty much finished, but just need some polish and
code reviews before landing.</p></li>
<li><p>For the next month or two at least, I am going to continue to meet weekly with Jim to assist with
the transition as he ramps up new staff on the project.</p></li>
<li><p>I still plan to be the module owner for the Firefox Launcher Process and the MSCOM library, however most
day-to-day work will be done by others going forward;</p></li>
<li><p>I will continue to serve as the mozglue peer in charge of the DLL blocklist and DLL interceptor, with
the same caveat.</p></li>
</ul>


<p>Switching over to Android from Windows does not mean that I am leaving my Windows experience at the door; I
would like to continue to be a resource on that front, so I would encourage people to continue to ask me
for advice.</p>

<p>On the other hand, I am very much looking forward to stepping back into the mobile space. My first crack at
mobile was as an intern back in 2003, when I was working with some code that had to run on PalmOS 3.0! I have not
touched Android since I shipped a couple of utility apps back in 2011, so I am looking forward to learning
more about what has changed. I am also looking forward to learning more about native development on Android,
which is something that I never really had a chance to try.</p>

<p>As they used to say on Monty Python&rsquo;s Flying Circus, &ldquo;And now for something completely different!&rdquo;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2018 Roundup: Q2, Part 1 - Refactoring the DLL Interceptor]]></title>
    <link href="https://dblohm7.ca/blog/2019/01/23/2018-roundup-q2-part1/"/>
    <updated>2019-01-23T18:30:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2019/01/23/2018-roundup-q2-part1</id>
    <content type="html"><![CDATA[<p><em>This is the second post in my &ldquo;2018 Roundup&rdquo; series. For an index of all entries, please see my
blog entry for <a href="https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1/">Q1</a>.</em></p>

<p>As I have alluded to <a href="https://dblohm7.ca/blog/2016/01/11/bugs-from-hell-injected-third-party-code-plus-detours-equals-a-bad-time/">previously</a>,
Gecko includes a Detours-style API hooking mechanism for Windows. In Gecko, this code is referred to
as the &ldquo;DLL Interceptor.&rdquo; We use the DLL interceptor to instrument various functions within our own
processes. As a prerequisite for future DLL injection mitigations, I needed to spend a good chunk of
Q2 refactoring this code. While I was in there, I took the opportunity to improve the interceptor&rsquo;s
memory efficiency, thus benefitting the Fission MemShrink project. [<em>When these changes landed, we were
not yet tracking the memory savings, but I will include a rough estimate <a href="#vmsharing">later</a> in this post.</em>]</p>

<h3>A Brief Overview of Detours-style API Hooking</h3>

<p>While many distinct function hooking techniques are used in the Windows ecosystem, the Detours-style
hook is one of the most effective and most popular. While I am not going to go into too many specifics
here, I&rsquo;d like to offer a quick overview. In this description, &ldquo;target&rdquo; is the function being hooked.</p>

<p>Here is what happens when a function is detoured:</p>

<ol>
<li><p>Allocate a chunk of memory to serve as a &ldquo;trampoline.&rdquo; We must be able to adjust the protection
attributes on that memory.</p></li>
<li><p>Disassemble enough of the target to make room for a <code>jmp</code> instruction. On 32-bit x86 processors,
this requires 5 bytes. x86-64 is more complicated, but generally, to <code>jmp</code> to an absolute address, we
try to make room for 13 bytes.</p></li>
<li><p>Copy the instructions from step 2 over to the trampoline.</p></li>
<li><p>At the beginning of the target function, write a <code>jmp</code> to the hook function.</p></li>
<li><p>Append additional instructions to the trampoline that, when executed, will cause the processor to
jump back to the first valid instruction after the <code>jmp</code> written in step 4.</p></li>
<li><p>If the hook function wants to pass control on to the original target function, it calls the
trampoline.</p></li>
</ol>


<p>Note that these steps don&rsquo;t occur <em>exactly</em> in the order specified above; I selected the above ordering
in an effort to simplify my description.</p>

<p>Here is my attempt at visualizing the control flow of a detoured function on x86-64:</p>

<p><img src="https://dblohm7.ca/images/detours_hook.svg"></p>

<h3>Refactoring</h3>

<p>Previously, the DLL interceptor relied on directly manipulating pointers in order to read and write the
various instructions involved in the hook. In <a title="Parameterize memory operations in WindowsDllInterceptor" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1432653">bug 1432653</a> I changed things so that the memory
operations are parameterized based on two orthogonal concepts:</p>

<ul>
<li>In-process vs out-of-process memory access: I wanted to be able to abstract reads and writes such
that we could optionally set a hook in another process from our own.</li>
<li>Virtual memory allocation scheme: I wanted to be able to change how trampoline memory was allocated.
Previously, each instance of <code>WindowsDllInterceptor</code> allocated its own page of memory for trampolines,
but each instance also typically only sets one or two hooks. This means that most of the 4KiB page
was unused. Furthermore, since Windows allocates blocks of pages on a 64KiB boundary, this wasted a
lot of precious virtual address space in our 32-bit builds.</li>
</ul>


<p>By refactoring and parameterizing these operations, we ended up with the following combinations:</p>

<ul>
<li>In-process memory access, each <code>WindowsDllInterceptor</code> instance receives its own trampoline space;</li>
<li>In-process memory access, all <code>WindowsDllInterceptor</code> instances within a module <em>share</em> trampoline space;</li>
<li>Out-of-process memory access, each <code>WindowsDllInterceptor</code> instance receives its own trampoline space;</li>
<li>Out-of-process memory access, all <code>WindowsDllInterceptor</code> instances within a module share trampoline space (currently
not implemented as this option is not particularly useful at the moment).</li>
</ul>


<p>Instead of directly manipulating pointers, we now use instances of <code>ReadOnlyTargetFunction</code>,
<code>WritableTargetFunction</code>, and <code>Trampoline</code> to manipulate our code/data. Those classes in turn use the
memory management and virtual memory allocation policies to perform the actual reading and writing.</p>

<h3>Memory Management Policies</h3>

<p>The interceptor now supports two policies, <code>MMPolicyInProcess</code> and <code>MMPolicyOutOfProcess</code>. Each policy
must implement the following memory operations:</p>

<ul>
<li>Read</li>
<li>Write</li>
<li>Change protection attributes</li>
<li>Reserve trampoline space</li>
<li>Commit trampoline space</li>
</ul>


<p><code>MMPolicyInProcess</code> is implemented using <code>memcpy</code> for read and write, <code>VirtualProtect</code>
for protection attribute changes, and <code>VirtualAlloc</code> for reserving and committing trampoline space.</p>

<p><code>MMPolicyOutOfProcess</code> uses <code>ReadProcessMemory</code> and <code>WriteProcessMemory</code> for read and write. As a perf
optimization, we try to batch reads and writes together to reduce the system call traffic. We obviously
use <code>VirtualProtectEx</code> to adjust protection attributes in the other process.</p>

<p>Out-of-process trampoline reservation and commitment, however, is a bit different and is worth a
separate call-out. We allocate trampoline space using shared memory. It is mapped into the local
process with read+write permissions using <code>MapViewOfFile</code>. The memory is mapped into the remote process
as read+execute using some code that I wrote in <a title="Add out-of-process memory access policies to DLL interceptor" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1451511">bug 1451511</a> that either uses <code>NtMapViewOfSection</code> or
<code>MapViewOfFile2</code>, depending on availability. Individual pages from those chunks are then committed via
<code>VirtualAlloc</code> in the local process and <code>VirtualAllocEx</code> in the remote process. This scheme enables
us to read and write to trampoline memory directly, without needing to do cross-process reads and writes!</p>

<h3>VM Sharing Policies</h3>

<p>The code for these policies is a lot simpler than the code for the memory management policies. We now
have <code>VMSharingPolicyUnique</code> and <code>VMSharingPolicyShared</code>. Each of these policies must implement the
following operations:</p>

<ul>
<li>Reserve space for up to <em>N</em> trampolines of size <em>K</em>;</li>
<li>Obtain a <code>Trampoline</code> object for the next available <em>K</em>-byte trampoline slot;</li>
<li>Return an iterable collection of all extant trampolines.</li>
</ul>


<p><code>VMSharingPolicyShared</code> is actually implemented by delegating to a <code>static</code> instance of
<code>VMSharingPolicyUnique</code>.</p>

<h3>Implications of Refactoring</h3>

<p>To determine the performance implications, I added timings to our DLL Interceptor unit test. I was
very happy to see that, despite the additional layers of abstraction, the C++ compiler&rsquo;s optimizer was
doing its job: There was no performance impact whatsoever!</p>

<p><a name="vmsharing"></a>Once the refactoring was complete, I switched the default VM Sharing Policy for <code>WindowsDllInterceptor</code>
over to <code>VMSharingPolicyShared</code> in <a title="DLL interceptors should share trampoline VM" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1451524">bug 1451524</a>.</p>

<p>Browsing today&rsquo;s <code>mozilla-central</code> tip, I count 14 locations where we instantiate interceptors inside
<code>xul.dll</code>. Given that not all interceptors are necessarily instantiated at once, I am now offering a
worst-case back-of-the-napkin estimate of the memory savings:</p>

<ul>
<li>Each interceptor would likely be consuming 4KiB (most of which is unused) of committed VM. Due to
Windows&#8217; 64 KiB allocation guanularity, each interceptor would be leaving a further 60KiB
of address space in a free but unusable state. Assuming all 14 interceptors were actually instantiated,
they would thus consume a combined 56KiB of committed VM and 840KiB of free but unusable address space.</li>
<li>By sharing trampoline VM, the interceptors would consume only 4KiB combined and waste only 60KiB of
address space, thus yielding savings of 52KiB in committed memory and 780KiB in addressable memory.</li>
</ul>


<h3>Oh, and One More Thing</h3>

<p>Another problem that I discovered during this refactoring was <a title="Repeated failed function hook attempts cause exhaustion of interceptor trampoline space" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1459335">bug 1459335</a>. It turns out that some
of the interceptor&rsquo;s callers were not distinguishing between &ldquo;I have not set this hook yet&rdquo; and &ldquo;I
attempted to set this hook but it failed&rdquo; scenarios. Across several call sites, I discovered that
our code would repeatedly retry to set hooks even when they had previously failed, causing leakage
of trampoline space!</p>

<p>To fix this, I modified the interceptor&rsquo;s interface so that we use one-time initialization APIs to
set hooks; since landing this bug, it is no longer possible for clients of the DLL interceptor to
set a hook that had previously failed to be set.</p>

<p>Quantifying the memory costs of this bug is&hellip; non-trivial, but it suffices to say that fixing
this bug probably resulted in the savings of at least a few hundred KiB in committed VM on
affected machines.</p>

<p>That&rsquo;s it for today&rsquo;s post, folks! Thanks for reading! Coming up in <a href="https://dblohm7.ca/blog/2021/01/04/2018-roundup-q2-part2/">Q2, Part 2</a>: Implementing a Skeletal Launcher Process</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2018 Roundup: Q1 - Learning More About DLLs Injected Into Firefox]]></title>
    <link href="https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1/"/>
    <updated>2019-01-18T17:30:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1</id>
    <content type="html"><![CDATA[<p>I had a very busy 2018. So busy, in fact, that I have not been able to devote any time to actually
discussing what I worked on! I had intended to write these posts during the end of December, but a
hardware failure delayed that until the new year. Alas, here we are in 2019, and I am going to do a
series of retrospectives on last year&rsquo;s work, broken up by quarter.</p>

<p>Here is an index of all the entries in this series:</p>

<ul>
<li><a href="https://dblohm7.ca/blog/2019/01/18/2018-roundup-q1/">Q1 - Overview, Learning More About DLLs Injected into Firefox</a> (this post)</li>
<li><a href="https://dblohm7.ca/blog/2019/01/23/2018-roundup-q2-part1/">Q2, Part 1 - Refactoring the DLL Interceptor</a></li>
<li><a href="https://dblohm7.ca/blog/2021/01/04/2018-roundup-q2-part2/">Q2, Part 2 - Implementing a Skeletal Launcher Process</a></li>
<li><a href="https://dblohm7.ca/blog/2021/01/05/2018-roundup-q2-part3/">Q2, Part 3 - Fleshing Out the Launcher Process</a></li>
<li><a href="https://dblohm7.ca/blog/2021/02/24/2018-roundup-h2/">H2 - Preparing to Enable the Launcher Process by Default</a></li>
</ul>


<h2>Overview</h2>

<p>The general theme of my work in 2018 was dealing with the DLL injection problem: On Windows,
third parties love to forcibly load their DLLs into other processes &mdash; web browsers in particular,
thus making Firefox a primary target.</p>

<p>Many of these libraries tend to alter Firefox processes in ways that hurt the stability and/or performance
of our code; many chemspill releases have gone out over the years to deal with these problems. While I
could rant for hours over this, the fact is that DLL injection is rampant in the ecosystem of Windows
desktop applications and is not going to disappear any time soon. In the meantime, we need to be able
to deal with it.</p>

<p>Some astute readers might be ready to send me an email or post a comment about how ignorant I am about
the new(-ish) process mitigation policies that are available in newer versions of Windows. While those
features are definitely useful, they are not panaceas:</p>

<ul>
<li>We cannot turn on the &ldquo;Extension Point Disable&rdquo; policy for users of assistive technologies; screen
readers rely heavily on DLL injection using <code>SetWindowsHookEx</code> and <code>SetWinEventHook</code>, both of which
are covered by this policy;</li>
<li>We could enable the &ldquo;Microsoft Binary Signature&rdquo; policy, however that requires us to load our own
DLLs first before enabling; once that happens, it is often already too late: other DLLs have already
injected themselves by the time we are able to activate this policy. (Note that this could easily be
solved if this policy were augmented to also permit loading of any DLL signed by the same organization
as that of the process&rsquo;s executable binary, but Microsoft seems to be unwilling to do this.)</li>
<li>The above mitigations are not universally available. They do not help us on Windows 7.</li>
</ul>


<p>For me, Q1 2018 was all about gathering better data about injected DLLs.</p>

<h2>Learning More About DLLs Injected into Firefox</h2>

<p>One of our major pain points over the years of dealing with injected DLLs has been that the vendor of
the DLL is not always apparent to us. In general, our crash reports and telemetry pings only include
the leaf name of the various DLLs on a user&rsquo;s system. This is intentional on our part: we want to
preserve user privacy. On the other hand, this severely limits our ability to determine which party
is responsible for a particular DLL.</p>

<p>One avenue for obtaining this information is to look at any digital signature that is embedded in the
DLL. By examining the certificate that was used to sign the binary, we can extract the organization
of the cert&rsquo;s owner and include that with our crash reports and telemetry.</p>

<p>In <a title="Include authenticode cert information with crash reports" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1430857">bug 1430857</a> I wrote a bunch of code that enables us to extract that information from signed
binaries using the Windows Authenticode APIs. Originally, in that bug, all of that signature extraction
work happened from within the browser itself, while it was running: It would gather the cert information
on a background thread while the browser was running, and include those annotations in a subsequent
crash dump, should such a thing occur.</p>

<p>After some reflection, I realized that I was not gathering annotations in the right place. As an example,
what if an injected DLL were to trigger a crash before the background thread had a chance to grab
that DLL&rsquo;s cert information?</p>

<p>I realized that the best place to gather this information was in a post-processing step after the
crash dump had been generated, and in fact we already had the right mechanism for doing so: the
<code>minidump-analyzer</code> program was already doing post-processing on Firefox crash dumps before sending
them back to Mozilla. I moved the signature extraction and crash annotation code out of Gecko and
into the analyzer in <a title="Cert annotation performance and reliability improvements" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1436845">bug 1436845</a>.</p>

<p>(As an aside, while working on the <code>minidump-analyzer</code>, I found some problems with how it handled
command line arguments: it was assuming that <code>main</code> passes its <code>argv</code> as UTF-8, which is not true on
Windows. I fixed those issues in <a title="Minidump analyzer assuming utf-8 command-line arguments on Windows" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1437156">bug 1437156</a>.)</p>

<p>In <a title="Add module cert info to modules ping" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1434489">bug 1434489</a> I also ended up adding this information to the &ldquo;modules ping&rdquo; that we have in
telemetry; IIRC this ping is only sent weekly. When the modules ping is requested, we gather the
module cert info asynchronously on a background thread.</p>

<p>Finally, I had to modify Socorro (the back-end for <a href="https://crash-stats.mozilla.com">crash-stats</a>) to
be able to understand the signature annotations and be able to display them via <a title="Add module cert info to crash report "Modules" tab" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1434495">bug 1434495</a>. This
required two commits: one to modify the Socorro stackwalker to merge the module signature information
into the full crash report, and another to add a &ldquo;Signed By&rdquo; column to every report&rsquo;s &ldquo;Modules&rdquo; tab to
display the signature information (Note that this column is only present when at least one module in
a particular crash report contains signature information).</p>

<p>The end result was very satisfying: Most of the injected DLLs in our Windows crash reports are signed,
so it is now much easier to identify their vendors!</p>

<p>This project was very satisifying for me in many ways: First of all, surfacing this information was an
itch that I had been wanting to scratch for quite some time. Secondly, this really was a &ldquo;full stack&rdquo;
project, touching everything from extracting signature info from binaries using C++, all the way up to
writing some back-end code in Python and a little touch of front-end stuff to surface the data in the
web app.</p>

<p>Note that, while this project focused on Windows because of the severity of the library injection
problem on that platform, it would be easy enough to reuse most of this code for macOS builds as well;
the only major work for the latter case would be for extracting signature information from a dylib.
This is not currently a priority for us, though.</p>

<p>Thanks for reading! Coming up in <a href="https://dblohm7.ca/blog/2019/01/23/2018-roundup-q2-part1/">Q2</a>:
Refactoring the DLL Interceptor!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Legacy Firefox Extensions and "Userspace"]]></title>
    <link href="https://dblohm7.ca/blog/2017/11/16/legacy-firefox-extensions-and-userspace/"/>
    <updated>2017-11-16T13:30:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2017/11/16/legacy-firefox-extensions-and-userspace</id>
    <content type="html"><![CDATA[<p>This week&rsquo;s release of Firefox Quantum has prompted all kinds of feedback, both
positive and negative. That is not surprising to anybody &mdash; any software that
has a large number of users is going to be a topic for discussion, especially
when the release in question is undoubtedly a watershed.</p>

<p>While I have <a href="https://dblohm7.ca/blog/2015/08/30/on-webextensions/">previously</a>
blogged about the transition to WebExtensions, now that we have actually passed
through the cutoff for legacy extensions, I have decided to add some new
commentary on the subject.</p>

<p>One analogy that has been used in the discussion of the extension ecosystem is
that of kernelspace and userspace. The crux of the analogy is that Gecko is
equivalent to an operating system kernel, and thus extensions are the user-mode
programs that run atop that kernel. The argument then follows that Mozilla&rsquo;s
deprecation and removal of legacy extension capabilities is akin to &ldquo;breaking&rdquo;
userspace. [<em>Some people who say this are using the same tone as Linus does
whenever he eviscerates Linux developers who break userspace, which is neither
productive nor welcomed by anyone, but I digress.</em>] Unfortunately, that analogy
simply does not map to the legacy extension model.</p>

<h2>Legacy Extensions as Kernel Modules</h2>

<p>The most significant problem with the userspace analogy is that legacy extensions
effectively meld with Gecko and become part of Gecko itself. If we accept the
premise that Gecko is like a monolithic OS kernel, then we must also accept that
the analogical equivalent of loading arbitrary code into that kernel, is the
kernel module. Such components are loaded into the kernel and effectively become
part of it. Their code runs with full privileges. They break whenever
significant changes are made to the kernel itself.</p>

<p>Sound familiar?</p>

<p>Legacy extensions were akin to kernel modules. When there is no abstraction,
there can be no such thing as userspace. This is precisely the problem that
WebExtensions solves!</p>

<h2>Building Out a Legacy API</h2>

<p>Maybe somebody out there is thinking, &ldquo;well what if you took all the APIs that
legacy extensions used, turned that into a &lsquo;userspace,&rsquo; and then just left that
part alone?&rdquo;</p>

<p>Which APIs? Where do we draw the line? Do we check the code coverage for every
legacy addon in AMO and use that to determine what to include?</p>

<p>Remember, there was no abstraction; installed legacy addons are fused to Gecko.
If we pledge not to touch anything that legacy addons might touch, then we
cannot touch anything at all.</p>

<p>Where do we go from here? Freeze an old version of Gecko and host an entire copy
of it inside web content? Compile it to WebAssembly? [<em>Oh God, what have I done?</em>]</p>

<p>If <em>that&rsquo;s</em> not a maintenance burden, I don&rsquo;t know what is!</p>

<h2>A Kernel Analogy for WebExtensions</h2>

<p>Another problem with the legacy-extensions-as-userspace analogy is that it leaves
awkward room for web content, whose API is abstract and well-defined. I do not
think that it is appropriate to consider web content to be equivalent to a
sandboxed application, as sandboxed applications use the same (albeit restricted)
API as normal applications. I would suggest that the presence of WebExtensions
gives us a better kernel analogy:</p>

<ul>
<li>Gecko is the kernel;</li>
<li>WebExtensions are privileged user applications;</li>
<li>Web content runs as unprivileged user applications.</li>
</ul>


<h2>In Conclusion</h2>

<p>Declaring that legacy extensions are userspace does not make them so. The way that
the technology actually worked defies the abstract model that the analogy
attempts to impose upon it. On the other hand, we can use the failure of that
analogy to explain why WebExtensions are important and construct an extension
ecosystem that <em>does</em> fit with that analogy.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Win32 Gotchas]]></title>
    <link href="https://dblohm7.ca/blog/2017/07/17/win32-gotchas/"/>
    <updated>2017-07-17T12:00:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2017/07/17/win32-gotchas</id>
    <content type="html"><![CDATA[<p>For the second time since I have been at Mozilla I have encountered a situation
where hooks are called for notifications of a newly created window, but that
window has not yet been initialized properly, causing the hooks to behave badly.</p>

<p>The first time was inside our window neutering code in IPC, while the second
time was in our accessibility code.</p>

<p>Every time I have seen this, there is code that follows this pattern:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'><span class="n">HWND</span> <span class="n">hwnd</span> <span class="o">=</span> <span class="n">CreateWindowEx</span><span class="p">(</span><span class="cm">/* ... */</span><span class="p">);</span>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="n">hwnd</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// Do some follow-up initialization to hwnd (Using SetProp as an example):</span>
</span><span class='line'>  <span class="n">SetProp</span><span class="p">(</span><span class="n">hwnd</span><span class="p">,</span> <span class="s">&quot;Foo&quot;</span><span class="p">,</span> <span class="n">bar</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This seems innocuous enough, right?</p>

<p>The problem is that <code>CreateWindowEx</code> calls hooks. If those hooks then try to do
something like <code>GetProp(hwnd, "Foo")</code>, that call is going to fail because the
&ldquo;Foo&rdquo; prop has not yet been set.</p>

<p>The key takeaway from this is that, if you are creating a new window, you must
do any follow-up initialization from within your window proc&rsquo;s <code>WM_CREATE</code>
handler. This will guarantee that your window&rsquo;s initialization will have
completed before any hooks are called.</p>

<p>You might be thinking, &ldquo;But I don&rsquo;t set any hooks!&rdquo; While this may be true, you
must not forget about hooks set by third-party code.</p>

<p>&ldquo;But those hooks won&rsquo;t know anything about my program&rsquo;s internals, right?&rdquo;</p>

<p>Perhaps, perhaps not. But when those hooks fire, they give third-party software
the opportunity to run. In some cases, those hooks might even cause the thread
to <em>reenter your own code</em>. Your window had better be completely initialized
when this happens!</p>

<p>In the case of my latest discovery of this issue in <a title="Window emulation needs to SetProp inside WM_CREATE" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1380471">bug 1380471</a>, I made it
possible to use a C++11 lambda to simplify this pattern.</p>

<p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms632680.aspx"><code>CreateWindowEx</code></a>
accepts a <code>lpParam</code> parameter which is then passed to the <code>WM_CREATE</code> handler
as the <code>lpCreateParams</code> member of a <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms632603.aspx"><code>CREATESTRUCT</code></a>.</p>

<p>By setting <code>lpParam</code> to a pointer to a <code>std::function&lt;void(HWND)&gt;</code>, we may then
supply any callable that we wish for follow-up window initialization.</p>

<p>Using the previous code sample as a baseline, this allows me to revise the code
to safely set a property like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'><span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">(</span><span class="n">HWND</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">onCreate</span><span class="p">([](</span><span class="n">HWND</span> <span class="n">aHwnd</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">void</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">SetProp</span><span class="p">(</span><span class="n">aHwnd</span><span class="p">,</span> <span class="s">&quot;Foo&quot;</span><span class="p">,</span> <span class="n">bar</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'><span class="n">HWND</span> <span class="n">hwnd</span> <span class="o">=</span> <span class="n">CreateWindowEx</span><span class="p">(</span><span class="cm">/* ... */</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">onCreate</span><span class="p">);</span>
</span><span class='line'><span class="c1">// At this point is already too late to further initialize hwnd!</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that since <code>lpParam</code> is always passed during <code>WM_CREATE</code>, which always fires
before <code>CreateWindowEx</code> returns, it is safe for <code>onCreate</code> to live on the stack.</p>

<p>I liked this solution for the a11y case because it preserved the locality of
the initialization code within the function that called <code>CreateWindowEx</code>; the
window proc for this window is implemented in another source file and the
follow-up initialization depends on the context surrounding the <code>CreateWindowEx</code>
call.</p>

<p>Speaking of window procs, here is how that window&rsquo;s <code>WM_CREATE</code> handler invokes
the callable:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'><span class="k">switch</span> <span class="p">(</span><span class="n">uMsg</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">case</span> <span class="nl">WM_CREATE</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">auto</span> <span class="n">createStruct</span> <span class="o">=</span> <span class="k">reinterpret_cast</span><span class="o">&lt;</span><span class="n">CREATESTRUCT</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">lParam</span><span class="p">);</span>
</span><span class='line'>    <span class="k">auto</span> <span class="n">createProc</span> <span class="o">=</span> <span class="k">reinterpret_cast</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">(</span><span class="n">HWND</span><span class="p">)</span><span class="o">&gt;*&gt;</span><span class="p">(</span>
</span><span class='line'>      <span class="n">createStruct</span><span class="o">-&gt;</span><span class="n">lpCreateParams</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">createProc</span> <span class="o">&amp;&amp;</span> <span class="o">*</span><span class="n">createProc</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="p">(</span><span class="o">*</span><span class="n">createProc</span><span class="p">)(</span><span class="n">hwnd</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="c1">// ...</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>TL;DR:</strong> If you see a pattern where further initialization work is being done
on an <code>HWND</code> after a <code>CreateWindowEx</code> call, move that initialization code to your
window&rsquo;s <code>WM_CREATE</code> handler instead.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why I Prefer Using CRITICAL_SECTIONs for Mutexes in Windows Nightly Builds]]></title>
    <link href="https://dblohm7.ca/blog/2017/06/12/why-i-prefer-using-critical-sections-for-mutexes-in-windows-nightly-builds/"/>
    <updated>2017-06-12T15:50:43-06:00</updated>
    <id>https://dblohm7.ca/blog/2017/06/12/why-i-prefer-using-critical-sections-for-mutexes-in-windows-nightly-builds</id>
    <content type="html"><![CDATA[<p>In the past I have argued that our Nightly builds, both debug and release, should
use <code>CRITICAL_SECTION</code>s (with full debug info) for our implementation of
<code>mozilla::Mutex</code>. I&rsquo;d like to illustrate some reasons why this is so useful.</p>

<h2>They enable more utility in WinDbg extensions</h2>

<p>Every time you initialize a <code>CRITICAL_SECTION</code>, Windows inserts the CS&rsquo;s
debug info into a process-wide linked list. This enables their discovery by
the Windows debugging engine, and makes the <code>!cs</code>, <code>!critsec</code>, and <code>!locks</code>
commands more useful.</p>

<h2>They enable profiling of their initialization and acquisition</h2>

<p>When the &ldquo;Create user mode stack trace database&rdquo; gflag is enabled, Windows
records the call stack of the thread that called <code>InitializeCriticalSection</code>
on that CS. Windows also records the call stack of the owning thread once
it has acquired the CS. This can be very useful for debugging deadlocks.</p>

<h2>They track their contention counts</h2>

<p>Since every CS has been placed in a process-wide linked list, we may now ask
the debugger to dump statistics about every live CS in the process. In
particular, we can ask the debugger to output the contention counts for each
CS in the process. After running a workload against Nightly, we may then take
the contention output, sort it descendingly, and be able to determine which
<code>CRITICAL_SECTION</code>s are the most contended in the process.</p>

<p>We may then want to more closely inspect the hottest CSes to determine whether
there is anything that we can do to reduce contention and all of the extra
context switching that entails.</p>

<h2>In Summary</h2>

<p>When we use <code>SRWLOCK</code>s or initialize our <code>CRITICAL_SECTION</code>s with the
<code>CRITICAL_SECTION_NO_DEBUG_INFO</code> flag, we are denying ourselves access to this
information. That&rsquo;s fine on release builds, but on Nightly I think it is worth
having around. While I realize that most Mozilla developers have not used this
until now (otherwise I would not be writing this blog post), this rich debugger
info is one of those things that you do not miss until you do not have it.</p>

<p>For further reading about critical section debug info, check out
<a href="https://web.archive.org/web/20150419055323/https://msdn.microsoft.com/en-us/magazine/cc164040.aspx">this</a>
archived article from MSDN Magazine.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Asynchronous Plugin Initialization: Requiem]]></title>
    <link href="https://dblohm7.ca/blog/2017/04/07/asynchronous-plugin-initialization-requiem/"/>
    <updated>2017-04-07T15:00:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2017/04/07/asynchronous-plugin-initialization-requiem</id>
    <content type="html"><![CDATA[<p>My colleague <del>bsmedberg</del> njn is going to be removing asynchronous plugin
initialization in <a title="Remove support for async plugin init" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1352575">bug 1352575</a>. Sadly the feature never became solid enough
to remain enabled on release, so we cut our losses and cancelled the project
early in 2016. Now that code is just a bunch of dead weight. With the
deprecation of non-Flash NPAPI plugins in Firefox 52, our developers are now
working on simplifying the remaining NPAPI code as much as possible.</p>

<p>Obviously the removal of that code does not prevent me from discussing some of
the more interesting facets of that work.</p>

<p>Today I am going to talk about how async plugin init worked when web content
attempted to access a property on a plugin&rsquo;s scriptable object, when that
plugin had not yet completed its asynchronous initialization.</p>

<p>As <a href="https://developer.mozilla.org/en-US/docs/Plugins/Guide/Scripting_plugins">described on MDN</a>,
the DOM queries a plugin for scriptability by calling <code>NPP_GetValue</code> with the
<code>NPPVpluginScriptableNPObject</code> constant. With async plugin init, we did not
return the true NPAPI scriptable object back to the DOM. Instead we returned
a surrogate object. This meant that we did not need to synchronously wait for
the plugin to initialize before returning a result back to the DOM.</p>

<p>If the DOM subsequently called into that surrogate object, the surrogate would
be forced to synchronize with the plugin. There was a limit on how much fakery
the async surrogate could do once the DOM needed a definitive answer &mdash; after
all, the NPAPI itself is entirely synchronous. While you may question whether
the asynchronous surrogate actually bought us any responsiveness, performance
profiles and measurements that I took at the time did indeed demonstrate that
the asynchronous surrogate did buy us enough additional concurrency to make it
worthwhile. A good number of plugin instantiations were able to complete in
time before the DOM had made a single invocation on the surrogate.</p>

<p>Once the surrogate object had synchronized with the plugin, it would then mostly
act as a pass-through to the plugin&rsquo;s real NPAPI scriptable object, with one
notable exception: property accesses.</p>

<p>The reason for this is not necessarily obvious, so allow me to elaborate:</p>

<p>The DOM usually sets up a scriptable object as follows:</p>

<pre><samp>
this.__proto__.__proto__.__proto__
</samp></pre>


<ul>
<li>Where <code>this</code> is the WebIDL object (ie, content&rsquo;s <code>&lt;embed&gt;</code> element);</li>
<li>Whose prototype is the NPAPI scriptable object;</li>
<li>Whose prototype is the shared WebIDL prototype;</li>
<li>Whose prototype is <code>Object.prototype</code>.</li>
</ul>


<p>NPAPI is reentrant (some might say <em>insanely</em> reentrant). It is possible (and
indeed common) for a plugin to set properties on the WebIDL object from within
the plugin&rsquo;s <code>NPP_New</code>.</p>

<p>Suppose that the DOM tries to access a property on the plugin&rsquo;s WebIDL object
that is normally set by the plugin&rsquo;s <code>NPP_New</code>. In the asynchronous case, the
plugin&rsquo;s initialization might still be in progress, so that property might not
yet exist.</p>

<p>In the case where the property does not yet exist on the WebIDL object, JavaScript
fails to retrieve an &ldquo;own&rdquo; property. It then moves on to the first prototype
and attempts to resolve the property on that. As outlined above, this prototype
would actually be the async surrogate. The async surrogate would then be in a
situation where it must absolutely produce a definitive result, so this would
trigger synchronization with the plugin. At this point the plugin would be
guaranteed to have finished initializing.</p>

<p>Now we have a problem: JS was already examining the NPAPI scriptable object when
it blocked to synchronize with the plugin. Meanwhile, the plugin went ahead and
set properties (including the one that we&rsquo;re interested in) on the WebIDL object.
By the time that JS execution resumes, it would already be looking too far up the
prototype chain to see those new properties!</p>

<p>The surrogate needed to be aware of this when it synchronized with the plugin
during a property access. If the plugin had already completed its initialization
(thus rendering synchronization unnecessary), the surrogate would simply pass the
property access on to the real NPAPI scriptable object. On the other hand, if a
synchronization was performed, the surrogate would first retry the WebIDL object
by querying for the WebIDL object&rsquo;s &ldquo;own&rdquo; properties, and return the own property
if it now existed. If no own property existed on the WebIDL object, then the
surrogate would revert to its &ldquo;pass through all the things&rdquo; behaviour.</p>

<p>If I hadn&rsquo;t made the asynchronous surrogate scriptable object do that, we would
have ended up with a strange situation where the DOM&rsquo;s initial property access
on an embed could fail non-deterministically during page load.</p>

<p>That&rsquo;s enough chatter for today. I enjoy blogging about my crazy hacks that make
the impossible, umm&hellip; possible, so maybe I&rsquo;ll write some more of these in the
future.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New Team, New Project]]></title>
    <link href="https://dblohm7.ca/blog/2016/04/06/new-team-new-project/"/>
    <updated>2016-04-06T21:00:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2016/04/06/new-team-new-project</id>
    <content type="html"><![CDATA[<p>In February of this year I switched teams: After 3+ years on Mozilla&rsquo;s
Performance Team, and after having the word &ldquo;performance&rdquo; in my job description
in some form or another for several years prior to that, I decided that it was
time for me to move on to new challenges. Fortunately the Platform org was
willing to have me set up shop under the (e10s|sandboxing|platform integration)
umbrella.</p>

<p>I am pretty excited about this new role!</p>

<p>My first project is to sort out the accessibility situation under Windows e10s.
This started back at Mozlando last December. A number of engineers from across
the Platform org, plus me, got together to brainstorm. Not too long after we had
all returned home, I ended up making a suggestion on an email thread that has
evolved into the core concept that I am currently attempting. As is typical at
Mozilla, no deed goes unpunished, so I have been asked to flesh out my ideas.
An overview of this plan is available on the <a href="https://wiki.mozilla.org/Electrolysis/Accessibility/Windows">wiki</a>.</p>

<p>My hope is that I&rsquo;ll be able to deliver a working, &ldquo;version 0.9&rdquo; type of demo
in time for our London all-hands at the end of Q2. Hopefully we will be able to
deliver on that!</p>

<h2>Some Additional Notes</h2>

<p>I am using this section of the blog post to make some additional notes. I don&rsquo;t
feel that these ideas are strong enough to commit to a wiki yet, but I do want
them to be publicly available.</p>

<p>Once concern that our colleagues at NVAccess have identified is that the
current COM interfaces are too chatty; this is a major reason why screen
readers frequently inject libraries into the Firefox address space. If we serve
our content a11y objects as remote COM objects, there is concern that performance
would suffer. This concern is not due to latency, but rather due to frequency
of calls; one function call does not provide sufficient information to the a11y
client. As a result, multiple round trips are required to fetch all of the
information that is required for a particular DOM node.</p>

<p>My gut feeling about this is that this is probably a legitimate concern, however
we cannot make good decisions without quantifying the performance. My plan going
forward is to proceed with a na&iuml;ve implementation of COM remoting to start,
followed by work on reducing round trips as necessary.</p>

<h2>Smart Proxies</h2>

<p>One idea that was discussed is the idea of the content process speculatively
sending information to the chrome process that might be needed in the future.
For example, if we have an <code>IAccessible</code>, we can expect that multiple properties
will be queried off that interface. A smart proxy could ship that data across
the RPC channel during marshaling so that querying that additional information
does not require additional round trips.</p>

<p>COM makes this possible using &ldquo;handler marshaling.&rdquo; I have dug up some
information about how to do this and am posting it here for posterity:</p>

<p><a href="https://www.microsoft.com/msj/0599/com/com0599.aspx">House of COM, May 1999 <em>Microsoft Systems Journal</em></a>;<br/>
<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683786.aspx">Implementing and Activating a Handler with Extra Data Supplied by Server</a> on MSDN;<br/>
Wicked Code, August 2000 <em>MSDN Magazine</em>. This is not available on the MSDN Magazine website but I have an archived copy on CD-ROM.<br/></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New Mozdbgext Command: !iat]]></title>
    <link href="https://dblohm7.ca/blog/2016/02/11/new-mozdbgext-command-iat/"/>
    <updated>2016-02-11T18:30:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2016/02/11/new-mozdbgext-command-iat</id>
    <content type="html"><![CDATA[<p>As of today I have added a new command to <code>mozdbgext</code>: <code>!iat</code>.</p>

<p>The syntax is pretty simple:</p>

<p><code>!iat &lt;hexadecimal address&gt;</code></p>

<p>This address shouldn&rsquo;t be just any pointer; it should be the address of an
entry in the current module&rsquo;s import address table (IAT). These addresses
are typically very identifiable by the <code>_imp_</code> prefix in their symbol names.</p>

<p>The purpose of this extension is to look up the name of the DLL from whom the
function is being imported. Furthermore, the extension checks the expected
target address of the import with the actual target address of the import. This
allows us to detect API hooking via IAT patching.</p>

<h3>An Example Session</h3>

<p>I fired up a local copy of Nightly, attached a debugger to it, and dumped the
call stack of its main thread:</p>

<pre><samp>
 # ChildEBP RetAddr
00 0018ecd0 765aa32a ntdll!NtWaitForMultipleObjects+0xc
01 0018ee64 761ec47b KERNELBASE!WaitForMultipleObjectsEx+0x10a
02 0018eecc 1406905a USER32!MsgWaitForMultipleObjectsEx+0x17b
03 0018ef18 1408e2c8 xul!mozilla::widget::WinUtils::WaitForMessage+0x5a
04 0018ef84 13fdae56 xul!nsAppShell::ProcessNextNativeEvent+0x188
05 0018ef9c 13fe3778 xul!nsBaseAppShell::DoProcessNextNativeEvent+0x36
06 0018efbc 10329001 xul!nsBaseAppShell::OnProcessNextEvent+0x158
07 0018f0e0 1038e612 xul!nsThread::ProcessNextEvent+0x401
08 0018f0fc 1095de03 xul!NS_ProcessNextEvent+0x62
09 0018f130 108e493d xul!mozilla::ipc::MessagePump::Run+0x273
0a 0018f154 108e48b2 xul!MessageLoop::RunInternal+0x4d
0b 0018f18c 108e448d xul!MessageLoop::RunHandler+0x82
0c 0018f1ac 13fe78f0 xul!MessageLoop::Run+0x1d
0d 0018f1b8 14090f07 xul!nsBaseAppShell::Run+0x50
0e 0018f1c8 1509823f xul!nsAppShell::Run+0x17
0f 0018f1e4 1514975a xul!nsAppStartup::Run+0x6f
10 0018f5e8 15146527 xul!XREMain::XRE_mainRun+0x146a
11 0018f650 1514c04a xul!XREMain::XRE_main+0x327
12 0018f768 00215c1e xul!XRE_main+0x3a
13 0018f940 00214dbd firefox!do_main+0x5ae
14 0018f9e4 0021662e firefox!NS_internal_main+0x18d
15 0018fa18 0021a269 firefox!wmain+0x12e
16 0018fa60 76e338f4 firefox!__tmainCRTStartup+0xfe
17 0018fa74 77d656c3 KERNEL32!BaseThreadInitThunk+0x24
18 0018fabc 77d6568e ntdll!__RtlUserThreadStart+0x2f
19 0018facc 00000000 ntdll!_RtlUserThreadStart+0x1b
</samp></pre>


<p>Let us examine the code at frame 3:</p>

<pre><samp>
14069042 6a04            push    4
14069044 68ff1d0000      push    1DFFh
14069049 8b5508          mov     edx,dword ptr [ebp+8]
1406904c 2b55f8          sub     edx,dword ptr [ebp-8]
1406904f 52              push    edx
14069050 6a00            push    0
14069052 6a00            push    0
14069054 ff159cc57d19    call    dword ptr [xul!_imp__MsgWaitForMultipleObjectsEx (197dc59c)]
1406905a 8945f4          mov     dword ptr [ebp-0Ch],eax
</samp></pre>


<p>Notice the function call to <code>MsgWaitForMultipleObjectsEx</code> occurs indirectly;
the call instruction is referencing a pointer within the <code>xul.dll</code> binary
itself. This is the IAT entry that corresponds to that function.</p>

<p>Now, if I load <code>mozdbgext</code>, I can take the address of that IAT entry and execute
the following command:</p>

<pre><samp>
0:000> !iat 0x197dc59c
Expected target: USER32.dll!MsgWaitForMultipleObjectsEx
Actual target: USER32!MsgWaitForMultipleObjectsEx+0x0
</samp></pre>


<p><code>!iat</code> has done two things for us:</p>

<ol>
<li>It did a reverse lookup to determine the module and function name for the
import that corresponds to that particular IAT entry; and</li>
<li>It followed the IAT pointer and determined the symbol at the target address.</li>
</ol>


<p>Normally we want both the expected and actual targets to match. If they don&rsquo;t,
we should investigate further, as this mismatch may indicate that the IAT has
been patched by a third party.</p>

<p>Note that <code>!iat</code> command is breakpad aware (provided that you&rsquo;ve already
loaded the symbols using <code>!bploadsyms</code>) but can fall back to the Microsoft
symbol engine as necessary.</p>

<p>Further note that the <code>!iat</code> command does not yet accept the <code>_imp_</code> symbolic
names for the IAT entries, you need to enter the hexadecimal representation of
the pointer.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Announcing Mozdbgext]]></title>
    <link href="https://dblohm7.ca/blog/2016/01/26/announcing-mozdbgext/"/>
    <updated>2016-01-26T12:45:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2016/01/26/announcing-mozdbgext</id>
    <content type="html"><![CDATA[<p>A well-known problem at Mozilla is that, while most of our desktop users run
Windows, most of Mozilla&rsquo;s developers do not. There are a lot of problems that
result from that, but one of the most frustrating to me is that sometimes
those of us that actually use Windows for development find ourselves at a
disadvantage when it comes to tooling or other productivity enhancers.</p>

<p>In many ways this problem is also a Catch-22: People don&rsquo;t want to use Windows
for many reasons, but tooling is big part of the problem. OTOH, nobody is
motivated to improve the tooling situation if nobody is actually going to
use them.</p>

<p>A couple of weeks ago my frustrations with the situation boiled over when I
learned that our <code>Cpp</code> unit test suite could not log symbolicated call stacks,
resulting in my filing of <a title="cppunittests do not look up breakpad symbols for logged stack traces" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1238305">bug 1238305</a> and <a title="Set _NT_SYMBOL_PATH on Windows test machines" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1240605">bug 1240605</a>. Not only could we
not log those stacks, in many situations we could not view them in a debugger
either.</p>

<p>Due to the fact that PDB files consume a large amount of disk space, we don&rsquo;t
keep those when building from integration or try repositories. Unfortunately
they are be quite useful to have when there is a build failure. Most of our
integration builds, however, do include breakpad symbols. Developers may also
explicitly <a href="https://wiki.mozilla.org/ReleaseEngineering/TryServer#Getting_debug_symbols">request symbols</a>
for their try builds.</p>

<p>A couple of years ago I had begun working on a WinDbg debugger extension that
was tailored to Mozilla development. It had mostly bitrotted over time, but I
decided to resurrect it for a new purpose: to help WinDbg<sup><a href="#fn1" id="r1">*</a></sup>
grok breakpad.</p>

<h2>Enter mozdbgext</h2>

<p><a href="https://github.com/dblohm7/mozdbgext"><code>mozdbgext</code></a> is the result. This extension
adds a few commands that makes Win32 debugging with breakpad a little bit easier.</p>

<p>The original plan was that I wanted <code>mozdbgext</code> to load breakpad symbols and then
insert them into the debugger&rsquo;s symbol table via the <a href="https://msdn.microsoft.com/en-us/library/windows/hardware/ff537943%28v=vs.85%29.aspx"><code>IDebugSymbols3::AddSyntheticSymbol</code></a>
API. Unfortunately the design of this API is not well equipped for bulk loading
of synthetic symbols: each individual symbol insertion causes the debugger to
re-sort its entire symbol table. Since <code>xul.dll</code>&rsquo;s quantity of symbols is in the
six-figure range, using this API to load that quantity of symbols is
prohibitively expensive. I tweeted a Microsoft PM who works on Debugging Tools
for Windows, asking if there would be any improvements there, but it sounds like
this is not going to be happening any time soon.</p>

<p>My original plan would have been ideal from a UX perspective: the breakpad
symbols would look just like any other symbols in the debugger and could be
accessed and manipulated using the same set of commands. Since synthetic symbols
would not work for me in this case, I went for &ldquo;Plan B:&rdquo; Extension commands that
are separate from, but analagous to, regular WinDbg commands.</p>

<p>I plan to continuously improve the commands that are available. Until I have a
proper README checked in, I&rsquo;ll introduce the commands here.</p>

<h3>Loading the Extension</h3>

<ol>
<li>Use the <code>.load</code> command: <code>.load &lt;path_to_mozdbgext_dll&gt;</code></li>
</ol>


<h3>Loading the Breakpad Symbols</h3>

<ol>
<li>Extract the breakpad symbols into a directory.</li>
<li>In the debugger, enter <code>!bploadsyms &lt;path_to_breakpad_symbol_directory&gt;</code></li>
<li>Note that this command will take some time to load all the relevant symbols.</li>
</ol>


<h3>Working with Breakpad Symbols</h3>

<p><strong>Note: You must have successfully run the <code>!bploadsyms</code> command first!</strong></p>

<p>As a general guide, I am attempting to name each breakpad command similarly to
the native WinDbg command, except that the command name is prefixed by <code>!bp</code>.</p>

<ul>
<li>Stack trace: <code>!bpk</code></li>
<li>Find nearest symbol to address: <code>!bpln &lt;address&gt;</code> where <em>address</em> is specified
as a hexadecimal value.</li>
</ul>


<h3>Downloading windbgext</h3>

<p>I have pre-built binaries (<a href="https://github.com/dblohm7/mozdbgext/blob/master/bin/32/mozdbgext.dll?raw=true">32-bit</a>, <a href="https://github.com/dblohm7/mozdbgext/blob/master/bin/64/mozdbgext.dll?raw=true">64-bit</a>) available for download.</p>

<p>Note that there are several other commands that are &ldquo;roughed-in&rdquo; at this point
and do not work correctly yet. Please stick to the documented commands at this
time.</p>

<hr />

<p><sup><a href="#r1" id="fn1">*</a></sup> When I write &ldquo;WinDbg&rdquo;, I am really
referring to any debugger in the <em>Debugging Tools for Windows</em> package,
including <code>cdb</code>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Bugs From Hell: Injected Third-party Code + Detours = a Bad Time]]></title>
    <link href="https://dblohm7.ca/blog/2016/01/11/bugs-from-hell-injected-third-party-code-plus-detours-equals-a-bad-time/"/>
    <updated>2016-01-11T13:00:00-07:00</updated>
    <id>https://dblohm7.ca/blog/2016/01/11/bugs-from-hell-injected-third-party-code-plus-detours-equals-a-bad-time</id>
    <content type="html"><![CDATA[<p>Happy New Year!</p>

<p>I&rsquo;m finally getting &lsquo;round to writing about a nasty bug that I had to spend a
bunch of time with in Q4 2015. It&rsquo;s one of the more challenging problems that
I&rsquo;ve had to break and I&rsquo;ve been asked a lot of questions about it. I&rsquo;m talking
about <a title="Crash spike in CreateWindowEx with Firefox 42.0b9 on Optimus" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1218473">bug 1218473</a>.</p>

<h2>How This All Began</h2>

<p>In <a title="crash in mozilla::widget::TSFStaticSink::EnsureInitActiveTIPKeyboard()" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1213567">bug 1213567</a> I had landed a patch to intercept calls to <code>CreateWindowEx</code>.
This was necessary because it was apparent in that bug that window subclassing
was occurring while a window was neutered (&ldquo;neutering&rdquo; is terminology that is
specific to Mozilla&rsquo;s Win32 IPC code).</p>

<p>While I&rsquo;ll save a discussion on the specifics of window neutering for another
day, for our purposes it is sufficient for me to point out that subclassing a
neutered window is bad because it creates an infinite recursion scenario with
window procedures that will eventually overflow the stack.</p>

<p>Neutering is triggered during certain types of IPC calls as soon as a message is
sent to an unneutered window on the thread making the IPC call. Unfortunately in
the case of <a title="crash in mozilla::widget::TSFStaticSink::EnsureInitActiveTIPKeyboard()" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1213567">bug 1213567</a>, the message triggering the neutering was
<code>WM_CREATE</code>. Shortly after creating that window, the code responsible would
subclass said window. Since <code>WM_CREATE</code> had already triggered neutering, this
would result in the pathological case that triggers the stack overflow.</p>

<p>For a fix, what I wanted to do is to prevent messages that were sent immediately
during the execution of <code>CreateWindow</code> (such as <code>WM_CREATE</code>) from triggering
neutering prematurely. By intercepting calls to <code>CreateWindowEx</code>, I could wrap
those calls with a RAII object that temporarily suppresses the neutering. Since
the subclassing occurs immediately after window creation, this meant that
this subclassing operation was now safe.</p>

<p>Unfortunately, shortly after landing <a title="crash in mozilla::widget::TSFStaticSink::EnsureInitActiveTIPKeyboard()" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1213567">bug 1213567</a>, <a title="Crash spike in CreateWindowEx with Firefox 42.0b9 on Optimus" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1218473">bug 1218473</a> was filed.</p>

<h2>Where to Start</h2>

<p>It wasn&rsquo;t obvious where to start debugging this. While a crash spike was clearly
correlated with the landing of <a title="crash in mozilla::widget::TSFStaticSink::EnsureInitActiveTIPKeyboard()" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1213567">bug 1213567</a>, the crashes were occurring in
code that had nothing to do with IPC or Win32. For example, the first stack that
I looked at was <code>js::CreateRegExpMatchResult</code>!</p>

<p>When it is just not clear where to begin, I like to start by looking at our
correlation data in Socorro &mdash; you&rsquo;d be surprised how often they can bring
problems into sharp relief!</p>

<p>In this case, the correlation data didn&rsquo;t disappoint: there
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1218473#c10">was</a> 100% correlation
with a module called <code>_etoured.dll</code>. There was also correlation with the
presence of both NVIDIA video drivers <em>and</em> Intel video drivers. Clearly this
was a concern only when NVIDIA Optimus technology was enabled.</p>

<p>I also had a pretty strong hypothesis about what <code>_etoured.dll</code> was: For many
years, Microsoft Research has shipped a package called
<a href="http://research.microsoft.com/en-us/projects/detours/">Detours</a>. Detours is a
library that is used for intercepting Win32 API calls. While the changelog for
Detours 3.0 points out that it has &ldquo;Removed [the] requirement for including
<code>detoured.dll</code> in processes,&rdquo; in previous versions of the package, this library
was required to be injected into the target process.</p>

<p>I concluded that <code>_etoured.dll</code> was most likely a renamed version of
<code>detoured.dll</code> from Detours 2.x.</p>

<h2>Following The Trail</h2>

<p>Now that I knew the likely culprit, I needed to know how it was getting there.
During a November trip to the Mozilla Toronto office, I spent some time
debugging a test laptop that was configured with Optimus.</p>

<p>Knowing that the presence of Detours was somehow interfering with our own API
interception, I decided to find out whether it was also trying to intercept
<code>CreateWindowExW</code>. I launched <code>windbg</code>, started Firefox with it, and then told
it to break as soon as <code>user32.dll</code> was loaded:</p>

<pre><samp>
sxe ld:user32.dll
</samp></pre>


<p>Then I pressed <code>F5</code> to resume execution. When the debugger broke again, this
time <code>user32</code> was now in memory. I wanted the debugger to break as soon as
<code>CreateWindowExW</code> was touched:</p>

<pre><samp>
ba w 4 user32!CreateWindowExW
</samp></pre>


<p>Once again I resumed execution. Then the debugger broke on the memory access and
gave me this call stack:</p>

<pre><samp>
nvd3d9wrap!setDeviceHandle+0x1c91
nvd3d9wrap!initialise+0x373
nvd3d9wrap!setDeviceHandle+0x467b
nvd3d9wrap!setDeviceHandle+0x4602
ntdll!LdrpCallInitRoutine+0x14
ntdll!LdrpRunInitializeRoutines+0x26f
ntdll!LdrpLoadDll+0x453
ntdll!LdrLoadDll+0xaa
mozglue!`anonymous namespace'::patched_LdrLoadDll+0x1b0
KERNELBASE!LoadLibraryExW+0x1f7
KERNELBASE!LoadLibraryExA+0x26
kernel32!LoadLibraryA+0xba
nvinit+0x11cb
nvinit+0x5477
nvinit!nvCoprocThunk+0x6e94
nvinit!nvCoprocThunk+0x6e1a
ntdll!LdrpCallInitRoutine+0x14
ntdll!LdrpRunInitializeRoutines+0x26f
ntdll!LdrpLoadDll+0x453
ntdll!LdrLoadDll+0xaa
mozglue!`anonymous namespace'::patched_LdrLoadDll+0x1b0
KERNELBASE!LoadLibraryExW+0x1f7
kernel32!BasepLoadAppInitDlls+0x167
kernel32!LoadAppInitDlls+0x82
USER32!ClientThreadSetup+0x1f9
USER32!__ClientThreadSetup+0x5
ntdll!KiUserCallbackDispatcher+0x2e
GDI32!GdiDllInitialize+0x1c
USER32!_UserClientDllInitialize+0x32f
ntdll!LdrpCallInitRoutine+0x14
ntdll!LdrpRunInitializeRoutines+0x26f
ntdll!LdrpLoadDll+0x453
ntdll!LdrLoadDll+0xaa
mozglue!`anonymous namespace'::patched_LdrLoadDll+0x1b0
KERNELBASE!LoadLibraryExW+0x1f7
firefox!XPCOMGlueLoad+0x23c
firefox!XPCOMGlueStartup+0x1d
firefox!InitXPCOMGlue+0xba
firefox!NS_internal_main+0x5c
firefox!wmain+0xbe
firefox!__tmainCRTStartup+0xfe
kernel32!BaseThreadInitThunk+0xe
ntdll!__RtlUserThreadStart+0x70
ntdll!_RtlUserThreadStart+0x1b
</samp></pre>


<p>This stack is a gold mine of information. In particular, it tells us the
following:</p>

<ol>
<li><p>The offending DLLs are being injected by <code>AppInit_DLLs</code> (and in fact, Raymond
Chen <a href="https://blogs.msdn.microsoft.com/oldnewthing/20140422-00/?p=1173">has blogged about</a>
this exact case in the past).</p></li>
<li><p><code>nvinit.dll</code> is the name of the DLL that is injected by step 1.</p></li>
<li><p><code>nvinit.dll</code> loads <code>nvd3d9wrap.dll</code> which then uses Detours to patch
our copy of <code>CreateWindowExW</code>.</p></li>
</ol>


<p>I then became curious as to which other functions they were patching.</p>

<p>Since Detours is patching executable code, we know that at some point it is
going to need to call <code>VirtualProtect</code> to make the target code writable. In the
worst case, <code>VirtualProtect</code>&rsquo;s caller is going to pass the address of the page
where the target code resides. In the best case, the caller will pass in the
address of the target function itself!</p>

<p>I restarted <code>windbg</code>, but this time I set a breakpoint on <code>VirtualProtect</code>:</p>

<pre><samp>
bp kernel32!VirtualProtect
</samp></pre>


<p>I then resumed the debugger and examined the call stack every time it broke.
While not every single <code>VirtualProtect</code> call would correspond to a detour, it
would be obvious when it was, as the NVIDIA DLLs would be on the call stack.</p>

<p>The first time I caught a detour, I examined the address being passed to
<code>VirtualProtect</code>: I ended up with the best possible case: the address was
pointing to the actual target function! From there I was able to distill a
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1218473#c39">list</a> of other
functions being hooked by the injected NVIDIA DLLs.</p>

<h2>Putting it all Together</h2>

<p>By this point I knew who was hooking our code and knew how it was getting there.
I also noticed that <code>CreateWindowEx</code> is the only function that the NVIDIA DLLs
and our own code were both trying to intercept. Clearly there was some kind of
bad interaction occurring between the two interception mechanisms, but what was
it?</p>

<p>I decided to go back and examine a
<a href="https://crash-stats.mozilla.com/report/index/e884dc17-957f-4270-86ab-f59742151113">specific</a>
crash dump. In particular, I wanted to examine three different memory locations:</p>

<ol>
<li>The first few instructions of <code>user32!CreateWindowExW</code>;</li>
<li>The first few instructions of <code>xul!CreateWindowExWHook</code>; and</li>
<li>The site of the call to <code>user32!CreateWindowExW</code> that triggered the crash.</li>
</ol>


<p>Of those three locations, the only one that looked off was location 2:</p>

<pre><samp>
6b1f6611 56              push    esi
6b1f6612 ff15f033e975    call    dword ptr [combase!CClassCache::CLSvrClassEntry::GetDDEInfo+0x41 (75e933f0)]
6b1f6618 c3              ret
6b1f6619 7106            jno     xul!`anonymous namespace'::CreateWindowExWHook+0x6 (6b1f6621)
xul!`anonymous namespace'::CreateWindowExWHook:
6b1f661b cc              int     3
6b1f661c cc              int     3
6b1f661d cc              int     3
6b1f661e cc              int     3
6b1f661f cc              int     3
6b1f6620 cc              int     3
6b1f6621 ff              ???
</samp></pre>


<p><em>Why the hell were the first six bytes filled with breakpoint instructions?</em></p>

<p>I decided at this point to look at some source code. Fortunately Microsoft
publishes the 32-bit source code for Detours, licensed for non-profit use,
under the name &ldquo;Detours Express.&rdquo;</p>

<p>I found a copy of Detours Express 2.1 and checked out the code. First I wanted
to know where all of these <code>0xcc</code> bytes were coming from. A quick <code>grep</code> turned
up what I was looking for:</p>

<figure class='code'><figcaption><span>detours.cpp</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'><span class="kr">inline</span> <span class="n">PBYTE</span> <span class="nf">detour_gen_brk</span><span class="p">(</span><span class="n">PBYTE</span> <span class="n">pbCode</span><span class="p">,</span> <span class="n">PBYTE</span> <span class="n">pbLimit</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">while</span> <span class="p">(</span><span class="n">pbCode</span> <span class="o">&lt;</span> <span class="n">pbLimit</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="o">*</span><span class="n">pbCode</span><span class="o">++</span> <span class="o">=</span> <span class="mh">0xcc</span><span class="p">;</span>   <span class="c1">// brk;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">pbCode</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now that I knew which function was generating the <code>int 3</code> instructions, I then
wanted to find its callers. Soon I found:</p>

<figure class='code'><figcaption><span>detours.cpp</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1247</span>
<span class='line-number'>1248</span>
<span class='line-number'>1249</span>
<span class='line-number'>1250</span>
<span class='line-number'>1251</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'><span class="cp">#ifdef DETOURS_X86</span>
</span><span class='line'>    <span class="n">pbSrc</span> <span class="o">=</span> <span class="n">detour_gen_jmp_immediate</span><span class="p">(</span><span class="n">pTrampoline</span><span class="o">-&gt;</span><span class="n">rbCode</span> <span class="o">+</span> <span class="n">cbTarget</span><span class="p">,</span> <span class="n">pTrampoline</span><span class="o">-&gt;</span><span class="n">pbRemain</span><span class="p">);</span>
</span><span class='line'>    <span class="n">pbSrc</span> <span class="o">=</span> <span class="n">detour_gen_brk</span><span class="p">(</span><span class="n">pbSrc</span><span class="p">,</span>
</span><span class='line'>                           <span class="n">pTrampoline</span><span class="o">-&gt;</span><span class="n">rbCode</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">pTrampoline</span><span class="o">-&gt;</span><span class="n">rbCode</span><span class="p">));</span>
</span><span class='line'><span class="cp">#endif </span><span class="c1">// DETOURS_X86</span>
</span></code></pre></td></tr></table></div></figure>


<p>Okay, so Detours writes the breakpoints out immediately after it has written a
<code>jmp</code> pointing to its trampoline.</p>

<p><em>Why is our hook function being trampolined?</em></p>

<p>The reason must be because our hook was installed first! Detours has
detected that and has decided that the best place to trampoline to the NVIDIA
hook is at the beginning of our hook function.</p>

<p><em>But Detours is using the wrong address!</em></p>

<p>We can see that because the <code>int 3</code> instructions are written out at the
<em>beginning</em> of <code>CreateWindowExWHook</code>, even though there should be a <code>jmp</code>
instruction first.</p>

<p><strong>Detours is calculating the wrong address to write its <code>jmp</code>!</strong></p>

<h2>Finding a Workaround</h2>

<p>Once I knew <em>what</em> the problem was, I needed to know more about the <em>why</em> &ndash;
only then would I be able to come up with a way to work around this problem.</p>

<p>I decided to reconstruct the scenario where both our code and Detours are trying
to hook the same function, but our hook was installed first. I would then
follow along through the Detours code to determine how it calculated the wrong
address to install its <code>jmp</code>.</p>

<p>The first thing to keep in mind is that Mozilla&rsquo;s function interception code
takes advantage of <a href="https://blogs.msdn.microsoft.com/oldnewthing/20110921-00/?p=9583">hot-patch points</a>
in Windows. If the target function begins with a <code>mov edi, edi</code> prolog, we
use a hot-patch style hook instead of a trampoline hook. I am not going to go
into detail about hot-patch hooks here &mdash; the above Raymond Chen link contains
enough details to answer your questions. For the purposes of this blog post, the
important point is that Mozilla&rsquo;s code patches the <code>mov edi, edi</code>, so NVIDIA&rsquo;s
Detours library would need to recognize and follow the <code>jmp</code>s that our code
patched in, in order to write its own <code>jmp</code> at <code>CreateWindowExWHook</code>.</p>

<p>Tracing through the Detours code, I found the place where it checks for a
hot-patch hook and follows the <code>jmp</code> if necessary. While examining a function
called <code>detour_skip_jmp</code>, I found the bug:</p>

<figure class='code'><figcaption><span>detours.cpp</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>124</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'>        <span class="n">pbNew</span> <span class="o">=</span> <span class="n">pbCode</span> <span class="o">+</span> <span class="o">*</span><span class="p">(</span><span class="n">INT32</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">pbCode</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>This code is supposed to be telling Detours where the target address of a <code>jmp</code>
is, so that Detours can follow it. <code>pbNew</code> is supposed to be the target address
of the <code>jmp</code>. <code>pbCode</code> is referencing the address <em>of the beginning of the <code>jmp</code>
instruction itself</em>. Unfortunately, with this type of <code>jmp</code> instruction, target
addresses are always relative to the address of the <em>next</em> instruction, not
the <em>current</em> instruction! Since the current <code>jmp</code> instruction is five bytes
long, Detours ends up writing its <code>jmp</code> <em>five bytes prior</em> to the intended
target address!</p>

<p>I went and checked the source code for Detours Express 3.0 to see if this had
been fixed, and indeed it had:</p>

<figure class='code'><figcaption><span>detours.cpp</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>163</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'>        <span class="n">PBYTE</span> <span class="n">pbNew</span> <span class="o">=</span> <span class="n">pbCode</span> <span class="o">+</span> <span class="mi">5</span> <span class="o">+</span> <span class="o">*</span><span class="p">(</span><span class="n">INT32</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">pbCode</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>That doesn&rsquo;t do much for me right now, however, since the NVIDIA stuff is still
using Detours 2.x.</p>

<p>In the case of Mozilla&rsquo;s code, there is legitimate executable code at that
incorrect address that Detours writes to. It is corrupting the last few
instructions of that function, thus explaining those mysterious crashes that
were seemingly unrelated code.</p>

<p>I confirmed this by downloading the binaries from the build that was associated
with the crash dump that I was analyzing. [As an aside, I should point out that
you need to grab the <em>identical</em> binaries for this exercise; you cannot build
from the same source revision and expect this to work due to variability that
is introduced into builds by things like PGO.]</p>

<p>The five bytes preceeding <code>CreateWindowExHookW</code> in the crash dump diverged from
those same bytes in the original binaries. I could also make out that the
overwritten bytes consisted of a <code>jmp</code> instruction.</p>

<h3>In Summary</h3>

<p>Let us now review what we know at this point:</p>

<ul>
<li>Detours 2.x doesn&rsquo;t correctly follow <code>jmp</code>s from hot-patch hooks;</li>
<li>If Detours tries to hook something that has already been hot-patched
(including legitimate hot patches from Microsoft), it will write bytes at
incorrect addresses;</li>
<li>NVIDIA Optimus injects this buggy code into everybody&rsquo;s address spaces via an
<code>AppInit_DLLs</code> entry for <code>nvinit.dll</code>.</li>
</ul>


<p>How can we best distill this into a suitable workaround?</p>

<p>One option could be to block the NVIDIA DLLs outright. In most cases this would
probably be the simplest option, but I was hesitant to do so this time. I was
concerned about the unintended consequences of blocking what, for better or
worse, is a user-mode component of NVIDIA video drivers.</p>

<p>Instead I decided to take advantage of the fact that we now know how this bug is
triggered. I have modified our API interception code such that if it detects
the presence of NVIDIA Optimus, it disables hot-patch style hooks.</p>

<p>Not only will this take care of the crash spike that started when I landed
<a title="crash in mozilla::widget::TSFStaticSink::EnsureInitActiveTIPKeyboard()" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1213567">bug 1213567</a>, I also expect it to take care of other crash signatures
whose relationship to this bug was not obvious.</p>

<p>That concludes this episode of <em>Bugs from Hell</em>. Until next time&hellip;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[On WebExtensions]]></title>
    <link href="https://dblohm7.ca/blog/2015/08/30/on-webextensions/"/>
    <updated>2015-08-30T02:00:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2015/08/30/on-webextensions</id>
    <content type="html"><![CDATA[<p>There has been enough that has been said over the past week about <a href="https://blog.mozilla.org/addons/2015/08/21/the-future-of-developing-firefox-add-ons/">WebExtensions</a>
that I wasn&rsquo;t sure if I wanted to write this post. As usual, I can&rsquo;t seem to
help myself. Note the usual disclaimer that this is my personal opinion. Further
note that I have no involvement with WebExtensions at this time, so I write this
from the point of view of an observer.</p>

<h2>API? What API?</h2>

<p>I shall begin with the proposition that the legacy, non-jetpack
environment for addons is not an API. As ridiculous as some readers might
consider this to be, please humour me for a moment.</p>

<p>Let us go back to the acronym, &ldquo;API.&rdquo; <strong>A</strong>pplication <strong>P</strong>rogramming <strong>I</strong>nterface.
While the usage of the term &ldquo;API&rdquo; seems to have expanded over the years to encompass
just about any type of interface whatsoever, I&rsquo;d like to explore the first letter of that
acronym: <em>Application</em>.</p>

<p>An <em>Application</em> Programming Interface is a specific type of interface that is
exposed for the purposes of building applications. It typically provides a
formal abstraction layer that isolates applications from the implementation
details behind the lower tier(s) in the software stack. In the case of web
browsers, I suggest that there are two distinct types of applications:
web content, and extensions.</p>

<p>There is obviously a very well defined API for web content. On the other hand,
I would argue that Gecko&rsquo;s legacy addon environment is not an API at all! From
the point of view of an extension, there is no abstraction, limited formality,
and not necessarily an intention to be used by applications.</p>

<p>An extension is imported into Firefox with full privileges and can access whatever
it wants. Does it have access to interfaces? Yes, but are those interfaces intended
for <em>applications</em>? Some are, but many are not. The environment that Gecko
currently provides for legacy addons is analagous to an operating system running
every single application in kernel mode. Is that powerful? Absolutely! Is that
the best thing to do for maintainability and robustness? Absolutely not!</p>

<p>Somewhere a line needs to be drawn to demarcate this abstraction layer and
improve Gecko developers&#8217; ability to make improvements under the hood. Last
week&rsquo;s announcement was an invitation to addon developers to help shape that
future. Please participate and please do so constructively!</p>

<h2>WebExtensions are not Chrome Extensions</h2>

<p>When I first heard rumors about WebExtensions in Whistler, my source made it
very clear to me that the WebExtensions initiative is not about making Chrome
extensions run in Firefox. In fact, I am quite disappointed with some of the
press coverage that seems to completely miss this point.</p>

<p>Yes, WebExtensions will be implementing some APIs to be <em>source compatible</em>
with Chrome. That makes it easier to port a Chrome extension, but porting will
still be necessary. I like the Venn Diagram concept that the <a href="https://wiki.mozilla.org/WebExtensions/FAQ">WebExtensions FAQ</a>
uses: Some Chrome APIs will not be available in WebExtensions. On the other hand,
WebExtensions will be providing APIs above and beyond the Chrome API set that
will maintain Firefox&rsquo;s legacy of extensibility.</p>

<p>Please try not to think of this project as Mozilla taking functionality away.
In general I think it is safe to think of this as an opportunity to move that
same functionality to a mechanism that is more formal and abstract.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Interesting Win32 APIs]]></title>
    <link href="https://dblohm7.ca/blog/2015/07/28/interesting-win32-apis/"/>
    <updated>2015-07-28T11:30:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2015/07/28/interesting-win32-apis</id>
    <content type="html"><![CDATA[<p>Yesterday I decided to diff the export tables of some core Win32 DLLs to see what&rsquo;s
changed between Windows 8.1 and the Windows 10 technical preview. There weren&rsquo;t
many changes, but the ones that were there are quite exciting IMHO. While
researching these new APIs, I also stumbled across some others that were
added during the Windows 8 timeframe that we should be considering as well.</p>

<h2>Volatile Ranges</h2>

<p>While my diff showed these APIs as new exports for Windows 10, the MSDN docs
claim that these APIs are actually new for the Windows 8.1 Update. Using the
<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dn781436%28v=vs.85%29.aspx"><code>OfferVirtualMemory</code></a>
and <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dn781437%28v=vs.85%29.aspx"><code>ReclaimVirtualMemory</code></a>
functions, we can now specify ranges of virtual memory that are safe to
discarded under memory pressure. Later on, should we request that access be
restored to that memory, the kernel will either return that virtual memory to us
unmodified, or advise us that the associated pages have been discarded.</p>

<p>A couple of years ago we had an intern on the Perf Team who was working on
bringing this capability to Linux. I am pleasantly surprised that this is now
offered on Windows.</p>

<h2><code>madvise(MADV_WILLNEED)</code> for Win32</h2>

<p>For the longest time we have been hacking around the absence of a <code>madvise</code>-like
API on Win32. On Linux we will do a <code>madvise(MADV_WILLNEED)</code> on memory-mapped
files when we want the kernel to read ahead. On Win32, we were opening the
backing file and then doing a series of sequential reads through the file to
force the kernel to cache the file data. As of Windows 8, we can now call
<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/hh780543%28v=vs.85%29.aspx"><code>PrefetchVirtualMemory</code></a>
for a similar effect.</p>

<h2>Operation Recorder: An API for SuperFetch</h2>

<p>The <a href="https://msdn.microsoft.com/en-us/library/hh437562%28v=vs.85%29.aspx"><code>OperationStart</code></a>
and <a href="https://msdn.microsoft.com/en-us/library/hh437558%28v=vs.85%29.aspx"><code>OperationEnd</code></a>
APIs are intended to record access patterns during a file I/O operation.
SuperFetch will then create prefetch files for the operation, enabling prefetch
capabilities above and beyond the use case of initial process startup.</p>

<h2>Memory Pressure Notifications</h2>

<p>This API is not actually new, but I couldn&rsquo;t find any invocations of it in the
Mozilla codebase. <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa366541%28v=vs.85%29.aspx"><code>CreateMemoryResourceNotification</code></a>
allocates a kernel handle that becomes signalled when physical memory is running
low. Gecko already has facilities for handling memory pressure events on other
platforms, so we should probably add this to the Win32 port.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[WaitMessage Considered Harmful]]></title>
    <link href="https://dblohm7.ca/blog/2015/03/12/waitmessage-considered-harmful/"/>
    <updated>2015-03-12T15:00:00-06:00</updated>
    <id>https://dblohm7.ca/blog/2015/03/12/waitmessage-considered-harmful</id>
    <content type="html"><![CDATA[<p>I could apologize for the clickbaity title, but I won&rsquo;t. I have no shame.</p>

<p>Today I want to talk about some code that we imported from Chromium some time
ago. I replaced it in Mozilla&rsquo;s codebase a few months back in <a title="IPC message pump for windows should use WinUtils::WaitForMessage" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1072752">bug 1072752</a>:</p>

<figure class='code'><figcaption><span> (message_pump_win.cc)</span> <a href='https://dblohm7.ca/downloads/code/message_pump_win.cc'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>261</span>
<span class='line-number'>262</span>
<span class='line-number'>263</span>
<span class='line-number'>264</span>
<span class='line-number'>265</span>
<span class='line-number'>266</span>
<span class='line-number'>267</span>
<span class='line-number'>268</span>
<span class='line-number'>269</span>
<span class='line-number'>270</span>
<span class='line-number'>271</span>
<span class='line-number'>272</span>
<span class='line-number'>273</span>
<span class='line-number'>274</span>
<span class='line-number'>275</span>
<span class='line-number'>276</span>
<span class='line-number'>277</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'>    <span class="c1">// A WM_* message is available.</span>
</span><span class='line'>    <span class="c1">// If a parent child relationship exists between windows across threads</span>
</span><span class='line'>    <span class="c1">// then their thread inputs are implicitly attached.</span>
</span><span class='line'>    <span class="c1">// This causes the MsgWaitForMultipleObjectsEx API to return indicating</span>
</span><span class='line'>    <span class="c1">// that messages are ready for processing (Specifically, mouse messages</span>
</span><span class='line'>    <span class="c1">// intended for the child window may appear if the child window has</span>
</span><span class='line'>    <span class="c1">// capture).</span>
</span><span class='line'>    <span class="c1">// The subsequent PeekMessages call may fail to return any messages thus</span>
</span><span class='line'>    <span class="c1">// causing us to enter a tight loop at times.</span>
</span><span class='line'>    <span class="c1">// The WaitMessage call below is a workaround to give the child window</span>
</span><span class='line'>    <span class="c1">// some time to process its input messages.</span>
</span><span class='line'>    <span class="n">MSG</span> <span class="n">msg</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
</span><span class='line'>    <span class="n">DWORD</span> <span class="n">queue_status</span> <span class="o">=</span> <span class="n">GetQueueStatus</span><span class="p">(</span><span class="n">QS_MOUSE</span><span class="p">);</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">HIWORD</span><span class="p">(</span><span class="n">queue_status</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">QS_MOUSE</span> <span class="o">&amp;&amp;</span>
</span><span class='line'>        <span class="o">!</span><span class="n">PeekMessage</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">WM_MOUSEFIRST</span><span class="p">,</span> <span class="n">WM_MOUSELAST</span><span class="p">,</span> <span class="n">PM_NOREMOVE</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">WaitMessage</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This code is wrong. <strong>Very</strong> wrong.</p>

<p>Let us start with the calls to <code>GetQueueStatus</code> and <code>PeekMessage</code>. Those APIs
mark any messages already in the thread&rsquo;s message queue as having been seen,
such that they are no longer considered &ldquo;new.&rdquo; Even though those function calls
do not remove messages from the queue, any messages that were in the queue at
this point are considered to be &ldquo;old.&rdquo;</p>

<p>The logic in this code snippet is essentially saying, &ldquo;if the queue contains
mouse messages that do not belong to this thread, then they must belong to an
attached thread.&rdquo; The code then calls <code>WaitMessage</code> in an effort to give the
other thread(s) a chance to process their mouse messages. This is where the code
goes off the rails.</p>

<p>The <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms644956%28v=vs.85%29.aspx">documentation</a>
for <code>WaitMessage</code> states the following:</p>

<blockquote><p>Note that <code>WaitMessage</code> does not return if there is unread input in the message
queue after the thread has called a function to check the queue. This is
because functions such as <code>PeekMessage</code>, <code>GetMessage</code>, <code>GetQueueStatus</code>,
<code>WaitMessage</code>, <code>MsgWaitForMultipleObjects</code>, and <code>MsgWaitForMultipleObjectsEx</code>
check the queue and then change the state information for the queue so that
the input is no longer considered new. A subsequent call to <code>WaitMessage</code> will
not return until new input of the specified type arrives. The existing unread
input (received prior to the last time the thread checked the queue) is ignored.</p></blockquote>

<p><code>WaitMessage</code> will only return if there is <em>a new</em> (as opposed to <em>any</em>) message
in the queue for the calling thread. Any messages for the calling thread that
were already in there at the time of the <code>GetQueueStatus</code> and <code>PeekMessage</code> calls
are no longer new, so they are ignored.</p>

<p>There might very well be a message at the head of that queue that should be
processed by the current thread. Instead it is ignored while we wait for other
threads. Here is the crux of the problem: we&rsquo;re waiting on other threads whose
input queues are attached to our own! That other thread can&rsquo;t process its
messages because our thread has messages in front of its messages; on the other
hand, our thread has blocked itself!</p>

<p>The only way to break this deadlock is for new messages to be added to the queue.
That is a big reason why we&rsquo;re seeing things like <a title="Page doesn't load/render if the mouse is not moving" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1105386">bug 1105386</a>: Moving the
mouse adds new messages to the queue, making <code>WaitMessage</code> unblock.</p>

<p>I&rsquo;ve already eliminated this code in Mozilla&rsquo;s codebase, but the challenge is
going to be getting rid of this code in third-party binaries that attach their
own windows to Firefox&rsquo;s windows.</p>
]]></content>
  </entry>
  
</feed>
