The present invention relates generally to a controlling execution of a binary code and, more specifically, to a device and method of controlling an execution of a binary code by multiple threads.
There is a case where, while a converted binary code acquired by converting an original binary code is executed, part of the original binary code modifies itself. Such a code that modifies itself is called SMC (Self-Modifying Code). When the modification by this SMC occurs, it becomes impossible to use part of a converted binary code corresponding to the modified part.
JP2011-40087A discloses that, when writing in a protected page is attempted (for example, writing by a self-correcting code or in a page in which a text and data are mixed), it is trapped and the page is set without protection again.
JP2002-502516A discloses that, since a self-modifying code is used to often rewrite an instruction and therefore the conversion is repeatedly returned to a state of “original”, even in a case where the attempt to convert the instruction is useless, an instruction of type EXECUTED is used.
JP2010-525440A discloses that it is possible to intentionally install a segmentation violation as a trap to detect a case where a target code attempts access to an area in which a memory is protected, such as a case where it attempts writing in a self-modifying code.
In one embodiment, an apparatus for controlling an execution of a binary code by multiple threads includes a detection unit configured to detect an occurrence of modification of a first part that is a part of a first binary code by a self-modifying code; a specifying unit configured to specify a second part that is a part corresponding to the first part in a second binary code acquired by converting the first binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; and a correction unit configured to correct the second part such that a specific thread that executes the second part of the second binary code among the multiple threads causes an exception.
In another embodiment, an apparatus for controlling an execution of a binary code by multiple threads includes a generation unit configured to generate an optimized binary code by optimizing an original binary code; a detection unit configured to detect an occurrence of modification of a first part that is a part of the original binary code by a self-modifying code while multiple threads execute the optimized binary code; a specifying unit configured to specify a second part that is a part corresponding to the first part in the optimized binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; a correction unit configured to correct the second part such that a specific thread that executes the second part of the optimized binary code among the multiple threads causes an exception; and a control unit configured to perform control such that the specific thread executes the first part of the original binary code modified by the self-modifying code when the exception occurs.
In another embodiment, a method of controlling an execution of a binary code by multiple threads includes detecting an occurrence of modification of a first part that is a part of a first binary code by a self-modifying code; specifying a second part that is a part corresponding to the first part in a second binary code acquired by converting the first binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; and correcting the second part such that a specific thread that executes the second part of the second binary code among the multiple threads causes an exception.
In another embodiment, a non-transitory, computer readable storage medium has computer readable instructions stored thereon that, when executed by a computer, implement a method of controlling an execution of a binary code by multiple threads. The method includes detecting an occurrence of modification of a first part that is a part of a first binary code by a self-modifying code; specifying a second part that is a part corresponding to the first part in a second binary code acquired by converting the first binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; and correcting the second part such that a specific thread that executes the second part of the second binary code among the multiple threads causes an exception.
As described above, when modification by SMC occurs, since it is not possible to use part of a converted binary code, it is necessary to recover from such a state. At that time, the improvement of performance of the recovering processing is desired. Although the above described techniques relate to a self-modifying code, they do not suggest means for improving the performance of such recovering processing.
Accordingly, embodiments of the present invention improve the performance of processing to recover from a state where it is not possible to use part of a converted binary code due to the occurrence of modification by SMC. More specifically, embodiments of the present invention provide a device that controls an execution of a binary code by multiple threads, including: a detection unit configured to detect an occurrence of modification by a self-modifying code, of a first part that is a part of a first binary code; a specifying unit configured to specify a second part that is a part corresponding to the first part in a second binary code acquired by converting the first binary code, in response to detection of the occurrence of modification by the self-modifying code, of the first part; and a correction unit configured to correct the second part such that a specific thread that executes the second part of the second binary code among the multiple threads causes an exception.
In this device, the correction unit may correct the second part by replacing at least one instruction corresponding to an instruction included in the first part and modified by the self-modifying code among multiple instructions included in the second part, with an interrupt instruction.
Moreover, this device may further include a control unit configured to perform control such that the specific thread executes the first part of the first binary code modified by the self-modifying code when the exception occurs. In that case, the control unit may perform control such that the specific thread executes the first part of the first binary code after a link from other parts than the second part of the second binary code to the second part is separated.
Furthermore, this device may further include a maintenance unit configured to maintain a state where a page which is a memory area to store at least part of the first part and include an instruction modified by the self-modifying code is mapped to a virtual page to which page protection is not applied and which is not used, while the specifying unit specifies the second part and the correction unit corrects the second part.
Moreover, embodiments of the present invention also provide a device that controls an execution of a binary code by multiple threads, including: a generation unit configured to generate an optimized binary code by optimizing an original binary code; a detection unit configured to detect an occurrence of modification by a self-modifying code, of a first part that is a part of the original binary code while multiple threads execute the optimized binary code; a specifying unit configured to specify a second part that is a part corresponding to the first part in the optimized binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; a correction unit configured to correct the second part such that a specific thread that executes the second part of the optimized binary code among the multiple threads causes an exception; and a control unit configured to perform control such that the specific thread executes the first part of the original binary code modified by the self-modifying code when the exception occurs.
Furthermore, embodiments of the present invention also provide a method of controlling an execution of a binary code by multiple threads, including detecting an occurrence of modification by a self-modifying code, of a first part that is a part of a first binary code; specifying a second part that is a part corresponding to the first part in a second binary code acquired by converting the first binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; and correcting the second part such that a specific thread that executes the second part of the second binary code among the multiple threads causes an exception.
Furthermore, embodiments of the present invention also provide a program that causes a computer to function as a device that controls an execution of a binary code by multiple threads, and causes the computer to function as: a detection unit configured to detect an occurrence of modification by a self-modifying code, of a first part that is a part of a first binary code; a specifying unit configured to specify a second part that is a part corresponding to the first part in a second binary code acquired by converting the first binary code, in response to detection of the occurrence of modification of the first part by the self-modifying code; and a correction unit configured to correct the second part such that a specific thread that executes the second part of the second binary code among the multiple threads causes an exception.
According to embodiments of the present invention, the performance of processing to recover from a state where it is not possible to use part of a converted binary code due to the occurrence of modification by SMC, improves.
The original code cache 10 stores an original binary code before optimization by the optimizer 40. The optimized code cache 20 stores an optimized binary code after optimization by the optimizer 40. The optimizer 40 generates the optimized binary code by optimizing the original binary code stored in the original code cache 10 and stores this optimized binary code in the optimized code cache 20. In the present embodiment, the optimizer 40 is installed as one example of a generation unit to generate the optimized binary code.
The runtime 50 controls an execution of the optimized binary code stored in the optimized code cache 20 by multiple threads. Especially, it controls an execution in a case where the original binary code is modified by SMC while multiple threads execute the optimized binary code. In general, it is possible to deal with the SMC in the binary conversion device 1 as described in further detail below.
Moreover, when the global synchronization is to be performed, there is a possibility of falling into an infinite waiting state. For example, although one of the threads waits by a conditional variable, the other threads wait by a synchronous point. That is, it is assumed that one of the threads waits that the value of a certain variable becomes one. In that case, when the other threads are stopped by global synchronization, this thread continues to wait that the value of the variable becomes one, it becomes impossible to realize the global synchronization.
Therefore, in the present embodiment, correction to write a software interruption instruction in a trace that becomes invalid is performed using a thread that causes modification by SMC. In the present embodiment, this correction is called “poisoning”.
Functional Configuration of Runtime in Present Embodiment
The SMC detection unit 51 applies page protection to the original code cache 10 and detects that an instruction included in a trace with the original binary code is to be modified by SMC. Moreover, it permits original binary code to be modified by the SMC through a virtual page to which the mapping processing unit 52 mapped a page of the original binary code. In the present embodiment, the original binary code is used as one example of the first binary code, and the trace of the original binary code is used as the first part that is a part of the first binary code. Moreover, the SMC detection unit 51 is installed as one example of a detection unit to detect the occurrence of modification of the first part by the self-modifying code.
To enable modification of the original binary code by the SMC, the mapping processing unit 52 maps the page of the original binary code to a virtual page to which page protection is not applied and which is not used. Here, the mapped page is acceptable as long as it is a memory area in which at least part of a trace is stored and which includes an instruction modified by the SMC. Moreover, after the trace specifying unit 54 specifies the trace, the trace correction unit 55 corrects the trace and the SMC detection unit 51 permits the modification by the SMC, the mapping is released. In the present embodiment, the mapping processing unit 52 is installed as one example of a maintenance unit to maintain a state where the page is mapped.
When the SMC detection unit 51 detects that the original binary code is to be modified by the SMC, the flag setting unit 53 sets an SMC processing flag. Specifically, it turns off the SMC processing flag in the initial state, and, when the SMC detection unit 51 detects that an instruction included in a certain trace is to be modified by the SMC, it turns on the SMC processing flag with respect to the trace and also holds the optimizer 40 in a memory that can be referred to. Further, all threads which the optimizer 40 executes check this SMC processing flag before optimization is performed. This can be mounted by a reading lock or writing lock. That is, although the reading lock is held in a case where normal optimization is performed, the writing lock is held in a case where the modification by the SMC occurs. It seems that a cost to perform optimization is greatly larger than a cost to hold the reading lock or writing lock. By this means, in a case where the SMC processing flag is turned on, other threads do not perform optimization.
The trace specifying unit 54 specifies a trace of an optimized binary code corresponding to a trace of the original binary code to be modified by the SMC. In the present embodiment, the optimized binary code is used as one example of the second binary code acquired by converting the first binary code, and the trace of the optimized binary code is used as one example of the second part that is a part corresponding to the first part of the second binary code. Moreover, the trace specifying unit 54 is installed as one example of a specifying unit to specify the second part.
The trace correction unit 55 performs poisoning by overwriting all instructions included in the trace of the optimized binary code specified by the trace specifying unit 54 with a software interruption instruction. Alternatively, although only an instruction sequence corresponding to an instruction of the original binary code to be modified by the SMC may be overwritten with the software interruption instruction, in the present embodiment, an explanation is given with an assumption that all instructions included in a trace are overwritten with the software interruption instruction. In the present embodiment, the trace correction unit 55 is installed as one example of a correction unit to correct the second part of the second binary code.
The thread execution control unit 56 controls an execution of the optimized binary code by multiple threads. Moreover, in a case where a thread that executes the software interruption instruction written by the trace correction unit 55 causes an exception, it performs control such that the thread executes a trace of the original binary code modified by the SMC. In the present embodiment, the thread execution control unit 56 is installed as one example of a control unit to perform control such that a specific thread executes the first part of the first binary code modified by a self-modifying code.
Operation of Runtime in Present Embodiment
As illustrated in the figure, in the runtime 50, first, the SMC detection unit 51 detects that the original binary code stored in the original code cache 10 is to be modified by an SMC (operation 501). Specifically, page protection is applied to the original binary code stored in the original code cache 10. In this state, since interruption occurs when the original binary code is to be modified by the SMC, it is possible to detect that the original binary code is to be modified by the SMC.
Then, the SMC detection unit 51 specifies an instruction of the original binary code stored in the original code cache 10 to be modified by the SMC (operation 502). When detecting that the original binary code is to be modified by the SMC, an address to be modified is found. Meanwhile, since correspondence between an instruction and an address has been analyzed by acquisition of identification information and register information of an instruction code, correction of branch destination, and so on, it is possible to easily specify an instruction to be modified by the SMC.
Next, the mapping processing unit 52 maps a page including an instruction of the original binary code stored in the original code cache 10 to be modified by the SMC, to a virtual page to which page protection is not applied and which is not used (operation 503). Here, the unused virtual page can be found by making an inquiry to “/proc/pid/maps” in LINUX (R). Also, in WINDOWS (R), it is possible to find it by giving NULL to “MapViewOfFileEx( )”. In a case where writing is performed without performing global synchronization, it is not possible to turn off a page protection bit and therefore mapping to such a virtual page is required.
Moreover, the flag setting unit 53 turns on an SMC processing flag such that a series of processing succeeds or fails and access from others cannot be performed during processing (operation 504).
Thus, in a state where the SMC processing flag is turned on, first, the trace specifying unit 54 finds all traces of the original binary code subjected to an influence of modification by the SMC among traces stored in the optimized code cache 20 (operation 505).
Next, the trace correction unit 55 performs correction (poisoning) to write a software interruption instruction in all traces subjected to the influence of modification by the SMC, which are found in operation 505 (operation 506). By this means, a thread that executes a trace subjected to poisoning causes interruption.
Next, the SMC detection unit 51 performs modification by the SMC on the instruction specified in operation 502 in the original binary code stored in the original code cache 10, by the use of information obtained by analyzing the instruction (operation 507). By this means, the thread execution control unit 56 performs control such that a thread that causes interruption in response to operation 506 executes a trace of the original binary code modified by the SMC. At this time, as illustrated in
When these items of processing end, the flag setting unit 53 turns off the SMC processing flag which is turned on in operation 504 (operation 508). Here, important parts in operations 505 to 507 are expected to be short.
Moreover, the mapping processing unit 52 releases the mapping of the page of the original binary code to the virtual page, which is performed in operation 503 (operation 509). Even in a case where another thread performs modification by the SMC on the same page while processing related to the SMC is performed, a state where mapping up to this timing is performed is maintained such that this can be processed at high speed.
Here, in this operation example, although it is described that the trace of the original binary code modified by the SMC can be executed immediately after operation 507, the trace may be executed after the end of processing in operation 509.
While, in the present embodiment, the thread that executes the trace subjected to poisoning executes the trace of the original binary code modified by the SMC, it is not limited to this. A trace of an optimized binary code may be generated by optimizing the trace of the original binary code modified by the SMC again, and the trace of this optimized binary code may be executed.
Moreover, in the present embodiment, although a case has been described where the present invention is applied to the binary conversion device 1 that performs conversion for optimization on the original binary code, it is not limited to this. It is possible to apply the present invention even if the binary conversion device 1 performs any conversion on the original binary code.
As described above, in the present embodiment, when the original binary code is modified by the SMC, a trace subjected to the influence of a converted binary code acquired by converting the original binary code is specified, and the trace is corrected such that a thread that executes the trace causes an exception. By this means, it becomes possible to improve the performance of processing to recover from a state where part of the converted binary code cannot be used due to the occurrence of modification by the SMC.
Hardware Configuration of Digital Book Display Device of Present Embodiment
Here, in
Here, the present invention may be all realized by hardware or may be all realized by software. Moreover, it is also possible to realize it by both hardware and software. Moreover, the present invention can be realized as a computer, a data processing system and a computer program. This computer program may be stored in a computer-readable medium and provided. Here, as a medium, an electronic, magnetic, optical, electromagnetic, infrared or semiconductor system (device or equipment) or a propagation medium is possible. Moreover, as a computer-readable medium, there are exemplified a semiconductor, a solid-state storage device, a magnetic tape, a removable computer diskette, a random access memory (RAM), a read only memory (ROM), a rigid magnetic disk and an optical disc. Examples of an optical disc at present include a compact disc read only memory (CD-ROM), a compact disc read/write (CD-R/W) and a DVD.
Although an explanation has been given above using the embodiment of the present invention, the technical scope of the present invention is not limited to the above-mentioned embodiment. It is clear for those skilled in the art that it is possible to add various changes or adopt an alternative mode without departing from the spirit and scope of the present invention.
10 . . . Original code cache
20 . . . Optimized code cache
30 . . . DBT
40 . . . Optimizer
50 . . . Runtime
51 . . . SMC detection unit
52 . . . Mapping processing unit
53 . . . Flag setting unit
54 . . . Trace specifying unit
55 . . . Trace correction unit
56 . . . Thread execution control unit
Number | Date | Country | Kind |
---|---|---|---|
2014-055409 | Mar 2014 | JP | national |
This application is a continuation of U.S. patent application Ser. No. 14/746,875, filed Jun. 23, 2015; which is a continuation of U.S. patent application Ser. No. 14/634,018, filed Feb. 27, 2015; which claims priority to Japanese Patent Application No. 2014-055409, filed Mar. 18, 2014, and all the benefits accruing therefrom under 35 U.S.C. §119, the contents of which in its entirety are herein incorporated by reference.
Number | Date | Country | |
---|---|---|---|
Parent | 14746875 | Jun 2015 | US |
Child | 15437530 | US | |
Parent | 14634018 | Feb 2015 | US |
Child | 14746875 | US |