Skip to content

Commit

Permalink
Implement socket operations in Inko
Browse files Browse the repository at this point in the history
Instead of relying on Rust code exposed through runtime library
functions, all socket operations are now implemented in Inko's standard
library. For TLS sockets we rely on callbacks for performing TLS reads
and writes, such that we don't need to expose any socket logic to the
TLS code.

By making these changes we're now fully in control of the socket logic,
the functionality we want to support, and how we want to support it. In
addition, we can drop the dependency on the socket2 crate.

In case of TLS sockets this may also result in a more efficient
implementation, as encountering EAGAIN/EWOULDBLOCK only retries the
low-level read/write instead of the entire TLS operation, though it's
difficult to say whether this makes a measurable difference.

This fixes #752.

Changelog: changed
  • Loading branch information
yorickpeterse committed Oct 22, 2024
1 parent 6ac1062 commit ef10a60
Show file tree
Hide file tree
Showing 31 changed files with 2,112 additions and 1,501 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
run: 'cd std && cargo run -- fmt --check'

amd64-linux-gnu:
timeout-minutes: 15
runs-on: ubuntu-latest
container:
image: ghcr.io/inko-lang/ci:fedora
Expand All @@ -84,6 +85,7 @@ jobs:
run: 'cd std && cargo run -- test --opt=none'

amd64-linux-musl:
timeout-minutes: 15
runs-on: ubuntu-latest
container:
image: ghcr.io/inko-lang/ci:alpine
Expand All @@ -99,6 +101,7 @@ jobs:
run: 'cd std && cargo run -- test'

amd64-mac-native:
timeout-minutes: 15
runs-on: macos-13
env:
RUSTUP_HOME: ${{ github.workspace }}/.rustup-home
Expand All @@ -118,6 +121,7 @@ jobs:
run: 'cd std && cargo run -- test'

arm64-mac-native:
timeout-minutes: 15
runs-on: macos-14
env:
RUSTUP_HOME: ${{ github.workspace }}/.rustup-home
Expand All @@ -137,6 +141,7 @@ jobs:
run: 'cd std && cargo run -- test'

amd64-freebsd-native:
timeout-minutes: 15
runs-on: ubuntu-latest
env:
RUSTUP_HOME: ${{ github.workspace }}/.rustup-home
Expand Down
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compiler/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,7 +2467,7 @@ impl<'a> LowerToHir<'a> {
// We special-case this instruction because we need to attach extra
// type information, but don't want to introduce a dedicated
// `size_of` keyword just for this.
if node.name.name == "size_of_type_parameter" {
if node.name.name == "size_of_type_name" {
return self.size_of(node);
}

Expand Down
27 changes: 20 additions & 7 deletions compiler/src/llvm/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,17 +1538,30 @@ impl<'shared, 'module, 'ctx> LowerMethod<'shared, 'module, 'ctx> {
}
Intrinsic::IntSwapBytes => {
let reg_var = self.variables[&ins.register];
let val_var = self.variables[&ins.arguments[0]];
let val = self.builder.load_int(val_var);
let fun = self.module.intrinsic(
"llvm.bswap",
&[self.builder.context.i64_type().into()],
);
let res = self
let val_reg = ins.arguments[0];
let val_var = self.variables[&val_reg];

// This is done such that we can use this intrinsic with
// different integer types.
let val_typ = self.variable_types[&val_reg];
let signed = self
.method
.registers
.value_type(val_reg)
.is_signed_int(&self.shared.state.db);
let val = self
.builder
.load(val_typ, val_var)
.into_int_value();
let fun =
self.module.intrinsic("llvm.bswap", &[val_typ]);
let swapped = self
.builder
.call(fun, &[val.into()])
.into_int_value();

let res = self.builder.int_to_int(swapped, 64, signed);

self.builder.store(reg_var, res);
}
Intrinsic::IntAbsolute => {
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,11 @@ impl Mir {
Instruction::Send(i) => {
used[i.method.0 as usize] = true;
}
// Extern methods with a body shouldn't be removed if we
// create pointers to them.
Instruction::MethodPointer(i) => {
used[i.method.0 as usize] = true;
}
Instruction::CallDynamic(i) => {
let id = i.method;
let tid = id
Expand Down
8 changes: 2 additions & 6 deletions rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,12 @@ rustls-pemfile = "^2.1"
# https://github.com/inko-lang/inko/issues/329 for more details.
once_cell = "1.9"

[target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "tvos")))'.dependencies]
[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
rustls-native-certs = "0.7"
webpki = { package = "rustls-webpki", version = "0.102", default-features = false }

[target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))'.dependencies]
[target.'cfg(any(target_os = "macos"))'.dependencies]
core-foundation = "0.9"
core-foundation-sys = "0.8"
security-framework = { version = "2.10", features = ["OSX_10_14"] }
security-framework-sys = { version = "2.10", features = ["OSX_10_14"] }

[dependencies.socket2]
version = "^0.5"
features = ["all"]
6 changes: 3 additions & 3 deletions rt/src/result.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rustix::io::Errno;
use std::io;

const OK: i64 = 0;
const NONE: i64 = 1;
const ERROR: i64 = 2;
pub(crate) const OK: i64 = 0;
pub(crate) const NONE: i64 = 1;
pub(crate) const ERROR: i64 = 2;

pub(crate) fn error_to_int(error: io::Error) -> i64 {
let code = if let Some(code) = error.raw_os_error() {
Expand Down
1 change: 0 additions & 1 deletion rt/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod class;
mod env;
mod float;
mod general;
mod helpers;
mod int;
mod process;
mod random;
Expand Down
52 changes: 0 additions & 52 deletions rt/src/runtime/helpers.rs

This file was deleted.

Loading

0 comments on commit ef10a60

Please sign in to comment.