Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macros): unexpected input handler produces wrong output #257

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-Clink-arg=/force:unresolved", "-Ctarget-feature=+crt-static"]
50 changes: 47 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
anyhow = "1"
const-format = { package = "const_format", version = "0.2" }
convert-case = { package = "convert_case", version = "0.6" }
fluent-asserter = "0.1"
futures = { version = "0.3", default-features = false }
gear-core-errors = "1.4.1"
git-download = "0.1"
Expand Down
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sails-macros-core = { path = "./core" }
proc-macro-error.workspace = true

[dev-dependencies]
fluent-asserter.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-rtl.workspace = true
scale-info = { workspace = true, features = ["derive"] }
Expand Down
6 changes: 4 additions & 2 deletions macros/core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
let scale_codec_path = sails_paths::scale_codec_path();
let scale_info_path = sails_paths::scale_info_path();

let unexpected_route_panic =
shared::generate_unexpected_input_panic(&input_ident, "Unknown request");

quote!(
#service_impl

Expand All @@ -142,8 +145,7 @@ pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {

pub async fn handle(&mut self, mut #input_ident: &[u8]) -> Vec<u8> {
#(#invocation_dispatches)*
let invocation_path = String::decode(&mut #input_ident).expect("Failed to decode invocation path");
panic!("Unknown request: {}", invocation_path);
#unexpected_route_panic
}

#(#invocation_funcs)*
Expand Down
12 changes: 7 additions & 5 deletions macros/core/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::route;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Span, TokenStream as TokenStream2};
use proc_macro_error::abort;
use quote::{quote, ToTokens};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -156,16 +156,18 @@ pub(crate) fn discover_invocation_targets(

pub(crate) fn generate_unexpected_input_panic(input_ident: &Ident, message: &str) -> TokenStream2 {
let message_pattern = message.to_owned() + ": {}";
let copy_ident = Ident::new(&format!("__{}", input_ident), Span::call_site());
quote!({
let input = String::decode(&mut #input_ident)
let mut #copy_ident = #input_ident;
let input = String::decode(&mut #copy_ident)
.unwrap_or_else(|_| {
if #input_ident.len() <= 8 {
format!("0x{}", hex::encode(#input_ident))
format!("0x{}", sails_rtl::hex::encode(#input_ident))
} else {
format!(
"0x{}..{}",
hex::encode(&#input_ident[..4]),
hex::encode(&#input_ident[#input_ident.len() - 4..]))
sails_rtl::hex::encode(&#input_ident[..4]),
sails_rtl::hex::encode(&#input_ident[#input_ident.len() - 4..]))
}
});
panic!(#message_pattern, input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ pub mod wasm {
let input = gstd::msg::load_bytes().expect("Failed to read input");
if !input.is_empty() {
{
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand Down Expand Up @@ -112,14 +113,15 @@ pub mod wasm {
let output = service.handle(&input[__ROUTE_SVC1.len()..]).await;
[__ROUTE_SVC1.as_ref(), &output].concat()
} else {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ pub mod wasm {
let input = gstd::msg::load_bytes().expect("Failed to read input");
if !input.is_empty() {
{
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand All @@ -85,14 +86,15 @@ pub mod wasm {
let output = service.handle(&input[__ROUTE_SERVICE.len()..]).await;
[__ROUTE_SERVICE.as_ref(), &output].concat()
} else {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ pub mod wasm {
let program = MyProgram::new2(request.p2, request.p1);
(program, INVOCATION_ROUTE.as_ref())
} else {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand All @@ -90,14 +91,15 @@ pub mod wasm {
async fn main() {
let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input");
let output: Vec<u8> = {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ pub mod wasm {
let input = gstd::msg::load_bytes().expect("Failed to read input");
if !input.is_empty() {
{
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand All @@ -54,14 +55,15 @@ pub mod wasm {
async fn main() {
let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input");
let output: Vec<u8> = {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ pub mod wasm {
let program = MyProgram::new(request.p1, request.p2).await;
(program, INVOCATION_ROUTE.as_ref())
} else {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand All @@ -73,14 +74,15 @@ pub mod wasm {
async fn main() {
let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input");
let output: Vec<u8> = {
let input = String::decode(&mut input)
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", hex::encode(input))
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", hex::encode(& input[..4]), hex::encode(&
input[input.len() - 4..])
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
Expand Down
18 changes: 15 additions & 3 deletions macros/core/tests/snapshots/service__gservice_works.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,21 @@ impl Exposure<SomeService> {
static INVOCATION_ROUTE: [u8; 5usize] = [16u8, 84u8, 104u8, 105u8, 115u8];
return [INVOCATION_ROUTE.as_ref(), &output].concat();
}
let invocation_path = String::decode(&mut input)
.expect("Failed to decode invocation path");
panic!("Unknown request: {}", invocation_path);
{
let mut __input = input;
let input = String::decode(&mut __input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
format!("0x{}", sails_rtl::hex::encode(input))
} else {
format!(
"0x{}..{}", sails_rtl::hex::encode(& input[..4]),
sails_rtl::hex::encode(& input[input.len() - 4..])
)
}
});
panic!("Unknown request: {}", input)
}
}
async fn __do_this(&mut self, mut input: &[u8]) -> Vec<u8> {
let request = __DoThisParams::decode(&mut input)
Expand Down
Loading