# Introducing WASI for IC

**URL:** https://forum.dfinity.org/t/introducing-wasi-for-ic/18583
**Category:** Developers
**Tags:** Discussing
**Created:** [February 19, 2023, 11:46am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583 "2023-02-19T11:46:49Z")
**Posts on this page:** 20
**Page:** 4

<div class="post-metadata">

### Author: ![q2333gh](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/q2333gh/32/36020_2.png) [@q2333gh](https://forum.dfinity.org/u/q2333gh)
#### Post date: [March 6, 2024, 5:27am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/61 "2024-03-06T05:27:10Z")

</div>

check out this interesting :

[https://internetcomputer.org/blog/news-and-updates/team-spotlight-node](https://internetcomputer.org/blog/news-and-updates/team-spotlight-node)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/b/e/bef231542ab9f4844eff1e49d46d30fc76b09c8f.png)

---

<div class="post-metadata">

### Author: ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)
#### Post date: [June 24, 2024, 11:02am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/62 "2024-06-24T11:02:29Z")

</div>

@lastmjs ,

I am digging into the use of wasi2ic within both Kybra and Azle.

I noticed that both are using the feature `transient` for the ic-wasi-polyfill, with reference to this issue:

- [Renable stable files once fs-stable is more stable (#1731)](https://github.com/demergent-labs/azle/issues/1731)

What exactly is the impact of using this feature, and do you know if there is work on-going to fix it?

---

<div class="post-metadata">

### Author: ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)
#### Post date: [June 24, 2024, 11:40am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/63 "2024-06-24T11:40:34Z")

</div>

If you turn that off the polyfill will use the stable memory file system. We tried it and unfortunately found it to be unstable (ha) in various ways that did not make us feel comfortable using it yet.

@sgaflv can give you more details on timelines and current state of the polyfill and its stability.

---

<div class="post-metadata">

### Author: ![sgaflv](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@sgaflv](https://forum.dfinity.org/u/sgaflv)
#### Post date: [June 24, 2024, 11:46am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/64 "2024-06-24T11:46:54Z")

</div>

@lastmjs I am currently working on improving the work of stable-fs. I will provide a new version by the end of this month and will check that the issues are resolved.

---

<div class="post-metadata">

### Author: ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)
#### Post date: [June 27, 2024, 6:31pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/65 "2024-06-27T18:31:23Z")

</div>

@sgaflv , @lastmjs

Just a quick note that I was able to integrate the polyfill and wasi2ic into icpp-pro, the CDK for C++.

It is all still in my dev environment, and I need to do a lot more testing, but first experiments show that it is working really well also in the C++ world, and I believe this is going to help a lot with porting C++ libraries to the IC.

I took a lot of inspiration from kybra, and icpp-pro now installs rust + rust dependencies in a `.icpp` subfolder of the user’s home directory.

When a user builds their wasm, everything is transparently included. They don’t need to do anything to their code.

Some things that work now (tested it):

- std::cout : Prints to dfx console
- file open/close/write/read
- …

---

<div class="post-metadata">

### Author: ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)
#### Post date: [June 27, 2024, 9:10pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/66 "2024-06-27T21:10:42Z")

</div>

@sgaflv ,

Will stable-fs also work for a C++ canister?

If so, do you happen to have a simple demo example available? 🙏\*(please?)\*

The demo2 that you created was extremely helpful in figuring out how to get it all to work.

---

<div class="post-metadata">

### Author: ![sgaflv](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@sgaflv](https://forum.dfinity.org/u/sgaflv)
#### Post date: [June 29, 2024, 11:00am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/67 "2024-06-29T11:00:43Z")

</div>

yes, it should work with the file system as well. Sounds like a good idea to create an extra demo for that (or maybe make the demo2 more advanced). I will add an issue for that.

---

<div class="post-metadata">

### Author: ![kristofer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kristofer/32/11135_2.png) [@kristofer](https://forum.dfinity.org/u/kristofer)
#### Post date: [July 12, 2024, 2:45pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/68 "2024-07-12T14:45:41Z")

</div>

I am trying out an idea where the user keeps a local copy of sqlite in sync with the main copy running inside the canister. It might be an interesting design pattern for personal apps where the db is known to be small.

All works well, mostly. I am not able to upgrade the canister. The DB is still there after the upgrade but seems to have lost one of its tables.

```rust
#[init]
fn init() {
    ic_wasi_polyfill::init(&[0u8; 32], &[]);

    DB.with_borrow_mut(|db| {
        *db = Some(Connection::open(DB_FILE).unwrap());
        let db = db.as_mut().unwrap();

        modules::change::init_db(db);
        modules::chat::init_db(db);
    });
}

#[post_upgrade]
fn post_upgrade() {
    ic_wasi_polyfill::init(&[0u8; 32], &[]);

    DB.with_borrow_mut(|db| {
        *db = Some(Connection::open(DB_FILE).unwrap());
    });
}

```

Am I missing something here? Or maybe issues are related to the unstableness described by @lastmjs above?

Frontend: [https://vicki-naaaa-aaaal-qjgma-cai.icp0.io](https://vicki-naaaa-aaaal-qjgma-cai.icp0.io)  
Backend: [DFINITY Canister Candid UI](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.icp0.io/?id=ufmog-ciaaa-aaaal-qjglq-cai)

> **[GitHub - kristoferlund/ic-sqlite-chat: Chat demo for ICP that uses Sqlite for...](https://github.com/kristoferlund/ic-sqlite-chat)**
>
> Chat demo for ICP that uses Sqlite for storage with a "local first" approach - kristoferlund/ic-sqlite-chat

---

<div class="post-metadata">

### Author: ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)
#### Post date: [July 15, 2024, 12:58am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/69 "2024-07-15T00:58:47Z")

</div>

@kristofer ,  
I wonder if your issue is related to something @ulan fixed and described [here](https://forum.dfinity.org/t/technical-working-group-deai/24621/114)

---

<div class="post-metadata">

### Author: ![kristofer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kristofer/32/11135_2.png) [@kristofer](https://forum.dfinity.org/u/kristofer)
#### Post date: [July 15, 2024, 7:14am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/70 "2024-07-15T07:14:05Z")

</div>

Thanks for the tip. Yes, assigning the wasi memory a `MemoryId` sounds like good practice. I added that, problem remains though.

More details on the sqlite error code below. Seems sqlite can’t access the db file after upgrade. I have verified using `std:fs` that the file still exists and file size remains unchanged etc.

```bash
(1802) SQLITE_IOERR_FSTAT
The SQLITE_IOERR_FSTAT error code is an extended error code for 
SQLITE_IOERR indicating an I/O error in the VFS layer while trying to 
invoke fstat() (or the equivalent) on a file in order to determine 
information such as the file size or access permissions.

```

---

<div class="post-metadata">

### Author: ![sgaflv](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@sgaflv](https://forum.dfinity.org/u/sgaflv)
#### Post date: [July 15, 2024, 7:41pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/71 "2024-07-15T19:41:10Z")

</div>

Hello Kristofer,

I’ve checked your example. Unfortunately there seems for be a bug in the way the stable-fs is storing the data in stable structures, which leads to loosing files after canister upgrade. I am working on a fix now.

You have an awesome sqlite example! If you don’t mind, I’d mention it on the wasi2ic page.

---

<div class="post-metadata">

### Author: ![kristofer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kristofer/32/11135_2.png) [@kristofer](https://forum.dfinity.org/u/kristofer)
#### Post date: [July 15, 2024, 8:28pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/72 "2024-07-15T20:28:51Z")

</div>

Thanks for looking into this! And yes, please do mention my example if you will. 🤩

Perhaps canister upgrade could be added to your demo3 as well?

---

<div class="post-metadata">

### Author: ![sgaflv](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@sgaflv](https://forum.dfinity.org/u/sgaflv)
#### Post date: [July 16, 2024, 12:27am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/73 "2024-07-16T00:27:30Z")

</div>

@kristofer  
Good point, I’ll update demo3. Check out the latest published ic-wasi-polyfill version 0.4.3, see if it resolves your issue.

---

<div class="post-metadata">

### Author: ![kristofer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kristofer/32/11135_2.png) [@kristofer](https://forum.dfinity.org/u/kristofer)
#### Post date: [July 16, 2024, 6:21am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/74 "2024-07-16T06:21:44Z")

</div>

Yes, great! Issue resolved!

---

<div class="post-metadata">

### Author: ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)
#### Post date: [July 16, 2024, 11:10am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/75 "2024-07-16T11:10:55Z")

</div>

@lastmjs: fyi, this should also fix the flaky issue you were observing. ^

---

<div class="post-metadata">

### Author: ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)
#### Post date: [July 17, 2024, 11:12am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/76 "2024-07-17T11:12:40Z")

</div>

@sgaflv ,

FYI, the integration of ic-wasi-polyfill & wasi2ic into icpp-pro is finished, and released (as a pre-release initially, but it is working great so far)

You can check out the wonderfully simple C++ code, with stdio, file-io, file upload/download that this now enables in the [icpp-demos/canisters/files](https://github.com/icppWorld/icpp-demos/tree/main/canisters/files/src) example canister.

Also fyi, with extreme little effort I was now able to get files to upload to a C++ LLM that I am working on.

I can’t thank you enough for building wasi2ic. 😀 🙏

---

<div class="post-metadata">

### Author: ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)
#### Post date: [July 17, 2024, 11:57am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/77 "2024-07-17T11:57:39Z")

</div>

Yes it’s amazing! I really hope it can all get integrated into dfx eventually so that Wasi becomes the defacto standard on ICP and it just works.

---

<div class="post-metadata">

### Author: ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)
#### Post date: [July 17, 2024, 2:03pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/78 "2024-07-17T14:03:16Z")

</div>

@sgaflv ,  
from C++, we call raw\_init, like this:

```auto
int main() {
  raw_init(nullptr, 0);
  return 0;
}

```

Is that still correct?

I am concerned about the discussion above regarding Rust based aplications, that one should call [init\_with\_memory](https://github.com/wasm-forge/ic-wasi-polyfill/blob/417c48e24402949b3fe2eefb5e25cc6f6d671beb/src/lib.rs#L1710) or [init\_with\_memory\_manager](https://github.com/wasm-forge/ic-wasi-polyfill/blob/417c48e24402949b3fe2eefb5e25cc6f6d671beb/src/lib.rs#L1721).

Not sure if this applies to C++ as well.

---

<div class="post-metadata">

### Author: ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)
#### Post date: [July 17, 2024, 2:40pm UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/79 "2024-07-17T14:40:15Z")

</div>

@icpp it is only important if your C++ application also wants to directly access the stable memory. The Rust memory manager allows to share the stable memory between the application and the ic-wasi-polyfill library such that they don’t overwrite each other’s data in the stable memory.

If your C++ application stores data in the stable memory using the standard file system and doesn’t call the stable memory API functions, then it is all good and will work correctly because ic-wasi-polyfill will manage the stable memory.

---

<div class="post-metadata">

### Author: ![mycelia](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@mycelia](https://forum.dfinity.org/u/mycelia)
#### Post date: [July 18, 2024, 2:28am UTC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583/80 "2024-07-18T02:28:05Z")

</div>

I’m really fascinated by the technology behind this!. Could you explain how WASI fits into the ICP ecosystem? I’m curious about what this enables and how it is better than other WASI usages.

[Previous page](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583.md?page=3)

[Next page](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583.md?page=5)
