From 0949f5b342187849b90b94accc7271188ea2eabf Mon Sep 17 00:00:00 2001 From: Thomas Veerman Date: Fri, 10 Feb 2012 09:16:00 +0000 Subject: [PATCH] libmthread: don't free() user allocated stack space --- lib/libmthread/allocate.c | 7 ++++--- test/test59.c | 10 +++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/libmthread/allocate.c b/lib/libmthread/allocate.c index c16fe40e3..2ecd5ae05 100644 --- a/lib/libmthread/allocate.c +++ b/lib/libmthread/allocate.c @@ -422,9 +422,10 @@ mthread_thread_t thread; rt->m_arg = NULL; rt->m_result = NULL; rt->m_cond = NULL; - if (rt->m_context.uc_stack.ss_sp) { - free(rt->m_context.uc_stack.ss_sp); /* Free allocated stack */ - rt->m_context.uc_stack.ss_sp = NULL; + if (rt->m_attr.ma_stackaddr == NULL) { /* We allocated stack space */ + if (rt->m_context.uc_stack.ss_sp) + free(rt->m_context.uc_stack.ss_sp); /* Free allocated stack */ + rt->m_context.uc_stack.ss_sp = NULL; } rt->m_context.uc_stack.ss_size = 0; rt->m_context.uc_link = NULL; diff --git a/test/test59.c b/test/test59.c index e15c4b958..90b627cba 100644 --- a/test/test59.c +++ b/test/test59.c @@ -633,9 +633,6 @@ PRIVATE void test_attributes(void) if (stackaddr != newstackaddr) err(11, 14); if (stacksize != newstacksize) err(11, 15); if (mthread_attr_destroy(&tattr) != 0) err(11, 16); - /* Freeing the stack. Note that this is only possible because it wasn't - * actually used yet by a thread. If it was, mthread would clean it up after - * usage and this free would do something undefined. */ free(newstackaddr); /* Try to allocate too small a stack; it should fail and the attribute @@ -652,7 +649,6 @@ PRIVATE void test_attributes(void) if (stackaddr == newstackaddr) err(11, 22); if (stacksize == newstacksize) err(11, 23); if (mthread_attr_destroy(&tattr) != 0) err(11, 24); - /* Again, freeing because we can. Shouldn't do it if it was actually used. */ free(newstackaddr); /* Tell attribute to let the system allocate a stack for the thread and only @@ -742,9 +738,8 @@ PRIVATE void test_attributes(void) if (mthread_mutex_unlock(condition_mutex) != 0) err(11, 65); /* Verify stack hypothesis; we assume a stack is used from the top and grows - * downwards. At this point the stack should still exist, because we haven't - * 'joined' yet. After joining, the stack is cleaned up and this test becomes - * useless. */ + * downwards. + */ #if (_MINIX_CHIP == _CHIP_INTEL) if (stackp[0] != MAGIC) err(11, 66); /* End of the stack */ for (i = no_ints - 1 - 16; i < no_ints; i++) @@ -760,6 +755,7 @@ PRIVATE void test_attributes(void) if (mthread_attr_destroy(&tattr) != 0) err(11, 71); if (mthread_mutex_destroy(condition_mutex) != 0) err(11, 72); if (mthread_cond_destroy(&condition) != 0) err(11, 73); + free(stackaddr); #ifdef MDEBUG mthread_verify();