Comments (8)
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.
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.
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.
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.
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.
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.
the compiler usually doesn't complain about doing undefined behavior because it doesn't know that you're doing undefined behavior.
from rust.
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)
- What should SIMD bitmasks look like? HOT 6
- clarify local_crate_exports_generics
- Miscompile for `riscv64gc-unknown-none-elf` using `inout()` in `asm!()`
- "you might have meant to return this value" suggests invalid syntax HOT 1
- tidy error: rustc-perf workspace doesn't have a Cargo.lock
- inherent method selection uses subtyping even when using paths
- "./x.py run miri --stage 1" no longer works HOT 5
- Bootstrap rustc wrapper verbosity not tracked
- Mutex isn't guaranteed to have atomic acquire/release semantics HOT 13
- `unnameable_types` lint emits incorrect visibility syntax HOT 1
- Suggest adding `{ .. }` on accidental usage of unit variant pattern for a struct variant HOT 3
- Arity errors suggest incorrect fix when there are name errors
- Doctest doesn't print valid assert file line and column location link HOT 10
- PowerPC test failures due to "similarly named associated type" HOT 3
- mir-opt tests fail on big endian HOT 4
- run-make: audit `ignore-cross-compile` rmake.rs tests HOT 3
- ICE: `overflow computing relative variant idx` in `rustc_const_eval/src/interpret/discriminant.rs` HOT 1
- ICE: `assertion failed: !obligations.has_infer()` in `rustc_hir_analysis/src/impl_wf_check/min_specialization.rs` with feature `min_specialization` HOT 3
- ICE: `unsize_into: invalid conversion` in `rustc_const_eval/src/interpret/cast.rs` HOT 1
- ICE: `write_immediate_to_mplace: invalid ScalarPair layout: TyAndLayout` with feature `adt_const_params` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.