Comments (13)
I have a demo of the UB. Note that it requires 2 crates (or perhaps crate+some name mangling magic) to demonstrate: https://godbolt.org/z/3j4YhnzK5
from miri.
Can't it be done in a single crate with the per-function target_feature attribute?
from miri.
Can't it be done in a single crate with the per-function target_feature attribute?
It can indeed. https://godbolt.org/z/Tj3Gj43sq
I thought llvm was even weirder with function-specific #[target_feature]
, but I guess that's just clang.
from miri.
I tried turning this into a Miri test case:
use std::arch::x86_64::*;
use std::mem::transmute;
#[no_mangle]
#[target_feature(enable="avx")]
pub unsafe extern "C" fn foo(_y: f32, x: __m256) -> __m256{
x
}
pub fn bar(x: __m256) -> __m256 {
// The first and second argument get mixed up here since caller
// and callee do not have the same feature flags.
unsafe{foo(0.0,x)}
}
fn assert_eq_m256(a: __m256, b: __m256) {
unsafe {
assert_eq!(transmute::<_, [f32; 8]>(a), transmute::<_, [f32; 8]>(b))
}
}
fn main() {
let input = unsafe { _mm256_set1_ps(1.0) };
let copy = bar(input);
assert_eq_m256(input, copy);
}
However, this already gets rejected:
error: Undefined Behavior: calling a function that requires unavailable target features: avx
--> src/main.rs:23:26
|
23 | let input = unsafe { _mm256_set1_ps(1.0) };
| ^^^^^^^^^^^^^^^^^^^ calling a function that requires unavailable target features: avx
|
Does that mean we are good, or are there other ways to trigger this ABI mismatch?
from miri.
You can construct it using an explicit #[target_feature] attribute, or core::mem::transmute.
Also, I'd probably compare [u32;8] or [u64;4] (since f32 bit patterns can be interpreted as a NaN).
from miri.
from miri.
Unless the issue is #[target_feature(enable="avx")]
in general, you can make a __m256 by transmuting an [f32;8]
or [u32;8]
. If the issue is that miri can't distinguish between compile-time enabled features and runtime enabled features, that may be an issue that needs to be fixed.
from miri.
I am completely lost. Can you show some code that somehow uses transmute
to cause an issue here?
Miri dynamically considers exactly those features to be available that are enabled via -C target-feature
.
from miri.
Ok yeah, you'd need to have a way to separate statically enabled and dynamically enabled features to check this particular UB.
from miri.
Okay so I think Miri is good here for UB detection for now, but eventually it could be nice to support -Zmiri-target-feature
to enable a feature dynamically (but not statically) and then this will become a concern again.
from miri.
I'm mostly worried about code that will check is_<arch>_feature_enabled!()
then cross the divide with #[target_feature(enable)]
from miri.
Yeah, I understand. In Miri feature_enabled
will always return false
for non-statically-available features so there's no UB we are currently missing here. But ideally we'd support setting the set of dynamically available features.
from miri.
Closing in favor of #3099 and #3100. Thanks for the help!
from miri.
Related Issues (20)
- miri-script: use a proper CLI arg parsing tool HOT 1
- macOS: support calling native functions
- Support more ways of using mmap
- Complete basic support for wasm32-wasip2 HOT 9
- Complete basic support for android HOT 3
- strerror based on ErrorKind is technically valid but surprising HOT 3
- Miri accepts an open-coded compare-exchange loop but rejects the equivalent code using fetch_update HOT 5
- Android: implement support for pthread synchronization primitives
- Android: make std thread APIs work
- Big `Vec::try_reserve` OOMs Miri (slowly) HOT 12
- Clearer error message for unsupported flags: print names of flags (if possible) HOT 1
- "local crate" detection does not work any more
- Stuck when running `cargo miri test` HOT 3
- ICE due to overflow when using a large timeout with futexes HOT 2
- Possible false positive of stacked borrow rules HOT 1
- simd_bitmask: support vectors larger than 64 elements
- Miri does not detect invalid uses of alchemy (std::ptr::from_raw_parts w/ invalid vtable pointer) HOT 5
- Implement blocking support for eventfd and socketpair HOT 6
- Memory-reusing custom allocator fails in miri HOT 1
- Use strict_{add,sub,...} instead of checked_{add,sub,...}.unwrap()
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 miri.