Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/wasm-wasi-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ This is from a main function from a wasm module
```Containerfile
FROM scratch
COPY hello.wasm /
CMD ["/hello.wasm"]
ENTRYPOINT ["/hello.wasm"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you write it in commit message or here, about why is this change needed ?

Copy link
Author

Choose a reason for hiding this comment

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

Oh, sorry forgot to mention it ...

After adding the pass through of argv to the component I wanted to try it out. Upon running podman run mywasm-image:latest arg1 arg2 podman would replace CMD (the wasm binary) with arg1 which of course does not work. Changing to ENTRYPOINT resolves this.

Copy link
Collaborator

@flouthoc flouthoc Sep 12, 2025

Choose a reason for hiding this comment

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

This looks like a breaking change. I think adding this message to commit logs can help us revisit this and fix this if needed.

cc @giuseppe

Copy link
Author

Choose a reason for hiding this comment

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

Adding this to the commit logs should be possible but I don't really see where this is a breaking change? Without the changes of this PR a wasm module runs into the same problem. IIRC this is also the same behavior a non-wasm container would show.

Copy link
Author

Choose a reason for hiding this comment

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

Is the message in ce7d656 OK?

```
* Build wasm image using buildah
```console
Expand Down
26 changes: 26 additions & 0 deletions src/libcrun/handlers/handler-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define _GNU_SOURCE

#include <config.h>
#include <string.h>
#include "../container.h"
#include "../utils.h"
#include "handler-utils.h"
Expand Down Expand Up @@ -69,3 +70,28 @@ wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err

return 0;
}

wasm_encoding_t
wasm_interpret_header (const char *header, const size_t len)
{
if (len < 8)
return WASM_ENC_INVALID;

// Check for the WebAssembly magic bytes
// See: https://webassembly.github.io/spec/core/binary/modules.html#binary-module
if (memcmp (header, "\0asm", 4))
return WASM_ENC_INVALID;

/* The next four bytes are the WebAssembly version.
We don't care for the specific WebAssembly version
so we only read the value of the `layer` field which
was defined by the component spec.
See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md#component-definitions
*/
if (header[6] == '\0' && header[7] == '\0')
Copy link
Member

Choose a reason for hiding this comment

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

you need to make sure the header length is at least 8 before accessing these bytes.

Since you already need the length for the first comment, I'd suggest to store the header length first before any other check (size_t header_len = strlen (header);)

Copy link
Author

@t4chib4ne t4chib4ne Sep 30, 2025

Choose a reason for hiding this comment

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

I agree but strlen could terminate instantly for a correct header or never if we are unlucky because header just contains "random" bytes.
header is just a pointer to the whole wasm binary which we know the size of. So the function could also take that size as a parameter and check for at least a length of 8.

return WASM_ENC_MODULE;

// `layer` does not equal `0x00 0x00` so we are working
// with a component.
return WASM_ENC_COMPONENT;
}
9 changes: 9 additions & 0 deletions src/libcrun/handlers/handler-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
#include "../container.h"
#include <unistd.h>

typedef enum
{
WASM_ENC_INVALID,
WASM_ENC_MODULE,
WASM_ENC_COMPONENT
} wasm_encoding_t;

int wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err);

wasm_encoding_t wasm_interpret_header (const char *header, const size_t len);

#endif
Loading