Skip to content

Commit c05fbc5

Browse files
committed
auto merge of #9593 : fhahn/rust/logging-unsafe-removal, r=alexcrichton
This pull request changes to memory layout of the `CrateMap` struct to use static slices instead of raw pointers. Most of the discussion took place [here](fhahn@63b5975#L1R92) . The memory layout of CrateMap changed, without bumping the version number in the struct. Another, more backward compatible, solution would be to keep the old code and increase the version number in the new struct. On the other hand, the `annihilate_fn` pointer was removed without bumping the version number recently. At the moment, the stage0 compiler does not use the new memory layout, which would lead the segfaults during stage0 compilation, so I've added a dummy `iter_crate_map` function for stage0, which does nothing. Again, this could be avoided if we'd bump the version number in the struct and keep the old code. I'd like to use a normal `for` loop [here](https://github.com/fhahn/rust/compare/logging-unsafe-removal?expand=1#L1R109), for child in children.iter() { do_iter_crate_map(child, |x| f(x), visited); } but for some reason this only yields `error: unresolved enum variant, struct or const 'Some'` and I have no idea why.
2 parents a623fd3 + 23176fc commit c05fbc5

File tree

3 files changed

+343
-224
lines changed

3 files changed

+343
-224
lines changed

src/librustc/middle/trans/base.rs

+38-22
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use middle::trans::glue;
5454
use middle::trans::inline;
5555
use middle::trans::llrepr::LlvmRepr;
5656
use middle::trans::machine;
57-
use middle::trans::machine::{llalign_of_min, llsize_of};
57+
use middle::trans::machine::{llalign_of_min, llsize_of, llsize_of_alloc};
5858
use middle::trans::meth;
5959
use middle::trans::monomorphize;
6060
use middle::trans::tvec;
@@ -2911,9 +2911,10 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
29112911
}
29122912
}
29132913

2914-
pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef {
2915-
let elttype = Type::struct_([ccx.int_type, ccx.int_type], false);
2916-
let maptype = Type::array(&elttype, (ccx.module_data.len() + 1) as u64);
2914+
pub fn create_module_map(ccx: &mut CrateContext) -> (ValueRef, uint, uint) {
2915+
let str_slice_type = Type::struct_([Type::i8p(), ccx.int_type], false);
2916+
let elttype = Type::struct_([str_slice_type, ccx.int_type], false);
2917+
let maptype = Type::array(&elttype, ccx.module_data.len() as u64);
29172918
let map = do "_rust_mod_map".with_c_str |buf| {
29182919
unsafe {
29192920
llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf)
@@ -2931,19 +2932,18 @@ pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef {
29312932
}
29322933

29332934
for key in keys.iter() {
2934-
let val = *ccx.module_data.find_equiv(key).unwrap();
2935-
let s_const = C_cstr(ccx, *key);
2936-
let s_ptr = p2i(ccx, s_const);
2937-
let v_ptr = p2i(ccx, val);
2938-
let elt = C_struct([s_ptr, v_ptr]);
2939-
elts.push(elt);
2940-
}
2941-
let term = C_struct([C_int(ccx, 0), C_int(ccx, 0)]);
2942-
elts.push(term);
2935+
let val = *ccx.module_data.find_equiv(key).unwrap();
2936+
let v_ptr = p2i(ccx, val);
2937+
let elt = C_struct([
2938+
C_estr_slice(ccx, *key),
2939+
v_ptr
2940+
]);
2941+
elts.push(elt);
2942+
}
29432943
unsafe {
29442944
llvm::LLVMSetInitializer(map, C_array(elttype, elts));
29452945
}
2946-
return map;
2946+
return (map, keys.len(), llsize_of_alloc(ccx, elttype));
29472947
}
29482948

29492949

@@ -2959,9 +2959,10 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
29592959
} else {
29602960
~"toplevel"
29612961
};
2962+
29622963
let sym_name = ~"_rust_crate_map_" + mapname;
2963-
let arrtype = Type::array(&int_type, n_subcrates as u64);
2964-
let maptype = Type::struct_([Type::i32(), int_type, arrtype], false);
2964+
let slicetype = Type::struct_([int_type, int_type], false);
2965+
let maptype = Type::struct_([Type::i32(), slicetype, slicetype], false);
29652966
let map = do sym_name.with_c_str |buf| {
29662967
unsafe {
29672968
llvm::LLVMAddGlobal(llmod, maptype.to_ref(), buf)
@@ -2996,14 +2997,29 @@ pub fn fill_crate_map(ccx: &mut CrateContext, map: ValueRef) {
29962997
subcrates.push(p2i(ccx, cr));
29972998
i += 1;
29982999
}
2999-
subcrates.push(C_int(ccx, 0));
3000-
30013000
unsafe {
3002-
let mod_map = create_module_map(ccx);
3001+
let maptype = Type::array(&ccx.int_type, subcrates.len() as u64);
3002+
let vec_elements = do "_crate_map_child_vectors".with_c_str |buf| {
3003+
llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf)
3004+
};
3005+
lib::llvm::SetLinkage(vec_elements, lib::llvm::InternalLinkage);
3006+
3007+
llvm::LLVMSetInitializer(vec_elements, C_array(ccx.int_type, subcrates));
3008+
let (mod_map, mod_count, mod_struct_size) = create_module_map(ccx);
3009+
30033010
llvm::LLVMSetInitializer(map, C_struct(
3004-
[C_i32(1),
3005-
p2i(ccx, mod_map),
3006-
C_array(ccx.int_type, subcrates)]));
3011+
[C_i32(2),
3012+
C_struct([
3013+
p2i(ccx, mod_map),
3014+
// byte size of the module map array, an entry consists of two integers
3015+
C_int(ccx, ((mod_count * mod_struct_size) as int))
3016+
]),
3017+
C_struct([
3018+
p2i(ccx, vec_elements),
3019+
// byte size of the subcrates array, an entry consists of an integer
3020+
C_int(ccx, (subcrates.len() * llsize_of_alloc(ccx, ccx.int_type)) as int)
3021+
])
3022+
]));
30073023
}
30083024
}
30093025

0 commit comments

Comments
 (0)