CN101847096A - Optimization method of stack variable-containing function - Google Patents

Optimization method of stack variable-containing function Download PDF

Info

Publication number
CN101847096A
CN101847096A CN 201010107353 CN201010107353A CN101847096A CN 101847096 A CN101847096 A CN 101847096A CN 201010107353 CN201010107353 CN 201010107353 CN 201010107353 A CN201010107353 A CN 201010107353A CN 101847096 A CN101847096 A CN 101847096A
Authority
CN
China
Prior art keywords
stack
function
instruction
variable
optimised
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN 201010107353
Other languages
Chinese (zh)
Other versions
CN101847096B (en
Inventor
李建军
武成岗
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Institute of Computing Technology of CAS
Original Assignee
Institute of Computing Technology of CAS
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Institute of Computing Technology of CAS filed Critical Institute of Computing Technology of CAS
Priority to CN 201010107353 priority Critical patent/CN101847096B/en
Publication of CN101847096A publication Critical patent/CN101847096A/en
Application granted granted Critical
Publication of CN101847096B publication Critical patent/CN101847096B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Executing Machine-Instructions (AREA)

Abstract

The invention provides an optimization method of a stack variable-containing function. The method comprises the steps of: applying a section of physical memory space used as a mapping stack of a function to be optimized, and then applying a section of virtual memory space used as a shadow stack of the function to be optimized; selecting a stack variable to be lifted to an idle register from the function to be optimized; inserting an instruction for switching a function stack of the function to be optimized from a system stack to the mapping stack, an instruction for lifting the content of the stack variable to the registers and an instruction for setting a virtual storage page in the mapping stack to be in a protected mode; replacing all the instructions containing operations on the lifted stack variable to be instructions for operating corresponding registers, and replacing all the operations on the non-lifted stack variables to be operations on corresponding virtual storage addresses on the shadow stack; and inserting an instruction for removing the protected mode on the virtual storage page in the mapping stack, an instruction for storing the content of the lifted stack variable back to an internal memory from the registers, and an instruction for switching a function stack frame from the mapping stack back to the system stack.

Description

The optimization method that comprises the stack variable function
Technical field
The present invention relates to function optimization method, particularly comprise the optimization method of stack variable function.
Background technology
CPU (central processing unit) plays central role in computing machine, along with the continuous progress of science and technology, the processor technology is also in continuous development.As the word length according to the CPU process information, CPU has just experienced the development course of four bit microprocessors, eight bit microprocessors, sixteen bit microprocessor, 32 bit microprocessors and 64 bit microprocessors.In general, new processor is compared with old processor and can be introduced some new characteristics, as increasing register quantity, add new instruction, increasing cache capacity etc.
For convenience of the user, the new compatible software of developing based on old processor of processor meeting, but existing application program (being the binary code that old processor compiling generates) some characteristic that may not utilize the more original processor of new processor to increase newly when moving on new processor is come the operation of accelerated procedure, make that the performance potential of new processor is not excavated fully, caused the wasting of resources to a certain extent.
With the register in the processor (register) is example, and register is used to preserve the operand and the intermediate result of participating in computing usually as the fastest storer of travelling speed in the processor.Because it is few and access speed is fast that register has quantity with respect to internal storage location, therefore according to the principle of locality of program, operating system can be placed on the value of often using in the register reducing the memory access expense, thus the performance of raising whole procedure.For the above reasons, in an application program, use the operation that more register will accelerated procedure.The older processor (as 32 bit microprocessors) of new processor (as 64 bit microprocessors) increases on the register number to some extent, therefore, when operation has application program on new processor, can utilize the register that increases in the new processor to come the accelerated procedure operation.
The register that increases in utilizing new processor comes accelerated procedure when operation, need originally be kept at data storage in the internal memory to some in idle register, and this process is called as register and promotes and optimize.But, must solve the problem of internal memory another name in order to guarantee to optimize back program run result's correctness.So-called internal memory another name is meant after storing variable data into idle register, has just had two versions of same variable in the system, one in register, another is in internal memory.In order to guarantee the correctness of program run, needs all be redirected all operations to this variable and be the operation to corresponding registers.But, because the support that has the visit of a large amount of pointer in the program and lack program information, therefore possibly can't find out in the program all to being raised the instruction that variable is operated.
With the stack variable is example, great majority all are that (promptly visiting plot is stack top or stack frame pointer by explicit stack formula visit to the visit of stack variable in the program, as 0x18 among Fig. 1 (%esp) and 0x24 (%esp)), but also there is the visit stack operation of some implicit expression stack variable to be carried out access (as the 0x4 (%edx among Fig. 1 by pointer, %ebx, 1) and 0x4 (%eax)).Therefore, in order to guarantee the correctness of program run, operating system must identify the accessing operation of the stack variable that all implied addressings in the program are raised.Whether a simple method of the prior art is identical with the memory address of the stack variable that is raised in each non-stack formula accessing operation (memory access address be not be plot with stack top or stack frame pointer) preceding interpolation instruction checking memory access address.But expense when this method can be brought very big operation.Having provided one among Fig. 1 utilizes the stack variable register to promote the example of optimizing the X86-32 code on the X86-64 platform.The left side is the X86-32 code before optimizing among Fig. 1, wherein contains two stack variable 0x18 (%esp) and 0x24 (%esp).If do not call relation between these two stack variables and other memory access addresses, when program run, can directly these two stack variables be risen to so in the idle register on the X86-64 platform.But,, so just must take some measures the correctness that guarantees to optimize the post code execution result if can not determine between the stack variable that is raised and other memory access addresses another name relation.As previously mentioned, prior art can pass through to add the mode of calling the inspection instruction guarantees optimization post code when moving correctness.Provided the example code of checking the internal memory another name among Fig. 1, expense in the time of can finding from code sample that this method can be brought very big operation.
Summary of the invention
The objective of the invention is to overcome prior art and adopt another name to check the big problem of operation expense when instruction overcomes the internal memory alias problem, thereby provide the operation expense little function optimization method.
To achieve these goals, the invention provides a kind of optimization method that comprises the stack variable function, comprising:
Step 1), one section physical memory space of application are as the reflection stack of the function that will optimize, and then apply for this section virtual memory spatial mappings being arrived and the identical physical memory of described reflection stack the shadow stack of one section virtual memory space as the function that will optimize;
Step 2), in the described function that will optimize, discern stack variable, therefrom select to be thus lifted to stack variable in the idle register;
Step 3), insert in the porch of want majorized function with the function stack of optimised function switch to the instruction of reflection stack from system's stack, void that the content of stack variable is risen in the instruction the register and the stack of will video deposits a page instruction that is made as guard mode;
Step 4), in the code of want majorized function, all are comprised instructions to lifting stack variable operation replace with instruction, all operations to the stack variable that do not promote are replaced with the operation of corresponding void on the described shadow stack being deposited the address the corresponding registers operation;
Step 5), insert the instruction that void in the stack of will videoing is deposited page instruction of de-preservation state, the content of the stack variable that is raised is returned the instruction of internal memory, function stack frame is switched back system's stack from the reflection stack from register successively in the exit of want majorized function.
In the technique scheme, also comprise the step 6) of handling unusually that optimised function is occurred in the process of implementation; This step comprises: be arranged in the function that optimised function calls when unusual instruction takes place, when unusual instruction perhaps taking place being arranged in optimised code and memory access address and occurring in protected void and deposit the address realm of the stack variable that is raised of page or leaf, abandon optimization to function; When unusual instruction taking place be arranged in optimised code and memory access address and occur in protected void and deposit the address realm of the stack variable that is not raised of page or leaf, generate another name and check that instruction also is inserted in the optimize codes.
In the technique scheme, described step 6) comprises:
Step 61), obtain taking place the instruction address and the memory access address thereof of unauthorized access from unusual on-the-spot context;
Step 62), judge described memory access address whether in described reflection stack protected void deposit in the page or leaf, if, carry out next step, otherwise, directly to handling unusually of being taken place;
Step 63), whether unusual instruction judge takes place in the optimize codes of optimised function, if, carry out next step, if do not exist, and unusual instruction takes place in the function that optimised function called, then abandon optimization to optimised function;
Step 64), protected void deposited comprise the address realm that promotes variable in the page or leaf and be divided into two parts, a part has comprised all lifting variablees, another part does not comprise any lifting variable;
Step 65), memory access address that unusual instruction judge to take place whether in comprising the address realm that promotes variable, if, then abandon optimization to optimised function, otherwise, generate corresponding another name inspection and instruct and also be inserted in the optimize codes.
In the technique scheme, before described step 1), also comprise the step that the function that will optimize is selected; In this step, select not exist call relation and function that the execution time is relatively long as optimised function.
In the technique scheme, in described step 2) in, adopt a kind of selection in the following manner to be thus lifted to the stack variable of register: select at random, according to what of the accessed number of times of static state select, what are selected by accessed number of times.
In the technique scheme, in described step 4), if there is the function call instruction that is used to call other function in the optimised function, then insert the top-of-stack pointer switching command that is used for top-of-stack pointer is switched to the shadow stack before the described function call instruction in the code of optimised function.
In the technique scheme, described step 3) comprises:
Step 31), the function stack that inserts behind the inlet of optimised function optimised function switches to the stack switching command of reflection stack from system's stack, and inserts the instruction that the parameter relevant with optimised function is copied to the reflection stack from system's stack;
Step 32), behind described stack switching command, insert the prefetched instruction that the content be used for the stack variable that will will promote is got corresponding registers;
Step 33), behind described prefetched instruction, insert and be used for putting guard-page protection code status code what the void of reflection stack deposited that page or leaf is made as guard mode.
In the technique scheme, described step 4) comprises:
Step 41), all instructions that comprise the operation of lifting stack variable in the code of optimised function are replaced with the instruction that corresponding registers is operated;
Step 42), all operations to the stack variable that do not promote in the code of optimised function are replaced with the operation of corresponding void on the shadow stack being deposited the address;
Step 43), the code after will optimizing is kept in the code cache, and the inlet of majorized function inserted instruction jump to optimize codes in the code cache;
Step 44), insert the top-of-stack pointer switching command before the function call in the optimised function instruction, so that top-of-stack pointer is switched in the shadow stack.
In the technique scheme, it is characterized in that described step 5) comprises:
Step 51), insert the void that is used for the reflection stack in the exit of function optimization code and deposit page instruction of de-preservation state;
Step 52), after the instruction of described releasing virtual page guard mode, insert the value be used for the stack variable that will be raised is returned internal memory from register synchronic command;
Step 53), behind described synchronic command, insert the stack switching command that is used for function stack frame is switched back from the reflection stack system's stack.
In the technique scheme, in described step 1), call mmap (NULL, image_stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, 0,0) the function application stack of videoing.
In the technique scheme, in described step 1), call mremap (old_address, old_size, new_size, flags) function is applied for the shadow stack.
The invention has the advantages that:
By being added another name, all non-explicit visit stack operations in the optimised function check that instruction comes the method for majorized function to compare with prior art, the present invention can find implicit expression visit stack instructions all in the optimize codes, expense when the reduction stack variable promotes the operation of optimizing efficiently.
Description of drawings
Fig. 1 is the synoptic diagram that comprises the function code of stack variable;
Fig. 2 is the process flow diagram that comprises the optimization method of stack variable function of the present invention;
Fig. 3 is that the function stack of optimised function concerns synoptic diagram;
Fig. 4 is the comparison diagram of function code before and after optimizing;
Fig. 5 is the process flow diagram of method that function code is optimized;
Fig. 6 is the process flow diagram of abnormality eliminating method.
Embodiment
The present invention will be described below in conjunction with the drawings and specific embodiments.
Before the present invention is illustrated, at first notion related among the present invention is done unified the introduction.
Guard-page protection mechanism: in the operating system of using the page memory management, can carry out read and write access control to memory pages, have readable, can write, can carry out three attributes.Guard-page protection mechanism is exactly to utilize the correctness that different page access attributes come protected data security and program run result is set.
After related notion is illustrated, below how the present invention is realized that the existing optimization of application program on new processor elaborates.
Because application program is elementary cell with the function, therefore hereinafter will be with the optimization that example illustrates application program that is optimized for of function.
In an application program; generally comprise a plurality of functions; though can all do optimization to all functions in this application program in theory according to method of the present invention; but because optimization method of the present invention is to utilize guard-page protection mechanism to come to exist with the lifting variable in the recognizer accessing operation of another name relation; if all functions all are optimized then can cause when the program run a large amount of unusual; program feature is produced a very large impact; therefore; as a kind of preferred implementation, should be selected optimised function.Consider that different functions are roughly the same in the time of doing the required operation of finishing when optimizing, will spend, therefore the efficient that is optimized of the function that the execution time is grown is higher relatively.In addition, if two functions that have call relation are optimized simultaneously, the value that will promote variable before so need the function call point in optimize codes is returned internal memory from register, the value that will promote variable after the function call again from internal memory is prefetched to the register from internal memory, so also can bring bigger expense working time.For these reasons, can select not exist call relation in the present embodiment and function that the execution time is relatively long as optimised function.
Behind selected optimised function, in conjunction with Fig. 2, and be that example illustrates its optimizing process with one of them optimised function, for other the optimised function in the application program, their optimizing process does not have substantive difference.
Those of ordinary skill in the art should understand, system's stack (system stack) does not allow a plurality of virtual pages are mapped on the Physical Page of its application, so at first apply for the stack frame of one section physical memory space as optimised function in optimizing process, this section physical memory space is called as reflection stack (image stack).Application to the reflection stack can be adopted existing techniques in realizing, as in linux operating system, can call mmap (NULL by using system, image_stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, 0,0) applies for this section physical memory space.
Mention in the description of background technology in front, when existing application program is moved on new processor, because the new older processor of processor has more register, thereby some variable data that has in the application program can be risen in the register, improve the overall performance of program to utilize the fast advantage of register travelling speed.But the number of the register that the older processor of new processor increases newly is limited, in most of the cases the stack variable in the function can be more than the quantity of spendable register, that is to say, still have the stack variable that is not thus lifted in the register in the function after optimised under normal conditions.Will mention in the following description; function after optimised can be stored in when program run in the reflection stack; and contain in the reflection stack void that is raised stack variable deposit the page or leaf be in protected state, all can exception throw to the visit of these protected virtual pages.Therefore, if those stack variables that are not raised in the optimised function are not processed, will produce a large amount of unusually in when operation to being kept at the visit that is not raised stack variable in the protected virtual page so, this can produce a very large impact program feature.At the problems referred to above, the present invention has adopted the method that a plurality of virtual pages is mapped to same Physical Page.Specifically, at first apply for one section virtual memory space, then this section virtual memory spatial mappings arrived and the identical physical memory of described reflection stack that this section virtual memory space can be called as shadow stack (shadow stack).Application to the virtual memory space can be passed through existing techniques in realizing, as can (new_size flags) applies for that this section void deposits the space, wherein old_size=0 for old_address, old_size by system call mremap on linux operating system.In follow-up code generation process, all visits to the stack variable that is not raised all can be redirected in the shadow stack in the optimised function, all explicit visit stack operations just can not cause in operational process that extra memory access is unusual in the program like this, thus expense when having reduced the operation of optimization system.
After being optimised function creation reflection stack, shadow stack, original system stack in the coupling system has formed between a stratified stack and has concerned.As shown in Figure 3, the stack frame of optimised function calls function (promptly calling the function of optimised function) is on system's stack, and the run time stack frame of optimised function is on the reflection stack, and the stack frame of the function that optimised function called is on the shadow stack.In the optimize codes implementation, comprise the void that promotes variable in the stack frame of optimised function and deposit page or leaf and be in guard mode.
The establishment of above-mentioned reflection stack, shadow stack all is to prepare for the realization of function optimization, below the process that just can in optimised function, select stack variable to how and how generate the optimize codes of this optimised function be illustrated.
Mention in the explanation in front, in most of the cases therefore the stack variable in the function can need be selected the stack variable in the optimised function more than the quantity of spendable register.Selection mode to the stack variable in the optimised function can have multiple, the stack variable that will promote as picked at random, or according to the accessed number of times of static state how much select stack variable (i.e. the static quantity of the instruction that this stack variable is conducted interviews), or accessed stack variable often preferentially is thus lifted in the register.Certainly, before selecting stack variable, also will identify all stack variables from function, being embodied as of this identifying is conventionally known to one of skill in the art, does not therefore do repeat specification herein.
After the selected stack variable that will be thus lifted in the register, generate optimize codes according to selected stack variable.In conjunction with Fig. 4 and Fig. 5, the generative process of optimize codes is illustrated.
From the left-half of Fig. 4 as can be seen, a function can be divided into function entrance, function code, three parts of function outlet.Described generation optimize codes mainly is the function code part change to wherein, but before being done optimization, the code in the function at first to insert instruction, switch to the reflection stack with function stack from system's stack, and insert and instruct the parameter that and function is relevant to copy reflection stack (step 101 Fig. 5) to from system's stack with optimised function.From the right half part of Fig. 4 as can be seen, newly be inserted with stack switch code 1 after the function entrance.Then, insert prefetched instruction at optimize codes inlet, this instruction is used for the content of the stack variable that will promote and gets relevant register (step 102).In the right half part of Fig. 4, prefetched instruction is positioned at after the stack switch code 1, puts before guard-page protection state code and the optimize codes, and has also provided the example of prefetched instruction in Fig. 1.Follow again; also to insert the void that is used for the reflection stack and deposit the code that page or leaf is made as guard mode in the porch of optimize codes; it is the guard-page protection code status code (step 103) of putting noted earlier; call the function of mprotect as insertion, make that comprising the void that is raised stack variable in the reflection stack deposits the guard mode of page or leaf and be set as PROT_NONE.Under the PROT_NONE state, void is deposited page or leaf and can not be write and can not be read, if make have the empty visit of depositing page or leaf of protect in program process, system will produce unusually.In Fig. 4 right half part, be used for that void is deposited a code that page or leaf is made as guard mode and be positioned at after the prefetched instruction.Will be optimized function code after finishing aforesaid operations.Optimization to function code comprises: all instructions that comprise the operation of lifting stack variable are replaced with the instruction (step 104) that corresponding registers is operated; All operations to the stack variable that do not promote are replaced with the operation (step 105) of corresponding void on the shadow stack being deposited the address.Code after the optimization can be stored in the code cache (code cache), and the inlet of majorized function is inserted instruction jumps to optimize codes among the code cache, will move optimize codes (step 106) like this when program is carried out optimised function.The situation that in the code of optimised function, has function call, therefore, if the function call instruction is arranged in the optimize codes, before these function call instructions, to insert the top-of-stack pointer switching command so, so that top-of-stack pointer (esp or sp) is switched in the shadow stack (step 107).By this operation, the stack frame of the function that is called will be on the shadow stack.The top-of-stack pointer switching command that is inserted is positioned at before the function call instruction, after the parameter transmission instruction, therefore, if visit protected page or leaf in the reflection stack by pointer parameter in the function that is called, can cause that then visit goes beyond one's commission unusual.Because before function code is optimised; carried out setting void and deposited a series of beamhouse operations such as guard-page protection status command, insertion prefetched instruction such as insertion stack switching command, insertion; therefore after function code optimization is finished; also to do a series of " dealing with problems arising from an accident " operation, to eliminate the influence that aforementioned beamhouse operation is brought.As shown in Figure 4; at first; insert the void that is used for the reflection stack in the exit of function optimization code and deposit the instruction (step 108) of page de-preservation state, as call the mprotect function, be converted to original writing/readable state by guard mode the void in the protected reflection stack is deposited page or leaf.Then, insert synchronic command in the optimize codes exit, return (step 109) the internal memory from register with the value of the stack variable that will be raised.Provided an example of synchronic command among Fig. 1.At last, insert the stack switching command once more in the function outlet, stack switching command herein is used for function stack frame is switched back system's stack (step 110) from the reflection stack.
The present invention also will be to handling respectively by situation of being produced in the optimize codes implementation unusually except the stack variable in the function being risen to register with the realization optimization.
After taking place unusually in the optimize codes implementation, operating system can be preserved unusual on-the-spot context automatically, carries out abnormality processing by the memory access abnormality processing function of registering then in system.In processing procedure, comprise multiple situation, illustrated below in conjunction with Fig. 6.
At first, the unusual on-the-spot context of preserving from the operating system instruction address and the memory access address (step 201) thereof that can obtain taking place unauthorized access.The branch situation is discussed then.
If unusual memory access address takes place is not that protected void is deposited in the page or leaf in the reflection stack; so unusually, do not cause by optimization of the present invention; in this case, this is not done extra processing unusually, will handle (step 202) unusually by the exception handler of giving tacit consent in the system.
If unusual instruction takes place in the function that optimised function called, this suboptimization meeting is abandoned so.In this case, in abnormality processing function, the value of the variable that promotes can be returned internal memory from register, remove empty guard mode of depositing page or leaf in the reflection stack, the instruction (step 203) that deletion is inserted in former code.
If unusual instruction takes place in optimize codes, and the memory access address is in protected page or leaf, then will generate the internal memory another name according to the position at concrete place, memory access address and check instruction and be inserted in the optimize codes, perhaps abandons optimizing (step 204).Specifically, at first will comprise the address realm that promotes variable and be divided into two parts.A zone has comprised all lifting variablees, and another one does not contain the variable of any lifting.Like this, when operation, only need to check simply whether the memory access address that exceptional instructions takes place is comprising in the address realm that promotes variable.Do not contain in the address realm that promotes variable if occur in unusually, the corresponding another name of generation is checked instruction and is inserted in the optimize codes.Comprise in the address realm that promotes variable if occur in unusually; abandon optimization that current function is carried out so; promptly the value that promotes variable to be returned internal memory from register; releasing is to empty guard mode of depositing page or leaf in the reflection stack; the instruction that deletion is inserted in former code turns back to then in the former code and the unusual corresponding instruction of instruction takes place.The major function of the another name inspection mentioned in aforesaid operations instruction is (to carry out when exceptional instructions takes place) to judge the memory access address of this instruction when operation whether in first address realm; if; then carry out amended code (visiting protected address), otherwise carry out unmodified code by the shadow stack.If unusual (illustrate that this instruction may visit the address realm that contains the stack variable that is raised) takes place when carrying out unmodified code, this time will be handled once more by abnormality processing function unusually.
It should be noted last that above embodiment is only unrestricted in order to technical scheme of the present invention to be described.Although the present invention is had been described in detail with reference to embodiment, those of ordinary skill in the art is to be understood that, technical scheme of the present invention is made amendment or is equal to replacement, do not break away from the spirit and scope of technical solution of the present invention, it all should be encompassed in the middle of the claim scope of the present invention.

