Memory Layout (Virtual address space of a C process)
found a good example to demostrate the memory layout and its stack info of a user-mode process, only that this example is for Linux. But it is still worth taking a look at it.
C source file is quite simple:
- void func(int x, int y)
- {
- int a;
- int b[3];
- /* no other auto variable */
- …
- }
- void main()
- {
- …
- func(72,73);
- …
- }
Memory layout is as below. I will talk about the stack in the next session.
The diagram below shows the memory layout of a typical C’s process. The process load segments (corresponding to ” text ” and ” data ” in the diagram) at the process’s base address. The main stack is located just below and grows downwards. Any additional threads that are created will have their own stacks, located below the main stack. Each of the stack frames is separated by a guard page to detect stack overflows among stacks frame. The heap is located above the process and grows upwards.
In the middle of the process’s address space, there is a region is reserved for shared objects. When a new process is created, the process manager first maps the two segments from the executable into memory. It then decodes the program’s ELF header. If the program header indicates that the executable was linked against a shared library, the process manager will extract the name of the dynamic interpreter from the program header. The dynamic interpreter points to a shared library that contains the runtime linker code. The process manager will load this shared library in memory and will then pass control to the runtime linker code in this library.
Ref:
http://www.cs.uleth.ca/~holzmann/C/system/memorylayout.pdf
http://www.tenouk.com/Bufferoverflowc/Bufferoverflow1c.html
Stack Frame
Stack is one important segment of the process’s memory layout. It is a dynamic memory buffer portion used to store data implicitly normally during the run time.
The stack segment is where local (automatic) variables are allocated. In C program, local variables are all variables declared inside the opening left curly brace of a function body including the main() or other left curly brace that aren’t defined as static. The data is popped up or pushed into the stack following the Last In First Out (LIFO) rule. The stack holds local variables, temporary information/data, function parameters, return address and the like. When a function is called, a stack frame (or a procedure activation record) is created and PUSHed onto the top of the stack. This stack frame contains information such as the address from which the function was called and where to jump back to when the function is finished (return address), parameters, local variables, and any other information needed by the invoked function. The order of the information may vary by system and compiler. When a function returns, the stack frame is POPped from the stack. Typically the stack grows downward, meaning that items deeper in the call chain are at numerically lower addresses and toward the heap.
Stack frame constructed during the function call for memory allocation implicitly.
A typical layout of a stack frame is shown below although it may be organized differently in different operating systems:
*Function parameters.
*Function’s return address.
*Frame pointer.
*Exception Handler frame.
*Locally declared variables.
*Buffer
*Callee save registers
As an example in Windows/Intel, typically, when the function call takes place, data elements are stored on the stack in the following way:
1. The function parameters are pushed on the stack before the function is called. The parameters are pushed from right to left.
2. The function return address is placed on the stack by the x86 CALL instruction, which stores the current value of the EIP register.
3. Then, the frame pointer that is the previous value of the EBP register is placed on the stack.
4. If a function includes try/catch or any other exception handling construct such as SEH (Structured Exception Handling – Microsoft implementation), the compiler will include exception handling information on the stack.
5. Next, the locally declared variables.
6. Then the buffers are allocated for temporary data storage.
7. Finally, the callee save registers such as ESI, EDI, and EBX are stored if they are used at any point during the functions execution. For Linux/Intel, this step comes after step no. 4.
There are two CPU registers that are important for the functioning of the stack which hold information that is necessary when calling data residing in the memory. Their names areESP and EBP in 32 bits system.
The ESP (Extended Stack Pointer) holds the top stack address. TheEBP (Extended Base Pointer) points to the bottom of the current stack frame.
ESP points to the top of the stack (lower numerical address); it is often convenient to have a stack frame pointer (FP) which holds an address that point to a fixed location within a frame. Looking at the stack frame, local variables could be referenced by giving their offsets from ESP. However , as data are pushed onto the stack and popped off the stack, these offsets change, so the reference of the local variables is not consistent. Consequently, many compilers use another register, generally called Frame Pointer (FP), for referencing both local variables and parameters because their distances from FP do not change with PUSHes and POPs. On Intel CPUs,EBP (Extended Base Pointer) is used for this purpose.
Because the way stack grows, actual parameters have positive offsets and local variables have negative offsets from FP as shown below. Let examine the following simple C program.
- #include <stdio.h>
- int MyFunc(int parameter1, char parameter2)
- {
- int local1 = 9;
- char local2 = ’Z';
- return 0;
- }
- int main(int argc, char *argv[])
- {
- MyFunc(7, ’8′);
- return 0;
- }
And the memory layout will look something like this:
Each time a new function is called, the old value of EBP is the first to be pushed onto the stack and then the new value of ESP is moved to EBP. This new value of ESP held by EBP becomes the reference base to local variables that are needed to retrieve the stack section allocated for the new function call. As mentioned before, a stack grows downward to lower memory address. The stack pointer (ESP) points to the last address on the stack not the next free available address after the top of the stack.
The first thing a function must do when called is to save the previous EBP (so it can be restored by copying into the EIP at function exit later). Then it copies ESP into EBP to create the new stack frame pointer, and advances ESP to reserve space for the local variables. This code is called the procedure prolog . Upon function exit, the stack must be cleaned up again, something called theprocedure epilog .
Using a very simple C program skeleton, the following tries to figure out function calls and stack frames construction/destruction.
- #include <stdio.h>
- int a();
- int b();
- int c();
- int a()
- {
- b();
- c();
- return 0;
- }
- int b()
- {
- return 0;
- }
- int c()
- {
- return 0;
- }
- int main()
- {
- a();
- return 0;
- }
By taking the stack area only, the following is what happen when the above program is run.
By referring the previous program example and above figure, when a program begins execution in the function main(), stack frame is created, space is allocated on the stack for all variables declared within main(). Then, when main() calls a function, a(), new stack frame is created for the variables in a() at the top of the main() stack. Any parameters passed by main() to a() are stored on the stack. If a() were to call any additional functions such as b() and c(), new stack frames would be allocated at the new top of the stack. Notice that the order of the execution happened in the sequence. When c(), b() and a() return, storage for their local variables are de-allocated, the stack frames are destroyed and the top of the stack returns to the previous condition. The order of the execution is in the reverse. As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage.
Ref:
http://www.tenouk.com/Bufferoverflowc/Bufferoverflow1c.html
http://www.tenouk.com/Bufferoverflowc/Bufferoverflow2.html
Memory Layout (Virtual address space of a C process)的更多相关文章
- ARM64 Linux kernel virtual address space
墙外通道:http://thinkiii.blogspot.com/2014/02/arm64-linux-kernel-virtual-address-space.html Now let's ta ...
- ARM32 Linux kernel virtual address space
http://thinkiii.blogspot.jp/2014/02/arm32-linux-kernel-virtual-address-space.html The 32-bit ARM C ...
- Method of address space layout randomization for windows operating systems
A system and method for address space layout randomization ("ASLR") for a Windows operatin ...
- Method for address space layout randomization in execute-in-place code
The present application relates generally to laying out address space for execute-in-place code and, ...
- Virtual address cache memory, processor and multiprocessor
An embodiment provides a virtual address cache memory including: a TLB virtual page memory configure ...
- Multiple address space mapping technique for shared memory wherein a processor operates a fault handling routine upon a translator miss
Virtual addresses from multiple address spaces are translated to real addresses in main memory by ge ...
- Memory Layout of C Programs
Memory Layout of C Programs A typical memory representation of C program consists of following sec ...
- System and method for critical address space protection in a hypervisor environment
A system and method in one embodiment includes modules for detecting an access attempt to a critical ...
- Memory Layout for Multiple and Virtual Inheritance
Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...
随机推荐
- maven初始搭建一个基础项目(spring mvc+spring+jdbc mysql+jstl)
技术选型: 一.项目搭建: 1)创建maven项目 (我博客里面有介绍) 选择aptach的maven-archetype-webapp 填入groupIDhe artifactId等 确认项目名称 ...
- 钩子编程(HOOK) 安装进程内键盘钩子 (1)
摘要:钩子能够监视系统或进程中的各种事件消息.截获发往目标窗体的消息并进行处理.这样,我们就能够在系统中安装自己定义的钩子,监视系统中特定事件的发生.完毕特定的功能,比方截获键盘.鼠标的输入.屏幕取词 ...
- pycharm下: conda installation is not found ----一个公开的bug的解决方案
pycharm conda installation is not found ----一个公开的bug的解决方案 pycharm+anaconda 是当前的主流的搭建方案,但是常出现上述问题. ...
- Python内置函数之range()
range(stop)range(start,stop[,step]) 返回一个range对象,第三个参数的含义为:间隔的个数. range对象同时也是可迭代对象. >>> isin ...
- HTML5 2D平台游戏开发#8指令技
一般在动作游戏中,玩家可以通过对输入设备输入一系列的指令让角色完成某个或多个特定的动作.以格斗游戏<拳皇>为例,键入↓↘→↘↓↙← + A or C可以触发IORI的必杀技八稚女: 通过一 ...
- oracle如何进行索引监控分析和优化
在生产环境.我们会发现: ① 索引表空间 I/O 非常高 ② "db file sequential read" 等待事件也比较高 这种迹象表明.整个数据库系统.索引的 ...
- vmstat 命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行深 ...
- 浅析Apache中RewriteCond规则参数的详细介绍
RewriteCond就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始.基础的功能,为了方 ...
- 二级导航内容均分--jquery
这个是去年做过的一个项目中的算法,个人感觉还可以,所以拿出来分享下. 背景:头部导航二级导航有些内容太长,一列的话太过难看,就要分成两列,要做到按块尽量均分,排列顺序没有限制. 原理: 1.把各个二级 ...
- cvm母机宕机重启后数据库修复
下午正在开周会,然后收到短信,说是X.X.X.X的机器ping不通了,一轮测试过后,发现是某台数据库服务器挂了,先不急着重启,问下tencent客服... 乖乖的好家伙,母机的主板坏了....一个小时 ...