Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Jul 12, 2024
1 parent 9cba77b commit 916287c
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 14 deletions.
116 changes: 112 additions & 4 deletions scripts/s2n2rust/s2n2rust/src/refactor/text_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Owner {
module: String,
}

fn index_structs(_sh: &Shell, c_src: &Path, overrides: &Overrides) -> Result<StructIndex> {
fn index_structs(_sh: &Shell, c_src: &Path, _overrides: &Overrides) -> Result<StructIndex> {
std::env::set_current_dir(c_src)?;

let mut modules = HashMap::new();
Expand Down Expand Up @@ -298,6 +298,9 @@ processors!(
prelude: Prelude,
extern_block: ExternBlock,
libcrypto: LibcryptoPath,
literal_casts: LiteralCasts,
encryption_limit: CiphersuiteEncryptionLimit,
remove_casts: RemoveCasts,
}
);

Expand All @@ -314,7 +317,18 @@ impl ConstFilter {
let Some(candidate) = trimmed.strip_prefix("pub const ") else {
return ControlFlow::Continue(());
};
for prefix in ["S2N_ERR_", "_SC_", "S2N_SUCCESS", "S2N_FAILURE"] {
for prefix in [
"S2N_ERR_",
"_SC_",
"S2N_SUCCESS",
"S2N_FAILURE",
"S2N_SSLv2",
"S2N_SSLv3",
"S2N_TLS10",
"S2N_TLS11",
"S2N_TLS12",
"S2N_TLS13",
] {
if candidate.starts_with(prefix) {
return ControlFlow::Break(());
}
Expand Down Expand Up @@ -513,7 +527,7 @@ impl OwningStruct {
}

macro_rules! rewrite {
($name:ident, [$($pat:expr),* $(,)?], $out:expr) => {
($name:ident, [$($pat:expr),* $(,)?], $out:expr $(,)?) => {
struct $name {}

impl $name {
Expand Down Expand Up @@ -819,6 +833,15 @@ replace!(
(
"low_level: s2n_hash_low_level_digest {",
"low_level: crate::crypto::s2n_hash::s2n_hash_low_level_digest {"
),
(
"io: C2RustUnnamed_1 {",
// TODO this should be in a different place
"io: crate::crypto::s2n_aead_cipher_aes_gcm::C2RustUnnamed_2 {",
)(
"io: C2RustUnnamed_2 {",
// TODO this should be in a different place
"io: crate::crypto::s2n_aead_cipher_aes_gcm::C2RustUnnamed_2 {",
)
]
);
Expand Down Expand Up @@ -872,7 +895,89 @@ replace!(
]
);

replace!(AsBool, [("as bool", "!= 0"),]);
replace!(AsBool, [("as bool", ".as_bool()"),]);

replace!(
RemoveCasts,
[
("foobarbaz", ""),
("as libc::c_ulonglong", ""),
//("as libc::c_ulong", ""),
("as libc::c_longlong", ""),
("as libc::c_long", ""),
//("as libc::c_int", ""),
]
);

rewrite!(
CiphersuiteEncryptionLimit,
["encryption_limit: u64::MAX as libc::c_ulong,"],
"encryption_limit: u64::MAX",
);

struct LiteralCasts {
indices: Vec<usize>,
}

impl LiteralCasts {
fn new(_path: &Path, _config: &Arc<Config>) -> Self {
Self { indices: vec![] }
}

fn on_line(&mut self, line: &mut String) -> ControlFlow<()> {
let nums = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..];
let casts = &[
" as libc::c_int",
" as libc::c_ulonglong",
" as libc::c_ulong",
" as libc::longlong",
" as libc::long",
/*
" as i8",
" as u8",
" as i16",
" as u16",
" as i32",
" as u32",
" as i64",
" as u64",
*/
][..];
let indices = line.rmatch_indices(nums).map(|(idx, _)| idx);
self.indices.clear();
self.indices.extend(indices);
for mut index in self.indices.iter().copied() {
let match_line = &line[index..];
let mut m = match_line.trim_start_matches(nums);
index += match_line.len() - m.len();
let initial_len = m.len();

loop {
let mut found_match = false;
for cast in casts {
if let Some(s) = m.strip_prefix(cast) {
m = s;
found_match = true;
}
}

if !found_match {
break;
}
}

let len = initial_len - m.len();

if len == 0 {
continue;
}

let range = index..index + len;
line.drain(range);
}
ControlFlow::Continue(())
}
}

enum LibRs {
ProcessingHeader,
Expand Down Expand Up @@ -907,6 +1012,9 @@ impl LibRs {
line.insert_str(
0,
r#"
mod api;
pub use api::*;
#[macro_use]
pub mod error {
pub mod s2n_errno;
Expand Down
3 changes: 3 additions & 0 deletions scripts/s2n2rust/s2n2rust/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ impl<'a> Overrides<'a> {
}
}

#[allow(dead_code)]
mod api;
#[allow(dead_code)]
mod libc;

Expand All @@ -27,6 +29,7 @@ pub fn run(sh: &Shell) -> Result<HashSet<PathBuf>> {
};

o.write("libc.rs", include_str!("./replace/libc.rs"))?;
o.write("api.rs", include_str!("./replace/api.rs"))?;

crate::crypto::run(&mut o)?;
crate::error::run(&mut o)?;
Expand Down
54 changes: 54 additions & 0 deletions scripts/s2n2rust/s2n2rust/src/replace/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use core::cmp::Ordering;
use libc::c_int;

macro_rules! impl_cmp {
($ty:ident, $other:ident) => {
impl PartialEq<$other> for $ty {
#[inline]
fn eq(&self, other: &$other) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}

impl PartialOrd<$other> for $ty {
#[inline]
fn partial_cmp(&self, other: &$other) -> Option<Ordering> {
Some((*self as i32).cmp(&(*other as i32)))
}
}

impl PartialEq<$ty> for $other {
#[inline]
fn eq(&self, other: &$ty) -> bool {
other.eq(self)
}
}

impl PartialOrd<$ty> for $other {
#[inline]
fn partial_cmp(&self, other: &$ty) -> Option<Ordering> {
other.partial_cmp(self)
}
}
};
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub enum TlsVersion {
SSLv2 = 20,
SSLv3 = 30,
TLS10 = 31,
TLS11 = 32,
TLS12 = 33,
TLS13 = 34,
}

impl_cmp!(TlsVersion, c_int);

pub const S2N_SSLv2: TlsVersion = TlsVersion::SSLv2;
pub const S2N_SSLv3: TlsVersion = TlsVersion::SSLv3;
pub const S2N_TLS10: TlsVersion = TlsVersion::TLS10;
pub const S2N_TLS11: TlsVersion = TlsVersion::TLS11;
pub const S2N_TLS12: TlsVersion = TlsVersion::TLS12;
pub const S2N_TLS13: TlsVersion = TlsVersion::TLS13;
12 changes: 6 additions & 6 deletions scripts/s2n2rust/s2n2rust/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::path::{Path, PathBuf};
use xshell::{cmd, Shell};

pub fn run(sh: &Shell) -> Result<PathBuf> {
let include = setup_include(&sh)?;
let commands = collect_commands(&sh)?;
let commands = process_commands(&sh, commands, &include)?;
let include = setup_include(sh)?;
let commands = collect_commands(sh)?;
let commands = process_commands(sh, commands, &include)?;

let out = transpile(&sh, &commands)?;
clean_up_entrypoints(&sh, &out)?;
generate_errors(&sh, &out)?;
let out = transpile(sh, &commands)?;
clean_up_entrypoints(sh, &out)?;
generate_errors(sh, &out)?;
Ok(out)
}

Expand Down
33 changes: 29 additions & 4 deletions scripts/s2n2rust/s2n2rust/src/utils/s2n_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,17 @@ pub fn is_overflow_safe<T>(a: T, b: T, max: T) -> bool {
todo!()
}

// TODO get the correct conn type
// TODO add drop
pub struct s2n_blinding_guard<T> {
conn: *mut T,
pub struct s2n_blinding_guard {
conn: *mut core::ffi::c_void,
}

pub fn with_error_blinding<T>(conn: *mut T) -> s2n_blinding_guard<T> {
pub fn with_error_blinding<T>(conn: *mut T) -> s2n_blinding_guard {
todo!("return a guard and apply blinding")
}

pub fn blinding_cancel<T>(guard: s2n_blinding_guard<T>) {
pub fn blinding_cancel(guard: s2n_blinding_guard) {
todo!()
}

Expand Down Expand Up @@ -306,6 +307,30 @@ impl CtPtr {
}
}

pub trait AsBool {
fn as_bool(self) -> bool;
}

macro_rules! impl_as_bool {
($ty:ty) => {
impl AsBool for $ty {
fn as_bool(self) -> bool {
self > 0
}
}
};
}
impl_as_bool!(u8);
impl_as_bool!(i8);
impl_as_bool!(u16);
impl_as_bool!(i16);
impl_as_bool!(u32);
impl_as_bool!(i32);
impl_as_bool!(u64);
impl_as_bool!(i64);
impl_as_bool!(usize);
impl_as_bool!(isize);

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 916287c

Please sign in to comment.