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

Check the behavior when contract tries to open a file and fix it #219

Closed
da1suk8 opened this issue Jul 7, 2022 · 5 comments
Closed

Check the behavior when contract tries to open a file and fix it #219

da1suk8 opened this issue Jul 7, 2022 · 5 comments
Assignees

Comments

@da1suk8
Copy link
Member

da1suk8 commented Jul 7, 2022

  • Check for errors when a contract tries to open a file.
  • Contract that should not be stored (contract that try to open a file) is stored.

This contract should not be stored and needs to be fixed.

@da1suk8
Copy link
Member Author

da1suk8 commented Sep 2, 2022

  • Goal : The contract, which should not be stored or executed, can be stored and executed in line/cosmwasm, so check and modify it.
    • Add the following code and check by execute(instantiate, migrate), query

      1. line/cosmwasm(main branch)
      2. cosmwasm/cosmwasm
      3. line/cosmwasm(dynamic_link branch)
    • If there is a difference between the above results, correct the behavior (where should it fall? etc...) and correct it.

    • If there is no difference as a result of the above, correct the behavior (where should it fall? etc...) and where can it actually be executed? If it seems to be possible to fix it, then fix it.

use std::fs::File;
use std::io::prelude::*;
   
fn check_store(filename: String) {
    let mut f = File::open(filename).expect("file not found");
   
    let mut contents = String::new();
    f.read_to_string(&mut contents)
        .expect("something went wrong reading the file");
 
}

@da1suk8
Copy link
Member Author

da1suk8 commented Sep 2, 2022

Error in cosmwasm/cosmwasm

raw_log: 'failed to execute message; message index: 0: Error calling the VM: Error
executing Wasm: Wasmer runtime error: RuntimeError: Aborted: panicked at ''file
not found: Error { kind: Unsupported, message: "operation not supported on this
platform" }'', src/contract.rs:43:38: execute wasm contract failed'

Error in line/cosmwasm

{"height":"742","txhash":"48C091A7013110541E135BADB507F3FF4DBBF909C0DB830A192BDC88797150D7","codespace":"wasm","code":5,"data":"","raw_log":"failed to execute message; message index: 0: Error calling the VM: Error executing Wasm: Wasmer runtime error: RuntimeError: unreachable: execute wasm contract failed","logs":[],"info":"","gas_wanted":"200000","gas_used":"83203","tx":null,"timestamp":"","events":[{"type":"tx","attributes":[{"key":"ZmVl","value":"","index":false}]},{"type":"tx","attributes":[{"key":"YWNjX3NlcQ==","value":"bGluazF0d3NmbXVqMjhuZHBoNTRrNG53OGNyd3U4aDljOG1oM3J0eDcwNS8xMg==","index":false}]},{"type":"tx","attributes":[{"key":"c2lnbmF0dXJl","value":"L2xWMEt2TUxUdHNZRkNESjV6ZzJGWUhRQTZlWEJRODJibXVFQWVyWVFDcGd6WlFtWENNdFRENy9JblVZWFFyamVsSENxa2l5OXV0TFdmT0JGTzdaUlE9PQ==","index":false}]}],"index":0}

@da1suk8
Copy link
Member Author

da1suk8 commented Sep 16, 2022

Conculusion

In cosmwasm/cosmwasm(line/cosmwasm), target=wasm32-unknown-unknown is used, so that std::fs functions return runtime errors at executing. Therefore, as for File::open, it cannot be executed.

In addition, when compiled with target=wasm32-wasi using WASI, for which File::open, etc. is standardized, and tried to do store of wasm, an error occurred in validation. (EXCEPT dynamic link branch)

raw_log: 'failed to execute message; message index: 0: Error calling the VM: Error
  during static Wasm validation: Wasm contract requires unsupported import: "wasi_snapshot_preview1.fd_filestat_get".
  Required imports: {"env.addr_canonicalize", "env.addr_humanize", "env.addr_validate",
  "env.db_next", "env.db_read", "env.db_remove", "env.db_scan", "env.db_write", "env.debug",
  "env.ed25519_batch_verify", ... 15 more}. Available imports: ["env.abort", "env.db_read",
  "env.db_write", "env.db_remove", "env.addr_validate", "env.addr_canonicalize", "env.addr_humanize",
  "env.secp256k1_verify", "env.secp256k1_recover_pubkey", "env.ed25519_verify", "env.ed25519_batch_verify",
  "env.debug", "env.query_chain", "env.db_scan", "env.db_next"].: create wasm contract
  failed'

Although there is a difference in the error contents between cosmwasm/cosmwasm and line/cosmwasm, the location causing the error is the same, and adding an abort feature as shown below will result in the same error contents.

Found task

@loloicci
Copy link
Contributor

@da1suk8 Could you try the wasm32-wasi version WASM in the dynamic link branch version LBM? The error "Wasm contract requires unsupported import" seems not to occur in dynamic link branch. It is because of this change (https://github.com/line/cosmwasm/blame/dynamic_link/packages/vm/src/compatibility.rs#L116).

@da1suk8
Copy link
Member Author

da1suk8 commented Sep 20, 2022

The following error occurred during instantiate in the dynamic link branch. It seems that execution itself is not possible.

{"height":"789","txhash":"85A99B66B0B3A58D19947F2A7A81FDA5882AFA95BD6937D2FA5E55EA24BECD76","codespace":"wasm","code":4,"data":"","raw_log":"failed to execute message; message index: 0: Error calling the VM: Error executing Wasm: Wasmer runtime error: RuntimeError: Error in guest/host communication: Got an invalid region: Got a zero Wasm address in the offset: instantiate wasm contract failed","logs":[],"info":"","gas_wanted":"10000000000","gas_used":"63259","tx":null,"timestamp":""}

Also, the following modification(https://github.com/line/cosmwasm/blame/dynamic_link/packages/vm/src/compatibility.rs#L116) to the dynamic link branch causes functions such as wasi_snapshot_preview1.fd_filestat_get to be judged as functions used by dynamic link when compiled by wasm32-wasi, etc. I have created a new issue.

@da1suk8 da1suk8 closed this as completed Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants