-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements the current garbage collector in Rust. No changes were made to the GC design -- it's just ports the one implemented in code generator to Rust. The goals are: - Evaluate Rust for Motoko's RTS implementation - Make the collector easier to read, understand, modify, and extend. Currently passes the tests locally. We can't run this branch on CI yet as it needs to download rustc nightly and xargo and the domains are not allowed on the CI. I think in the final version we'll have to build rustc outselves instead of downloading. (Nightly rustc is needed as "core" distributed with rustc is not built with PIC relocation model on wam32, so we can't use it to generate a shared wasm32 library) Main changes: - New Rust crate "motoko-rts" introduced, which currently implements the garbage collector. It also has some utilities for printing the heap or individual objects, to be used when debugging. - Nix files updated to download rustc and xargo. These are used to build Rust's "core" library with PIC relocation model for wasm32. - We no longer build memset and memcpy of musl as those are provided by Rust's "core" now. The main algorithm is in `gc.rs`. Rest of the Rust files are helpers, mainly for debugging. Other changes: - I had to update lots of ic-ref-run outputs. See #1854 for the details. Remaining work and issues: - Figure out how to use rustc nightly (with PIC wasm32 libraries) in CI. Note to reviewers: - The main algorithm is in `gc.rs`, everything else is helpers. Start reading from `gc.rs`.
- Loading branch information
Showing
120 changed files
with
1,263 additions
and
592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
_out | ||
_output | ||
_build | ||
target | ||
|
||
**/*~ | ||
/result* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# xargo is used to build motoko-rts for wasm32. We need to make a shared Wasm | ||
# library for the RTS (that's what moc-ld supports) but Rust ships wasm32 | ||
# libraries (core and std) without PIC relocation model, so we use xargo to make | ||
# PIC versions of core and std. | ||
|
||
{ rustPlatform-nightly, fetchFromGitHub, lib, python, cmake, llvmPackages, clang, stdenv, darwin, zlib }: | ||
|
||
rustPlatform-nightly.buildRustPackage rec { | ||
name = "xargo"; | ||
|
||
src = fetchFromGitHub { | ||
owner = "japaric"; | ||
repo = "${name}"; | ||
rev = "16035a7c401262824edcb87e1401fe4b05a5ccc0"; | ||
sha256 = "0m1dg7vwmmlpqp20p219gsm7zbnnii6lik6hc2vvfsdmnygf271l"; | ||
fetchSubmodules = true; | ||
}; | ||
|
||
cargoSha256 = "0zzksgi2prgw01m6r4bqjjz902h5g5ich0h3xvb60w4sshlss891"; | ||
|
||
# nativeBuildInputs = [ python cmake clang ]; | ||
# buildInputs = [ llvmPackages.libclang ] ++ | ||
# lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security ]; | ||
# LIBCLANG_PATH = "${llvmPackages.libclang}/lib"; | ||
|
||
doCheck = false; | ||
# error: couldn't lock thumbv6m-panic_abort-eabi's sysroot as read-only | ||
USER = "nobody"; # for xargo tests (if we would run them) | ||
|
||
meta = with lib; { | ||
description = "The sysroot manager that lets you build and customize std"; | ||
homepage = "https://github.com/japaric/xargo"; | ||
license = licenses.mit; | ||
maintainers = [ { | ||
email = "omer.agacan@dfinity.org"; | ||
github = "osa1"; | ||
githubId = 123123; | ||
name = "Ömer Sinan Ağacan"; | ||
} ]; | ||
platforms = platforms.unix; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# NB. codegen-units=1 is not necessary, but it generates less .o files for core, | ||
# std, and compiler_builtins and makes it easier to find symbols. | ||
[build] | ||
rustflags = ["-Crelocation-model=pic", "-Ccodegen-units=1"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// https://github.com/rust-analyzer/rust-analyzer/blob/master/editors/code/package.json | ||
{ | ||
"rust-analyzer.cargo.target": "wasm32-unknown-emscripten", | ||
|
||
// This is required as `cargo check --all-targets` doesn't seem to work well | ||
// on no-std crates, it generates false "duplicate lang item" errors. | ||
"rust-analyzer.checkOnSave.allTargets": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "motoko-rts" | ||
version = "0.1.0" | ||
authors = ["Ömer Sinan Ağacan <omeragacan@gmail.com>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
libc = "0.2.73" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[profile.dev] | ||
panic = "abort" | ||
|
||
[profile.release] | ||
panic = "abort" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Implements allocation routines used by the generated code and the GC. | ||
|
||
use core::arch::wasm32; | ||
|
||
use crate::gc; | ||
use crate::rts_trap_with; | ||
use crate::types::*; | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn alloc_bytes(n: Bytes<u32>) -> SkewedPtr { | ||
alloc_words(bytes_to_words(n)) | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn alloc_words(n: Words<u32>) -> SkewedPtr { | ||
let bytes = words_to_bytes(n); | ||
// Update ALLOCATED | ||
gc::ALLOCATED.0 += bytes.0 as u64; | ||
|
||
// Update heap pointer | ||
let old_hp = gc::get_hp(); | ||
let new_hp = old_hp + bytes.0 as usize; | ||
gc::set_hp(new_hp); | ||
|
||
// Grow memory if needed | ||
grow_memory(new_hp); | ||
|
||
skew(old_hp) | ||
} | ||
|
||
/// Page allocation. Ensures that the memory up to the given pointer is allocated. | ||
pub(crate) unsafe fn grow_memory(ptr: usize) { | ||
let total_pages_needed = ((ptr / 65536) + 1) as i32; | ||
let current_pages = wasm32::memory_size(0) as i32; | ||
let new_pages_needed = total_pages_needed - current_pages; | ||
if new_pages_needed > 0 { | ||
if wasm32::memory_grow(0, new_pages_needed as usize) == core::usize::MAX { | ||
rts_trap_with("Cannot grow memory\0".as_ptr()); | ||
} | ||
} | ||
} |
Oops, something went wrong.