oslib-posix: Use MAP_STACK in qemu_alloc_stack() on OpenBSD
PATCH 1/1 oslib-posix: Use MAP_STACK in qemu_alloc_stack() on OpenBSD
Use MAP_STACK in qemu_alloc_stack() on OpenBSD.
Added to -current and will be in our soon to be 6.4 release.
MAP_STACK Indicate that the mapping is used as a stack. This
flag must be used in combination with MAP_ANON and MAP_PRIVATE.
Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and syscall) confirm the stack register points at MAP_STACK memory, otherwise SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified to create a MAP_STACK sub-region which satisfies alignment requirements. Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the contents of the region -- there is no mprotect() equivalent operation, so there is no MAP_STACK-adding gadget.
util/oslib-posix.c +5 -1
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz) *sz += pagesz; ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS +#ifdef MAP_STACK + |MAP_STACK +#endif + ,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();
Quoted 28 lines · Brad Smith
Use
MAP_STACKinqemu_alloc_stack()on OpenBSD.Added to -current and will be in our soon to be 6.4 release.
MAP_STACKIndicate that the mapping is used as a stack. This
flag must be used in combination withMAP_ANONandMAP_PRIVATE.Implement
MAP_STACKoption formmap(). Synchronous faults (pagefault and syscall) confirm the stack register points atMAP_STACKmemory, otherwise SIGSEGV is delivered.sigaltstack()andpthread_attr_setstack()are modified to create aMAP_STACKsub-region which satisfies alignment requirements. Observe thatMAP_STACKcan only be set/cleared bymmap(), which zeroes the contents of the region -- there is nomprotect()equivalent operation, so there is no MAP_STACK-adding gadget.Signed-off-by Brad Smith <[email protected]>util/oslib-posix.c +0
-0@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz) *sz += pagesz;
util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621
Quoted 10 lines
util/oslib-posix.c +5
-1ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS +#ifdef MAP_STACK + |MAP_STACK +#endif + ,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();
Can we handle it differently, storing MAP_* flags in a variable:
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef MAP_STACK
flags |= MAP_STACK;
#endif
ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
This way it will look nicer as we won't ifdef the middle of a function call.
Quoted 42 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith
Use
MAP_STACKinqemu_alloc_stack()on OpenBSD.Added to -current and will be in our soon to be 6.4 release.
MAP_STACKIndicate that the mapping is used as a stack. This
flag must be used in combination withMAP_ANONandMAP_PRIVATE.Implement
MAP_STACKoption formmap(). Synchronous faults (pagefault and syscall) confirm the stack register points atMAP_STACKmemory, otherwise SIGSEGV is delivered.sigaltstack()andpthread_attr_setstack()are modified to create aMAP_STACKsub-region which satisfies alignment requirements. Observe thatMAP_STACKcan only be set/cleared bymmap(), which zeroes the contents of the region -- there is nomprotect()equivalent operation, so there is no MAP_STACK-adding gadget.Signed-off-by Brad Smith <[email protected]>util/oslib-posix.c +0
-0@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz) *sz += pagesz;util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621
Quoted 10 lines
util/oslib-posix.c +5
-1ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS +#ifdef MAP_STACK + |MAP_STACK +#endif + ,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();Can we handle it differently, storing MAP_* flags in a variable:
Quoted 8 lines
int flags =
MAP_PRIVATE|MAP_ANONYMOUS;
#ifdefMAP_STACK
flags |=MAP_STACK;
#endifptr =
mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);This way it will look nicer as we won't ifdef the middle of a function call.
The other nice way to handle that is to have osdep.h do #ifndef MAP_STACK
#define MAP_STACK 0
#endif
and then you can just unconditionally use MAP_STACK in your expression for the mmap flags.
I note that Linux also defines a MAP_STACK, about which the manpage says:
MAP_STACK (since Linux 2.6.27)
Allocate the mapping at an address suitable for a
process or thread stack.
This flag is currently a no-op, but is used in the
glibc threading imple‐
mentation so that if some architectures require special
treatment for stack
allocations, support can later be transparently
implemented for glibc.
So this patch would be opting Linux QEMU builds into whatever that potential future behaviour change is. That sounds I guess like it's more likely to be the right thing than the wrong thing, but it would be useful if some Linux expert could confirm...
thanks
-- PMM
Quoted 50 lines · Peter Maydell
Quoted 49 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith
Use
MAP_STACKinqemu_alloc_stack()on OpenBSD.Added to -current and will be in our soon to be 6.4 release.
MAP_STACKIndicate that the mapping is used as a stack. This
flag must be used in combination withMAP_ANONandMAP_PRIVATE.Implement
MAP_STACKoption formmap(). Synchronous faults (pagefault and syscall) confirm the stack register points atMAP_STACKmemory, otherwise SIGSEGV is delivered.sigaltstack()andpthread_attr_setstack()are modified to create aMAP_STACKsub-region which satisfies alignment requirements. Observe thatMAP_STACKcan only be set/cleared bymmap(), which zeroes the contents of the region -- there is nomprotect()equivalent operation, so there is no MAP_STACK-adding gadget.Signed-off-by Brad Smith <[email protected]>util/oslib-posix.c +0
-0@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz) *sz += pagesz;util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621
Quoted 10 lines
util/oslib-posix.c +5
-1ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS +#ifdef MAP_STACK + |MAP_STACK +#endif + ,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();Can we handle it differently, storing MAP_* flags in a variable:
int flags =
MAP_PRIVATE|MAP_ANONYMOUS;
#ifdefMAP_STACK
flags |=MAP_STACK;
#endifptr =
mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
Quoted 9 lines
Quoted 1 line
This way it will look nicer as we won't ifdef the middle of a function call.
The other nice way to handle that is to have
osdep.hdo #ifndefMAP_STACK
#defineMAP_STACK0
#endif
and then you can just unconditionally useMAP_STACKin your expression for the mmap flags.
I assume that this is a cleaner solution.
Quoted 13 lines
I note that Linux also defines a
MAP_STACK, about which the manpage says:
MAP_STACK(since Linux 2.6.27)
Allocate the mapping at an address suitable for a
process or thread stack.
This flag is currently a no-op, but is used in the
glibc threading imple‐
mentation so that if some architectures require special
treatment for stack
allocations, support can later be transparently
implemented for glibc.
Quoted 8 lines
So this patch would be opting Linux QEMU builds into whatever that potential future behaviour change is. That sounds I guess like it's more likely to be the right thing than the wrong thing, but it would be useful if some Linux expert could confirm...
thanks
-- PMM
There is a similar description on NetBSD:
MAP_STACK
Allocate a memory segment that can be used either for a process or thread stack. This currently has no effect, but its use is reserved for architectures that might require special treatment of that address space. Unimplemented.
Apparently OpenBSD started to overload it for some hardening.
Quoted 84 lines · Kamil Rytarowski
Quoted 49 lines · Peter Maydell
Quoted 48 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith
Use
MAP_STACKinqemu_alloc_stack()on OpenBSD.Added to -current and will be in our soon to be 6.4 release.
MAP_STACKIndicate that the mapping is used as a stack. This
flag must be used in combination withMAP_ANONandMAP_PRIVATE.Implement
MAP_STACKoption formmap(). Synchronous faults (pagefault and syscall) confirm the stack register points atMAP_STACKmemory, otherwise SIGSEGV is delivered.sigaltstack()andpthread_attr_setstack()are modified to create aMAP_STACKsub-region which satisfies alignment requirements. Observe thatMAP_STACKcan only be set/cleared bymmap(), which zeroes the contents of the region -- there is nomprotect()equivalent operation, so there is no MAP_STACK-adding gadget.Signed-off-by Brad Smith <[email protected]>util/oslib-posix.c +0
-0@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz) *sz += pagesz;util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621
Quoted 10 lines
util/oslib-posix.c +5
-1ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS +#ifdef MAP_STACK + |MAP_STACK +#endif + ,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();Can we handle it differently, storing MAP_* flags in a variable:
int flags =
MAP_PRIVATE|MAP_ANONYMOUS;
#ifdefMAP_STACK
flags |=MAP_STACK;
#endifptr =
mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);Quoted 8 lines
Quoted 1 line
This way it will look nicer as we won't ifdef the middle of a function call.
The other nice way to handle that is to have
osdep.hdo #ifndefMAP_STACK
#defineMAP_STACK0
#endif
and then you can just unconditionally useMAP_STACKin your expression for the mmap flags.I assume that this is a cleaner solution.
Quoted 12 lines
I note that Linux also defines a
MAP_STACK, about which the manpage says:
MAP_STACK(since Linux 2.6.27)
Allocate the mapping at an address suitable for a
process or thread stack.
This flag is currently a no-op, but is used in the
glibc threading imple‐
mentation so that if some architectures require special
treatment for stack
allocations, support can later be transparently
implemented for glibc.Quoted 8 lines
So this patch would be opting Linux QEMU builds into whatever that potential future behaviour change is. That sounds I guess like it's more likely to be the right thing than the wrong thing, but it would be useful if some Linux expert could confirm...
thanks
-- PMMThere is a similar description on NetBSD:
MAP_STACK
Quoted 8 lines
Allocate a memory segment that can be used either for a process or thread stack. This currently has no effect, but its use is reserved for architectures that might require special treatment of that address space. Unimplemented.
Apparently OpenBSD started to overload it for some hardening.
And from FreeBSD...
MAP_STACK MAP_STACK implies MAP_ANON, and /offset/ of 0. The/fd/
argument must be -1 and /prot/ must include at least
PROT_READ and PROT_WRITE.
This option creates a memory region that grows to at
most/len/ bytes in size, starting from the stack top
and growing down. The stack top is the starting
address returned by the call, plus/len/ bytes. The
bottom of the stack at maximum growth is the starting
address returned by the call.
Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping
the whole stack in advance.
Quoted 13 lines · Brad Smith
And from FreeBSD...
MAP_STACKMAP_STACKimpliesMAP_ANON, and offset of 0. The fd
argument must be -1 and prot must include at leastPROT_READandPROT_WRITE.This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.
Quoted 4 lines
Stacks created with
MAP_STACKautomatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.
Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(
thanks
-- PMM
Quoted 14 lines · Peter Maydell
Quoted 13 lines · Brad Smith
And from FreeBSD...
MAP_STACKMAP_STACKimpliesMAP_ANON, and offset of 0. The fd
argument must be -1 and prot must include at leastPROT_READandPROT_WRITE.This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.
Quoted 9 lines
Quoted 4 lines
Stacks created with
MAP_STACKautomatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(
I propose to restrict MAP_STACK it to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.
Quoted 3 lines
thanks
-- PMM
Quoted 19 lines · Kamil Rytarowski
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Brad Smith
And from FreeBSD...
MAP_STACKMAP_STACKimpliesMAP_ANON, and offset of 0. The fd
argument must be -1 and prot must include at leastPROT_READandPROT_WRITE.This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.
Stacks created with
MAP_STACKautomatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.
Quoted 7 lines
Quoted 4 lines
Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(
I propose to restrict
MAP_STACKit to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.
I was going to propose doing something like that but you had replied before I did. What sort of comment did you have in mind?
Quoted 25 lines · Brad Smith
Quoted 23 lines · Kamil Rytarowski
Quoted 22 lines · Peter Maydell
Quoted 13 lines · Brad Smith
And from FreeBSD...
MAP_STACKMAP_STACKimpliesMAP_ANON, and offset of 0. The fd argument must be -1 and prot must include at leastPROT_READandPROT_WRITE.This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.
Quoted 4 lines
Stacks created with
MAP_STACKautomatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(
Quoted 8 lines
Quoted 3 lines
I propose to restrict
MAP_STACKit to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.I was going to propose doing something like that but you had replied before I did.
What sort of comment did you have in mind?
Why do we want it only on OpenBSD and its either unneeded or meaning something else on other OSes.
#ifdef __OpenBSD__
flags |= MAP_STACK;
#endif
Quoted 35 lines · Kamil Rytarowski
Quoted 25 lines · Brad Smith
Quoted 23 lines · Kamil Rytarowski
Quoted 22 lines · Peter Maydell
Quoted 13 lines · Brad Smith
And from FreeBSD...
??????????
MAP_STACKMAP_STACKimpliesMAP_ANON, and offset of 0.?? The fd argument must be -1 and prot must include at leastPROT_READandPROT_WRITE.This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down.?? The stack top is the starting address returned by the call, plus len bytes.?? The bottom of the stack at maximum growth is the starting address returned by the call.
Quoted 4 lines
Stacks created with
MAP_STACKautomatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(
Quoted 8 lines
Quoted 3 lines
I propose to restrict
MAP_STACKit to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.I was going to propose doing something like that but you had replied before I did.
What sort of comment did you have in mind?
Quoted 6 lines
Why do we want it only on OpenBSD and its either unneeded or meaning something else on other OSes.
#ifdef
__OpenBSD__
flags |=MAP_STACK;
#endif
How about the following?
util/oslib-posix.c +10 -2
@@ -596,6 +596,7 @@ pid_t qemu_fork(Error **errp)void *qemu_alloc_stack (size_t *sz) {void *ptr, *guardpage; +int flags;#ifdef CONFIG_DEBUG_STACK_USAGE void *ptr2;#endif
@@ -610,8 +611,15 @@ void *qemu_alloc_stack(size_t *sz)/* allocate one extra page for the guard page */ *sz += pagesz; - ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + flags =MAP_PRIVATE |MAP_ANONYMOUS ; +#if defined(MAP_STACK ) && defined(__OpenBSD__) +/* Only enable MAP_STACK on OpenBSD. Other OS's such +as Linux/FreeBSD/NetBSD have a flag with the same +name but have differing functionality. */ + flags |=MAP_STACK ; +#endif + + ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , flags,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();
Kamil Rytarowski
Reviewed-byQuoted 36 lines · Brad Smith
Quoted 35 lines · Kamil Rytarowski
Quoted 33 lines · Brad Smith
Quoted 19 lines · Kamil Rytarowski
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Brad Smith
And from FreeBSD...
??????????
MAP_STACKMAP_STACKimpliesMAP_ANON, and offset of 0.?? The fd argument must be -1 and prot must include at leastPROT_READandPROT_WRITE.This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down.?? The stack top is the starting address returned by the call, plus len bytes.?? The bottom of the stack at maximum growth is the starting address returned by the call.
Stacks created with
MAP_STACKautomatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.Quoted 7 lines
Quoted 4 lines
Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(
I propose to restrict
MAP_STACKit to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.I was going to propose doing something like that but you had replied before I did.
What sort of comment did you have in mind?
Quoted 9 lines
Quoted 6 lines
Why do we want it only on OpenBSD and its either unneeded or meaning something else on other OSes.
#ifdef
__OpenBSD__
flags |=MAP_STACK;
#endifHow about the following?
It looks good to me.
Quoted 32 lines
util/oslib-posix.c +10
-2@@ -596,6 +596,7 @@ pid_t qemu_fork(Error **errp)void *qemu_alloc_stack (size_t *sz) {void *ptr, *guardpage; +int flags;#ifdef CONFIG_DEBUG_STACK_USAGE void *ptr2;#endif @@ -610,8 +611,15 @@ void *qemu_alloc_stack(size_t *sz)/* allocate one extra page for the guard page */ *sz += pagesz; - ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + flags =MAP_PRIVATE |MAP_ANONYMOUS ; +#if defined(MAP_STACK ) && defined(__OpenBSD__) +/* Only enable MAP_STACK on OpenBSD. Other OS's such +as Linux/FreeBSD/NetBSD have a flag with the same +name but have differing functionality. */ + flags |=MAP_STACK ; +#endif + + ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , flags,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();
Quoted 42 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith
Use
MAP_STACKinqemu_alloc_stack()on OpenBSD.Added to -current and will be in our soon to be 6.4 release.
MAP_STACKIndicate that the mapping is used as a stack. This
flag must be used in combination withMAP_ANONandMAP_PRIVATE.Implement
MAP_STACKoption formmap(). Synchronous faults (pagefault and syscall) confirm the stack register points atMAP_STACKmemory, otherwise SIGSEGV is delivered.sigaltstack()andpthread_attr_setstack()are modified to create aMAP_STACKsub-region which satisfies alignment requirements. Observe thatMAP_STACKcan only be set/cleared bymmap(), which zeroes the contents of the region -- there is nomprotect()equivalent operation, so there is no MAP_STACK-adding gadget.Signed-off-by Brad Smith <[email protected]>util/oslib-posix.c +0
-0@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz) *sz += pagesz;util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621
Quoted 10 lines
util/oslib-posix.c +5
-1ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS +#ifdef MAP_STACK + |MAP_STACK +#endif + ,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();Can we handle it differently, storing MAP_* flags in a variable:
Quoted 8 lines
int flags =
MAP_PRIVATE|MAP_ANONYMOUS;
#ifdefMAP_STACK
flags |=MAP_STACK;
#endifptr =
mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);This way it will look nicer as we won't ifdef the middle of a function call.
How about the following?
util/oslib-posix.c +7 -2
@@ -596,6 +596,7 @@ pid_t qemu_fork(Error **errp)void *qemu_alloc_stack (size_t *sz) {void *ptr, *guardpage; +int flags;#ifdef CONFIG_DEBUG_STACK_USAGE void *ptr2;#endif
@@ -610,8 +611,12 @@ void *qemu_alloc_stack(size_t *sz)/* allocate one extra page for the guard page */ *sz += pagesz; - ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + flags =MAP_PRIVATE |MAP_ANONYMOUS ; +#ifdef MAP_STACK + flags |=MAP_STACK ; +#endif + + ptr =mmap (NULL , *sz,PROT_READ |PROT_WRITE , flags,-1 ,0 );if (ptr ==MAP_FAILED ) {perror ("failed to allocate memory for stack" );abort ();