Author : David Lin 林鹏
E-mail : linpeng1577@gmail.com linpeng1577@163.com
OS : 源码级理解掌握UcosRt-thread等嵌入式操作系统内核的设计与实现目前在研究linux内核路漫漫其修远兮吾将上下而求索 :)
转载请注明出处谢谢
前言
在嵌入式操作系统运行中进程的栈溢出问题是大家比较关心的问题由于资源限制栈大小受到限制本文主要介绍uc/os自带的栈检测机制为什么是 ucos而不是ecos或者其他因为我现在项目用这个后续有时间再介绍一种更简单通用不依赖具体操作系统的栈溢出检测机制。
原理
1.在进程的栈初始化的时候按预设的字节对齐方式用特定魔数比如0x9527ucosII预设是0值将栈元素依次完成初始化
2.在系统运行的时候通过守护进程依次遍历各个进程的栈检测栈元素的值是否等于初始化的魔数求得被污染的元素个数所占栈大小的比例即为栈使用量。
3.不通过守护进程直接对进程进行栈溢出检测是否可行可以不过不推荐在进程内部或者进程调度的时候进行如此使用量检测即使使用二分查找算法进行优化如果栈大到一定程度效 率问题还是需要慎重考虑的
4.不扯为什么这么设计这些事情了这跟小学语文写作者的中心思想一样不是ucos原作者所以不写你想知道Jean J. Labrosse怎么想的可以给他发邮件:-)
系统版本uc/osII V2.85
开发环境IAR for ARM (为什么不在linux下开发我也想知道
因为我喜欢gnu/linux,喜欢用gcc,gdb,vim,make,(⊙o⊙)…不过你懂的:-)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
3851. 相关宏定义os_cfg.h #defineOS_TASK_STAT_EN 1 /* Enable (1) or Disable(0) the statistics task*/ #defineOS_TASK_STAT_STK_CHK_EN 1 /* Check task stacks from statistic task */ 2. 相关函数os_core.c os_task.c static void OS_InitTaskStat (void) {……}; //初始化栈守护进程看哪个进程最贪吃 :-) /* ********************************************************************************************************* * INITIALIZATION * CREATING THE STATISTIC TASK * * Description: This function creates the Statistic Task. * * Arguments : none * * Returns : none ********************************************************************************************************* */ #if OS_TASK_STAT_EN > 0 static void OS_InitTaskStat (void) { #if OS_TASK_NAME_SIZE > 7 INT8U err; #endif #if OS_TASK_CREATE_EXT_EN > 0 #if OS_STK_GROWTH == 1 (void)OSTaskCreateExt(OS_TaskStat, (void *)0, /* No args passed to OS_TaskStat()*/ &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Top-Of-Stack */ OS_TASK_STAT_PRIO, /* One higher than the idle task */ OS_TASK_STAT_ID, &OSTaskStatStk[0], /* Set Bottom-Of-Stack */ OS_TASK_STAT_STK_SIZE, (void *)0, /* No TCB extension */ OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear */ #else (void)OSTaskCreateExt(OS_TaskStat, (void *)0, /* No args passed to OS_TaskStat()*/ &OSTaskStatStk[0], /* Set Top-Of-Stack */ OS_TASK_STAT_PRIO, /* One higher than the idle task */ OS_TASK_STAT_ID, &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Bottom-Of-Stack */ OS_TASK_STAT_STK_SIZE, (void *)0, /* No TCB extension */ OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear */ #endif #else #if OS_STK_GROWTH == 1 (void)OSTaskCreate(OS_TaskStat, (void *)0, /* No args passed to OS_TaskStat()*/ &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Top-Of-Stack */ OS_TASK_STAT_PRIO); /* One higher than the idle task */ #else (void)OSTaskCreate(OS_TaskStat, (void *)0, /* No args passed to OS_TaskStat()*/ &OSTaskStatStk[0], /* Set Top-Of-Stack */ OS_TASK_STAT_PRIO); /* One higher than the idle task */ #endif #endif #if OS_TASK_NAME_SIZE > 14 OSTaskNameSet(OS_TASK_STAT_PRIO, (INT8U *)"uC/OS-II Stat", &err); #else #if OS_TASK_NAME_SIZE > 7 OSTaskNameSet(OS_TASK_STAT_PRIO, (INT8U *)"OS-Stat", &err); #endif #endif } #endif #if OS_TASK_CREATE_EXT_EN > 0 void OS_TaskStkClr (OS_STK *pbos, INT32U size,INT16U opt) {……}; //用于栈初始化 #endif /* ********************************************************************************************************* * CLEAR TASK STACK * * Description: This function is used to clear the stack of a task (i.e. write all zeros) * * Arguments : pbos is a pointer to the task's bottom of stack. If the configuration constant * OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high * memory to low memory). 'pbos' will thus point to the lowest (valid) memory * location of the stack. If OS_STK_GROWTH is set to 0, 'pbos' will point to the * highest memory location of the stack and the stack will grow with increasing * memory locations. 'pbos' MUST point to a valid 'free' data item. * * size is the number of 'stack elements' to clear. * * opt contains additional information (or options) about the behavior of the task. The * LOWER 8-bits are reserved by uC/OS-II while the upper 8 bits can be application * specific. See OS_TASK_OPT_??? in uCOS-II.H. * * Returns : none ********************************************************************************************************* */ #if OS_TASK_CREATE_EXT_EN > 0 void OS_TaskStkClr (OS_STK *pbos, INT32U size, INT16U opt) { if ((opt & OS_TASK_OPT_STK_CHK) != 0x0000) { /* See if stack checking has been enabled */ if ((opt & OS_TASK_OPT_STK_CLR) != 0x0000) { /* See if stack needs to be cleared */ #if OS_STK_GROWTH == 1 while (size > 0) { /* Stack grows from HIGH to LOW memory */ size--; *pbos++ = (OS_STK)0; /* Clear from bottom of stack and up! */ } #else while (size > 0) { /* Stack grows from LOW to HIGH memory */ size--; *pbos-- = (OS_STK)0; /* Clear from bottom of stack and down */ } #endif } } } #endif #if OS_TASK_CREATE_EXT_EN > 0 INT8U OSTaskStkChk (INT8U prio, OS_STK_DATA *p_stk_data) {……}; //用于检测各个进程的栈使用率被守护进程OS_TaskStat(void *p_arg)调用 #endif /* ********************************************************************************************************* * STACK CHECKING * * Description: This function is called to check the amount of free memory left on the specified task's * stack. * * Arguments : prio is the task priority * * p_stk_data is a pointer to a data structure of type OS_STK_DATA. * * Returns : OS_ERR_NONE upon success * OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum allowed * (i.e. > OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF. * OS_ERR_TASK_NOT_EXIST if the desired task has not been created or is assigned to a Mutex PIP * OS_ERR_TASK_OPT if you did NOT specified OS_TASK_OPT_STK_CHK when the task was created * OS_ERR_PDATA_NULL if 'p_stk_data' is a NULL pointer ********************************************************************************************************* */ #if OS_TASK_CREATE_EXT_EN > 0 INT8U OSTaskStkChk (INT8U prio, OS_STK_DATA *p_stk_data) { OS_TCB *ptcb; OS_STK *pchk; INT32U free; INT32U size; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0; #endif #if OS_ARG_CHK_EN > 0 if (prio > OS_LOWEST_PRIO) { /* Make sure task priority is valid */ if (prio != OS_PRIO_SELF) { return (OS_ERR_PRIO_INVALID); } } if (p_stk_data == (OS_STK_DATA *)0) { /* Validate 'p_stk_data' */ return (OS_ERR_PDATA_NULL); } #endif p_stk_data->OSFree = 0; /* Assume failure, set to 0 size */ p_stk_data->OSUsed = 0; OS_ENTER_CRITICAL(); if (prio == OS_PRIO_SELF) { /* See if check for SELF */ prio = OSTCBCur->OSTCBPrio; } ptcb = OSTCBPrioTbl[prio]; if (ptcb == (OS_TCB *)0) { /* Make sure task exist */ OS_EXIT_CRITICAL(); return (OS_ERR_TASK_NOT_EXIST); } if (ptcb == OS_TCB_RESERVED) { OS_EXIT_CRITICAL(); return (OS_ERR_TASK_NOT_EXIST); } if ((ptcb->OSTCBOpt & OS_TASK_OPT_STK_CHK) == 0) { /* Make sure stack checking option is set */ OS_EXIT_CRITICAL(); return (OS_ERR_TASK_OPT); } free = 0; size = ptcb->OSTCBStkSize; pchk = ptcb->OSTCBStkBottom; OS_EXIT_CRITICAL(); #if OS_STK_GROWTH == 1 while (*pchk++ == (OS_STK)0) { /* Compute the number of zero entries on the stk */ free++; } #else while (*pchk-- == (OS_STK)0) { free++; } #endif p_stk_data->OSFree = free * sizeof(OS_STK); /* Compute number of free bytes on the stack */ p_stk_data->OSUsed = (size - free) * sizeof(OS_STK); /* Compute number of bytes used on the stack */ return (OS_ERR_NONE); } #endif /*$PAGE*/ /* /* ********************************************************************************************************* * TASK STACK DATA ********************************************************************************************************* */ #if OS_TASK_CREATE_EXT_EN > 0 typedef struct os_stk_data { INT32U OSFree; /* Number of free bytes on the stack */ INT32U OSUsed; /* Number of bytes used on the stack */ } OS_STK_DATA; #endif #if OS_TASK_STAT_EN > 0 void OS_TaskStat (void *p_arg) {……}; //这个就是守护进程 #endif /* ********************************************************************************************************* * STATISTICS TASK * * Description: This task is internal to uC/OS-II and is used to compute some statistics about the * multitasking environment. Specifically, OS_TaskStat() computes the CPU usage. * CPU usage is determined by: * * OSIdleCtr * OSCPUUsage = 100 * (1 - ------------) (units are in %) * OSIdleCtrMax * * Arguments : parg this pointer is not used at this time. * * Returns : none * * Notes : 1) This task runs at a priority level higher than the idle task. In fact, it runs at the * next higher priority, OS_TASK_IDLE_PRIO-1. * 2) You can disable this task by setting the configuration #define OS_TASK_STAT_EN to 0. * 3) You MUST have at least a delay of 2/10 seconds to allow for the system to establish the * maximum value for the idle counter. ********************************************************************************************************* */ #if OS_TASK_STAT_EN > 0 void OS_TaskStat (void *p_arg) { INT32U run; INT32U max; INT8S usage; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0; #endif p_arg = p_arg; /* Prevent compiler warning for not using 'parg' */ while (OSStatRdy == OS_FALSE) { OSTimeDly(2 * OS_TICKS_PER_SEC / 10); /* Wait until statistic task is ready */ } max = OSIdleCtrMax / 100L; for (;;) { OS_ENTER_CRITICAL(); OSIdleCtrRun = OSIdleCtr; /* Obtain the of the idle counter for the past second */ run = OSIdleCtr; OSIdleCtr = 0L; /* Reset the idle counter for the next second */ OS_EXIT_CRITICAL(); if (max > 0L) { usage = (INT8S)(100L - run / max); if (usage >= 0) { /* Make sure we don't have a negative percentage */ OSCPUUsage = usage; } else { OSCPUUsage = 0; } } else { OSCPUUsage = 0; max = OSIdleCtrMax / 100L; } OSTaskStatHook(); /* Invoke user definable hook */ #if (OS_TASK_STAT_STK_CHK_EN > 0) && (OS_TASK_CREATE_EXT_EN > 0) OS_TaskStatStkChk(); /* Check the stacks for each task */ #endif OSTimeDly(OS_TICKS_PER_SEC / 10); /* Accumulate OSIdleCtr for the next 1/10 second */ } } #endif /*$PAGE*/ #if OS_TASK_STAT_EN > 0 void OSStatInit (void) {……}; //每个进程都得调用它 #endif /* ********************************************************************************************************* * STATISTICS INITIALIZATION * * Description: This function is called by your application to establish CPU usage by first determining * how high a 32-bit counter would count to in 1 second if no other tasks were to execute * during that time. CPU usage is then determined by a low priority task which keeps track * of this 32-bit counter every second but this time, with other tasks running. CPU usage is * determined by: * * OSIdleCtr * CPU Usage (%) = 100 * (1 - ------------) * OSIdleCtrMax * * Arguments : none * * Returns : none ********************************************************************************************************* */ #if OS_TASK_STAT_EN > 0 void OSStatInit (void) { #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0; #endif OSTimeDly(2); /* Synchronize with clock tick */ OS_ENTER_CRITICAL(); OSIdleCtr = 0L; /* Clear idle counter */ OS_EXIT_CRITICAL(); OSTimeDly(OS_TICKS_PER_SEC / 10); /* Determine MAX. idle counter value for 1/10 second */ OS_ENTER_CRITICAL(); OSIdleCtrMax = OSIdleCtr; /* Store maximum idle counter count in 1/10 second */ OSStatRdy = OS_TRUE; OS_EXIT_CRITICAL(); } #endif /*$PAGE*/ /* 3. 如何使用系统API Task_42 (void *p_arg) { error_t error; OSStatInit(); //这里应该是CCTV所描述的最美好的天朝请放我在这里 while(1) { switch(num) { case 42: printf(“42 is the answer to life, the universe, and everythingn”); break; ...... default: break; } } } 4. 使用 注意进程控制块的数据结构 /* ********************************************************************************************************* * TASK CONTROL BLOCK ********************************************************************************************************* */ typedef struct os_tcb { OS_STK *OSTCBStkPtr; /* Pointer to current top of stack */ #if OS_TASK_CREATE_EXT_EN > 0 void *OSTCBExtPtr; /* Pointer to user definable data for TCB extension */ OS_STK *OSTCBStkBottom; /* Pointer to bottom of stack */ INT32U OSTCBStkSize; /* Size of task stack (in number of stack elements) */ INT16U OSTCBOpt; /* Task options as passed by OSTaskCreateExt() */ INT16U OSTCBId; /* Task ID (0..65535) */ #endif struct os_tcb *OSTCBNext; /* Pointer to next TCB in the TCB list */ struct os_tcb *OSTCBPrev; /* Pointer to previous TCB in the TCB list */ #if OS_EVENT_EN|| (OS_FLAG_EN > 0) OS_EVENT *OSTCBEventPtr; /* Pointer to event control block */ #endif #if ((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0) void *OSTCBMsg; /* Message received from OSMboxPost() or OSQPost() */ #endif #if (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0) #if OS_TASK_DEL_EN > 0 OS_FLAG_NODE *OSTCBFlagNode; /* Pointer to event flag node */ #endif OS_FLAGS OSTCBFlagsRdy; /* Event flags that made task ready to run */ #endif INT16U OSTCBDly; /* Nbr ticks to delay task or, timeout waiting for event */ INT8U OSTCBStat; /* Task status */ INT8U OSTCBStatPend; /* Task PEND status */ INT8U OSTCBPrio; /* Task priority (0 == highest) */ INT8U OSTCBX; /* Bit position in group corresponding to task priority */ INT8U OSTCBY; /* Index into ready table corresponding to task priority */ #if OS_LOWEST_PRIO <= 63 INT8U OSTCBBitX; /* Bit mask to access bit position in ready table */ INT8U OSTCBBitY; /* Bit mask to access bit position in ready group */ #else INT16U OSTCBBitX; /* Bit mask to access bit position in ready table */ INT16U OSTCBBitY; /* Bit mask to access bit position in ready group */ #endif #if OS_TASK_DEL_EN > 0 INT8U OSTCBDelReq; /* Indicates whether a task needs to delete itself */ #endif #if OS_TASK_PROFILE_EN > 0 INT32U OSTCBCtxSwCtr; /* Number of time the task was switched in */ INT32U OSTCBCyclesTot; /* Total number of clock cycles the task has been running */ INT32U OSTCBCyclesStart; /* Snapshot of cycle counter at start of task resumption */ OS_STK *OSTCBStkBase; /* Pointer to the beginning of the task stack *//* 这里是栈基址*/ INT32U OSTCBStkUsed; /* Number of bytes used from the stack *//* 这里是使用量*/ #endif #if OS_TASK_NAME_SIZE > 1 INT8U OSTCBTaskName[OS_TASK_NAME_SIZE]; #endif } OS_TCB;
例如如下两个进程的控制块
1.栈溢出的进程如图1所示
OSTCBStkSize = 100(100是假设的值实际值如图所示是256)(怎么解决,把该进程OSTCBStkSize设置为大于840/4比如256 OK)
OSTCBStkUsed = 840(注意这里单位是char型所以需要除4之后与OSTCBStkSize比较)
两者的单位都是INT32U型OSTCBStkUsed > OSTCBStkSize
OMG!!! StackOverFlow (Id of Mine in stackOverFlow is linpeng1577@gmail.com )
图1
2.栈正常的进程如图2所示
注意进程控制块
OSTCBStkSize = 512
OSTCBStkUsed = 136(136/4 = 34 < 512)
两者的单位都是INT32U型OSTCBStkUsed < OSTCBStkSize
OMG!!! StackOverFlow? NO! (Id of Mine in stackOverFlow is linpeng1577@gmail.com )
图2
转载请注明出处
blog of linpeng1577
谢谢
转载于:https://blog.51cto.com/linpeng/1596713
最后
以上就是爱撒娇犀牛最近收集整理的关于操作系统栈溢出检测之ucosII篇的全部内容,更多相关操作系统栈溢出检测之ucosII篇内容请搜索靠谱客的其他文章。
发表评论 取消回复