NO

Author Topic: Regarding __chkstk  (Read 8866 times)

x79

  • Guest
Regarding __chkstk
« on: March 06, 2015, 06:11:47 AM »
I'm trying to rewrite my small project without a standard library because it is mostly windows API calls a some string manipulation. When I tried to declare an array the size of input file, I get this error and I understand why.

My question is, can I put a chkstk function in my project or must I include it in an external library? I found an implementation in wcrt source in asm. When I tried to put a dummy function in my project, I got a redefinition error.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Regarding __chkstk
« Reply #1 on: March 06, 2015, 09:30:37 AM »
I'm trying to rewrite my small project without a standard library because it is mostly windows API calls a some string manipulation. When I tried to declare an array the size of input file, I get this error and I understand why.
Which error?
My question is, can I put a chkstk function in my project or must I include it in an external library? I found an implementation in wcrt source in asm. When I tried to put a dummy function in my project, I got a redefinition error.
Yes you can
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Regarding __chkstk
« Reply #2 on: March 06, 2015, 10:52:40 AM »
use compiler option -Zl Omit default library name in object files.
or linker option -nodefaultlib Ignore standard places.
« Last Edit: March 06, 2015, 03:03:32 PM by TimoVJL »
May the source be with you

x79

  • Guest
Re: Regarding __chkstk
« Reply #3 on: March 06, 2015, 02:01:59 PM »
Which error?
Unresolved external symbol '___chkstk'
Sorry, when I googled chkstk, this error is the most common thing I saw.
Yes you can
use compiler option -Zl Omit default library name in object files.
This option is already set in my project but I still get a redefinition error when I write a dummy __chkstk function.
BTW, I'm using POIDE v8 RC7.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Regarding __chkstk
« Reply #4 on: March 06, 2015, 11:55:57 PM »
Which error?
Unresolved external symbol '___chkstk'
Sorry, when I googled chkstk, this error is the most common thing I saw.
Yes you can
use compiler option -Zl Omit default library name in object files.
This option is already set in my project but I still get a redefinition error when I write a dummy __chkstk function.
BTW, I'm using POIDE v8 RC7.
Post your project code (too bad that 8.0RC7 has a bug in the ZIP project option that causes it to crash the IDE), without seeing what you are doing it is hard to tell what's wrong.

Ralf

x79

  • Guest
Re: Regarding __chkstk
« Reply #5 on: March 07, 2015, 01:09:01 AM »
nolib.zip is my actual project. nolib-test is a sample to recreate my problem.
The goal of my project is to read some input from a text file and execute a shell command based on the input. I don't like the shim creator app that comes with chocolatey so I'm writing my own.

The code that raised this bug for me was where I read the input file into a buffer. I want to get the file size then create an appropriately sized buffer. Because I pass a variable for the array size, poasm wants to check the stack (for overflow?). __chkstk (I assume) is part of pocrt.lib. and since I omitted it, I get unresolved external symbol. If I try to put a dummy function in my code, I get a redefinition error.

I understand that I can compile the chkstk function I found in wcrt source and link to that lib but I wondered if I could put it as inline asm in my project.

So you should be able to reproduce like this
  • Create a blank project
  • Check the compiler option 'Omit default library name from object files
  • Set the linker entry point to _start
  • create a _start function, declare an int x initialized to 2, create an array of size x

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Regarding __chkstk
« Reply #6 on: March 07, 2015, 05:32:20 PM »
To be honest I don't know why, but the chkstk function must be in a different module.
See the attached project...  :-X
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Regarding __chkstk
« Reply #7 on: March 07, 2015, 07:36:40 PM »
compiler internally define [extern ___chkstk]
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Regarding __chkstk
« Reply #8 on: March 07, 2015, 09:38:25 PM »
compiler internally define [extern ___chkstk]
Hi Timo  :)
This explain why, thanks.
I would add some notes on the __chkstk function.
In the past it was used to check against stack overflow, actually its main scope is to 'touch' memory when the space to be allocate is more than 4096 bytes to committ memory allocated from MM to guarantee physical page presence inn correct sequence with execution. MSVC has a flag to use it only on large allocations that are more then 4096 bytes so can cross MM page bound.
In PellesC it seems to be implemented to be triggered independently from the required space ammount.
In X79 code it is triggered because an array is allocated using a variable value. If you change it with a constant __chkstk is not called.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

x79

  • Guest
Re: Regarding __chkstk
« Reply #9 on: March 08, 2015, 05:09:33 AM »
If you change it with a constant __chkstk is not called.

Actually, if you keep small static variables, then it doesn't call it. If you use a large value (like wchar_t *myarray[2048]) then it will still call __chkstk.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Regarding __chkstk
« Reply #10 on: March 08, 2015, 12:53:30 PM »
If you change it with a constant __chkstk is not called.

Actually, if you keep small static variables, then it doesn't call it. If you use a large value (like wchar_t *myarray[2048]) then it will still call __chkstk.
OK, at least this is compliant with M$.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

x79

  • Guest
Re: Regarding __chkstk
« Reply #11 on: March 16, 2015, 02:29:31 PM »
I also learned that if I use HeapAlloc and HeapFree for the dynamic arrays that I can avoid chkstk. This was based on some comment I found on a google result that one could avoid chkstk by using the heap. Now my project works without any stdlib :)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Regarding __chkstk
« Reply #12 on: March 16, 2015, 06:06:19 PM »
I also learned that if I use HeapAlloc and HeapFree for the dynamic arrays that I can avoid chkstk. This was based on some comment I found on a google result that one could avoid chkstk by using the heap. Now my project works without any stdlib :)
???
I think the sense is a little bit different. Using the dynamic memory you don't stress the stack and, on MS compilers, you can use the switch to require to don't emit stack check for every stack allocation. Anyway the compiler always emits the chkstk when stack allocations are bigger than 2048 bytes.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Regarding __chkstk
« Reply #13 on: March 17, 2015, 01:30:48 PM »
Stack is limited to about 1MB (depending on linker settings), heap is not limited. But getting memory from the stack is faster. What _chktst does is "stack probing", i.e. it accesses every page walking down from the top, touching the guard pages, thus giving the memory manager a chance to make the page accessible.