Search

CN-121996301-A - SCM stack use limit evaluation method based on real-time interrupt sampling, device, storage medium and embedded system thereof

CN121996301ACN 121996301 ACN121996301 ACN 121996301ACN-121996301-A

Abstract

A method for evaluating the use limit of single-chip microcomputer stack based on real-time interrupt sampling includes setting/calling a periodical interrupt source with highest interrupt priority, directly reading the instantaneous value of hardware stack pointer register when the execution of service program of highest interrupt priority is finished, comparing the read instantaneous value with the minimum value of stack pointer history, updating the minimum value of stack pointer according to the growing direction of stack memory, and calculating the history use peak value of stack memory based on the updated minimum value of stack pointer and the initial address of stack memory. By reading the instantaneous value of the stack pointer register when the interrupt occurs, the invention obtains accurate measurement results and eradicates all security risks introduced by writing and detecting the memory content. Because only the minimum historical value of the stack pointer is required to be dynamically maintained, accurate measurement results can be obtained under the condition of low cost of performance resources.

Inventors

  • LI DA
  • HE ZI
  • LUO XUE
  • BI LEI
  • BI CHAO

Assignees

  • 峰岹科技(深圳)股份有限公司

Dates

Publication Date
20260508
Application Date
20251223

Claims (10)

  1. 1. A singlechip stack use limit evaluation method based on real-time interrupt sampling is characterized by comprising the following steps: setting/calling a periodic interrupt source with the highest interrupt priority; When the execution of the service program of the interrupt with the highest priority is finished, directly reading the instantaneous value of the hardware stack pointer register; comparing the read stack pointer instantaneous value with a dynamically maintained stack pointer history minimum value, and updating the stack pointer history minimum value according to the growth direction of a stack memory; And calculating to obtain a historical use peak value of the stack memory based on the updated historical minimum value of the stack pointer and the initial address of the stack memory.
  2. 2. The method for evaluating the use limit of the single-chip microcomputer stack based on real-time interrupt sampling according to claim 1, wherein the updating the stack pointer history minimum value according to the growth direction of the stack memory specifically comprises: and if the stack memory grows towards the low address direction and the stack pointer instantaneous value is smaller than the stack pointer history minimum value, updating the stack pointer history minimum value by using the instantaneous value.
  3. 3. The method for evaluating the usage limit of the singlechip stack based on real-time interrupt sampling according to claim 1, wherein the method for evaluating the usage limit of the singlechip stack based on real-time interrupt sampling further comprises the following steps: And initializing the minimum historical value of the stack pointer as the starting address of the stack memory during system initialization.
  4. 4. The method for evaluating the usage limit of the single-chip microcomputer stack based on the real-time interrupt sampling according to claim 1, wherein the calculating obtains the historical usage peak value of the stack memory, and further comprises: comparing the historical usage peak value with a preset safety threshold value; and if the historical use peak value is larger than the preset safety threshold value, triggering stack overflow early warning.
  5. 5. The method for evaluating the utilization limit of a single-chip microcomputer stack based on real-time interrupt sampling according to claim 1, wherein the minimum value of the history of the stack pointer is stored in a nonvolatile memory and is used for keeping history peak information after system reset or dormancy wakeup.
  6. 6. The method for evaluating the utilization limit of a singlechip stack based on real-time interrupt sampling according to any one of claims 1 to 5, wherein the period of the periodic interrupt source is not in an integer multiple relationship with the period of any other periodic task in the singlechip system.
  7. 7. A storage medium, wherein the storage medium stores a real-time interrupt sampling-based single-chip stack use limit evaluation program, which when executed by a microprocessor, implements the steps corresponding to the real-time interrupt sampling-based single-chip stack use limit evaluation method according to any one of claims 1 to 6.
  8. 8. A singlechip stack use limit evaluation device based on real-time interrupt sampling, comprising: The interrupt configuration module is used for setting a periodic interrupt source and configuring the periodic interrupt source as the highest priority interrupt in the singlechip system; the stack pointer sampling module is used for directly reading the instantaneous value of the hardware stack pointer register when the execution of the service program of the highest priority interrupt is finished; The comparison and updating module is used for comparing the read stack pointer instantaneous value with a dynamically maintained stack pointer history minimum value and updating the stack pointer history minimum value according to the growth direction of the stack memory; And the peak value calculation module is used for calculating and obtaining the historical use peak value of the stack memory based on the historical minimum value of the stack pointer and the starting address of the stack memory.
  9. 9. The device for evaluating the utilization limit of a single-chip microcomputer stack based on real-time interrupt sampling according to claim 8, wherein the period of the periodic interrupt source configured by the interrupt configuration module is not in an integer multiple relationship with the period of any other periodic task in the single-chip microcomputer system.
  10. 10. An embedded system, comprising: a microprocessor; the memory is used for storing program instructions and being used as a stack memory; A hardware timer configured to generate a periodic interrupt; Wherein the microprocessor is configured to execute program instructions stored in the memory to implement the real-time interrupt sampling based single chip stack usage limit evaluation method of any one of claims 1 to 6.

