Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / linux / memfd_create.c
blobf37e4b5c2f1f89b2eba39be4ed122fa99a606e65
1 #define _GNU_SOURCE
3 #include <assert.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <sys/mman.h>
7 #include <unistd.h>
9 #include "../../memcheck.h"
11 static void
12 assert_expected (int fd, int expected_seals)
14 int current_seals = fcntl (fd, F_GET_SEALS);
15 assert (current_seals == expected_seals);
18 static void
19 add_seal (int fd, int *expected_seals, int new_seal)
21 int r = fcntl (fd, F_ADD_SEALS, new_seal);
22 assert (r == 0);
24 *expected_seals |= new_seal;
26 // Make sure we get the result we expected.
27 assert_expected (fd, *expected_seals);
30 static void
31 add_seal_expect_fail (int fd, int new_seal, int expected_errno)
33 int r = fcntl (fd, F_ADD_SEALS, new_seal);
34 assert (r == -1 && errno == expected_errno);
37 int
38 main (void)
40 int expected_seals = 0;
41 int fd;
43 // Try with an fd that doesn't support sealing.
44 fd = memfd_create ("xyz", 0);
45 if (fd < 0)
47 // Not supported, nothing to test...
48 return 1;
51 assert_expected (fd, F_SEAL_SEAL);
52 add_seal_expect_fail (fd, F_SEAL_WRITE, EPERM);
53 assert_expected (fd, F_SEAL_SEAL); // ...should still be unset after failed attempt
54 close (fd);
56 // Now, try the successful case.
57 fd = memfd_create ("xyz", MFD_ALLOW_SEALING);
58 add_seal (fd, &expected_seals, F_SEAL_SHRINK);
59 add_seal (fd, &expected_seals, F_SEAL_GROW);
61 // Now prevent more sealing.
62 add_seal (fd, &expected_seals, F_SEAL_SEAL);
64 // And make sure that it indeed fails.
65 add_seal_expect_fail (fd, F_SEAL_WRITE, EPERM);
66 assert_expected (fd, expected_seals);
67 close (fd);
69 // Test the only memory failure possible: passing an undefined argument to F_ADD_SEALS
70 int undefined_seal = 0;
71 VALGRIND_MAKE_MEM_UNDEFINED(&undefined_seal, sizeof undefined_seal);
72 fcntl (-1, F_ADD_SEALS, undefined_seal);
74 return 0;