From 16b056dec27672e81e12075849bf4844ae3ce377 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 1 Sep 2015 10:43:32 +0100 Subject: Don't free the context's stack while it's still being executed --- libadhocutil/runtimeContext.cpp | 16 +++++++++++++--- libadhocutil/runtimeContext.h | 2 ++ libadhocutil/unittests/testContext.cpp | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/libadhocutil/runtimeContext.cpp b/libadhocutil/runtimeContext.cpp index cb938d4..4cb5a17 100644 --- a/libadhocutil/runtimeContext.cpp +++ b/libadhocutil/runtimeContext.cpp @@ -2,6 +2,7 @@ #include RuntimeContext::RuntimeContext(size_t stacksize) : + completed(false), swapped(false) { stack = malloc(stacksize); @@ -26,17 +27,26 @@ RuntimeContext::swapContext() swapcontext(&ctxInitial, &ctxCallback); } else { - if (stack) { + if (!completed) { swapcontext(&ctxCallback, &ctxInitial); + if (completed) { + free(stack); + stack = nullptr; + } } } } +bool +RuntimeContext::hasCompleted() const +{ + return completed; +} + void RuntimeContext::callbackWrapper(RuntimeContext * rc) { rc->callback(); - free(rc->stack); - rc->stack = nullptr; + rc->completed = true; } diff --git a/libadhocutil/runtimeContext.h b/libadhocutil/runtimeContext.h index aa424d4..b7ce0a3 100644 --- a/libadhocutil/runtimeContext.h +++ b/libadhocutil/runtimeContext.h @@ -11,6 +11,7 @@ class DLL_PUBLIC RuntimeContext { virtual ~RuntimeContext(); void swapContext(); + bool hasCompleted() const; protected: DLL_PRIVATE virtual void callback() = 0; @@ -21,6 +22,7 @@ class DLL_PUBLIC RuntimeContext { void * stack; ucontext_t ctxInitial; ucontext_t ctxCallback; + bool completed; bool swapped; }; diff --git a/libadhocutil/unittests/testContext.cpp b/libadhocutil/unittests/testContext.cpp index 727425e..de76493 100644 --- a/libadhocutil/unittests/testContext.cpp +++ b/libadhocutil/unittests/testContext.cpp @@ -3,7 +3,7 @@ #include "runtimeContext.h" -class TestRuntimeContext : RuntimeContext { +class TestRuntimeContext : public RuntimeContext { public: void run() { @@ -32,5 +32,6 @@ BOOST_AUTO_TEST_CASE ( basic ) TestRuntimeContext trc; trc.run(); BOOST_REQUIRE_EQUAL("aebfcd", trc.log); + BOOST_REQUIRE(trc.hasCompleted()); } -- cgit v1.2.3