Skip to content

Truly independent web browser

LibJS: Reject cached bytecode from another instruction set

sideshowbarkerc7566bdc3d7f
diff --git a/Libraries/LibJS/Rust/build.rs b/Libraries/LibJS/Rust/build.rs
index cb112f81f0c..b0a81e26962 100644
--- a/Libraries/LibJS/Rust/build.rs
+++ b/Libraries/LibJS/Rust/build.rs
@@ -110,6 +110,13 @@ fn heap_region_size(target: &Target, thread_sanitizer: bool) -> u64 {
}
}
+/// The 64-bit FNV-1a hash of `bytes`, which stays the same from build to build, unlike std's hashers.
+fn fnv1a_64(bytes: &[u8]) -> u64 {
+ bytes.iter().fold(0xcbf2_9ce4_8422_2325, |hash, &byte| {
+ (hash ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3)
+ })
+}
+
fn main() {
let manifest_directory = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let output_directory = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
@@ -127,6 +134,19 @@ fn main() {
instructions::generate(&interpreter_source, &output_directory).expect("generate the frontend's instruction types");
+ // NB: Bytecode cache blobs hold encoded instructions, so a blob is only good for the instruction set it was encoded
+ // with. The generated instruction types spell out every opcode, in the order that numbers them, and every
+ // operand, so their fingerprint changes with any change to the instructions in interpreter.flap.
+ let instruction_types =
+ fs::read(output_directory.join("instruction_generated.rs")).expect("read instruction_generated.rs");
+ write_if_changed(
+ &output_directory.join("instruction_set_fingerprint.rs"),
+ &format!(
+ "pub(crate) const INSTRUCTION_SET_FINGERPRINT: u64 = {:#018x};\n",
+ fnv1a_64(&instruction_types)
+ ),
+ );
+
let target = target();
let address_sanitizer = env::var_os("CARGO_FEATURE_ADDRESS_SANITIZER").is_some();
let thread_sanitizer = env::var_os("CARGO_FEATURE_THREAD_SANITIZER").is_some();
diff --git a/Libraries/LibJS/Rust/src/bytecode_cache.rs b/Libraries/LibJS/Rust/src/bytecode_cache.rs
index aaa257ccbd1..6542337dc98 100644
--- a/Libraries/LibJS/Rust/src/bytecode_cache.rs
+++ b/Libraries/LibJS/Rust/src/bytecode_cache.rs
@@ -50,7 +50,9 @@ use crate::compile::CompiledProgramBytecode;
use crate::u32_from_usize;
const MAGIC: &[u8; 8] = b"LBJSBC\0\0";
-const FORMAT_VERSION: u32 = 30;
+const FORMAT_VERSION: u32 = 31;
+// A blob's instructions are only good for the instruction set they were encoded with; see build.rs.
+include!(concat!(env!("OUT_DIR"), "/instruction_set_fingerprint.rs"));
/// The size of the source hash a blob is keyed by.
pub(crate) const SOURCE_HASH_SIZE: usize = 32;
const BYTECODE_ALIGNMENT: usize = 8;
@@ -626,6 +628,7 @@ impl Encode for CacheBlob<'_> {
fn encode(&self, encoder: &mut Encoder) {
encoder.bytes(MAGIC);
FORMAT_VERSION.encode(encoder);
+ INSTRUCTION_SET_FINGERPRINT.encode(encoder);
self.runtime.tag().encode(encoder);
self.program_type.encode(encoder);
encoder.bytes(self.source_hash);
@@ -650,6 +653,7 @@ impl CacheBlob<'_> {
) -> Option<DecodedCacheBlob> {
decoder.expect_bytes(MAGIC)?;
(u32::decode(decoder)? == FORMAT_VERSION).then_some(())?;
+ (u64::decode(decoder)? == INSTRUCTION_SET_FINGERPRINT).then_some(())?;
(u8::decode(decoder)? == expected_runtime.tag()).then_some(())?;
let program_type = ast::ProgramType::decode(decoder)?;
(program_type == expected_program_type).then_some(())?;
@@ -3298,6 +3302,7 @@ mod tests {
let mut bytes = Vec::new();
bytes.extend_from_slice(MAGIC);
bytes.extend_from_slice(&FORMAT_VERSION.to_le_bytes());
+ bytes.extend_from_slice(&INSTRUCTION_SET_FINGERPRINT.to_le_bytes());
bytes.push(BytecodeCacheRuntime::Rust.tag());
bytes.push(ast::ProgramType::Script as u8);
bytes.extend_from_slice(&stored_source_hash);
@@ -3307,6 +3312,23 @@ mod tests {
assert_eq!(releases.get(), 1);
}
+ #[test]
+ fn decode_rejects_another_instruction_set() {
+ let source_hash = [1u8; SOURCE_HASH_SIZE];
+
+ let mut bytes = Vec::new();
+ bytes.extend_from_slice(MAGIC);
+ bytes.extend_from_slice(&FORMAT_VERSION.to_le_bytes());
+ bytes.extend_from_slice(&(INSTRUCTION_SET_FINGERPRINT ^ 1).to_le_bytes());
+ bytes.push(BytecodeCacheRuntime::Rust.tag());
+ bytes.push(ast::ProgramType::Script as u8);
+ bytes.extend_from_slice(&source_hash);
+
+ let releases = Rc::default();
+ assert!(decode_test_blob(&bytes, ast::ProgramType::Script, &source_hash, &releases).is_none());
+ assert_eq!(releases.get(), 1);
+ }
+
#[test]
fn utf16_decode_borrows_from_foreign_blob() {
let mut bytes = Vec::new();