Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r80354 - in trunk/libs/context: doc src src/asm
From: oliver.kowalke_at_[hidden]
Date: 2012-09-01 18:56:54


Author: olli
Date: 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
New Revision: 80354
URL: http://svn.boost.org/trac/boost/changeset/80354

Log:
context: correct stack frames on arm, mips, ppc
 - alignment of stack frame for arm, mips, ppc
 - stack::allocate() returns address of stack begin

Text files modified:
   trunk/libs/context/doc/fcontext.qbk | 15 +++++++------
   trunk/libs/context/doc/stack.qbk | 4 +++
   trunk/libs/context/src/asm/fcontext_arm_aapcs_elf_gas.S | 16 +++++++-------
   trunk/libs/context/src/asm/fcontext_i386_ms_pe_masm.asm | 20 ++++++++++--------
   trunk/libs/context/src/asm/fcontext_i386_sysv_elf_gas.S | 4 --
   trunk/libs/context/src/asm/fcontext_i386_sysv_macho_gas.S | 4 --
   trunk/libs/context/src/asm/fcontext_mips32_o32_elf_gas.S | 43 ++++++++++++++++++++++-----------------
   trunk/libs/context/src/asm/fcontext_ppc32_sysv_elf_gas.S | 41 +++++++++++++++++++++----------------
   trunk/libs/context/src/asm/fcontext_ppc64_sysv_elf_gas.S | 39 ++++++++++++++++++++---------------
   trunk/libs/context/src/asm/fcontext_x86_64_ms_pe_masm.asm | 22 +++++++++++---------
   trunk/libs/context/src/asm/fcontext_x86_64_sysv_elf_gas.S | 4 --
   trunk/libs/context/src/asm/fcontext_x86_64_sysv_macho_gas.S | 4 --
   trunk/libs/context/src/stack_allocator_posix.cpp | 8 +++++-
   trunk/libs/context/src/stack_allocator_windows.cpp | 8 +++++-
   14 files changed, 128 insertions(+), 104 deletions(-)

Modified: trunk/libs/context/doc/fcontext.qbk
==============================================================================
--- trunk/libs/context/doc/fcontext.qbk (original)
+++ trunk/libs/context/doc/fcontext.qbk 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -35,7 +35,7 @@
 
             // creates and manages a protected stack (with guard page)
             ctx::stack_allocator alloc;
- fc.fc_stack.base = alloc.allocate(ctx::minimum_stacksize());
+ fc.fc_stack.sp = alloc.allocate(ctx::minimum_stacksize());
             fc.fc_stack.size = ctx::minimum_stacksize();
 
             // context fc uses f() as context function
