CN100426237C - Stack overflow protection method in operation for embedded system - Google Patents

Stack overflow protection method in operation for embedded system Download PDF

Info

Publication number
CN100426237C
CN100426237C CNB2006100516256A CN200610051625A CN100426237C CN 100426237 C CN100426237 C CN 100426237C CN B2006100516256 A CNB2006100516256 A CN B2006100516256A CN 200610051625 A CN200610051625 A CN 200610051625A CN 100426237 C CN100426237 C CN 100426237C
Authority
CN
China
Prior art keywords
function
stack
code
storehouse
program
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Expired - Fee Related
Application number
CNB2006100516256A
Other languages
Chinese (zh)
Other versions
CN1851648A (en
Inventor
陈天洲
沙峰
谢斌
赵懿
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhejiang University ZJU
Original Assignee
Zhejiang University ZJU
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhejiang University ZJU filed Critical Zhejiang University ZJU
Priority to CNB2006100516256A priority Critical patent/CN100426237C/en
Publication of CN1851648A publication Critical patent/CN1851648A/en
Application granted granted Critical
Publication of CN100426237C publication Critical patent/CN100426237C/en
Expired - Fee Related legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Devices For Executing Special Programs (AREA)
  • Executing Machine-Instructions (AREA)

Abstract

The present invention relates to the field of embedded system program compiling technique, which aims at providing a stack overflow protection method in operating an embedded system. The method comprises (1), a compiler analyzes and scans an original program to generate a program function call table for being used for optimizing a rollback function; (2), the compiler inserts detection codes in functions at running time to detect the use state of a stack, and information is returned for being used by the inserted processing codes at running time; (3), the compiler inserts the processing codes in the functions when the stack runs; (4), the codes inserted into the functions are optimized by adopting function rollback technique. The present invention does not need inserting the codes in each nested function, and thus, code number can be reduced greatly. The stack space can be dynamically detected when an application program runs by the method and relevant processing is carried out, and thus, the possibility of stack operation overflowing can be avoided, and the system can be guaranteed to run at safe precondition ultimately.

Description

A kind of stack overflow protection method in operation for embedded system
Technical field
The present invention relates to embedded system program technique of compiling field, particularly relate to a kind of stack overflow protection method in operation for embedded system.
Background technology
Obtained significant progress nearest two, 30 years built-in fields, the development of many industries has be unable to do without the participation of built-in field, as: automobile, aviation, heavy industry, robot, intelligent house keeper etc. are numerous.The demand of these industry built-in fields has been impelled the development of embedded system, has also proposed more and more higher requirement simultaneously.
Present embedded system mainly is divided into high-end embedded system and low side embedded system two big classes, so-called high-endly embeddedly is meant that those have high processing rate, the systems that use more, and there is senior hardware device in these systems.And low side is meant the general systems in aspect such as processing speed, function.Often there is the problem of space constraint in these systems on internal memory.So they are used in different environment because different characteristics is arranged, and different function and efficient is provided.
All there is the problem of low memory greatly in the embedded system of low side, and the reason that causes this problem is mainly from two aspects: the one, and the restriction on the hardware technology, the 2nd, the restriction on the cost.Low memory has seriously influenced the safety that runs on the program in the system.Exception error can take place to the internal memory space requirement than higher program in some under the situation of not having warning, finally cause system crash.Though the problem odds such for small programs is very little, as long as such possibility is arranged, just there is hidden danger in system so.
For many years, the research that restriction causes storehouse to overflow at embedded system memory has a lot, but wherein some is too theoretical, and feasibility is too poor.As: the internal memory compression.The purpose of internal memory compression is exactly to store more information in limited space, but data compression time and data decompression time that this method costs a lot of money, so plant effective very poor in the time application.
Summary of the invention
The objective of the invention is to overcome deficiency of the prior art, a kind of stack overflow protection method in operation for embedded system is provided.
The technical scheme that the present invention solves its technical matters employing is as follows:
Stack overflow protection method in operation for embedded system may further comprise the steps:
(1) compiler analysis scan original program, generator program function call table uses during for the backrush function optimization;
(2) compiler is inserted into beginning place of program function with detection of code, detects the user mode of storehouse, and return message, handles the code use during for the operation of inserting;
Handle code when (3) compiler inserts the storehouse operation in function:
If not having when operation exception error takes place then handles code and do not do anything;
During wrong generation:
A, there is available space more than needed at the place if program is found the stack space bottom, then adjusts the position of stack register and the position of storehouse reference register, thereby enlarges stack space, and the data in the former storehouse of translation simultaneously are at the bottom of the new storehouse; Recovery causes the state before the wrong function call, and re-executes function call;
B, if find to seek available space at whole memory headroom in useful space in stack space bottom;
If in whole memory headroom, have and use the space, then utilize Memory Allocation to call, distribute an internal memory, and connect together by data structure and former storehouse, form new storehouse, and the data in the former storehouse of translation arrive at the bottom of the new storehouse; Recovery causes the state before the wrong function call, and re-executes function call;
If do not have free space in whole memory headroom, then program is suspended, and is placed into once more in the operating system task formation when by the time free space being arranged and moves;
(4) adopt function backrush technology, optimize the code that in function, inserts:
To generic function, be left intact;
To the function of nesting allocation, carry out function backrush optimization, from the function call table, form and call the path, the code that all insert in the path is simplified in the function that merges to top layer the simplified code amount in function.
The present invention compares with background technology, and the useful effect that has is:
The present invention handles the place that begins that code is inserted into each function when detection of code is with operation when adopting technique of compiling to move, when nested function occurring and call, adopt detection of code backrush technology, only in initial call function, insert code, and need not in each nested function, to insert code.So just reduced size of code greatly.By such method just can be when application program be moved the detection of dynamic stack space, and do corresponding processing, get rid of the possibility that the storehouse operation is overflowed take place, the system that finally guarantees can move under the prerequisite of safety.
Description of drawings
Fig. 1 is the schematic flow sheet of the inventive method.
Embodiment
Below in conjunction with the drawings and specific embodiments content of the present invention is described in detail.
When implementing stack overflow protection method in operation for embedded system, concrete mode is as follows:
The first step, compiler analysis scan original program, generator program function call table:
The core of this method is detection of code and a program stack processing code when inserting operation in application code.But need different insertion methods for different functions.If common single function call, then normal the insertion just can.But for the function of nesting allocation, situation is just different, compares simple function and calls just complicated many.Following code is exactly two kinds of different situations:
void?fun() void?fun1()
{ {
// inner no function call fun2 (); // call function fun2 ()
} }
For fun () and fun1 (), be exactly two kinds of different situations, need different processing.So there is the function call table to be analyzed, the function call table that obtains is used by following step.
Second step, detection of code when in function, inserting operation:
The main function of detection of code is to detect the user mode of storehouse when program run during operation, judges when whether the storehouse operation takes place and overflows, and need do following detection:
Whether with stack pointer have intersection, illustrate that overflow error took place when storehouse moved if having to intersect if detecting the downward growth position of heap.If do not intersect then illustrate that running status does not well have mistake.The information of returning is as follows:
(1) wrong, return error message, comprise the position and the function call information that causes mistake of heap at that time: function local variable, parameter.
(2) correct information is returned in inerrancy.Be left intact.
Code is inserted into beginning place of program function by compiler, so that detect immediately after the routine call function.
In the 3rd step, handle code when in function, inserting operation:
The function of handling code during operation is according to the result that second step was returned storehouse to be handled, if the result that returns of second step without any problem this partial code be left intact.If overflow error takes place during the return results explanation storehouse operation in second step, then do and check that the storehouse bottom has or not unnecessary free space:
(1) redundant space is arranged, then adjust the storehouse bottom position, the outwards expansion of storehouse bottom.This moment, skew took place in data in the stack position at the bottom of with respect to stack that is stored in originally of storehouse inside, so the data in the original stack must be moved at the bottom of the stack, and reset stack base pointer position at the bottom of the stack, stack pointer re-executes the instruction of the initiation mistake of being carried out by second step to when the position, top of setting out in advance to make arrangements.Trigger wrong function call and will be called again by such, and the set data of calling when causing mistake have been carried out record in second step, these records are used to return to the error condition back.Just can call again after expanding and cause calling of mistake in storehouse, so just mistake can be recovered, guarantee the storehouse security of program run.
(2) do not have redundant space, then the available internal memory space is sought by system call by system in whole free memory,
(3) in whole internal memory, free memory is arranged, then distributes an internal memory, and this piece internal memory coupled together at the bottom of by data structure and original stack and the former storehouse of translation in data to new storehouse bottom, reset stack base pointer and stack pointer.
Do not have free memory in whole memory headroom, then this program is suspended, and could be put once more as carrying out in the operating system task formation after free memory is arranged by the time.
The 4th step, insert code backrush technology, optimize and insert code:
The insertion of the code in the first step and second step is not optimize, and the size of code that inserts by first, second two step is very big, so to also increased the size of code of program when providing the storehouse overflow protection for program.An effective method is to be optimized inserting code.Situation about optimizing mainly is divided into two parts:
(1) simple function optimization:
Simple function is meant that inside does not have the function of other function calls, such function, inside all is simple code, there is not nested function to call, when so compiler inserts code at this class function, only need in function simple handle when detection of code and operation when inserting operation code just can, so need not optimization.
(2) nested function optimization:
Referring to during nested function has other function call in the function, so so whole function call can relate to the many places function call, can be because the insertion code of this function need insert a large amount of codes.Main method is the function call table that utilizes the first step to generate, classification simple function nested function.Call part at nested function,, find out the function call path according to the function call table.Whichever function all is that simple function is piled up and formed, so in the function call path, according to the function call table, find out the function call path., optimize and merge rollback a code that inserts each function the path from the bottom beginning of function call path to top layer function.So just reduce the size of code that inserts, improved availability.
At last, it is also to be noted that what more than enumerate only is specific embodiments of the invention.Obviously, the invention is not restricted to above examples of implementation, many distortion can also be arranged.All distortion that those of ordinary skill in the art can directly derive or associate from content disclosed by the invention all should be thought protection scope of the present invention.

Claims (1)

1, a kind of stack overflow protection method in operation for embedded system may further comprise the steps:
(1) compiler analysis scan original program, generator program function call table uses during for the backrush function optimization;
(2) compiler is inserted into beginning place of program function with detection of code, detects the user mode of storehouse, and return message, handles the code use during for the operation of inserting;
Handle code when (3) compiler inserts the storehouse operation in function:
If not having when operation exception error takes place then handles code and do not do anything;
During wrong generation:
A, there is available space more than needed at the place if program is found the stack space bottom, then adjusts the position of stack register and the position of storehouse reference register, thereby enlarges stack space, and the data in the former storehouse of translation simultaneously are at the bottom of the new storehouse; Recovery causes the state before the wrong function call, and re-executes function call;
B, if find to seek available space at whole memory headroom in useful space in stack space bottom;
If in whole memory headroom, have and use the space, then utilize Memory Allocation to call, distribute an internal memory, and connect together by data structure and former storehouse, form new storehouse, and the data in the former storehouse of translation arrive at the bottom of the new storehouse; Recovery causes the state before the wrong function call, and re-executes function call;
If do not have free space in whole memory headroom, then program is suspended, and is placed into once more in the operating system task formation when by the time free space being arranged and moves;
(4) adopt function backrush technology, optimize the code that in function, inserts:
To generic function, be left intact;
To the function of nesting allocation, carry out function backrush optimization, from the function call table, form and call the path, the code that all insert in the path is simplified in the function that merges to top layer the simplified code amount in function.
CNB2006100516256A 2006-05-25 2006-05-25 Stack overflow protection method in operation for embedded system Expired - Fee Related CN100426237C (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CNB2006100516256A CN100426237C (en) 2006-05-25 2006-05-25 Stack overflow protection method in operation for embedded system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CNB2006100516256A CN100426237C (en) 2006-05-25 2006-05-25 Stack overflow protection method in operation for embedded system

Publications (2)

Publication Number Publication Date
CN1851648A CN1851648A (en) 2006-10-25
CN100426237C true CN100426237C (en) 2008-10-15

Family

ID=37133127

Family Applications (1)

Application Number Title Priority Date Filing Date
CNB2006100516256A Expired - Fee Related CN100426237C (en) 2006-05-25 2006-05-25 Stack overflow protection method in operation for embedded system

Country Status (1)

Country Link
CN (1) CN100426237C (en)

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101216774B (en) * 2008-01-03 2012-09-05 中国科学院计算技术研究所 Optimizing method for using idle register analog as rapid overflow space
CN102063325B (en) * 2011-01-06 2013-04-03 哈尔滨工业大学 Method for implementing multi-task real-time operating system embedded into 51 series singlechip
US8677189B2 (en) * 2011-11-16 2014-03-18 GM Global Technology Operations LLC Recovering from stack corruption faults in embedded software systems
CN104679645A (en) * 2013-11-28 2015-06-03 中国航空工业集团公司航空动力控制系统研究所 Method for detecting stack space allowance on real time

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1423196A (en) * 2001-11-30 2003-06-11 日本电气株式会社 Stack-using method
US6618797B1 (en) * 1998-11-24 2003-09-09 Secap Device and method for protection against stack overflow and franking machine using same
US20040103252A1 (en) * 2002-11-25 2004-05-27 Nortel Networks Limited Method and apparatus for protecting memory stacks
CN1609785A (en) * 2004-10-27 2005-04-27 上海大学 Stack area expanding method

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6618797B1 (en) * 1998-11-24 2003-09-09 Secap Device and method for protection against stack overflow and franking machine using same
CN1423196A (en) * 2001-11-30 2003-06-11 日本电气株式会社 Stack-using method
US20040103252A1 (en) * 2002-11-25 2004-05-27 Nortel Networks Limited Method and apparatus for protecting memory stacks
CN1609785A (en) * 2004-10-27 2005-04-27 上海大学 Stack area expanding method

Non-Patent Citations (4)

* Cited by examiner, † Cited by third party
Title
Win32平台堆栈溢出保护软件的设计. 杨荣,杨鑫.计算机应用,第23卷第10期. 2003
Win32平台堆栈溢出保护软件的设计. 杨荣,杨鑫.计算机应用,第23卷第10期. 2003 *
如何在嵌入式软件设计中发现堆栈溢出. 郭烨,曹英存.洛阳师范学院学报,第5期. 2004
如何在嵌入式软件设计中发现堆栈溢出. 郭烨,曹英存.洛阳师范学院学报,第5期. 2004 *

Also Published As

Publication number Publication date
CN1851648A (en) 2006-10-25

Similar Documents

Publication Publication Date Title
CN104102506B (en) A kind of Android Acceleration of starting methods based on ARM platforms
CN100426237C (en) Stack overflow protection method in operation for embedded system
CN101515320B (en) Vulnerability testing method in attack and system thereof
US8108627B2 (en) Array comparison and swap operations
WO2006095184A3 (en) Data processing system
CN101154185A (en) Method for performing recovery and playback when running software
CN111581090A (en) Automatic test case generation method and system based on NLP and RF framework
CN102929656A (en) Method for using ActiveX plug-in in browser, and client
CA2561204A1 (en) System and method for recovering from a software processing error
CN101216792B (en) Real-time operating system tasks management process and device of real-time operating system
US8261251B2 (en) Modification of array access checking in AIX
CN102147734B (en) The method and system that during operation, class is extracted
CN105302924A (en) File management method and device
CN104574275B (en) A kind of method for merging textures during modeling rendering
US20120059997A1 (en) Apparatus and method for detecting data race
CN103279329A (en) Efficient fetch production line supporting synchronous EDAC inspection
CN101751260B (en) Business object persistence processing method based on dynamic labels
CN104462953A (en) Information processing method and electronic equipment
CN100549958C (en) A kind of class file stowage and system
CN100419694C (en) System and method for testing correctness of position of installing hard disk
CN112394939B (en) User contract automatic deployment method, device and storage medium based on alliance chain
CN101763537A (en) Processing method for automatic storage of returned goods
CN101329623B (en) Computer system and memory control method
CN103019865A (en) Virtual machine monitoring method and system
CN101441572A (en) Method for implementing operation code loading in DSP chip

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant
C17 Cessation of patent right
CF01 Termination of patent right due to non-payment of annual fee

Granted publication date: 20081015

Termination date: 20110525