@@ -229,7 +229,7 @@ the C standard library.
229
229
230
230
### Non-robust types: references, function pointers, enums
231
231
232
- A *trap representation* of a particular type is a representation (pattern of
232
+ A _trap representation_ of a particular type is a representation (pattern of
233
233
bits) that respects the type's representation constraints (such as size and
234
234
alignment) but does not represent a valid value of this type and leads to
235
235
undefined behavior.
@@ -253,15 +253,15 @@ the C-compatible types:
253
253
- compound types that contain a field of a non-robust type.
254
254
255
255
On the other hand, integer types (`u*`/`i*`), packed compound types that contain
256
- no non-robust fields, for instance are *robust types* .
256
+ no non-robust fields, for instance are _robust types_ .
257
257
258
258
Non-robust types are a difficulty when interfacing two languages. It revolves
259
259
into deciding **which language of the two is responsible in asserting the
260
260
validity of boundary-crossing values** and how to do it.
261
261
262
262
> ### Rule {{#check FFI-CKNONROBUST | Do not use unchecked non-robust foreign values}}
263
263
>
264
- > In a secure Rust development, there must not be any use of *unchecked* foreign
264
+ > In a secure Rust development, there must not be any use of _unchecked_ foreign
265
265
> values of non-robust types.
266
266
>
267
267
> In other words, either Rust translates robust types to non-robust types
@@ -288,10 +288,10 @@ function references, and enums, and are discussed below.
288
288
>
289
289
> Rust's `bool` has been made equivalent to C99's `_Bool` (aliased as `bool`
290
290
> in `<stdbool.h>`) and C++'s `bool`. However, loading a value other than 0 and
291
- > 1 as a `_Bool`/`bool` is an undefined behavior *on both sides* .
291
+ > 1 as a `_Bool`/`bool` is an undefined behavior _on both sides_ .
292
292
> Safe Rust ensures that. Standard-compliant C and C++ compilers ensure that no
293
- > value but 0 and 1 can be *stored* in a `_Bool`/`bool` value but cannot
294
- > guarantee the absence of an *incorrect reinterpretation* (e.g., union types,
293
+ > value but 0 and 1 can be _stored_ in a `_Bool`/`bool` value but cannot
294
+ > guarantee the absence of an _incorrect reinterpretation_ (e.g., union types,
295
295
> pointer cast). To detect such a bad reinterpretation, sanitizers such as
296
296
> LLVM's `-fsanitize=bool` may be used.
297
297
@@ -317,7 +317,7 @@ Rust references may be used reasonably with other C-compatible languages
317
317
including C variants allowing for non-null type checking, e.g. Microsoft SAL
318
318
annotated code.
319
319
320
- On the other hand, Rust's *pointer types* may also lead to undefined behaviors
320
+ On the other hand, Rust's _pointer types_ may also lead to undefined behaviors
321
321
but are more verifiable, mostly against `std/core::ptr::null()` (C's `(void*)0`)
322
322
but also in some context against a known valid memory range (particularly in
323
323
embedded systems or kernel-level programming). Another advantage of using Rust
@@ -345,7 +345,7 @@ an `unsafe` block or function.
345
345
> In a secure Rust development, every foreign references that is transmitted to
346
346
> Rust through FFI must be **checked on the foreign side** either automatically
347
347
> (for instance, by a compiler) or manually.
348
- >
348
+ >
349
349
> Exceptions include Rust references in an opaque wrapping that is created
350
350
> and manipulated only from the Rust side and `Option`-wrapped references
351
351
> (see Note below).
@@ -358,7 +358,7 @@ an `unsafe` block or function.
358
358
> pointer must check their validity beforehand.
359
359
> In particular, pointers must be checked to be non-null before any use.
360
360
>
361
- > Stronger approaches are advisable when possible. They includes checking
361
+ > Stronger approaches are advisable when possible. They include checking
362
362
> pointers against known valid memory range or tagging (or signing) pointers
363
363
> (particularly applicable if the pointed value is only manipulated from Rust).
364
364
@@ -378,7 +378,7 @@ pub unsafe extern fn add_in_place(a: *mut u32, b: u32) {
378
378
```
379
379
380
380
Note that the methods ` as_ref ` and ` as_mut ` (for mutable pointers) allows easy
381
- access to a reference while ensuring a null check in a very * Rusty * way.
381
+ access to a reference while ensuring a null check in a very _ Rusty _ way.
382
382
On the other side in C, it can be used as follows:
383
383
384
384
``` c
@@ -463,7 +463,7 @@ severe consequences on software security. Unfortunately, checking an `enum`
463
463
value at the FFI boundary is not simple on both sides.
464
464
465
465
On the Rust side, it consists to actually use an integer type in the `extern`
466
- block declaration, a *robust* type, and then to perform a checked conversion
466
+ block declaration, a _robust_ type, and then to perform a checked conversion
467
467
to the enum type.
468
468
469
469
On the foreign side, it is possible only if the other language allows for
@@ -560,13 +560,13 @@ The same is true for other kind of resources such as sockets or files.
560
560
Rust tracks variable ownership and lifetime to determine at compilation time if
561
561
and when memory should be deallocated. Thanks to the ` Drop ` trait, one can
562
562
exploit this system to reclaim other kind of resources such as file or network
563
- access. * Moving * some piece of data from Rust to a foreign language means also
563
+ access. _ Moving _ some piece of data from Rust to a foreign language means also
564
564
abandoning the possible reclamations associated with it.
565
565
566
566
> ### Rule {{#check FFI-MEM-NODROP | Do not use types that implement ` Drop ` at FFI boundary}}
567
567
>
568
568
> In a secure Rust development, Rust code must not implement ` Drop ` for any
569
- > types that are directly transmitted to foreign code (i.e. not through a
569
+ > types that are directly transmitted to foreign code (i.e. not through a
570
570
> pointer or reference).
571
571
572
572
In fact, it is advisable to only use ` Copy ` types. Note that ` *const T ` is
@@ -594,10 +594,10 @@ wrapper around the foreign type:
594
594
595
595
> ### Recommendation {{#check FFI-MEM-WRAPPING | Wrap foreign data in memory releasing wrapper}}
596
596
>
597
- > In a secure Rust development, any non-sensitive foreign piece of data that are
597
+ > In a secure Rust development, any non-sensitive foreign piece of data that is
598
598
> allocated and deallocated in the foreign language should be encapsulated in a
599
599
> ` Drop ` type in such a way as to provide automatic deallocation in Rust,
600
- > through an automatic call to the foreing language deallocation routine.
600
+ > through an automatic call to the foreign language deallocation routine.
601
601
602
602
A simple example of Rust wrapping over an external opaque type:
603
603
@@ -653,7 +653,7 @@ impl Drop for Foo {
653
653
654
654
> ### Warning
655
655
>
656
- > Because panics may lead to not running the ` Drop::drop ` method this solution
656
+ > Because panics may lead to not running the ` Drop::drop ` method, this solution
657
657
> is not sufficient for sensitive deallocation (such as wiping sensitive data)
658
658
> except if the code is guaranteed to never panic.
659
659
>
@@ -834,7 +834,7 @@ trick: the linker fails if a non-trivially-dead branch leads to `panic!`.
834
834
> Interfacing a library written in another language in Rust should be done in
835
835
> two parts:
836
836
>
837
- > - a low-level, possibly * hidden * , module that closely translates the original
837
+ > - a low-level, possibly _ hidden _ , module that closely translates the original
838
838
> C API into ` extern ` blocks,
839
839
> - a safe wrapping module that ensures memory safety and security invariants at
840
840
> the Rust level.
0 commit comments