# Failed to decode wasm module

**URL:** <https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486>\
**Category:** Developers\
**Tags:** Discussing\
**Created:** [June 2, 2024, 4:46pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486 "2024-06-02T16:46:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlliDoizCode](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/allidoizcode/32/1151_2.png) [@AlliDoizCode](https://forum.dfinity.org/u/AlliDoizCode)\
**Post date:** [June 2, 2024, 4:46pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/1 "2024-06-02T16:46:53Z")

</div>

When trying to upgrade a canister with a new wasm module programmatically through the admin canister I am getting this error.

```bash
(
  "Error from Canister dfztg-4aaaa-aaaak-qijzq-cai: Canister\'s Wasm module is not valid: Failed to decode wasm module: unsupported canister module format",
)

```

Here is the code block that is is attempting to make the upgrade

```auto
try {
      status := "stopping";
      await Canister.CanisterUtils().stopCanister(canister);
      status := "installing";
      await Canister.CanisterUtils().installCode(canister, arg, wasm);
      status := "starting";
      await Canister.CanisterUtils().startCanister(canister);
      status := "success";
  } catch (e) {
      await Canister.CanisterUtils().startCanister(canister);
      status := Error.message(e);
  };
},

```

```auto
public func stopCanister(canisterId: Principal): async () {
    await ic.stop_canister({ canister_id = canisterId });
};

public func startCanister(canisterId: Principal): async () {
    await ic.start_canister({ canister_id = canisterId });
};

public func installCode(canisterId: Principal, arg: Blob, wasmModule: Blob): async() {
    await ic.install_code({
        arg = arg;
        wasm_module = wasmModule;
        mode = #upgrade;
        canister_id = canisterId;
    });
};

```

DFX Version 0.20.1

The canister that is attempting to make the upgrade is a controller of the canister that it is trying to upgrade. I also tried deleting and creating a new canister but the error persist.

The logic I am using above has worked for multiple other projects so I’m not sure what the issue is

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [June 2, 2024, 7:20pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/2 "2024-06-02T19:20:09Z")

</div>

> [@AlliDoizCode](#):
>
> `unsupported canister module format"`

The error comes from [here](https://github.com/dfinity/ic/blob/4fd4484020819ece99a1ef5b6612c245c7f6b83d/rs/embedders/src/wasm_utils/decoding.rs#L56). Seems, it’s not about the install code logic, but just the Wasm file is invalid?

---

<div class="post-metadata">

**Author:** ![AlliDoizCode](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/allidoizcode/32/1151_2.png) [@AlliDoizCode](https://forum.dfinity.org/u/AlliDoizCode)\
**Post date:** [June 2, 2024, 10:45pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/3 "2024-06-02T22:45:12Z")

</div>

The wasm was created from a motoko project and I don’t think I’m using gzip.

dfx.json below

```auto
{
    "canisters": {
        "treasury": {
            "main": "src/stacked_dao_backend/treasury/main.mo",
            "type": "motoko"
        },
        "governance": {
            "main": "src/stacked_dao_backend/governance/main.mo",
            "type": "motoko"
        },
        "upgrader": {
            "main": "src/stacked_dao_backend/upgrader/main.mo",
            "type": "motoko"
        }
    },
    "networks": {
        "development": {
            "providers": ["https://icp0.io"],
            "type": "persistent"
        },
        "staging": {
            "providers": ["https://icp0.io"],
            "type": "persistent"
        }
    },
    "defaults": {
        "build": {
            "args": "",
            "packtool": "mops sources"
        }
    },
    "output_env_file": ".env",
    "version": 1
}

```

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [June 3, 2024, 2:47am UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/4 "2024-06-03T02:47:10Z")

</div>

Maybe you should try specifically using gzip? I think it is the default now(although old stuff shouldn’t break)

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [June 3, 2024, 7:59am UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/5 "2024-06-03T07:59:21Z")

</div>

From the replica code, it’s just the beginning of the Wasm file should start with either `\x00asm` or `\x1f\x8b\x08`.

You could probably just manually check if the Wasm you’re installing starts with one of those signatures. Something like:

```auto
find . -name '*.wasm'
xxd <Wasm file> | head -1

```

---

<div class="post-metadata">

**Author:** ![AlliDoizCode](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/allidoizcode/32/1151_2.png) [@AlliDoizCode](https://forum.dfinity.org/u/AlliDoizCode)\
**Post date:** [June 3, 2024, 12:15pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/6 "2024-06-03T12:15:07Z")

</div>

```bash
jonathangreen@Jonathans-Mac-mini Uploader % xxd ./governance.wasm | head -1
00000000: 0061 736d 0100 0000 01cc 011f 6000 0060 .asm........`..`
jonathangreen@Jonathans-Mac-mini Uploader % gzip ./governance.wasm      
jonathangreen@Jonathans-Mac-mini Uploader % xxd ./governance.wasm.gz | head -1
00000000: 1f8b 0808 a100 5d66 0003 676f 7665 726e ......]f..govern

```

Here is the output before and after using gzip

Both still give me the same error when I try to install them programmatically. This is the wasm file generated from both the `dfx deploy` and `dfx build` commands and when using the `dfx deploy` command it installs fine. The upload logic and install logic I am using are from pervious projects that worked, namely TheRegistry, CigDao v1 and the original implementation of CanDB

---

<div class="post-metadata">

**Author:** ![PanIndustrial](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/panindustrial/32/13740_2.png) [@PanIndustrial](https://forum.dfinity.org/u/PanIndustrial)\
**Post date:** [June 3, 2024, 12:50pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/7 "2024-06-03T12:50:02Z")

</div>

Does it work locally?

---

<div class="post-metadata">

**Author:** ![AlliDoizCode](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/allidoizcode/32/1151_2.png) [@AlliDoizCode](https://forum.dfinity.org/u/AlliDoizCode)\
**Post date:** [June 4, 2024, 1:02am UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/8 "2024-06-04T01:02:09Z")

</div>

The issue persist locally

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [June 5, 2024, 8:43am UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/9 "2024-06-05T08:43:29Z")

</div>

> [@AlliDoizCode](#):
>
> `await Canister.CanisterUtils().installCode(canister, arg, wasm);`

Then probably we should make sure the `wasm` we pass here still starts with those signatures? If it reproduces locally, we can just dump the first few bytes on the console.

There is an error somewhere, we just need to keep looking…

---

<div class="post-metadata">

**Author:** ![AlliDoizCode](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/allidoizcode/32/1151_2.png) [@AlliDoizCode](https://forum.dfinity.org/u/AlliDoizCode)\
**Post date:** [June 5, 2024, 11:04am UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/10 "2024-06-05T11:04:45Z")

</div>

dfx deploy works and I’m using the same wasm generated from that so seems like it has something to so with how dfx is building the wasm

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [June 5, 2024, 5:34pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/11 "2024-06-05T17:34:16Z")

</div>

I’d really focus on making sure that the Wasm binary stored in the admin canister is valid. The `unsupported canister module format` error is really about Wasm binary signature.

---

<div class="post-metadata">

**Author:** ![AlliDoizCode](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/allidoizcode/32/1151_2.png) [@AlliDoizCode](https://forum.dfinity.org/u/AlliDoizCode)\
**Post date:** [June 6, 2024, 6:55pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/12 "2024-06-06T18:55:51Z")

</div>

How would I do that and why wouldn’t it be invalid when the same wasm deploys successfully when using `dfx deploy`

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [June 6, 2024, 7:13pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/13 "2024-06-06T19:13:46Z")

</div>

As a start, debug.print the size of the wasm before you send it off and make sure it matches what is on disc. If they are the same, slice the first few bytes and last few bytes and make sure they match.

let x = Blob.toArray(wasm);  
let xsize = wasm.size();  
Debug.print(debug\_show(xsize);  
Debug.print(debug\_show((x[0],x[1],x[2],x[3]));  
Debug.print(debug\_show((x[xsize-4],x[xsize-3],x[x-2],x[x-1]));

---

<div class="post-metadata">

**Author:** ![HeliosFz](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/heliosfz/32/4575_2.png) [@HeliosFz](https://forum.dfinity.org/u/HeliosFz)\
**Post date:** [April 28, 2025, 2:00pm UTC](https://forum.dfinity.org/t/failed-to-decode-wasm-module/31486/14 "2025-04-28T14:00:25Z")

</div>

Has this problem been solved?
