Controller stack detection method and device, electronic equipment and storage medium

By acquiring and comparing the usage and allocation amount of the stack in the MCU, predicting the spatial state of the stack, the real-time problem of stack overflow detection in the prior art is solved, dynamic monitoring and prediction are realized, resource consumption is reduced, and system is improved stability.

CN118897764APending Publication Date: 2024-11-05DEEPAL AUTOMOBILE TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410933219.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-12
Publication Date
2024-11-05

AI Technical Summary

Technical Problem

The prior art cannot realize dynamic monitoring of the stack during the MCU software operation, and lacks real-time performance, resulting in the inability to discover and solve the problem of stack overflow in a timely manner.

Method used

After the previous call function of the controller currently executing task is completed, the current to be called function, stack usage and stack allocation amount of the currently executed task is obtained, the stack remaining amount is calculated, and the estimated stack consumption is compared, and the prediction is made. The spatial state of the stack, if overflowed, an exception flag is generated and returned.

Benefits of technology

Dynamic stack prediction during the operation of the controller software is realized, and it can predict whether the stack has overflow risk before calling the function, reducing the controller resource consumption and improving the stability of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118897764A_ABST
    Figure CN118897764A_ABST
Patent Text Reader

Abstract

The invention provides a controller stack detection method and device, electronic equipment and a storage medium, and the method comprises the steps: obtaining a current to-be-called function in a current execution task, and a stack usage amount and a stack distribution amount of a stack corresponding to the current execution task after the calling of a last calling function in the current execution task of a controller is finished; according to the stack distribution amount and the stack usage amount, the stack residual amount of the stack is determined, the stack residual amount is compared with the estimated stack consumption amount of the current to-be-called function, a comparison result is obtained, the estimated stack consumption amount of the current to-be-called function is obtained based on the historical stack consumption amount of the current to-be-called function, and the current to-be-called function is obtained based on the historical stack consumption amount of the current to-be-called function. Based on the comparison result, the space state of the stack is predicted when the current execution task calls the current to-be-called function, and the space state comprises overflow or normal; dynamic prediction of stack overflow can be realized, the system overhead of the controller in the whole process is small, and the resource consumption of the controller can be effectively reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of stack detection, and in particular to a controller stack detection method, device, electronic device and storage medium. Background Art

[0002] In recent years, with the continuous advancement of MCU (Microcontroller Unit) technology, hardware functions have become increasingly powerful, and the corresponding software systems have become increasingly complex. This increase in complexity has put forward higher requirements for the stability and reliability of software, especially in stack management. Stack overflow is one of the common serious problems in embedded system development. Once the stack overflows for some reason, it is likely to tamper with key system variables or data, which will lead to a series of unpredictable consequences, including system crashes, data loss, and even hardware damage. Therefore, it is particularly important to monitor or detect the stack of MCU in order to timely discover and solve potential stack overflow problems and ensure the stable operation of the MCU system.

[0003] Chinese patent CN102012833A discloses a static stack detection method based on the intermediate results of the compilation process. The solution of this patent uses the intermediate files of the compiler to perform static analysis of the stack to determine whether a function has the risk of stack overflow. It cannot realize dynamic monitoring of the stack during the operation of the program software and lacks real-time performance. Summary of the invention

[0004] In view of the shortcomings of the prior art described above, the present application provides a controller stack detection method, device, electronic device and storage medium to solve the technical problem that the above-mentioned static stack detection cannot realize dynamic monitoring of the stack during the operation of the program software and lacks real-time performance.

[0005] The present application provides a controller stack detection method, which includes: after the last calling function in the current executing task of the controller is called, obtaining the stack usage and stack allocation of the current to-be-called function in the current executing task and the stack corresponding to the current executing task; determining the stack remaining amount of the stack according to the stack allocation amount and the stack usage, and comparing the stack remaining amount with the estimated stack consumption of the current to-be-called function to obtain a comparison result, wherein the estimated stack consumption of the current to-be-called function is obtained based on the historical stack consumption of the current to-be-called function; and predicting the space status of the stack when the current executing task calls the current to-be-called function based on the comparison result, wherein the space status includes overflow or normal.

[0006] In one embodiment of the present application, before comparing the stack remaining amount with the estimated stack consumption of the current function to be called, the controller stack detection method includes: obtaining multiple historical stack consumptions of the current function to be called; determining the maximum historical stack consumption from the multiple historical stack consumptions; determining the estimated stack consumption based on the maximum historical stack consumption, so as to compare the stack remaining amount with the estimated stack consumption.

[0007] In one embodiment of the present application, after predicting the space state of the stack when the currently executing task calls the currently to-be-called function based on the comparison result, the controller stack detection method includes: if it is predicted that the space state of the stack is normal, then determining the currently to-be-called function as the currently calling function, and triggering the current task to call the currently calling function; if it is predicted that the space state of the stack is overflow, then generating a stack exception flag based on the currently to-be-called function and returning.

[0008] In one embodiment of the present application, after triggering the current task to call the current calling function, the controller stack detection method includes: reading the stack pointer value of the stack at the start time and the end time of the call of the current calling function, respectively, to obtain the start stack pointer value and the end stack pointer value corresponding to the current calling function; calculating the current stack consumption of the current calling function according to the start stack pointer value and the end stack pointer value corresponding to the current calling function; comparing the current stack consumption of the current calling function with the estimated stack consumption of the current calling function, if the current stack consumption of the current calling function is greater than the estimated stack consumption of the current calling function, then using the current stack consumption of the current calling function as the new estimated stack consumption of the current calling function.

