| 1 | #include <cstdlib>
|
|---|
| 2 | #include <array>
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <boost/context/all.hpp>
|
|---|
| 5 |
|
|---|
| 6 | void stack_overflow(int n)
|
|---|
| 7 | {
|
|---|
| 8 | // Silence warnings about infinite recursion
|
|---|
| 9 | if (n == 0xdeadbeef) return;
|
|---|
| 10 | // Allocate more than 4k bytes on the stack.
|
|---|
| 11 | std::array<char, 8192> blob;
|
|---|
| 12 | // Repeat...
|
|---|
| 13 | stack_overflow(n + 1);
|
|---|
| 14 |
|
|---|
| 15 | // Prevent blob from being optimized away
|
|---|
| 16 | std::memcpy(blob.data(), blob.data(), n);
|
|---|
| 17 | std::cout << blob.data();
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | int main(int argc, char* argv[])
|
|---|
| 21 | {
|
|---|
| 22 | using namespace boost::context;
|
|---|
| 23 |
|
|---|
| 24 | // Calling stack_overflow outside of Boost context results in a regular stack
|
|---|
| 25 | // overflow exception.
|
|---|
| 26 | // stack_overflow(0);
|
|---|
| 27 |
|
|---|
| 28 | execution_context<int> source([](execution_context<int> sink, int) {
|
|---|
| 29 | // Calling stack_overflow inside results in a memory access violation caused
|
|---|
| 30 | // by the compiler generated _chkstk function.
|
|---|
| 31 | stack_overflow(0);
|
|---|
| 32 | return sink;
|
|---|
| 33 | });
|
|---|
| 34 | source(0);
|
|---|
| 35 | return EXIT_SUCCESS;
|
|---|
| 36 | }
|
|---|