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

Transmute of 0 to fn(..) -> .. is UB #13

Open
HeroicKatora opened this issue Mar 10, 2020 · 0 comments
Open

Transmute of 0 to fn(..) -> .. is UB #13

HeroicKatora opened this issue Mar 10, 2020 · 0 comments

Comments

@HeroicKatora
Copy link

HeroicKatora commented Mar 10, 2020

An internal macro (set_callback) uses a transmute to create several different function types. This is UB and indicative of an underlying problem.

libretro-backend/src/lib.rs

Lines 223 to 233 in 9248d74

macro_rules! set_callback {
($output: expr, $input: expr) => (
unsafe {
if $input == mem::transmute( 0 as usize ) {
$output = None;
} else {
$output = Some( $input );
}
}
)
}

According to the documentation of the function type:

Like references, function pointers are, among other things, assumed to not be null, so if you want to pass a function pointer over FFI and be able to accommodate null pointers, make your type Option<fn()> with your required signature.

The comparison might simply never succeed. What was likely intended is to use the types Option<RespectiveFn> for the input argument, and not only for those stored. This might have been enver observed as a possible (and somewhat likely) likely compilation assigns the null pointer directly to the Option field, thus changing the discriminant to the intended None.

As also aluded to in the documentation, the correct type for ffi is also Option<fn(_) -> _ which should be reflected in the exposed C-interface here.

Also unfortunately, the liberal use of unsafe in the macro hides the fact that the assignment to the static mut ENVIRONMENT_CALLBACK is the one that is actually unsafe and strictly speaking would require a form of synchronization, such as using an AtomicPtr.

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

No branches or pull requests

1 participant