Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

mgeisler
Copy link
Collaborator

@mgeisler mgeisler commented Oct 15, 2024

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.

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.
@mgeisler mgeisler requested a review from djmitche October 28, 2024 13:41
@@ -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)
Copy link
Collaborator

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.
Copy link
Collaborator

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.
Copy link
Collaborator

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.
Copy link
Collaborator

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.

src/unsafe-rust/calling-unsafe-functions.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants