-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
[MIR] Handle call return values that need to be casted properly. #34054
Conversation
// does a `PointerCast` and a `Store`. Unfortunately, | ||
// that may run afoul of strict aliasing rules, leading | ||
// to wacky results after optimization since LLVM | ||
// may just replace it with an `undef`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem has nothing to do with aliasing rules... the problem is the buffer overflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer copying the comment from callee
.
LGTM but to be sure r? @dotdash |
Seems ok except for the misleading comment. But you might actually want to extract that into some function (not sure if For example: #![crate_type="lib"]
struct S {
x: i8,
y: i8,
}
extern {
fn foo(s: S);
}
pub fn bla() {
unsafe {
foo(S { x: 4, y: 8 });
}
} Results in: define void @_ZN3ffi3bla17h4acc14686285caf0E() unnamed_addr #0 {
entry-block:
%temp0 = alloca {}
%temp1 = alloca %S
%arg = alloca %S
br label %start
start: ; preds = %entry-block
%0 = getelementptr inbounds %S, %S* %temp1, i32 0, i32 0
store i8 4, i8* %0
%1 = getelementptr inbounds %S, %S* %temp1, i32 0, i32 1
store i8 8, i8* %1
%2 = load %S, %S* %temp1
store %S %2, %S* %arg
%3 = bitcast %S* %arg to { i64 }*
%4 = load { i64 }, { i64 }* %3, align 1
call void @foo({ i64 } %4)
br label %bb2
end: ; preds = %bb3
ret void
bb2: ; preds = %start
br label %bb3
bb3: ; preds = %bb2
br label %end
} Notice how we try to load 8 bytes out of a 2 byte allocation. Also, we perform FCA loads/store :-( (that's unrelated to this PR though). |
fbd57ee
to
8434bd7
Compare
r=me with this test failure fixed |
8434bd7
to
d14c5cd
Compare
@bors r=eddyb |
📌 Commit d14c5cd has been approved by |
☔ The latest upstream changes (presumably #33622) made this pull request unmergeable. Please resolve the merge conflicts. |
FWIW I've included this in #33905, with some extra logic to also work for the generalized |
[MIR] Implement overflow checking The initial set of changes is from @Aatch's #33255 PR, rebased on master, plus: Added an `Assert` terminator to MIR, to simplify working with overflow and bounds checks. With this terminator, error cases can be accounted for directly, instead of looking for lang item calls. It also keeps the MIR slimmer, with no extra explicit blocks for the actual panic calls. Warnings can be produced when the `Assert` is known to always panic at runtime, e.g.: ```rust warning: index out of bounds: the len is 1 but the index is 3 --> <anon>:1:14 1 |> fn main() { &[std::io::stdout()][3]; } |> ^^^^^^^^^^^^^^^^^^^^^^ ``` Generalized the `OperandValue::FatPtr` optimization to any aggregate pair of immediates. This allows us to generate the same IR for overflow checks as old trans, not something worse. For example, addition on `i16` calls `llvm.sadd.with.overflow.i16`, which returns `{i16, i1}`. However, the Rust type `(i16, bool)`, has to be `{i16, i8}`, only an immediate `bool` is `i1`. But if we split the pair into an `i16` and an `i1`, we can pass them around as such for free. The latest addition is a rebase of #34054, updated to work for pairs too. Closes #34054, fixes #33873. Last but not least, the `#[rustc_inherit_overflow_checks]` attribute was introduced to control the overflow checking behavior of generic or `#[inline]` functions, when translated in another crate. It is **not** intended to be used by crates other than `libcore`, which is in the unusual position of being distributed as only an optimized build with no checks, even when used from debug mode. Before MIR-based translation, this worked out fine, as the decision for overflow was made at translation time, in the crate being compiled, but MIR stored in `rlib` has to contain the checks. To avoid always generating the checks and slowing everything down, a decision was made to use an attribute in the few spots of `libcore` that need it (see #33255 for previous discussion): * `core::ops::{Add, Sub, Mul, Neg, Shl, Shr}` implementations for integers, which have `#[inline]` methods and can be used in generic abstractions from other crates * `core::ops::{Add, Sub, Mul, Neg, Shl, Shr}Assign` same as above, for augmented assignment * `pow` and `abs` methods on integers, which intentionally piggy-back on built-in multiplication and negation, respectively, to get overflow checks * `core::iter::{Iterator, Chain, Peek}::count` and `core::iter::Enumerate::{next, nth}`, also documented as panicking on overflow, from addition, counting elements of an iterator in an `usize`
Fixes #33873
r? @eddyb