Skip to content

[Guideline] Add guideline requiring wrapping unsafe code in safe abstractions #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/coding-guidelines/unsafety.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,66 @@

Unsafety
========

.. guideline:: All unsafe code shall be contained inside a sound safe abstraction
:id: gui_goekLVFUAjSM
:category: required
:status: draft
:release: -
:fls: fls_jep7p27kaqlp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simply references the entire Unsafety chapter of the FLS.
I wasn't able to find anything better, since the FLS is mostly about describing the Rust language itself, instead of how to use it correctly.

:decidability: undecidable
:scope: module
:tags: undefined-behavior

A safe abstraction is considered sound, when it is impossible to build a **safe** program using
the safe abstraction that invokes undefined behavior.

Safe abstractions shall be kept as small as possible and only include features that cannot be built
on top in safe Rust.

.. rationale::
:id: rat_3FoizIv2mZ4Z
:status: draft

Unsound safe abstractions leak the possibility for undefined behavior to safe Rust.
With violations of this rule, it would no longer suffice to only focus on unsafe modules
as the root cause of undefined behavior

Because safe abstractions are more difficult to review compared to safe code due to the
subtle semantics of unsafe operations, their size need to be minimized.

.. non_compliant_example::
:id: non_compl_ex_4Rj4YQkr1Nr4
:status: draft

The following module with a safe API uses unsafe code and is therefore a safe abstraction.
However, when passing a data slice with an index that is outside the range of the slice,
the safe function will cause undefined behavior.

.. code-block:: rust

pub mod bad {
pub fn get_value(data: &[i32], index: usize) -> i32 {
unsafe {
data.get_unchecked(usize)
}
}
}

.. compliant_example::
:id: compl_ex_aM7w7UbgSdvT
:status: draft

This safe module checks that its argument are valid, (i.e., they satisfy the safety
precondition of the unsafe operation) before performing the unsafe operation.

.. code-block:: rust

pub mod good {
pub fn get_value(data: &[i32], index: usize) -> i32 {
assert!(usize < data.len());
unsafe {
data.get_unchecked(usize)
}
}
}