    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Profiting from the Folly of Others</title>
        <link>https://members.accu.org/index.php/articles/2776</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #156 - April 2020</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c409/">o156</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-409/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+409/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Profiting from the Folly of Others</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 03 April 2020 21:01:39 +01:00 or Fri, 03 April 2020 21:01:39 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Code referred to as a hack can raise an eyebrow. Alastair Harrison learns about accessing private members of C++ classes by investigating a header called UninitializedMemoryHacks.h</p>
<p><strong>Body:</strong>&nbsp;<p>I always enjoy browsing through the source code of libraries written by other people. With so many dark corners in C++ I often come across new and interesting ideas. Iâ€™d like to share one such example from the â€˜Follyâ€™ library. Not because I think it illustrates best practice (it doesnâ€™t!), but because I learned something about C++ in the process of deciphering it.</p>

<p>Folly [<a href="#[GitHub]">GitHub</a>] is a C++ library developed at Facebook and released under the open-source Apache 2.0 licence. It contains useful algorithms, vocabulary types and utility functions. Hidden amongst the main-stream functionality are some utilities tailored towards the more unusual situations that a C++ developer may find themselves in.</p>

<p>The code Iâ€™d like to focus on lives in a header with the ominous title of <span class="filename">UninitializedMemoryHacks.h</span>. Its subtle use of loopholes and language features is fascinating, despite its obviously questionable nature.</p>

<p>The file contains a collection of helper functions that do reprehensible things in the name of performance. In particular, it provides a set of overloaded functions in the <code>folly::</code> namespace, named <code>resizeWithoutInitialization</code> and whose purpose is to â€˜<em>resize </em><code>std::string</code><em> or </em><code>std::vector</code><em> without constructing or initializing new elements</em>â€™.</p>

<h2>It does <em>what?</em></h2>

<p>Normally when we call <code>resize</code> to increase the size of a <code>std::vector</code>, the container first checks to see if the existing capacity is sufficient to hold the requested number of elements. Even when the existing capacity is sufficient, the implementation needs to do something with the newly added elements. They each need to be constructed or initialized to ensure that they are in a valid state. For trivial types such as <code>int</code> itâ€™s actually OK to leave the values uninitialized, as long as we donâ€™t try to read them before weâ€™ve first written something to them. But <code>std::vector</code> always forces us to pay the cost of initialization, even if we were intending to overwrite all of the newly initialized elements straight after calling <code>resize</code>.</p>

<p>In contrast, when we call <code>folly::resizeWithoutInitialization</code> on a <code>std::vector</code> with sufficient capacity, it simply <em>reaches in to the private implementation</em> and moves the pointer representing the end of the sequence. The memory for the new elements is left uninitialized, leaving the caller responsible for that task.</p>

<p>The first time I looked at the implementation of this function, I was amazed and alarmed to see it somehow bypassing the normal C++ access restrictions to modify a private member variable of a standard library component. I say â€˜somehowâ€™ because the precise mechanism was so thoroughly obfuscated behind layers of macros, template trickery and arcane member function pointer syntax that it might as well have been magic. The baffling part was that it claimed to pull off this magic trick without invoking any undefined behaviour. I had to know how this worked!</p>

<p>I wonâ€™t dwell further on the specifics of how Folly meddles with the internals of the standard containers. The interesting part is how it bypasses the access control mechanisms of C++. Herb Sutter has a <em>Guru of the Week </em>article [<a href="#[GotW]">GotW</a>] discussing three nefarious techniques for accessing private members of a class, though none of them quite matches the applicability of the method in the Folly library. The first two are illegal and the third involves writing a sneaky member function specialization, which makes it relevant only to classes that contain member function templates.</p>

<p>Whatâ€™s interesting about the technique used in Folly is that itâ€™s able to freely access private members of any class, without any particular structural requirements. It does this with a clever combination of infrequently-used language features and a small loophole allowed by the C++ standard.</p>

<h2>The effect</h2>

<p>Letâ€™s take a simple class with a private member function:</p>

<pre class="programlisting">
  class Widget {
   private:
    void forbidden();
  };</pre>
  
<p>Our aim is to write a free function called <code>hijack</code> which takes a reference to a <code>Widget</code> as input and calls the <code>Widget::forbidden()</code> member function on it. Assume that the <code>Widget</code> class is closed for modification by us, so we canâ€™t just change the <code>private</code> to <code>public</code>, or make <code>hijack</code> a friend of <code>Widget</code>.</p>

<p>Obviously we canâ€™t call the private member function directly:</p>

<pre class="programlisting">
  void hijack(Widget&amp; w) {
    w.forbidden();  // ERROR!
  }</pre>
  
<p>because the compiler will stop us: </p>

<pre class="programlisting">
  In function 'void hijack(Widget&amp;)':
  error: 'void Widget::forbidden()' is private
  within this context
        |     w.forbidden();
        |                 ^</pre>
		
<p>Using techniques from the Folly library, weâ€™ll build up a solution piece-by-piece. This article covers the specific case of calling private member functions, but the approach is equally applicable to accessing and mutating private member variables in a class. The underlying techniques all work in C++98, but some more modern features will be used to ease exposition.</p>

<h2>A syntax refresher for pointers to member functions</h2>

<p>Weâ€™ll be using pointers to member functions (PMFs) extensively, so itâ€™s worth revisiting their syntax before we dive in further. PMFs enable a primitive form of polymorphism over methods in a class. For the sake of exposition, letâ€™s start with a hypothetical calculator class (Listing 1).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Calculator {
  float current_val = 0.f;
 public:
   void clear_value() { current_val = 0.f; };
   float value() const {
     return current_val;
   };

   void add(float x) { current_val += x; };
   void multiply(float x) { current_val *= x; };
};
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>Arguably the easiest way to work with pointers to member functions is through type aliases. The type alias is specific to a given class, but the pointer can be bound to any member function in the class that matches the signature. In the case of <code>Calculator</code>, both <code>multiply</code> and <code>add</code> take a single <code>float</code> argument and return <code>void</code>, so we can use the same type alias for both. It looks like this:</p>

<pre class="programlisting">
  using Operation = void (Calculator::*)(float);</pre>
  
<p>We can then store the address of either <code>multiply</code> or <code>add</code>. But <code>value</code> doesnâ€™t match the signature, so its address cannot be assigned to an <code>Operation</code> pointer.</p>

<pre class="programlisting">
  // OK
  Operation op1 = &amp;Calculator::add;
  Operation op2 = &amp;Calculator::multiply;
  
  // ERROR! Signature mismatch
  Operation op3 = &amp;Calculator::value; </pre>
  
<p>Weâ€™ll need to make a new alias to match the signature of <code>value</code>:</p>

<pre class="programlisting">
  using Getter = float (Calculator::*)() const;
  
  // OK - signature now matches
  Getter get = &amp;Calculator::value;</pre>
  
<p>A pointer to a member function isnâ€™t very useful unless we know which object instance we want to call it on. Hereâ€™s the syntax for calling members of <code>Calculator</code> through their pointers:<a href="#FN01"><sup>1</sup></a></p>

<pre class="programlisting">
  Calculator calc{};
  (calc.*op1)(123.0f); // Calls add

  (calc.*op2)(10.0f);  // Calls multiply

  
  // Prints 1230.0
  std::cout &lt;&lt; (calc.*get)() &lt;&lt; '\n';</pre>
  
<p>One of the interesting things about pointers to member functions is that they can be bound to private member functions. Thatâ€™s the first piece of the Folly puzzle.</p>

<h2>Puzzle piece 1: Pointers to private member functions can be called from any scope</h2>

