Create serializer module

This commit is contained in:
Brian Carroll 2021-10-28 10:59:35 +01:00
parent eb47b5183c
commit 82acf55d38
5 changed files with 298 additions and 231 deletions

View file

@ -0,0 +1,260 @@
use bumpalo::collections::vec::Vec;
pub trait SerialBuffer {
fn append_byte(&mut self, b: u8);
fn append_slice(&mut self, b: &[u8]);
fn size(&self) -> usize;
fn encode_u32(&mut self, value: u32) -> usize;
fn encode_u64(&mut self, value: u64) -> usize;
fn encode_i32(&mut self, value: i32) -> usize;
fn encode_i64(&mut self, value: i64) -> usize;
fn encode_f32(&mut self, value: f32);
fn encode_f64(&mut self, value: f64);
/// Write a LEB-128 encoded u32 value, padded to maximum length (5 bytes)
/// so that it can be overwritten later without moving other bytes.
/// The value 3 is encoded as 0x83 0x80 0x80 0x80 0x00.
/// https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md#relocation-sections
/// Return its starting index in the buffer.
fn encode_padded_u32(&mut self, value: u32) -> usize;
/// Overwrite a padded LEB-128 encoded value at the given index
fn overwrite_padded_u32(&mut self, index: usize, value: u32);
}
pub trait Serialize {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T);
}
/// Write an unsigned integer into the provided buffer in LEB-128 format, returning byte length
///
/// All integers in Wasm are variable-length encoded, which saves space for small values.
/// The most significant bit indicates "more bytes are coming", and the other 7 are payload.
macro_rules! encode_uleb128 {
($name: ident, $ty: ty) => {
fn $name(&mut self, value: $ty) -> usize {
let mut x = value;
let start_len = self.len();
while x >= 0x80 {
self.push(0x80 | ((x & 0x7f) as u8));
x >>= 7;
}
self.push(x as u8);
self.len() - start_len
}
};
}
/// Write a signed integer into the provided buffer in LEB-128 format, returning byte length
macro_rules! encode_sleb128 {
($name: ident, $ty: ty) => {
fn $name(&mut self, value: $ty) -> usize {
let mut x = value;
let start_len = self.len();
loop {
let byte = (x & 0x7f) as u8;
x >>= 7;
let byte_is_negative = (byte & 0x40) != 0;
if ((x == 0 && !byte_is_negative) || (x == -1 && byte_is_negative)) {
self.push(byte);
break;
}
self.push(byte | 0x80);
}
self.len() - start_len
}
};
}
/// Write a float with no LEB encoding, in little-endian format regardless of compiler host.
macro_rules! encode_float {
($name: ident, $ty: ty) => {
fn $name(&mut self, value: $ty) {
let mut x = value.to_bits();
let size = std::mem::size_of::<$ty>();
for _ in 0..size {
self.push((x & 0xff) as u8);
x >>= 8;
}
}
};
}
fn overwrite_padded_u32_help(buffer: &mut [u8], value: u32) {
let mut x = value;
for byte in buffer.iter_mut().take(4) {
*byte = 0x80 | ((x & 0x7f) as u8);
x >>= 7;
}
buffer[4] = x as u8;
}
impl SerialBuffer for std::vec::Vec<u8> {
fn append_byte(&mut self, b: u8) {
self.push(b);
}
fn append_slice(&mut self, b: &[u8]) {
self.extend_from_slice(b);
}
fn size(&self) -> usize {
self.len()
}
encode_uleb128!(encode_u32, u32);
encode_uleb128!(encode_u64, u64);
encode_sleb128!(encode_i32, i32);
encode_sleb128!(encode_i64, i64);
encode_float!(encode_f32, f32);
encode_float!(encode_f64, f64);
fn encode_padded_u32(&mut self, value: u32) -> usize {
let index = self.len();
let new_len = index + 5;
self.resize(new_len, 0);
overwrite_padded_u32_help(&mut self[index..new_len], value);
index
}
fn overwrite_padded_u32(&mut self, index: usize, value: u32) {
overwrite_padded_u32_help(&mut self[index..(index + 5)], value);
}
}
impl<'a> SerialBuffer for Vec<'a, u8> {
fn append_byte(&mut self, b: u8) {
self.push(b);
}
fn append_slice(&mut self, b: &[u8]) {
self.extend_from_slice(b);
}
fn size(&self) -> usize {
self.len()
}
encode_uleb128!(encode_u32, u32);
encode_uleb128!(encode_u64, u64);
encode_sleb128!(encode_i32, i32);
encode_sleb128!(encode_i64, i64);
encode_float!(encode_f32, f32);
encode_float!(encode_f64, f64);
fn encode_padded_u32(&mut self, value: u32) -> usize {
let index = self.len();
let new_len = index + 5;
self.resize(new_len, 0);
overwrite_padded_u32_help(&mut self[index..new_len], value);
index
}
fn overwrite_padded_u32(&mut self, index: usize, value: u32) {
overwrite_padded_u32_help(&mut self[index..(index + 5)], value);
}
}
#[cfg(test)]
mod tests {
use super::*;
use bumpalo::{self, collections::Vec, Bump};
fn help_u32<'a>(arena: &'a Bump, value: u32) -> Vec<'a, u8> {
let mut buffer = Vec::with_capacity_in(5, arena);
buffer.encode_u32(value);
buffer
}
#[test]
fn test_encode_u32() {
let a = &Bump::new();
assert_eq!(help_u32(a, 0), &[0]);
assert_eq!(help_u32(a, 64), &[64]);
assert_eq!(help_u32(a, 0x7f), &[0x7f]);
assert_eq!(help_u32(a, 0x80), &[0x80, 0x01]);
assert_eq!(help_u32(a, 0x3fff), &[0xff, 0x7f]);
assert_eq!(help_u32(a, 0x4000), &[0x80, 0x80, 0x01]);
assert_eq!(help_u32(a, u32::MAX), &[0xff, 0xff, 0xff, 0xff, 0x0f]);
}
fn help_u64<'a>(arena: &'a Bump, value: u64) -> Vec<'a, u8> {
let mut buffer = Vec::with_capacity_in(10, arena);
buffer.encode_u64(value);
buffer
}
#[test]
fn test_encode_u64() {
let a = &Bump::new();
assert_eq!(help_u64(a, 0), &[0]);
assert_eq!(help_u64(a, 64), &[64]);
assert_eq!(help_u64(a, 0x7f), &[0x7f]);
assert_eq!(help_u64(a, 0x80), &[0x80, 0x01]);
assert_eq!(help_u64(a, 0x3fff), &[0xff, 0x7f]);
assert_eq!(help_u64(a, 0x4000), &[0x80, 0x80, 0x01]);
assert_eq!(
help_u64(a, u64::MAX),
&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01],
);
}
fn help_i32<'a>(arena: &'a Bump, value: i32) -> Vec<'a, u8> {
let mut buffer = Vec::with_capacity_in(5, arena);
buffer.encode_i32(value);
buffer
}
#[test]
fn test_encode_i32() {
let a = &Bump::new();
assert_eq!(help_i32(a, 0), &[0]);
assert_eq!(help_i32(a, 1), &[1]);
assert_eq!(help_i32(a, -1), &[0x7f]);
assert_eq!(help_i32(a, 63), &[63]);
assert_eq!(help_i32(a, 64), &[0xc0, 0x0]);
assert_eq!(help_i32(a, -64), &[0x40]);
assert_eq!(help_i32(a, -65), &[0xbf, 0x7f]);
assert_eq!(help_i32(a, i32::MAX), &[0xff, 0xff, 0xff, 0xff, 0x07]);
assert_eq!(help_i32(a, i32::MIN), &[0x80, 0x80, 0x80, 0x80, 0x78]);
}
fn help_i64<'a>(arena: &'a Bump, value: i64) -> Vec<'a, u8> {
let mut buffer = Vec::with_capacity_in(10, arena);
buffer.encode_i64(value);
buffer
}
#[test]
fn test_encode_i64() {
let a = &Bump::new();
assert_eq!(help_i64(a, 0), &[0]);
assert_eq!(help_i64(a, 1), &[1]);
assert_eq!(help_i64(a, -1), &[0x7f]);
assert_eq!(help_i64(a, 63), &[63]);
assert_eq!(help_i64(a, 64), &[0xc0, 0x0]);
assert_eq!(help_i64(a, -64), &[0x40]);
assert_eq!(help_i64(a, -65), &[0xbf, 0x7f]);
assert_eq!(
help_i64(a, i64::MAX),
&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00],
);
assert_eq!(
help_i64(a, i64::MIN),
&[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f],
);
}
#[test]
fn test_overwrite_u32_padded() {
let mut buffer = [0, 0, 0, 0, 0];
overwrite_padded_u32_help(&mut buffer, u32::MAX);
assert_eq!(buffer, [0xff, 0xff, 0xff, 0xff, 0x0f]);
overwrite_padded_u32_help(&mut buffer, 0);
assert_eq!(buffer, [0x80, 0x80, 0x80, 0x80, 0x00]);
overwrite_padded_u32_help(&mut buffer, 127);
assert_eq!(buffer, [0xff, 0x80, 0x80, 0x80, 0x00]);
overwrite_padded_u32_help(&mut buffer, 128);
assert_eq!(buffer, [0x80, 0x81, 0x80, 0x80, 0x00]);
}
}