[0009] In one embodiment of the present application, after the current task is triggered to call the current calling function, the controller stack detection method includes: reading the stack pointer value of the stack at the call start time and call end time of the current calling function and the call end time of the current calling sub-function, respectively, to obtain the start stack pointer value and end stack pointer value corresponding to the current calling function and the end stack pointer value corresponding to the current calling sub-function, wherein the current calling sub-function is a function called within the current calling function; if the end stack pointer value corresponding to the current calling function is greater than or equal to the end stack pointer value corresponding to the current calling sub-function, then according to the start stack pointer value corresponding to the current calling function and the end stack pointer value of the current calling sub-function Use the end stack pointer value corresponding to the function to calculate the current stack consumption of the currently called function; if the end stack pointer value corresponding to the currently called function is less than the end stack pointer value corresponding to the currently called sub-function, then calculate the current stack consumption of the currently called function according to the start stack pointer value corresponding to the currently called function and the end stack pointer value corresponding to the currently called sub-function; compare the current stack consumption of the currently called function with the estimated stack consumption of the currently called function, if the current stack consumption of the currently called function is greater than the estimated stack consumption of the currently called function, then use the current stack consumption of the currently called function as the new estimated stack consumption of the currently called function.

[0010] In one embodiment of the present application, if it is predicted that the space status of the stack is overflow, the controller stack detection method also includes: obtaining the next function to be called in the currently executing task; comparing the remaining amount of the stack with the estimated stack consumption of the next function to be called, and predicting the space status of the stack when the currently executing task calls the next function to be called based on the comparison result, wherein the estimated stack consumption of the next function to be called is obtained based on the historical stack consumption of the next function to be called.

[0011] In one embodiment of the present application, the space status of the stack is predicted based on the comparison result when the currently executing task calls the currently to-be-called function, including: when the stack remaining amount is greater than or equal to the estimated stack consumption of the currently to-be-called function, the stack space status is determined to be normal; when the stack remaining amount is less than the estimated stack consumption of the currently to-be-called function, the stack space status is determined to be overflow.

[0012] In one embodiment of the present application, a controller stack detection device is also provided, and the controller stack detection device includes: a data acquisition module, which is used to obtain the current to-be-called function in the current execution task, the stack usage and stack allocation amount of the stack corresponding to the current execution task after the call of the last calling function in the current execution task of the controller is completed; an information processing module, which is used to determine the stack remaining amount of the stack according to the stack allocation amount and the stack usage, and compare the stack remaining amount with the estimated stack consumption of the current to-be-called function, and predict the space status of the stack when the current execution task calls the current to-be-called function based on the comparison result, wherein the estimated stack consumption is obtained based on the historical stack consumption of the current to-be-called function, and the space status includes overflow or normal.

[0013] In one embodiment of the present application, an electronic device is also provided, comprising: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the electronic device implements the controller stack detection method as described above.

[0014] In one embodiment of the present application, a computer-readable storage medium is further provided, on which a computer program is stored. When the computer program is executed by a processor of a computer, the computer executes the controller stack detection method as described above.

[0015] Beneficial effects of the present invention: The present invention provides a controller stack detection method, device, electronic device and storage medium. The method obtains the stack usage and stack allocation of the current to-be-called function and the corresponding stack of the current to-be-called function in the current to-be-called task after the call of the last called function in the current to-be-called task of the controller is completed, determines the stack remaining amount according to the stack allocation amount and the stack usage, compares the stack remaining amount with the estimated stack consumption of the current to-be-called function, and predicts the space status of the stack of the current to-be-called function when the current to-be-called function is called based on the comparison result. This method can realize dynamic prediction of whether the stack overflows, detect the stack usage of the current to-be-called task during the software running process of the controller, and predict whether the stack has overflow risk before calling the function. In addition, the controller system overhead of the entire process is small, which can effectively reduce the resource consumption of the controller.

[0016] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] The drawings herein are incorporated into the specification and constitute a part of the specification, showing embodiments consistent with the present application, and together with the specification, are used to explain the principles of the present application. Obviously, the drawings described below are only some embodiments of the present application, and for those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative work. In the drawings:

[0018] Figure 1 is a schematic diagram of an implementation environment of a controller stack detection method shown in an exemplary embodiment of the present application;

[0019] Figure 2 is a flow chart of a controller stack detection method shown in an exemplary embodiment of the present application;

[0020] Figure 3 is a stack detection flow chart shown in a specific embodiment of the present application;

[0021] Figure 4 is a flowchart of detecting stack consumption corresponding to a function shown in a specific embodiment of the present application;

[0022] Figure 5 is a flowchart of stack prediction result processing shown in a specific embodiment of the present application;

[0023] Figure 6 is a block diagram of a controller stack detection device shown in an exemplary embodiment of the present application;

[0024] Figure 7 A schematic diagram of the structure of a computer system suitable for implementing an electronic device of an embodiment of the present application is shown. DETAILED DESCRIPTION

[0025] The following will describe the implementation methods of the present application with reference to the accompanying drawings and preferred embodiments. Those skilled in the art can easily understand other advantages and effects of the present application from the contents disclosed in this specification. The present application can also be implemented or applied through other different specific implementation methods, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present application. It should be understood that the preferred embodiments are only for illustrating the present application, not for limiting the scope of protection of the present application.

[0026] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present application, and thus the drawings only show components related to the present application rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed at will, and the component layout may also be more complicated.

[0027] It should be noted that in this application, "first", "second", etc. are only used to distinguish similar objects, and are not used to limit the order or precedence of similar objects. The variations of "including", "having", etc. described above indicate that the scope covered by the subject of the word is not exclusive except for the examples shown by the word.

