Add integration browser tests for settings hardening.
[chromium-blink-merge.git] / native_client_sdk / doc_generated / reference / pnacl-undefined-behavior.html
blob272b4a7bfb813e97fe0e89577681f10eac81706f
1 {{+bindTo:partials.standard_nacl_article}}
3 <section id="pnacl-undefined-behavior">
4 <h1 id="pnacl-undefined-behavior">PNaCl Undefined Behavior</h1>
5 <div class="contents local" id="contents" style="display: none">
6 <ul class="small-gap">
7 <li><a class="reference internal" href="#overview" id="id2">Overview</a></li>
8 <li><a class="reference internal" href="#specification" id="id3">Specification</a></li>
9 <li><p class="first"><a class="reference internal" href="#behavior-in-pnacl-bitcode" id="id4">Behavior in PNaCl Bitcode</a></p>
10 <ul class="small-gap">
11 <li><a class="reference internal" href="#well-defined" id="id5">Well-Defined</a></li>
12 <li><p class="first"><a class="reference internal" href="#not-well-defined" id="id6">Not Well-Defined</a></p>
13 <ul class="small-gap">
14 <li><a class="reference internal" href="#potentially-fixable" id="id7">Potentially Fixable</a></li>
15 <li><a class="reference internal" href="#floating-point" id="id8">Floating-Point</a></li>
16 <li><a class="reference internal" href="#simd-vectors" id="id9">SIMD Vectors</a></li>
17 <li><a class="reference internal" href="#hard-to-fix" id="id10">Hard to Fix</a></li>
18 </ul>
19 </li>
20 </ul>
21 </li>
22 </ul>
24 </div><section id="overview">
25 <span id="undefined-behavior"></span><h2 id="overview"><span id="undefined-behavior"></span>Overview</h2>
26 <p>C and C++ undefined behavior allows efficient mapping of the source
27 language onto hardware, but leads to different behavior on different
28 platforms.</p>
29 <p>PNaCl exposes undefined behavior in the following ways:</p>
30 <ul class="small-gap">
31 <li><p class="first">The Clang frontend and optimizations that occur on the developer&#8217;s
32 machine determine what behavior will occur, and it will be specified
33 deterministically in the <em>pexe</em>. All targets will observe the same
34 behavior. In some cases, recompiling with a newer PNaCl SDK version
35 will either:</p>
36 <ul class="small-gap">
37 <li>Reliably emit the same behavior in the resulting <em>pexe</em>.</li>
38 <li>Change the behavior that gets specified in the <em>pexe</em>.</li>
39 </ul>
40 </li>
41 <li><p class="first">The behavior specified in the <em>pexe</em> relies on PNaCl&#8217;s bitcode,
42 runtime or CPU architecture vagaries.</p>
43 <ul class="small-gap">
44 <li>In some cases, the behavior using the same PNaCl translator version
45 on different architectures will produce different behavior.</li>
46 <li>Sometimes runtime parameters determine the behavior, e.g. memory
47 allocation determines which out-of-bounds accesses crash versus
48 returning garbage.</li>
49 <li>In some cases, different versions of the PNaCl translator
50 (i.e. after a Chrome update) will compile the code differently and
51 cause different behavior.</li>
52 <li>In some cases, the same versions of the PNaCl translator, on the
53 same architecture, will generate a different <em>nexe</em> for
54 defense-in-depth purposes, but may cause code that reads invalid
55 stack values or code sections on the heap to observe these
56 randomizations.</li>
57 </ul>
58 </li>
59 </ul>
60 </section><section id="specification">
61 <h2 id="specification">Specification</h2>
62 <p>PNaCl&#8217;s goal is that a single <em>pexe</em> should work reliably in the same
63 manner on all architectures, irrespective of runtime parameters and
64 through Chrome updates. This goal is unfortunately not attainable; PNaCl
65 therefore specifies as much as it can and outlines areas for
66 improvement.</p>
67 <p>One interesting solution is to offer good support for LLVM&#8217;s sanitizer
68 tools (including <a class="reference external" href="http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation">UBSan</a>)
69 at development time, so that developers can test their code against
70 undefined behavior. Shipping code would then still get good performance,
71 and diverging behavior would be rare.</p>
72 <p>Note that none of these issues are vulnerabilities in PNaCl and Chrome:
73 the NaCl sandboxing still constrains the code through Software Fault
74 Isolation.</p>
75 </section><section id="behavior-in-pnacl-bitcode">
76 <h2 id="behavior-in-pnacl-bitcode">Behavior in PNaCl Bitcode</h2>
77 <section id="well-defined">
78 <h3 id="well-defined">Well-Defined</h3>
79 <p>The following are traditionally undefined behavior in C/C++ but are well
80 defined at the <em>pexe</em> level:</p>
81 <ul class="small-gap">
82 <li>Dynamic initialization order dependencies: the order is deterministic
83 in the <em>pexe</em>.</li>
84 <li>Bool which isn&#8217;t <code>0</code>/<code>1</code>: the bitcode instruction sequence is
85 deterministic in the <em>pexe</em>.</li>
86 <li>Out-of-range <code>enum</code> value: the backing integer type and bitcode
87 instruction sequence is deterministic in the <em>pexe</em>.</li>
88 <li>Aggressive optimizations based on type-based alias analysis: TBAA
89 optimizations are done before stable bitcode is generated and their
90 metadata is stripped from the <em>pexe</em>; behavior is therefore
91 deterministic in the <em>pexe</em>.</li>
92 <li>Operator and subexpression evaluation order in the same expression
93 (e.g. function parameter passing, or pre-increment): the order is
94 defined in the <em>pexe</em>.</li>
95 <li>Signed integer overflow: two&#8217;s complement integer arithmetic is
96 assumed.</li>
97 <li>Atomic access to a non-atomic memory location (not declared as
98 <code>std::atomic</code>): atomics and <code>volatile</code> variables all lower to the
99 same compatible intrinsics or external functions; the behavior is
100 therefore deterministic in the <em>pexe</em> (see <a class="reference internal" href="/native-client/reference/pnacl-c-cpp-language-support.html#memory-model-and-atomics"><em>Memory Model and
101 Atomics</em></a>).</li>
102 <li>Integer divide by zero: always raises a fault (through hardware on
103 x86, and through integer divide emulation routine or explicit checks
104 on ARM).</li>
105 </ul>
106 </section><section id="not-well-defined">
107 <h3 id="not-well-defined">Not Well-Defined</h3>
108 <p>The following are traditionally undefined behavior in C/C++ which also
109 exhibit undefined behavior at the <em>pexe</em> level. Some are easier to fix
110 than others.</p>
111 <section id="potentially-fixable">
112 <h4 id="potentially-fixable">Potentially Fixable</h4>
113 <ul class="small-gap">
114 <li><p class="first">Shift by greater-than-or-equal to left-hand-side&#8217;s bit-width or
115 negative (see <a class="reference external" href="https://code.google.com/p/nativeclient/issues/detail?id=3604">bug 3604</a>).</p>
116 <ul class="small-gap">
117 <li>Some of the behavior will be specified in the <em>pexe</em> depending on
118 constant propagation and integer type of variables.</li>
119 <li>There is still some architecture-specific behavior.</li>
120 <li>PNaCl could force-mask the right-hand-side to <cite>bitwidth-1</cite>, which
121 could become a no-op on some architectures while ensuring all
122 architectures behave similarly. Regular optimizations could also be
123 applied, removing redundant masks.</li>
124 </ul>
125 </li>
126 <li><p class="first">Using a virtual pointer of the wrong type, or of an unallocated
127 object.</p>
128 <ul class="small-gap">
129 <li>Will produce wrong results which will depend on what data is treated
130 as a <cite>vftable</cite>.</li>
131 <li>PNaCl could add runtime checks for this, and elide them when types
132 are provably correct (see this CFI <a class="reference external" href="https://code.google.com/p/nativeclient/issues/detail?id=3786">bug 3786</a>).</li>
133 </ul>
134 </li>
135 <li><p class="first">Some unaligned load/store (see <a class="reference external" href="https://code.google.com/p/nativeclient/issues/detail?id=3445">bug 3445</a>).</p>
136 <ul class="small-gap">
137 <li>Could force everything to <cite>align 1</cite>; performance cost should be
138 measured.</li>
139 <li>The frontend could also be more pessimistic when it sees dubious
140 casts.</li>
141 </ul>
142 </li>
143 <li>Some values can be marked as <code>undef</code> (see <a class="reference external" href="https://code.google.com/p/nativeclient/issues/detail?id=3796">bug 3796</a>).</li>
144 <li>Reaching end-of-value-returning-function without returning a value:
145 reduces to <code>ret i32 undef</code> in bitcode. This is mostly-defined, but
146 could be improved (see <a class="reference external" href="https://code.google.com/p/nativeclient/issues/detail?id=3796">bug 3796</a>).</li>
147 <li><p class="first">Reaching “unreachable” code.</p>
148 <ul class="small-gap">
149 <li>LLVM provides an IR instruction called “unreachable” whose effect
150 will be undefined. PNaCl could change this to always trap, as the
151 <code>llvm.trap</code> intrinsic does.</li>
152 </ul>
153 </li>
154 <li>Zero or negative-sized variable-length array (and <code>alloca</code>) aren&#8217;t
155 defined behavior. PNaCl&#8217;s frontend or the translator could insert
156 checks with <code>-fsanitize=vla-bound</code>.</li>
157 </ul>
158 </section><section id="floating-point">
159 <span id="undefined-behavior-fp"></span><h4 id="floating-point"><span id="undefined-behavior-fp"></span>Floating-Point</h4>
160 <p>PNaCl offers a IEEE-754 implementation which is as correct as the
161 underlying hardware allows, with a few limitations. These are a few
162 sources of undefined behavior which are believed to be fixable:</p>
163 <ul class="small-gap">
164 <li>Float cast overflow is currently undefined.</li>
165 <li>Float divide by zero is currently undefined.</li>
166 <li>The default denormal behavior is currently unspecified, which isn&#8217;t
167 IEEE-754 compliant (denormals must be supported in IEEE-754). PNaCl
168 could mandate flush-to-zero, and may give an API to enable denormals
169 in a future release. The latter is problematic for SIMD and
170 vectorization support, where some platforms do not support denormal
171 SIMD operations.</li>
172 <li><code>NaN</code> values are currently not guaranteed to be canonical; see <a class="reference external" href="https://code.google.com/p/nativeclient/issues/detail?id=3536">bug
173 3536</a>.</li>
174 <li>Passing <code>NaN</code> to STL functions (the math is defined, but the
175 function implementation isn&#8217;t, e.g. <code>std::min</code> and <code>std::max</code>), is
176 well-defined in the <em>pexe</em>.</li>
177 </ul>
178 </section><section id="simd-vectors">
179 <h4 id="simd-vectors">SIMD Vectors</h4>
180 <p>SIMD vector instructions aren&#8217;t part of the C/C++ standards and as such
181 their behavior isn&#8217;t specified at all in C/C++; it is usually left up to
182 the target architecture to specify behavior. Portable Native Client
183 instead exposed <a class="reference internal" href="/native-client/reference/pnacl-c-cpp-language-support.html#portable-simd-vectors"><em>Portable SIMD Vectors</em></a> and
184 offers the same guarantees on these vectors as the guarantees offered by
185 the contained elements. Of notable interest amongst these guarantees are
186 those of alignment for load/store instructions on vectors: they have the
187 same alignment restriction as the contained elements.</p>
188 </section><section id="hard-to-fix">
189 <h4 id="hard-to-fix">Hard to Fix</h4>
190 <ul class="small-gap">
191 <li><p class="first">Null pointer/reference has behavior determined by the NaCl sandbox:</p>
192 <ul class="small-gap">
193 <li>Raises a segmentation fault in the bottom <code>64KiB</code> bytes on all
194 platforms, and on some sandboxes there are further non-writable
195 pages after the initial <code>64KiB</code>.</li>
196 <li>Negative offsets aren&#8217;t handled consistently on all platforms:
197 x86-64 and ARM will wrap around to the stack (because they mask the
198 address), whereas x86-32 will fault (because of segmentation).</li>
199 </ul>
200 </li>
201 <li><p class="first">Accessing uninitialized/free&#8217;d memory (including out-of-bounds array
202 access):</p>
203 <ul class="small-gap">
204 <li>Might cause a segmentation fault or not, depending on where memory
205 is allocated and how it gets reclaimed.</li>
206 <li>Added complexity because of the NaCl sandboxing: some of the
207 load/stores might be forced back into sandbox range, or eliminated
208 entirely if they fall out of the sandbox.</li>
209 </ul>
210 </li>
211 <li><p class="first">Executing non-program data (jumping to an address obtained from a
212 non-function pointer is undefined, can only do <code>void(*)()</code> to
213 <code>intptr_t</code> to <code>void(*)()</code>).</p>
214 <ul class="small-gap">
215 <li>Just-In-Time code generation is supported by NaCl, but is not
216 currently supported by PNaCl. It is currently not possible to mark
217 code as executable.</li>
218 <li>Offering full JIT capabilities would reduce PNaCl&#8217;s ability to
219 change the sandboxing model. It would also require a &#8220;jump to JIT
220 code&#8221; syscall (to guarantee a calling convention), and means that
221 JITs aren&#8217;t portable.</li>
222 <li>PNaCl could offer &#8220;portable&#8221; JIT capabilities where the code hands
223 PNaCl some form of LLVM IR, which PNaCl then JIT-compiles.</li>
224 </ul>
225 </li>
226 <li>Out-of-scope variable usage: will produce unknown data, mostly
227 dependent on stack and memory allocation.</li>
228 <li>Data races: any two operations that conflict (target overlapping
229 memory), at least one of which is a store or atomic read-modify-write,
230 and at least one of which is not atomic: this will be very dependent
231 on processor and execution sequence, see <a class="reference internal" href="/native-client/reference/pnacl-c-cpp-language-support.html#memory-model-and-atomics"><em>Memory Model and
232 Atomics</em></a>.</li>
233 </ul>
234 </section></section></section></section>
236 {{/partials.standard_nacl_article}}