-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Split large unsafe function slide #2406
base: main
Are you sure you want to change the base?
Conversation
The old slice was doing several things at the same time: demonstrating both external functions as well as unsafe Rust functions. We now treat those two topics separately. In addition, the “Calling Unsafe Functions” heading has become its own slide with a non-crashing example that shows what can go wrong if an argument is misunderstood in a call to an unsafe function. The old example didn’t actually illustrate the danger clearly: it would produce mangled UTF-8 output, which the Playground server refuses to print.
@@ -207,6 +207,9 @@ | |||
- [Mutable Static Variables](unsafe-rust/mutable-static.md) | |||
- [Unions](unsafe-rust/unions.md) | |||
- [Unsafe Functions](unsafe-rust/unsafe-functions.md) | |||
- [Unsafe External Functions](unsafe-rust/unsafe-extern-c-functions.md) | |||
- [Unsafe Rust Functions](unsafe-rust/unsafe-rust-functions.md) | |||
- [Calling Unsafe Functions](unsafe-rust/calling-unsafe-functions.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For subslides, we've used a subdirectory to contain the subslides. Also, only the top-level slide has the timing information. See src/methods-and-traits/traits for an example.
/// | ||
/// # Safety | ||
/// | ||
/// The pointers must be valid and properly aligned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
..and must not overlap!
|
||
Note that unsafe code is allowed within an unsafe function without an `unsafe` | ||
block. We can prohibit this with `#[deny(unsafe_op_in_unsafe_fn)]`. Try adding | ||
it and see what happens. This will likely change in a future Rust edition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust-lang/rust#120535 in the 2024 edition, in fact!
`abs` is unsafe because it is an external function (FFI). Calling external | ||
functions is usually only a problem when those functions do things with pointers | ||
which might violate Rust's memory model, but in general any C function might | ||
have undefined behaviour under any arbitrary circumstances. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth mentioning here that there is no automatic checking of the Rust function signature against that of the target function? For example, in this case the C function might take uint64_t
and this would cause UB.
Co-authored-by: Dustin J. Mitchell <[email protected]>
The old slice was doing several things at the same time: demonstrating
both external functions as well as unsafe Rust functions.
We now treat those two topics separately. In addition, the “Calling
Unsafe Functions” heading has become its own slide with a non-crashing
example that shows what can go wrong if an argument is misunderstood
in a call to an unsafe function. The old example didn’t actually
illustrate the danger clearly: it would produce mangled UTF-8 output,
which the Playground server refuses to print.
Part of #2445.