Unsafe
The unsafe Keyword
Turns off some of the compiler checks when applied to a block or function
Can call unsafe functions
Can dereference raw pointers
Can mutate global variables
Can use
extern
Foreign-Function Interface (FFI)
Consequence Of unsafe Abuse
Undefined Behavior (UB) when a program
Reads uninitialized memory
Creates invalid primitive values
- Invalid (including null) references or Boxes
bool
values that are not either 0 or 1enum
values with bogus discriminantschar
values that are not Unicode code pointsstr
values that are not UTF-8
Violates the lifetime or sharing rules with references
Dereferences a bogus pointer — points somewhere invalid or misaligned
Has a data race
Unwinds across FFI calls (obscure)
Violates standard library (or other) function contracts
UB may do nothing, may cause trouble, or may only cause trouble when the optimizer gets clever
Unsafe Function "Contracts"
Code should be written so that when properly used UB cannot occur
To inform the user of the code, a natural-language "contract" should be written specifying when an
unsafe
function can be safely calledThe contract should appear as a rustdoc comment before the function
unsafe
blocks inside a function must be used in such a way that no valid call to the function can cause UB
Unsafe Block "Contracts"
Every
unsafe
block should have a comment above that describesNeed (why is this block here?)
Safety (why is this block not UB?)
Raw Pointers
Declared for some type
T
via*const T
or*mut T
Are essentially just C pointers
Safe to do anything other than dereference them
Whole bunch of functions in
std::ptr
andstd::mem
for dealing with them: Rust has no raw pointer arithmetic.https://play.rust-lang.org/?gist=3467b73129409cade96bc2d4648966ad
A raw pointer owns nothing. Watch out for ownership problems
Can cast back to a ref with
as
, but be careful: default lifetime is'static
, while the referenced data probably isn't at all
Examples
RefWithFlag
from the bookhttps://github.com/ProgrammingRust/examples/blob/master/ref-with-flag/src/lib.rs
SIVec
FFI
Can declare external stuff with
extern
Watch out for "name mangling"
A bunch of stuff in
std::os
for dealing with C types and valuesNumeric types and sizes
Strings
Mostly outside the scope of this course.