Repository

vmemfd

Experimental memfd_create ELF execution prototype in vlang

memfd_create

memfd_create() creates an anonymous file and returns a file descriptor that refers to it. The file behaves like a regular file, and so can be modified, truncated, memory-mapped, and so on. However, unlike a regular file, it lives in RAM and has a volatile backing storage. Once all references to the file are dropped, it is automatically released. Anonymous memory is used for all backing pages of the file.

Goals

  • memfd based ELF execution in v
  • use $embed_file for files < 20Mb
  • self-extracting binary decompressing to memfd pointer

Gist


import lmangani.memfd

// Read binary ELF
data := os.read_file(filename) or {
  panic('error reading elf $filename')
  return
}

// Allocate a memfd MFD_CLOEXEC and write ELF
fd_id := memfd.vmemfd_new('myapp')
os.fd_write(fd_id, data)	

// Execute w/ arguments from memfd
pointer := '/proc/self/fd/$fd_id'
os.execve(pointer, args, []) or {
  panic('error executing $pointer')
  return
}

Test

Compile an ELF binary expecting args (or bring your own)

v -o app -prod app.v

Load and execute ELF using memdfd

v run vload.v test me

About

0
4
last May 7

Author

lmangani