Skip to content

Commit

Permalink
incr.comp.: Add test case for cache artifact file headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Sep 26, 2016
1 parent 76f76ae commit 1e5c253
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/librustc_incremental/persist/file_format.rs
Expand Up @@ -22,6 +22,9 @@
use std::io::{self, Read};
use std::path::Path;
use std::fs::File;
use std::env;

use rustc::session::config::nightly_options;

/// The first few bytes of files generated by incremental compilation
const FILE_MAGIC: &'static [u8] = b"RSIC";
Expand All @@ -38,9 +41,11 @@ pub fn write_file_header<W: io::Write>(stream: &mut W) -> io::Result<()> {
stream.write_all(FILE_MAGIC)?;
stream.write_all(&[(HEADER_FORMAT_VERSION >> 0) as u8,
(HEADER_FORMAT_VERSION >> 8) as u8])?;
assert_eq!(RUSTC_VERSION.len(), (RUSTC_VERSION.len() as u8) as usize);
stream.write_all(&[RUSTC_VERSION.len() as u8])?;
stream.write_all(RUSTC_VERSION.as_bytes())?;

let rustc_version = rustc_version();
assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize);
stream.write_all(&[rustc_version.len() as u8])?;
stream.write_all(rustc_version.as_bytes())?;

Ok(())
}
Expand Down Expand Up @@ -103,3 +108,13 @@ pub fn read_file(path: &Path) -> io::Result<Option<Vec<u8>>> {

Ok(Some(data))
}

fn rustc_version() -> String {
if nightly_options::is_nightly_build() {
if let Some(val) = env::var_os("RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER") {
return val.to_string_lossy().into_owned()
}
}

RUSTC_VERSION.to_string()
}
29 changes: 29 additions & 0 deletions src/test/incremental/cache_file_headers.rs
@@ -0,0 +1,29 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// This test case makes sure that the compiler does not try to re-use anything
// from the incremental compilation cache if the cache was produced by a
// different compiler version. This is tested by artificially forcing the
// emission of a different compiler version in the header of rpass1 artifacts,
// and then making sure that the only object file of the test program gets
// re-translated although the program stays unchanged.

// The `l33t haxx0r` Rust compiler is known to produce incr. comp. artifacts
// that are outrageously incompatible with just about anything, even itself:
//[rpass1] rustc-env:RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER="l33t haxx0r rustc 2.1 LTS"

// revisions:rpass1 rpass2

#![feature(rustc_attrs)]
#![rustc_partition_translated(module="cache_file_headers", cfg="rpass2")]

fn main() {
// empty
}

0 comments on commit 1e5c253

Please sign in to comment.