Unable to use memfd_create method

281 views
Skip to first unread message

Aitha Tarun

unread,
Oct 14, 2022, 6:19:09 AM10/14/22
to emscripten-discuss
Hi, I am unable to use the memfd_create method while building the C script to emscripten.
Below I have given my references to reproduce the error.

Code :

#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#define _GNU_SOURCE

int memfd_create(const char *name, unsigned int flags);

int main()
{
    int fd = memfd_create("temp", 0);

    printf("Fd : %d\n", fd);

    int wrote = write(fd, "Hello", 5);

    printf("Wrote : %d\n", wrote);

    char * buf = malloc(5);

    int red = pread(fd, buf, 5, 0);

    printf("Read : %d\n", red);
    printf("Content %s\n", buf);
}

Error :
1.png

Sam Clegg

unread,
Oct 14, 2022, 5:39:55 PM10/14/22
to emscripte...@googlegroups.com
Emscripten doesn't yet support the memfd_create API.  If you need to use it you could look into adding support for it (or file a bug to get it added).   I imagine that most codebased that use that API have some kind of cross-platform fallback.  Perhaps you could use that instead?

cheers,
sam

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/ef9839a4-8f8c-47c5-8af7-782b05e58c10n%40googlegroups.com.

Aitha Tarun

unread,
Oct 15, 2022, 1:08:04 AM10/15/22
to emscripten-discuss
Thanks for the reply, I will raise a bug to get this added.

Floh

unread,
Oct 15, 2022, 6:46:02 AM10/15/22
to emscripten-discuss
Hmm... does it really make sense to add such Linux specific functions to Emscripten? From looking at the MUSL source, it does have an memfd_create entry point, but that just jumps into the Linux SYS_memfd_create syscall, so it doesn't work anywhere but on Linux. Suppporting a random collection of Linux-specific syscalls might be more confusing to Emscripten users than having a clear definition of what's supposed to work (for instance: only CRT functions, only POSIX functions up to version X, etc...).

For instance, OP's code doesn't compile on macOS either:

Undefined symbols for architecture arm64:
  "_memfd_create", referenced from:
      _main in test-26b3ed.o
ld: symbol(s) not found for architecture arm64

Cheers,
-Floh.
Reply all
Reply to author
Forward
0 new messages