Claims (11)

1. optimization method that comprises the stack variable function comprises:
Step 1), one section physical memory space of application are as the reflection stack of the function that will optimize, and then apply for this section virtual memory spatial mappings being arrived and the identical physical memory of described reflection stack the shadow stack of one section virtual memory space as the function that will optimize;
Step 2), in the described function that will optimize, discern stack variable, therefrom select to be thus lifted to stack variable in the idle register;
Step 3), insert in the porch of want majorized function with the function stack of optimised function switch to the instruction of reflection stack from system's stack, void that the content of stack variable is risen in the instruction the register and the stack of will video deposits a page instruction that is made as guard mode;
Step 4), in the code of want majorized function, all are comprised instructions to lifting stack variable operation replace with instruction, all operations to the stack variable that do not promote are replaced with the operation of corresponding void on the described shadow stack being deposited the address the corresponding registers operation;
Step 5), insert the instruction that void in the stack of will videoing is deposited page instruction of de-preservation state, the content of the stack variable that is raised is returned the instruction of internal memory, function stack frame is switched back system's stack from the reflection stack from register successively in the exit of want majorized function.
2. the optimization method that comprises the stack variable function according to claim 1 is characterized in that, also comprises the step 6) of handling unusually that optimised function is occurred in the process of implementation; This step comprises: be arranged in the function that optimised function calls when unusual instruction takes place, when unusual instruction perhaps taking place being arranged in optimised code and memory access address and occurring in protected void and deposit the address realm of the stack variable that is raised of page or leaf, abandon optimization to function; When unusual instruction taking place be arranged in optimised code and memory access address and occur in protected void and deposit the address realm of the stack variable that is not raised of page or leaf, generate another name and check that instruction also is inserted in the optimize codes.
3. the optimization method that comprises the stack variable function according to claim 2 is characterized in that, described step 6) comprises:
Step 61), obtain taking place the instruction address and the memory access address thereof of unauthorized access from unusual on-the-spot context;
Step 62), judge described memory access address whether in described reflection stack protected void deposit in the page or leaf, if, carry out next step, otherwise, directly to handling unusually of being taken place;
Step 63), whether unusual instruction judge takes place in the optimize codes of optimised function, if, carry out next step, if do not exist, and unusual instruction takes place in the function that optimised function called, then abandon optimization to optimised function;
Step 64), protected void deposited comprise the address realm that promotes variable in the page or leaf and be divided into two parts, a part has comprised all lifting variablees, another part does not comprise any lifting variable;
Step 65), memory access address that unusual instruction judge to take place whether in comprising the address realm that promotes variable, if, then abandon optimization to optimised function, otherwise, generate corresponding another name inspection and instruct and also be inserted in the optimize codes.
4. the optimization method that comprises the stack variable function according to claim 1 and 2 is characterized in that, before described step 1), also comprises the step that the function that will optimize is selected; In this step, select not exist call relation and function that the execution time is relatively long as optimised function.
5. according to claim 1 or the 2 or 4 described optimization methods that comprise the stack variable function, it is characterized in that, in described step 2) in, adopt a kind of selection in the following manner to be thus lifted to the stack variable of register: select at random, according to what of the accessed number of times of static state select, what are selected by accessed number of times.
6. according to claim 1 or the 2 or 4 described optimization methods that comprise the stack variable function, it is characterized in that, in described step 4), if there is the function call instruction that is used to call other function in the optimised function, then insert the top-of-stack pointer switching command that is used for top-of-stack pointer is switched to the shadow stack before the described function call instruction in the code of optimised function.
7. according to claim 1 or the 2 or 4 described optimization methods that comprise the stack variable function, it is characterized in that described step 3) comprises:
Step 31), the function stack that inserts behind the inlet of optimised function optimised function switches to the stack switching command of reflection stack from system's stack, and inserts the instruction that the parameter relevant with optimised function is copied to the reflection stack from system's stack;
Step 32), behind described stack switching command, insert the prefetched instruction that the content be used for the stack variable that will will promote is got corresponding registers;
Step 33), behind described prefetched instruction, insert and be used for putting guard-page protection code status code what the void of reflection stack deposited that page or leaf is made as guard mode.
8. the optimization method that comprises the stack variable function according to claim 6 is characterized in that, described step 4) comprises:
Step 41), all instructions that comprise the operation of lifting stack variable in the code of optimised function are replaced with the instruction that corresponding registers is operated;
Step 42), all operations to the stack variable that do not promote in the code of optimised function are replaced with the operation of corresponding void on the shadow stack being deposited the address;
Step 43), the code after will optimizing is kept in the code cache, and the inlet of majorized function inserted instruction jump to optimize codes in the code cache;
Step 44), insert the top-of-stack pointer switching command before the function call in the optimised function instruction, so that top-of-stack pointer is switched in the shadow stack.
9. according to claim 1 or the 2 or 4 described optimization methods that comprise the stack variable function, it is characterized in that described step 5) comprises:
Step 51), insert the void that is used for the reflection stack in the exit of function optimization code and deposit page instruction of de-preservation state;
Step 52), after the instruction of described releasing virtual page guard mode, insert the value be used for the stack variable that will be raised is returned internal memory from register synchronic command;
Step 53), behind described synchronic command, insert the stack switching command that is used for function stack frame is switched back from the reflection stack system's stack.
10. according to claim 1 or the 2 or 4 described optimization methods that comprise the stack variable function, it is characterized in that, in described step 1), call mmap (NULL, image_stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, 0,0) the function application stack of videoing.
11. according to claim 1 or the 2 or 4 described optimization methods that comprise the stack variable function, it is characterized in that, in described step 1), call mremap (old_address, old_size, new_size, flags) function is applied for the shadow stack.
CN 201010107353 2010-02-05 2010-02-05 Optimization method of stack variable-containing function Active CN101847096B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN 201010107353 CN101847096B (en) 2010-02-05 2010-02-05 Optimization method of stack variable-containing function

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN 201010107353 CN101847096B (en) 2010-02-05 2010-02-05 Optimization method of stack variable-containing function

