Merge #205

205: Add a heap feature to libtock-rs/core. r=gendx a=gendx

This pull request adds a `heap` feature to the core crate of libtock-rs. This allows to configure the core crate to only enable and initialize the `HEAP` variable, while leaving the choice of defining a custom allocator.

Indeed, the `HEAP` is conveniently initialized in the entry point with the app's heap start/size.

An example use case is in OpenSK (see this pull request https://github.com/google/OpenSK/pull/123), where the custom allocator can print some debugging information about allocations & deallocation to the console - while the heap implementation is the same as the default of libtock-rs (i.e. `linked_list_allocator`).

---

An alternative to this feature could be to somehow communicate the app's heap start/size to the `main` function, so that each app can initialize its own allocator when the `alloc` feature is off.

Co-authored-by: Guillaume Endignoux <guillaumee@google.com>
diff --git a/.github/workflows/size-diff.yml b/.github/workflows/size-diff.yml
index d35fde0..6f8c7d0 100644
--- a/.github/workflows/size-diff.yml
+++ b/.github/workflows/size-diff.yml
@@ -40,12 +40,13 @@
           cd "${GITHUB_WORKSPACE}"
           rustup target add riscv32imc-unknown-none-elf thumbv7em-none-eabi
           make -j2 examples  # The VM this runs on has 2 logical cores.
-          cargo run --release -p print-sizes >'${{runner.temp}}/merge-sizes'
+          cargo run --release -p print_sizes >'${{runner.temp}}/merge-sizes'
           git remote set-branches "${UPSTREAM_REMOTE_NAME}" "${GITHUB_BASE_REF}"
           git fetch --depth=1 "${UPSTREAM_REMOTE_NAME}" "${GITHUB_BASE_REF}"
           git checkout "${UPSTREAM_REMOTE_NAME}/${GITHUB_BASE_REF}"
+          rustup target add riscv32imc-unknown-none-elf thumbv7em-none-eabi
           make -j2 examples
-          cargo run --release -p print-sizes >'${{runner.temp}}/base-sizes'
+          cargo run --release -p print_sizes >'${{runner.temp}}/base-sizes'
 
       # Computes and displays the size diff. diff returns a nonzero status code
       # if the files differ, and GitHub interprets a nonzero status code as an
diff --git a/Cargo.toml b/Cargo.toml
index b8e0bec..f5cb932 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,13 +6,13 @@
 edition = "2018"
 
 [features]
-alloc = ["libtock-core/alloc"]
-custom_panic_handler = ["libtock-core/custom_panic_handler"]
-custom_alloc_error_handler = ["libtock-core/custom_alloc_error_handler"]
+alloc = ["libtock_core/alloc"]
+custom_panic_handler = ["libtock_core/custom_panic_handler"]
+custom_alloc_error_handler = ["libtock_core/custom_alloc_error_handler"]
 __internal_disable_gpio_in_integration_test = []
 
 [dependencies]
-libtock-core = { path = "core" }
+libtock_core = { path = "core" }
 libtock_codegen = { path = "codegen" }
 futures = { version = "0.3.1", default-features = false, features = ["unstable", "cfg-target-has-atomic"] }
 
@@ -62,6 +62,6 @@
 members = [
     "codegen",
     "core",
-    "test-runner",
-    "tools/print-sizes",
+    "test_runner",
+    "tools/print_sizes",
 ]
diff --git a/Makefile b/Makefile
index 36921bb..87b6a10 100644
--- a/Makefile
+++ b/Makefile
@@ -59,23 +59,23 @@
 # Prints out the sizes of the example binaries.
 .PHONY: print-sizes
 print-sizes: examples
-	cargo run --release -p print-sizes
+	cargo run --release -p print_sizes
 
 # Runs the libtock_test tests in QEMU on a simulated HiFive board.
 .PHONY: test-qemu-hifive
 test-qemu-hifive: kernel-hifive setup-qemu
 	PLATFORM=hifive1 cargo rrv32imac --example libtock_test --features=alloc \
 		--features=__internal_disable_gpio_in_integration_test
-	cargo run -p test-runner
+	cargo run -p test_runner
 
 .PHONY: examples
 examples:
-	PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples -p libtock -p libtock-core
+	PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples -p libtock -p libtock_core
 	PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --examples --features=alloc
 	PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --example panic --features=custom_panic_handler,custom_alloc_error_handler
 	PLATFORM=nrf52 cargo build --release --target=thumbv7em-none-eabi --example alloc_error --features=alloc,custom_alloc_error_handler
 	# Important: This tests a platform without atomic instructions.
-	PLATFORM=opentitan cargo build --release --target=riscv32imc-unknown-none-elf --examples -p libtock -p libtock-core
+	PLATFORM=opentitan cargo build --release --target=riscv32imc-unknown-none-elf --examples -p libtock -p libtock_core
 
 .PHONY: test
 test: examples test-qemu-hifive
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 636755c..bfa9570 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,5 +1,5 @@
 [package]
-name = "libtock-core"
+name = "libtock_core"
 version = "0.1.0"
 authors = ["Tock Project Developers <tock-dev@googlegroups.com>"]
 edition = "2018"
diff --git a/core/README.md b/core/README.md
index ba9602a..839c9ed 100644
--- a/core/README.md
+++ b/core/README.md
@@ -1,4 +1,4 @@
-# libtock-core
+# libtock\_core
 
 Core crate of `libtock-rs`. It contains the architecture specific code of `libtock-rs`. In particular:
 
diff --git a/core/examples/empty_main.rs b/core/examples/empty_main.rs
index dd062bc..1179b43 100644
--- a/core/examples/empty_main.rs
+++ b/core/examples/empty_main.rs
@@ -1,13 +1,13 @@
-// The most minimal libtock-core example possible. This file primarily exists
+// The most minimal libtock_core example possible. This file primarily exists
 // for code size measurement, as this should create the smallest-possible
-// libtock-core app.
+// libtock_core app.
 
 #![no_std]
 
-// If you don't *use* anything from libtock-core directly, cargo will not link
+// If you don't *use* anything from libtock_core directly, cargo will not link
 // it into the executable. However, we still need the runtime and lang items.
-// Therefore a libtock-core app that doesn't directly mention anything in
-// libtock-core needs to explicitly declare its dependency on libtock-core as
+// Therefore a libtock_core app that doesn't directly mention anything in
+// libtock_core needs to explicitly declare its dependency on libtock_core as
 // follows.
 extern crate libtock_core;
 
diff --git a/core/src/debug/platform_arm.rs b/core/src/debug/platform_arm.rs
index 46211b3..a777334 100644
--- a/core/src/debug/platform_arm.rs
+++ b/core/src/debug/platform_arm.rs
@@ -1,5 +1,5 @@
 pub fn get_stack_pointer() -> usize {
     let stack_pointer;
-    unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };
+    unsafe { llvm_asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };
     stack_pointer
 }
