Skip to content

Commit 9f15a8e

Browse files
committed
deploy: 01afd49
1 parent 758b628 commit 9f15a8e

61 files changed

Lines changed: 811 additions & 317 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ <h1>Introduction<a class="headerlink" href="#introduction" title="Link to this h
113113
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
114114
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
115115
provided by <a href="https://readthedocs.org">Read the Docs</a>.
116-
116+
117+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
118+
117119

118120
</footer>
119121
</div>

_sources/coding-guidelines/expressions.rst.txt

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,154 @@ Expressions
463463
/* ... */
464464
}
465465
466+
467+
.. guideline:: Integer shift shall only be performed through `checked_` APIs
468+
:id: gui_RHvQj8BHlz9b
469+
:category: required
470+
:status: draft
471+
:release: 1.7.0-latest
472+
:fls: fls_sru4wi5jomoe
473+
:decidability: decidable
474+
:scope: module
475+
:tags: numerics, reduce-human-error, maintainability, portability, surprising-behavior, subset
476+
477+
In particular, the user should only perform left shifts via the `checked_shl <https://doc.rust-lang.org/core/index.html?search=%22checked_shl%22>`_ function and right shifts via the `checked_shr <https://doc.rust-lang.org/core/index.html?search=%22checked_shr%22>`_ function. Both of these functions exist in `core <https://doc.rust-lang.org/core/index.html>`_.
478+
479+
This rule applies to the following primitive types:
480+
481+
482+
* ``i8``
483+
* ``i16``
484+
* ``i32``
485+
* ``i64``
486+
* ``i128``
487+
* ``u8``
488+
* ``u16``
489+
* ``u32``
490+
* ``u64``
491+
* ``u128``
492+
* ``usize``
493+
* ``isize``
494+
495+
.. rationale::
496+
:id: rat_3MpR8QfHodGT
497+
:status: draft
498+
499+
This is a Subset rule, directly inspired by `INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand <https://wiki.sei.cmu.edu/confluence/x/ItcxBQ>`_.
500+
501+
In Rust these out-of-range shifts don't give rise to Undefined Behavior; however, they are still problematic in Safety Critical contexts for two reasons.
502+
503+
504+
*
505+
**Reason 1: inconsistent behavior**
506+
507+
The behavior of shift operations depends on the compilation mode. Say for example, that we have a number ``x`` of type ``uN``\ , and we perform the operation
508+
509+
``x << M``
510+
511+
Then, it will behave like this:
512+
513+
+------------------+-----------------+-----------------------+-----------------------+
514+
| Compilation Mode | ``0 <= M < N`` | ``M < 0`` | ``N <= M`` |
515+
+==================+=================+=======================+=======================+
516+
| Debug | Shifts normally | Panics | Panics |
517+
+------------------+-----------------+-----------------------+-----------------------+
518+
| Release | Shifts normally | Shifts by ``M mod N`` | Shifts by ``M mod N`` |
519+
+------------------+-----------------+-----------------------+-----------------------+
520+
521+
..
522+
523+
Note: the behavior is exactly the same for the ``>>`` operator.
524+
525+
526+
Panicking in ``Debug`` is an issue by itself, however, a perhaps larger issue there is that its behavior is different from that of ``Release``. Such inconsistencies aren't acceptable in Safety Critical scenarios.
527+
528+
Therefore, a consistently-behaved operation should be required for performing shifts.
529+
530+
*
531+
**Reason 2: programmer intent**
532+
533+
There is no scenario in which it makes sense to perform a shift of negative length, or of more than ``N - 1`` bits. The operation itself becomes meaningless.
534+
535+
Therefore, an API that restricts the length of the shift to the range ``[0, N - 1]`` should be used instead of the ``<<`` and ``>>`` operators.
536+
537+
*
538+
**The Solution**
539+
540+
The ideal solution for this exists in ``core``\ : ``checked_shl`` and ``checked_shr``.
541+
542+
``<T>::checked_shl(M)`` returns a value of type ``Option<T>``\ , in the following way:
543+
544+
545+
* If ``M < 0``\ , the output is ``None``
546+
* If ``0 <= M < N`` for ``T`` of ``N`` bits, then the output is ``Some(T)``
547+
* If ``N <= M``\ , the output is ``None``
548+
549+
This API has consistent behavior across ``Debug`` and ``Release``\ , and makes the programmer intent explicit, which effectively solves this issue.
550+
551+
.. non_compliant_example::
552+
:id: non_compl_ex_O9FZuazu3Lcn
553+
:status: draft
554+
555+
As seen in the example below:
556+
557+
558+
* A ``Debug`` build **panics**\ ,
559+
*
560+
Whereas a ``Release`` build prints the values:
561+
562+
.. code-block::
563+
564+
61 << -1 = 2147483648
565+
61 << 4 = 976
566+
61 << 40 = 15616
567+
568+
This shows **Reason 1** prominently.
569+
570+
**Reason 2** is not seen in the code, because it is a reason of programmer intent: shifts by less than 0 or by more than ``N - 1`` (N being the bit-length of the value being shifted) are both meaningless.
571+
572+
.. code-block:: rust
573+
574+
fn bad_shl(bits: u32, shift: i32) -> u32 {
575+
bits << shift
576+
}
577+
578+
let bits : u32 = 61;
579+
let shifts = vec![-1, 4, 40];
580+
581+
for sh in shifts {
582+
println!("{bits} << {sh} = {}", bad_shl(bits, sh));
583+
}
584+
585+
.. compliant_example::
586+
:id: compl_ex_xpPQqYeEPGIo
587+
:status: draft
588+
589+
As seen in the example below:
590+
591+
592+
* Both ``Debug`` and ``Release`` give the same exact output, which addresses **Reason 1**.
593+
* Shifting by negative values is impossible due to the fact that ``checked_shl`` only accepts unsigned integers as shift lengths.
594+
* Shifting by more than ``N - 1`` (N being the bit-length of the value being shifted) returns a ``None`` value:
595+
.. code-block::
596+
597+
61 << 4 = Some(976)
598+
61 << 40 = None
599+
600+
The last 2 observations show how this addresses **Reason 2**.
601+
602+
.. code-block:: rust
603+
604+
fn good_shl(bits: u32, shift: u32) -> Option<u32> {
605+
bits.checked_shl(shift)
606+
}
607+
608+
let bits : u32 = 61;
609+
// let shifts = vec![-1, 4, 40];
610+
// ^--- Would not typecheck, as checked_shl
611+
// only accepts positive shift amounts
612+
let shifts = vec![4, 40];
613+
614+
for sh in shifts {
615+
println!("{bits} << {sh} = {:?}", good_shl(bits, sh));
616+
}