Publications (2)

Publication Number Publication Date
CN101847096A true CN101847096A (en) 2010-09-29
CN101847096B CN101847096B (en) 2012-12-12

Family

ID=42771722

Family Applications (1)

Application Number Title Priority Date Filing Date
CN 201010107353 Active CN101847096B (en) 2010-02-05 2010-02-05 Optimization method of stack variable-containing function

Country Status (1)

Country Link
CN (1) CN101847096B (en)

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106844103A (en) * 2017-01-16 2017-06-13 山东大学 Non-volatile processor spare capacity setting, Memory Backup method and system
CN109213591A (en) * 2017-06-30 2019-01-15 合网络技术(北京)有限公司 Assist the distribution method and device of journey running stack
CN110532030A (en) * 2019-08-01 2019-12-03 安凯(广州)微电子技术有限公司 Optimize the method for register access in a kind of CPU
CN110716747A (en) * 2019-09-17 2020-01-21 百富计算机技术(深圳)有限公司 Program operation efficiency optimization method based on function parameter statistics and terminal equipment
CN111090466A (en) * 2019-12-20 2020-05-01 北京字节跳动网络技术有限公司 Optimization method, device, equipment and storage medium of call instruction
CN111209042A (en) * 2020-01-06 2020-05-29 北京字节跳动网络技术有限公司 Method, device, medium and electronic equipment for establishing function stack
CN112199163A (en) * 2020-10-16 2021-01-08 常熟理工学院 Virtual instruction method, device, equipment and storage medium for analyzing and interpreting routine
CN113377466A (en) * 2021-06-29 2021-09-10 展讯通信(天津)有限公司 Algorithm execution method and device and electronic equipment

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN100511119C (en) * 2007-08-03 2009-07-08 东南大学 Method for realizing shadow stack memory on picture and circuit thereof