Description

SCM stack use limit evaluation method based on real-time interrupt sampling, device, storage medium and embedded system thereof Technical Field The invention relates to the technical field of stack use limit evaluation, in particular to a singlechip stack use limit evaluation method based on real-time interrupt sampling, a device, a storage medium and an embedded system thereof. Background In the embedded system design based on the middle-low end singlechip, a foreground and background software architecture without an operating system (namely 'bare computer') is adopted, and is a widely applied efficient and reliable solution. The memory in the stack plays a key role in storing function return addresses, parameters, local variables, and interrupt contexts in such systems. However, the amount of stack usage is not fixed, but rather varies dynamically with the nesting depth of function calls, the triggering of interrupt services, and the level of interrupt nesting that may occur. This strong dynamics and uncertainty makes stack memory overflow an extremely hidden and dangerous potential fault in the system. Once stack overflow occurs, adjacent critical data areas will be destroyed, resulting in program execution exceptions, hardware errors, and even overall system crashes, and such failures are difficult to reproduce and debug. Currently, the evaluation of stack usage limits relies mainly on two types of technical means. One is static analysis, namely, in the compiling and linking stage, a stack usage report of each function is generated by means of compiler options, and theoretical worst call path estimation is carried out by combining a memory mapping File (Map File) output by a linker. The limitation of the method is that the analysis of the static control flow based on the codes is completely, and the extremely complex runtime scene of interrupt random triggering and nesting thereof cannot be effectively simulated and covered, so that the estimation result is often too pessimistic or has larger deviation from the actual running condition, thereby causing the waste of memory resources or the failure of the estimation result. The second is a dynamic monitoring method, with the "stack fill mode method" being the most typical. The method fills the reserved stack space into a specific mode value (for example, 0 xCD) in the system initialization stage, and after the system is subjected to full test operation for a period of time, searches the boundary between the rewritten mode value and the non-rewritten mode value by scanning the whole stack space, so as to infer the historical maximum stack depth in the operation period. Although the method is closer to the actual running state than static analysis, three inherent defects still exist, namely, firstly, traversing and scanning the whole reserved stack area belongs to computationally intensive operation, and even if the method is executed at a lower frequency, precious CPU cycles of a single chip microcomputer are continuously consumed, and non-negligible performance overhead is introduced in the application with the tensed resources, so that the real-time response capability of the system is affected. Second, the accuracy of the method builds on the assumption that "stack memory data, once written, is no longer changed". However, in actual operation, after the stack frame used by the previous function or interrupt service routine exits, the corresponding memory area is likely to be written, covered, and even cleared again by the subsequent different function or interrupt service routine, thereby resulting in the previously recorded historical "highest water line" trace being erased. This makes the scan result unable to reflect the actual peak stack usage, possibly giving a false security indication, misleading the developer. Again, to ensure the accuracy of the scan stack usage trace, the fill value is better than 0 with a non-zero value, but the program developer may behave as defining local variables and starting operation without an initial value, such as performing an "or" and "operation, or a self-increasing, self-decreasing operation, etc., since it is habitually default to 0, when the stack content is filled with a non-zero value, it may cause unexpected results, affecting the program security. If the padding value is 0, the local variable is often calculated to be 0, so that the trace which is not used by the stack can not be scanned. Again, some functions define that the local variables do not actually use it, which is still stack space consuming in cases where compilers do not take optimization, in which case the fill scan may miss statistics of them, resulting in a lower evaluation result. In the prior art, on the premise of ensuring extremely low system overhead, the use peak value of the stack memory during the operation of the bare metal system is difficult to monitor accurately in real time. Therefore, a new scheme capable of realizing real-time