10713053eSKevin Athey // RUN: %clangxx %s -o %t && %run %t %p
20713053eSKevin Athey 
30713053eSKevin Athey #include <assert.h>
40713053eSKevin Athey #include <errno.h>
50713053eSKevin Athey #include <stdint.h>
6*a4c97e19SDavid Carlier #include <sys/mman.h>
70713053eSKevin Athey #include <sys/prctl.h>
80713053eSKevin Athey 
95562d9b3SKevin Athey #ifndef PR_SCHED_CORE
105562d9b3SKevin Athey #  define PR_SCHED_CORE 62
115562d9b3SKevin Athey #endif
125562d9b3SKevin Athey 
135562d9b3SKevin Athey #ifndef PR_SCHED_CORE_CREATE
145562d9b3SKevin Athey #  define PR_SCHED_CORE_CREATE 1
155562d9b3SKevin Athey #endif
165562d9b3SKevin Athey 
17b401d2a4SKevin Athey #ifndef PR_SCHED_CORE_GET
18b401d2a4SKevin Athey #  define PR_SCHED_CORE_GET 0
19b401d2a4SKevin Athey #endif
20b401d2a4SKevin Athey 
21*a4c97e19SDavid Carlier #ifndef PR_SET_VMA
22*a4c97e19SDavid Carlier #  define PR_SET_VMA 0x53564d41
23*a4c97e19SDavid Carlier #  define PR_SET_VMA_ANON_NAME 0
24*a4c97e19SDavid Carlier #endif
25*a4c97e19SDavid Carlier 
main()260713053eSKevin Athey int main() {
270713053eSKevin Athey 
280713053eSKevin Athey   int res;
290713053eSKevin Athey   res = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, 0, 0);
300713053eSKevin Athey   if (res < 0) {
31e4085a01SUlrich Weigand     assert(errno == EINVAL || errno == ENODEV);
320713053eSKevin Athey     return 0;
330713053eSKevin Athey   }
340713053eSKevin Athey 
350713053eSKevin Athey   uint64_t cookie = 0;
360713053eSKevin Athey   res = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, 0, 0, &cookie);
370713053eSKevin Athey   if (res < 0) {
380713053eSKevin Athey     assert(errno == EINVAL);
390713053eSKevin Athey   } else {
400713053eSKevin Athey     assert(cookie != 0);
410713053eSKevin Athey   }
420713053eSKevin Athey 
43*a4c97e19SDavid Carlier   char invname[81], vlname[] = "prctl";
44*a4c97e19SDavid Carlier   for (auto i = 0; i < sizeof(invname); i++) {
45*a4c97e19SDavid Carlier     invname[i] = 0x1e;
46*a4c97e19SDavid Carlier   }
47*a4c97e19SDavid Carlier   invname[80] = 0;
48*a4c97e19SDavid Carlier   auto p =
49*a4c97e19SDavid Carlier       mmap(nullptr, 128, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
50*a4c97e19SDavid Carlier   assert(p != MAP_FAILED);
51*a4c97e19SDavid Carlier   // regardless of kernel support, the name is invalid
52*a4c97e19SDavid Carlier   res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (uintptr_t)p, 128,
53*a4c97e19SDavid Carlier               (uintptr_t)invname);
54*a4c97e19SDavid Carlier   assert(res == -1);
55*a4c97e19SDavid Carlier   assert(errno == EINVAL);
56*a4c97e19SDavid Carlier   res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (uintptr_t)p, 128,
57*a4c97e19SDavid Carlier               (uintptr_t)vlname);
58*a4c97e19SDavid Carlier   if (res < 0) {
59*a4c97e19SDavid Carlier     assert(errno == EINVAL);
60*a4c97e19SDavid Carlier   }
61*a4c97e19SDavid Carlier   munmap(p, 128);
62*a4c97e19SDavid Carlier 
630713053eSKevin Athey   return 0;
640713053eSKevin Athey }
65