]> git.ipfire.org Git - thirdparty/gcc.git/blame - libphobos/testsuite/libphobos.thread/fiber_guard_page.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / libphobos / testsuite / libphobos.thread / fiber_guard_page.d
CommitLineData
81e07d42 1// { dg-options "-O0" }
b4c522fa
IB
2// { dg-shouldfail "segv or bus error" }
3import core.thread;
6eae7549 4import core.sys.posix.signal;
b4c522fa
IB
5import core.sys.posix.sys.mman;
6
5fee5ec3
IB
7version (LDC) import ldc.attributes;
8else struct optStrategy { string a; }
9
b4c522fa
IB
10// this should be true for most architectures
11// (taken from core.thread)
6eae7549
IB
12version (GNU_StackGrowsDown)
13 version = StackGrowsDown;
b4c522fa 14
6eae7549 15enum stackSize = MINSIGSTKSZ;
b4c522fa
IB
16
17// Simple method that causes a stack overflow
5fee5ec3 18@optStrategy("none")
b4c522fa
IB
19void stackMethod()
20{
21 // Over the stack size, so it overflows the stack
22 int[stackSize/int.sizeof+100] x;
23}
24
25void main()
26{
05b6520e 27 auto test_fiber = new Fiber(&stackMethod, stackSize, stackSize);
b4c522fa
IB
28
29 // allocate a page below (above) the fiber's stack to make stack overflows possible (w/o segfaulting)
30 version (StackGrowsDown)
31 {
92dd3e71 32 auto stackBottom = __traits(getMember, test_fiber, "m_pmem");
b4c522fa
IB
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 {
92dd3e71
IB
39 auto m_sz = __traits(getMember, test_fiber, "m_sz");
40 auto m_pmem = __traits(getMember, test_fiber, "m_pmem");
b4c522fa
IB
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}