[0028] It is understood that the various numbers, step numbers, and other reference numerals recorded in this application are distinguished for the convenience of description and are not intended to limit the scope of this application. The size of the reference numerals in this application does not mean the order of execution. The execution order of each process should be determined by its function and internal logic.

[0029] In the following description, a large number of details are discussed to provide a more thorough explanation of the embodiments of the present application. However, it is obvious to those skilled in the art that the embodiments of the present application can be implemented without these specific details. In other embodiments, well-known structures and devices are shown in the form of block diagrams rather than in detail to avoid making the embodiments of the present application difficult to understand.

[0030] It should be noted that traditional stack detection methods are mainly divided into static detection and dynamic detection. Static detection is to use stack analysis tools to directly analyze the source files in the software code and the process files compiled by the compiler to determine whether the function has the risk of stack overflow. However, such analysis tools usually require payment, which will increase the cost of software development. Dynamic detection mainly includes software preset detection method, stack limit register detection method and MPU (Memory Protection Unit) memory protection method. Among them, the software preset detection method is the most commonly used method of RTOS (Real-Time Operating System). This method is also usually accompanied by a relatively large system overhead. If real-time monitoring is performed, the system overhead will be further increased. The stack limit register detection method and the MPU memory protection method require specific hardware support to realize the detection function.

[0031] To solve the above problems, the embodiments of the present application respectively propose a controller stack detection method, a controller stack detection device, an electronic device, a computer-readable storage medium and a computer program product, which will be described in detail below.

[0032] See also Figure 1 , Figure 1 It is a schematic diagram of an implementation environment of a controller stack detection method shown in an exemplary embodiment of the present application.

[0033] like Figure 1As shown, the implementation environment may include a controller 110 and a computer device 120, wherein the controller 110 may be at least one of an MCU, an ECU (Electronic Control Unit), a VIU (Vehicle Information Unit), a VCU (Vehicle Control Unit), a ZCU (Zone Control Unit), etc., and the computer device 120 may be at least one of a microcomputer, an embedded computer, a neural network computer, etc. The computer device 120 may be configured in the controller 110, or the computer device 120 may be an independent computer device, which is not limited here. The controller 110 may obtain the current to-be-called function of the current execution task and the stack usage of the corresponding stack of the current execution task after each function call of its own current execution task ends, and provide them to the computer device 120 for processing, so that the computer device 120 predicts whether the corresponding stack of the current execution task has an overflow risk when calling the current to-be-called function when the current execution task calls the current to-be-called function.

[0034] Exemplarily, after the last calling function in the current execution task of the controller is called, the computer device 120 obtains the stack usage and stack allocation of the current to-be-called function and the corresponding stack of the current execution task in the current execution task; determines the stack remaining amount of the stack according to the stack allocation amount and the stack usage, and compares the stack remaining amount with the estimated stack consumption of the current to-be-called function to obtain a comparison result, wherein the estimated stack consumption is obtained based on the historical stack consumption of the current to-be-called function; predicts the space state of the stack when the current execution task calls the current to-be-called function based on the comparison result, wherein the space state includes overflow or normal. It can be seen that the technical solution of the embodiment of the present application can not only realize the dynamic prediction of whether the stack overflows, but also detect the stack usage of the current execution task during the software running process of the controller, and predict whether the stack has an overflow risk before calling the function, and the whole process controller system overhead is small, which can effectively reduce the resource consumption of the controller. Some dynamic detection methods monitor the state of the stack space in real time by reading the value of the current stack pointer after each code is executed, but such an operation itself is also a consumption of the stack. The technical solution of the embodiment of the present application can monitor the state of the stack space more efficiently and with lower consumption by predicting.

[0035] It should be noted that the controller stack detection method provided in the embodiment of the present application is generally performed by the computer device 120 , and the corresponding controller stack detection device is generally disposed in the computer device 120 .

[0036] See also Figure 2 , Figure 2is a flowchart of a controller stack detection method shown in an exemplary embodiment of the present application. The method can be applied to Figure 1 The implementation environment shown is specifically executed by the computer device 120 in the implementation environment. It should be understood that the method can also be applied to other exemplary implementation environments and be specifically executed by devices in other implementation environments. This embodiment does not limit the implementation environment to which the method is applicable.

[0037] like Figure 2 As shown, in an exemplary embodiment, the controller stack detection method includes at least steps S210 to S230, which are described in detail as follows:

[0038] Step S210, after the last called function in the currently executed task of the controller is called, the stack usage and stack allocation of the current to-be-called function in the currently executed task and the stack corresponding to the currently executed task are obtained.

[0039] In one embodiment of the present application, the controller may be a controller such as an MCU, ECU, VIU, VCU, or ZCU. The controller may also be a single-core controller or a multi-core controller, which is not limited here.

[0040] It should be understood that a task is an independent execution unit responsible for executing one or more related functions or code segments, and each task has a corresponding stack for storing local variables, function call parameters, return addresses, etc. Function is the basic unit function or basic building block of programming, which is used to perform specific operations. It is mainly composed of a series of instructions or code blocks, which are executed when the function is called. A task is a container for executing functions. During the execution process, a task can call multiple functions to complete its work. The function called by the task uses the task's stack space to store local variables and return addresses, and the function is the main execution body of the task. When a function is called by a task, the instructions or code in the function will be executed.

[0041] Therefore, the currently executed task refers to the task currently being executed in the controller during the running process of the controller. During the task execution process, each function is called in sequence, and another function is called after one function call is completed. Therefore, the last called function refers to the last function called by the currently executed task, and the current to-be-called function refers to the first function waiting to be called in the calling sequence after the last called function call is completed. The stack usage refers to the size of the stack memory or stack space occupied by the currently executed task during the current execution process. The real-time stack usage dynamically reflects the stack memory usage of the currently executed task. The stack allocation refers to the size of the set stack memory or space. Usually, when creating a task, the stack space of the stack corresponding to the task is set or allocated, and the stack allocation of the stack is obtained and stored.

[0042] The process of calling the function of the currently executed task can be monitored in real time. If the call of the last called function is detected to have ended, the current function to be called of the currently executed task and the current stack usage and stack allocation of the stack corresponding to the currently executed task can be obtained. In schematic form, the stack information of the stack can be recorded when the currently executed task starts to execute, including the stack size, i.e., the stack allocation amount and the stack starting address. The stack detection function is called in the function called by the currently executed task, and the current stack pointer of the stack is read through the stack detection function. The current stack usage of the stack is determined based on the current stack pointer and the stack starting address, and recorded for subsequent stack overflow risk detection.

[0043] Step S220, determining the stack remaining amount of the stack according to the stack allocation amount and the stack usage amount, and comparing the stack remaining amount with the estimated stack consumption of the current function to be called to obtain a comparison result.

[0044] In one embodiment of the present application, the stack remaining amount refers to the size of the current available space or remaining space of the stack. After obtaining the stack usage and stack allocation amount of the stack corresponding to the currently executed task, the difference between the stack allocation amount of the stack and the stack usage of the stack can be calculated, and the difference is used as the stack remaining amount of the stack, and then the stack remaining amount is compared with the estimated stack consumption of the current function to be called.

[0045] Among them, the estimated stack consumption is obtained based on the historical stack consumption of the current function to be called. When a function is called by a task, the function will be executed on the stack of the task and use the local variables and resources of the task. Therefore, the stack consumption corresponding to the function refers to the size of the stack space occupied by the function when it is executed on the stack. Since the size of the stack space occupied by the same function changes each time it is called and executed, the historical stack consumption of each function can be obtained in advance. Before the next call and execution of the function, the stack consumption of the next call and execution of the function is estimated based on the historical stack consumption of the function to obtain the estimated stack consumption of the function. Schematically, for the current function to be called, the historical stack consumption of the current function to be called can be directly used as the estimated stack consumption of the current function to be called, or a preset change can be used, and the sum of the preset change and the historical stack consumption of the current function to be called can be used as the estimated stack consumption of the current function to be called.

[0046] In one embodiment of the present application, before comparing the stack remaining amount with the estimated stack consumption of the current function to be called, the controller stack detection method includes: obtaining multiple historical stack consumptions of the current function to be called; determining the maximum historical stack consumption from the multiple historical stack consumptions; determining the estimated stack consumption based on the maximum historical stack consumption, so as to compare the stack remaining amount with the estimated stack consumption.

[0047] In this embodiment, multiple historical stack consumptions of the current function to be called can be collected from the previous function call records. After collecting multiple historical stack consumptions, invalid or abnormal data can be filtered out, and then the remaining historical stack consumptions are traversed to find the maximum value, that is, the maximum historical stack consumption, which represents the maximum amount of stack space consumed by the current function to be called in the previous calling process. After determining the maximum value, the maximum value can be used to estimate the amount of stack space that the current function to be called may consume when it is called by the task next time. For example, the maximum historical stack consumption can be directly used as the estimated stack consumption; or a safety margin can be added to the maximum historical stack consumption. The safety margin can be a fixed value or a dynamically calculated value based on historical data to ensure that the estimated stack consumption can cover most cases; a statistical model or a machine learning algorithm can also be used to determine the estimated stack consumption based on the maximum historical stack consumption, which is not limited here.

[0048] Step S230, comparing the remaining amount of the stack with the estimated stack consumption of the current function to be called, and predicting the stack space state when the current executing task calls the current function to be called based on the comparison result.

[0049] In one embodiment of the present application, based on the size of the stack remaining amount and the estimated stack consumption of the current function to be called, it is estimated that if the currently executing task needs to call the current function to be called, the space status of the corresponding stack is estimated, wherein the space status includes overflow or normal, reflecting whether there is an overflow risk of the stack. If the stack space status is predicted to be overflow, it means that there is an overflow risk of the stack of the currently executing task when calling the current function to be called. If the stack space status is predicted to be normal, it means that there is no overflow risk of the stack of the currently executing task when calling the current function to be called.

[0050] It should be noted that the space state of the stack described in this embodiment is not the current real space state, but the future space state of the stack estimated in advance when the current executing task subsequently calls the current to-be-called function.

[0051] In one embodiment of the present application, the space status of the stack is predicted based on the comparison result when the currently executing task calls the currently to-be-called function, including: when the stack remaining amount is greater than or equal to the estimated stack consumption of the currently to-be-called function, the stack space status is determined to be normal; when the stack remaining amount is less than the estimated stack consumption of the currently to-be-called function, the space status is determined to be overflow.

[0052] In this embodiment, if the remaining amount of the stack is greater than or equal to the estimated stack consumption of the current function to be called, the space state of the stack is determined to be normal, that is, when it is estimated that the current execution task is about to call the current function to be called, the stack corresponding to the current execution task has no overflow risk or a small overflow risk, and the current execution task can be allowed to call the current function to be called. If the remaining amount of the stack is less than the estimated stack consumption of the current function to be called, the space state of the stack is determined to be overflow, that is, when it is estimated that the current execution task is about to call the current function to be called, the stack corresponding to the current execution task has an overflow risk or a large overflow risk, and a corresponding early warning mechanism can be triggered, wherein the early warning mechanism may include sending an error code to the current execution task, recording a log, reducing the priority of the current execution task, or terminating the current execution task, etc.

[0053] In one embodiment of the present application, after predicting the space state of the stack when the currently executing task calls the currently to-be-called function based on the comparison result, the controller stack detection method includes: if the space state of the stack is predicted to be normal, the currently to-be-called function is determined as the currently calling function, and the current task is triggered to call the currently calling function; if the space state of the stack is predicted to be overflow, a stack exception flag is generated based on the currently to-be-called function and returned.

[0054] In this embodiment, if the stack space state is determined to be normal, it means that calling the current function to be called will not cause stack overflow, and the currently executed task can be allowed to use the current function to be called as the current calling function and call it. If the stack space state is determined to be overflow, it means that calling the current function to be called may cause stack overflow, and the current function to be called will not be called, and the stack exception flag can be set as an early warning mechanism.

[0055] In one embodiment of the present application, if the space status of the stack is predicted to be overflow, the controller stack detection method also includes: obtaining the next function to be called in the currently executing task; comparing the remaining amount of the stack with the estimated stack consumption of the next function to be called, and predicting the space status of the stack when the currently executing task calls the next function to be called based on the comparison result, wherein the estimated stack consumption of the next function to be called is obtained based on the historical stack consumption of the next function to be called.

[0056] In this embodiment, after determining that the subsequent call to the current to-be-called function will cause a stack overflow, the stack exception flag is set, and the next to-be-called function in the current execution task is obtained in the function call order to predict the stack space state when calling the next to-be-called function. The prediction method is the same as the method of predicting the stack space state when calling the current to-be-called function recorded in the aforementioned embodiment, and will not be repeated here. If it is determined that the stack space state of the current execution task is normal when calling the next to-be-called function, the next to-be-called function is determined as the current calling function, and the current task is triggered to call the current calling function; if it is determined that the stack space state of the current execution task is overflowing when calling the next to-be-called function, a stack exception flag is generated based on the next to-be-called function and returned, and then the remaining to-be-called functions of the current execution task are traversed in the function call order, and the stack remaining amount is compared with the estimated stack consumption of the to-be-called function, and the stack space state of the current execution task when calling the to-be-called function is predicted based on the comparison result, until it is predicted that the stack space state of the current execution task when calling a to-be-called function is normal, or all the remaining to-be-called functions of the current execution task are traversed.

[0057] The technical solution of this embodiment dynamically adjusts the task execution function according to the prediction result after determining that the subsequent call of the currently called function will cause a stack overflow, so that the stack of the currently executed task will not overflow. It only sets some stack exception flags, which can effectively prevent the entire controller software from falling into a serious abnormal state.

[0058] In one embodiment of the present application, after the current task is triggered to call the current calling function, the controller stack detection method includes: reading the stack pointer value of the stack at the start time and the end time of the call of the current calling function, respectively, to obtain the start stack pointer value and the end stack pointer value corresponding to the current calling function; calculating the current stack consumption of the current calling function according to the start stack pointer value and the end stack pointer value corresponding to the current calling function; comparing the current stack consumption of the current calling function with the estimated stack consumption of the current calling function, if the current stack consumption of the current calling function is greater than the estimated stack consumption of the current calling function, then using the current stack consumption of the current calling function as the new estimated stack consumption of the current calling function.

[0059] In this embodiment, after the current task is triggered to call the current calling function, the estimated stack consumption of the current calling function can also be updated. At the start time of the call of the current calling function, the stack pointer value of the stack corresponding to the current executing task is read once and recorded as the start stack pointer value corresponding to the current calling function. At the end time of the call of the current calling function, the stack pointer value of the stack corresponding to the current executing task is read once and recorded as the end stack pointer value corresponding to the current calling function. Among them, the start stack pointer value corresponding to the current calling function can represent the minimum pointing position of the stack pointer when calling the current calling function, and the end stack pointer value corresponding to the current calling function can represent the maximum pointing position of the stack pointer when calling the current calling function. Then calculate the difference between the start stack pointer value corresponding to the current calling function and the end stack pointer value corresponding to the current calling function as the current stack consumption of the current calling function; if the current stack consumption is greater than the estimated stack consumption of the current calling function, then the current stack consumption is used as the new estimated stack consumption of the current calling function to realize the update of the estimated stack consumption of the current calling function; otherwise, the estimated stack consumption of the current calling function is not updated.

[0060] In another embodiment of the present application, after the current task is triggered to call the current calling function, the controller stack detection method includes: reading the stack pointer value of the stack at the call start time and call end time of the current calling function and the call end time of the current calling sub-function, respectively, to obtain the start stack pointer value and end stack pointer value corresponding to the current calling function and the end stack pointer value corresponding to the current calling sub-function, wherein the current calling sub-function is a function called within the current calling function; if the end stack pointer value corresponding to the current calling function is greater than or equal to the end stack pointer value corresponding to the current calling sub-function, then according to the start stack pointer value corresponding to the current calling function and the current The end stack pointer value corresponding to the calling function is used to calculate the current stack consumption of the current calling function; if the end stack pointer value corresponding to the current calling function is less than the end stack pointer value corresponding to the current calling sub-function, then the current stack consumption of the current calling function is calculated according to the start stack pointer value corresponding to the current calling function and the end stack pointer value corresponding to the current calling sub-function; the current stack consumption of the current calling function is compared with the estimated stack consumption of the current calling function. If the current stack consumption of the current calling function is greater than the estimated stack consumption of the current calling function, then the current stack consumption of the current calling function is used as the new estimated stack consumption of the current calling function.

[0061] In this embodiment, when a task calls a function, the function may include calls to other functions, that is, a function (called the caller) can call another function (called the callee), and the callee will also use the stack space to store local variables and return addresses. The callee can be called the sub-function of the caller. Therefore, the current calling sub-function refers to the function called by the current calling function as the caller, that is, the callee. Schematically, the number of currently called sub-functions can be one or more. Due to the characteristics of the stack, after a function call is completed, the stack will also be released accordingly. Therefore, if you need to find the maximum pointing position of the stack pointer when calling the function, you need to count the stack usage of the function and all sub-functions called in the function.

[0062] Therefore, for the current calling function that needs to call other functions, in addition to obtaining the start stack pointer value and the end stack pointer value corresponding to the current calling function, it is also necessary to read the stack pointer value of the stack corresponding to the current execution task once at the end of the call of each current calling sub-function and record it as the end stack pointer value corresponding to each current calling sub-function. Then the end stack pointer value corresponding to the current calling function is compared with the end stack pointer value corresponding to each current calling sub-function respectively. If the end stack pointer value corresponding to the current calling function is greater than or equal to the end stack pointer value corresponding to all the current calling sub-functions, it means that the end stack pointer value corresponding to the current calling function is the maximum pointing position of the stack pointer when calling the current calling function, then the difference between the start stack pointer value corresponding to the current calling function and the end stack pointer value corresponding to the current calling function is calculated as the current stack consumption of the current calling function; if the end stack pointer value corresponding to the current calling function is less than the maximum value among the end stack pointer values ​​corresponding to all the current calling sub-functions, it means that the maximum value is the maximum pointing position of the stack pointer when calling the current calling function, then the difference between the start stack pointer value corresponding to the current calling function and the maximum value is calculated as the current stack consumption of the current calling function. Then, the current stack consumption is compared with the estimated stack consumption of the current calling function. If the current stack consumption is greater than the estimated stack consumption, the current stack consumption is used as the new estimated stack consumption of the current calling function to update the estimated stack consumption; otherwise, the estimated stack consumption is not updated.

[0063] In addition, at the start time of the call of the current calling sub-function, the stack pointer value of the stack can be read as the start stack pointer value corresponding to the current calling sub-function, and the difference between the start stack pointer value corresponding to the current calling sub-function and the end stack pointer value corresponding to the current calling sub-function can be calculated as the current stack consumption of the current calling sub-function. If the current stack consumption of the current calling sub-function is greater than the estimated stack consumption of the current calling sub-function, the current stack consumption of the current calling sub-function is used as the new estimated stack consumption of the current calling sub-function, wherein the estimated stack consumption of the current calling sub-function is obtained based on the historical stack consumption of the current calling sub-function.

[0064] The technical solutions of the above two embodiments can improve the accuracy of predicting the function's future stack requirements by continuously updating the estimated stack consumption of the function each time the function is called. As the controller program runs, more actual data about the function's stack consumption will be collected, which can more accurately estimate the stack space required by the function, thereby further improving the accuracy of predicting the space status of the stack corresponding to the task when the function is called.

[0065] Below Figure 3 , Figure 4 and Figure 5 The scheme of the present application is described as a specific embodiment.

[0066] See also Figure 3 , Figure 3 FIG. 1 is a flow chart of stack detection according to a specific embodiment of the present application. Figure 3 As shown, the stack detection process is as follows:

[0067] First, you can choose whether to enable stack detection for the task after the task starts executing. If not, execute the task normally; if yes, enable stack monitoring, read the stack information of the task, record the starting address and stack size of the stack, and perform stack monitoring and prediction. Before calling a function, you can embed assembly instructions in the program to read the stack pointer to obtain the current stack usage. Based on the current stack usage, predict whether the stack space is sufficient when calling the function. If not, skip the function and set the stack exception flag, and then continue to judge the call of other functions; if yes, execute the function, and then continue to judge the call of other functions.

[0068] See also Figure 4 , Figure 4 FIG. 1 is a flowchart of detecting the stack consumption corresponding to a function according to a specific embodiment of the present application. Figure 4 As shown, the function corresponding stack consumption detection process is as follows:

[0069] First, obtain the value of the stack pointer at the beginning and end of the current function F, i.e., the start time and the end time of the call. The value of the stack pointer at the end can be used as a reference for the stack usage of the current function F; compare this value with the value of the stack pointer at the end of other functions S called in the current function F, and select the largest value as the maximum pointing position of the stack pointer when the current function F is called; calculate the current stack consumption of the current function F according to the value of the stack pointer at the start of the call of the current function F and the maximum pointing position, and record it; this operation is performed for each function call, and the current stack consumption will be compared with the current record value before each record is updated, and only the maximum value of the two will be recorded to maintain the maximum stack usage of the function during operation, so as to update the estimated stack consumption and ensure that stack overflow does not occur during subsequent function execution. For example, the values ​​of F2 and S2 can be compared, where S2 and F2 are the stack pointer values ​​at the end of the called function S and the current function F, respectively, that is, the end time of each call. The larger value is the stack pointer value at the maximum consumption of the stack corresponding to the current function F. The difference between the maximum value of F2 and S2 and F1 can be calculated to obtain the current stack consumption of function F.

[0070] See also Figure 5 , Figure 5FIG. 1 is a flowchart of stack prediction result processing shown in a specific embodiment of the present application. Figure 5 As shown, the stack prediction result processing flow is as follows:

