* swapcontext and free memory
@ 2009-03-18 18:05 xcomp
2009-03-18 19:22 ` Tiago Maluta
0 siblings, 1 reply; 3+ messages in thread
From: xcomp @ 2009-03-18 18:05 UTC (permalink / raw)
To: linux-c-programming
Hi all,
I have written a small test case which simply creates a new context, swaps to it and returns to the main function. I am allocating memory for the ucontext_t structures and the stack on which the context is executed using malloc. Because of this I also want to deallocate the memory again using free. The problem is I can't deallocate the memory after swapping back. It runs only if all three free() calls are commented out. Otherwise a segmentation fault occurs. Does swapcontext handle this on its own? Can anyone explain this behavior? I didn't find anything concerning this problem in the web.
I am running this program on Ubuntu 8.04 with the following system information: Linux ubuntu8041 2.6.24-23-generic #1 SMP i686 GNU/Linux
The test program is the following one:
---
#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>
#define STACKSIZE 4096
void thread_function() {
printf("\nthread_function was called...");
}
int main(int argc, char** argv) {
printf("\nStart");
ucontext_t *main_context, *thread_context;
main_context = (ucontext*) malloc(sizeof(ucontext));
thread_context = (ucontext*) malloc(sizeof(ucontext));
void* thread_stack = malloc(STACKSIZE);
printf("\n\nAllocated memory:\n\tmain_context:\t%p\n\tthread_context:\t%p\n\tthread_stack:\t%p", main_context, thread_context, thread_stack);
getcontext(thread_context);
thread_context->uc_stack.ss_sp = thread_stack;
thread_context->uc_stack.ss_size = sizeof(thread_stack);
thread_context->uc_stack.ss_flags = 0;
thread_context->uc_flags = 0;
thread_context->uc_link = main_context;
sigemptyset(&(thread_context->uc_sigmask));
makecontext(thread_context, thread_function, 0);
swapcontext(main_context, thread_context);
printf("\n\nswapcontext() returned. Freeing memory starts now...");
free(thread_stack);
printf("\nthread_stack was deallocated...");
free(thread_context);
printf("\nthread_context was deallocated...");
free(main_context);
printf("\nmain_context was deallocated...");
printf("\n\nEnd");
}
---
Best,
Matthias :-)
Zerreißen Sie die Netze der Phisher, Hacker und Betrüger!
Ihre Internet-Sicherheits-Seiten auf Arcor.de bieten alle Infos und Hilfsmittel, die Sie zum sicheren Surfen brauchen! Play it safe!
http://www.arcor.de/footer-sicherheit/
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: swapcontext and free memory
2009-03-18 18:05 swapcontext and free memory xcomp
@ 2009-03-18 19:22 ` Tiago Maluta
2009-03-19 10:21 ` Jon Mayo
0 siblings, 1 reply; 3+ messages in thread
From: Tiago Maluta @ 2009-03-18 19:22 UTC (permalink / raw)
To: xcomp; +Cc: linux-c-programming
xcomp@arcor.de wrote:
> Hi all,
> I have written a small test case which simply creates a new context, swaps to it and returns to the main function. I am allocating memory for the ucontext_t structures and the stack on which the context is executed using malloc.
Because of this I also want to deallocate the memory again using free.
The problem is I can't deallocate the memory after swapping back. It
runs only if all three free() calls are commented out. Otherwise a
segmentation fault occurs. Does swapcontext handle this on its own? Can
anyone explain this behavior? I didn't find anything concerning this
problem in the web.
> I am running this program on Ubuntu 8.04 with the following system information: Linux
ubuntu8041 2.6.24-23-generic #1 SMP i686 GNU/Linux
(...)
> main_context = (ucontext*) malloc(sizeof(ucontext));
> thread_context = (ucontext*) malloc(sizeof(ucontext));
changing:
main_context = (ucontext_t*) malloc(sizeof(ucontext_t));
thread_context = (ucontext_t*) malloc(sizeof(ucontext_t));
and compiling with free() I got:
#./a.out
start
Allocated memory:
main_context: 0x804b008
thread_context: 0x804b168
thread_stack: 0x804b2c8
thread_function was called...
swapcontext() returned. Freeing memory starts now...
thread_stack was deallocated...
thread_context was deallocated...
main_context was deallocated...
I'm using glibc version 2.8.
--tm
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: swapcontext and free memory
2009-03-18 19:22 ` Tiago Maluta
@ 2009-03-19 10:21 ` Jon Mayo
0 siblings, 0 replies; 3+ messages in thread
From: Jon Mayo @ 2009-03-19 10:21 UTC (permalink / raw)
To: Tiago Maluta; +Cc: linux-c-programming
On Wed, Mar 18, 2009 at 12:22 PM, Tiago Maluta
<maluta_tiago@yahoo.com.br> wrote:
> xcomp@arcor.de wrote:
>> Hi all,
>> I have written a small test case which simply creates a new context, swaps to it and returns to the main function. I am allocating memory for the ucontext_t structures and the stack on which the context is executed using malloc.
> Because of this I also want to deallocate the memory again using free.
> The problem is I can't deallocate the memory after swapping back. It
> runs only if all three free() calls are commented out. Otherwise a
> segmentation fault occurs. Does swapcontext handle this on its own? Can
> anyone explain this behavior? I didn't find anything concerning this
> problem in the web.
>> I am running this program on Ubuntu 8.04 with the following system information: Linux
> ubuntu8041 2.6.24-23-generic #1 SMP i686 GNU/Linux
>
> (...)
>
>> main_context = (ucontext*) malloc(sizeof(ucontext));
>> thread_context = (ucontext*) malloc(sizeof(ucontext));
>
> changing:
>
> main_context = (ucontext_t*) malloc(sizeof(ucontext_t));
> thread_context = (ucontext_t*) malloc(sizeof(ucontext_t));
>
> and compiling with free() I got:
>
> #./a.out
> start
>
> Allocated memory:
> main_context: 0x804b008
> thread_context: 0x804b168
> thread_stack: 0x804b2c8
> thread_function was called...
>
> swapcontext() returned. Freeing memory starts now...
> thread_stack was deallocated...
> thread_context was deallocated...
> main_context was deallocated...
>
> I'm using glibc version 2.8.
>
> --tm
>
>
>
I think you should be able to free() them fine. Could you post a short
bit of source that exhibits the problem? Also I tend to mmap() some
anonymous memory instead of using malloc() for things like makecontext
and sigaltstack. Because I want to put guard pages on my stacks due to
them generally being very small.
--
Jon Mayo
<jon.mayo@gmail.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-03-19 10:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-18 18:05 swapcontext and free memory xcomp
2009-03-18 19:22 ` Tiago Maluta
2009-03-19 10:21 ` Jon Mayo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).