Skip to content

Commit 11ca70e

Browse files
committed
Use Rust nightly
1 parent b1a21de commit 11ca70e

File tree

9 files changed

+190
-29
lines changed

9 files changed

+190
-29
lines changed

default.nix

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ let
2222
rtsBuildInputs = [
2323
nixpkgs.clang_10 # for native/wasm building
2424
nixpkgs.lld_10 # for wasm building
25-
nixpkgs.rustc
26-
nixpkgs.cargo
25+
nixpkgs.rustc-nightly
26+
nixpkgs.cargo-nightly
27+
nixpkgs.xargo
2728
];
2829

2930
llvmEnv = ''
@@ -331,6 +332,7 @@ rec {
331332
[ { name = "bin/FileCheck"; path = "${nixpkgs.llvm}/bin/FileCheck";} ];
332333
wabt = nixpkgs.wabt;
333334
wasmtime = nixpkgs.wasmtime;
335+
xargo = nixpkgs.xargo;
334336
wasm = nixpkgs.wasm;
335337

336338
overview-slides = stdenv.mkDerivation {

nix/default.nix

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,30 @@ let
1010
import nixpkgs_src {
1111
inherit system;
1212
overlays = [
13-
(self: super: { sources = import sourcesnix { sourcesFile = ./sources.json; pkgs = super; }; })
13+
# rust nightly
14+
(self: super: let
15+
moz_overlay = import (self.fetchzip {
16+
url = https://github.com/mozilla/nixpkgs-mozilla/archive/efda5b357451dbb0431f983cca679ae3cd9b9829.tar.gz;
17+
sha256 = "11wqrg86g3qva67vnk81ynvqyfj0zxk83cbrf0p9hsvxiwxs8469";
18+
}) self super;
19+
rust-channel = moz_overlay.rustChannelOf { date = "2020-07-22"; channel = "nightly"; };
20+
in rec {
21+
rustc-nightly = rust-channel.rust.override {
22+
targets = [ "wasm32-unknown-unknown" "wasm32-unknown-emscripten" ];
23+
extensions = ["rust-src"];
24+
};
25+
cargo-nightly = rustc-nightly;
26+
rustPlatform-nightly = pkgs.makeRustPlatform {
27+
rustc = rustc-nightly;
28+
cargo = cargo-nightly;
29+
};
30+
})
31+
32+
# add nix/sources.json
33+
(self: super: {
34+
sources = import sourcesnix { sourcesFile = ./sources.json; pkgs = super; };
35+
})
36+
1437
# Selecting the ocaml version
1538
# (self: super: { ocamlPackages = super.ocamlPackages; })
1639
(
@@ -30,21 +53,21 @@ let
3053
inherit (self) ocamlPackages;
3154
};
3255
};
33-
# wasmtime
3456
wasmtime = self.callPackage ./wasmtime.nix {};
57+
xargo = self.callPackage ./xargo.nix {};
3558
}
3659
)
3760
# nixpkgs's rustc does not include the wasm32-unknown-unknown target, so
3861
# lets add it here.
39-
(self: super: {
40-
rustc = super.rustc.overrideAttrs (old: {
41-
configureFlags = self.lib.lists.forEach old.configureFlags (flag:
42-
if self.lib.strings.hasPrefix "--target=" flag
43-
then flag + ",wasm32-unknown-unknown,wasm32-unknown-emscripten"
44-
else flag
45-
);
46-
});
47-
})
62+
# (self: super: {
63+
# rustc = super.rustc.overrideAttrs (old: {
64+
# configureFlags = self.lib.lists.forEach old.configureFlags (flag:
65+
# if self.lib.strings.hasPrefix "--target=" flag
66+
# then flag + ",wasm32-unknown-unknown,wasm32-unknown-emscripten"
67+
# else flag
68+
# );
69+
# });
70+
# })
4871
];
4972
};
5073
in

