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

Merged
merged 8 commits into from
Feb 5, 2025
Merged

Split large unsafe function slide #2406

merged 8 commits into from
Feb 5, 2025

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
///
/// # 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!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, because these are u8 it's actually ok to overlap

`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.

@djmitche djmitche self-assigned this Jan 19, 2025
@djmitche djmitche requested a review from qwandor January 26, 2025 01:39
println!("a = {}, b = {}", a, b);
}
```
- Foreign functions in `extern "C"` blocks.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Rust 1.82 (with unsafe extern) foreign functions aren't necessarily unsafe, so I don't think these are really separate categories. Either way, a function is unsafe if it has some safety prerequisites which must be met to avoid undefined behaviour (or IO safety issues).

We should also update the example to use unsafe extern.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR was from before Rust 1.82, so perhaps we can land it and update, unless you think that splitting the slides doesn't make sense anymore?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed some commits to fix this and some other issues.

@@ -0,0 +1,32 @@
# Unsafe External Functions

All functions implemented in a foreign language are considered unsafe in Rust:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer true as of 1.82. We should talk about unsafe extern here.

@djmitche
Copy link
Collaborator

djmitche commented Feb 5, 2025

Let's merge it before Rust 1.85.0 :)

@qwandor qwandor merged commit c07ac40 into main Feb 5, 2025
37 checks passed
@qwandor qwandor deleted the split-unsafe-func-slide branch February 5, 2025 14:00
michael-kerscher pushed a commit to michael-kerscher/comprehensive-rust that referenced this pull request Feb 7, 2025
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 google#2445.

---------

Co-authored-by: Dustin J. Mitchell <[email protected]>
Co-authored-by: Andrew Walbran <[email protected]>
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.

3 participants