本页尚未翻译,以下为英文原文。
#4.15. unsafe - function modifier and block expression
本页内容
- 4.15.1. unsafe fn - function modifier
- With method receivers
- Inside extern "C" blocks
- 4.15.2. unsafe { ... } - block expression
- Operations allowed in the block
- Narrowing the scope
- 4.15.3. Call rules
- unsafe fn types
- 4.15.4. In traits
- unsafe trait
- unsafe impl
- 4.15.5. Responsibility model
- 4.15.6. Relationship to implicit unsafe
- 4.15.7. Comparison with other languages
- 4.15.8. Undefined behavior
The unsafe keyword appears in two forms that visibly confine regions whose safety the compiler does not guarantee: the modifier on function / method declarations and the block expression. The two work as a pair: a function declared unsafe fn must be called inside an unsafe { ... } block.
unsafe_fn ::= "unsafe" fn_decl
unsafe_block ::= "unsafe" block_expr
This model is isomorphic to Rust's unsafe; its purpose is to make dangerous regions explicit in the code.
#4.15.1. unsafe fn - function modifier
Placing unsafe before a function or method declaration means that the function's body is outside the guarantees of the safe abstraction. The signature states directly that the caller is responsible for satisfying the preconditions.
impl<T> Ptr<T> {
unsafe fn read(self) -> T;
unsafe fn write(self, value: T);
}
extern "C" {
unsafe fn strlen(s: ConstPtr<c_char>) -> usize;
}
unsafe fn transmute_unchecked<A, B>(a: A) -> B { ... }
Inside the body of an unsafe fn itself, unsafe calls are allowed without a separate unsafe { ... } block. Because the signature already declares unsafe, the whole body is treated as an unsafe scope.
unsafe fn copy_two<T>(p: Ptr<T>) -> (T, T) {
let a = p.read(); // body is an unsafe scope: no explicit block needed
let b = p.add(1).read();
(a, b)
}
#With method receivers
Methods in impl follow the same form.
impl Buffer {
unsafe fn raw_data(self) -> Ptr<u8>;
unsafe fn raw_data_mut(ref self) -> Ptr<u8>;
}
unsafe does not conflict with receiver notations such as ref self / self, and it comes first in the function header.
#Inside extern "C" blocks
User-declared C ABI functions are all implicitly unsafe, because their definitions are outside the compiler's control (extern blocks). Writing it explicitly (unsafe fn) is recommended; if omitted, it is treated as unsafe automatically.
The same holds for every extern function, extern "nabi" declarations of the runtime's symbols included: calling one outside an unsafe scope is an error (E0061).
extern "C" {
fn malloc(size: usize) -> CPtr<u8>; // treated as unsafe automatically
unsafe fn free(ptr: CPtr<u8>); // explicit (recommended)
}
#4.15.2. unsafe { ... } - block expression
Operations such as calling unsafe functions or dereferencing raw pointers are allowed only inside an unsafe { ... } block. Like an ordinary block expression, the block's result is the value of its last expression.
let n: usize = unsafe { strlen(cs.as_ptr()) };
let v: i32 = unsafe {
let p = ptr.add(idx);
p.read()
};
unsafe {
free(ptr);
}
#Operations allowed in the block
Inside an unsafe { ... } block, the following operations are additionally allowed:
- Calling functions / methods declared
unsafe fn - Dereferencing raw pointers (
Ptr<T>,ConstPtr<T>,ManyPtr<T>) (p.read(),p.write(v)) - Directly calling dangerous functions of the
bare::*modules - Reading
unionfields (bit reinterpretation, union). Writing fields is allowed withoutunsafe. - Using a value after it was handed to another owner (stdlib 2.13.9): the block's author guarantees no other thread uses it at the same time.
All other ordinary safe operations work the same inside an unsafe block: unsafe adds capabilities; it does not change existing meaning.
#Narrowing the scope
unsafe blocks should be kept as narrow as possible. Instead of wrapping a whole function in unsafe { ... }, wrapping only the one line that is actually dangerous makes the safety of the other lines visibly clear.
// recommended: narrow unsafe scope
fn safe_wrapper(p: Ptr<i32>) throws -> i32 {
let validated = try check_ptr(p);
unsafe { validated.read() } // only the dangerous operation is unsafe
}
// not recommended: wide unsafe scope
fn safe_wrapper(p: Ptr<i32>) throws -> i32 {
unsafe {
let validated = try check_ptr(p);
validated.read()
}
}
#4.15.3. Call rules
Calls of unsafe fn follow these rules.
| Call position | Can call unsafe fn? |
|---|---|
| ordinary function body | ✗ (compile error) |
unsafe fn body | ✓ (automatic unsafe scope) |
unsafe { ... } block | ✓ |
Violating the rules is a compile error:
fn safe_caller(p: Ptr<i32>) -> i32 {
p.read() // ERROR (E0061): call to unsafe function `read` outside an `unsafe` block
}
There are two fixes: wrap the call in unsafe { ... }, or mark the caller itself unsafe fn.
#unsafe fn types
When an unsafe fn or a C function is used as a value, its type is unsafe fn(..) -> R (4.4). Calling a value of this type requires unsafe. A safe fn(..) -> R value may be used in an unsafe fn(..) -> R position, but not the reverse.
let f: unsafe fn(usize) -> CPtr<u8> = malloc; // C function value
let p = unsafe { f(16) };
let g: unsafe fn(i32) -> i32 = |x| x + 1; // OK: safe → unsafe
let h: fn(usize) -> CPtr<u8> = f; // ERROR: unsafe → safe
#4.15.4. In traits
Trait methods may also use unsafe fn. The impl side must implement them with the same signature, unsafe included: a safe impl of an unsafe fn method, or an unsafe fn impl of a safe one, is an error (E0074). Callers follow the same rules as for ordinary call positions.
trait RawAccess {
unsafe fn raw_get(self, idx: usize) -> u8;
}
impl RawAccess for Buffer {
unsafe fn raw_get(self, idx: usize) -> u8 {
self.ptr.add(idx).read()
}
}
let buf: Buffer = ...;
let b = unsafe { buf.raw_get(0) };
#unsafe trait
A trait whose impls promise something the compiler cannot check is declared unsafe trait. Every impl of it is unsafe impl; a plain impl is a compile error (E0078) that gives the unsafe impl form. Using the trait (bounds, calls) needs no unsafe. bare::mem::Pod (every bit pattern is a value) is one (stdlib 1.3.3).
/// Every all-zero bit pattern is a value.
unsafe trait Zeroable {
fn zeroed() -> Self;
}
unsafe impl Zeroable for i32 {
fn zeroed() -> i32 { 0 }
}
#unsafe impl
unsafe impl implements an unsafe trait, or the auto marker traits Send / Sync by hand. It marks that the author guarantees what the trait promises (for Send / Sync, that the type is safe to cross thread boundaries). Using unsafe impl for any other trait is a compile error (2.9.1).
unsafe impl Send for Handle { }
#4.15.5. Responsibility model
unsafe does not mean the compiler gives up safety checking. It means that the following two responsibilities transfer to the caller:
- Memory safety: the responsibility not to cause undefined behavior (4.15.8) such as dangling pointers, misaligned access or data races.
- Preconditions: the responsibility to satisfy the usage conditions promised by the
unsafe fn's documentation (e.g., that a pointer points to a valid object, that a length is within the buffer).
unsafe { ... } is the visible signal that the caller accepts these two responsibilities, so dangerous regions are apparent at a glance during code review.
#4.15.6. Relationship to implicit unsafe
The following positions are treated as unsafe scopes automatically, without an explicit unsafe:
- The body of an
unsafe fn
This automatic treatment reduces noisy repetition of unsafe { ... } in commonly occurring dangerous regions.
#4.15.7. Comparison with other languages
| Language | unsafe notation model |
|---|---|
| Rust | unsafe fn + unsafe { ... } (isomorphic) |
| C / C++ | no separate notation; all code is potentially unsafe |
| Swift | @unsafe annotation / withUnsafePointer functional pattern |
| Go | unsafe package (separate import); no block notation |
| Zig | explicit builtins such as @ptrCast; no block notation |
| Nabi | adopts Rust-style unsafe fn + unsafe { ... } |
Nabi follows Rust's model as-is. Visual separation of dangerous regions is marked consistently with a single keyword, which benefits both code review and static analysis.
#4.15.8. Undefined behavior
This is the complete list of undefined behavior in Nabi. A program whose run does one of these things has no meaning: the compiler may assume it never happens, and anything may follow. Every item needs unsafe code (or foreign code) to happen; safe Nabi cannot cause any of them. Each library chapter that names an undefined behavior names one of these kinds.
Memory and pointers:
- Reading or writing through a pointer that does not point at live memory of its type: null, dangling (the local, pin or allocation it came from is gone), out of the bounds of its object, or past a
ManyPtr/GcBuf/Slice::from_raw_partsrange the caller vouched for (stdlib 1.1, stdlib 1.3.9). - Using a raw pointer into a GC object after the pin that made it valid was released (stdlib 1.6).
- Reading or writing through a misaligned pointer, as
ptr_castcan make (stdlib 1.2.2). - Dereferencing a pointer whose provenance was lost, other than one made by
from_exposed_provenancefrom an exposed address (stdlib 1.1.2). - Writing through a pointer to a non-
mutstatic, or throughconst_cast's result to memory that is not writable (stdlib 1.1.2, stdlib 1.2.4). copy_nonoverlappingon overlapping ranges (stdlib 1.3.4).- Using a value after
drop_in_placeran its drop glue, or running it twice (stdlib 1.3.4).
Values:
- Reading uninitialized memory, at any type:
MaybeUninit::assume_initbefore the value was written, or a read of bytes nothing wrote (stdlib 1.3). - Producing a value that is not valid for its type, by a
unionread (2.5.2),transmute_unchecked, a raw write or a falseunsafe impl Pod: aboolother than0/1, acharthat is not a Unicode scalar value, an enum or sum tag no variant has, a GC reference that is not null or a live object, a non-noneextern "C" fnthat is null (stdlib 1.2.6, 5.9.4). - Breaking the invariant of a checked wrapper made with
new_unchecked: a zeroNonZero, a NaNNonNan, a NaN or infiniteFinite, a negativePositive(stdlib 2.2.6). - A GC reference in memory outside the GC heap that the collector does not scan: a
ManyPtr::sliceover such memory holding GC references, or one stored there by a raw write (stdlib 1.1.4).
Threads:
- A data race (7.9.4): through raw pointers or
GcBuf, a falseunsafe impl Send/Sync,Mutex::new_unsharedon a value something else reaches, a hand-off check skipped insideunsafe, or C code that touches the environment whileenv::set_varruns (stdlib 3.5).
Foreign code:
- A foreign function,
staticor callback whose declared type does not match the foreign definition, or a GC value passed where C holds raw bits (5.9.4, 5.9.6). - An exception or unwind from foreign code crossing a Nabi frame (5.9.2).
- A null
char*returned where the declaration saysstr; C keeping astrargument's pointer after the call returns (5.9.4). CStr::from_ptron a pointer that is dangling or points at bytes without a NUL (stdlib 1.7.2).
Everything else is defined, in safe and unsafe code alike. In particular these are not undefined behavior: integer overflow (a panic where checks are on, wrapping where they are off, stdlib 2.3), an out-of-bounds index or slice of a collection (a panic), division by zero (a panic), stack overflow (the process aborts, 7), every floating-point operation, NaN and infinity included (stdlib 2.1.4), reading a union field (bit reinterpretation; only the value it makes can be invalid, item 9), a transmute_unchecked between types of different sizes (the missing bytes read as zero), a deadlock, and a leak.
Found a mistake? Edit this page on GitHub. Last updated