nix/xargo.nix

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# xargo is used to build motoko-rts for wasm32. We need to make a shared Wasm
2+
# library for the RTS (that's what moc-ld supports) but Rust ships wasm32
3+
# libraries (core and std) without PIC relocation model, so we use xargo to make
4+
# PIC versions of core and std.
5+
6+
{ rustPlatform-nightly, fetchFromGitHub, lib, python, cmake, llvmPackages, clang, stdenv, darwin, zlib }:
7+
8+
rustPlatform-nightly.buildRustPackage rec {
9+
name = "xargo";
10+
11+
src = fetchFromGitHub {
12+
owner = "japaric";
13+
repo = "${name}";
14+
rev = "16035a7c401262824edcb87e1401fe4b05a5ccc0";
15+
sha256 = "0m1dg7vwmmlpqp20p219gsm7zbnnii6lik6hc2vvfsdmnygf271l";
16+
fetchSubmodules = true;
17+
};
18+
19+
cargoSha256 = "0zzksgi2prgw01m6r4bqjjz902h5g5ich0h3xvb60w4sshlss891";
20+
21+
# nativeBuildInputs = [ python cmake clang ];
22+
# buildInputs = [ llvmPackages.libclang ] ++
23+
# lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security ];
24+
# LIBCLANG_PATH = "${llvmPackages.libclang}/lib";
25+
26+
doCheck = false;
27+
# error: couldn't lock thumbv6m-panic_abort-eabi's sysroot as read-only
28+
USER = "nobody"; # for xargo tests (if we would run them)
29+
30+
meta = with lib; {
31+
description = "The sysroot manager that lets you build and customize std";
32+
homepage = "https://github.com/japaric/xargo";
33+
license = licenses.mit;
34+
maintainers = [ {
35+
email = "[email protected]";
36+
github = "osa1";
37+
githubId = 123123;
38+
name = "Ömer Sinan Ağacan";
39+
} ];
40+
platforms = platforms.unix;
41+
};
42+
}

rts/Makefile

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,20 @@ _build/native/tommath_%.o: %.c rts.h buf.h | _build/native
156156
_build/wasm/musl_%.o: %.c | _build/wasm
157157
$(WASM_CLANG) $(CLANG_FLAGS) $(WASM_FLAGS) $(MUSL_FLAGS) $< --output $@
158158

159-
.PHONY: _build/wasm/motoko_rts.o
160-
_build/wasm/motoko_rts.o: | _build/wasm
161-
cd motoko-rts && \
162-
cargo rustc --target=wasm32-unknown-emscripten --release -v -- -Crelocation-model=pic --emit=obj
163-
cp motoko-rts/target/wasm32-unknown-emscripten/release/deps/motoko_rts*.o $@
164-
165-
.PHONY: _build/native/motoko_rts.o
166-
_build/native/motoko_rts.o: | _build/native
167-
cd motoko-rts && \
168-
cargo rustc --release -v -- -Crelocation-model=pic --emit=obj
169-
cp motoko-rts/target/release/deps/motoko_rts*.o $@
159+
.PHONY: _build/wasm/libmotoko_rts.a
160+
_build/wasm/libmotoko_rts.a: | _build/wasm
161+
cd motoko-rts && cargo build --target=wasm32-unknown-emscripten --release
162+
cp motoko-rts/target/wasm32-unknown-emscripten/release/libmotoko_rts.a $@
163+
164+
.PHONY: _build/native/libmotoko_rts.a
165+
_build/native/libmotoko_rts.a: | _build/native
166+
cd motoko-rts && cargo build --release
167+
cp motoko-rts/target/release/libmotoko_rts.a $@
170168

171169
RTS_WASM_O=$(RTSFILES:%=_build/wasm/%.o)
172170
RTS_NATIVE_O=$(RTSFILES:%=_build/native/%.o)
173-
RTS_RUST_WASM_O=_build/wasm/motoko_rts.o
174-
RTS_RUST_NATIVE_O=_build/native/motoko_rts.o
171+
RTS_RUST_WASM_O=_build/wasm/libmotoko_rts.a
172+
RTS_RUST_NATIVE_O=_build/native/libmotoko_rts.a
175173

176174
#
177175
# The actual RTS, as we ship it with the compiler
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// https://github.com/rust-analyzer/rust-analyzer/blob/master/editors/code/package.json
12
{
2-
"rust-analyzer.cargo.target": "wasm32-unknown-emscripten"
3+
"rust-analyzer.cargo.target": "wasm32-unknown-emscripten",
4+
5+
// This is required as `cargo check --all-targets` doesn't seem to work well
6+
// on no-std crates, it generates false "duplicate lang item" errors.
7+
"rust-analyzer.checkOnSave.allTargets": false
38
}

rts/motoko-rts/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ version = "0.1.0"
44
authors = ["Ömer Sinan Ağacan <[email protected]>"]
55
edition = "2018"
66

7-
[dependencies]
7+
[lib]
8+
crate-type = ["staticlib"]
9+
10+
[profile.dev]
11+
panic = "abort"
12+
13+
[profile.release]
14+
panic = "abort"

