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

[feature] #2511: Restrict FFI types on wasm #2590

Merged

Conversation

Erigara
Copy link
Contributor

@Erigara Erigara commented Aug 3, 2022

Signed-off-by: Shanin Roman shanin1000@yandex.ru

Description of the Change

  • Add wasm feature to iroha_ffi crate;
  • Add PrimitiveRepr trait to substitute unsupported types with supported ones;
  • Refactor Vec transfer through ff-boundry.

Issue

Closes #2511.

Benefits

Protect from various errors (when user can pass u32 where u8 supposed to be).

Possible Drawbacks

More complex implementation.

Usage Examples or Tests [optional]

Alternate Designs [optional]

@Erigara Erigara added the iroha2-dev The re-implementation of a BFT hyperledger in RUST label Aug 3, 2022
@codecov
Copy link

codecov bot commented Aug 3, 2022

Codecov Report

Merging #2590 (47bc640) into iroha2-dev (a16d9c3) will decrease coverage by 1.74%.
The diff coverage is 63.26%.

❗ Current head 47bc640 differs from pull request most recent head 762bbb9. Consider uploading reports for the commit 762bbb9 to get more accurate results

@@              Coverage Diff               @@
##           iroha2-dev    #2590      +/-   ##
==============================================
- Coverage       67.61%   65.86%   -1.75%     
==============================================
  Files             140      156      +16     
  Lines           26173    28198    +2025     
==============================================
+ Hits            17696    18574     +878     
- Misses           8477     9624    +1147     
Impacted Files Coverage Δ
cli/derive/src/lib.rs 74.72% <ø> (ø)
cli/src/torii/mod.rs 28.88% <ø> (ø)
cli/src/torii/routing.rs 69.92% <0.00%> (-2.87%) ⬇️
client/src/client.rs 43.36% <0.00%> (-2.96%) ⬇️
client_cli/src/main.rs 0.26% <ø> (ø)
config/base/src/runtime_upgrades.rs 35.63% <ø> (ø)
core/src/block.rs 70.00% <ø> (ø)
core/src/queue.rs 95.67% <ø> (-0.08%) ⬇️
core/src/smartcontracts/isi/asset.rs 54.82% <ø> (ø)
core/src/smartcontracts/wasm.rs 95.39% <ø> (+0.12%) ⬆️
... and 154 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@@ -285,12 +285,12 @@ fn return_result() {
unsafe {
assert_eq!(
FfiResult::ExecutionFail,
FfiStruct__fallible_int_output(u8::from(false), output.as_mut_ptr())
FfiStruct__fallible_int_output(From::from(false), output.as_mut_ptr())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this change, being that it makes the type checker do a better job?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When wasm feature is active ffi representation of bool would be u32 not u8, so I did this so that the test worked in both cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u8 for bool is problematic. Every operation involving booleans would be extremely slow (which is why it's almost always size_t)

ffi/src/handle.rs Show resolved Hide resolved
source: Self::Source,
_: &'itm mut <Self as iroha_ffi::owned::TryFromReprCVec<'itm>>::Store,
) -> Result<Vec<Self>, iroha_ffi::FfiResult> {
let slice = source.into_rust().ok_or(iroha_ffi::FfiResult::ArgIsNull)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that FfiResult has a lot more variants, wouldn't it make sense to add FfiResult::ConversionFailed? There's non-zero overlap between the two, but even if 100% of the failures were because ArgIsNull, we'd want to be able to handle it differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added FfiResult::ConversionFailed, however for this particular case FfiResult::ArgIsNull is fine, because only case in which slice.into_rust() return None is when slice is null.

ffi/tests/gen_shared_fns.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
@Erigara Erigara force-pushed the restrict-wasm-ffi-types branch 2 times, most recently from 332e2a1 to 47bc640 Compare August 9, 2022 08:16
@mversic mversic self-requested a review August 9, 2022 09:02
@Erigara Erigara changed the title [feature] #2511: Restrict FFI types on wasm (WIP) [feature] #2511: Restrict FFI types on wasm Aug 10, 2022
@Erigara Erigara marked this pull request as ready for review August 10, 2022 13:35
}

primitive_impls! {u8, u16, u32, u64, u128, i8, i16, i32, i64}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do we do when we actually have a u128?

Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
@appetrosyan appetrosyan merged commit 8a0ff69 into hyperledger-iroha:iroha2-dev Aug 14, 2022
s8sato pushed a commit to s8sato/iroha that referenced this pull request Aug 15, 2022
@Erigara Erigara deleted the restrict-wasm-ffi-types branch August 18, 2022 03:26
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 6, 2022
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 6, 2022
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 6, 2022
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 7, 2022
BAStos525 pushed a commit to mversic/iroha that referenced this pull request Sep 7, 2022
…dger-iroha#2590)

Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
Signed-off-by: BAStos525 <jungle.vas@yandex.ru>
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 8, 2022
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 9, 2022
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 9, 2022
BAStos525 pushed a commit to mversic/iroha that referenced this pull request Sep 9, 2022
mversic pushed a commit to mversic/iroha that referenced this pull request Sep 9, 2022
BAStos525 pushed a commit to mversic/iroha that referenced this pull request Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iroha2-dev The re-implementation of a BFT hyperledger in RUST
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants