CN114968282A - Optimization method and system for exception handling execution - Google Patents

Optimization method and system for exception handling execution Download PDF

Info

Publication number
CN114968282A
CN114968282A CN202210549368.8A CN202210549368A CN114968282A CN 114968282 A CN114968282 A CN 114968282A CN 202210549368 A CN202210549368 A CN 202210549368A CN 114968282 A CN114968282 A CN 114968282A
Authority
CN
China
Prior art keywords
exception
exception handling
information table
package
stack
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.)
Pending
Application number
CN202210549368.8A
Other languages
Chinese (zh)
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.)
Beijing WatchSmart Technologies Co Ltd
Original Assignee
Beijing WatchSmart Technologies Co Ltd
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 Beijing WatchSmart Technologies Co Ltd filed Critical Beijing WatchSmart Technologies Co Ltd
Priority to CN202210549368.8A priority Critical patent/CN114968282A/en
Publication of CN114968282A publication Critical patent/CN114968282A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/60Software deployment
    • G06F8/61Installation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/455Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
    • G06F9/45533Hypervisors; Virtual machine monitors
    • G06F9/45558Hypervisor-specific management and integration aspects
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/455Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
    • G06F9/45533Hypervisors; Virtual machine monitors
    • G06F9/45558Hypervisor-specific management and integration aspects
    • G06F2009/45562Creating, deleting, cloning virtual machine instances
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/455Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
    • G06F9/45533Hypervisors; Virtual machine monitors
    • G06F9/45558Hypervisor-specific management and integration aspects
    • G06F2009/45575Starting, stopping, suspending or resuming virtual machine instances

Abstract

The invention relates to an optimization method and system for exception handling execution, and belongs to the technical field of computer software. The method comprises the steps of converting a plurality of class files compiled in a program package into a loadable execution file, executing a method call instruction to execute the method in the loadable file and judge whether each method has an exception handler or not, if so, adding position information of an exception handling information table in the method to a field corresponding to a stack frame, and when the virtual machine throws an exception, acquiring the position of the exception handling information table according to the stack frame of the current method and handling the exception. By adopting the method, the virtual machine does not need to maintain a global exception handling information table link list when realizing the exception handling function, the installation and deletion speed of the program package can be improved, the processing efficiency of the virtual machine when handling the exception can be improved, and the execution performance of the virtual machine on the program package is improved.

Description