[0071] After the stack monitoring operation is completed, the current stack usage and the stack consumption of subsequent functions are obtained. That is, before calling a function, the current stack remaining amount is compared with the overhead required for the function call. If the comparison result does not exceed the stack allocation amount corresponding to the task, it means that the stack prediction has no overflow risk, and the subsequent function is called. If the result exceeds, it means that the stack prediction has an overflow risk, the function is not called, and the stack exception flag is returned to the task.

[0072] For the detailed process of this specific embodiment, please refer to the records in the above-mentioned embodiments, which will not be described here. The technical solution of this specific embodiment can realize that the controller detects the stack of the task during operation and records the stack overhead of each stage of the task operation, and predicts the usage of the stack based on this, and determines whether the subsequent operation will cause the stack overflow. The task execution function is dynamically adjusted according to the prediction result, so that the stack of the task will not overflow, but only some stack exception flags are set, thereby avoiding the entire software from falling into a serious abnormal state and reducing the resource consumption of the MCU.

[0073] First, through stack monitoring, the stack size and usage of the current task can be obtained in the currently executed task, and the stack overhead of the function call in the task can be recorded. The stack usage of the current task is obtained by reading the stack pointer. The stack allocation of the current task is obtained by calling the stack detection start interface at the beginning of task execution, and the stack detection function is called in the function called by the task to record the stack usage of the function for subsequent stack overflow prediction. Compared with the traditional preset detection method, the detection speed is faster, and there is no need to actively clear the contents of the stack, reducing the risk of the stack being modified by manual misoperation.

[0074] Secondly, through stack overflow prediction processing, it is possible to detect whether the stack will overflow, and process subsequent function calls based on the detection results. According to the stack usage obtained by stack monitoring and the stack consumption required for the next step, the remaining amount of the current stack is compared with the stack consumption of the next function to be called. If the consumption required to call the function is greater than the current remaining amount, it is considered that the stack will overflow, otherwise it is considered that the stack will not overflow. If the prediction result is that there will be no overflow, the subsequent function will be executed directly. If the prediction result is that there will be an overflow, the execution of the code will be actively intervened and the abnormal state will be reported. The risk of software abnormalities caused by stack overflow can be prevented in advance.

[0075] See also Figure 6 , Figure 6 is a block diagram of a controller stack detection device shown in an exemplary embodiment of the present application. The device can be applied to Figure 1 The implementation environment shown in the figure is specifically configured in the computer device 120. The device can also be applied to other exemplary implementation environments and specifically configured in other devices. This embodiment does not limit the implementation environment to which the device is applied.

[0076] like Figure 6 As shown, the exemplary controller stack detection device includes: a data acquisition module 610, which is used to obtain the stack usage and stack allocation of the current to-be-called function in the current execution task and the stack corresponding to the current execution task after the call of the last called function in the current execution task of the controller is completed; an information processing module 620, which is used to determine the stack remaining amount of the stack according to the stack allocation amount and the stack usage, compare the stack remaining amount with the estimated stack consumption of the current to-be-called function, and predict the space status of the stack when the current execution task calls the current to-be-called function based on the comparison result, wherein the estimated stack consumption is obtained based on the historical stack consumption of the current to-be-called function, and the space status includes overflow or normal.

[0077] It should be noted that the controller stack detection device provided in the above embodiment and the controller stack detection method provided in the above embodiment belong to the same concept, wherein the specific manner in which each module and unit performs operations has been described in detail in the method embodiment, and will not be repeated here. In practical applications, the controller stack detection device provided in the above embodiment can allocate the above functions to different functional modules as needed, that is, divide the internal structure of the device into different functional modules to complete all or part of the functions described above, and this is not limited here.

[0078] An embodiment of the present application also provides an electronic device, comprising: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by the one or more processors, the electronic device implements the controller stack detection method provided in the above-mentioned embodiments.

[0079] See also Figure 7 , Figure 7 The structure diagram of the computer system suitable for implementing the electronic device of the embodiment of the present application is shown. It should be noted that: Figure 7 The computer system 700 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0080] like Figure 7As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) 702 or the program loaded from the storage part 708 to the random access memory (RAM) 703, such as executing the method described in the above embodiment. In the RAM 703, various programs and data required for system operation are also stored. The CPU 701, ROM 702 and RAM 703 are connected to each other through a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0081] The following components are connected to the input / output interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN (Local Area Network) card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the input / output interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed so that a computer program read therefrom is installed into the storage section 708 as needed.

[0082] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a computer program for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication section 709, and / or installed from a removable medium 711. When the computer program is executed by a central processing unit (CPU) 701, various functions defined in the system of the present application are executed.

[0083] It should be noted that the computer-readable medium shown in the embodiment of the present application can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, wherein a computer-readable computer program is carried. This propagated data signal can take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. A computer program contained on a computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.

[0084] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the apparatus, methods and computer program products according to various embodiments of the present application. Among them, each box in the flowchart or block diagram can represent a module, a program segment, or a part of the code, and the above-mentioned module, program segment, or a part of the code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0085] The units involved in the embodiments described in this application may be implemented by software or hardware, and the units described may also be set in a processor. The names of these units do not, in some cases, constitute limitations on the units themselves.

[0086] Another aspect of the present application further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor of a computer, causes the computer to execute the controller stack detection method as described above. The computer-readable storage medium may be included in the electronic device described in the above embodiment, or may exist independently without being assembled into the electronic device.

