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