Optimization method and system for exception handling execution
Technical Field
The invention belongs to the technical field of computer software, and particularly relates to an optimization method and system for exception handling execution, which are used for realizing an exception handling function of a virtual machine on a resource-limited device.
Background
In a conventional Java program, all methods in a class are stored in a data structure called a class file, each method may have an exception table and the code for each method includes the code for the exception handler to which its exception handling table refers, and when a class file is created, all exceptions associated with the method are arranged in an exception handling table. If no matching catch clause exists, the exception state of the caller of the method is recovered, the matched exception handler is continuously searched, the steps are repeated until the matched exception handler is found for exception handling, or the matched exception handler does not exist, and the virtual machine stops executing.
When a Java language virtual machine is realized in a resource-limited device in the prior art, a link list of a global exception handling information table needs to be maintained when a mask is preset or a post-installation package is sent to the virtual machine, so that the complexity of installation and deletion operations of the post-installation package is increased; when the program throws the exception, the virtual machine needs to query the global exception handling information table link list, obtain the position information of the exception handler of the current method, and execute to find the matched exception handler, and when the platform has more installation program packages, the query time of the exception handling information table is increased, so that the execution time of the program is increased. Therefore, the prior art method has the problem of long processing time when the exception is processed by executing the program package in the resource-limited device.
Disclosure of Invention
In view of the defects in the prior art, the invention aims to provide an optimization method and system for exception handling execution. According to the method and the system, the content of each component in the method is rearranged, a class internal representation method is adopted for the exception class defined by the program package in the exception handling information table, when the virtual machine executes the method and calls an instruction, the newly-built stack frame optimizes the exception handling process by increasing the position information of the exception handling information table of the method, the virtual machine does not need to maintain a global exception handling information table link list when realizing an exception handling function, the installation and deletion speed of the program package can be improved, the processing efficiency of the virtual machine when handling the exception can be improved, and the execution performance of the virtual machine on the program package is improved.
In order to achieve the purpose, the invention adopts the technical scheme that: a method for optimizing exception handling execution, comprising the steps of:
converting a plurality of class files compiled in the program package into an initial file which can be installed and executed on a resource-limited device through a preset conversion program; the program package is a program installation package to be installed; the initial file comprises sections and sub-sections related to the abnormity, wherein the sections and sub-sections related to the abnormity comprise a dynamic binding section, a code section, a constant pool sub-section and a code constant pool index position sub-section; the abnormal class defined by the program installation package is represented by the offset of the dynamic binding segment;
executing each method in the initial file by executing a method calling instruction, and judging whether each method has an exception handler or not in the executing process; when the method is called, the created method stack frame comprises the position information of the exception handling information table of the method;
if the method has an exception handler, adding the position information of an exception handling information table in the method to a field corresponding to the stack frame;
and when the virtual machine throws the exception, acquiring the position of an exception handling information table according to the stack frame of the current method and handling the exception in the method.
Further, the dynamic binding section acquires information of an abnormal class defined by the program package; the information of the abnormal class is represented by a final clause, the abnormal class defined by the package and the abnormal class of the imported package, the abnormal class defined by the package is represented by the offset of a dynamic binding segment, and the abnormal class of the imported package is represented by a constant pool index mode;
the constant pool subsections acquire the reference information of the abnormal class in the program package; the constant pool sub-segment refers to an exception class imported into the program package;
the code constant pool index position sub-segment is used for analyzing the constant pool index when the program package is installed;
the method for acquiring all classes in the program package forms the code segment, and each method comprises a method header, a corresponding byte code and an optional exception handling information table.
Further, the bytecodes of the method include a normally executed bytecode and an optional bytecode of an exception handler, and each method includes: a method header and an optional exception handling information table of the method, and a bytecode of the method;
the method header includes a tag field that indicates whether an exception handling information table is included in the method.
Further, the exception handling information table includes a field indicating the number of the exception entry items and an exception handling information table, and the acquiring a position of the exception handling information table according to the stack frame of the current method and handling the exception in the method includes:
when the exception is thrown out, the position of an exception handling information table is obtained according to the stack frame of the current method; wherein the exception handling information table comprises a start address, a length, exception class information and an offset address of an exception handler;
judging whether the method has an abnormal processing information table or not;
and reading the exception handling information table to perform matching processing of the exception handler so as to process the exception in the method.
Further, after executing the method call instruction to execute each method in the initial file and determining whether each method has an exception handler in the executing process, the method further includes:
if the method has no exception handler, adding a special value for identifying no exception to a field corresponding to the frame stack; the special value is not the same as the valid method field location information.
A system for optimizing exception handling execution, comprising:
the initial file acquisition device is used for converting a plurality of class files compiled in the program package into an initial file which can be installed and executed on the resource limited device through a preset conversion program; the program package is a program installation package to be installed; the initial file comprises sections and subsections related to the abnormity, wherein the sections and subsections related to the abnormity comprise a dynamic binding section, a code section, a constant pool subsection and a code constant pool index position subsection;
the exception handler judging device is used for executing each method in the initial file by executing the method calling instruction and judging whether each method has an exception handler or not in the executing process; when the method is called, the created method stack frame comprises the position information of the exception handling information table of the method;
the position information adding device is used for adding the position information of the exception handling information table in the method to a field corresponding to the stack frame if the method has an exception handler;
and the exception handling device is used for acquiring the position of an exception handling information table according to the stack frame of the current method and handling the exception in the method when the virtual machine throws the exception.
Further, the dynamic binding section acquires information of an abnormal class defined by the program package; the information of the exception class is represented by a final clause, the exception class defined by the package and the exception class of the import package, the exception class defined by the program installation package is represented by the offset of a dynamic binding segment, and the exception class of the import package is represented by a constant pool index;
the constant pool subsections acquire the reference information of the abnormal class in the program package; the constant pool sub-segment refers to an exception class imported into the program package;
the code constant pool index position sub-segment is used for analyzing the constant pool index when the program package is installed;
the method for acquiring all classes in the program package forms the code segment, and each method comprises a method header, a corresponding byte code and an optional exception handling information table.
Further, the bytecodes of the method include a normally executed bytecode and an optional bytecode of an exception handler, and each method includes: a method header and an optional exception handling information table of the method, and a bytecode of the method;
the method header includes a tag field that indicates whether an exception handling information table is included in the method.
Further, the exception handling information table includes a field indicating the number of the entry entries of the exception and an exception handling information table, and the exception handling apparatus includes:
the position acquisition unit is used for acquiring the position of an exception handling information table according to the stack frame of the current method when the exception is thrown; wherein the exception handling information table comprises a start address, a length, exception class information and an offset address of an exception handler;
the judging unit is used for judging whether the method has an abnormal processing information table or not;
and the matching processing unit is used for reading the exception handling information table to perform matching processing of the exception handler so as to process the exception in the method.
Further, the system further comprises:
a special value adding device, configured to add a special value identifying no exception to a field corresponding to the frame stack if the method has no exception handler; the special value is not the same as the valid method field location information.
The invention has the following effects: by adopting the method, the content of each component in the method is rearranged, the internal representation method of the class is adopted for the abnormal class defined by the program package in the abnormal processing information table, when the virtual machine executes the method and calls the instruction, the newly-built stack frame optimizes the abnormal processing process by increasing the position information of the abnormal processing information table of the method, the virtual machine does not need to maintain a global abnormal processing information table link list when realizing the abnormal processing function, the installation and deletion speed of the program package can be improved, the processing efficiency of the virtual machine when processing the abnormal processing can be improved, and the execution performance of the virtual machine on the program package can be improved.
Drawings
FIG. 1 is a flow chart of a method of the present invention;
FIG. 2 is a schematic diagram of an application scenario of the method of the present invention;
FIG. 3 is a schematic diagram of another application scenario of the method of the present invention;
FIG. 4 is a schematic view of another sub-flow of the process of the present invention;
fig. 5 is a block diagram of the system of the present invention.
Detailed Description
The invention is further described with reference to the following figures and detailed description.
The invention describes an optimization method for executing exception handling, which is applied to a virtual machine configured in a user terminal, and is executed through application software installed in the user terminal, wherein the user terminal is terminal equipment used for executing the optimization method for executing the exception handling so as to install or delete a program package, the virtual machine of the user terminal processes exceptions in the program package while executing the program package, and the user terminal can be a resource-limited device such as an intelligent SE (secure element), a safety MCU (microprogrammed control unit) and the like. The invention belongs to the technical field of computer software, particularly relates to object-oriented computer software, and particularly relates to an exception handling function of a virtual machine in resource-limited equipment such as an intelligent SE (secure element).
Abbreviations and key term definitions are shown in table 1.
TABLE 1
Figure BDA0003654035070000061
Figure BDA0003654035070000071
In the context of a computer program written in the Java language, an exception handler is used to protect a specific set of program code, called protected code block, when the Java program violates semantic constraints of the Java programming language, the Java virtual machine notifies the program of the error as an exception, Java uses a try/catch statement block to capture the exception, a try statement comprises a try block, zero or more catch clauses and optionally a find clause, uses try to pack code segments where exceptions may occur, the catch clause defines an exception handler, during execution, once an exception occurs, an instance corresponding to an exception class is generated, and matching is performed in catch according to the type of the instance; and when the exception instance in the try is matched with a catch, entering the catch to process the exception, and executing subsequent codes after the processing is finished. the try-catch-finish structures may be nested.
The code fragments for exception handling are as follows:
try{
// program code
Great cat (variable name 1 of exception type 1 exception)
// program code
Great cat (variable name 2 of exception type 2 exception)
// program code
}finally{
// program code
}。
In a conventional Java program, all methods in a class (the methods associated with the code set forth in the specific embodiment are all methods in the code) are stored in a data structure called a class file, each method may have an exception table and the code of each method includes the code of an exception handler referenced by its exception handling table, and when a class file is created, all exceptions associated with the method are arranged in an exception handling table.
If no matching catch clause exists, the exception state of the caller of the method is recovered, the matched exception handler is continuously searched, the steps are repeated until the matched exception handler is found for exception handling, or the matched exception handler does not exist, and the virtual machine stops executing.
The Java Stack (Stack) saves the running state of threads (a virtual machine implemented by a resource-limited device usually does not support multithreading) by taking a Frame Stack (Frame) as a unit, and the virtual machine directly executes two operations on the Java Stack: pushing or popping the stack by taking the frame stack as a unit; when a thread calls a Java method, a virtual machine presses a new frame stack in the Java stack, the new frame stack becomes a current frame stack, and when the method is executed, the frame stack is used for storing data such as parameters, local variables, intermediate operation results (operand stacks) and the like.
The Java method is completed in two ways, one is returned through return, which is called normal return; one is aborted by throwing an exception, and regardless of the way back, the virtual machine pops up and then releases the current frame stack, and the frame stack of the previous method becomes the current frame stack.
The stack frame of a virtual machine executing an operand stack instruction typically consists of three parts: a local variable region, an operand stack and a frame stack data region. The compiler puts the parameters into the local variables according to the declared sequence, and the parameter this is implicitly added for any example method and used for representing the object of the method. And the operand stack is organized into an array with the word length as a unit, the array is accessed through stack operation (stack pushing and stack popping), the virtual machine operates in a stack-based mode, and the instruction acquires the operand from the operand stack. Frame stack data area, saving calling method state information, supporting method return, exception handling table position of method, firewall information, etc. And the stack frame of the virtual machine for executing the register instruction consists of a register area and a frame stack data area.
In the field of security chips (including intelligent SE, secure MCU, etc.), a multi-application chip operating system supporting dynamic downloading of applications has been widely deployed and used in the fields of finance, telecommunications, transportation, etc.
The virtual machine is an abstract computer generated by a software application program or an instruction sequence executed by a processor, the platform independence refers to an application program written by an object-oriented high-level programming language such as Python, Java, Kotlin and the like, the application program can be executed on various different computer platforms through the virtual machine, and the instruction sequence of the virtual machine is one or more byte code streams coded by an independent platform.
In the process of Java exception handling by the conventional resource-constrained device, because the virtual machine implemented by the conventional resource-constrained device cannot directly load and run Class files, a conversion program running on the PC end is required to convert a plurality of Class files in the package into a file format that can be installed and executed in the SE, such as a CAP file of a Java card, where the CAP includes Class components, method components, etc., and constant pool components, etc., and specific contents may refer to specifications of the Java card virtual machine.
The conversion program processes the methods in the program package, the exception processing information of all methods of all classes in the package is placed in the first part of the method component, and the byte codes of the methods of all classes in the package are connected together and placed in the second part of the method component.
The virtual machine is realized, a link list of a global exception handling information table is established, exception handling information of all methods of all classes of each packet forms an exception handling information table in a method component of the packet, the exception handling information tables of all packets are linked together to form an exception information table link list, a later-installed exception information table is placed in a table head of the link list, and a global pointer points to the table head of the link list.
Establishing a global exception table, and linking the exception handling information tables (if existing) of all the method components of the mask film into an exception handling information table linked list by a conversion program; a package installer implemented within the device links the exception handling information table (if any) of the method components of the package to the header of the exception handling linked list.
The stack frame uses the global exception handling information table to link the list, and the newly-built stack frame does not need to record the position information of the exception handling information table of the method when the method is called, so that the requirement of the stack frame on the RAM can be reduced, and the effect is obvious particularly when the calling depth of the method is larger.
When the exception is thrown, because the stack frame does not record the position information of the exception handling information table of the method, a global exception handling information table link list needs to be inquired according to a program counter and an exception instance, whether the exception handler can capture the thrown exception or not is determined, if the exception is thrown, control is transferred to the exception handler for execution, if the current method has no exception handler, the virtual machine pops up the current stack frame and recovers the previous one, namely, the stack frame of the method is called, the global exception handling information table link list is inquired by using the program counter and the exception instance, whether the exception handler exists or not is determined, and the steps are repeated until the exception handler is found or the exception handler virtual machine is not found and stops executing.
As shown in fig. 1, an embodiment of the present invention discloses an optimization method for exception handling execution, which includes the following steps:
and S110, converting the plurality of class files compiled in the program package into an initial file which can be installed and executed on the resource-limited device through a preset conversion program.
Specifically, a plurality of Class files compiled by Java source codes in a program package can be converted into an initial file which can be installed and executed on a resource-limited device through a preset conversion program, wherein the program package is a program installation package to be installed; the initial file comprises sections and sub-sections related to the exception, and the sections and sub-sections related to the exception comprise a dynamic binding section, a code section, a constant pool sub-section and a code constant pool index position sub-section.
When the technical method is concretely realized, a conversion program running at a PC end is needed to convert a Class (or a suitably modified Class) file compiled by a plurality of Java source codes in a package into an initial file which can be installed and executed in an SE (secure element), namely the file format of the initial file is the same as the executable format in the SE, such as a TeF (tool File Format) file of Song. The obtained specific information of the initial file is shown in fig. 2, and the executable file 200 (initial file) contains segments and sub-segments related to exception handling, specifically including a dynamic binding segment 201, a code segment 202, a constant pool sub-segment 203, and a code constant pool index position sub-segment 204.
In one embodiment, the dynamic binding segment 201 includes information of the exception class defined by the package (installation package). The dynamic binding section acquires the information of the abnormal class defined by the program package; the information of the abnormal class is represented by a final clause, the abnormal class defined by the package and the abnormal class of the imported package, the abnormal class defined by the package is represented by the offset of the dynamic binding segment, and the abnormal class of the imported package is represented by a constant pool index mode. The two types of representation methods are distinguished by the most significant bit, and the finally exception handler is represented by a special value of 0 xFFF.
The exception class in the import package is referenced by the constant pool subsection 204. The constant pool subsections acquire the reference information of the abnormal class in the program package; the constant pool subsegment references an exception class imported into the package.
The code constant pool index position subsection 204 is used when the constant pool index is parsed when the package is installed. And the code constant pool index position sub-segment is used for analyzing the constant pool index when the program package is installed.
The code segment 202 is a code segment, which at least includes a method header and a corresponding bytecode, and forms all methods of all classes in the package into one code segment; the byte codes at least comprise normally executed byte codes. The method for acquiring all classes in the program package forms the code segment, and the method comprises a method header, a corresponding byte code and an optional exception handling information table.
In one embodiment, the bytecodes of the method include a normally executed bytecode and optionally a bytecode of an exception handler, and each method includes: a method header and an optional exception handling information table of the method, and a bytecode of the method; the method header includes a tag field that indicates whether an exception handling information table is included in the method.
Optionally, the code segment may further include an exception handling information table, when the code segment includes the exception handling information table, the bytecode includes, in addition to the normally executed bytecode, a bytecode of the exception handler, and the method header further includes a flag field, where a bit in the flag field is used to indicate whether the method has the exception handling information table.
In one embodiment, the exception handling information table includes a field indicating the number of exception entry entries and an exception handling information table. The exception handling information table comprises a start address, a length, exception class information and an offset address of the exception handler. Specifically, the exception handling information table includes a field indicating the number of the exception entry items and an exception handling information table, where a plurality of exception handling information entries 206 are included in the exception handling information table, and the number of the exception entry items can be determined by obtaining the exception handling information entries 206, so as to obtain a specific numerical value of the field indicating the number of the exception entry items. The exception handling information table 205 includes a start address 210, a length 211, exception class information 212, and an offset address 213 of the exception handler. The exception class information may be a class component offset or a constant pool index, distinguished by the most significant bits.
For example, the specific code included in the code segment, which contains the specific layout of the components, is as follows, wherein the instruction set of the method is based on the register instruction type, other similar content layouts, or the operand stack-based instruction types, and so on, are all covered by the present invention.
Figure BDA0003654035070000121
Figure BDA0003654035070000131
The tag field may contain a plurality of flags (flags), and specific information of the tag field is shown in table 2.
TABLE 2
Sign (C) Value of
ACC_EXTENDED 0x8
ACC_ABSTRACT 0x4
ACC_NATIVE 0x2
ACC_EXCEPTION 0x1
Specifically, when the flag ACC _ EXCEPTION is 1, it indicates that the method has an EXCEPTION handling information table, and the subsequent handlerCount +8 × handlerCount byte indicates the EXCEPTION handling information table, where the number of entry entries in the handlerCount EXCEPTION handling information table is equal.
The code segment corresponding to the exception handling information table is as follows:
Figure BDA0003654035070000132
Figure BDA0003654035070000141
wherein startOffset represents the start position of the try block, and the value is the offset of the instruction relative to the method component; workLength represents the length of the try block, in bytes; handlerOffset represents the location of the exception handling instruction, the offset of the instruction at the Method component; the cathtytypeindexofset (b15b14 … … b1b0) represents the constant pool index of the exception class or the offset of the class component, and if the cathtytypeindex value is 0xFFFF, this exception handling block is the finally module. Otherwise this exception handling block captures exceptions of the type specified by the catchTypeIndex. The most significant bit (b15) is 1, which represents the internal class defined by the present package, the rest of the bits represent the offset of the class in the class component, the most significant bit (b15) is 0, which represents the class defined by the import package, and the rest of the bits represent the index of the constant pool component, which points to the specified exception class of the specified import package. The internal class defined by the package adopts internal representation (not by constant pool index reference), reduces the sizes of the constant pool subsegment and the code constant pool index position subsegment, and reduces the operation processing of constant pool index analysis during package installation, thereby improving the installation speed of the package.
S120, executing each method in the initial file by executing a method calling instruction, and judging whether each method has an exception handler or not in the executing process; when the method is called, the created method frame stack comprises the position information of the exception handling information table of the method.
Executing each method in the initial file by executing a method calling instruction, and judging whether each method has an exception handler or not in the executing process; when the method is called, the created method frame stack comprises the position information of the exception handling information table of the method, wherein the special value 0xFFF represents that the method has no exception handling program.
In the embodiment of the present application, the implementation of the virtual machine exception handling function is described below in the following with a virtual machine implementation based on a register instruction type, and an instruction virtual machine implementation based on an operand stack is similar.
The specific implementation process of the virtual machine for the method call is shown in fig. 3, and fig. 3 illustrates a call stack of the method of the present invention and a schematic structure of each stack frame content. The stack frame 310 of the virtual machine 300 executing the register instructions consists of two parts: a register area 311 and a frame stack data area 312. The register area 311 is organized into a register group with word length as a unit, and comprises parameters and local variables of corresponding methods, a compiler puts the parameters into the local variables according to a declaration sequence, and the parameter this is implicitly added for any example method and is used for representing the object of the method; the frame stack data area 312 stores calling method state information, a return location 322 (supporting return of a method), a method exception handling table location 321, firewall information of an application, and the like.
The exception handling table location 321 of a method may be represented by a 2-byte unsigned short type integer, which may have a value of 0xFFFF if the method has no exception handler, and an offset in the method component for the exception handling table of the method if the method has an exception handler.
When the method calling instruction is executed, the method header information of the specified method is analyzed, and because the method header comprises the mark field, whether the method has the exception handler or not can be easily obtained by analyzing the mark field.
Step S120 is followed by: if the method has no exception handler, adding a special value for identifying no exception to a field corresponding to the frame stack; the special value is not the same as the valid method field location information.
If the exception handling information table does not exist, adding a special value for identifying no exception to a field corresponding to the frame stack; the special value can be clearly distinguished from valid method field location information, such as the special value 0xFFFF in the embodiment.
S130, if the method has an exception handler, adding the position information of the exception handling information table in the method to a field corresponding to the stack frame.
If the method has an exception handler, the position of the exception handling information table can be directly obtained, and the position information is added into the corresponding field of the stack frame. If the method has no exception handler, that is, there is no exception information to be processed in the method, the normally executable bytecode in the method is directly executed.
S140, when the virtual machine throws the exception, acquiring the position of an exception handling information table according to the stack frame of the current method and handling the exception in the method.
When the exception is thrown out, the virtual machine acquires the position of the exception handling information table through the stack frame of the current method, and handles the exception according to the position of the exception handling information table.
In an embodiment, as shown in fig. 4, step S140 includes sub-steps S141, S142 and S143.
S141, when the exception is thrown out, acquiring the position of an exception handling information table according to the stack frame of the current method; s142, judging whether the method has an abnormal processing information table or not; s143, reading the exception handling information table to carry out matching processing of the exception handler so as to handle the exception in the method.
The virtual machine performs matching operation of the exception handler according to the location of the exception handling information table, and the matching operation is described in detail in the above description and is not described in detail here.
The virtual machine involves system overhead in executing the method, and the system overhead comprises storage performance and execution performance. For the storage performance, if the methods involve exception handling, each method increases 1 byte for representing the number of entries of the exception handling information table, and the size of the executable file is influenced very little; for execution performance, the created Frame needs to be increased by 2 bytes each time the method is called, and considering that the nesting depth of the method call of the application program written by the resource-limited device is limited, the RAM requirement of the stack is increased to an acceptable extent.
In addition, the embodiment of the present invention is described by using Java programming language, and other similar object-oriented programming languages also belong to the scope covered by the present invention if the exception handling implementation mechanism of the present invention can be applied.
The beneficial effects of the technical method in the invention are mainly reflected in two aspects.
Firstly, for the installation performance, the invention adopts a class internal representation method for the exception class defined by the package in the exception handling information table by re-laying the content of the method component, creates a stack frame when the virtual machine executes a method calling instruction, increases the position information of the exception handling information table of the method by the stack frame, does not need to maintain a global exception handling information table link list when the virtual machine realizes the exception handling function, and can improve the installation and deletion speed of the package.
Secondly, for the execution performance, when a method calling instruction is executed, analyzing method header information of a specified method, easily acquiring whether the method has an exception handler, if so, directly acquiring the position of an exception handling information table, and adding the position information into a corresponding field of a stack frame; because the number of the entries of the exception handling information table of each method is less, the entries of the exception handling information table can be matched quickly; the special value 0xFFFF represents that the method has no exception handler, the virtual machine can pop up a stack frame of the method, and the calling method continues to handle exceptions; the optimization mechanism can improve the matching speed of the exception handler, thereby improving the execution performance of the application.
In this embodiment, by re-laying out the contents of each component in the method, a class internal representation method is adopted for the exception class defined by the program package in the exception handling information table, when the virtual machine executes the method and calls an instruction, the newly-built stack frame optimizes the exception handling process by increasing the position information of the exception handling information table of the method, and when the virtual machine realizes the exception handling function, a global exception handling information table link list does not need to be maintained, so that the installation and deletion speed of the program package can be increased, the processing efficiency of the virtual machine when the virtual machine handles the exception can be increased, and the execution performance of the virtual machine on the program package can be improved.
As shown in fig. 5, an optimization system for exception handling execution, where the system is configured in a virtual machine of a user terminal, where the virtual machine executes an optimization method for exception handling execution to optimize an exception handling process of a virtual machine execution program installation package, a stack frame of the virtual machine, where a register instruction is executed, is composed of a register area and a frame stack data area, and a stack frame of the virtual machine, where an operand stack instruction is executed, is composed of a local variable area, an operand stack and a frame stack data area, where the frame stack data area is used to store state information of a called method and an exception handling information table location of the method, and where the system is configured to execute any one embodiment of the foregoing optimization method for exception handling execution, the system includes the following devices: initial file acquisition means 110, abnormality processor determination means 120, position information addition means 130, and abnormality processing means 140.
An initial file obtaining device 110, configured to convert, by using a preset conversion program, a plurality of class files compiled in a package into an initial file that can be installed and executed on a resource-constrained device; the program package is a program installation package to be installed; the initial file comprises sections and sub-sections related to the abnormity, wherein the sections and sub-sections related to the abnormity comprise a dynamic binding section, a code section, a constant pool sub-section and a code constant pool index position sub-section; and the abnormal class defined by the program installation package is represented by the offset of the dynamic binding segment.
In a specific embodiment, the dynamic binding segment acquires information of an abnormal class defined by the program package; the information of the abnormal class is represented as a final clause, the abnormal class defined by the package and the abnormal class of the import package, and the abnormal class of the import package is represented in a constant pool index mode; the constant pool subsections acquire the reference information of the abnormal class in the program package; the constant pool sub-segment refers to an exception class imported into the program package; the code constant pool index position sub-segment is used for analyzing the constant pool index when the program package is installed; the method for acquiring all classes in the program package forms the code segment, and each method comprises a method header, a corresponding byte code and an optional exception handling information table.
In a specific embodiment, the bytecodes of the method include a normally executed bytecode and, optionally, a bytecode of an exception handler, and each method includes: a method header and an optional exception handling information table of the method, and a bytecode of the method; the method header includes a tag field that indicates whether an exception handling information table is included in the method.
An exception handler determining means 120, configured to execute each method in the initial file by executing a method call instruction, and determine whether there is an exception handler for each method during execution; when the method is called, the created method stack frame comprises the position information of the exception handling information table of the method.
A location information adding device 130, configured to add, if there is an exception handler in the method, location information of an exception handling information table in the method to a field corresponding to the stack frame.
And the exception handling device 140 is configured to, when the virtual machine throws an exception, obtain a position of the exception handling information table according to the stack frame of the current method, and handle the exception in the method.
In one embodiment, the exception handling information table includes a field indicating the number of exception entry entries and an exception handling information table, and the exception handling apparatus 140 includes: the position acquisition unit is used for acquiring the position of an exception handling information table according to the stack frame of the current method when the exception is thrown; wherein the exception handling information table comprises a start address, a length, exception class information and an offset address of an exception handler; the judging unit is used for judging whether the method has an abnormal processing information table or not; and the matching processing unit is used for reading the exception handling information table to perform matching processing of the exception handler so as to process the exception in the method.
In a specific embodiment, the system further comprises: a special value adding device, configured to add, if the method has no exception handler, a special value identifying that there is no exception to a field corresponding to the frame stack; the special value is not the same as the valid method field location information.
In this embodiment, by re-laying out the contents of each component in the method, a class internal representation method is adopted for the exception class defined by the program package in the exception handling information table, when the virtual machine executes the method and calls an instruction, the newly-built stack frame optimizes the exception handling process by increasing the position information of the exception handling information table of the method, and when the virtual machine realizes the exception handling function, a global exception handling information table link list does not need to be maintained, so that the installation and deletion speed of the program package can be increased, the processing efficiency of the virtual machine when the virtual machine handles the exception can be increased, and the execution performance of the virtual machine on the program package can be improved.
It will be appreciated by those skilled in the art that the method and system of the present invention is not limited to the embodiments described in the detailed description, and that the foregoing detailed description is for the purpose of illustrating the invention and is not to be taken in a limiting sense. Other embodiments will be apparent to those skilled in the art from the following detailed description, which is intended to cover all modifications, equivalents, and alternatives falling within the spirit and scope of the invention as defined by the appended claims.

Claims (10)

1. An optimization method for exception handling execution is applied to a virtual machine, and is used for optimizing an exception handling process of a virtual machine executing a program installation package, a stack frame of the virtual machine executing a register instruction consists of a register area and a frame stack data area, a stack frame of the virtual machine executing an operand stack instruction consists of a local variable area, an operand stack and a frame stack data area, and the frame stack data area is used for storing state information of a called method and an exception handling information table position of the method, and the method comprises the following steps of:
converting a plurality of class files compiled in the program package into an initial file which can be installed and executed on a resource-limited device through a preset conversion program; the program package is a program installation package to be installed; the initial file comprises sections and sub-sections related to the abnormity, wherein the sections and sub-sections related to the abnormity comprise a dynamic binding section, a code section, a constant pool sub-section and a code constant pool index position sub-section;
executing each method in the initial file by executing a method calling instruction, and judging whether each method has an exception handler or not in the executing process; when the method is called, the created method stack frame comprises the position information of the exception handling information table of the method;
if the method has an exception handler, adding the position information of an exception handling information table in the method to a field corresponding to the stack frame;
and when the virtual machine throws the exception, acquiring exception handling information according to the stack frame of the current method, if the exception handling information exists, reading an exception handling information table according to the position of the acquired exception handling information table, and handling the exception in the method.
2. A method of optimizing execution of exception handling as claimed in claim 1 wherein;
the dynamic binding section acquires the information of the exception handling class defined by the program package; the information of the abnormal class is represented by a final clause, the abnormal class defined by the package and the abnormal class of the imported package, the abnormal class defined by the package is represented by the offset of a dynamic binding segment, and the abnormal class of the imported package is represented by a constant pool index mode;
the constant pool subsections acquire the reference information of the abnormal class in the program package; the constant pool sub-segment refers to an exception class imported into the program package;
the code constant pool index position sub-segment is used for analyzing the constant pool index when the program package is installed;
the method for acquiring all classes in the program package forms the code segment, and each method comprises a method header, an optional exception handling information table and a corresponding byte code.
3. A method of optimising the execution of exception handling as claimed in claim 2 wherein the method includes within the byte code thereof normally executed byte code and optionally exception handler byte code, each method including: a method header and an optional exception handling information table of the method, and a bytecode of the method;
the method header includes a tag field that indicates whether an exception handling information table is included in the method.
4. A method as claimed in claim 3, wherein said exception handling information table includes a field indicating the number of exception entry entries and an exception handling information table, and said obtaining the location of the exception handling information table according to the stack frame of the current method and handling the exception in the method comprises:
when the exception is thrown out, the position of an exception handling information table is obtained according to the stack frame of the current method; wherein the exception handling information table comprises a start address, a length, exception class information and an offset address of an exception handler;
judging whether the method has an abnormal processing information table or not;
and reading the exception handling information table to perform matching processing of the exception handler so as to process the exception in the method.
5. The method for optimizing execution of exception handling according to claim 1, wherein said executing a method call instruction to execute each method in said initial file and determining whether each method has an exception handler during execution further comprises:
if the method has no exception handler, adding a special value for identifying no exception to a field corresponding to the frame stack; the special value is not the same as the valid method field location information.
6. An optimization system for exception handling execution, the system being configured in a virtual machine, the virtual machine executing an optimization method for exception handling execution to optimize an exception handling process of a virtual machine executing a program installation package, a stack frame of the virtual machine executing a register instruction being composed of a register area and a frame stack data area, a stack frame of the virtual machine executing an operand stack instruction being composed of a local variable area, an operand stack and a frame stack data area, the frame stack data area being used for storing state information of a called method and an exception handling information table position of the method, the system comprising the following devices:
an initial file acquisition device, configured to convert, through a preset conversion program, a plurality of class files compiled in a package into an initial file that can be installed and executed in a resource-constrained device; the program package is a program installation package to be installed; the initial file comprises sections and sub-sections related to the abnormity, wherein the sections and sub-sections related to the abnormity comprise a dynamic binding section, a code section, a constant pool sub-section and a code constant pool index position sub-section;
the exception handler judging device is used for executing each method in the initial file by executing the method calling instruction and judging whether each method has an exception handler or not in the executing process; when the method is called, the created method stack frame comprises the position information of the exception handling information table of the method;
the position information adding device is used for adding the position information of the exception handling information table in the method to a field corresponding to the stack frame if the method has an exception handler;
and the exception handling device is used for acquiring the position of an exception handling information table according to the stack frame of the current method and handling the exception in the method when the virtual machine throws the exception.
7. A system for optimizing execution of exception handling as recited in claim 6;
the dynamic binding section acquires the information of the abnormal class defined by the program package; the information of the exception class is represented by a final clause, the exception class defined by the package and the exception class of the import package, the exception class defined by the program installation package is represented by the offset of a dynamic binding segment, and the exception class of the import package is represented by a constant pool index;
the constant pool subsections acquire the reference information of the abnormal class in the program package; the constant pool sub-segment refers to an exception class imported into the program package;
the code constant pool index position sub-segment is used for analyzing the constant pool index when the program package is installed;
the method for acquiring all classes in the program package forms the code segment, and each method comprises a method header, a corresponding byte code and an optional exception handling information table.
8. The system for optimizing execution of exception handling as claimed in claim 7 wherein said method bytecode includes normally executing bytecode and optionally bytecode for exception handlers, each method comprising: a method header and an optional exception handling information table of the method, and a bytecode of the method;
the method header includes a tag field that indicates whether an exception handling information table is included in the method.
9. A system for optimizing execution of exception handling as claimed in claim 8, wherein said exception handling information table includes a field indicating the number of exception entry entries and an exception handling information table, said exception handling means comprising means for:
the position acquisition unit is used for acquiring the position of an exception handling information table according to the stack frame of the current method when the exception is thrown; wherein the exception handling information table comprises a start address, a length, exception class information and an offset address of an exception handler;
the judging unit is used for judging whether the method has an abnormal processing information table or not;
and the matching processing unit is used for reading the exception handling information table to perform matching processing of the exception handler so as to process the exception in the method.
10. The system for optimizing execution of exception handling of claim 6, wherein said system further comprises:
a special value adding device, configured to add a special value identifying no exception to a field corresponding to the frame stack if the method has no exception handler; the special value is not the same as valid method field location information.
CN202210549368.8A 2022-05-20 2022-05-20 Optimization method and system for exception handling execution Pending CN114968282A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210549368.8A CN114968282A (en) 2022-05-20 2022-05-20 Optimization method and system for exception handling execution

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210549368.8A CN114968282A (en) 2022-05-20 2022-05-20 Optimization method and system for exception handling execution

