当前位置:网站首页>A simple and general method to obtain the size of function stack space
A simple and general method to obtain the size of function stack space
2022-07-29 03:15:00 【Stars sink and earth move】
Sometimes we need to evaluate the stack space used by a function , For example, when creating threads , We need to estimate the stack space required by this thread function , Allocate the appropriate thread stack .
This paper introduces a simple method , To get the stack space occupied by a function .
principle :
For the decreasing stack memory model , When you call a function , The stack pointer will move down , The distance moved depends on the local variable of the function and the calling sub function , That is, the stack space used by this function , When the function returns , The stack pointer will return to the position before the call .
According to this characteristic , We can fill the memory at the bottom of the stack with a specific value before calling the function , After the call, check whether the value at the bottom of the stack changes , At the end of the search 1 The changed address is the bottom of the stack used by this function . By subtracting the pointer at the bottom of the stack from the current stack pointer, the actual stack space used by this function can be calculated .
The implementation of :
I don't say much nonsense , Test the code directly :
// use 0xAA Fill the bottom of the stack 512 byte
void stack_test_begin(void)
{
register int i;
unsigned char mem[512];
for(i = 0; i < 512; i++)
{
mem[i] = 0xAA;
}
}
// Check the bottom of the stack , Return stack size
int stack_test_end(void)
{
register int i;
unsigned char mem[512];
for(i = 0; i < 512; i++)
{
if(mem[i] != 0xAA)
{
return 512 - i;
}
}
return 0;
}
// Tested function , The stack space occupied by this function theory is 128 byte
void stack_use_128(void)
{
register int i;
char mem[128];
for(i=0; i<128; i++)
{
mem[i] = 0;
}
}
// test
void main(void)
{
int stack_size;
stack_test_begin();
stack_use_128();
stack_size = stack_test_end();
printf("stack: %d\n", stack_size);
}
Code reading :
For the convenience of analysis , hypothesis main The initial stack pointer of the function is SP=0x00001000.
1. call stack_test_begin() , This function local variable mem Occupy 512 byte , this 512 For byte 0xAA fill , It will actually lead to 0x00000E00 - 0x00001000 Filled with 0xAA. This completes the step of filling a specific value at the bottom of the stack .
2. call stack_use_128(), This function is the target function that we want to obtain stack space , The operation of this function on local variables , Will cause us to fill 0xAA Change to other values .
3. call stack_test_end(), This function also uses 512 Local variables of bytes , We search this 512 Byte space , Look back before , Find not 0xAA Value , It means here is stack_use_128() Stack boundary used by function . This boundary was previously unused stack memory , So with 512 Subtracting this boundary value is the actual stack space used by the function .
4. If the stack space of the function under test exceeds 512, Need to put 512 Change to a larger value , Add more fill depth .
summary :
In this method ARM and x86 The test on the platform can work normally . Independent of system calls , It's universal .
But filling memory and searching memory will consume CPU resources , Affect overall performance , Therefore, it is only suitable for use in the code debugging phase .
End
边栏推荐
- 融云 IM & RTC 能力上新盘点
- 复现20字符短域名绕过以及xss相关知识点
- 多行文本省略
- Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names
- Unity game special effects
- 军品技术文件划分及说明
- 《QA离业务代码能有多近?》通过codediff直接暴露缺陷
- Principle knowledge is useful
- Flask的创建的流程day05-06之创建项目
- 数字图像处理 第10章——图像分割
猜你喜欢

STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)

C语言程序设计 | 交换二进制数奇偶位(宏实现)

Chapter 2 VRP command line

单例模式(饿汉式 懒汉式)

逐步分析类的拆分之案例——五彩斑斓的小球碰撞

带你来浅聊一下,单商户功能模块汇总

Singleton mode (hungry and lazy)

融云实时社区解决方案
![[open the door to the new world] see how the old bird of testing plays API testing between applause](/img/79/1bc836cefef24d23e09d9865ff1fba.png)
[open the door to the new world] see how the old bird of testing plays API testing between applause

makefile详解
随机推荐
正则表达绕过waf
【FreeSwitch开发实践】media bug获取通话语音流
How dare you write a resume that is proficient in concurrent programming? Why do you use a two-way linked list in AQS?
扫雷简单版
C traps and defects Chapter 3 semantic "traps" 3.2 pointers to non arrays
《QA离业务代码能有多近?》QA对业务代码进行可测性改造
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Digital image processing Chapter 10 - image segmentation
HTB-Blocky
4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
【FreeSwitch开发实践】UniMRCP编译与安装
Detailed steps for installing MySQL 8.0 under Linux
复现20字符短域名绕过以及xss相关知识点
[open the door to the new world] see how the old bird of testing plays API testing between applause
2022-07-28 第四小组 修身课 学习笔记(every day)
Several methods of converting object to string
[freeswitch development practice] media bug obtains call voice flow
Hangao database best practice configuration tool Hg_ BP log collection content
GJB常见混淆概念
mycat读写分离配置