diff --git a/example/callcc/Jamfile.v2 b/example/callcc/Jamfile.v2 index 942dee1..e361142 100644 --- a/example/callcc/Jamfile.v2 +++ b/example/callcc/Jamfile.v2 @@ -70,6 +70,10 @@ exe endless_loop : endless_loop.cpp ; +exe stack_alignment + : stack_alignment.cpp + ; + #exe backtrace # : backtrace.cpp # ; diff --git a/example/callcc/stack_alignment.cpp b/example/callcc/stack_alignment.cpp new file mode 100644 index 0000000..9818ab0 --- /dev/null +++ b/example/callcc/stack_alignment.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +void stack_overflow(int n) +{ + // Silence warnings about infinite recursion + if (n == 0xdeadbeef) return; + // Allocate more than 4k bytes on the stack. + std::array blob; + // Repeat... + stack_overflow(n + 1); + + // Prevent blob from being optimized away + std::memcpy(blob.data(), blob.data(), n); + std::cout << blob.data(); +} + +int main(int argc, char* argv[]) +{ + using namespace boost::context; + + // Calling stack_overflow outside of Boost context results in a regular stack + // overflow exception. + // stack_overflow(0); + + callcc([](continuation && sink) { + // Calling stack_overflow inside results in a memory access violation caused + // by the compiler generated _chkstk function. + stack_overflow(0); + return std::move( sink); + }); + return EXIT_SUCCESS; +}