diff --git a/core/src/debug/platform_riscv32.rs b/core/src/debug/platform_riscv32.rs
index 7f4a4e0..c276442 100644
--- a/core/src/debug/platform_riscv32.rs
+++ b/core/src/debug/platform_riscv32.rs
@@ -1,5 +1,5 @@
 pub fn get_stack_pointer() -> usize {
     let stack_pointer;
-    unsafe { asm!("mv $0, sp" : "=r"(stack_pointer) : : : "volatile") };
+    unsafe { llvm_asm!("mv $0, sp" : "=r"(stack_pointer) : : : "volatile") };
     stack_pointer
 }
diff --git a/core/src/entry_point/start_item_arm.rs b/core/src/entry_point/start_item_arm.rs
index c9390f6..894088e 100644
--- a/core/src/entry_point/start_item_arm.rs
+++ b/core/src/entry_point/start_item_arm.rs
@@ -12,7 +12,7 @@
     _memory_len: usize,
     app_heap_break: usize,
 ) -> ! {
-    asm!("
+    llvm_asm!("
         // Because ROPI-RWPI support in LLVM/rustc is incomplete, Rust
         // applications must be statically linked. An offset between the
         // location the program is linked at and its actual location in flash
@@ -23,7 +23,7 @@
         // the Program Counter) will match the intended location of .start. We
         // don't have an easy way to signal an error, so for now we just yield
         // if the location is wrong.
-        sub r4, pc, #4    // r4 = pc
+        subw r4, pc, #4    // r4 = pc
         ldr r5, =.start   // r5 = address of .start
         cmp r4, r5
         beq .Lstack_init  // Jump to stack initialization if pc was correct
diff --git a/core/src/entry_point/start_item_riscv32.rs b/core/src/entry_point/start_item_riscv32.rs
index 347400c..f8a0e19 100644
--- a/core/src/entry_point/start_item_riscv32.rs
+++ b/core/src/entry_point/start_item_riscv32.rs
@@ -14,7 +14,7 @@
 // Due to Rust issue: https://github.com/rust-lang/rust/issues/42779 we can't have
 // args to the function
 pub unsafe extern "C" fn _start() -> ! {
-    asm!(
+    llvm_asm!(
     // Compute the stack top.
     //
     // struct hdr* myhdr = (struct hdr*) app_start;
@@ -105,7 +105,7 @@
 #[export_name = "abort"]
 pub extern "C" fn abort() {
     unsafe {
-        asm! ("
+        llvm_asm! ("
             // Simply go back to the start as if we had just booted.
             j    _start
         "
diff --git a/core/src/lib.rs b/core/src/lib.rs
index ce77176..3bdfe5b 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -1,4 +1,4 @@
-#![feature(asm, lang_items, naked_functions)]
+#![feature(lang_items, llvm_asm, naked_functions)]
 #![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)]
 #![cfg_attr(feature = "alloc", feature(alloc_error_handler))]
 
diff --git a/core/src/syscalls/platform_arm.rs b/core/src/syscalls/platform_arm.rs
index 4ff146a..066459e 100644
--- a/core/src/syscalls/platform_arm.rs
+++ b/core/src/syscalls/platform_arm.rs
@@ -22,7 +22,7 @@
     // registers r4-r8, r10, r11 and SP (and r9 in PCS variants that designate
     // r9 as v6) As our compilation flags mark r9 as the PIC base register, it
     // does not need to be saved. Thus we must clobber r0-3, r12, and LR
-    asm!(
+    llvm_asm!(
             "svc 0"
             :
             :
@@ -40,7 +40,7 @@
     ud: usize,
 ) -> isize {
     let res;
-    asm!("svc 1" : "={r0}"(res)
+    llvm_asm!("svc 1" : "={r0}"(res)
                  : "{r0}"(major) "{r1}"(minor) "{r2}"(cb) "{r3}"(ud)
                  : "memory"
                  : "volatile");
@@ -52,7 +52,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn command(major: usize, minor: usize, arg1: usize, arg2: usize) -> isize {
     let res;
-    asm!("svc 2" : "={r0}"(res)
+    llvm_asm!("svc 2" : "={r0}"(res)
                      : "{r0}"(major) "{r1}"(minor) "{r2}"(arg1) "{r3}"(arg2)
                      : "memory"
                      : "volatile");
@@ -64,7 +64,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn command1(major: usize, minor: usize, arg: usize) -> isize {
     let res;
-    asm!("svc 2" : "={r0}"(res)
+    llvm_asm!("svc 2" : "={r0}"(res)
                  : "{r0}"(major) "{r1}"(minor) "{r2}"(arg)
                  : "memory"
                  : "volatile");
@@ -76,7 +76,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn allow(major: usize, minor: usize, slice: *mut u8, len: usize) -> isize {
     let res;
-    asm!("svc 3" : "={r0}"(res)
+    llvm_asm!("svc 3" : "={r0}"(res)
                  : "{r0}"(major) "{r1}"(minor) "{r2}"(slice) "{r3}"(len)
                  : "memory"
                  : "volatile");
@@ -88,7 +88,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn memop(major: u32, arg1: usize) -> isize {
     let res;
-    asm!("svc 4" : "={r0}"(res)
+    llvm_asm!("svc 4" : "={r0}"(res)
                  : "{r0}"(major) "{r1}"(arg1)
                  : "memory"
                  : "volatile");
diff --git a/core/src/syscalls/platform_riscv32.rs b/core/src/syscalls/platform_riscv32.rs
index f7d1858..f2c49fe 100644
--- a/core/src/syscalls/platform_riscv32.rs
+++ b/core/src/syscalls/platform_riscv32.rs
@@ -3,7 +3,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn yieldk() {
     /* TODO: Stop yielding */
-    asm! (
+    llvm_asm! (
             "li    a0, 0
             ecall"
             :
@@ -23,7 +23,7 @@
     ud: usize,
 ) -> isize {
     let res;
-    asm!("li    a0, 1
+    llvm_asm!("li    a0, 1
           ecall"
          : "={x10}" (res)
          : "{x11}" (major), "{x12}" (minor), "{x13}" (cb), "{x14}" (ud)
@@ -37,7 +37,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn command(major: usize, minor: usize, arg1: usize, arg2: usize) -> isize {
     let res;
-    asm!("li    a0, 2
+    llvm_asm!("li    a0, 2
           ecall"
          : "={x10}" (res)
          : "{x11}" (major), "{x12}" (minor), "{x13}" (arg1), "{x14}" (arg2)
@@ -51,7 +51,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn command1(major: usize, minor: usize, arg: usize) -> isize {
     let res;
-    asm!("li    a0, 2
+    llvm_asm!("li    a0, 2
           ecall"
          : "={x10}" (res)
          : "{x11}" (major), "{x12}" (minor), "{x13}" (arg)
@@ -65,7 +65,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn allow(major: usize, minor: usize, slice: *mut u8, len: usize) -> isize {
     let res;
-    asm!("li    a0, 3
+    llvm_asm!("li    a0, 3
           ecall"
          : "={x10}" (res)
          : "{x11}" (major), "{x12}" (minor), "{x13}" (slice), "{x14}" (len)
@@ -79,7 +79,7 @@
 #[allow(clippy::missing_safety_doc)]
 pub unsafe fn memop(major: u32, arg1: usize) -> isize {
     let res;
-    asm!("li    a0, 4
+    llvm_asm!("li    a0, 4
           ecall"
          : "={x10}" (res)
          : "{x11}" (major), "{x12}" (arg1)
diff --git a/doc/Dependencies.md b/doc/Dependencies.md
index f157049..77da194 100644
--- a/doc/Dependencies.md
+++ b/doc/Dependencies.md
@@ -25,5 +25,5 @@
 
 ## Avoiding Optional Dependencies
 
-To avoid pulling in optional dependencies, users should use `libtock-core`
-instead of `libtock`. `libtock-core` is in the `core/` directory.
+To avoid pulling in optional dependencies, users should use `libtock_core`
+instead of `libtock`. `libtock_core` is in the `core/` directory.
diff --git a/rust-toolchain b/rust-toolchain
index 8bd4ff0..4aec5c6 100644
--- a/rust-toolchain
+++ b/rust-toolchain
@@ -1 +1 @@
-nightly-2020-04-06
+nightly-2020-06-10
diff --git a/src/hmac.rs b/src/hmac.rs
index e5475cc..5185128 100644
--- a/src/hmac.rs
+++ b/src/hmac.rs
@@ -99,7 +99,7 @@
         syscalls::allow(DRIVER_NUMBER, allow_nr::DEST, &mut buffer.buffer).map_err(Into::into)
     }
 
-    pub fn subscribe<CB: FnMut(usize, usize) -> () + FnMut(usize, usize)>(
+    pub fn subscribe<CB: FnMut(usize, usize)>(
         &self,
         callback: &'a mut CB,
     ) -> TockResult<CallbackSubscription> {
diff --git a/test-runner/Cargo.toml b/test_runner/Cargo.toml
similarity index 95%
rename from test-runner/Cargo.toml
rename to test_runner/Cargo.toml
index b9769b9..133ff23 100644
--- a/test-runner/Cargo.toml
+++ b/test_runner/Cargo.toml
@@ -1,5 +1,5 @@
 [package]
-name = "test-runner"
+name = "test_runner"
 version = "0.1.0"
 authors = ["torfmaster <briefe@kebes.de>"]
 edition = "2018"
diff --git a/test-runner/src/main.rs b/test_runner/src/main.rs
similarity index 100%
rename from test-runner/src/main.rs
rename to test_runner/src/main.rs
diff --git a/tools/print-sizes/Cargo.toml b/tools/print_sizes/Cargo.toml
similarity index 61%
rename from tools/print-sizes/Cargo.toml
rename to tools/print_sizes/Cargo.toml
index 97bdc48..12f76de 100644
--- a/tools/print-sizes/Cargo.toml
+++ b/tools/print_sizes/Cargo.toml
@@ -1,12 +1,12 @@
-# Finds all the libtock-core and libtock-rs examples and prints the sizes of
+# Finds all the libtock_core and libtock examples and prints the sizes of
 # several of their sections. Searches the target/$ARCH/release directory. Note
-# that print-sizes will not build the examples; that is done by the
+# that print_sizes will not build the examples; that is done by the
 # `print-sizes` Makefile action.
 
 [package]
 authors = ["Tock Project Developers <tock-dev@googlegroups.com>"]
 edition = "2018"
-name = "print-sizes"
+name = "print_sizes"
 version = "0.1.0"
 
 [dependencies]
diff --git a/tools/print-sizes/src/main.rs b/tools/print_sizes/src/main.rs
similarity index 100%
rename from tools/print-sizes/src/main.rs
rename to tools/print_sizes/src/main.rs