appendices/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ <h1>Appendices<a class="headerlink" href="#appendices" title="Link to this headi
122122
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
123123
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
124124
provided by <a href="https://readthedocs.org">Read the Docs</a>.
125-
125+
126+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
127+
126128

127129
</footer>
128130
</div>

appendices/licenses.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ <h2>Rust Reference MIT License<a class="headerlink" href="#rust-reference-mit-li
366366
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
367367
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
368368
provided by <a href="https://readthedocs.org">Read the Docs</a>.
369-
369+
370+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
371+
370372

371373
</footer>
372374
</div>

appendices/standards-matrices.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ <h1>Standards Matrices<a class="headerlink" href="#standards-matrices" title="Li
117117
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
118118
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
119119
provided by <a href="https://readthedocs.org">Read the Docs</a>.
120-
120+
121+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
122+
121123

122124
</footer>
123125
</div>

coding-guidelines/associated-items.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116

117117
<section id="associated-items">
118118
<h1>Associated Items<a class="headerlink" href="#associated-items" title="Link to this heading"></a></h1>
119-
<div class="need_container docutils container" id="SNCB-c93cac57">
119+
<div class="need_container docutils container" id="SNCB-7347d38f">
120120
<table class="need needs_grid_simple needs_layout_clean needs_style_none needs_type_guideline docutils align-default" id="gui_ot2Zt3dd6of1">
121121
<tbody>
122122
<tr class="need head row-odd"><td class="need head"><div class="needs_head line-block">
@@ -139,7 +139,7 @@ <h1>Associated Items<a class="headerlink" href="#associated-items" title="Link t
139139
</td>
140140
</tr>
141141
<tr class="need content row-odd"><td class="need content" colspan="1"><p>Any function shall not call itself directly or indirectly</p>
142-
<div class="need_container docutils container" id="SNCB-ccffe174">
142+
<div class="need_container docutils container" id="SNCB-5499a81b">
143143
<table class="need needs_grid_simple needs_layout_clean needs_style_none needs_type_rationale docutils align-default" id="rat_gvoKeVSKK8fD">
144144
<tbody>
145145
<tr class="need head row-odd"><td class="need head"><div class="needs_head line-block">
@@ -160,7 +160,7 @@ <h1>Associated Items<a class="headerlink" href="#associated-items" title="Link t
160160
</tbody>
161161
</table>
162162
</div>
163-
<div class="need_container docutils container" id="SNCB-b5389d24">
163+
<div class="need_container docutils container" id="SNCB-78643da2">
164164
<table class="need needs_grid_simple needs_layout_clean needs_style_none needs_type_non_compliant_example docutils align-default" id="non_compl_ex_MxqhjfkStJJy">
165165
<tbody>
166166
<tr class="need head row-odd"><td class="need head"><div class="needs_head line-block">
@@ -201,7 +201,7 @@ <h1>Associated Items<a class="headerlink" href="#associated-items" title="Link t
201201
</tbody>
202202
</table>
203203
</div>
204-
<div class="need_container docutils container" id="SNCB-35fa20bb">
204+
<div class="need_container docutils container" id="SNCB-5e11be18">
205205
<table class="need needs_grid_simple needs_layout_clean needs_style_none needs_type_compliant_example docutils align-default" id="compl_ex_9pK3h65rfceO">
206206
<tbody>
207207
<tr class="need head row-odd"><td class="need head"><div class="needs_head line-block">
@@ -281,7 +281,9 @@ <h1>Associated Items<a class="headerlink" href="#associated-items" title="Link t
281281
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
282282
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
283283
provided by <a href="https://readthedocs.org">Read the Docs</a>.
284-
284+
285+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
286+
285287