Cited By (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106844103B (en) * 2017-01-16 2019-05-21 山东大学 Non-volatile processor spare capacity setting, Memory Backup method and system
CN106844103A (en) * 2017-01-16 2017-06-13 山东大学 Non-volatile processor spare capacity setting, Memory Backup method and system
CN109213591B (en) * 2017-06-30 2021-06-04 阿里巴巴(中国)有限公司 Method and device for distributing coroutine running stack
CN109213591A (en) * 2017-06-30 2019-01-15 合网络技术(北京)有限公司 Assist the distribution method and device of journey running stack
CN110532030A (en) * 2019-08-01 2019-12-03 安凯(广州)微电子技术有限公司 Optimize the method for register access in a kind of CPU
CN110532030B (en) * 2019-08-01 2023-07-07 广州安凯微电子股份有限公司 Method for optimizing register access in CPU
CN110716747A (en) * 2019-09-17 2020-01-21 百富计算机技术(深圳)有限公司 Program operation efficiency optimization method based on function parameter statistics and terminal equipment
CN111090466A (en) * 2019-12-20 2020-05-01 北京字节跳动网络技术有限公司 Optimization method, device, equipment and storage medium of call instruction
CN111209042A (en) * 2020-01-06 2020-05-29 北京字节跳动网络技术有限公司 Method, device, medium and electronic equipment for establishing function stack
CN111209042B (en) * 2020-01-06 2022-08-26 北京字节跳动网络技术有限公司 Method, device, medium and electronic equipment for establishing function stack
CN112199163B (en) * 2020-10-16 2021-12-28 常熟理工学院 Virtual instruction method, device, equipment and storage medium for analyzing and interpreting routine
CN112199163A (en) * 2020-10-16 2021-01-08 常熟理工学院 Virtual instruction method, device, equipment and storage medium for analyzing and interpreting routine
CN113377466A (en) * 2021-06-29 2021-09-10 展讯通信(天津)有限公司 Algorithm execution method and device and electronic equipment

Also Published As

Publication number Publication date
CN101847096B (en) 2012-12-12

Similar Documents

Publication Publication Date Title
CN101847096B (en) Optimization method of stack variable-containing function
US9110826B2 (en) Memory allocation in a system using memory striping
CN1127687C (en) RISC processor with context switch register sets accessible by external coprocessor
CN102819497B (en) A kind of memory allocation method, Apparatus and system
KR20020022068A (en) Method and apparatus for enhancing scheduling in an advanced microprocessor
JPH04225431A (en) Method for compiling computer instruction for increasing instruction-cache efficiency
US9201806B2 (en) Anticipatorily loading a page of memory
KR100368166B1 (en) Methods for renaming stack references in a computer processing system
CN102667714B (en) Support the method and system that the function provided by the resource outside operating system environment is provided
US20060010304A1 (en) Systems for loading unaligned words and methods of operating the same
CN105074657A (en) Hardware and software solutions to divergent branches in a parallel pipeline
US5375213A (en) Address translation device and method for managing address information using the device
EP2182433A1 (en) Indirect branching program, and indirect branching method
CN101236489B (en) Dummy hardware system and its instruction execution method and dummy machine
CN103140834A (en) Processor support for filling memory regions
US8327122B2 (en) Method and system for providing context switch using multiple register file
CN107451070A (en) The processing method and server of a kind of data
US8266379B2 (en) Multithreaded processor with multiple caches
US20160092182A1 (en) Methods and systems for optimizing execution of a program in a parallel processing environment
US7934073B2 (en) Method for performing jump and translation state change at the same time
JP3896238B2 (en) Computer system and program runtime representation method
US7890740B2 (en) Processor comprising a first and a second mode of operation and method of operating the same
JP2022140995A (en) Information processing device, compile program, and compile method
CN101571831A (en) Memory subsystem, management method thereof and computer system containing memory subsystem
TW201005649A (en) Operating system fast run command

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant