Skip to content

Commit d073071

Browse files
committed
Auto merge of rust-lang#121282 - saethlin:gep-null-means-no-provenance, r=<try>
Lower transmutes from int to pointer type as gep on null I thought of this while looking at rust-lang#121242 The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
2 parents 6122397 + 903dd09 commit d073071

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
305305
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
306306
(Int(..) | F32 | F64, Int(..) | F32 | F64) => bx.bitcast(imm, to_backend_ty),
307307
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
308-
(Int(..), Pointer(..)) => bx.inttoptr(imm, to_backend_ty),
308+
(Int(..), Pointer(..)) => bx.gep(bx.type_i8(), bx.const_null(bx.type_ptr()), &[imm]),
309309
(Pointer(..), Int(..)) => bx.ptrtoint(imm, to_backend_ty),
310310
(F32 | F64, Pointer(..)) => {
311311
let int_imm = bx.bitcast(imm, bx.cx().type_isize());
312-
bx.inttoptr(int_imm, to_backend_ty)
312+
bx.gep(bx.type_i8(), bx.const_null(bx.type_ptr()), &[int_imm])
313313
}
314314
(Pointer(..), F32 | F64) => {
315315
let int_imm = bx.ptrtoint(imm, bx.cx().type_isize());

tests/codegen/intrinsics/transmute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ pub unsafe fn check_pair_with_bool(x: (u8, bool)) -> (bool, i8) {
296296
pub unsafe fn check_float_to_pointer(x: f64) -> *const () {
297297
// CHECK-NOT: alloca
298298
// CHECK: %0 = bitcast double %x to i64
299-
// CHECK: %_0 = inttoptr i64 %0 to ptr
299+
// CHECK: %_0 = getelementptr i8, ptr null, i64 %0
300300
// CHECK: ret ptr %_0
301301
transmute(x)
302302
}
@@ -371,7 +371,7 @@ pub unsafe fn check_issue_110005(x: (usize, bool)) -> Option<Box<[u8]>> {
371371
// CHECK-LABEL: @check_pair_to_dst_ref(
372372
#[no_mangle]
373373
pub unsafe fn check_pair_to_dst_ref<'a>(x: (usize, usize)) -> &'a [u8] {
374-
// CHECK: %_0.0 = inttoptr i64 %x.0 to ptr
374+
// CHECK: %_0.0 = getelementptr i8, ptr null, i64 %x.0
375375
// CHECK: %0 = insertvalue { ptr, i64 } poison, ptr %_0.0, 0
376376
// CHECK: %1 = insertvalue { ptr, i64 } %0, i64 %x.1, 1
377377
// CHECK: ret { ptr, i64 } %1

tests/codegen/transmute-scalar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn ptr_to_int(p: *mut u16) -> usize {
4949
}
5050

5151
// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] %i)
52-
// CHECK: %_0 = inttoptr [[USIZE]] %i to ptr
52+
// CHECK: %_0 = getelementptr i8, ptr null, i64 %i
5353
// CHECK-NEXT: ret ptr %_0
5454
#[no_mangle]
5555
pub fn int_to_ptr(i: usize) -> *mut u16 {

tests/ui/abi/foreign/foreign-call-no-runtime.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ pub fn main() {
4040

4141
extern "C" fn callback_isize(data: libc::uintptr_t) {
4242
unsafe {
43-
let data: *const isize = mem::transmute(data);
43+
let data = data as *const isize;
4444
assert_eq!(*data, 100);
4545
}
4646
}
4747

4848
extern "C" fn callback_i64(data: libc::uintptr_t) {
4949
unsafe {
50-
let data: *const i64 = mem::transmute(data);
50+
let data = data as *const i64;
5151
assert_eq!(*data, 100);
5252
}
5353
}
5454

5555
extern "C" fn callback_i32(data: libc::uintptr_t) {
5656
unsafe {
57-
let data: *const i32 = mem::transmute(data);
57+
let data = data as *const i32;
5858
assert_eq!(*data, 100);
5959
}
6060
}

0 commit comments

Comments
 (0)