3 Replies - 2607 Views - Last Post: 16 July 2012 - 06:56 PM

#1 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Why do I get a stack overflow here?

Posted 16 July 2012 - 06:13 PM

Yes, I'm definitely going to look into getting that book. There's a good chance my friend's father has it anyway, and I doubt he uses it much after being in the field for so long - so I'll borrow it for a while.

Thanks for the tip!

Right now, though...I'm a bit more concerned as to why my application causes a stack overflow in the chktsk.asm here:

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
here => test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10

_chkstk endp

        end



:o

It could be because I have strings as long as 16384 char (32768 wchar). Then again, does that explain why I can't click anything and the application freezes?

Haha, this thread...I've gone from not knowing how to put things in edit boxes, to rendering numbers with 30K+ digits in edit boxes and causing the program to kinda seize up...

Is This A Good Question/Topic? 0
  • +

Replies To: Why do I get a stack overflow here?

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,287
  • Joined: 28-March 11

Re: Why do I get a stack overflow here?

Posted 16 July 2012 - 06:25 PM

Your mixing C with Assembly or is this a separate program? Need to see more of your code.

I will split this and move it over to the Assembly forum.

    test    dword ptr [eax],eax     ; probe page.
    jmp     short cs10


This makes no sense. What are you testing for? It will jump no matter the result of the test.

eax is a pointer? So you are ANDing the contents of the pointer [eax], with the pointer - eax.
Usually there is a JZ after a test.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: Why do I get a stack overflow here?

Posted 16 July 2012 - 06:44 PM

I don't think he's mixing C and assembly. He's running into the stack overflow boundary checks that comes with the Microsoft C runtime library. chkstk.asm

Here is the entire listing if you are interested.
        page    ,132
        title   chkstk - C stack checking routine
;***
;chkstk.asm - C stack checking routine
;
;       Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
;       Provides support for automatic stack checking in C procedures
;       when stack checking is enabled.
;
;*******************************************************************************

.xlist
        include cruntime.inc
.list

; size of a page of memory

_PAGESIZE_      equ     1000h


        CODESEG

page
;***
;_chkstk - check stack upon procedure entry
;
;Purpose:
;       Provide stack checking on procedure entry. Method is to simply probe
;       each page of memory required for the stack in descending order. This
;       causes the necessary pages of memory to be allocated via the guard
;       page scheme, if possible. In the event of failure, the OS raises the
;       _XCPT_UNABLE_TO_GROW_STACK exception.
;
;       NOTE:  Currently, the (EAX < _PAGESIZE_) code path falls through
;       to the "lastpage" label of the (EAX >= _PAGESIZE_) code path.  This
;       is small; a minor speed optimization would be to special case
;       this up top.  This would avoid the painful save/restore of
;       ecx and would shorten the code path by 4-6 instructions.
;
;Entry:
;       EAX = size of local frame
;
;Exit:
;       ESP = new stackframe, if successful
;
;Uses:
;       EAX
;
;Exceptions:
;       _XCPT_GUARD_PAGE_VIOLATION - May be raised on a page probe. NEVER TRAP
;                                    THIS!!!! It is used by the OS to grow the
;                                    stack on demand.
;       _XCPT_UNABLE_TO_GROW_STACK - The stack cannot be grown. More precisely,
;                                    the attempt by the OS memory manager to
;                                    allocate another guard page in response
;                                    to a _XCPT_GUARD_PAGE_VIOLATION has
;                                    failed.
;
;*******************************************************************************

public  _alloca_probe

_chkstk proc

_alloca_probe    =  _chkstk

        push    ecx

; Calculate new TOS.

        lea     ecx, [esp] + 8 - 4      ; TOS before entering function + size for ret value
        sub     ecx, eax                ; new TOS

; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.

        sbb     eax, eax                ; 0 if CF==0, ~0 if CF==1
        not     eax                     ; ~0 if TOS did not wrapped around, 0 otherwise
        and     ecx, eax                ; set to 0 if wraparound

        mov     eax, esp                ; current TOS
        and     eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary

cs10:
        cmp     ecx, eax                ; Is new TOS
        jb      short cs20              ; in probed page?
        mov     eax, ecx                ; yes.
        pop     ecx
        xchg    esp, eax                ; update esp
        mov     eax, dword ptr [eax]    ; get return address
        mov     dword ptr [esp], eax    ; and put it at new TOS
        ret

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
        test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10

_chkstk endp

        end


Was This Post Helpful? 0
  • +
  • -

#4 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Re: Why do I get a stack overflow here?

Posted 16 July 2012 - 06:56 PM

Yes - when I ran the debugger, it throws this exception immediately. Before the application window is even shown!

It could have something to do with this:

				TCHAR szBig[16384]; // THIS prints the ACTUAL NUMBER to the number box.
				#ifdef UNICODE
				CHAR szaBig[32768];
				gmp_sprintf(szaBig, "%Zd", proth);
				mbstowcs(szBig, szaBig, CountOf(szBig));
				#else
				gmp_sprintf(szBig, "%Zd", proth);
				#endif
				
				SetDlgItemText(hWnd, IDC_BIGNUM, szBig);


where it prints a number to an edit box, sometimes with over 30K digits. However, this happens before the application is even started.

I could give you the full code - around 900 lines - or you could just tell me reasons why I'd also get

Quote

First-chance exception at 0x0033cbc7 in gjs15x64_GUI.exe: 0xC0000005: Access violation reading location 0x00450000.


I got this before, when I didn't zero-out the strings. Now I do. And it's no longer a very low memory address it's referencing (before it was 0xccccccc) - is it a simple memory allocation issue?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1