Address review comments and add a test
This commit is contained in:
parent
e0d4226677
commit
b81d873cdf
4 changed files with 38 additions and 7 deletions
|
@ -29,12 +29,12 @@ use rustc_hir::LangItem;
|
|||
use rustc_middle::dep_graph::WorkProduct;
|
||||
use rustc_middle::middle::dependency_format::Dependencies;
|
||||
use rustc_middle::ty::query::{ExternProviders, Providers};
|
||||
use rustc_serialize::{opaque, Decodable, Decoder, Encoder};
|
||||
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
|
||||
use rustc_session::cstore::{self, CrateSource};
|
||||
use rustc_session::utils::NativeLibKind;
|
||||
use rustc_span::symbol::Symbol;
|
||||
use std::path::{Path, PathBuf};
|
||||
use rustc_serialize::{Decodable, Decoder, Encoder, opaque};
|
||||
|
||||
pub mod back;
|
||||
pub mod base;
|
||||
|
@ -192,13 +192,18 @@ pub fn looks_like_rust_object_file(filename: &str) -> bool {
|
|||
ext2 == Some(RUST_CGU_EXT)
|
||||
}
|
||||
|
||||
const RLINK_MAGIC: &'static [u8; 5] = b"rlink";
|
||||
const RLINK_VERSION: u32 = 1;
|
||||
const RLINK_MAGIC: &[u8] = b"rustlink";
|
||||
|
||||
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
|
||||
|
||||
impl CodegenResults {
|
||||
pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec<u8> {
|
||||
let mut encoder = opaque::Encoder::new(vec![]);
|
||||
encoder.emit_raw_bytes(RLINK_MAGIC).unwrap();
|
||||
// `emit_raw_bytes` is used to make sure that the version representation does not depend on
|
||||
// Encoder's inner representation of `u32`.
|
||||
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()).unwrap();
|
||||
encoder.emit_str(RUSTC_VERSION.unwrap()).unwrap();
|
||||
|
||||
let mut encoder = rustc_serialize::opaque::Encoder::new(encoder.into_inner());
|
||||
|
@ -207,14 +212,29 @@ impl CodegenResults {
|
|||
}
|
||||
|
||||
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
|
||||
// The Decodable machinery is not used here because it panics if the input data is invalid
|
||||
// and because its internal representation may change.
|
||||
if !data.starts_with(RLINK_MAGIC) {
|
||||
return Err("The input does not look like a .rlink file".to_string());
|
||||
}
|
||||
let mut decoder = opaque::Decoder::new(&data[RLINK_MAGIC.len()..], 0);
|
||||
let data = &data[RLINK_MAGIC.len()..];
|
||||
if data.len() < 4 {
|
||||
return Err("The input does not contain version number".to_string());
|
||||
}
|
||||
|
||||
let mut version_array: [u8; 4] = Default::default();
|
||||
version_array.copy_from_slice(&data[..4]);
|
||||
if u32::from_be_bytes(version_array) != RLINK_VERSION {
|
||||
return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
|
||||
}
|
||||
|
||||
let mut decoder = opaque::Decoder::new(&data[4..], 0);
|
||||
let rustc_version = decoder.read_str();
|
||||
let current_version = RUSTC_VERSION.unwrap();
|
||||
if rustc_version != current_version {
|
||||
return Err(format!(".rlink file was produced by rustc version {rustc_version}, but the current version is {current_version}."));
|
||||
return Err(format!(
|
||||
".rlink file was produced by rustc version {rustc_version}, but the current version is {current_version}."
|
||||
));
|
||||
}
|
||||
|
||||
let codegen_results = CodegenResults::decode(&mut decoder);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue