Mark crate metadata with a version tag. Close #3390.

This commit is contained in:
Graydon Hoare 2012-09-05 15:27:22 -07:00
parent 31af60b257
commit 81695d85dc
4 changed files with 50 additions and 6 deletions

View File

@ -27,6 +27,7 @@ export encode_metadata;
export encoded_ty; export encoded_ty;
export reachable; export reachable;
export encode_inlined_item; export encode_inlined_item;
export metadata_encoding_version;
// used by astencode: // used by astencode:
export def_to_str; export def_to_str;
@ -1084,6 +1085,13 @@ fn encode_hash(ebml_w: ebml::Writer, hash: ~str) {
ebml_w.end_tag(); ebml_w.end_tag();
} }
// NB: Increment this as you change the metadata encoding version.
const metadata_encoding_version : &[u8] = &[0x72, //'r' as u8,
0x75, //'u' as u8,
0x73, //'s' as u8,
0x74, //'t' as u8,
0, 0, 0, 1 ];
fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] { fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
let buf = io::mem_buffer(); let buf = io::mem_buffer();
let stats = let stats =
@ -1163,7 +1171,18 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
// Pad this, since something (LLVM, presumably) is cutting off the // Pad this, since something (LLVM, presumably) is cutting off the
// remaining % 4 bytes. // remaining % 4 bytes.
buf_w.write(&[0u8, 0u8, 0u8, 0u8]); buf_w.write(&[0u8, 0u8, 0u8, 0u8]);
flate::deflate_buf(io::mem_buffer_buf(buf))
// FIXME #3396: weird bug here, for reasons unclear this emits random
// looking bytes (mostly 0x1) if we use the version byte-array constant
// above; so we use a string constant inline instead.
//
// Should be:
//
// vec::from_slice(metadata_encoding_version) +
(do str::as_bytes(~"rust\x00\x00\x00\x01") |bytes| {
vec::slice(bytes, 0, 8)
}) + flate::deflate_buf(io::mem_buffer_buf(buf))
} }
// Get the encoded string for a type // Get the encoded string for a type

View File

@ -185,11 +185,30 @@ fn get_metadata_section(os: os,
if name == meta_section_name(os) { if name == meta_section_name(os) {
let cbuf = llvm::LLVMGetSectionContents(si.llsi); let cbuf = llvm::LLVMGetSectionContents(si.llsi);
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint; let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
let mut found = None;
unsafe { unsafe {
let cvbuf: *u8 = unsafe::reinterpret_cast(&cbuf); let cvbuf: *u8 = unsafe::reinterpret_cast(&cbuf);
let v = vec::unsafe::from_buf(cvbuf, csz); let vlen = vec::len(encoder::metadata_encoding_version);
let inflated = flate::inflate_buf(v); debug!("checking %u bytes of metadata-version stamp",
return Some(@inflated); vlen);
let minsz = uint::min(vlen, csz);
let mut version_ok = false;
do vec::unsafe::form_slice(cvbuf, minsz) |buf0| {
version_ok = (buf0 ==
encoder::metadata_encoding_version);
}
if !version_ok { return None; }
let cvbuf1 = ptr::offset(cvbuf, vlen);
debug!("inflating %u bytes of compressed metadata",
csz - vlen);
do vec::unsafe::form_slice(cvbuf1, csz-vlen) |buf| {
let inflated = flate::inflate_buf(buf);
found = move Some(@(move inflated));
}
if found != None {
return found;
}
} }
} }
llvm::LLVMMoveToNextSection(si.llsi); llvm::LLVMMoveToNextSection(si.llsi);

View File

@ -970,13 +970,19 @@ fn C_array(ty: TypeRef, elts: ~[ValueRef]) -> ValueRef unsafe {
} }
fn C_bytes(bytes: ~[u8]) -> ValueRef unsafe { fn C_bytes(bytes: ~[u8]) -> ValueRef unsafe {
return llvm::LLVMConstString(
unsafe::reinterpret_cast(&vec::unsafe::to_ptr(bytes)),
bytes.len() as c_uint, True);
}
fn C_bytes_plus_null(bytes: ~[u8]) -> ValueRef unsafe {
return llvm::LLVMConstString( return llvm::LLVMConstString(
unsafe::reinterpret_cast(&vec::unsafe::to_ptr(bytes)), unsafe::reinterpret_cast(&vec::unsafe::to_ptr(bytes)),
bytes.len() as c_uint, False); bytes.len() as c_uint, False);
} }
fn C_shape(ccx: @crate_ctxt, bytes: ~[u8]) -> ValueRef { fn C_shape(ccx: @crate_ctxt, bytes: ~[u8]) -> ValueRef {
let llshape = C_bytes(bytes); let llshape = C_bytes_plus_null(bytes);
let llglobal = str::as_c_str(fmt!("shape%u", ccx.names(~"shape")), |buf| { let llglobal = str::as_c_str(fmt!("shape%u", ccx.names(~"shape")), |buf| {
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llshape), buf) llvm::LLVMAddGlobal(ccx.llmod, val_ty(llshape), buf)
}); });

View File

@ -1,3 +1,3 @@
// error-pattern: unresolved import // error-pattern: unresolved name
fn main() { let foo = thing::len(~[]); } fn main() { let foo = thing::len(~[]); }