Skip to content

Commit 43d0a3a

Browse files
committed
Fix a bug in the pointer address space assignment code
The code was not checking the "function-ness" of the type at the correct level. It was checking the "root type", which may be, say a struct, but would use that exclusively for classifying the address spaces of the fields within the struct. A function pointer within a struct would be incorrectly defined with address space 0 only because its parent struct is in address space 0. This patch also moves the address space classification logic for constants down a layer, which allows removal of the address space parameter from a few functions, and fixes the problem at the source rather than patching over top of it in a method higher up in the call stack.
1 parent 7fcd433 commit 43d0a3a

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/librustc_codegen_llvm/common.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
249249
Scalar::Ptr(ptr) => {
250250
let base_addr = match self.tcx.global_alloc(ptr.alloc_id) {
251251
GlobalAlloc::Memory(alloc) => {
252-
let init =
253-
const_alloc_to_llvm(self, alloc, self.address_space_of_type(llty));
252+
let init = const_alloc_to_llvm(self, alloc);
254253
let value = match alloc.mutability {
255254
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
256255
_ => self.static_addr_of(init, alloc.align, None),
@@ -300,7 +299,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
300299
let llval = self.const_usize(alloc.align.bytes());
301300
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
302301
} else {
303-
let init = const_alloc_to_llvm(self, alloc, address_space);
302+
let init = const_alloc_to_llvm(self, alloc);
304303
let base_addr = self.static_addr_of(init, alloc.align, None);
305304

306305
let llval = unsafe {

src/librustc_codegen_llvm/consts.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::def_id::DefId;
1313
use rustc_hir::Node;
1414
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1515
use rustc_middle::mir::interpret::{
16-
read_target_uint, Allocation, ConstValue, ErrorHandled, Pointer,
16+
read_target_uint, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer,
1717
};
1818
use rustc_middle::mir::mono::MonoItem;
1919
use rustc_middle::ty::{self, Instance, Ty};
@@ -24,11 +24,7 @@ use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive,
2424

2525
use std::ffi::CStr;
2626

27-
pub fn const_alloc_to_llvm(
28-
cx: &CodegenCx<'ll, '_>,
29-
alloc: &Allocation,
30-
address_space: AddressSpace,
31-
) -> &'ll Value {
27+
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
3228
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
3329
let dl = cx.data_layout();
3430
let pointer_size = dl.pointer_size.bytes() as usize;
@@ -57,6 +53,12 @@ pub fn const_alloc_to_llvm(
5753
)
5854
.expect("const_alloc_to_llvm: could not read relocation pointer")
5955
as u64;
56+
57+
let address_space = match cx.tcx.global_alloc(alloc_id) {
58+
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
59+
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) => AddressSpace::DATA,
60+
};
61+
6062
llvals.push(cx.scalar_to_backend(
6163
Pointer::new(alloc_id, Size::from_bytes(ptr_offset)).into(),
6264
&Scalar { value: Primitive::Pointer, valid_range: 0..=!0 },
@@ -83,17 +85,12 @@ pub fn codegen_static_initializer(
8385
cx: &CodegenCx<'ll, 'tcx>,
8486
def_id: DefId,
8587
) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
86-
let address_space = if cx.tcx.type_of(def_id).is_fn() {
87-
cx.data_layout().instruction_address_space
88-
} else {
89-
AddressSpace::DATA
90-
};
91-
9288
let alloc = match cx.tcx.const_eval_poly(def_id)? {
9389
ConstValue::ByRef { alloc, offset } if offset.bytes() == 0 => alloc,
9490
val => bug!("static const eval returned {:#?}", val),
9591
};
96-
Ok((const_alloc_to_llvm(cx, alloc, address_space), alloc))
92+
93+
Ok((const_alloc_to_llvm(cx, alloc), alloc))
9794
}
9895

9996
fn set_global_alignment(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: Align) {

0 commit comments

Comments
 (0)