[0087] Another aspect of the present application also provides a computer program product or a computer program, the computer program product or the computer program includes computer instructions, the computer instructions are stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the controller stack detection method provided in each of the above embodiments.

[0088] The above embodiments are merely illustrative of the principles and effects of the present application, and are not intended to limit the present application. Anyone familiar with the technology may modify or change the above embodiments without violating the spirit and scope of the present application. Therefore, all equivalent modifications or changes made by a person of ordinary skill in the art without departing from the spirit and technical ideas disclosed in the present application shall still be covered by the claims of the present application.

Claims

1. A controller stack detection method, characterized in that: The controller stack detection method comprises: After the last called function in the currently executed task of the controller is called, the current to-be-called function in the currently executed task, the stack usage and stack allocation of the stack corresponding to the currently executed task are obtained; Determine the stack remaining amount of the stack according to the stack allocation amount and the stack usage amount, and compare the stack remaining amount with the estimated stack consumption of the current function to be called to obtain a comparison result, wherein the estimated stack consumption of the current function to be called is obtained based on the historical stack consumption of the current function to be called; The space state of the stack is predicted based on the comparison result when the currently executing task calls the currently to-be-called function, wherein the space state includes overflow or normal.

2. The controller stack detection method according to claim 1, characterized in that: Before comparing the stack remaining amount with the estimated stack consumption of the current function to be called, the controller stack detection method includes: Obtaining multiple historical stack consumptions of the current function to be called; determining a maximum history stack consumption from among a plurality of history stack consumptions; The estimated stack consumption is determined based on the maximum historical stack consumption to compare the stack remaining amount with the estimated stack consumption.

3. The controller stack detection method according to any one of claims 1 or 2, characterized in that: After predicting the space state of the stack when the currently executed task calls the currently to-be-called function based on the comparison result, the controller stack detection method includes: If it is predicted that the space state of the stack is normal, the current to-be-called function is determined as the current calling function, and the current task is triggered to call the current calling function; If it is predicted that the space state of the stack is overflow, a stack exception flag is generated based on the current function to be called and returned.

4. The controller stack detection method according to claim 3, characterized in that: After triggering the current task to call the current calling function, the controller stack detection method includes: Reading the stack pointer value of the stack at the start time and the end time of the call of the currently called function, respectively, to obtain the start stack pointer value and the end stack pointer value corresponding to the currently called function; Calculate the current stack consumption of the currently called function according to the starting stack pointer value and the ending stack pointer value corresponding to the currently called function; The current stack consumption of the current calling function is compared with the estimated stack consumption of the current calling function. If the current stack consumption of the current calling function is greater than the estimated stack consumption of the current calling function, the current stack consumption of the current calling function is used as the new estimated stack consumption of the current calling function.

5. The controller stack detection method according to claim 3, characterized in that: After triggering the current task to call the current calling function, the controller stack detection method includes: Reading the stack pointer value of the stack at the call start time and call end time of the currently called function and the call end time of the currently called sub-function, respectively, to obtain the start stack pointer value and the end stack pointer value corresponding to the currently called function and the end stack pointer value corresponding to the currently called sub-function, wherein the currently called sub-function is a function called within the currently called function; If the end stack pointer value corresponding to the currently called function is greater than or equal to the end stack pointer value corresponding to the currently called sub-function, then according to the start stack pointer value corresponding to the currently called function and the end stack pointer value corresponding to the currently called function, calculate the current stack consumption of the currently called function; If the end stack pointer value corresponding to the currently called function is less than the end stack pointer value corresponding to the currently called sub-function, then according to the start stack pointer value corresponding to the currently called function and the end stack pointer value corresponding to the currently called sub-function, calculate the current stack consumption of the currently called function; The current stack consumption of the current calling function is compared with the estimated stack consumption of the current calling function. If the current stack consumption of the current calling function is greater than the estimated stack consumption of the current calling function, the current stack consumption of the current calling function is used as the new estimated stack consumption of the current calling function.

6. The controller stack detection method according to claim 3, characterized in that: If it is predicted that the space state of the stack is overflow, the controller stack detection method further includes: Obtain the next function to be called in the currently executed task; The stack remaining amount is compared with the estimated stack consumption of the next function to be called, and the space state of the stack is predicted when the current executing task calls the next function to be called based on the comparison result, wherein the estimated stack consumption of the next function to be called is obtained based on the historical stack consumption of the next function to be called.

7. The controller stack detection method according to any one of claims 1 or 2, characterized in that: Predicting the space state of the stack when the currently executing task calls the currently to-be-called function based on the comparison result includes: When the remaining amount of the stack is greater than or equal to the estimated stack consumption of the current function to be called, determining that the space state of the stack is normal; When the remaining amount of the stack is less than the estimated stack consumption of the current function to be called, it is determined that the space state of the stack is overflow.

8. A controller stack detection device, characterized in that: The controller stack detection device comprises: A data acquisition module is used to obtain the current to-be-called function in the currently executed task, the stack usage and stack allocation of the stack corresponding to the currently executed task after the last called function in the currently executed task of the controller is called; An information processing module is used to determine the stack remaining amount of the stack according to the stack allocation amount and the stack usage amount, and compare the stack remaining amount with the estimated stack consumption of the current function to be called, and predict the space status of the stack when the current executing task calls the current function to be called based on the comparison result, wherein the estimated stack consumption is obtained based on the historical stack consumption of the current function to be called, and the space status includes overflow or normal.

9. An electronic device, characterized in that: The electronic device comprises: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, enables the electronic device to implement the controller stack detection method as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor of a computer, the computer is caused to execute the controller stack detection method according to any one of claims 1 to 7.