Git Product home page Git Product logo

Comments (8)

git-blame avatar git-blame commented on June 20, 2024

Since passing an int* and returning a void* still works, our current fix is to write a wrapper similar to this:

 extern {
     fn initialize_wrapper(result: *mut i32) -> *mut c_void;
}
...
let mut result: i32 = 0;
let context = unsafe { initialize_wrapper(&mut result) };
// check result
...
// pass context to another C function

from rust.

ChrisDenton avatar ChrisDenton commented on June 20, 2024
extern {
    fn initialize(ctx: &*mut c_void) -> i32;
}

That looks like UB because your passing a shared reference rather than a mutable one. I.e. it should be defined as:

extern {
    fn initialize(ctx: &mut *mut c_void) -> i32;
}

from rust.

git-blame avatar git-blame commented on June 20, 2024

It's just odd that it worked up to 1.77.0 and also with 1.78.0, there are still some weird conditions where it still works (see description).

from rust.

Urgau avatar Urgau commented on June 20, 2024

I believe that what you're trying to do is UB, by taking an immutable reference and trying to assign to it you're violating the immutability requirements of that reference.

At least that's what I think is happening when I try to recreate your sample code in pure Rust, playground.

Using a mutable reference should already be a step in the right direction.

extern {
    fn initialize(ctx: &mut *mut c_void) -> i32;
}
// ...
let mut context = ptr::null_mut();
let result = unsafe { initialize(&mut context) };

from rust.

Urgau avatar Urgau commented on June 20, 2024

It's just odd that it worked up to 1.77.0 and also with 1.78.0, there are still some weird conditions where it still works (see description).

Optimization around aliasing are (if I remember correctly) only done at higher level of optimization.

from rust.

git-blame avatar git-blame commented on June 20, 2024

Thanks, using the &mut option works. But I'm surprised that the 1.78.0 compiler did not complain about passing immutable reference since it behaves differently than previous versions.

from rust.

Nilstrieb avatar Nilstrieb commented on June 20, 2024

the compiler usually doesn't complain about doing undefined behavior because it doesn't know that you're doing undefined behavior.

from rust.

Nilstrieb avatar Nilstrieb commented on June 20, 2024

here the compiler just assumed that you wouldn't modify it, and it has no way of knowing that you are modifying it. you just have to be careful. i don't think there's a tool that could have helped in this case, but in general, make sure to use tools like address sanitizer to check your unsafe code (and for unsafe code without FFI, Miri).

closing as not a bug

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.