Hi,
Can I allocate memory with 'sub esp, size'? The main program is a
Delphi7 application. There is some manually compiled asm routine in
that.
|
|
0
|
|
|
|
Reply
|
JohnSmith
|
12/15/2009 11:00:06 AM |
|
>>>>> "John" == JohnSmith <csnews77@MUNGED.microcosmotalk.com> writes:
John> Hi,
John> Can I allocate memory with 'sub esp, size'? The main program
John> is a Delphi7 application. There is some manually compiled asm
John> routine in that.
On which system?
On bare metal yes. Provided you're sure you're not bumping into your
code or other data structures.
On Unix/Linux no. There're pages below the current stack pointer which
are not mapped to the process.
On Windows I don't know, but I presume you're in the same situation as
Linux.
If you're still working on your threads' stuff, do yourself a favour and
allocate all stacks on the heap. There's no advantages in doing
otherwise.
oth,
Maurizio
--
|
|
0
|
|
|
|
Reply
|
Maurizio
|
12/15/2009 2:11:31 PM
|
|
JohnSmith <csnews77@munged.microcosmotalk.com> wrote in part:
> Can I allocate memory with 'sub esp, size'? The main program
> is a Delphi7 application. There is some manually compiled
> asm routine in that.
Certainly you can legitimately grab some memory (up to stack
size limits). But stack allocations like this are local --
only persist while the subroutine runs. When it hits a RET ,
the stack has to be back as it was at subr entry.
The previously grabbed area is on dead stack [ESP-x] which
will get reused, certainly by later CALLs, possibly by userland
signal handlers but not by PM-OS interrupts.
-- Robert
>
>
|
|
0
|
|
|
|
Reply
|
Robert
|
12/15/2009 2:13:13 PM
|
|
Maurizio Vitale
<mav@cuma.i-did-not-set--mail-host-address--so-tickle-me.dotsrc.org> wrote in
part:
> On Unix/Linux no. There're pages below the current stack
> pointer which are not mapped to the process.
I just tried a push 40960 bytes lower (10 pages) and got no
segfault on Linux. Within whatever stack limits, I expect
a decent OS to silently pagefault and map in new pages.
-- Robert
|
|
0
|
|
|
|
Reply
|
Robert
|
12/15/2009 5:41:34 PM
|
|
>>>>> "Robert" == Robert Redelmeier <redelm@ev1.net.invalid> writes:
Robert> Maurizio Vitale
Robert> <mav@cuma.i-did-not-set--mail-host-address--so-tickle-me.dotsrc.org>
Robert> wrote in part:
>> On Unix/Linux no. There're pages below the current stack pointer
>> which are not mapped to the process.
Robert> I just tried a push 40960 bytes lower (10 pages) and got no
Robert> segfault on Linux. Within whatever stack limits, I expect a
Robert> decent OS to silently pagefault and map in new pages.
OSes typically check whether the pagefault is within a certain distance
from the current top (bottom) of the stack. If so they extend the stack,
otherwise they raise an exception.
This is the most useful policy, in that it allows the stack to grow
while still catch many memory access errors.
I do not know what that distance is under Linux, but there's certainly a
limit over which you cannot simply move the stack pointer and access
memory there.
--
|
|
0
|
|
|
|
Reply
|
Maurizio
|
12/15/2009 7:52:57 PM
|
|
Maurizio Vitale
<mav@cuma.i-did-not-set--mail-host-address--so-tickle-me.dotsrc.org> wrote in
part:
>>>>>> "Robert" == Robert Redelmeier <redelm@ev1.net.invalid> writes:
> Robert> I just tried a push 40960 bytes lower (10 pages) and got no
> Robert> segfault on Linux. Within whatever stack limits, I expect a
> Robert> decent OS to silently pagefault and map in new pages.
>
> OSes typically check whether the pagefault is within a certain
> distance from the current top (bottom) of the stack. If so they
> extend the stack, otherwise they raise an exception.
>
> This is the most useful policy, in that it allows the stack to
> grow while still catch many memory access errors.
>
> I do not know what that distance is under Linux, but there's
> certainly a limit over which you cannot simply move the stack
> pointer and access memory there.
Easy: `unlimit -a` lists limits and `ulimit -s` gets/sets stack.
I just checked, and my system has ulimit -s 8192 KiB .
Push at esp-8192000 ran fine, esp-9000000 segfaulted.
OP's environment may be more limited.
-- Robert
|
|
0
|
|
|
|
Reply
|
Robert
|
12/15/2009 11:17:20 PM
|
|
On 15 Dec, 19:52, Maurizio Vitale <m...@cuma.i-did-not-set--mail-host-
address--so-tickle-me.dotsrc.org> wrote:
> >>>>> "Robert" =3D=3D Robert Redelmeier <red...@ev1.net.invalid> writes:
>
> =A0 =A0 Robert> Maurizio Vitale
> =A0 =A0 Robert> <m...@cuma.i-did-not-set--mail-host-address--so-tickle-me=
..dotsrc.org>
> =A0 =A0 Robert> wrote in part:
> =A0 =A0 >> On Unix/Linux no. There're pages below the current stack point=
er
> =A0 =A0 >> which are not mapped to the process.
>
> =A0 =A0 Robert> I just tried a push 40960 bytes lower (10 pages) and got =
no
> =A0 =A0 Robert> segfault on Linux. =A0Within whatever stack limits, I exp=
ect a
> =A0 =A0 Robert> decent OS to silently pagefault and map in new pages.
>
> OSes typically check whether the pagefault is within a certain distance
> from the current top (bottom) of the stack. If so they extend the stack,
> otherwise they raise an exception.
>
> This is the most useful policy, in that it allows the stack to grow
> while still catch many memory access errors.
>
> I do not know what that distance is under Linux, but there's certainly a
> limit over which you cannot simply move the stack pointer and access
> memory there.
You should get hundreds of megabytes, if not thousands. I tried it
once. Can't remember the exact figure but it was large.
To the OP, you should be OK to allocate what you need but remember
that you need to restore the stack pointer before you leave the
function that runs sub esp.
James
|
|
0
|
|
|
|
Reply
|
James
|
12/15/2009 11:17:55 PM
|
|
On Dec 15, 8:11=A0am, Maurizio Vitale <m...@cuma.i-did-not-set--mail-
host-address--so-tickle-me.dotsrc.org> wrote:
> >>>>> "John" =3D=3D JohnSmith =A0<csnew...@MUNGED.microcosmotalk.com> wri=
tes:
>
> =A0 =A0 John> Hi,
>
> =A0 =A0 John> Can I allocate memory with 'sub esp, size'? The main progra=
m
> =A0 =A0 John> is a Delphi7 application. There is some manually compiled a=
sm
> =A0 =A0 John> routine in that.
>
> On which system?
> On bare metal yes. Provided you're sure you're not bumping into your
> code or other data structures.
> On Unix/Linux no. There're pages below the current stack pointer which
> are not mapped to the process.
> On Windows I don't know, but I presume you're in the same situation as
> Linux.
On Windows, the OS will not extend a stack (by mapping in additional
pages) by more than 4KB at a time (on x86-32 and x86-64 - on IPF it's
8KB). The approach is to insert stack probes if your local function
allocates more than 4KB of stack space, touching each 4KB that you're
allocating. Compilers for Windows typically do this automatically,
although some, like MSVC, can turn the behavior off (for example, you
don't need it in a device driver).
Alternatively you could touch the entire stack area at startup.
|
|
0
|
|
|
|
Reply
|
robertwessel2
|
12/16/2009 2:05:07 AM
|
|
"James Harris" <james.harris.1@googlemail.com> wrote in message
news:4b281923$0$5125$9a6e19ea@unlimited.newshosting.com...
On 15 Dec, 19:52, Maurizio Vitale <m...@cuma.i-did-not-set--mail-host-
address--so-tickle-me.dotsrc.org> wrote:
> >>>>> "Robert" == Robert Redelmeier <red...@ev1.net.invalid> writes:
>
> Robert> Maurizio Vitale
> Robert>
> <m...@cuma.i-did-not-set--mail-host-address--so-tickle-me.dotsrc.org>
> Robert> wrote in part:
> >> On Unix/Linux no. There're pages below the current stack pointer
> >> which are not mapped to the process.
>
> Robert> I just tried a push 40960 bytes lower (10 pages) and got no
> Robert> segfault on Linux. Within whatever stack limits, I expect a
> Robert> decent OS to silently pagefault and map in new pages.
>
> OSes typically check whether the pagefault is within a certain distance
> from the current top (bottom) of the stack. If so they extend the stack,
> otherwise they raise an exception.
>
> This is the most useful policy, in that it allows the stack to grow
> while still catch many memory access errors.
>
> I do not know what that distance is under Linux, but there's certainly a
> limit over which you cannot simply move the stack pointer and access
> memory there.
<--
You should get hundreds of megabytes, if not thousands. I tried it
once. Can't remember the exact figure but it was large.
-->
errm.... no...
hundreds of kB maybe, but not MB... (and sure as hell not thousands of
MB...).
AFAIK, Windows reserves around 4MB for stacks, and generally has these
regions set to be guard pages. past this, I think there is usually a region
of reserved pages (so that a PF will happen if the stack overflows), then,
there is whatever happens to be mapped in below.
the default load address for EXE's is 0x400000, and I think Windows usually
puts the stack below the EXE. hence, around 4MB for the stack, then some
reserved memory (64kB I think, this being essentially "page 0", as
VirtualAlloc seems to use 64kB blocks).
hence, if one overflows the thread-0 stack, it hits the 0 page, or tries to
wrap around into kernel space.
dunno much how it works on Linux exactly, as I have not looked into this so
much.
<--
To the OP, you should be OK to allocate what you need but remember
that you need to restore the stack pointer before you leave the
function that runs sub esp.
-->
and keep it "sane", since, no, you infact can't do a 256MB or 1GB alloc
simply by subtracting the stack pointer...
actually, it is not really "safe" much beyond 10s of kB, as then one puts
themselves in major risk of stack overflows if there happen to be a number
of such frames on the stack...
|
|
0
|
|
|
|
Reply
|
BGB
|
12/17/2009 7:26:01 AM
|
|
"BGB / cr88192" <cr88192@MUNGED.microcosmotalk.com> wrote:
>
>hundreds of kB maybe, but not MB... (and sure as hell not thousands of
>MB...).
>>
>AFAIK, Windows reserves around 4MB for stacks, and generally has these
>regions set to be guard pages. past this, I think there is usually a region
>of reserved pages (so that a PF will happen if the stack overflows), then,
>there is whatever happens to be mapped in below.
Yes. ESP sits at the end of allocated space, and above that is reserved
space (which generates a page fault that allocates more memory). The stack
reservation size and initial stack allocation size are both parameters in
the PE executable header. VC++ defaults to 64kB allocation and 1MB
reservation, but both are easily changable via a linker parameter.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
|
|
0
|
|
|
|
Reply
|
Tim
|
12/21/2009 2:50:59 AM
|
|
|
9 Replies
138 Views
(page loaded in 0.077 seconds)
Similiar Articles: Allocating Memory in DOS - comp.lang.asm.x86Hi I'm doing a very basic dos app that reads a file in to memory. If I allocate a fixed memory area in the .DATA section, there are no problems. But ... Allocating memory for Option ROM during BIOS POST and bootloader ...Hi All, I am wondering if anybody here have any experience on what is the best way to dynamically allocate buffer for Option ROM during BIOS POST ... Unable to allocate memory - comp.lang.idl-pvwaveThe message "unable to allocate memory" has become a frequent visitor in my console output on IDL 8.0.1 for Windows. Highly frustrating... ... How to allocate memory for an array of more than 2G - comp.lang ...Hi, I have a trouble to read a .h5 file which is about 3G. I am using {x86_64 linux unix linux 7.1.1 Aug 21 2009 64 64} IDL. So you see, my system... Allocating RAM to Solaris 10 Zones - comp.sys.sun.adminUnable to allocate memory - comp.lang.idl-pvwave Allocating RAM to Solaris 10 Zones - comp.sys.sun.admin Unable to allocate memory - comp.lang.idl-pvwave Allocating RAM to ... ide vs command line: unable to allocate memory - comp.lang.idl ...Hi all, on IDLDE 8.0 on my Vaio running WinXP with 4GB IDL> arr = fltarr(320,256,400) IDL> arr = fltarr(320,256,500) IDL> arr = fltarr(320,256... allocating a previously allocated pointer - comp.lang.fortran ...allocating memory - comp.lang.asm.x86 The previously grabbed area is on dead stack [ESP-x ... ESP sits at the end of allocated space, and above ... STL allocators, global new/delete using the heap and shared memory ...I have a large C++ program that needs to allocate memory for its objects using shared memory (don't ask why). Meyers III item 10 says "most of the st... Allocating, Deallocating and Reallocating - comp.lang.fortran ...if > you do a lot of allocating and reallocating of memory, will that > fragment the memory in such a way as to possibly cause problems. Sort > of like a hard disk ... How to check whether malloc has allocated memory properly in case ...How to check whether malloc has allocated memory properly in case if malloc(0) can return valid pointer Follow Allocating Memory - About C, C++ and C#Learn How to allocate ram for pointer variables. ... As we've seen the Operating System manages applications and their use of memory, so to guarantee that you have ... Memory management - Wikipedia, the free encyclopediaMemory management is the act of managing computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory ... 7/14/2012 5:09:00 AM
|