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

Update check for contract instantiation #5699

Merged
merged 4 commits into from
Sep 4, 2023
Merged

Update check for contract instantiation #5699

merged 4 commits into from
Sep 4, 2023

Conversation

statictype
Copy link
Member

Replaced the isWasm check on contract instantiation with isU8a in order to allow experimentation with other targets.
For more info see use-ink/contracts-ui#507

@statictype statictype requested a review from jacogr August 3, 2023 07:40
@athei
Copy link

athei commented Aug 11, 2023

Can we get this merged? I can't use contracts UI with RISC-V contracts otherwise.

@statictype
Copy link
Member Author

this PR seems pretty innocent, can we get it merged @jacogr? 🙏🏻

Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

I'm not convinced this is "innocent enough".

Basically at this point we can throw any data into the mix as expect it to work, i.e. some JSON, a random piece of text, etc. Basically we end up with garbage that cannot be executed in any interpreter.

If there are additional formats that should be supported, the better approach is to check the magic flag for that specific format in addition to the isWasm check.

EDIT: The is<NewFormat>() check function can just be added locally to the specific file - it would make sense to also add it to common (and when that is available in a release, the local version inside the file can be removed)

@athei
Copy link

athei commented Aug 31, 2023

Basically at this point we can throw any data into the mix as expect it to work, i.e. some JSON, a random piece of text, etc. Basically we end up with garbage that cannot be executed in any interpreter.

So what? pallet_contracts has to make absolutely sure that it can handle any input securely anyways. It will fail gracefully and report an error back in case of the data not being processable.

The verification also does only check the first bytes for the "wasm magic". There are still infinite many ways to send "garbage that cannot be executed in any interpreter" after those bytes.

I cannot see the value that this very shallow check provides. Users have to do error handling anyways since pallet_contracts can reject a module for all sorts of reasons. On the other hand having this check makes our lives more difficult for the stated reasons.

@jacogr
Copy link
Member

jacogr commented Aug 31, 2023

It is what it is :)

jacogr
jacogr previously requested changes Aug 31, 2023
Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

As explained in #5699 (review):

  1. Add a is<FormatName>() check function (e.g. assuming the new format is XXX we add isXXX - used in the sample for the next points) to check the magic bytes for the specific additional type
  2. Adjust the isWasm checks to do isWasm(...) || isXXX(...)
  3. (Optional, but appreciated) Also add the isXXX to the common repo (will eventually replace the local version when that stuff is released)

That should more-or-less cover the same functionality. (Not 100% sure on the source.wasm usage, implementers would know better on that)

@statictype
Copy link
Member Author

Added a similar check for RISC-V @jacogr
If it's ok i can add it to the common repo and import it from there in a follow-up PR

Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

Thank you, looks like it fits the bill.

Two small comment added just to be slightly more pedantic/explicit on return types/guards. (It always helps when we come back years from now)

packages/api-contract/src/base/Code.ts Outdated Show resolved Hide resolved
@@ -42,12 +52,12 @@ export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
constructor (api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, wasm: Uint8Array | string | Buffer | null | undefined, decorateMethod: DecorateMethod<ApiType>) {
super(api, abi, decorateMethod);

this.code = isWasm(this.abi.info.source.wasm)
this.code = isValidCode(this.abi.info.source.wasm)
Copy link
Member

Choose a reason for hiding this comment

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

Slightly weird that a different format may come through in source.wasm, but either way, all ok.

Copy link
Member Author

Choose a reason for hiding this comment

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

the field in the contract metadata will most likely be renamed once the support for RISC-V is released. not something we should address now imo.

packages/api-contract/src/base/Code.ts Outdated Show resolved Hide resolved
@statictype statictype changed the title Remove isWasm check for contract instantiation Update check for contract instantiation Sep 4, 2023
@jacogr jacogr merged commit ae24a99 into master Sep 4, 2023
4 checks passed
@jacogr jacogr deleted the ae-no-wasm-check branch September 4, 2023 21:13
@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Sep 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants