Skip to content

Commit 63b1a9f

Browse files
Fixes for Rust 1.81.0 (#1333)
### What Fixes for Rust 1.81: 1. Update semver-checks to 0.35.0. 2. Don't use the `extern` function for invoking a contract function when executing tests. ### Why 1. Semver checks is failing to run on the new version of Rust and needs updating. 2. Rust 1.81.0 changed the behavior of panics on `extern "C"` functions, such that panics cannot be caught by [std::panic::catch_unwind](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html). Contracts expose their functions as an `extern` function that defaults to `extern "C"`. The test environment catches panics inside contract functions and resurfaces them as errors by using catch_unwind. The solution used was to make it so that the test environment never calls through the extern function, and instead calls through a non-extern function that does the same work. This seems like the sanest way to take away any dependence / relationship between the test environment and the Rust C-ABI so that any future changes to the C-ABI don't affect the test environment. An alternative solution would be to mark the contract function as `extern "C-unwind"` so that it retained unwind functionality, but that would continue to bind the SDK test environment to behaviour changes in extern fns. In the solution in this change the Abi qualifier `"C"` has been added. When the Abi qualifier is not specified it defaults to `"C"`, but for the sake of being explicit and removing any doubt from a reader it is now specified. The Rust reference confirms that `"C"` is the default: - https://doc.rust-lang.org/reference/items/functions.html#extern-function-qualifier. More details on this change to Rust 1.81.0 can be found at: - https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#abort-on-uncaught-panics-in-extern-c-functions - rust-lang/rust#74990 Close #1332
1 parent f57d245 commit 63b1a9f

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ jobs:
5757
steps:
5858
- uses: actions/checkout@v3
5959
- run: rustup update
60-
- uses: stellar/binaries@v24
60+
- uses: stellar/binaries@v30
6161
with:
6262
name: cargo-semver-checks
63-
version: 0.32.0
63+
version: 0.35.0
6464
- run: cargo semver-checks
6565

6666
build-and-test:

soroban-sdk-macros/src/derive_fn.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn derive_pub_fn(
5050
});
5151

5252
// Prepare the argument inputs.
53-
let (wrap_args, wrap_calls): (Vec<_>, Vec<_>) = inputs
53+
let (wrap_args, passthrough_calls, wrap_calls): (Vec<_>, Vec<_>, Vec<_>) = inputs
5454
.iter()
5555
.skip(if env_input.is_some() { 1 } else { 0 })
5656
.enumerate()
@@ -78,6 +78,7 @@ pub fn derive_pub_fn(
7878
colon_token: Colon::default(),
7979
ty: Box::new(Type::Verbatim(quote! { #crate_path::Val })),
8080
});
81+
let passthrough_call = quote! { #ident };
8182
let call = quote! {
8283
<_ as #crate_path::unwrap::UnwrapOptimized>::unwrap_optimized(
8384
<_ as #crate_path::TryFromValForContractFn<#crate_path::Env, #crate_path::Val>>::try_from_val_for_contract_fn(
@@ -86,11 +87,11 @@ pub fn derive_pub_fn(
8687
)
8788
)
8889
};
89-
(arg, call)
90+
(arg, passthrough_call, call)
9091
}
9192
FnArg::Receiver(_) => {
9293
errors.push(Error::new(a.span(), "self argument not supported"));
93-
(a.clone(), quote! {})
94+
(a.clone(), quote! {}, quote! {})
9495
}
9596
})
9697
.multiunzip();
@@ -132,8 +133,7 @@ pub fn derive_pub_fn(
132133
use super::*;
133134

134135
#[deprecated(note = #deprecated_note)]
135-
#[cfg_attr(target_family = "wasm", export_name = #wrap_export_name)]
136-
pub extern fn invoke_raw(env: #crate_path::Env, #(#wrap_args),*) -> #crate_path::Val {
136+
pub fn invoke_raw(env: #crate_path::Env, #(#wrap_args),*) -> #crate_path::Val {
137137
#use_trait;
138138
<_ as #crate_path::IntoVal<#crate_path::Env, #crate_path::Val>>::into_val(
139139
#[allow(deprecated)]
@@ -154,6 +154,13 @@ pub fn derive_pub_fn(
154154
invoke_raw(env, #(#slice_args),*)
155155
}
156156

157+
#[deprecated(note = #deprecated_note)]
158+
#[cfg_attr(target_family = "wasm", export_name = #wrap_export_name)]
159+
pub extern "C" fn invoke_raw_extern(env: #crate_path::Env, #(#wrap_args),*) -> #crate_path::Val {
160+
#[allow(deprecated)]
161+
invoke_raw(env, #(#passthrough_calls),*)
162+
}
163+
157164
use super::*;
158165
}
159166
})

0 commit comments

Comments
 (0)