@@ -77,11 +77,11 @@
         {
                 ctx::stack_allocator alloc1, alloc2;
 
- fc1.fc_stack.base = alloc1.allocate(ctx::minimum_stacksize());
+ fc1.fc_stack.sp = alloc1.allocate(ctx::minimum_stacksize());
                 fc1.fc_stack.size = ctx::minimum_stacksize();
                 ctx::make_fcontext( & fc1, f1);
 
- fc2.fc_stack.base = alloc2.allocate(ctx::minimum_stacksize());
+ fc2.fc_stack.sp = alloc2.allocate(ctx::minimum_stacksize());
                 fc2.fc_stack.size = ctx::minimum_stacksize();
                 ctx::make_fcontext( & fc2, f2);
 
@@ -144,7 +144,7 @@
         {
             ctx::stack_allocator alloc;
 
- fc1.fc_stack.base = alloc.allocate(ctx::minimum_stacksize());
+ fc1.fc_stack.sp = alloc.allocate(ctx::minimum_stacksize());
             fc1.fc_stack.size = ctx::minimum_stacksize();
             fc1.fc_link = & fcm;
             pair_t p( std::make_pair( 2, 7) );
@@ -195,7 +195,7 @@
 
         struct stack_t
         {
- void * base;
+ void * sp;
             std::size_t size;
         };
 
@@ -209,9 +209,10 @@
         intptr_t jump_fcontext( fcontext_t * ofc, fcontext_t const* nfc, intptr_t vp);
         void make_fcontext( fcontext_t * fc, void(* fn)(intptr_t) );
 
-[heading `base`]
+[heading `sp`]
 [variablelist
-[[Member:] [Pointer to the top of the stack.]]
+[[Member:] [Pointer to the beginning of the stack (depending of the architecture the stack grows
+downwards or upwards).]]
 ]
 
 [heading `size`]

Modified: trunk/libs/context/doc/stack.qbk
==============================================================================
--- trunk/libs/context/doc/stack.qbk (original)
+++ trunk/libs/context/doc/stack.qbk 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -45,6 +45,10 @@
 [note The stack is not required to be aligned; alignment takes place inside
 `make_fcontext()`.]
 
+[note Depending on the architecture `allocate()` returns an address from the
+top of the stack (growing downwards) or the bottom of the stack (growing
+upwards).]
+
 
 [section:stack_allocator Class `stack_allocator`]
 

Modified: trunk/libs/context/src/asm/fcontext_arm_aapcs_elf_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_arm_aapcs_elf_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_arm_aapcs_elf_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -78,25 +78,25 @@
 .type make_fcontext,%function
 make_fcontext:
     str a2, [a1,#40] @ save address of context function
- ldr a2, [a1,#44] @ load address of context stack base
- ldr a3, [a1,#48] @ load size of context stack
- add a2, a3 @ compute start address of context stack
+ ldr a2, [a1,#44] @ load address of context stack pointer (base)
 
- push {a1,lr} @ save pointer to fcontext_t
+ push {a1,lr} @ save pointer to fcontext_t and return address, SP % 8 == 0
     mov a1, a2 @ context stack pointer as arg for align_stack
     bl align_stack_at_PLT @ call align_stack
     mov a2, a1 @ begin of aligned context stack
- pop {a1,lr} @ restore pointer to fcontext_t
+ pop {a1,lr} @ restore pointer to fcontext_t and return address
 
- str a2, [a1,#32] @ save the aligned context stack base
+ str a2, [a1,#32] @ save address in A2 as stack pointer for context function
 
- adr a2, finish @ address of finish; called after context function returns
- str a2, [a1,#36]
+ adr a2, finish @ compute abs address of label finish
+ str a2, [a1,#36] @ save address of finish as return address for context function
+ @ entered after context function returns
 
     mov a1, #0
     bx lr
 
 finish:
+ @ SP points to same address as SP on entry of context function
     mov a1, #0 @ exit code is zero
     bl _exit_at_PLT @ exit application
 .size make_fcontext,.-make_fcontext

Modified: trunk/libs/context/src/asm/fcontext_i386_ms_pe_masm.asm
==============================================================================
--- trunk/libs/context/src/asm/fcontext_i386_ms_pe_masm.asm (original)
+++ trunk/libs/context/src/asm/fcontext_i386_ms_pe_masm.asm 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -16,7 +16,7 @@
 ; --------------------------------------------------------------
 ; | 018h | 01ch | 020h | |
 ; --------------------------------------------------------------
-; | sp | size | base | |
+; | sp | size | limit | |
 ; --------------------------------------------------------------
 ; --------------------------------------------------------------
 ; | 9 | |
@@ -61,9 +61,9 @@
     mov eax, [edx] ; load current SEH exception list
     mov [ecx+024h], eax ; save current exception list
     mov eax, [edx+04h] ; load current stack base
- mov [ecx+020h], eax ; save current stack base
+ mov [ecx+018h], eax ; save current stack base
     mov eax, [edx+08h] ; load current stack limit
- mov [ecx+018h], eax ; save current stack limit
+ mov [ecx+020h], eax ; save current stack limit
     mov eax, [edx+010h] ; load fiber local storage
     mov [ecx+028h], eax ; save fiber local storage
 
@@ -93,9 +93,9 @@
     assume fs:error
     mov eax, [ecx+024h] ; load SEH exception list
     mov [edx], eax ; restore next SEH item
- mov eax, [ecx+020h] ; load stack base
+ mov eax, [ecx+018h] ; load stack base
     mov [edx+04h], eax ; restore stack base
- mov eax, [ecx+018h] ; load stack limit
+ mov eax, [ecx+020h] ; load stack limit
     mov [edx+08h], eax ; restore stack limit
     mov eax, [ecx+028h] ; load fiber local storage
     mov [edx+010h], eax ; restore fiber local storage
@@ -117,10 +117,12 @@
     mov eax, [ebp+08h] ; load address of fcontext_t
     mov ecx, [ebp+0ch] ; load address of context function
     mov [eax+014h], ecx ; save address of context function
- mov edx, [eax+018h] ; load address of context stack (limit)
+ mov edx, [eax+018h] ; load address of context stack (base)
     mov ecx, [eax+01ch] ; load context stack size
- lea edx, [edx+ecx] ; compute top address of context stack (base)
- mov [eax+020h], edx ; save top address of context stack (base)
+ neg ecx ; negate stack size for LEA instruction (== substraction)
+ lea edx, [edx+ecx] ; compute top address of context stack
+ mov [eax+020h], edx ; save bottom address of context stack (limit)
+ mov edx, [eax+018h] ; load address of context stack (base)
 
     mov [esp+04h], eax ; save pointer to fcontext_t
     mov [esp], edx ; context stack as arg for align_stack
@@ -152,7 +154,7 @@
     ret
 
 finish:
- ; ESP == stack pointer of context function + 0x4
+ ; ESP points to same address as ESP on entry of context function + 0x4
     xor eax, eax
     mov [esp], eax ; exit code is zero
     call _exit ; exit application

Modified: trunk/libs/context/src/asm/fcontext_i386_sysv_elf_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_i386_sysv_elf_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_i386_sysv_elf_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -84,8 +84,6 @@
     movl 0xc(%ebp), %ecx /* load address of context function */
     movl %ecx, 0x14(%eax) /* save address of context function */
     movl 0x18(%eax), %edx /* load address of context stack base */
- movl 0x1c(%eax), %ecx /* load size of context stack */
- leal (%edx,%ecx), %edx /* compute top address of context stack */
 
     movl %ebx, 0x8(%esp) /* save EBX */
     movl %eax, 0x4(%esp) /* save pointer to fcontext_t */
@@ -117,7 +115,7 @@
     ret
 
 finish:
- /* ESP == stack pointer of context function + 0x4 */
+ /* ESP points to same address as ESP on entry of context function + 0x4 */
     call 3f
 3: popl %ebx /* address of label 3 */
     addl $_GLOBAL_OFFSET_TABLE_+[.-3b], %ebx /* compute address of GOT and store it in EBX */

Modified: trunk/libs/context/src/asm/fcontext_i386_sysv_macho_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_i386_sysv_macho_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_i386_sysv_macho_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -81,8 +81,6 @@
     movl 0xc(%ebp), %ecx /* load address of context function */
     movl %ecx, 0x14(%eax) /* save address of context function */
     movl 0x18(%eax), %edx /* load address of context stack base */
- movl 0x1c(%eax), %ecx /* load size of context stack */
- leal (%edx,%ecx), %edx /* compute top address of context stack */
 
     movl %eax, 0x4(%esp) /* save pointer to fcontext_t */
     movl %edx, (%esp) /* context stack pointer as arg for align_stack */
@@ -109,7 +107,7 @@
     ret
 
 finish:
- /* ESP == stack pointer of context function + 0x4 */
+ /* ESP points to same address as ESP on entry of context function + 0x4 */
     xorl %eax, %eax
     movl %eax, (%esp) /* exit code is zero */
     call _exit /* exit application */

Modified: trunk/libs/context/src/asm/fcontext_mips32_o32_elf_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_mips32_o32_elf_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_mips32_o32_elf_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -95,6 +95,7 @@
 
     lw $t9, 96($a1) # load PC
     jr $t9 # jump to context
+ nop
 .end jump_fcontext
 .size jump_fcontext, .-jump_fcontext
 
@@ -109,37 +110,41 @@
 .cpload $t9
 .set reorder
 #endif
+ addiu $sp, $sp, -40 # allocate stack space (contains shadow space for subroutines)
+ sw $ra, 36($sp) # save return address
+
     sw $gp, 24($a0) # save global pointer
     sw $a1, 96($a0) # save address of context function
- lw $t0, 104($a0) # load address of context stack base
- lw $t1, 112($a0) # load size of context stack
- add $t0, $t1, $t0 # compute start address of context stack
-
- sub $sp, $sp, 28
- sw $ra, 24($sp)
- sw $a0, 20($sp)
+ lw $t0, 104($a0) # load address of context stack pointer (base)
+
+ sw $a0, 28($sp) # save pointer to fcontext_t
     move $a0, $t0 # context stack pointer as arg for align_stack
     lw $t9, %call16(align_stack)($gp) # address of align_stack
     jalr $t9 # call align_stack
- nop
     move $t0, $v0 # begin of aligned context stack
- lw $ra, 24($sp)
- lw $a0, 20($sp)
- addi $sp, $sp, 28
+ lw $a0, 28($sp) # restore pointer to fcontext_t
+
+ addiu $t0, $t0, -40 # reserve 16 byte (includes 4 byte shadow space), T0 % 16 == 0
+ sw $t0, 72($a0) # save address in T0 as stack pointer for context function, SP % 16 == 0
 
- sub $t0, $t0, 16 # reserve 16 byte of argument space
- sw $t0, 72($a0) # save the algned stack base
+ la $t9, finish # compute abs address of label finish
+ sw $t9, 88($a0) # save address of finish as return address for context function
+ # entered after context function returns
 
- la $t9, finish # helper code executed after context function returns
- sw $t9, 88($a0)
+ lw $ra, 36($sp) # restore return address
+ addiu $sp, $sp, 40 # deallocate stack space
 
     move $v0, $zero
     jr $ra
 
 finish:
- move $gp, $s3 # restore GP (global pointer)
- move $a0, $zero # exit code is zero
- lw $t9, %call16(_exit)($gp) # exit application
- jalr $t9
+ # SP points to same adddress as SP on entry of context function
+ addiu $sp, $sp, -32 # allocate stack space (contains shadow space for subroutines)
+ sw $ra, 28($sp) # save return address
+
+ move $gp, $s3 # restore GP (global pointer)
+ move $a0, $zero # exit code is zero
+ lw $t9, %call16(_exit)($gp) # address of exit
+ jalr $t9 # exit application
 .end make_fcontext
 .size make_fcontext, .-make_fcontext

Modified: trunk/libs/context/src/asm/fcontext_ppc32_sysv_elf_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_ppc32_sysv_elf_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_ppc32_sysv_elf_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -184,40 +184,45 @@
 .align 2
 .type make_fcontext,@function
 make_fcontext:
+ mflr %r0 # save return address into R0
+ stw %r0, 4(%r1) # save return address on stack, set up stack frame
+ stwu %r1, -32(%r1) # allocate stack space, SP % 16 == 0
+
     stw %r4, 88(%r3) # save address of context function
- lwz %r0, 92(%r3) # load address of context stack base
- lwz %r4, 96(%r3) # load size of context stack
- add %r0, %r4, %r0 # compute start address of context stack
-
- li %r4, 28
- subf %r1, %r4, %r1 # reserve space on stack
- stw %r3, 24(%r1) # store pointer to fcontext_t on stack
- mflr %r4 # load LR
- stw %r4, 20(%r1) # store LR on stack
+ lwz %r0, 92(%r3) # load address of context stack pointer (base)
+
+ stw %r3, 8(%r1) # store pointer to fcontext_t on stack
     mr. %r3, %r0 # context stack as arg to align_stack
     bl align_stack_at_plt # call align_stack
     mr. %r0, %r3 # load result into R0
- lwz %r4, 20(%r1) # pop LR from stack
- mtlr %r4 # restore LR
- lwz %r3, 24(%r1) # pop pointer to fcontext_t from stack
- addi %r1, %r1, 28 # release space on stack
+ lwz %r3, 8(%r1) # pop pointer to fcontext_t from stack
 
- li %r4, 32
- subf %r0, %r4, %r0 # 32 bytes on stack for parameter area(== 8 registers)
+ li %r4, 56
+ subf %r0, %r4, %r0 # reserve 24 bytes lingage + 32 bytes parameter area on context stack
     stw %r0, 76(%r3) # save the aligned stack base
 
     mflr %r0 # load LR
     bl 1f # jump to label 1
 1:
- mflr %r4 # load LR
- addi %r4, %r4, finish - 1b # address of finish; called after context function returns
+ mflr %r4 # load LR into R4
+ addi %r4, %r4, finish - 1b # compute abs address of label finish
     mtlr %r0 # restore LR
- stw %r4, 84(%r3) # save address of finish
+ stw %r4, 84(%r3) # save address of finish as return address for context function
+ # entered after context function returns
+
+ addi %r1, %r1, 32 # deallocate stack space
+ lwz %r0, 4(%r1) # load return address from stack, destroy stack frame
+ mtlr %r0 # restore return address
 
     li %r3, 0
     blr
 
 finish:
+ # SP points to same address as SP on entry of context function
+ mflr %r0 # save return address into R0
+ stw %r0, 4(%r1) # save return address on stack, set up stack frame
+ stwu %r1, -16(%r1) # allocate stack space, SP % 16 == 0
+
     li %r3, 0 # exit code is zero
     bl _exit_at_plt # exit application
 .size make_fcontext, .-make_fcontext

Modified: trunk/libs/context/src/asm/fcontext_ppc64_sysv_elf_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_ppc64_sysv_elf_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_ppc64_sysv_elf_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -212,23 +212,18 @@
 .type .make_fcontext,@function
 .globl .make_fcontext
 .make_fcontext:
+ mflr %r0 # save return address into R0
+ stw %r0, 8(%r1) # save return address on stack, set up stack frame
+ stwu %r1, -64(%r1) # allocate stack space, SP % 16 == 0
+
     std %r4, 176(%r3) # save address of context function
- ld %r0, 184(%r3) # load address of context stack base
- ld %r4, 192(%r3) # load size of context stack
- add %r0, %r4, %r0 # compute start address of context stack
-
- li %r4, 56
- subf %r1, %r4, %r1 # reserve space on stack
- stw %r3, 48(%r1) # store pointer to fcontext_t on stack
- mflr %r4 # load LR
- stw %r4, 40(%r1) # store LR on stack
+ ld %r0, 184(%r3) # load address of context stack pointer (base)
+
+ stw %r3, 16(%r1) # store pointer to fcontext_t on stack
     mr. %r3, %r0 # context stack as arg to align_stack
     bl align_stack_at_plt # call align_stack
     mr. %r0, %r3 # load result into R0
- lwz %r4, 40(%r1) # pop LR from stack
- mtlr %r4 # restore LR
- lwz %r3, 48(%r1) # pop pointer to fcontext_t from stack
- addi %r1, %r1, 56 # release space on stack
+ lwz %r3, 16(%r1) # pop pointer to fcontext_t from stack
 
     li %r4, 64
     subf %r0, %r4, %r0 # 64 bytes on stack for parameter area (== 8 registers)
@@ -237,15 +232,25 @@
     mflr %r0 # load LR
     bl 1f # jump to label 1
 1:
- mflr %r4 # load LR
- addi %r4, %r4, finish - 1b # calulate absolute address of finish
+ mflr %r4 # load LR into R4
+ addi %r4, %r4, finish - 1b # compute abs address of label finish
     mtlr %r0 # restore LR
- std %r4, 168(%r3) # save address of finish
+ std %r4, 168(%r3) # save address of finish as return address for context function
+ # entered after context function returns
 
- li %r3, 0 # set return value to zero
+ addi %r1, %r1, 64 # deallocate stack space
+ lwz %r0, 8(%r1) # load return address from stack, destroy stack frame
+ mtlr %r0 # restore return address
+
+ li %r3, 0
     blr
 
 finish:
+ # SP points to same address as SP on entry of context function
+ mflr %r0 # save return address into R0
+ stw %r0, 8(%r1) # save return address on stack, set up stack frame
+ stwu %r1, -32(%r1) # allocate stack space, SP % 16 == 0
+
     li %r3, 0 # set return value to zero
     bl _exit_at_plt # exit application
 .size .make_fcontext, .-.make_fcontext

Modified: trunk/libs/context/src/asm/fcontext_x86_64_ms_pe_masm.asm
==============================================================================
--- trunk/libs/context/src/asm/fcontext_x86_64_ms_pe_masm.asm (original)
+++ trunk/libs/context/src/asm/fcontext_x86_64_ms_pe_masm.asm 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -30,7 +30,7 @@
 ; ----------------------------------------------------------------------------------
 ; | 0x50 | 0x54 | 0x58 | 0x5c | 0x60 | 0x64 | |
 ; ----------------------------------------------------------------------------------
-; | sp | size | base | |
+; | sp | size | limit | |
 ; ----------------------------------------------------------------------------------
 ; ----------------------------------------------------------------------------------
 ; | 26 | 27 | |
@@ -101,9 +101,9 @@
 
     mov r10, gs:[030h] ; load NT_TIB
     mov rax, [r10+08h] ; load current stack base
- mov [rcx+060h], rax ; save current stack base
+ mov [rcx+050h], rax ; save current stack base
     mov rax, [r10+010h] ; load current stack limit
- mov [rcx+050h], rax ; save current stack limit
+ mov [rcx+060h], rax ; save current stack limit
     mov rax, [r10+018h] ; load fiber local storage
     mov [rcx+068h], rax ; save fiber local storage
 
@@ -154,9 +154,9 @@
     mov rbp, [rdx+038h] ; restore RBP
 
     mov r10, gs:[030h] ; load NT_TIB
- mov rax, [rdx+060h] ; load stack base
+ mov rax, [rdx+050h] ; load stack base
     mov [r10+08h], rax ; restore stack base
- mov rax, [rdx+050h] ; load stack limit
+ mov rax, [rdx+060h] ; load stack limit
     mov [r10+010h], rax ; restore stack limit
     mov rax, [rdx+068h] ; load fiber local storage
     mov [r10+018h], rax ; restore fiber local storage
@@ -175,13 +175,15 @@
 
     push rbp ; save previous frame pointer; get the stack 16 byte aligned
     mov rbp, rsp ; set RBP to RSP
- sub rsp, 040h ; allocate shadow space
+ sub rsp, 040h ; allocate stack space (contains shadow space for subroutines)
 
     mov [rcx+048h], rdx ; save address of context function
- mov rdx, [rcx+050h] ; load address of context stack pointer (limit)
+ mov rdx, [rcx+050h] ; load address of context stack pointer (base)
     mov r8, [rcx+058h] ; load context stack size
- lea rdx, [rdx+r8] ; compute top address of context stack (base)
- mov [rcx+060h], rdx ; save top address of context stack (base)
+ neg r8 ; negate stack size for LEA instruction (== substraction)
+ lea rdx, [rdx+r8] ; compute bottom address of context stack
+ mov [rcx+060h], rdx ; save bottom address of context stack (limit)
+ mov rdx, [rcx+050h] ; load address of context stack pointer (base)
 
     mov [rbp-08h], rcx ; save pointer to fcontext_t
     mov rcx, rdx ; context stack pointer as arg for align_stack
@@ -206,7 +208,7 @@
     ret
 
 finish:
- ; RSP == stack pointer in fcontext + 0x8
+ ; RSP points to same address as RSP on entry of context function + 0x8
     xor rcx, rcx ; exit code is zero
     call _exit ; exit application
     hlt

Modified: trunk/libs/context/src/asm/fcontext_x86_64_sysv_elf_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_x86_64_sysv_elf_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_x86_64_sysv_elf_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -92,8 +92,6 @@
 
     movq %rsi, 0x38(%rdi) /* save address of context function */
     movq 0x40(%rdi), %rdx /* load address of context stack base */
- movq 0x48(%rdi), %rax /* load size of context stack */
- leaq (%rdx,%rax), %rdx /* compute top address of context stack */
 
     movq %rdi, (%rsp) /* save pointer to fcontext_t */
     movq %rdx, %rdi /* context stack pointer as arg for align_stack */
@@ -118,7 +116,7 @@
     ret
 
 finish:
- /* RSP == stack pointer of context function + 0x8 */
+ /* RSP points to same address as RSP on entry of context function + 0x8 */
     xorq %rdi, %rdi /* exit code is zero */
     call _exit_at_PLT /* exit application */
     hlt

Modified: trunk/libs/context/src/asm/fcontext_x86_64_sysv_macho_gas.S
==============================================================================
--- trunk/libs/context/src/asm/fcontext_x86_64_sysv_macho_gas.S (original)
+++ trunk/libs/context/src/asm/fcontext_x86_64_sysv_macho_gas.S 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -89,8 +89,6 @@
 
     movq %rsi, 0x38(%rdi) /* save address of context function */
     movq 0x40(%rdi), %rdx /* load address of context stack base */
- movq 0x48(%rdi), %rax /* load size of context stack */
- leaq (%rdx,%rax), %rdx /* compute top address of context stack */
 
     movq %rdi, (%rsp) /* save pointer to fcontext_t */
     movq %rdx, %rdi /* context stack pointer as arg for align_stack */
@@ -115,7 +113,7 @@
     ret
 
 finish:
- /* RSP == stack pointer of context function + 0x8 */
+ /* RSP points to same address as RSP on entry of context function + 0x8 */
     xorq %rdi, %rdi /* exit code is zero */
     call _exit /* exit application */
     hlt

Modified: trunk/libs/context/src/stack_allocator_posix.cpp
==============================================================================
--- trunk/libs/context/src/stack_allocator_posix.cpp (original)
+++ trunk/libs/context/src/stack_allocator_posix.cpp 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -16,6 +16,7 @@
 #include <unistd.h>
 }
 
+#include <cstring>
 #include <stdexcept>
 
 #include <boost/config.hpp>
@@ -60,10 +61,12 @@
     ::close( fd);
     if ( ! limit) throw std::bad_alloc();
 
+ std::memset( limit, size_, '\0');
+
     const int result( ::mprotect( limit, pagesize(), PROT_NONE) );
     BOOST_ASSERT( 0 == result);
 
- return limit;
+ return static_cast< char * >( limit) + size_;
 }
 
 void
@@ -74,7 +77,8 @@
         const std::size_t pages = page_count( size);
         const std::size_t size_ = pages * pagesize();
         BOOST_ASSERT( 0 < size && 0 < size_);
- ::munmap( vp, size_);
+ void * limit = static_cast< char * >( vp) - size_;
+ ::munmap( limit, size_);
     }
 }
 

Modified: trunk/libs/context/src/stack_allocator_windows.cpp
==============================================================================
--- trunk/libs/context/src/stack_allocator_windows.cpp (original)
+++ trunk/libs/context/src/stack_allocator_windows.cpp 2012-09-01 18:56:52 EDT (Sat, 01 Sep 2012)
@@ -12,6 +12,7 @@
 #include <windows.h>
 }
 
+#include <cstring>
 #include <stdexcept>
 
 #include <boost/config.hpp>
@@ -54,12 +55,14 @@
     void * limit = ::VirtualAlloc( 0, size_, MEM_COMMIT, PAGE_READWRITE);
     if ( ! limit) throw std::bad_alloc();
 
+ std::memset( limit, size_, '\0');
+
     DWORD old_options;
     const BOOL result = ::VirtualProtect(
         limit, pagesize(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
     BOOST_ASSERT( FALSE != result);
 
- return limit;
+ return static_cast< char * >( limit) + size_;
 }
 
 void
@@ -70,7 +73,8 @@
         const std::size_t pages = page_count( size);
         const std::size_t size_ = pages * pagesize();
         BOOST_ASSERT( 0 < size && 0 < size_);
- ::VirtualFree( vp, 0, MEM_RELEASE);
+ void * limit = static_cast< char * >( vp) - size_;
+ ::VirtualFree( limit, 0, MEM_RELEASE);
     }
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk