Skip to content

Commit

Permalink
memfd_secret: test case for memfd_secret
Browse files Browse the repository at this point in the history
Adds a test case to test checkpoint and restore of a
memfd_secret fd containing process.

Signed-off-by: Dhanuka Warusadura <[email protected]>
  • Loading branch information
warusadura committed Aug 17, 2023
1 parent c4881ac commit 7354e48
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ TST_NOFILE := \
memfd02 \
memfd02-hugetlb \
memfd03 \
memfd-secret00 \
shmemfd \
shmemfd-priv \
time \
Expand Down
76 changes: 76 additions & 0 deletions test/zdtm/static/memfd-secret00.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>

#include "zdtmtst.h"

#define SECRET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define SIZE 26

const char *test_doc = "memfd_secret file descriptor";
const char *test_author = "Dhanuka Warusadura <[email protected]>";

static int _memfd_secret(unsigned int flags)
{
return syscall(SYS_memfd_secret, flags);
}

static void *secret_init(size_t size)
{
int fd;
void *secretmem = NULL;

fd = _memfd_secret(0);
if (fd < 0)
return secretmem;

if (ftruncate(fd, size) < 0) {
close(fd);
return secretmem;
}

secretmem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (secretmem == MAP_FAILED) {
close(fd);
return secretmem;
}

return secretmem;
}

static void secret_fini(void *mem, size_t size)
{
munmap(mem, size);
}

int main(int argc, char *argv[])
{
char *secretmem;

test_init(argc, argv);

secretmem = secret_init(SIZE);
if (!secretmem) {
fail("memfd_secret: not supported operation");
return 1;
}

for (int i = 0; i < SIZE; i++)
secretmem[i] = 'A' + i;

test_daemon();
test_waitsig();

if (strncmp(secretmem, SECRET, SIZE)) {
fail("secretmem content mismatch");
return 1;
}

secret_fini(secretmem, SIZE);

pass();

return 0;
}

0 comments on commit 7354e48

Please sign in to comment.