wasm-forge / ic-wasi-polyfill

The polyfill implementation for WASI functions in the IC environment
MIT License
17 stars 4 forks source link

Basic stable filesystem read and write not working #13

Closed lastmjs closed 10 months ago

lastmjs commented 10 months ago

Perhaps I'm missing something simple. I am using the polyfill and trying to write a simple file and then read it after deploy. I don't have the transient feature turned on. I've tested this in both Azle and Rust and it doesn't work in either CDK.

Rust

#[ic_cdk::init]
fn init() {
    ic_wasi_polyfill::init(&[], &[]);
}

#[ic_cdk::post_upgrade]
fn post_upgrade() {
    ic_wasi_polyfill::init(&[], &[]);
}

#[ic_cdk::query]
fn read() -> String {
    std::fs::read_to_string("test.txt").unwrap()
}

#[ic_cdk::update]
fn write(contents: String) -> String {
    std::fs::write("test.txt", contents.clone()).unwrap();

    contents
}

Writing and then reading works within the same deploy. After deploying again I get the following error:

Error: Failed update call.
Caused by: Failed update call.
  The replica returned a replica error: reject code CanisterError, reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai trapped explicitly: Panicked at 'called Result::unwrap() on an Err value: Os { code: 28, kind: InvalidInput, message: "Invalid argument" }', src/test_wasi_backend/src/lib.rs:14:41, error code None

Azle

import { Canister, query, text, update } from 'azle';
import { readFileSync, writeFileSync } from 'fs';

export default Canister({
    read: query([], text, () => {
        return readFileSync('test.txt').toString();
    }),
    write: update([text], text, (fileContents) => {
        writeFileSync('test.txt', fileContents);

        return fileContents;
    })
});

Writing and then reading works within the same deploy. After deploying again I get the following error:

("Error: EINVAL: invalid argument, open \'test.txt\'")

In Azle the report_wasi_calls feature is turned on and I everything looks fine:

Reading:

2024-01-05 16:50:14.040678536 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_prestat_get    instructions:    158085    
2024-01-05 16:50:14.040678536 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_prestat_dir_name    instructions:    422    parameters:    fd=3
2024-01-05 16:50:14.040678536 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_prestat_get    instructions:    331    
2024-01-05 16:50:14.040678536 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_path_open    instructions:    35697    parameters:    parent_fd=3 file_name="test.txt"

Writing:

2024-01-05 16:51:42.035167071 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_prestat_get    instructions:    158085    
2024-01-05 16:51:42.035167071 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_prestat_dir_name    instructions:    422    parameters:    fd=3
2024-01-05 16:51:42.035167071 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_prestat_get    instructions:    331    
2024-01-05 16:51:42.035167071 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_path_open    instructions:    289546    parameters:    parent_fd=3 file_name="test.txt"
2024-01-05 16:51:42.035167071 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_write    instructions:    149664    parameters:    fd=4 len=1
2024-01-05 16:51:42.035167071 UTC: [Canister bkyz2-fmaaa-aaaaa-qaaaq-cai]     __ic_custom_fd_close    instructions:    3898    parameters:    fd=4

In Azle I've tried to strip away any other stable structures code, and I have put logs into my fork of ic-wasi-polyfill to ensure that the StableStorage filesystem implementation is being used.

The Rust example is extremely simple and it's basically just that code above with the latest ic-wasi-polyfill commit.

What am I doing wrong? Is this a bug?

ulan commented 10 months ago

This is most likely an instance of https://github.com/wasm-forge/ic-wasi-polyfill/issues/6

@lastmjs: please try using the newly added init_with_memory() and pass a virtual memory for the polyfill to use for storage.

wasm-forge commented 10 months ago

Hi Jordan,

I've found one issue with the stable storage creation, please check if it works on the newer ic-wasi-polyfill version 0.3.13.

lastmjs commented 10 months ago

So far working well, thanks!