Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libcc2rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub use reinterpret::ByteRepr;
mod rc;
pub use rc::*;

mod libc_shims;
pub use libc_shims::*;

mod fn_ptr;
pub use fn_ptr::FnPtr;

Expand Down
49 changes: 49 additions & 0 deletions libcc2rs/src/libc_shims/dirent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use crate::{ByteRepr, Value};
use std::cell::{Cell, RefCell};
use std::rc::Rc;

pub struct Dirent {
pub d_ino: Value<u64>,
pub d_off: Value<i64>,
pub d_reclen: Value<u16>,
pub d_type: Value<u8>,
pub d_name: Value<Box<[u8]>>,
}

impl Default for Dirent {
fn default() -> Self {
Self {
d_ino: Rc::new(RefCell::new(0)),
d_off: Rc::new(RefCell::new(0)),
d_reclen: Rc::new(RefCell::new(0)),
d_type: Rc::new(RefCell::new(0)),
d_name: Rc::new(RefCell::new(vec![0u8; 256].into_boxed_slice())),
}
}
}

impl Clone for Dirent {
fn clone(&self) -> Self {
Self {
d_ino: Rc::new(RefCell::new(*self.d_ino.borrow())),
d_off: Rc::new(RefCell::new(*self.d_off.borrow())),
d_reclen: Rc::new(RefCell::new(*self.d_reclen.borrow())),
d_type: Rc::new(RefCell::new(*self.d_type.borrow())),
d_name: Rc::new(RefCell::new(self.d_name.borrow().clone())),
}
}
}

impl ByteRepr for Dirent {}

pub struct CDir {
pub entries: Vec<(u64, Vec<u8>, u8)>,
pub pos: Cell<usize>,
}

impl ByteRepr for CDir {}

impl ByteRepr for ::libc::dirent {}
32 changes: 32 additions & 0 deletions libcc2rs/src/libc_shims/ifaddrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use super::Sockaddr;
use crate::{ByteRepr, Ptr, Value};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default)]
pub struct Ifaddrs {
pub ifa_next: Value<Ptr<Ifaddrs>>,
pub ifa_name: Value<Ptr<u8>>,
pub ifa_flags: Value<u32>,
pub ifa_addr: Value<Ptr<Sockaddr>>,
pub ifa_netmask: Value<Ptr<Sockaddr>>,
}

impl Clone for Ifaddrs {
fn clone(&self) -> Self {
Self {
ifa_next: Rc::new(RefCell::new(self.ifa_next.borrow().clone())),
ifa_name: Rc::new(RefCell::new(self.ifa_name.borrow().clone())),
ifa_flags: Rc::new(RefCell::new(*self.ifa_flags.borrow())),
ifa_addr: Rc::new(RefCell::new(self.ifa_addr.borrow().clone())),
ifa_netmask: Rc::new(RefCell::new(self.ifa_netmask.borrow().clone())),
}
}
}

impl ByteRepr for Ifaddrs {}

impl ByteRepr for ::libc::ifaddrs {}
76 changes: 76 additions & 0 deletions libcc2rs/src/libc_shims/ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use crate::{AsPointer, ByteRepr, Ptr, Value};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default)]
pub struct InAddr {
pub s_addr: Value<u32>,
}

pub struct In6Addr {
pub s6_addr: Value<Box<[u8]>>,
}

impl In6Addr {
pub fn s6_addr(&self) -> Ptr<u8> {
self.s6_addr.as_pointer()
}
}

impl Default for In6Addr {
fn default() -> Self {
Self {
s6_addr: Rc::new(RefCell::new(vec![0u8; 16].into_boxed_slice())),
}
}
}

impl Clone for InAddr {
fn clone(&self) -> Self {
Self {
s_addr: Rc::new(RefCell::new(*self.s_addr.borrow())),
}
}
}

impl Clone for In6Addr {
fn clone(&self) -> Self {
Self {
s6_addr: Rc::new(RefCell::new(self.s6_addr.borrow().clone())),
}
}
}

impl ByteRepr for InAddr {
fn byte_size() -> usize {
4
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.s_addr.borrow()).to_bytes(&mut buf[0..4]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
s_addr: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))),
}
}
}

impl ByteRepr for In6Addr {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
buf[0..16].copy_from_slice(&self.s6_addr.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
s6_addr: Rc::new(RefCell::new(buf[0..16].to_vec().into_boxed_slice())),
}
}
}

impl ByteRepr for ::libc::in_addr {}
impl ByteRepr for ::libc::in6_addr {}
24 changes: 24 additions & 0 deletions libcc2rs/src/libc_shims/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

mod dirent;
mod ifaddrs;
mod ip;
mod netdb;
mod poll;
mod pwd;
mod socket;
mod stat;
mod termios;
mod time;

pub use dirent::*;
pub use ifaddrs::*;
pub use ip::*;
pub use netdb::*;
pub use poll::*;
pub use pwd::*;
pub use socket::*;
pub use stat::*;
pub use termios::*;
pub use time::*;
38 changes: 38 additions & 0 deletions libcc2rs/src/libc_shims/netdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use super::Sockaddr;
use crate::{ByteRepr, Ptr, Value};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default)]
pub struct Addrinfo {
pub ai_flags: Value<i32>,
pub ai_family: Value<i32>,
pub ai_socktype: Value<i32>,
pub ai_protocol: Value<i32>,
pub ai_addrlen: Value<u32>,
pub ai_addr: Value<Ptr<Sockaddr>>,
pub ai_canonname: Value<Ptr<u8>>,
pub ai_next: Value<Ptr<Addrinfo>>,
}

impl Clone for Addrinfo {
fn clone(&self) -> Self {
Self {
ai_flags: Rc::new(RefCell::new(*self.ai_flags.borrow())),
ai_family: Rc::new(RefCell::new(*self.ai_family.borrow())),
ai_socktype: Rc::new(RefCell::new(*self.ai_socktype.borrow())),
ai_protocol: Rc::new(RefCell::new(*self.ai_protocol.borrow())),
ai_addrlen: Rc::new(RefCell::new(*self.ai_addrlen.borrow())),
ai_addr: Rc::new(RefCell::new(self.ai_addr.borrow().clone())),
ai_canonname: Rc::new(RefCell::new(self.ai_canonname.borrow().clone())),
ai_next: Rc::new(RefCell::new(self.ai_next.borrow().clone())),
}
}
}

impl ByteRepr for Addrinfo {}

impl ByteRepr for ::libc::addrinfo {}
27 changes: 27 additions & 0 deletions libcc2rs/src/libc_shims/poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use crate::{ByteRepr, Value};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default)]
pub struct Pollfd {
pub fd: Value<i32>,
pub events: Value<i16>,
pub revents: Value<i16>,
}

impl Clone for Pollfd {
fn clone(&self) -> Self {
Self {
fd: Rc::new(RefCell::new(*self.fd.borrow())),
events: Rc::new(RefCell::new(*self.events.borrow())),
revents: Rc::new(RefCell::new(*self.revents.borrow())),
}
}
}

impl ByteRepr for Pollfd {}

impl ByteRepr for ::libc::pollfd {}
35 changes: 35 additions & 0 deletions libcc2rs/src/libc_shims/pwd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use crate::{ByteRepr, Ptr, Value};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default)]
pub struct Passwd {
pub pw_name: Value<Ptr<u8>>,
pub pw_passwd: Value<Ptr<u8>>,
pub pw_uid: Value<u32>,
pub pw_gid: Value<u32>,
pub pw_gecos: Value<Ptr<u8>>,
pub pw_dir: Value<Ptr<u8>>,
pub pw_shell: Value<Ptr<u8>>,
}

impl Clone for Passwd {
fn clone(&self) -> Self {
Self {
pw_name: Rc::new(RefCell::new(self.pw_name.borrow().clone())),
pw_passwd: Rc::new(RefCell::new(self.pw_passwd.borrow().clone())),
pw_uid: Rc::new(RefCell::new(*self.pw_uid.borrow())),
pw_gid: Rc::new(RefCell::new(*self.pw_gid.borrow())),
pw_gecos: Rc::new(RefCell::new(self.pw_gecos.borrow().clone())),
pw_dir: Rc::new(RefCell::new(self.pw_dir.borrow().clone())),
pw_shell: Rc::new(RefCell::new(self.pw_shell.borrow().clone())),
}
}
}

impl ByteRepr for Passwd {}

impl ByteRepr for ::libc::passwd {}
Loading
Loading