<p>Suppose the author of the <code>Widget</code> class had helpfully provided a means to obtain a pointer to the <code>Widget::forbidden()</code> member function. Once we have that pointer, we are able to call it from <em>any</em> scope where we have a <code>Widget</code> available (Listing 2).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Widget {
 public:
  static auto forbidden_fun() {
    return &amp;Widget::forbidden;
  }
 private:
  void forbidden();
};

void hijack(Widget&amp; w) {
  using ForbiddenFun = void (Widget::*)();
  ForbiddenFun const forbidden_fun =
    Widget::forbidden_fun();

  // Calls a private member function on the Widget
  // instance passed in to the function.
  (w.*forbidden_fun)();
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>Thatâ€™s useful to know, but most classes donâ€™t offer to hand out pointers to their private member functions. We need to find a sneakier way to get hold of one from outside of the class scope.</p>

<p>Thereâ€™s a curious loophole in the C++ standard around the use of explicit template instantiation which allows us to refer to private class members. That gives us the second piece of the Folly puzzle.</p>

<h2>Puzzle piece 2: The explicit template instantiation loophole</h2>

<p>The C++17 standard contains the following paragraph (with the parts of interest to us marked in bold):</p>

<p class="blockquote"><strong>17.7.2 (item 12)</strong></p>

<p class="blockquote"><strong>The usual access checking rules do not apply to names used to specify explicit instantiations.</strong> [Note: In particular, the template arguments and names used in the function declarator (including parameter types, return types and exception specifications) <strong>may be private types</strong> or objects which would normally not be accessible and the template may be a member template or member function which would not normally be accessible.]</p>

<p>To understand the reason behind this curiosity, we need to discuss the explicit template instantiation mechanism for a moment.</p>

<p>Suppose weâ€™ve got a <code>Company</code> class with an internal private member function template, <code>update_employee</code>. Perhaps there is one particular template argument, <code>OnSiteEmployeePolicy</code> which is expensive to compile, but used regularly. Weâ€™d like to avoid the cost of instantiating that version of the template in lots of translation units. We can achieve this by explicitly instantiating the member template in just one translation unit and marking it as <code>extern</code> everywhere else. See Listing 3 (<span class="filename">company.h</span>) and Listing 4 (<span class="filename">company.cpp</span>).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class OnSiteEmployeePolicy {
  // ... contains daring and unfettered use of
  // ... hairy template meta-programming tricks.
};
class Company {
 private:
  template &lt;typename EmployeePolicy&gt;
  void update_employee(int employee_id) {
    // ...
  }
};
// Prevents implicit instantiation of a specific
// specialization.
extern template
Company::update_employee&lt;OnSiteEmployeePolicy&gt;;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &quot;company.h&quot;

// Explicit instantiation of the template only
// needs to happen in a single translation unit.
template
Company::update_employee&lt;OnSiteEmployeePolicy&gt;;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<p>Brushing aside the question of how someone ever snuck such an awkward API design through code review, notice how the template instantiation mechanism needs to allow a reference to a <em>private</em> member of <code>Company</code> â€“  <code>Company::update_employee</code> â€“ in a context where we would not normally be able to (i.e. outside the scope of the <code>Company</code> class). Thatâ€™s the reason for the exception in the C++ standard that allows for private types to appear in explicit template instantiations.</p>

<p>Itâ€™s also the crucial loophole that Folly takes advantage of. We canâ€™t relax just yet, though. Thereâ€™s still some work to be done.</p>

<h2>Puzzle piece 3: Passing a member-function pointer as a non-type template parameter</h2>

<p>In C++, template arguments are usually types, but there is some support for non-type template parameters if they are of integral or pointer type.<a href="#FN02"><sup>2</sup></a> Conveniently enough, itâ€™s perfectly legal to pass a pointer-to-member-function as a template argument.<a href="#FN03"><sup>3</sup></a> Listing 5 is an example of what that looks like.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class SpaceShip {
 public:
  void dock();
  // ...
};

// Member function alias that matches the
// signature of SpaceShip::dock()
using SpaceShipFun = void (SpaceShip::*)();

// spaceship_fun is a pointer-to-member-function
// value which is baked-in to the type of the
// SpaceStation template at compile time.
template &lt;SpaceShipFun spaceship_fun&gt;
class SpaceStation {
  // ...
};

// Instantiate a SpaceStation and pass in a
// pointer to member function statically as a
// template argument.
SpaceStation&lt;&amp;SpaceShip::dock&gt; space_station{};
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 5</td>
	</tr>
</table>

<p>The intermediate <code>SpaceShipFun</code> alias hampers the genericity of the <code>SpaceStation</code> template, so we can move the type of the pointer-to-member-function into the template arguments too (Listing 6).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
template &lt;
  typename SpaceShipFun,
  SpaceShipFun spaceship_fun
&gt;
class SpaceStation {
  // ...
};

// Now we must also pass the type of the pointer to
// member function when we instantiate the
// SpaceStation template.
SpaceStation&lt;
  void (SpaceShip::*)(),
  &amp;SpaceShip::dock
&gt; space_station{};
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 6</td>
	</tr>
</table>

<p>We can take that a step further and have the compiler deduce the type of the member function for us:</p>

<pre class="programlisting">
  SpaceStation&lt;
    decltype(&amp;SpaceShip::dock),
    &amp;SpaceShip::dock
  &gt; space_station{};</pre>
  
<p>That relieves us of some of the burden of having to pass the member function signature to the template. Weâ€™ll stick with this approach for the rest of article as itâ€™s whatâ€™s used in the Folly library, but itâ€™s worth mentioning that C++17â€™s <code>template &lt;auto&gt;</code> feature removes the need for the first template parameter entirely.<a href="#FN04"><sup>4</sup></a></p>

<h3>Passing a private pointer-to-member-function as a template parameter</h3>

<p>Letâ€™s combine the explicit template instantiation loophole with the ability to pass member function pointers as template parameters. The <code>HijackImpl</code> struct receives a pointer to <code>Widget::forbidden()</code> as a template parameter (see Listing 7).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
// The first template parameter is the type
// signature of the pointer-to-member-function.
// The second template parameter is the pointer
// itself.
template &lt;
  typename ForbiddenFun,
  ForbiddenFun forbidden_fun
&gt;
struct HijackImpl {
  static void apply(Widget&amp; w) {
    // Calls a private method of Widget
    (w.*forbidden_fun)();
  }
};

// Explicit instantiation is allowed to refer to
// `Widget::forbidden` in a scope where it's not
// normally permissible.
template struct HijackImpl&lt;
  decltype(&amp;Widget::forbidden),
  &amp;Widget::forbidden
&gt;;

			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 7</td>
	</tr>
</table>

<p>Brilliant! Weâ€™ve instantiated a template that is able to reach in and call the <code>forbidden()</code> member function on any <code>Widget</code> that we care to pass in. So we just have to write the free function, <code>hijack</code> and we can go back to watching cat videos on YouTube, right?</p>

<pre class="programlisting">
  void hijack(Widget&amp; w) {
    HijackImpl&lt;
      decltype(&amp;Widget::forbidden),
      &amp;Widget::forbidden
    &gt;::apply(w);
  }</pre>
  
<p>The only problem is that it doesnâ€™t work. The compiler sees through our ruse and raps us smartly on the knuckles:</p>

<pre class="programlisting">
 error: 'forbidden' is a private member of 'Widget'
   HijackImpl&lt;decltype(&amp;Widget::forbidden),
     &amp;Widget::forbidden&gt;::hijack(w);</pre>
	 
<p>The use of the <code>HijackImpl</code> template inside the <code>hijack</code> function is <em>not</em> an explicit template instantiation. Itâ€™s just a â€˜normalâ€™ implicit instantiation. So the loophole doesnâ€™t apply. Itâ€™s time to phone a friend for help with solving the next piece of the puzzle.</p>

<h2>Puzzle piece 4: A friend comes to our aid</h2>

<p>Because weâ€™re not allowed to refer to <code>Widget::forbidden</code> inside our <code>hijack</code> function, we must solve the conundrum of accessing the value of the <code>ForbiddenFun</code> template parameter <em>withou</em>t making any direct reference to the <code>HijackImpl&lt;...&gt;</code> template. This apparently unreasonable requirement is easily solved with a shrewd application of the <code>friend</code> keyword.</p>

<p>Letâ€™s take another step back from the task in hand and look at some of the different effects one can achieve when marking a free function as a <code>friend</code> of a class. The behaviour depends on whether the class contains only a declaration of the function (i.e. function signature only), or whether the complete definition (including the function body) appears inside the class.</p>

<h3>â€˜friendâ€™ function declarations</h3>

<p>Most C++ developers will be familiar with the pattern of placing a free function declaration inside of a class definition and marking it as a <code>friend</code>. The definition of the free-function still lives outside of the class, but is now allowed to access private members of the class. (See Listing 8.)</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Gadget {
  // Friend declaration gives `frobnicate` access
  // to Gadget's private members.
  friend void frobnicate();

 private:
  void internal() {
    // ...
  }
};

// Definition as a normal free function
void frobnicate() {
  Gadget g;
  // OK because `frobnicate()` is a friend of
  // `Gadget`.
  g.internal();
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 8</td>
	</tr>
</table>

<p>If we could make <code>hijack</code> be a <code>friend</code> of <code>Widget</code> then the compiler would allow us to refer to <code>Widget::forbidden</code> inside the <code>hijack</code> function. Alas, this option is unavailable because the rules of our game donâ€™t allow us to modify <code>Widget</code>. Letâ€™s try something else.</p>

<h3>Inline â€˜friendâ€™ function definitions</h3>

<p>Itâ€™s also possible to <em>define</em> a <code>friend</code> function inside a class (as opposed to just declaring it there). This isnâ€™t something seen as often in C++ code. Probably because when we try to call the free function, the compiler is unable to find it! (See Listing 9.) Hereâ€™s the compile error:</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Gadget {
  // Free function declared as a friend of Gadget
  friend void frobnicate() {
    Gadget g;
    g.internal(); // Still OK
  }

 private:
   void internal();
};

void do_something() {
  // NOT OK: Compiler can't find frobnicate()
  // during name lookup
  frobnicate();
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 9</td>
	</tr>
</table>

<pre class="programlisting">
  error: 'frobnicate' was not declared in this scope
        |   frobnicate();
        |   ^</pre>
		
<p>As before, <code>frobnicate()</code> is still a free function that lives in the global namespace, but it behaves quite differently under name lookup now that it is defined inside the <code>Gadget</code> class. A <code>friend</code> function defined inside a class is sometimes known as a â€˜hidden friendâ€™ [<a href="#[JSS19]">JSS19</a>] [<a href="#[Saks18]">Saks18</a>]. Hidden friends can <em>only</em> be found through Argument Dependent Lookup (ADL) and ADL only works if one of the arguments to the function is of the enclosing class type. In the above example <code>frobnicate()</code> takes no arguments, so argument dependent lookup wonâ€™t happen. The result is that <code>frobnicate()</code> canâ€™t be called from anywhere. Not even from within <code>frobnicate()</code> itself!</p>

<p>If we add a parameter of the enclosing class type to <code>frobnicate()</code> then weâ€™re able to call it via ADL (Listing 10).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Gadget {
  friend void frobnicate(Gadget&amp; gadget) {
    gadget.internal();
  }

 private:
   void internal();
};

void do_something(Gadget&amp; gadget) {
  // OK: Compiler is now able to find the
  // definition of `frobnicate` inside Gadget
  // because ADL adds it to the candidate set for
  // name lookup.
  frobnicate(gadget);
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 10</td>
	</tr>
</table>

<h3>Making hidden friends visible</h3>

<p>The hidden-friend ADL trick can be very useful; itâ€™s an ideal tool when writing operator overloads for user-defined types. But weâ€™ll use a slightly bigger hammer for our <code>hijack</code> function. Thereâ€™s another way of allowing the compiler to find hidden friends, and that is to put a declaration of the function in the enclosing namespace (Listing 11).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Gadget {
  // Definition stays inside the Gadget class
  friend void frobnicate() {
    Gadget g;
    g.internal();
  }

 private:
   void internal();
};

// An additional namespace-scope declaration makes
// the function available for normal name lookup.
void frobnicate();

void do_something() {
  // The compiler can now find the function
  frobnicate();
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 11</td>
	</tr>
</table>

<p>This is exactly the opposite of the usual pattern of defining a free function and then placing a <code>friend</code> declaration for it inside of a class. The new behaviour is almost identical except for one critical difference: when the enclosing class is a template, the free function has access to the template parameters!</p>

<h3>Using friends to pilfer template parameters</h3>

<p>I trust you will be at least a little unsettled to discover that the program in Listing 12 is valid.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;iostream&gt;

template &lt;int N&gt;
class SpookyAction {
  friend int observe() {
    return N;
  }
};

int observe();

int main() {
  SpookyAction&lt;42&gt;{};
  std::cout &lt;&lt; observe() &lt;&lt; '\n';  // Prints 42
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 12</td>
	</tr>
</table>

<p>Whatâ€™s happening is that the <code>observe()</code> function is not defined until the point at which the <code>SpookyAction</code> template is instantiated (by its use in the <code>main</code> function). There is a single definition of the <code>observe()</code> function, because there is a single instantiation of the <code>SpookyAction</code> template. The really useful part is that the <code>observe()</code> function gains access to the template parameter of the <code>SpookyAction&lt;42&gt;</code> instantiation that caused it to be defined.</p>

<p>Of course things go wrong very quickly if we try to instantiate any more versions of the <code>SpookyAction</code> template, as each one results in a redefinition of the <code>observe()</code> function and an angry compiler.</p>

<p>Provided we use it carefully, we now have the last piece of our puzzle â€“ a way to access the template parameters of a class from a scope external to that class.</p>

<h2>Putting the puzzle pieces together</h2>

<p>Letâ€™s go back to our original <code>Widget</code> example, now that weâ€™ve got all of the pieces that we need to be able to reach in and call its private member function, <code>Widget::forbidden()</code>. In summary:</p>

<ol>
	<li>We use the loophole in the explicit template instantiation rules to allow us to refer to <code>Widget::forbidden()</code> from outside of the <code>Widget</code> class.</li>
	
	<li>We inject the address of <code>Widget::forbidden()</code> into our <code>HijackImpl</code> class as a template parameter.</li>
	
	<li>We define the <code>hijack()</code> function directly inside of <code>HijackImpl</code> so that it can access the template parameter containing the address of <code>Widget::forbidden()</code>.</li>
	
	<li>We mark <code>hijack</code> as a <code>friend</code> so that it becomes a free function and we provide a declaration of <code>hijack</code> at namespace scope so that it participates in name-lookup.</li>
	
	<li>We can now invoke <code>Widget::forbidden()</code> on any <code>Widget</code> instance through the member-function address that is exposed to the <code>hijack</code> function.</li>
</ol>

<p>The key parts of the mechanism are shown in Listing 13.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
// HijackImpl is the mechanism for injecting the
// private member function pointer into the
// hijack function.
template &lt;
  typename ForbiddenFun,
  ForbiddenFun forbidden_fun
&gt;
class HijackImpl {
  // Definition of free function inside the class
  // template to give it access to the
  // forbidden_fun template argument.
  // Marking hijack as a friend prevents it from
  // becoming a member function.
  friend void hijack(Widget&amp; w) {
    (w.*forbidden_fun)();
  }
};
// Declaration in the enclosing namespace to make
// hijack available for name lookup.
void hijack(Widget&amp; w);

// Explicit instantiation of HijackImpl template
// bypasses access controls in the Widget class.
template class
HijackImpl&lt;
  decltype(&amp;Widget::forbidden),
  &amp;Widget::forbidden
&gt;;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 13</td>
	</tr>
</table>


<h3>Dealing with multiple definitions of the friend function</h3>

<p>Thereâ€™s still one more issue to overcome.<a href="#FN05"><sup>5</sup></a> To avoid violating the One Definition Rule, there must be one â€“ and only one â€“ explicit instantiation of a template (with given template arguments) across all translation units.</p>

<p>Consider what happens when our <code>HijackImpl</code> class is put in a header and is used in multiple translation units. The explicit instantiation of the class template must live outside of that header, otherwise it will appear in every translation unit that includes the header. We need to ensure that there is just one explicit template instantiation in the whole program. Whatâ€™s more, the linker is not actually required to report duplicate instantiations across multiple translation units, so it wonâ€™t even help us to avoid the problem. Thatâ€™s a recipe for a big maintenance headache.</p>

<p>The approach employed by the Folly library is to add an extra template parameter to the <code>HijackImpl</code> class and use it to accept an empty â€˜tagâ€™ struct which is defined in an anonymous namespace.</p>

<p>The anonymous namespace ensures that the tag parameter is of a different type in every translation unit. Every translation unit will therefore get its own unique explicit instantiation of the <code>HijackImpl</code> class.</p>

<p>The final solution is short, but packs in a surprising amount of nuance. See <span class="filename">widget.h</span> (Listing 14), <span class="filename">widget_hijack.h</span> (Listing 15) and <span class="filename">main.cc</span> (Listing 16.)</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#pragma once
#include &lt;iostream&gt;

class Widget {
 private:
  void forbidden() {
    std::cout &lt;&lt; &quot;Whoops...\n&quot;;
  }
};
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 14</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#pragma once
#include &quot;widget.h&quot;

namespace {
// This is a *different* type in every translation
// unit because of the anonymous namespace.
struct TranslationUnitTag {};
}

void hijack(Widget&amp; w);

template &lt;
  typename Tag,
  typename ForbiddenFun,
  ForbiddenFun forbidden_fun
&gt;
class HijackImpl {
  friend void hijack(Widget&amp; w) {
    (w.*forbidden_fun)();
  }
};

// Every translation unit gets its own unique
// explicit instantiation because of the
// guaranteed-unique tag parameter.
template class HijackImpl&lt;
  TranslationUnitTag,
  decltype(&amp;Widget::forbidden),
  &amp;Widget::forbidden
&gt;;
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 15</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &quot;widget.h&quot;
#include &quot;widget_hijack.h&quot;

int main() {
  Widget w;
  hijack(w); // Prints &quot;Whoops...&quot;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 16</td>
	</tr>
</table>

<h2>Conclusion</h2>

<p>Should you use this access-violation hack in production code? Almost certainly not. Well, not unless you enjoy the excitement and explosive unpredictability of maintaining extremely brittle code.</p>

<p>The C++ class member access rules are there to help authors of types to enforce invariants. If you fool the compiler into mutating private class members then youâ€™re likely to be violating the class invariants, which risks leaving the program in an invalid state. Youâ€™re also relying on intimate knowledge of internal implementation details, for which the library author is under no stability obligations.</p>

<p>Should you experiment with this access-violation technique outside of production code? Absolutely! Learning how to subvert a system in a safe environment is not only fun, but it helps to foster a deeper understanding of that system. Untangling the machiavellian mechanisms in the Folly library tested my knowledge of C++, requiring me to improve my understanding of the language features I encountered along the way. Itâ€™s almost as if someone had reached in to my brainâ€™s internal implementation and fiddled with its contents...</p>

<h2>A note on the origins of the technique</h2>

<p>The idea of using explicit template instantiation to bypass class access rules pre-dates the Folly library by a few years. The first mention I can find is from a 2010 blog post by Johannes Schaub [<a href="#[Schaub10]">Schaub10</a>], which describes a method using initialization of static class members. At the time, there was a discussion on the Boost mailing list about how the technique might prove to be a useful addition to the Boost serialization library.</p>

<p>A year later, Schaub offered the dubiously tempting promise of â€˜Safer Nastinessâ€™ in a follow-up blog post [<a href="#[Schaub11]">Schaub11</a>] in which he presented an improved version of the code. This removed the need for static class members and is much closer to whatâ€™s used today by the Folly library.</p>

<h2>Acknowledgements</h2>

<p>The author would like to thank Geoff Hester, Anthony Kirby and Kirsty McNaught for advice on early drafts of this article and Balog Pal for very patiently explaining some finer points of the one-definition rule.</p>

<h2>References</h2>

<p class="bibliomixed"><a id="[GitHub]"></a>[GitHub]: Facebook Folly on GitHub: <a href="https://github.com/facebook/folly">https://github.com/facebook/folly</a></p>

<p class="bibliomixed"><a id="[GotW]"></a>[GotW]: â€˜Uses and Abuses of Access Rights at <a href="http://www.gotw.ca/gotw/076.htm">http://www.gotw.ca/gotw/076.htm</a></p>

<p class="bibliomixed"><a id="[JSS19]"></a>[JSS19]: â€˜The Power of Hidden Friends in C++â€™ posted 25 June 2019: https://www.justsoftwaresolutions.co.uk/cplusplus/hidden-friends.html</p>

<p class="bibliomixed"><a id="[Saks18]"></a>[Saks18]: Dan Saks â€˜Making New Friendsâ€™ recorded at <em>CPPCon 18</em>, available at: <a href="https://www.youtube.com/watch?v=POa_V15je8Y">https://www.youtube.com/watch?v=POa_V15je8Y</a></p>

<p class="bibliomixed"><a id="[Schaub10]"></a>[Schaub10]: Johannes Schaub â€˜Access to private members. Thatâ€™s easy!â€™, posted 3 July 2010: <a href="http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html">http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html</a></p>

<p class="bibliomixed"><a id="[Schaub11]"></a>[Schaub11]: Johannes Schaub â€˜Access to private members: Safer nastinessâ€™, posted 30 December 2011: <a href="http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html">http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html</a> </p>

<p class="footnote"></p>

<h2>Footnotes</h2>

<ol>
	<li><a id="FN01"></a>C++17 introduced the <code>std::invoke</code> template, which gives a unified syntax for working with callables.</li>

	<li><a id="FN02"></a>C++20 will significantly relax the restrictions on non-type template parameters.</li>

	<li><a id="FN03"></a>I imagine itâ€™s staggeringly useful to <em>someone</em>.</li>

	<li><a id="FN04"></a>Readers lacking both a C++17 compiler and a certain amount of moral fibre have probably already worked out how to use a macro to remove the duplication in the template arguments.</li>
	
	<li><a id="FN05"></a>If you choose to ignore the pitchfork-bearing members of the C++ standards committee currently approaching your front door with a polite request that you stop doing this sort of thing at all.</li>
</ol>

<p class="bio"><span class="author"><b>Alastair Harrison</b></span> started out as a robotics researcher but accidentally became a C++ build engineer because nobody else wanted to do it. These days he appreciates the fact that his customers are all in the same building as him and that they are apparently unfazed by his eagerness to delete old code.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
