Repository: nervosnetwork/ckb-std (language: rust)

Issue:
Stable rust
Now that the default alloc error handler is stabilized, it's possible to build no_std + alloc programs with stable rust.

A drawback is that the default alloc error handler pulls in fmt, so it might increase binary size.


Discussion / hints:
https://github.com/XuJiandong/ckb-std/commit/0eb43ed1c596d3c7cd8d0dd73226ea01a43773c7

https://github.com/XuJiandong/ckb-stable-rust-demo/tree/master
@XuJiandong do you want to submit a PR for this?
I'm not sure. Because we need to update capsule and other toolchain after that.

I can update capsule template.
Capsule develop already supports changing rust-toolchain in the `rust-toolchain` file.
I'll submit a PR for this.

Fix patch:
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 8e5d68f..6d77f4e 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -13,13 +13,13 @@ jobs:
       run: git submodule update --init --recursive
     - name: Install Rust target
       run: rustup target add riscv64imac-unknown-none-elf
+    - name: Install cross
+      run: cargo install cross --git https://github.com/cross-rs/cross --rev=6982b6c
     - name: Build without riscv C compiler
       run: cargo build --target=riscv64imac-unknown-none-elf --no-default-features --features=ckb-types,allocator
-    - name: Install tools
-      run: make install-tools
-    - name: Run integration
-      run: make integration-in-docker
+    - name: Check example
+      run: cross check --examples
     - name: Install capsule
-      run: cargo install ckb-capsule --version 0.8.0
+      run: cargo install ckb-capsule --git https://github.com/nervosnetwork/capsule --rev ccd6a7c39f0e
     - name: Run tests
       run: make test
diff --git a/Cross.toml b/Cross.toml
new file mode 100644
index 0000000..278f31b
--- /dev/null
+++ b/Cross.toml
@@ -0,0 +1,5 @@
+[build]
+default-target = "riscv64imac-unknown-none-elf"
+
+[target.riscv64imac-unknown-none-elf]
+image = "nervos/ckb-riscv-gnu-toolchain:focal-20230214"
diff --git a/capsule.toml b/capsule.toml
index eddbc25..62142c5 100644
--- a/capsule.toml
+++ b/capsule.toml
@@ -1,11 +1,6 @@
-
-# # path of rust contracts workspace directory,
-# # a `Cargo.toml` file is expected under the directory.
-toolchain = "nightly-2022-08-01"
-docker_image = "thewawar/ckb-capsule:2022-08-01"
-
+# We are actually using unreleased features in capsule. Use 0.10 after it's released.
 # capsule version
-version = "0.8.0"
+version = "0.9.1"
 # path of deployment config file
 deployment = "deployment.toml"
 
diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock
index 99e795d..6b68be2 100644
--- a/contracts/Cargo.lock
+++ b/contracts/Cargo.lock
@@ -4,9 +4,9 @@ version = 3
 
 [[package]]
 name = "blake2b-ref"
-version = "0.3.0"
+version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "88756126a7634ad9c13ed0a6da9aa3376a6b7000eb5fcdbb89d153196326bbcd"
+checksum = "294d17c72e0ba59fad763caa112368d0672083779cdebbb97164f4bb4c1e339a"
 
 [[package]]
 name = "buddy-alloc"
diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml
index dfd77da..edbf583 100644
--- a/contracts/Cargo.toml
+++ b/contracts/Cargo.toml
@@ -6,5 +6,4 @@ overflow-checks = true
 opt-level = 's'
 lto = true
 codegen-units = 1
-panic = 'abort'
 
diff --git a/contracts/Cross.toml b/contracts/Cross.toml
new file mode 100644
index 0000000..278f31b
--- /dev/null
+++ b/contracts/Cross.toml
@@ -0,0 +1,5 @@
+[build]
+default-target = "riscv64imac-unknown-none-elf"
+
+[target.riscv64imac-unknown-none-elf]
+image = "nervos/ckb-riscv-gnu-toolchain:focal-20230214"
diff --git a/contracts/exec-callee/src/main.rs b/contracts/exec-callee/src/main.rs
index 9dd966a..f47235b 100644
--- a/contracts/exec-callee/src/main.rs
+++ b/contracts/exec-callee/src/main.rs
@@ -6,9 +6,6 @@
 
 #![no_std]
 #![no_main]
-#![feature(lang_items)]
-#![feature(alloc_error_handler)]
-#![feature(panic_info_message)]
 
 // define modules
 mod entry;
diff --git a/contracts/exec-caller-by-code-hash/src/main.rs b/contracts/exec-caller-by-code-hash/src/main.rs
index 9dd966a..f47235b 100644
--- a/contracts/exec-caller-by-code-hash/src/main.rs
+++ b/contracts/exec-caller-by-code-hash/src/main.rs
@@ -6,9 +6,6 @@
 
 #![no_std]
 #![no_main]
-#![feature(lang_items)]
-#![feature(alloc_error_handler)]
-#![feature(panic_info_message)]
 
 // define modules
 mod entry;
diff --git a/contracts/exec-caller/src/main.rs b/contracts/exec-caller/src/main.rs
index 9dd966a..f47235b 100644
--- a/contracts/exec-caller/src/main.rs
+++ b/contracts/exec-caller/src/main.rs
@@ -6,9 +6,6 @@
 
 #![no_std]
 #![no_main]
-#![feature(lang_items)]
-#![feature(alloc_error_handler)]
-#![feature(panic_info_message)]
 
 // define modules
 mod entry;
diff --git a/contracts/rust-toolchain b/contracts/rust-toolchain
new file mode 100644
index 0000000..2bf5ad0
--- /dev/null
+++ b/contracts/rust-toolchain
@@ -0,0 +1,1 @@
+stable
diff --git a/examples/main.rs b/examples/main.rs
index 9756234..ce6b818 100644
--- a/examples/main.rs
+++ b/examples/main.rs
@@ -1,8 +1,5 @@
 #![no_std]
 #![no_main]
-#![feature(lang_items)]
-#![feature(alloc_error_handler)]
-#![feature(panic_info_message)]
 
 use alloc::vec;
 use ckb_std::{debug, default_alloc, entry};
diff --git a/src/entry.rs b/src/entry.rs
index 16a241a..e7e837b 100644
--- a/src/entry.rs
+++ b/src/entry.rs
@@ -1,4 +1,4 @@
-/// Define program entry point (`_start` function) and lang items (panic handler, oom handler, etc.).
+/// Define program entry point (`_start` function) and lang items (panic handler, etc.).
 ///
 /// # Examples
 ///
@@ -16,11 +16,6 @@ macro_rules! entry {
     ($main:path) => {
         extern crate alloc;
 
-        #[alloc_error_handler]
-        fn oom_handler(_layout: alloc::alloc::Layout) -> ! {
-            panic!("Out of memory")
-        }
-
         #[cfg(not(target_arch = "riscv64"))]
         #[no_mangle]
         pub extern "C" fn _start() -> ! {
@@ -55,42 +50,9 @@ macro_rules! entry {
             "ecall",
         );
 
-        #[lang = "eh_personality"]
-        extern "C" fn eh_personality() {}
-
-        /// Fix symbol missing
-        #[no_mangle]
-        pub extern "C" fn abort() {
-            panic!("abort!");
-        }
-
         #[panic_handler]
         fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
-            #[cfg(debug_assertions)]
-            {
-                use alloc::format;
-
-                let mut s = alloc::string::String::new();
-                if let Some(p) = panic_info.payload().downcast_ref::<&str>() {
-                    s.push_str(&format!("panic occurred: {:?}", p));
-                } else {
-                    s.push_str(&format!("panic occurred:"));
-                }
-                if let Some(m) = panic_info.message() {
-                    s.push_str(&format!(" {:?}", m));
-                }
-                if let Some(location) = panic_info.location() {
-                    s.push_str(&format!(
-                        ", in file {}:{}",
-                        location.file(),
-                        location.line()
-                    ));
-                } else {
-                    s.push_str(&format!(", but can't get location information..."));
-                }
-
-                $crate::syscalls::debug(s);
-            }
+            $crate::debug!("{}", panic_info);
             $crate::syscalls::exit(-1)
         }
     };


Test patch:
diff --git a/contracts/ckb-std-tests/src/main.rs b/contracts/ckb-std-tests/src/main.rs
index 1ac10d5..e55679f 100644
--- a/contracts/ckb-std-tests/src/main.rs
+++ b/contracts/ckb-std-tests/src/main.rs
@@ -6,9 +6,6 @@
 
 #![no_std]
 #![no_main]
-#![feature(lang_items)]
-#![feature(alloc_error_handler)]
-#![feature(panic_info_message)]
 
 // define modules
 mod code_hashes;