Publications (1)

Publication Number Publication Date
CN114968282A true CN114968282A (en) 2022-08-30

Family

ID=82985993

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210549368.8A Pending CN114968282A (en) 2022-05-20 2022-05-20 Optimization method and system for exception handling execution

Country Status (1)

Country Link
CN (1) CN114968282A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117033064A (en) * 2023-10-10 2023-11-10 北京中电华大电子设计有限责任公司 Method and device for processing code exception
CN117056906A (en) * 2023-07-27 2023-11-14 北京握奇数据股份有限公司 Instruction access control method, system, storage medium and equipment

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1359491A (en) * 1999-02-02 2002-07-17 太阳微系统公司 Object-oriented instruction set for resource-constrained devices
CN1534465A (en) * 1999-02-02 2004-10-06 ̫��΢ϵͳ���޹�˾ Zero expense abnormal treatment
CN102819435A (en) * 2012-07-26 2012-12-12 复旦大学 Position independent code generation method of Java virtual machine
DE102018127317B3 (en) * 2018-11-01 2020-04-09 Infineon Technologies Ag METHOD AND DEVICES FOR COMPUTER-IMPLEMENTED GENERATION OF AN EXECUTABLE PROGRAM CODE AND FOR EXECUTING AN EXECUTABLE PROGRAM CODE

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1359491A (en) * 1999-02-02 2002-07-17 太阳微系统公司 Object-oriented instruction set for resource-constrained devices
CN1534465A (en) * 1999-02-02 2004-10-06 ̫��΢ϵͳ���޹�˾ Zero expense abnormal treatment
CN102819435A (en) * 2012-07-26 2012-12-12 复旦大学 Position independent code generation method of Java virtual machine
DE102018127317B3 (en) * 2018-11-01 2020-04-09 Infineon Technologies Ag METHOD AND DEVICES FOR COMPUTER-IMPLEMENTED GENERATION OF AN EXECUTABLE PROGRAM CODE AND FOR EXECUTING AN EXECUTABLE PROGRAM CODE

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117056906A (en) * 2023-07-27 2023-11-14 北京握奇数据股份有限公司 Instruction access control method, system, storage medium and equipment
CN117056906B (en) * 2023-07-27 2024-03-29 北京握奇数据股份有限公司 Instruction access control method, system, storage medium and equipment
CN117033064A (en) * 2023-10-10 2023-11-10 北京中电华大电子设计有限责任公司 Method and device for processing code exception

