Skip to content

Commit 358ea1b

Browse files
committed
Add refcount shims for libc structs
1 parent b0296fb commit 358ea1b

11 files changed

Lines changed: 939 additions & 0 deletions

File tree

libcc2rs/src/libc_shims/dirent.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::{ByteRepr, Value};
5+
use std::cell::RefCell;
6+
use std::rc::Rc;
7+
8+
pub struct dirent {
9+
pub d_ino: Value<u64>,
10+
pub d_off: Value<i64>,
11+
pub d_reclen: Value<u16>,
12+
pub d_type: Value<u8>,
13+
pub d_name: Value<Box<[u8]>>,
14+
}
15+
16+
impl Default for dirent {
17+
fn default() -> Self {
18+
Self {
19+
d_ino: Rc::new(RefCell::new(0)),
20+
d_off: Rc::new(RefCell::new(0)),
21+
d_reclen: Rc::new(RefCell::new(0)),
22+
d_type: Rc::new(RefCell::new(0)),
23+
d_name: Rc::new(RefCell::new(vec![0u8; 256].into_boxed_slice())),
24+
}
25+
}
26+
}
27+
28+
impl Clone for dirent {
29+
fn clone(&self) -> Self {
30+
Self {
31+
d_ino: Rc::new(RefCell::new(*self.d_ino.borrow())),
32+
d_off: Rc::new(RefCell::new(*self.d_off.borrow())),
33+
d_reclen: Rc::new(RefCell::new(*self.d_reclen.borrow())),
34+
d_type: Rc::new(RefCell::new(*self.d_type.borrow())),
35+
d_name: Rc::new(RefCell::new(self.d_name.borrow().clone())),
36+
}
37+
}
38+
}
39+
40+
impl ByteRepr for dirent {}
41+
42+
pub struct CDir {
43+
pub entries: Vec<(u64, Vec<u8>, u8)>,
44+
pub pos: ::std::cell::Cell<usize>,
45+
}
46+
47+
impl ByteRepr for CDir {}
48+
49+
impl ByteRepr for ::libc::dirent {}

libcc2rs/src/libc_shims/ifaddrs.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use super::sockaddr;
5+
use crate::{ByteRepr, Ptr, Value};
6+
use std::cell::RefCell;
7+
use std::rc::Rc;
8+
9+
pub struct ifaddrs {
10+
pub ifa_next: Value<Ptr<ifaddrs>>,
11+
pub ifa_name: Value<Ptr<u8>>,
12+
pub ifa_flags: Value<u32>,
13+
pub ifa_addr: Value<Ptr<sockaddr>>,
14+
pub ifa_netmask: Value<Ptr<sockaddr>>,
15+
}
16+
17+
impl Default for ifaddrs {
18+
fn default() -> Self {
19+
Self {
20+
ifa_next: Rc::new(RefCell::new(Ptr::null())),
21+
ifa_name: Rc::new(RefCell::new(Ptr::null())),
22+
ifa_flags: Rc::new(RefCell::new(0)),
23+
ifa_addr: Rc::new(RefCell::new(Ptr::null())),
24+
ifa_netmask: Rc::new(RefCell::new(Ptr::null())),
25+
}
26+
}
27+
}
28+
29+
impl Clone for ifaddrs {
30+
fn clone(&self) -> Self {
31+
Self {
32+
ifa_next: Rc::new(RefCell::new(self.ifa_next.borrow().clone())),
33+
ifa_name: Rc::new(RefCell::new(self.ifa_name.borrow().clone())),
34+
ifa_flags: Rc::new(RefCell::new(*self.ifa_flags.borrow())),
35+
ifa_addr: Rc::new(RefCell::new(self.ifa_addr.borrow().clone())),
36+
ifa_netmask: Rc::new(RefCell::new(self.ifa_netmask.borrow().clone())),
37+
}
38+
}
39+
}
40+
41+
impl ByteRepr for ifaddrs {}
42+
43+
impl ByteRepr for ::libc::ifaddrs {}

libcc2rs/src/libc_shims/ip.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::{AsPointer, ByteRepr, Ptr, Value};
5+
use std::cell::RefCell;
6+
use std::rc::Rc;
7+
8+
#[derive(Default)]
9+
pub struct in_addr {
10+
pub s_addr: Value<u32>,
11+
}
12+
13+
pub struct in6_addr {
14+
pub s6_addr: Value<Box<[u8]>>,
15+
}
16+
17+
impl in6_addr {
18+
pub fn s6_addr(&self) -> Ptr<u8> {
19+
self.s6_addr.as_pointer()
20+
}
21+
}
22+
23+
impl Default for in6_addr {
24+
fn default() -> Self {
25+
Self {
26+
s6_addr: Rc::new(RefCell::new(vec![0u8; 16].into_boxed_slice())),
27+
}
28+
}
29+
}
30+
31+
impl Clone for in_addr {
32+
fn clone(&self) -> Self {
33+
Self {
34+
s_addr: Rc::new(RefCell::new(*self.s_addr.borrow())),
35+
}
36+
}
37+
}
38+
39+
impl Clone for in6_addr {
40+
fn clone(&self) -> Self {
41+
Self {
42+
s6_addr: Rc::new(RefCell::new(self.s6_addr.borrow().clone())),
43+
}
44+
}
45+
}
46+
47+
impl ByteRepr for in_addr {
48+
fn byte_size() -> usize {
49+
4
50+
}
51+
fn to_bytes(&self, buf: &mut [u8]) {
52+
(*self.s_addr.borrow()).to_bytes(&mut buf[0..4]);
53+
}
54+
fn from_bytes(buf: &[u8]) -> Self {
55+
Self {
56+
s_addr: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))),
57+
}
58+
}
59+
}
60+
61+
impl ByteRepr for in6_addr {
62+
fn byte_size() -> usize {
63+
16
64+
}
65+
fn to_bytes(&self, buf: &mut [u8]) {
66+
buf[0..16].copy_from_slice(&self.s6_addr.borrow());
67+
}
68+
fn from_bytes(buf: &[u8]) -> Self {
69+
Self {
70+
s6_addr: Rc::new(RefCell::new(buf[0..16].to_vec().into_boxed_slice())),
71+
}
72+
}
73+
}
74+
75+
impl ByteRepr for ::libc::in_addr {}
76+
impl ByteRepr for ::libc::in6_addr {}

libcc2rs/src/libc_shims/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#![allow(non_camel_case_types)]
5+
6+
mod dirent;
7+
mod ifaddrs;
8+
mod ip;
9+
mod netdb;
10+
mod poll;
11+
mod pwd;
12+
mod socket;
13+
mod stat;
14+
mod termios;
15+
mod time;
16+
17+
pub use dirent::*;
18+
pub use ifaddrs::*;
19+
pub use ip::*;
20+
pub use netdb::*;
21+
pub use poll::*;
22+
pub use pwd::*;
23+
pub use socket::*;
24+
pub use stat::*;
25+
pub use termios::*;
26+
pub use time::*;

libcc2rs/src/libc_shims/netdb.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use super::sockaddr;
5+
use crate::{ByteRepr, Ptr, Value};
6+
use std::cell::RefCell;
7+
use std::rc::Rc;
8+
9+
pub struct addrinfo {
10+
pub ai_flags: Value<i32>,
11+
pub ai_family: Value<i32>,
12+
pub ai_socktype: Value<i32>,
13+
pub ai_protocol: Value<i32>,
14+
pub ai_addrlen: Value<u32>,
15+
pub ai_addr: Value<Ptr<sockaddr>>,
16+
pub ai_canonname: Value<Ptr<u8>>,
17+
pub ai_next: Value<Ptr<addrinfo>>,
18+
}
19+
20+
impl Default for addrinfo {
21+
fn default() -> Self {
22+
Self {
23+
ai_flags: Rc::new(RefCell::new(0)),
24+
ai_family: Rc::new(RefCell::new(0)),
25+
ai_socktype: Rc::new(RefCell::new(0)),
26+
ai_protocol: Rc::new(RefCell::new(0)),
27+
ai_addrlen: Rc::new(RefCell::new(0)),
28+
ai_addr: Rc::new(RefCell::new(Ptr::null())),
29+
ai_canonname: Rc::new(RefCell::new(Ptr::null())),
30+
ai_next: Rc::new(RefCell::new(Ptr::null())),
31+
}
32+
}
33+
}
34+
35+
impl Clone for addrinfo {
36+
fn clone(&self) -> Self {
37+
Self {
38+
ai_flags: Rc::new(RefCell::new(*self.ai_flags.borrow())),
39+
ai_family: Rc::new(RefCell::new(*self.ai_family.borrow())),
40+
ai_socktype: Rc::new(RefCell::new(*self.ai_socktype.borrow())),
41+
ai_protocol: Rc::new(RefCell::new(*self.ai_protocol.borrow())),
42+
ai_addrlen: Rc::new(RefCell::new(*self.ai_addrlen.borrow())),
43+
ai_addr: Rc::new(RefCell::new(self.ai_addr.borrow().clone())),
44+
ai_canonname: Rc::new(RefCell::new(self.ai_canonname.borrow().clone())),
45+
ai_next: Rc::new(RefCell::new(self.ai_next.borrow().clone())),
46+
}
47+
}
48+
}
49+
50+
impl ByteRepr for addrinfo {}
51+
52+
impl ByteRepr for ::libc::addrinfo {}

libcc2rs/src/libc_shims/poll.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::{ByteRepr, Value};
5+
use std::cell::RefCell;
6+
use std::rc::Rc;
7+
8+
pub struct pollfd {
9+
pub fd: Value<i32>,
10+
pub events: Value<i16>,
11+
pub revents: Value<i16>,
12+
}
13+
14+
impl Default for pollfd {
15+
fn default() -> Self {
16+
Self {
17+
fd: Rc::new(RefCell::new(0)),
18+
events: Rc::new(RefCell::new(0)),
19+
revents: Rc::new(RefCell::new(0)),
20+
}
21+
}
22+
}
23+
24+
impl Clone for pollfd {
25+
fn clone(&self) -> Self {
26+
Self {
27+
fd: Rc::new(RefCell::new(*self.fd.borrow())),
28+
events: Rc::new(RefCell::new(*self.events.borrow())),
29+
revents: Rc::new(RefCell::new(*self.revents.borrow())),
30+
}
31+
}
32+
}
33+
34+
impl ByteRepr for pollfd {}
35+
36+
impl ByteRepr for ::libc::pollfd {}

libcc2rs/src/libc_shims/pwd.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::{ByteRepr, Ptr, Value};
5+
use std::cell::RefCell;
6+
use std::rc::Rc;
7+
8+
pub struct passwd {
9+
pub pw_name: Value<Ptr<u8>>,
10+
pub pw_passwd: Value<Ptr<u8>>,
11+
pub pw_uid: Value<u32>,
12+
pub pw_gid: Value<u32>,
13+
pub pw_gecos: Value<Ptr<u8>>,
14+
pub pw_dir: Value<Ptr<u8>>,
15+
pub pw_shell: Value<Ptr<u8>>,
16+
}
17+
18+
impl Default for passwd {
19+
fn default() -> Self {
20+
Self {
21+
pw_name: Rc::new(RefCell::new(Ptr::null())),
22+
pw_passwd: Rc::new(RefCell::new(Ptr::null())),
23+
pw_uid: Rc::new(RefCell::new(0)),
24+
pw_gid: Rc::new(RefCell::new(0)),
25+
pw_gecos: Rc::new(RefCell::new(Ptr::null())),
26+
pw_dir: Rc::new(RefCell::new(Ptr::null())),
27+
pw_shell: Rc::new(RefCell::new(Ptr::null())),
28+
}
29+
}
30+
}
31+
32+
impl Clone for passwd {
33+
fn clone(&self) -> Self {
34+
Self {
35+
pw_name: Rc::new(RefCell::new(self.pw_name.borrow().clone())),
36+
pw_passwd: Rc::new(RefCell::new(self.pw_passwd.borrow().clone())),
37+
pw_uid: Rc::new(RefCell::new(*self.pw_uid.borrow())),
38+
pw_gid: Rc::new(RefCell::new(*self.pw_gid.borrow())),
39+
pw_gecos: Rc::new(RefCell::new(self.pw_gecos.borrow().clone())),
40+
pw_dir: Rc::new(RefCell::new(self.pw_dir.borrow().clone())),
41+
pw_shell: Rc::new(RefCell::new(self.pw_shell.borrow().clone())),
42+
}
43+
}
44+
}
45+
46+
impl ByteRepr for passwd {}
47+
48+
impl ByteRepr for ::libc::passwd {}

0 commit comments

Comments
 (0)