Debian Codegraph @

libphobos/testsuite/libphobos.thread/fiber_guard_page.d

raw
1 // { dg-options "-O0" }
2 // { dg-shouldfail "segv or bus error" }
3 import core.thread;
4 import core.sys.posix.signal : MINSIGSTKSZ;
5 import core.sys.posix.sys.mman : MAP_ANON, MAP_PRIVATE, mmap, PROT_READ, PROT_WRITE;
6
7 version (LDC) import ldc.attributes;
8 else struct optStrategy { string a; }
9
10 // this should be true for most architectures
11 // (taken from core.thread)
12 version (GNU_StackGrowsDown)
13 version = StackGrowsDown;
14
15 enum stackSize = MINSIGSTKSZ;
16
17 // Simple method that causes a stack overflow
18 @optStrategy("none")
19 void stackMethod()
20 {
21 // Over the stack size, so it overflows the stack
22 int[stackSize/int.sizeof+100] x;
23 }
24
25 void main()
26 {
27 auto test_fiber = new Fiber(&stackMethod, stackSize, stackSize);
28
29 // allocate a page below (above) the fiber's stack to make stack overflows possible (w/o segfaulting)
30 version (StackGrowsDown)
31 {
32 auto stackBottom = __traits(getMember, test_fiber, "m_pmem");
33 auto p = mmap(stackBottom - 8 * stackSize, 8 * stackSize,
34 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
35 assert(p !is null, "failed to allocate page");
36 }
37 else
38 {
39 auto m_sz = __traits(getMember, test_fiber, "m_sz");
40 auto m_pmem = __traits(getMember, test_fiber, "m_pmem");
41
42 auto stackTop = m_pmem + m_sz;
43 auto p = mmap(stackTop, 8 * stackSize,
44 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
45 assert(p !is null, "failed to allocate page");
46 }
47
48 // the guard page should prevent a mem corruption by stack
49 // overflow and cause a segfault instead (or generate SIGBUS on *BSD flavors)
50 test_fiber.call();
51 }