286288
</footer>
287289
</div>

coding-guidelines/attributes.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ <h1>Attributes<a class="headerlink" href="#attributes" title="Link to this headi
134134
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
135135
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
136136
provided by <a href="https://readthedocs.org">Read the Docs</a>.
137-
137+
138+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
139+
138140

139141
</footer>
140142
</div>

coding-guidelines/concurrency.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ <h1>Concurrency<a class="headerlink" href="#concurrency" title="Link to this hea
134134
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
135135
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
136136
provided by <a href="https://readthedocs.org">Read the Docs</a>.
137-
137+
138+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
139+
138140

139141
</footer>
140142
</div>

coding-guidelines/entities-and-resolution.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ <h1>Entities and Resolution<a class="headerlink" href="#entities-and-resolution"
134134
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
135135
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
136136
provided by <a href="https://readthedocs.org">Read the Docs</a>.
137-
137+
138+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
139+
138140

139141
</footer>
140142
</div>

coding-guidelines/exceptions-and-errors.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ <h1>Exceptions and Errors<a class="headerlink" href="#exceptions-and-errors" tit
134134
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
135135
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
136136
provided by <a href="https://readthedocs.org">Read the Docs</a>.
137-
137+
138+
This site is powered through <a href="https://www.netlify.com">Netlify</a>.
139+
138140

139141
</footer>
140142
</div>

0 commit comments

Comments
 (0)