rts/motoko-rts/src/common.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
11
extern "C" {
22
pub fn rts_trap_with(msg: *const u8) -> !;
33
}
4+
5+
#[cfg(target_arch = "wasm32")]
6+
mod ic0 {
7+
// NB: The #[link(...)] line below really needs to be before `extern "C"` part, we can't move
8+
// it inside the extern block, it doesn't work.
9+
#[link(wasm_import_module = "ic0")]
10+
extern "C" {
11+
#[no_mangle]
12+
pub fn debug_print(msg: *const u8, len: u32);
13+
}
14+
}
15+
16+
#[cfg(target_arch = "wasm32")]
17+
pub unsafe fn debug_print(s: &str) {
18+
ic0::debug_print(s.as_ptr(), s.len() as u32)
19+
}
20+
21+
pub struct Wrapper<'a> {
22+
buf: &'a mut [u8],
23+
offset: usize,
24+
}
25+
26+
impl<'a> Wrapper<'a> {
27+
pub fn new(buf: &'a mut [u8]) -> Self {
28+
Wrapper {
29+
buf: buf,
30+
offset: 0,
31+
}
32+
}
33+
}
34+
35+
use core::fmt;
36+
37+
impl<'a> fmt::Write for Wrapper<'a> {
38+
fn write_str(&mut self, s: &str) -> fmt::Result {
39+
let bytes = s.as_bytes();
40+
41+
// Skip over already-copied data
42+
let remainder = &mut self.buf[self.offset..];
43+
// Check if there is space remaining (return error instead of panicking)
44+
if remainder.len() < bytes.len() {
45+
return Err(core::fmt::Error);
46+
}
47+
// Make the two slices the same length
48+
let remainder = &mut remainder[..bytes.len()];
49+
// Copy
50+
remainder.copy_from_slice(bytes);
51+
52+
// Update offset to avoid overwriting
53+
self.offset += bytes.len();
54+
55+
Ok(())
56+
}
57+
}

rts/motoko-rts/src/gc.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
// TODO: inconsistent use of size vs. len
77

88
use core::arch::wasm32;
9+
use core::fmt::Write;
910

1011
use crate::array::array_idx_unchecked;
11-
use crate::common::rts_trap_with;
12+
use crate::common::{debug_print, rts_trap_with, Wrapper};
1213
use crate::types::*;
1314

1415
extern "C" {
@@ -413,6 +414,8 @@ unsafe fn evac_static_roots(
413414

414415
#[no_mangle]
415416
pub unsafe extern "C" fn rust_collect_garbage() {
417+
debug_print("### GC begins");
418+
416419
let static_roots = get_static_roots();
417420

418421
// Beginning of tospace = end of fromspace
@@ -421,8 +424,21 @@ pub unsafe extern "C" fn rust_collect_garbage() {
421424
let begin_to_space = end_from_space;
422425
let mut end_to_space = begin_to_space;
423426

427+
debug_print("### Evacuating roots");
428+
429+
let mut buf = [0 as u8; 100];
430+
let _ = write!(
431+
Wrapper::new(&mut buf),
432+
"### begin_from_space={}",
433+
begin_from_space.unskew()
434+
);
435+
436+
424437
// Evacuate roots
425438
end_to_space = evac_static_roots(begin_from_space, begin_to_space, end_to_space, static_roots);
439+
440+
debug_print("### Evacuated static roots");
441+
426442
end_to_space = evacuate(
427443
begin_from_space.unskew(),
428444
begin_to_space.unskew(),
@@ -458,4 +474,6 @@ pub unsafe extern "C" fn rust_collect_garbage() {
458474
set_hp(skew(
459475
begin_from_space.unskew() + (end_to_space.unskew() - begin_to_space.unskew()),
460476
));
477+
478+
debug_print("### GC finished");
461479
}

rts/motoko-rts/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ pub mod types;
99

1010
#[cfg(target_arch = "wasm32")]
1111
pub mod gc;
12+
13+
#[cfg(target_arch = "wasm32")]
14+
#[panic_handler]
15+
fn panic(_info: &core::panic::PanicInfo) -> ! {
16+
unsafe { ::core::arch::wasm32::unreachable() }
17+
}
18+
19+
#[cfg(not(target_arch = "wasm32"))]
20+
#[panic_handler]
21+
fn panic(_info: &core::panic::PanicInfo) -> ! {
22+
loop {}
23+
}

0 commit comments

Comments
 (0)