Similar Documents

Publication Publication Date Title
KR100713738B1 (en) Zero overhead exception handling
US5748963A (en) Adaptive binding
CN114968282A (en) Optimization method and system for exception handling execution
US6446254B1 (en) Packaging memory image files
EP0528028B1 (en) Automatic flowgraph generation for program analysis and translation
US6637025B1 (en) Dynamic selection/definition of which class/methods should or should not be jit'ed using information stored in a jar file
CN111381938B (en) Method and system for executing intelligent contracts in block chain based on basic instruction set
US6363522B1 (en) Method and apparatus for handling exceptions as normal control flow
CN1690957A (en) A method and system of enforcing a security policy via a security virtual machine
US8042103B2 (en) Pre-translated files used in a virtual machine
US20040098712A1 (en) System and apparatus for dynamically upgrading concentrated executable computer software code
US6804681B2 (en) Identifying and tracking object references in a java programming environment
CN113176926A (en) API dynamic monitoring method and system based on virtual machine introspection technology
US6732355B1 (en) Method and device for generating registration data at compilation to enable trace of stack
US20060277552A1 (en) Facilitating handling of exceptions in a program implementing a M-on-N threading model
US20060282820A1 (en) COBOL syntax for native XML file parsing and file generation
US7065747B2 (en) Identifying references to objects during bytecode verification
CN114625646A (en) Method and device for detecting system memory boundary crossing
US6934726B2 (en) Storing and retrieving of field descriptors in Java computing environments
US9342319B1 (en) Accelerated class check
CN113296834B (en) Android closed source service type information extraction method based on reverse engineering
CN115037735A (en) Short name based linking method and system
CN117667738A (en) Method and device for processing WebAssembly module exception and electronic equipment
Morrison et al. Processbase abstract machine manual (version 2.0. 6)

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination