Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
25 changes: 2 additions & 23 deletions lind_config.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
glibc_base="$script_dir/src/glibc"
wasmtime_base="$script_dir/src/wasmtime"
rustposix_base="$script_dir/src/safeposix-rust"
rawposix_base="$script_dir/src/RawPOSIX"


CC="${CLANG:=/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04}/bin/clang"

export_cmd="export LD_LIBRARY_PATH=$wasmtime_base/crates/rustposix:\$LD_LIBRARY_PATH"

compile_test_cmd_fork="$CC --target=wasm32-unknown-wasi \
--sysroot $glibc_base/sysroot \
-Wl, --export="__stack_pointer",--export=__stack_low \
[input] -g -O0 -o [output] && \
wasm-opt --asyncify --debuginfo [output] -o [output]"

compile_test_cmd_noshared="$CC --target=wasm32-unknown-wasi \
--sysroot $glibc_base/sysroot \
-Wl,--export="__stack_pointer",--export=__stack_low \
[input] -g -O0 -o [output]"

compile_test_cmd="$CC -pthread --target=wasm32-unknown-wasi \
--sysroot $glibc_base/sysroot \
-Wl,--import-memory,--export-memory,--max-memory=67108864,--export=__stack_low \
[input] -g -O0 -o [output]"

precompile_wasm="$wasmtime_base/target/debug/wasmtime compile [input] -o [output]"

compile_test_cmd_fork_test="$CC -pthread --target=wasm32-unknown-wasi \
--sysroot $glibc_base/sysroot \
-Wl,--import-memory,--export-memory,--max-memory=67108864,--export="__stack_pointer",--export=__stack_low \
[input] -g -O0 -o [output] && \
wasm-opt --asyncify --debuginfo [output] -o [output]"
$script_dir/tools/binaryen/bin/wasm-opt --epoch-injection --asyncify -O2 --debuginfo [output] -o [output]"

run_cmd="$wasmtime_base/target/debug/wasmtime run --wasi \
threads=y \
Expand All @@ -49,8 +29,7 @@ run_cmd_debug="gdb --args $wasmtime_base/target/debug/wasmtime run \
--wasi preview2=n [target]"

compile_wasmtime_cmd="cd $wasmtime_base && cargo build"
compile_rustposix_cmd="cd $rustposix_base && cargo build && cp $rustposix_base/target/debug/librustposix.so $wasmtime_base/crates/rustposix"
compile_rawposix_cmd="cd $rawposix_base && cargo build && cp $rawposix_base/target/debug/librustposix.so $wasmtime_base/crates/rustposix"
compile_rawposix_cmd="cd $rawposix_base && cargo build"

compile_pthread_create="$CC --target=wasm32-unkown-wasi \
-v -Wno-int-conversion pthread_create.c \
Expand Down
68 changes: 8 additions & 60 deletions lindtool.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

# Load configuration
source "$(dirname "$0")/lind_config.sh"

Expand Down Expand Up @@ -173,10 +175,6 @@ if [ "${!#}" = "-p" ]; then
fi

case $1 in
export)
echo -e "${GREEN}$export_cmd${RESET}"
echo "please manually run the export command"
;;
compile_test|cptest)
if [ -z "$2" ]; then
echo -e "${RED}error: source file name not provided${RESET}"
Expand Down Expand Up @@ -257,12 +255,6 @@ case $1 in
eval "$compile_wasmtime_cmd"
fi
;;
compile_rustposix|cpposix)
echo -e "${GREEN}$compile_rustposix_cmd${RESET}"
if [ "$pmode" -eq 0 ]; then
eval "$compile_rustposix_cmd"
fi
;;
compile_rawposix|cpraw)
echo -e "${GREEN}$compile_rawposix_cmd${RESET}"
if [ "$pmode" -eq 0 ]; then
Expand All @@ -277,64 +269,20 @@ case $1 in

compile_src $2 $3
;;
make_all)
make_all|make_glibc)
unset LD_LIBRARY_PATH
echo -e "${GREEN}$make_cmd${RESET}"
if [ "$pmode" -eq 0 ]; then
eval "$make_cmd"
fi
;;
make)
result=$(lindmake "$glibc_base" "$pmode")
if [ -z "$result" ]; then
echo -e "${GREEN}No changes detected.${RESET}"
exit 0
fi
echo $result

# Set IFS to semicolon and read the string into an array
IFS=';' read -r -a array <<< "$result"

compiled=()

# Print each element of the array
for line in "${array[@]}"; do
# Split the string into two variables
if [[ "$line" =~ ^([^[:space:]]+)([[:space:]]+)(.+)$ ]]; then
src_file=${BASH_REMATCH[1]}
compiled+=($src_file)
compile_src $src_file ${BASH_REMATCH[3]}

if [ $? -ne 0 ]; then
echo -e "${RED}Compilation Failed.${RESET}"
exit 1
fi
else
compile_src $line

if [ $? -ne 0 ]; then
echo -e "${RED}Compilation Failed.${RESET}"
exit 1
fi
fi
done

echo ""
for file in "${compiled[@]}"; do
echo -e "${GREEN}Compiled $file successfully.${RESET}"
done

;;
help)
echo "avaliable commands are:"
echo "1. export"
echo "2. compile_test"
echo "3. run"
echo "4. compile_wasmtime"
echo "5. compile_rustposix"
echo "6. compile_src"
echo "7. make_all"
echo "8. make"
echo "1. compile_test"
echo "2. run"
echo "3. compile_wasmtime"
echo "4. compile_src"
echo "5. make_all"
;;
*)
echo "Unknown command identifier."
Expand Down
244 changes: 6 additions & 238 deletions src/sysdefs/src/data/net_struct.rs
Original file line number Diff line number Diff line change
@@ -1,241 +1,9 @@
/// THIS FILE NEEDS TO BE REFACTORED LATER!
///
use std::str::from_utf8;
use std::sync::atomic::{AtomicUsize, Ordering};
use libc::*;

extern crate libc;

static mut UD_ID_COUNTER: AtomicUsize = AtomicUsize::new(0);

#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum GenSockaddr {
Unix(SockaddrUnix),
V4(SockaddrV4),
V6(SockaddrV6),
}
impl GenSockaddr {
pub fn port(&self) -> u16 {
match self {
GenSockaddr::Unix(_) => panic!("Invalid function called for this type of Sockaddr."),
GenSockaddr::V4(v4addr) => v4addr.sin_port,
GenSockaddr::V6(v6addr) => v6addr.sin6_port,
}
}
pub fn set_port(&mut self, port: u16) {
match self {
GenSockaddr::Unix(_) => panic!("Invalid function called for this type of Sockaddr."),
GenSockaddr::V4(v4addr) => v4addr.sin_port = port,
GenSockaddr::V6(v6addr) => v6addr.sin6_port = port,
};
}

pub fn addr(&self) -> GenIpaddr {
match self {
GenSockaddr::Unix(_) => panic!("Invalid function called for this type of Sockaddr."),
GenSockaddr::V4(v4addr) => GenIpaddr::V4(v4addr.sin_addr),
GenSockaddr::V6(v6addr) => GenIpaddr::V6(v6addr.sin6_addr),
}
}

pub fn set_addr(&mut self, ip: GenIpaddr) {
match self {
GenSockaddr::Unix(_unixaddr) => {
panic!("Invalid function called for this type of Sockaddr.")
}
GenSockaddr::V4(v4addr) => {
v4addr.sin_addr = if let GenIpaddr::V4(v4ip) = ip {
v4ip
} else {
unreachable!()
}
}
GenSockaddr::V6(v6addr) => {
v6addr.sin6_addr = if let GenIpaddr::V6(v6ip) = ip {
v6ip
} else {
unreachable!()
}
}
};
}

pub fn set_family(&mut self, family: u16) {
match self {
GenSockaddr::Unix(unixaddr) => unixaddr.sun_family = family,
GenSockaddr::V4(v4addr) => v4addr.sin_family = family,
GenSockaddr::V6(v6addr) => v6addr.sin6_family = family,
};
}

pub fn get_family(&self) -> u16 {
match self {
GenSockaddr::Unix(unixaddr) => unixaddr.sun_family,
GenSockaddr::V4(v4addr) => v4addr.sin_family,
GenSockaddr::V6(v6addr) => v6addr.sin6_family,
}
}

pub fn path(&self) -> &str {
match self {
GenSockaddr::Unix(unixaddr) => {
let pathiter = &mut unixaddr.sun_path.split(|idx| *idx == 0);
let pathslice = pathiter.next().unwrap();
let path = from_utf8(pathslice).unwrap();
path
}
GenSockaddr::V4(_) => panic!("Invalid function called for this type of Sockaddr."),
GenSockaddr::V6(_) => panic!("Invalid function called for this type of Sockaddr."),
}
// create a sockaddr_un struct
pub fn create_sockaddr_un() -> sockaddr_un{
sockaddr_un {
sun_family: 0,
sun_path: [0; 108],
}
}

#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
pub enum GenIpaddr {
V4(V4Addr),
V6(V6Addr),
}

impl GenIpaddr {
pub fn is_unspecified(&self) -> bool {
match self {
GenIpaddr::V4(v4ip) => v4ip.s_addr == 0,
GenIpaddr::V6(v6ip) => v6ip.s6_addr == [0; 16],
}
}
pub fn from_string(string: &str) -> Option<Self> {
let v4candidate: Vec<&str> = string.split('.').collect();
let v6candidate: Vec<&str> = string.split(':').collect();
let v4l = v4candidate.len();
let v6l = v6candidate.len();
if v4l == 1 && v6l > 1 {
//then we should try parsing it as an ipv6 address
let mut shortarr = [0u8; 16];
let mut shortindex = 0;
let mut encountered_doublecolon = false;
for short in v6candidate {
if short.is_empty() {
//you can only have a double colon once in an ipv6 address
if encountered_doublecolon {
return None;
}
encountered_doublecolon = true;

let numzeros = 8 - v6l + 1; //+1 to account for this empty string element
if numzeros == 0 {
return None;
}
shortindex += numzeros;
} else {
//ok we can actually parse the element in this case
if let Ok(b) = short.parse::<u16>() {
//manually handle big endianness
shortarr[2 * shortindex] = (b >> 8) as u8;
shortarr[2 * shortindex + 1] = (b & 0xff) as u8;
shortindex += 1;
} else {
return None;
}
}
}
return Some(Self::V6(V6Addr { s6_addr: shortarr }));
} else if v4l == 4 && v6l == 1 {
//then we should try parsing it as an ipv4 address
let mut bytearr = [0u8; 4];
let mut shortindex = 0;
for byte in v4candidate {
if let Ok(b) = byte.parse::<u8>() {
bytearr[shortindex] = b;
shortindex += 1;
} else {
return None;
}
}
return Some(Self::V4(V4Addr {
s_addr: u32::from_ne_bytes(bytearr),
}));
} else {
return None;
}
}
}

#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub struct SockaddrUnix {
pub sun_family: u16,
pub sun_path: [u8; 108],
}

impl Default for SockaddrUnix {
fn default() -> Self {
SockaddrUnix {
sun_family: 0,
sun_path: [0; 108],
}
}
}

pub fn new_sockaddr_unix(family: u16, path: &[u8]) -> SockaddrUnix {
let pathlen = path.len();
if pathlen > 108 {
panic!("Unix domain paths cannot exceed 108 bytes.")
}
let mut array_path: [u8; 108] = [0; 108];
array_path[0..pathlen].copy_from_slice(path);
SockaddrUnix {
sun_family: family,
sun_path: array_path,
}
}

pub fn gen_ud_path() -> String {
let mut owned_path: String = "/sock".to_owned();
unsafe {
let id = UD_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
owned_path.push_str(&id.to_string());
}
owned_path.clone()
}

#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)]
pub struct V4Addr {
pub s_addr: u32,
}
#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)]
pub struct SockaddrV4 {
pub sin_family: u16,
pub sin_port: u16,
pub sin_addr: V4Addr,
pub padding: u64,
}

#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)]
pub struct V6Addr {
pub s6_addr: [u8; 16],
}
#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)]
pub struct SockaddrV6 {
pub sin6_family: u16,
pub sin6_port: u16,
pub sin6_flowinfo: u32,
pub sin6_addr: V6Addr,
pub sin6_scope_id: u32,
}

#[derive(Debug, Default)]
#[repr(C)]
pub struct PollStruct {
pub fd: i32,
pub events: i16,
pub revents: i16,
}

#[repr(C)]
pub struct SockaddrDummy {
pub sa_family: u16,
pub _sa_data: [u16; 14],
}
Loading