Thread stack processing method, apparatus, device, medium, and program product
By dynamically switching between the primary and backup stack spaces of kernel-mode threads, the problem of wasted kernel-mode thread storage space is solved, thread execution efficiency and stability are improved, and parallel operation of multiple program modules is achieved.
Patent Information
- Application Number
- CN202310637939.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-31
- Publication Date
- 2026-12-04
- Estimated Expiration
- 2043-05-31
AI Technical Summary
In existing technologies, the stack space size of kernel-mode threads is fixed, which causes some threads to be allocated more storage space than actually needed, resulting in a waste of memory space.
By monitoring the memory requirements of the target thread's execution program module, the main stack space and the spare stack space are dynamically switched during operation. The main stack space is initially allocated, while the spare stack space is pre-configured based on the maximum running memory, ensuring that memory waste is avoided when the thread switches between different program modules.
It effectively avoids wasting memory space, reduces the risk of stack overflow, improves the efficiency and stability of thread execution, and realizes time-sharing parallelism of multiple programs.
Smart Images

Figure CN116662002B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a thread stack processing method, apparatus, device, medium, and program product. Background Technology
[0002] Stack space is the storage space allocated by the operating system for running threads.
[0003] In related technologies, the stack space size of both user-mode threads and kernel-mode threads is determined before program startup. Taking kernel-mode threads as an example, once the operating system is released, the stack space size of kernel-mode threads is fixed. To modify the stack space size of kernel-mode threads, the operating system needs to be customized to make the stack space of kernel-mode threads larger.
[0004] However, the kernel stack space size is allocated to all threads, while some threads that only require a small portion of the stack space to complete their work are also allocated a large thread stack space, resulting in a waste of memory space. Summary of the Invention
[0005] Therefore, it is necessary to provide a thread stack processing method, device, equipment, medium, and program product to address the above-mentioned technical problems, so as to ensure the normal operation of threads while avoiding the waste of stack space resources.
[0006] Firstly, this application provides a thread stack processing method, which includes:
[0007] During the execution of the target thread, monitor the memory required for the target thread to execute the next program module after the current program module;
[0008] Based on the running memory, the target thread is controlled to switch between the main stack space and the spare stack space to execute the next program module of the current program module; the spare stack space is a stack space pre-configured according to the maximum running memory required by the target thread to execute each program module.
[0009] In the technical solution of this application embodiment, during the execution of the target thread, the memory required for the target thread to execute the next program module of the current program module is monitored. Then, based on the memory required, the target thread is controlled to switch between the main stack space and the backup stack space to execute the next program module of the current program module. The backup stack space is a stack space pre-configured based on the maximum memory required for the target thread to execute the program. Since this method controls the target thread to switch between the main stack space and the backup stack space based on the memory required for the target thread to execute the next program module of the current program module, it is equivalent to dividing the program executed by the target thread into multiple program modules during the execution of the target thread and pre-setting a main stack space and a backup stack space for the target thread. Based on this, if the stack space of the target thread running the current program cannot meet the actual memory requirements of the current program, the target thread's stack space is switched from the main stack space to the backup stack space, or switched from the backup stack space back to the main stack space, thereby ensuring the normal operation of each program module of the target thread. Furthermore, the spare stack space is a stack space pre-configured based on the maximum running memory required by the target thread to execute the program. This targeted pre-configured spare stack space for the target thread avoids wasting memory space.
[0010] In one embodiment, based on runtime memory, controlling the target thread to switch between the primary stack space and the backup stack space includes:
[0011] If the running memory exceeds the remaining main stack space of the target thread, then control the target thread to switch from the main stack space to the spare stack space;
[0012] If the running memory does not exceed the remaining main stack space of the target thread, and the target thread is executing in the spare stack space, then control the target thread to switch from the spare stack space to the main stack space.
[0013] In the technical solution of this application embodiment, based on the comparison result between the running memory and the main stack space of the target thread, and the stack space of the currently executing program module, the system flexibly switches between the main stack space and the spare stack space. This method of determining the stack space of the next program module to be executed by the target thread reduces the risk of stack overflow. Simultaneously, this application embodiment expands the target thread's stack space to both the main stack space and the spare stack space. After expansion, different stack spaces support the target thread executing different programs. By switching the target thread's stack space, multi-program time-sharing parallelism of the target thread is achieved, improving the efficiency of the target thread's program execution.
[0014] In one embodiment, the thread stack processing method further includes:
[0015] Obtain the first register set information; the first register set information is the minimum register set information obtained based on the execution of the current program module;
[0016] Obtain the address of the first instruction to be executed from the main stack space; the address of the first instruction to be executed is obtained based on the current program module;
[0017] Store the first register set information and the address of the first instruction to be executed into a memory data structure associated with the main stack space.
[0018] In the technical solution of this application embodiment, when the running memory exceeds the remaining main stack space of the target thread, the register set information and the address of the instruction to be executed corresponding to the current program module are stored in the memory data structure of the main stack space, and the stack space of the next program module is switched to the backup stack space. This is equivalent to backing up the associated information of the next program module and preparing in advance for the main stack space to execute the current program module again.
[0019] In one embodiment, controlling the target thread to switch from the primary stack space to the backup stack space includes:
[0020] Obtain the address of the second instruction to be executed and the information of the second register set from the memory data structure associated with the spare stack space; the second instruction to be executed is obtained from the next program module of the current program module; the information of the second register set is the minimum register set information required to run the next program module of the current program module.
[0021] Write the information of the second register set into the corresponding registers and write the address of the second instruction to be executed into the spare stack space to complete the switch from the main stack space to the spare stack space.
[0022] In the technical solution of this application embodiment, the first instruction to be executed in the current program module is replaced by the second instruction to be executed in the pre-stored next program module, and the first register set information of the current program module is replaced by the second register set information in the pre-stored next program module, thereby completing the switch from the main stack space to the spare stack space, ensuring that the target thread will not experience stack overflow when executing each program module, and improving the stability of the thread stack operation.
[0023] In one embodiment, executing the next program module of the current program module includes:
[0024] Based on the address of the second instruction to be executed in the spare stack space, execute the next program module of the current program module.
[0025] In the technical solution of this application embodiment, according to the instruction address pre-stored in the memory data structure of the spare stack space, the target thread is controlled to execute the next program module of the current program module through the second instruction address to be run provided by the spare stack space, so that the target thread can stably run the next program module.
[0026] In one embodiment, before controlling the target thread to switch from the spare stack space to the primary stack space, the method further includes:
[0027] Obtain the information of the second register set and the address of the second instruction to be executed;
[0028] The second register set information and the address of the second instruction to be executed are stored in a memory data structure associated with the spare stack space.
[0029] In the technical solution of this application embodiment, when the running memory does not exceed the remaining main stack space of the target thread, the second register set information and the address of the second instruction to be executed are stored in the memory data structure associated with the spare stack space. This is equivalent to backing up the information associated with the next program module of the current program module, and preparing in advance for the spare stack space to execute the next program module of the current program module again.
[0030] In one embodiment, controlling the target thread to switch from the spare stack space to the main stack space includes:
[0031] Obtain the address of the first instruction to be executed and the information of the first register set from the memory data structure associated with the main stack space;
[0032] Write the information of the first register set into the corresponding registers and write the address of the first instruction to be executed into the main stack space to complete the switch from the spare stack space to the main stack space.
[0033] In the technical solution of this application embodiment, the first register set information and the first instruction to be executed corresponding to the current program module are extracted from the memory data structure associated with the main stack space, and the first register set information is written into the corresponding register so that the processor can read the contents of the current program module from the register and write the address of the first instruction to be executed into the main stack space so as to control the target thread to execute the current program module in the main stack space.
[0034] In one embodiment, the thread processing method further includes:
[0035] Execute the current program module based on the address of the first instruction to be executed in the main stack space.
[0036] In the technical solution of this application embodiment, the target thread is controlled to execute the current program through the first instruction address to be run provided by the main stack space according to the instruction address pre-stored in the memory data structure of the main stack space, so that the target thread can quickly switch to the main stack space and run the current program module stably.
[0037] Secondly, this application also provides a thread stack processing apparatus, the apparatus comprising:
[0038] The monitoring module is used to monitor the memory required for the target thread to execute the next program module after the current program module during the execution of the target thread.
[0039] The adjustment module is used to control the target thread to switch between the main stack space and the spare stack space based on the running memory, so as to execute the next program module of the current program module; the spare stack space is a stack space pre-configured according to the maximum running memory required by the target thread to execute each program module.
[0040] Thirdly, this application also provides a computer device. The computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the steps of the method in any of the embodiments of the first aspect described above.
[0041] Fourthly, this application also provides a computer-readable storage medium. This computer-readable storage medium stores a computer program thereon, which, when executed by a processor, implements the steps of the method in any of the embodiments of the first aspect described above.
[0042] Fifthly, this application also provides a computer program product. This computer program product includes a computer program that, when executed by a processor, implements the steps of the method in any of the embodiments of the first aspect described above. Attached Figure Description
[0043] Figure 1 This is an internal structural diagram of a computer device in one embodiment;
[0044] Figure 2 This is a flowchart illustrating a thread stack processing method in one embodiment;
[0045] Figure 3 This is a flowchart illustrating the stack space switching step in one embodiment;
[0046] Figure 4 This is a flowchart illustrating the space switching step in another embodiment;
[0047] Figure 5 This is a flowchart illustrating the space switching step in another embodiment;
[0048] Figure 6 This is a flowchart illustrating the space switching step in another embodiment;
[0049] Figure 7 This is a flowchart illustrating the program execution steps in another embodiment;
[0050] Figure 8 This is a flowchart illustrating the space switching step in another embodiment;
[0051] Figure 9 This is a flowchart illustrating the space switching step in another embodiment;
[0052] Figure 10 This is a flowchart illustrating the space switching step in another embodiment;
[0053] Figure 11 This is a structural block diagram of a thread stack processing device in one embodiment. Detailed Implementation
[0054] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0055] The thread stack processing method provided in this application can be applied to a computer device. The computer device can be a server, and its internal structure diagram can be as follows: Figure 1 As shown. The computer device includes a processor, memory, input / output interface (I / O), and communication interface. The processor, memory, and I / O interface are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interface. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores thread stack processing data. The I / O interface allows the processor to exchange information with external devices. The communication interface allows communication with external terminals via a network connection. When the computer program is executed by the processor, it implements a thread stack processing method. Those skilled in the art will understand that... Figure 1The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0056] Stack space is the storage space allocated by the operating system for running threads. The size of the stack space is determined before the thread executes its program. In other words, once the operating system is released, the size of the thread's stack space is fixed. In related technologies, modifying the thread's stack space size requires customizing the operating system to increase the stack space.
[0057] However, the stack space size is allocated to all threads, while some threads that only require a small amount of stack space to complete their work are still allocated a large amount of thread stack space. Therefore, this technology suffers from memory wastage.
[0058] Based on this, this application provides a thread stack processing method. With a preset spare stack space, the method controls the target thread to switch between the main stack space and the spare stack space based on the running memory required by the target thread to execute the current program, thereby avoiding memory waste.
[0059] In one embodiment, such as Figure 2 As shown, a thread stack handling method is provided, including the following steps:
[0060] S201, During the execution of the target thread, monitor the memory required for the target thread to execute the next program module of the current program module.
[0061] In this context, the target thread is the smallest unit of computation that the operating system can schedule. It is contained within a process and is the actual unit of operation within that process. A target thread refers to a single, sequential flow of control within a process. The target thread executes the program modules in a single order to complete the task corresponding to the target thread.
[0062] During the execution of a task by the target thread, the target thread executes different program modules in the program in sequence. In other words, the current program executed by the target thread is dynamically changing, and the memory required for the target thread to execute the current program changes as the current program changes.
[0063] It should be noted that the deeper the function call hierarchy within a program module, the greater the amount of memory required for the target thread to execute the program module.
[0064] If the memory required by the target thread to execute the program module exceeds the memory space preset by the operating system for the target thread, the target thread will crash while running the program module, meaning the target thread cannot run normally.
[0065] To prevent the target thread from crashing, during the execution of the target thread, the memory required for the next program module to be executed by the target thread is monitored, and the memory space corresponding to the next program module to be executed by the target thread is switched, expanded, or adjusted.
[0066] S202, based on runtime memory, controls the target thread to switch between the main stack space and the spare stack space to execute the next program module of the current program module; the spare stack space is a stack space pre-configured according to the maximum runtime memory required by the target thread to execute each program module.
[0067] Before the target thread executes its program, a spare stack space is pre-set based on the maximum runtime memory required by the target thread. In this embodiment, both the primary stack space and the spare stack space are storage spaces that support the execution of the target thread, and the memory space of the spare stack space is greater than or equal to the memory space of the primary stack space.
[0068] Given the required runtime memory for the next program module to be executed by the target thread, the stack space corresponding to the next program module to be executed by the target thread is determined by comparing the runtime memory with the memory of the main stack space.
[0069] For example, if the target thread is executing the current program module in the main stack space, then based on the comparison result between the running memory and the memory of the main stack space, the target thread is controlled to continue executing the next program module of the current program module in the main stack space, or the target thread is controlled to switch from the main stack space to the standby stack space and execute the next program module of the current program module in the standby stack space.
[0070] For example, if the target thread is executing the current program module in the spare stack space, then based on the comparison result between the running memory and the memory of the main stack space, the target thread is controlled to continue executing the next program module of the current program module in the spare stack space, or the target thread is controlled to switch from the spare stack space to the main stack space and execute the next program module of the current program module in the main stack space.
[0071] In this embodiment, during the execution of the target thread, the required runtime memory for executing the next program module of the current program module is monitored. Then, based on the runtime memory, the target thread is controlled to switch between the primary stack space and the backup stack space to execute the next program module. The backup stack space is a stack space pre-configured based on the maximum runtime memory required by the target thread to execute the program. Since this method controls the target thread to switch between the primary and backup stack spaces based on the obtained runtime memory required for the next program module of the current program module, it is equivalent to dividing the program executed by the target thread into multiple program modules during its execution and pre-setting primary and backup stack spaces for the target thread. Therefore, if the stack space of the target thread cannot meet the actual memory requirements of the current program, the target thread's stack space is switched from the primary stack space to the backup stack space, or vice versa, thereby ensuring the normal operation of each program module of the target thread. Furthermore, the spare stack space is a stack space pre-configured based on the maximum running memory required by the target thread to execute the program. This targeted pre-configured spare stack space for the target thread avoids wasting memory space.
[0072] During thread stack processing, based on a comparison between the running memory and the target thread's execution of the current program, the stack space of the target thread is switched to prevent stack overflow during program execution. Based on this, the following example illustrates the basis and steps for stack space switching.
[0073] In one embodiment, such as Figure 3 As shown, based on runtime memory, the control of the target thread switching between the main stack space and the spare stack space includes:
[0074] S301: If the running memory exceeds the target thread's main stack space, control the target thread to switch from the main stack space to the spare stack space.
[0075] If the memory required for the next program module to be executed by the target thread exceeds the main stack space, and the target thread is executing the current program module in the main stack space, control the target thread to switch from the main stack space to the standby stack space, and execute the next program module in the standby stack space.
[0076] It should be noted that in the embodiments of this application, if the memory required for the target thread to execute the next program module of the current program module exceeds the main stack space, and the target thread is executing the current program module in the spare stack space, no stack space switching is performed, that is, the target thread continues to execute the next program module of the current program module in the spare stack space.
[0077] S302: If the running memory does not exceed the remaining main stack space of the target thread, and the target thread is executing in the spare stack space, then control the target thread to switch from the spare stack space to the main stack space.
[0078] If the memory required for the target thread to execute the next program module of the current program module does not exceed the main stack space, and the target thread is executing the current program module in the spare stack space, control the target thread to switch from the spare stack space to the main stack space, and execute the next program module of the current program module in the main stack space.
[0079] It should be noted that in the embodiments of this application, if the memory required for the target thread to execute the next program module of the current program module does not exceed the main stack space, and the target thread is executing the current program module in the main stack space, no stack space switching is performed, that is, the target thread continues to execute the next program module of the current program module in the main stack space.
[0080] In this embodiment, based on the comparison between the running memory and the target thread's main stack space, and the stack space of the currently executing program module, the system flexibly switches between the main stack space and the spare stack space. This method of determining the stack space for the next program module to be executed by the target thread reduces the risk of stack overflow. Simultaneously, this embodiment expands the target thread's stack space to include both the main and spare stack spaces. These expanded stack spaces support the target thread executing different programs. By switching the target thread's stack space, time-sharing parallelism across multiple programs is achieved, improving the efficiency of the target thread's program execution.
[0081] The above embodiments explain the basis for switching between the primary stack space and the backup stack space, including the switching basis for switching from the primary stack space to the backup stack space and the switching basis for switching from the backup stack space to the primary stack space. Based on this, the following embodiment describes the switching steps for switching from the primary stack space to the backup stack space.
[0082] In one embodiment, such as Figure 4 As shown, controlling the target thread to switch from the primary stack space to the backup stack space includes:
[0083] S401, obtain the first register set information; the first register set information is the minimum register set information obtained based on the current program module execution.
[0084] It should be noted that during the execution of a program in the target thread, the program content included in different program modules is different. The processor executes the program module corresponding to the program content by calling the program content in registers. Typically, the processor calls the program content in multiple registers to execute a program module. In addition, different processors and different program modules have different minimum register sets.
[0085] The target thread executes multiple registers involved in the current program module. These registers called by the processor are grouped into one set, and the set of registers storing the program content of the current module is defined as the minimum register set. By summarizing the values of the minimum register set, the register locations, and the contents of each register within the minimum register set, the information for the first register set is obtained.
[0086] In addition, the target thread executing the program corresponds to a set of registers. This set of registers stores the program content of the current program, which can be called by the Central Processing Unit (CPU) to execute the current program. Different CPUs use different sets of registers. The correspondence between each CPU and its register set, stack alignment, and excess parameters is shown in Table 1. Table 1 shows the correspondence between CPUs and register sets under different application binary interface standards.
[0087] Table 1
[0088]
[0089]
[0090] As shown in Table 1, when the Application Binary Interface (ABI) standard is System V, the return value registers for i386 include EAX and EDX; excess parameters are pushed onto the stack from right to left; temporary registers include EAX, ECX, and EDX; reserved registers include EBX, ESI, EDI, EBP, and ESP; and the call chain register includes EBP.
[0091] With the ABI standard set to System V, the x86_64 return value registers include RAX and RDX; the parameter registers include RDI, RSI, RDX, RCX, R8, and R9; the stack alignment is 16 bytes; excess parameters are pushed onto the stack from right to left; the temporary registers include RAX, RDI, RSI, RDX, RCX, and R8 to R11; the reserved registers include RBX, RSP, RBP, and R12 to R15; and the call chain register includes RBP.
[0092] When the ABI standard is Microsoft, the return value register for X64 includes RAX; the parameter registers include RCX, RDX, and R8 to R9; the stack alignment is 16B; excess parameters are pushed onto the stack from right to left; the temporary registers include RAX, RCX, RDX, and R8 to R11; the reserved registers include RBX, RDI, RSI, RSP, RBP, and R12 to R15; and the call chain register includes RBP.
[0093] The return value registers for Arm 32 include R0 and R1; the parameter registers include R0 to R3; the stack alignment is 8 bytes; excess parameters are pushed onto the stack; the temporary registers include R0 to R3 and R12; and the reserved registers include R4 to R11 and R11 to R14.
[0094] The return value registers for Arm V8 aarch64 include X0 to X7, X8, and X30; the parameter registers include X0 to X7; excess parameters are pushed onto the stack; the temporary registers include X9 to X15 and X16 to X1; the reserved registers include X19 to X28, X29, and X30; and the call chain register includes X31.
[0095] Taking the CPU as System V x86_64 as an example, the minimum register set includes RSP, RBP, RBX, R12~R15, RDI, RSI, RDX, RCX, R8 and R9. The first register set information includes the value of the minimum register set and the stack contents at the RSP.1 register location.
[0096] S402, obtain the address of the first instruction to be executed from the main stack space; the address of the first instruction to be executed is obtained based on the current program module.
[0097] The address of the first instruction to be executed is the basis for the processor to execute the current program module. Since the current program module runs in the main stack space, the address of the first instruction to be executed in the current program module is also in the main stack space.
[0098] In this embodiment of the application, if the running memory exceeds the remaining main stack space of the target thread during the execution of the target thread, the address of the first instruction to be executed is automatically extracted from the main stack space.
[0099] S403, store the first register set information and the address of the first instruction to be executed into a memory data structure associated with the main stack space.
[0100] It should be noted that different stack spaces are associated with different memory data structures, which are used to store the program modules of the corresponding stack space. That is, the primary stack space corresponds to one memory data structure, and the spare stack space corresponds to another memory data structure.
[0101] Memory data structures encompass the program content of the current program module, primarily including three aspects: the logical structure of the current program module, the physical storage structure of the current program module, and the basic operations of the current program module. In memory data structures, the logical structure refers to the logical description of the continuity and constraints between the current program data elements; the physical storage structure refers to the storage method in the computer's memory, corresponding to the logical structure; and the basic operations refer to the basic operations performed on the current program data elements in the data structure, such as insertion, searching, or traversal.
[0102] The first register set information obtained based on the minimum register set information and the address of the first instruction to be executed in the current program module are stored in the memory data structure associated with the main stack space.
[0103] In this embodiment of the application, when the running memory exceeds the remaining main stack space of the target thread, the register set information and the address of the instruction to be executed corresponding to the current program module are stored in the memory data structure of the main stack space, and the stack space of the next program module is switched to the backup stack space. This is equivalent to backing up the associated information of the next program module and preparing in advance for the main stack space to execute the current program module again.
[0104] During the execution of the target thread, program content is typically stored in registers. The processor interacts with these registers to execute the corresponding program modules, thereby improving program execution speed. Based on this, the following example illustrates the steps for switching to the spare stack space.
[0105] In one embodiment, such as Figure 5 As shown, controlling the target thread to switch from the primary stack space to the backup stack space includes:
[0106] S501: Obtain the pre-stored address of the second instruction to be executed and the information of the second register set from the memory data structure associated with the spare stack space; the second instruction to be executed is obtained according to the next program module of the current program module; the information of the second register set is the minimum register set information required to run the next program module of the current program module.
[0107] Since the next program module of the current program module needs to run in the spare stack space, the embodiments of this application pre-store the address of the second instruction to be run and the information of the second register set in the memory data structure associated with the spare stack space.
[0108] The address of the second instruction to be executed is the basis for the processor to execute the next program module after the current program module. The second register set information includes the minimum register set value, register location, and contents of each register in the minimum register set that stores the program content of the next program module after the current program module.
[0109] S502 writes the second register set information into the corresponding registers and writes the address of the second instruction to be executed into the spare stack space to complete the switch from the main stack space to the spare stack space.
[0110] If the stack space is not switched to the spare stack space, the register content corresponding to the register location is the program content of the current program module. In order to complete the stack space switch, the contents of the register need to be updated so that the CPU can call the register content and execute the next program module.
[0111] In the process of updating the register contents, the second register set information is written into the corresponding register, and the address of the second instruction to be executed is written into the spare stack space to complete the switch from the main stack space to the spare stack space.
[0112] In this embodiment, the first instruction to be executed in the current program module is replaced by the second instruction to be executed in the pre-stored next program module, and the first register set information of the current program module is replaced by the second register set information in the pre-stored next program module. This completes the switching from the main stack space to the backup stack space, ensuring that the target thread will not experience stack overflow when executing each program module, and improving the stability of the thread stack operation.
[0113] After switching from the primary stack space to the backup stack space, the next program module can be executed. Based on this, the steps for executing the next program module are explained below through an example.
[0114] In one embodiment, executing the next program module of the current program module includes:
[0115] Based on the address of the second instruction to be executed in the spare stack space, execute the next program module of the current program module.
[0116] Optionally, in response to assembly instructions, the address of the second instruction to be executed corresponding to the next program module of the current program module is obtained from the spare stack space, and the target thread is controlled to execute the next program module of the current program module in the spare stack space according to the address of the second instruction to be executed.
[0117] In this embodiment, based on the instruction address pre-stored in the memory data structure of the spare stack space, the target thread is controlled to execute the next program module of the current program module through the second instruction address to be run provided by the spare stack space, so that the target thread can stably run the next program module.
[0118] The foregoing embodiments have described the switching steps from the primary stack space to the backup stack space. Based on this, the following embodiment will describe the switching steps from the backup stack space to the primary stack space.
[0119] In one embodiment, such as Figure 6 As shown, before the target thread switches from the spare stack space to the main stack space, the thread stack handling method also includes:
[0120] S601, obtain the second register set information and the address of the second instruction to be executed.
[0121] Obtain the address of the second execution instruction from the spare stack space, and obtain the second register set information based on the register set that stores the program content of the next program module of the current program module.
[0122] S602 stores the second register set information and the address of the second instruction to be executed into a memory data structure associated with the spare stack space.
[0123] It should be noted that the program module executed on the spare stack at this time is the next program module after the current program module. If the spare stack space needs to call the next program module of the current program module again, it needs to extract the second register set information and the address of the second instruction to be executed corresponding to the next program module from the spare stack space, and store the second register set information and the address of the second instruction to be executed into the memory data structure associated with the spare stack space.
[0124] In this embodiment of the application, when the running memory does not exceed the remaining main stack space of the target thread, the second register set information and the address of the second instruction to be executed are stored in the memory data structure associated with the spare stack space. This is equivalent to backing up the information associated with the next program module of the current program module, and preparing in advance for the spare stack space to execute the next program module of the current program module again.
[0125] After the instruction address and register set information have been updated and backed up, the stack space switching operation can be performed. Based on this, the following example, corresponding to the examples in S401 to S403, describes the steps for switching back from the spare stack space to the main stack.
[0126] In one embodiment, such as Figure 7 As shown, controlling the target thread to switch from the spare stack space to the main stack space includes:
[0127] S701 obtains the address of the first instruction to be executed and the information of the first register set from the memory data structure associated with the main stack space.
[0128] As can be seen from step S403 of the aforementioned embodiment, before switching from the primary stack space to the backup stack space, the first register set information and the address of the first instruction to be executed are stored in the memory data structure associated with the primary stack space. The first register set information is the minimum register set information corresponding to the current program module, and the address of the first instruction to be executed is the basis for the processor to execute the current program module.
[0129] Correspondingly, in this embodiment of the application, to control the target thread to switch from the spare stack space to the main stack space to execute the current program module, it is necessary to extract the address of the first instruction to be executed and the information of the first register set from the memory data structure associated with the main stack space.
[0130] S702, write the first register set information into the corresponding registers and write the address of the first instruction to be executed into the main stack space to complete the switch from the spare stack space to the main stack space.
[0131] Having extracted the address of the first instruction to be executed and the information of the first register set, the information of the first register set is written into the corresponding register so that the processor can read the contents of the current program module from the register. The address of the first instruction to be executed is written into the main stack space so that the processor can execute the current program module.
[0132] In this embodiment, the first register set information and the first instruction to be executed corresponding to the current program module are extracted from the memory data structure associated with the main stack space, and the first register set information is written into the corresponding register so that the processor can read the contents of the current program module from the register and write the address of the first instruction to be executed into the main stack space so as to control the target thread to execute the current program module in the main stack space.
[0133] After switching to the spare stack space, the current program module can continue running. Based on this, the steps for executing the current program module are explained below through an example.
[0134] In one embodiment, executing the next program module of the current program module includes:
[0135] Execute the current program module based on the address of the first instruction to be executed in the main stack space.
[0136] Optionally, in response to assembly instructions, the address of the first instruction to be executed corresponding to the current program module is obtained from the main stack space, and the target thread is controlled to execute the current program module in the main stack space based on the address of the first instruction to be executed.
[0137] In this embodiment, the target thread is controlled to execute the current program through the first instruction address to be executed provided by the main stack space, based on the instruction address pre-stored in the memory data structure of the main stack space. This facilitates the target thread to quickly switch to the main stack space and to run the current program module stably.
[0138] In one embodiment, using a System V x86_64 CPU, the steps for switching from the main stack space to a spare stack space are described when the memory required for the target thread to execute the current program exceeds the target thread's main stack space. Figure 8 As shown, switching from the primary stack space to the standby stack space includes the following steps:
[0139] S801, Stack Space Switching Function. This function is called to switch the primary stack space to the backup stack space.
[0140] S802. Save the stack contents. The stack contents are the address of the first instruction to be executed, located in the RSP.1 register of the main stack space. Save the address of the first instruction to be executed to the memory data structure associated with the main stack space.
[0141] S803. Save the first register set information. Based on the minimum register set information (first register set information) obtained from the current program module execution, save the first register set information to the memory data structure associated with the main stack space. The register set includes: RSP, RBP, RBX, R12~R15, RDI, RSI, RDX, RCX, R8, and R9.
[0142] S804. Restore stack contents. Fill the address of the second instruction to be executed, which is pre-stored in the memory data structure associated with the spare stack space, into the location of the RSP.1 register in the spare stack space.
[0143] S805. Restore register information set. Write the pre-stored second register set information from the memory data structure associated with the spare stack space into the corresponding registers.
[0144] S806, Call the return instruction. The next program module is executed according to the address of the second instruction to be executed in the spare stack space.
[0145] Furthermore, when the memory required for the target thread to execute the next program module does not exceed the remaining main stack space of the target thread, and the target thread is executing in the spare stack space, the steps for switching from the spare stack space to the main stack space are explained, such as... Figure 9 As shown, switching from the primary stack space to the standby stack space includes the following steps:
[0146] S807, Stack Space Switching Function. This function is called to switch the backup stack space to the primary stack space.
[0147] S808, Save the stack contents. The stack contents are the address of the second instruction to be executed, located in the RSP.1 register of the spare stack space. Save the address of the second instruction to be executed to the memory data structure associated with the spare stack space.
[0148] S809. Save the second register set information. Based on the minimum register set information (second register set information) obtained from the execution of the next program module of the current program module, save the second register set information to the memory data structure associated with the spare stack space. The register set includes: RSP, RBP, RBX, R12~R15, RDI, RSI, RDX, RCX, R8, and R9.
[0149] S810. Restore stack contents. Fill the address of the first instruction to be executed, which is stored in the memory data structure associated with the main stack space, into the location of the RSP.1 register in the main stack space.
[0150] S811. Restore register information set. Write the first register set information stored in the memory data structure associated with the main stack space into the corresponding register.
[0151] S812, Call Return Instruction. The current program module is executed according to the address of the first instruction to be executed in the main stack space.
[0152] In this embodiment, when the memory required for the target thread to execute the next program module of the current program module exceeds the main stack space of the target thread, the target thread is switched from the main stack space to the standby stack space. Then, when the memory required for the target thread to execute the next program module of the current program module does not exceed the remaining main stack space of the target thread, and the target thread is executing in the standby stack space, the steps of switching from the standby stack space to the main stack space are described, and the switching basis is reliable.
[0153] Since the process containing the target thread also includes other threads, if these other threads use dangling pointers or access arrays out of bounds, the target thread may be "stacked" on the stack, leading to a deviation in the execution result obtained by the target thread. Therefore, the following example illustrates how to handle the thread stack when the execution result of the target thread deviates.
[0154] In one embodiment, such as Figure 10 As shown, the thread stack handling methods also include:
[0155] S1001, obtain the execution result obtained by the target thread after all programs have been executed.
[0156] After the target thread has finished executing all the programs, the execution result of the entire target thread is obtained.
[0157] S1002, if the execution result is incorrect, control the target thread to switch from the main stack space to the standby stack space to re-execute each program.
[0158] Based on the execution results of the preset program, the execution results obtained by the target thread after executing all programs are verified. If the execution results are different from the execution results of the preset program, that is, the execution results are incorrect, it indicates that the target thread has been pushed onto the stack by other programs. In order to ensure the safety of the stack space corresponding to the target thread, the target thread is controlled to switch from the main stack space to the backup stack space to re-execute each program to obtain accurate execution results.
[0159] In this embodiment, the safety of the primary stack space is determined based on the execution results obtained by the target thread after executing all programs. If the execution results are incorrect, it indicates that the primary stack space is unsafe. By switching from the primary stack space to the backup stack space, the stack space of the target thread is prevented from being accidentally accessed by other threads, thereby improving the accuracy of the execution results obtained by the target thread after executing all programs.
[0160] In one embodiment, a thread stack handling method is provided, including the following steps:
[0161] (1) During the execution of the target thread, monitor the memory required for the target thread to execute the next program module of the current program module.
[0162] (2) If the running memory exceeds the remaining main stack space of the target thread, obtain the first register set information. The first register set information is the minimum register set information obtained based on the current program module execution.
[0163] (3) Obtain the address of the first instruction to be executed from the main stack space; the address of the first instruction to be executed is obtained based on the current program module.
[0164] (4) Store the first register set information and the address of the first instruction to be executed into a memory data structure associated with the main stack space.
[0165] (5) Obtain the pre-stored address of the second instruction to be executed and the information of the second register set from the memory data structure associated with the spare stack space. The second instruction to be executed is obtained from the next program module of the current program module; the information of the second register set is the minimum register set required to run the next program module of the current program module.
[0166] (6) Write the second register set information into the corresponding register and write the address of the second instruction to be executed into the spare stack space to complete the switch from the main stack space to the spare stack space.
[0167] (7) Execute the next program module of the current program module according to the address of the second instruction to be executed in the spare stack space.
[0168] (8) During the execution of the target thread, continue to monitor the memory required for the target thread to execute the next program module of the current program module.
[0169] (9) If the running memory exceeds the remaining main stack space of the target thread, store the second register set information and the address of the second instruction to be executed into the memory data structure associated with the spare stack space.
[0170] (10) Obtain the address of the first instruction to be executed and the information of the first register set from the memory data structure associated with the main stack space.
[0171] (11) Write the information of the first register set into the corresponding register and write the address of the first instruction to be executed into the main stack space to complete the switch from the spare stack space to the main stack space.
[0172] (12) Execute the current program module according to the address of the first instruction to be executed in the main stack space.
[0173] In this embodiment, during the execution of the target thread, the required runtime memory for executing the next program module of the current program module is monitored. Then, based on the runtime memory, the target thread is controlled to switch between the primary stack space and the backup stack space to execute the next program module. The backup stack space is a stack space pre-configured based on the maximum runtime memory required by the target thread to execute the program. Since this method controls the target thread to switch between the primary and backup stack spaces based on the obtained runtime memory required for the next program module of the current program module, it is equivalent to dividing the program executed by the target thread into multiple program modules during its execution and pre-setting primary and backup stack spaces for the target thread. Therefore, if the stack space of the target thread cannot meet the actual memory requirements of the current program, the target thread's stack space is switched from the primary stack space to the backup stack space, or vice versa, thereby ensuring the normal operation of each program module of the target thread. Furthermore, the spare stack space is a stack space pre-configured based on the maximum running memory required by the target thread to execute the program. This targeted pre-configured spare stack space for the target thread avoids wasting memory space.
[0174] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0175] Based on the same inventive concept, this application also provides a thread stack processing apparatus for implementing the thread stack processing method described above. The solution provided by this apparatus is similar to the implementation described in the above method; therefore, the specific limitations in one or more thread stack processing apparatus embodiments provided below can be found in the limitations of the thread stack processing method described above, and will not be repeated here.
[0176] In one embodiment, such as Figure 10 As shown, a thread stack processing device 1000 is provided, including: a monitoring module 1001 and an adjustment module 1002, wherein:
[0177] The monitoring module 1001 is used to monitor the memory required for the target thread to execute the next program module during the execution of the current program module during the execution of the target thread.
[0178] The adjustment module 1002 is used to control the target thread to switch between the main stack space and the spare stack space based on the running memory, so as to execute the next program module of the current program module; the spare stack space is a stack space pre-configured according to the maximum running memory required by the target thread to execute each program module.
[0179] In one embodiment, the adjustment module 1002 includes a first switching unit and a second switching unit, wherein:
[0180] The first switching unit is used to control the target thread to switch from the main stack space to the standby stack space if the running memory exceeds the remaining main stack space of the target thread;
[0181] The second switching unit is used to control the target thread to switch from the spare stack space to the main stack space if the running memory does not exceed the remaining main stack space of the target thread and the target thread is executing in the spare stack space.
[0182] In one embodiment, the thread stack processing device 1000 further includes a collection information acquisition module, an instruction address acquisition module, and an information address storage module, wherein:
[0183] The set information acquisition module is used to acquire the first register set information; the first register set information is the minimum register set information obtained based on the execution of the current program module;
[0184] The instruction address acquisition module is used to obtain the address of the first instruction to be executed from the main stack space; the address of the first instruction to be executed is obtained based on the current program module;
[0185] The first storage module is used to store the first register set information and the address of the first instruction to be executed into a memory data structure associated with the main stack space.
[0186] In one embodiment, the first switching unit further includes a first acquisition subunit and a first write subunit, wherein:
[0187] The first acquisition subunit is used to acquire the address of the second instruction to be executed and the second register set information from the memory data structure associated with the spare stack space; the second instruction to be executed is obtained according to the next program module of the current program module; the second register set information is the minimum register set information required to run the next program module of the current program module.
[0188] The first write subunit is used to write the second register set information into the corresponding registers and write the address of the second instruction to be executed into the spare stack space, so as to complete the switch from the main stack space to the spare stack space.
[0189] In one embodiment, the adjustment module 1002 further includes a first execution unit, which is used to execute the next program module of the current program module according to the address of the second instruction to be run in the spare stack space.
[0190] In one embodiment, the thread stack processing device 1000 further includes an information address acquisition module and a second storage module, wherein:
[0191] The information address acquisition module is used to acquire information about the second register set and the address of the second instruction to be executed.
[0192] The second storage module is used to store the second register set information and the address of the second instruction to be executed into a memory data structure associated with the spare stack space.
[0193] In one embodiment, the second switching unit includes a second acquisition subunit and a second write subunit, wherein:
[0194] The second acquisition subunit is used to acquire the address of the first instruction to be executed and the information of the first register set from the memory data structure associated with the main stack space;
[0195] The second write subunit is used to write the information of the first register set into the corresponding register and write the address of the first instruction to be executed into the main stack space, so as to complete the switch from the spare stack space to the main stack space.
[0196] In one embodiment, the thread stack processing device 1000 further includes an execution module, which is used to execute the next program module of the current program module according to the address of the first instruction to be executed in the main stack space.
[0197] Each module in the aforementioned thread stack processing device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.
[0198] In one embodiment, a computer device is provided, including a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the thread stack processing method provided in any of the above embodiments.
[0199] The computer device provided in the above embodiments has a similar implementation principle and technical effect to the above method embodiments, and will not be described again here.
[0200] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the thread stack processing method provided in any of the above embodiments.
[0201] The computer-readable storage medium provided in the above embodiments has a similar implementation principle and technical effect to the above method embodiments, and will not be described again here.
[0202] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the thread stack processing method provided in any of the above embodiments.
[0203] The computer program product provided in the above embodiments has a similar implementation principle and technical effect to the above method embodiments, and will not be described again here.
[0204] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited to these.
[0205] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0206] The above embodiments merely illustrate several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of this application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
