1 // RUN: %clangxx %s -o %t && %run %t %p
2 
3 #include <assert.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <sys/mman.h>
7 #include <sys/prctl.h>
8 
9 #ifndef PR_SCHED_CORE
10 #  define PR_SCHED_CORE 62
11 #endif
12 
13 #ifndef PR_SCHED_CORE_CREATE
14 #  define PR_SCHED_CORE_CREATE 1
15 #endif
16 
17 #ifndef PR_SCHED_CORE_GET
18 #  define PR_SCHED_CORE_GET 0
19 #endif
20 
21 #ifndef PR_SET_VMA
22 #  define PR_SET_VMA 0x53564d41
23 #  define PR_SET_VMA_ANON_NAME 0
24 #endif
25 
main()26 int main() {
27 
28   int res;
29   res = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, 0, 0);
30   if (res < 0) {
31     assert(errno == EINVAL || errno == ENODEV);
32     return 0;
33   }
34 
35   uint64_t cookie = 0;
36   res = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, 0, 0, &cookie);
37   if (res < 0) {
38     assert(errno == EINVAL);
39   } else {
40     assert(cookie != 0);
41   }
42 
43   char invname[81], vlname[] = "prctl";
44   for (auto i = 0; i < sizeof(invname); i++) {
45     invname[i] = 0x1e;
46   }
47   invname[80] = 0;
48   auto p =
49       mmap(nullptr, 128, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
50   assert(p != MAP_FAILED);
51   // regardless of kernel support, the name is invalid
52   res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (uintptr_t)p, 128,
53               (uintptr_t)invname);
54   assert(res == -1);
55   assert(errno == EINVAL);
56   res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (uintptr_t)p, 128,
57               (uintptr_t)vlname);
58   if (res < 0) {
59     assert(errno == EINVAL);
60   }
61   munmap(p, 128);
62 
63   return 0;
64 }
65