CN116450402A - Program flow monitoring method, compiling device, processor and computer equipment - Google Patents
Program flow monitoring method, compiling device, processor and computer equipment Download PDFInfo
- Publication number
- CN116450402A CN116450402A CN202310708268.XA CN202310708268A CN116450402A CN 116450402 A CN116450402 A CN 116450402A CN 202310708268 A CN202310708268 A CN 202310708268A CN 116450402 A CN116450402 A CN 116450402A
- Authority
- CN
- China
- Prior art keywords
- basic block
- signature
- instruction
- control flow
- verification
- 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
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/07—Responding to the occurrence of a fault, e.g. fault tolerance
- G06F11/08—Error detection or correction by redundancy in data representation, e.g. by using checking codes
- G06F11/10—Adding special bits or symbols to the coded information, e.g. parity check, casting out 9's or 11's
- G06F11/1004—Adding special bits or symbols to the coded information, e.g. parity check, casting out 9's or 11's to protect a block of data words, e.g. CRC or checksum
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/30—Monitoring
- G06F11/3003—Monitoring arrangements specially adapted to the computing system or computing system component being monitored
- G06F11/3024—Monitoring arrangements specially adapted to the computing system or computing system component being monitored where the computing system component is a central processing unit [CPU]
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements 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/30—Arrangements for executing machine instructions, e.g. instruction decode
- G06F9/30003—Arrangements for executing specific machine instructions
- G06F9/3005—Arrangements for executing specific machine instructions to perform operations for flow control
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements 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/30—Arrangements for executing machine instructions, e.g. instruction decode
- G06F9/38—Concurrent instruction execution, e.g. pipeline or look ahead
- G06F9/3818—Decoding for concurrent execution
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Software Systems (AREA)
- Quality & Reliability (AREA)
- Computing Systems (AREA)
- Computer Security & Cryptography (AREA)
- Mathematical Physics (AREA)
- Debugging And Monitoring (AREA)
Abstract
Description
技术领域technical field
本发明涉及程序流监控技术领域,尤其涉及一种程序流监控方法、编译方法、装置、处理器及计算机设备。The invention relates to the technical field of program flow monitoring, in particular to a program flow monitoring method, compiling method, device, processor and computer equipment.
背景技术Background technique
CPU(Central Processing Unit,中央处理单元)作为芯片上的核心单元,在各领域都有广泛的应用。通常,CPU按照指令流的顺序,对指令流序列依次进行取指、译码、执行操作完成对指令的完整执行。如果程序流执行的顺序被破坏,会产生程序逻辑错误,甚至引起整个系统异常。因此,有必要对程序流程进行监控,以确保整个系统程序安全、可靠地运行。As a core unit on a chip, a CPU (Central Processing Unit, central processing unit) is widely used in various fields. Usually, the CPU sequentially fetches, decodes, and executes the instruction stream sequence in accordance with the order of the instruction stream to complete the complete execution of the instruction. If the sequence of program flow execution is broken, program logic errors will occur, and even the entire system will be abnormal. Therefore, it is necessary to monitor the program flow to ensure the safe and reliable operation of the entire system program.
相关技术中,通常采用CPU内部看门狗或外部系统看门狗模块来对程序流进行监控。然而,基于看门狗的程序流监控方法能够检测程序流在一段时间内是否执行完成,不能检测程序流是否按照设计逻辑有效地执行。In related technologies, the program flow is usually monitored by an internal CPU watchdog or an external system watchdog module. However, the watchdog-based program flow monitoring method can detect whether the program flow is executed within a period of time, but cannot detect whether the program flow is effectively executed according to the design logic.
发明内容Contents of the invention
本发明旨在至少在一定程度上解决相关技术中的技术问题之一。为此,本发明的第一个目的在于提出一种程序流监控方法,通过软硬件结合的程序流监控方法,在数据流和控制流上对程序流进行双重监控,可以有效检测由于电磁干扰、线路串扰等因素影响而导致的程序执行行为与预期不符的问题。The present invention aims to solve one of the technical problems in the related art at least to a certain extent. For this reason, the first purpose of the present invention is to propose a program flow monitoring method, through the program flow monitoring method combining software and hardware, the program flow is double monitored on the data flow and control flow, which can effectively detect due to electromagnetic interference, The problem that the program execution behavior is not as expected due to the influence of factors such as line crosstalk.
本发明的第二个目的在于提出一种程序代码编译方法。The second object of the present invention is to provide a program code compiling method.
本发明的第三个目的在于提出一种处理器。A third object of the present invention is to propose a processor.
本发明的第四个目的在于提出一种程序流监控装置。The fourth object of the present invention is to provide a program flow monitoring device.
本发明的第五个目的在于提出一种程序代码编译装置。The fifth object of the present invention is to provide a program code compiling device.
本发明的第六个目的在于提出一种计算机设备。A sixth object of the present invention is to propose a computer device.
本发明的第七个目的在于提出一种计算机可读存储介质。The seventh object of the present invention is to provide a computer-readable storage medium.
为达到上述目的,本发明第一方面实施方式提出了一种程序流监控方法,所述程序流对应有控制流和数据流;所述控制流包括若干基本块;基本块头部在编译阶段插入有控制流校验指令,以及基本块尾部在链接阶段插入有数据流校验指令;其中,所述数据流校验指令是在所述链接阶段对所述编译阶段中插入所述基本块尾部的占位指令进行替换而插入的;所述方法包括:在所述程序流运行到当前基本块的情况下,执行所述当前基本块内的所述控制流校验指令以校验所述控制流的正确性;在所述当前基本块运行至所述数据流校验指令的情况下,确定所述当前基本块的所有指令的CRC签名值,以校验所述数据流的完整性。In order to achieve the above-mentioned purpose, the implementation mode of the first aspect of the present invention proposes a program flow monitoring method, the program flow corresponds to a control flow and a data flow; the control flow includes several basic blocks; the basic block header is inserted in the compilation stage There is a control flow check instruction, and a data flow check instruction is inserted into the tail of the basic block in the linking stage; wherein, the data flow checking instruction is inserted into the tail of the basic block in the compilation stage in the linking stage The placeholder instruction is replaced and inserted; the method includes: when the program flow runs to the current basic block, executing the control flow verification instruction in the current basic block to verify the control flow correctness; in the case that the current basic block runs to the data flow verification instruction, determine the CRC signature values of all instructions of the current basic block to verify the integrity of the data flow.
根据本发明的一个实施方式,所述基本块分配有静态签名;所述若干基本块包括当前基本块、跳转至所述当前基本块的前驱基本块;所述执行所述当前基本块内的控制流校验指令以校验所述控制流的正确性,包括:获取所述前驱基本块的补偿签名和所述前驱基本块的运行时签名;其中,所述补偿签名是基于所述前驱基本块的静态签名与前驱基准基本块的静态签名的异或计算结果确定的;所述前驱基准基本块为所述前驱基本块所处的控制级别上的基准基本块;确定所述当前基本块的静态签名和签名差;其中,所述签名差是基于所述前驱基准基本块的静态签名与所述当前基本块的静态签名进行异或计算的结果确定的;若所述前驱基本块的运行时签名、所述当前基本块的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述当前基本块的静态签名,所述当前基本块通过控制流校验。According to an embodiment of the present invention, the basic block is assigned with a static signature; the several basic blocks include the current basic block and the predecessor basic block that jumps to the current basic block; the execution of the current basic block in the current basic block A control flow verification instruction to verify the correctness of the control flow, including: obtaining the compensation signature of the predecessor basic block and the runtime signature of the predecessor basic block; wherein, the compensation signature is based on the predecessor basic block determined by the XOR calculation result of the static signature of the block and the static signature of the predecessor basic block; the predecessor basic block is a reference basic block at the control level of the predecessor basic block; Static signature and signature difference; wherein, the signature difference is determined based on the result of XOR calculation between the static signature of the predecessor basic block and the static signature of the current basic block; if the runtime of the predecessor basic block The XOR calculation result of the signature, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static signature of the current basic block, and the current basic block passes the control flow verification.
根据本发明的一个实施方式,所述前驱基本块的数量为1时,所述前驱基本块为所述前驱基准基本块,所述补偿签名为0。According to an embodiment of the present invention, when the number of the predecessor basic block is 1, the predecessor basic block is the predecessor reference basic block, and the compensation signature is 0.
根据本发明的一个实施方式,所述前驱基本块的数量大于1时,所述前驱基准基本块是基于所述前驱基本块所处的控制级别上的基本块的静态签名确定的。According to an embodiment of the present invention, when the number of the predecessor basic block is greater than 1, the predecessor reference basic block is determined based on the static signature of the basic block at the control level of the predecessor basic block.
根据本发明的一个实施方式,所述程序流监控方法还包括:若所述前驱基本块的运行时签名、所述当前基本块的签名差、所述前驱基本块的补偿签名的异或计算结果不等于所述当前基本块的静态签名,所述当前基本块不通过控制流校验,以触发异常。According to an embodiment of the present invention, the program flow monitoring method further includes: if the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the XOR calculation result of the compensation signature of the predecessor basic block Not equal to the static signature of the current basic block, the current basic block does not pass the control flow check to trigger an exception.
根据本发明的一个实施方式,在若所述前驱基本块的运行时签名、所述当前基本块的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述当前基本块的静态签名,则所述当前基本块通过控制流校验之前,所述程序流监控方法还包括:基于所述签名差和所述当前基本块的静态签名,在所述当前基本块的头部插入有控制流校验指令;所述若所述前驱基本块的运行时签名、所述当前基本块的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述当前基本块的静态签名,则所述当前基本块通过控制流校验,包括:若所述前驱基本块的运行时签名、所述控制流校验指令中附带的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述控制流校验指令中附带的静态签名,所述当前基本块通过控制流校验。According to an embodiment of the present invention, if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static value of the current basic block signature, before the current basic block passes the control flow check, the program flow monitoring method further includes: based on the signature difference and the static signature of the current basic block, inserting Control flow verification instruction; if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static signature of the current basic block , then the current basic block passes the control flow verification, including: if the runtime signature of the predecessor basic block, the signature difference attached to the control flow verification instruction, and the XOR of the compensation signature of the predecessor basic block The calculation result is equal to the static signature attached to the control flow verification instruction, and the current basic block passes the control flow verification.
根据本发明的一个实施方式,所述数据流校验指令中附带有静态校验签名,所述静态校验签名是基于编译器编译链接时所述当前基本块内所有指令的CRC签名值确定的;所述确定所述当前基本块的所有指令的CRC签名值,以校验所述数据流的完整性,包括:计算所述当前基本块内所有指令的CRC签名值,得到动态校验签名;若所述当前基本块完成执行时的动态校验签名与所述静态校验签名一致,所述当前基本块通过数据流校验。According to an embodiment of the present invention, the data flow verification instruction is accompanied by a static verification signature, and the static verification signature is determined based on the CRC signature values of all instructions in the current basic block when the compiler compiles and links ; The determining the CRC signature values of all instructions of the current basic block to verify the integrity of the data stream includes: calculating the CRC signature values of all instructions in the current basic block to obtain a dynamic verification signature; If the dynamic verification signature when the current basic block is executed is consistent with the static verification signature, the current basic block passes the data stream verification.
根据本发明的一个实施方式,所述确定所述当前基本块的所有指令的CRC签名值,以校验所述数据流的完整性,还包括:若所述当前基本块完成执行时的动态校验签名与所述静态校验签名不一致,所述当前基本块不通过数据流校验以触发异常。According to an embodiment of the present invention, the determining the CRC signature values of all instructions of the current basic block to verify the integrity of the data stream further includes: if the current basic block completes execution, the dynamic verification The verification signature is inconsistent with the static verification signature, and the current basic block does not pass the data stream verification to trigger an exception.
根据本发明的一个实施方式,所述控制流包括函数控制流,所述函数控制流包括入口基本块和出口基本块;所述入口基本块在编译阶段插入有签名信息保存指令,所述出口基本块在编译阶段插入有签名信息恢复指令;所述程序流监控方法还包括:在函数调用时执行所述签名信息保存指令,以支持运行时签名和补偿签名的压栈操作;在调用的函数返回时执行所述签名信息恢复指令,以支持保存的运行时签名和补偿签名的弹栈操作。According to an embodiment of the present invention, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; A signature information recovery instruction is inserted into the block at the compilation stage; the program flow monitoring method also includes: executing the signature information preservation instruction when the function is called, so as to support the push operation of the runtime signature and the compensation signature; when the called function returns Executing the signature information restoration instruction at a time, so as to support the pop-up operation of the saved runtime signature and the compensation signature.
根据本发明的一个实施方式,所述程序流监控方法还包括:增加全局监控的编译器参数,以对应用程序的全部运行过程进行监控。According to an embodiment of the present invention, the program flow monitoring method further includes: adding global monitoring compiler parameters to monitor the entire running process of the application program.
根据本发明的一个实施方式,所述程序流监控方法还包括:针对源代码中的指定函数,在所述指定函数的指定位置增加程序流检测标记以及编译器参数,以对所述指定函数的运行过程进行监控。According to an embodiment of the present invention, the program flow monitoring method further includes: for a specified function in the source code, adding a program flow detection flag and a compiler parameter at a specified position of the specified function, so as to monitor the specified function Monitor the running process.
根据本发明的一个实施方式,所述程序流监控方法还包括:在源代码中通过括号将需要监控的目标源码括起来;在所述目标源码的指定位置处增加制导语句,以对所述目标源码的运行过程进行监控。According to an embodiment of the present invention, the program flow monitoring method further includes: enclosing the target source code to be monitored through parentheses in the source code; adding a guidance statement at a specified position of the target source code to The running process of the source code is monitored.
为达到上述目的,本发明第二方面实施方式提出了一种程序代码编译方法,所述程序代码对应有程序控制流图,且所述程序控制流图中具有若干基本块;所述方法包括:在编译阶段,在基本块头部插入控制流校验指令,在基本块尾部插入占位指令;其中,所述控制流校验指令用于在所述程序代码对应的程序流运行到任意基本块时校验所述控制流的正确性;在链接阶段,将所述占位指令对应的指令码替换为数据流校验指令;其中,所述数据流校验指令用于在所述程序代码对应的程序流运行到任意基本块的数据流校验指令时,确定所述任意基本块的所有指令的CRC签名值,以校验所述数据流的完整性。In order to achieve the above-mentioned purpose, the implementation mode of the second aspect of the present invention proposes a program code compiling method, the program code corresponds to a program control flow graph, and the program control flow graph has several basic blocks; the method includes: In the compilation phase, insert control flow verification instructions at the head of the basic block, and insert placeholder instructions at the end of the basic block; wherein, the control flow verification instructions are used to run the program flow corresponding to the program code to any basic block When verifying the correctness of the control flow; in the linking stage, the instruction code corresponding to the placeholder instruction is replaced with a data flow verification instruction; wherein, the data flow verification instruction is used in the program code corresponding to When the program flow of the program runs to the data flow verification instruction of any basic block, determine the CRC signature value of all instructions of the arbitrary basic block, so as to verify the integrity of the data flow.
根据本发明的一个实施方式,所述控制流包括函数控制流,所述函数控制流包括入口基本块和出口基本块;所述程序代码编译方法还包括:在编译阶段,在所述入口基本块插入签名信息保存指令,以及,在所述出口基本块插入签名信息恢复指令;其中,所述签名信息保存指令用于在函数调用时的运行时签名和调整签名的压栈操作,所述签名信息恢复指令用于在调用的函数返回之前的运行时签名和调整签名的弹栈操作。According to an embodiment of the present invention, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; the program code compiling method further includes: Inserting a signature information saving instruction, and inserting a signature information restoration instruction in the exit basic block; wherein, the signature information saving instruction is used for the runtime signature and the push operation of adjusting the signature when the function is called, and the signature information The restore instruction is used to pop the runtime signature and adjust the signature before the called function returns.
为达到上述目的,本发明第三方面实施方式提出了一种处理器,所述处理器用于执行程序流,所述程序流对应有控制流和数据流;所述控制流包括若干基本块;所述处理器包括位于译码流水级的扩展译码电路、位于写回流水级的控制流校验电路和数据流校验电路;所述扩展译码电路,用于译码控制流校验指令和数据流校验指令;其中,所述控制流校验指令是在编译阶段插入基本块头部的,所述数据流校验指令是在链接阶段对所述编译阶段中插入基本块尾部的占位指令进行替换而插入的;所述控制流校验电路,用于在所述程序流运行到当前基本块的情况下,执行所述当前基本块内的控制流校验指令以校验所述控制流的正确性;所述数据流校验电路,用于在所述当前基本块运行至所述数据流校验指令的情况下,确定所述当前基本块的所有指令的CRC签名值,以校验所述数据流的完整性。In order to achieve the above-mentioned purpose, the implementation manner of the third aspect of the present invention proposes a processor, the processor is used to execute a program flow, and the program flow corresponds to a control flow and a data flow; the control flow includes several basic blocks; The processor includes an extended decoding circuit at the decoding pipeline level, a control flow verification circuit and a data flow verification circuit at the write-back pipeline level; the extended decoding circuit is used for decoding control flow verification instructions and A data flow verification instruction; wherein, the control flow verification instruction is inserted into the head of the basic block during the compilation phase, and the data flow verification instruction is a placeholder inserted into the tail of the basic block during the compilation phase during the linking phase Instructions are replaced and inserted; the control flow verification circuit is used to execute the control flow verification instructions in the current basic block to verify the control when the program flow runs to the current basic block The correctness of the flow; the data flow verification circuit is used to determine the CRC signature values of all instructions of the current basic block when the current basic block runs to the data flow verification instruction, so as to verify verify the integrity of the data stream.
根据本发明的一个实施方式,所述基本块分配有静态签名;所述若干基本块包括当前基本块、跳转至所述当前基本块的前驱基本块;所述处理器设置有运行时签名寄存器、补偿签名寄存器;所述运行时签名寄存器存储有所述前驱基本块的运行时签名,所述补偿签名寄存器存储有前驱基本块的补偿签名;其中,补偿签名是基于所述前驱基本块的静态签名与前驱基准基本块的静态签名的异或计算结果确定的;所述前驱基准基本块为所述前驱基本块所处的控制级别上的基准基本块;所述控制流校验电路,还用于确定所述当前基本块的静态签名和签名差,若所述前驱基本块的运行时签名、所述当前基本块的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述当前基本块的静态签名,所述当前基本块通过控制流校验;其中,所述签名差是基于所述前驱基准基本块的静态签名与所述当前基本块的静态签名进行异或计算的结果确定的。According to an embodiment of the present invention, the basic block is assigned a static signature; the several basic blocks include a current basic block and a predecessor basic block that jumps to the current basic block; the processor is provided with a runtime signature register . Compensation signature register; the runtime signature register stores the runtime signature of the predecessor basic block, and the compensation signature register stores the compensation signature of the predecessor basic block; wherein, the compensation signature is based on the static signature of the predecessor basic block The XOR calculation result of the signature and the static signature of the predecessor basic block is determined; the predecessor basic block is the reference basic block on the control level where the predecessor basic block is located; the control flow verification circuit also uses To determine the static signature and signature difference of the current basic block, if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the The static signature of the current basic block, the current basic block passing the control flow verification; wherein, the signature difference is the result of XOR calculation based on the static signature of the predecessor reference basic block and the static signature of the current basic block definite.
根据本发明的一个实施方式,所述控制流包括函数控制流,所述函数控制流包括入口基本块和出口基本块;所述入口基本块在编译阶段插入有签名信息保存指令,所述出口基本块在编译阶段插入有签名信息恢复指令;所述处理器设置有签名栈;所述签名栈用于在函数调用时执行所述签名信息保存指令,支持运行时签名和补偿签名的压栈操作;在调用的函数返回时执行所述签名信息恢复指令,支持保存的运行时签名和补偿签名的弹栈操作。According to an embodiment of the present invention, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; A signature information recovery instruction is inserted into the block at the compilation stage; the processor is provided with a signature stack; the signature stack is used to execute the signature information preservation instruction when the function is called, and supports the push operation of the runtime signature and the compensation signature; The signature information restoration instruction is executed when the called function returns, and the operation of popping the saved runtime signature and compensation signature is supported.
根据本发明的一个实施方式,所述数据流校验指令中附带有静态校验签名,所述静态校验签名是基于编译器编译链接时所述当前基本块内所有指令的CRC签名值确定的;所述处理器设置有CRC中间值寄存器;所述CRC中间值寄存器用于存储所述当前基本块运行至当前指令时所生成的中间CRC签名值;所述数据流校验电路,还用于计算所述当前基本块内所有指令的CRC签名值,得到动态校验签名;若所述当前基本块完成执行时的动态校验签名与所述静态校验签名一致,所述当前基本块通过数据流校验。According to an embodiment of the present invention, the data flow verification instruction is accompanied by a static verification signature, and the static verification signature is determined based on the CRC signature values of all instructions in the current basic block when the compiler compiles and links ; The processor is provided with a CRC intermediate value register; the CRC intermediate value register is used to store the intermediate CRC signature value generated when the current basic block runs to the current instruction; the data flow verification circuit is also used for Calculate the CRC signature values of all instructions in the current basic block to obtain a dynamic verification signature; if the dynamic verification signature when the current basic block completes execution is consistent with the static verification signature, the current basic block passes the data Stream check.
为达到上述目的,本发明第四方面实施方式提出了一种程序流监控装置,所述程序流对应有控制流和数据流;所述控制流包括若干基本块;基本块头部在编译阶段插入有控制流校验指令,以及基本块尾部在链接阶段插入有数据流校验指令;其中,所述数据流校验指令是在所述链接阶段对所述编译阶段中插入所述基本块尾部的占位指令进行替换而插入的;所述装置包括:控制流校验模块,用于在所述程序流运行到当前基本块的情况下,执行所述当前基本块内的所述控制流校验指令以校验所述控制流的正确性;数据流校验模块,用于在所述当前基本块运行至所述数据流校验指令的情况下,确定所述当前基本块的所有指令的CRC签名值,以校验所述数据流的完整性。In order to achieve the above object, the implementation mode of the fourth aspect of the present invention proposes a program flow monitoring device, the program flow corresponds to a control flow and a data flow; the control flow includes several basic blocks; the basic block header is inserted in the compilation stage There is a control flow check instruction, and a data flow check instruction is inserted into the tail of the basic block in the linking stage; wherein, the data flow checking instruction is inserted into the tail of the basic block in the compilation stage in the linking stage The placeholder instruction is replaced and inserted; the device includes: a control flow verification module, configured to perform the control flow verification in the current basic block when the program flow runs to the current basic block Instructions to verify the correctness of the control flow; a data flow verification module, used to determine the CRC of all instructions of the current basic block when the current basic block runs to the data flow verification instruction Signature value to verify the integrity of the data stream in question.
根据本发明的一个实施方式,所述基本块分配有静态签名;所述若干基本块包括当前基本块、跳转至所述当前基本块的前驱基本块;所述控制流校验模块,还用于获取所述前驱基本块的补偿签名和所述前驱基本块的运行时签名;其中,所述补偿签名是基于所述前驱基本块的静态签名与前驱基准基本块的静态签名的异或计算结果确定的;所述前驱基准基本块为所述前驱基本块所处的控制级别上的基准基本块;确定所述当前基本块的静态签名和签名差;其中,所述签名差是基于所述前驱基准基本块的静态签名与所述当前基本块的静态签名进行异或计算的结果确定的;若所述前驱基本块的运行时签名、所述当前基本块的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述当前基本块的静态签名,所述当前基本块通过控制流校验。According to an embodiment of the present invention, the basic block is assigned with a static signature; the several basic blocks include the current basic block and the predecessor basic block that jumps to the current basic block; the control flow verification module also uses Obtaining the compensation signature of the predecessor basic block and the runtime signature of the predecessor basic block; wherein, the compensation signature is based on the XOR calculation result of the static signature of the predecessor basic block and the static signature of the predecessor reference basic block Determined; the predecessor reference basic block is a reference basic block at the control level of the predecessor basic block; determine the static signature and signature difference of the current basic block; wherein, the signature difference is based on the predecessor The static signature of the reference basic block and the static signature of the current basic block are determined by the XOR calculation result; if the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the The XOR calculation result of the compensation signature is equal to the static signature of the current basic block, and the current basic block passes the control flow verification.
根据本发明的一个实施方式,所述控制流校验模块,还用于基于所述签名差和所述当前基本块的静态签名,在所述当前基本块的头部插入有控制流校验指令;若所述前驱基本块的运行时签名、所述控制流校验指令中附带的签名差、所述前驱基本块的补偿签名的异或计算结果等于所述控制流校验指令中附带的静态签名,所述当前基本块通过控制流校验。According to an embodiment of the present invention, the control flow verification module is further configured to insert a control flow verification instruction at the head of the current basic block based on the signature difference and the static signature of the current basic block ; If the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference attached to the control flow verification instruction, and the compensation signature of the predecessor basic block is equal to the static signature, the current basic block passes the control flow check.
根据本发明的一个实施方式,所述数据流校验指令中附带有静态校验签名,所述静态校验签名是基于编译器编译链接时所述当前基本块内所有指令的CRC签名值确定的;所述数据流校验模块,还用于计算所述当前基本块内所有指令的CRC签名值,得到动态校验签名;若所述当前基本块完成执行时的动态校验签名与所述静态校验签名一致,所述当前基本块通过数据流校验。According to an embodiment of the present invention, the data flow verification instruction is accompanied by a static verification signature, and the static verification signature is determined based on the CRC signature values of all instructions in the current basic block when the compiler compiles and links ; The data stream verification module is also used to calculate the CRC signature value of all instructions in the current basic block to obtain a dynamic verification signature; if the dynamic verification signature when the current basic block completes execution is the same as the static The verification signatures are consistent, and the current basic block passes the data stream verification.
根据本发明的一个实施方式,所述控制流包括函数控制流,所述函数控制流包括入口基本块和出口基本块;所述入口基本块在编译阶段插入有签名信息保存指令,所述出口基本块在编译阶段插入有签名信息恢复指令;所述程序流监控装置还包括:签名压栈模块,用于在函数调用时执行所述签名信息保存指令,以支持运行时签名和补偿签名的压栈操作;签名弹栈模块,用于在调用的函数返回时执行所述签名信息恢复指令,以支持保存的运行时签名和补偿签名的弹栈操作。According to an embodiment of the present invention, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; A signature information recovery instruction is inserted into the block at the compilation stage; the program flow monitoring device also includes: a signature push module, which is used to execute the signature information preservation instruction when the function is called, so as to support the push of the runtime signature and compensation signature Operation: a signature popping stack module, configured to execute the signature information recovery instruction when the called function returns, so as to support the stack popping operation of the saved runtime signature and compensation signature.
为达到上述目的,本发明第五方面实施方式提出了一种程序代码编译装置,所述程序代码对应有程序控制流图,且所述程序控制流图中具有若干基本块;所述装置包括:第一指令插入模块,用于在编译阶段,在基本块头部插入控制流校验指令,在基本块尾部插入占位指令;其中,所述控制流校验指令用于在所述程序代码对应的程序流运行到任意基本块时校验所述控制流的正确性;占位指令替换模块,用于在链接阶段,将所述占位指令对应的指令码替换为数据流校验指令;其中,所述数据流校验指令用于在所述程序代码对应的程序流运行到任意基本块的数据流校验指令时,确定所述任意基本块的所有指令的CRC签名值,以校验所述数据流的完整性。In order to achieve the above-mentioned purpose, the embodiment of the fifth aspect of the present invention proposes a program code compiling device, the program code corresponds to a program control flow graph, and the program control flow graph has several basic blocks; the device includes: The first instruction insertion module is used to insert a control flow verification instruction at the head of the basic block and insert a placeholder instruction at the end of the basic block during the compilation phase; wherein the control flow verification instruction is used to correspond to the program code When the program flow runs to any basic block, the correctness of the control flow is checked; the placeholder instruction replacement module is used to replace the instruction code corresponding to the placeholder instruction with a data flow verification instruction in the link stage; wherein The data flow verification instruction is used to determine the CRC signature values of all instructions of any basic block when the program flow corresponding to the program code runs to the data flow verification instruction of any basic block, so as to verify all integrity of the data stream.
根据本发明的一个实施方式,所述控制流包括函数控制流,所述函数控制流包括入口基本块和出口基本块;所述程序代码编译装置还包括:第二指令插入模块,用于在编译阶段,在所述入口基本块插入签名信息保存指令,以及,在所述出口基本块插入签名信息恢复指令;其中,所述签名信息保存指令用于在函数调用时的运行时签名和调整签名的压栈操作,所述签名信息恢复指令用于在调用的函数返回之前的运行时签名和调整签名的弹栈操作。According to an embodiment of the present invention, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; the program code compiling device further includes: a second instruction insertion module, configured to In the stage, a signature information saving instruction is inserted into the entry basic block, and a signature information restoration instruction is inserted into the exit basic block; wherein, the signature information preservation instruction is used to execute the signature and adjust the signature when the function is called In a stack push operation, the signature information recovery instruction is used for a runtime signature before the called function returns and a stack pop operation for adjusting the signature.
为达到上述目的,本发明第六方面实施方式提出了一种计算机设备,包括存储器和处理器,所述存储器存储有计算机程序,所述处理器执行所述计算机程序时实现前述任一项实施方式所述的方法的步骤。In order to achieve the above object, the embodiment of the sixth aspect of the present invention proposes a computer device, including a memory and a processor, the memory stores a computer program, and when the processor executes the computer program, any one of the above embodiments is realized The steps of the method.
为达到上述目的,本发明第七方面实施方式提出了一种计算机可读存储介质,其上存储有计算机程序,所述计算机程序被处理器执行时实现前述任一项实施方式所述的方法的步骤。In order to achieve the above object, the implementation manner of the seventh aspect of the present invention proposes a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the method described in any one of the foregoing implementation manners is implemented. step.
根据本发明提供的多个实施方式,通过签名信息保存指令、签名信息恢复指令、控制流校验指令、数据流校验指令,用于对应用程序的控制流和数据流的完整性和正确性进行实时校验。然后分别实现支持程序流监控指令执行的CPU微架构和支持程序流监控指令生成的编译工具链,通过软硬件结合的程序流监控方法,在数据流和控制流上对程序流进行双重监控。同时,采用先在编译阶段向汇编代码插入控制流校验指令和数据流校验指令的占位指令,后在链接阶段向二进制指令码中插入数据流校验指令以替换占位指令的方法,实现了对新增的控制流校验指令的完整性校验。According to multiple implementations provided by the present invention, the integrity and correctness of the control flow and data flow of the application program are checked by using the signature information preservation instruction, signature information recovery instruction, control flow verification instruction, and data flow verification instruction. Perform real-time verification. Then realize the CPU micro-architecture supporting the execution of program flow monitoring instructions and the compilation tool chain supporting the generation of program flow monitoring instructions, and double monitor the program flow on the data flow and control flow through the program flow monitoring method combining software and hardware. At the same time, the method of first inserting control flow verification instructions and data flow verification instructions into the assembly code in the compilation stage, and then inserting data flow verification instructions into the binary instruction code in the linking stage to replace the placeholder instructions, The integrity verification of the newly added control flow verification instruction is realized.
本发明附加的方面和优点将在下面的描述中部分给出,部分将从下面的描述中变得明显,或通过本发明的实践了解到。Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention.
附图说明Description of drawings
图1为根据本说明书一个实施方式提供的程序流监控方法的流程示意图。Fig. 1 is a schematic flowchart of a program flow monitoring method provided according to an embodiment of the present specification.
图2为根据本说明书一个实施方式提供的校验控制流的正确性的流程示意图。Fig. 2 is a schematic flowchart of verifying the correctness of a control flow provided according to an embodiment of the present specification.
图3为根据本说明书一个实施方式提供的校验数据流的完整性的流程示意图。Fig. 3 is a schematic flowchart of verifying the integrity of a data stream according to an embodiment of the present specification.
图4为根据本说明书一个实施方式提供的程序流监控方法的流程示意图。Fig. 4 is a schematic flowchart of a program flow monitoring method provided according to an embodiment of the present specification.
图5a为根据本说明书一个实施方式提供的程序代码编译方法的流程示意图。Fig. 5a is a schematic flowchart of a program code compiling method provided according to an embodiment of the present specification.
图5b为根据本说明书一个实施方式提供的编译器系统的结构示意图。Fig. 5b is a schematic structural diagram of a compiler system provided according to an embodiment of the present specification.
图5c为根据本说明书一个实施方式提供的控制流校验指令的编码格式示意图。Fig. 5c is a schematic diagram of an encoding format of a control flow verification instruction provided according to an embodiment of the present specification.
图5d为根据本说明书一个实施方式提供的控制流校验指令的编码格式示意图。Fig. 5d is a schematic diagram of an encoding format of a control flow verification instruction provided according to an embodiment of the present specification.
图5e为根据本说明书一个实施方式提供的数据流校验指令的编码格式示意图。Fig. 5e is a schematic diagram of an encoding format of a data stream verification instruction provided according to an embodiment of the present specification.
图5f为根据本说明书一个实施方式提供的插入控制流校验指令和数据流校验指令的过程示意图。Fig. 5f is a schematic diagram of a process of inserting a control flow verification instruction and a data flow verification instruction according to an embodiment of the present specification.
图6a为根据本说明书一个实施方式提供的签名信息保存指令的编码格式示意图。Fig. 6a is a schematic diagram of an encoding format of a signature information saving instruction provided according to an embodiment of the present specification.
图6b为根据本说明书一个实施方式提供的签名信息恢复指令的编码格式示意图。Fig. 6b is a schematic diagram of an encoding format of a signature information restoration instruction provided according to an embodiment of the present specification.
图6c为根据本说明书一个实施方式提供的在基本块中插入指令的过程示意图。Fig. 6c is a schematic diagram of a process of inserting an instruction into a basic block according to an embodiment of the present specification.
图7为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 7 is a structural block diagram of a processor provided according to an embodiment of the present specification.
图8a为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 8a is a structural block diagram of a processor provided according to an embodiment of the present specification.
图8b为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 8b is a structural block diagram of a processor provided according to an embodiment of the present specification.
图9a为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 9a is a structural block diagram of a processor provided according to an embodiment of the present specification.
图9b为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 9b is a structural block diagram of a processor provided according to an embodiment of the present specification.
图10a为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 10a is a structural block diagram of a processor provided according to an embodiment of the present specification.
图10b为根据本说明书一个实施方式提供的处理器的结构框图。Fig. 10b is a structural block diagram of a processor provided according to an embodiment of the present specification.
图11a为根据本说明书一个实施方式提供的程序流监控装置的结构框图。Fig. 11a is a structural block diagram of a program flow monitoring device provided according to an embodiment of the present specification.
图11b为根据本说明书一个实施方式提供的程序流监控装置的结构框图。Fig. 11b is a structural block diagram of a program flow monitoring device provided according to an embodiment of the present specification.
图12a为根据本说明书一个实施方式提供的程序代码编译装置的结构框图。Fig. 12a is a structural block diagram of a program code compiling device provided according to an embodiment of the present specification.
图12b为根据本说明书一个实施方式提供的程序代码编译装置的结构框图。Fig. 12b is a structural block diagram of a program code compiling device provided according to an embodiment of the present specification.
图13为根据本说明书一个实施方式提供的计算机设备的结构框图。Fig. 13 is a structural block diagram of a computer device provided according to an embodiment of the present specification.
具体实施方式Detailed ways
下面详细描述本发明的实施例,所述实施例的示例在附图中示出,其中自始至终相同或类似的标号表示相同或类似的元件或具有相同或类似功能的元件。下面通过参考附图描述的实施例是示例性的,旨在用于解释本发明,而不能理解为对本发明的限制。Embodiments of the present invention are described in detail below, examples of which are shown in the drawings, wherein the same or similar reference numerals designate the same or similar elements or elements having the same or similar functions throughout. The embodiments described below by referring to the figures are exemplary and are intended to explain the present invention and should not be construed as limiting the present invention.
CPU(Central Processing Unit,中央处理单元)作为芯片上的核心单元,在各领域都有广泛的应用。以电力、航天等领域为例,由于电力系统、航天设备等所处环境的特殊性,CPU极易受到电磁干扰、线路串扰、空间单粒子等因素的影响,发生程序运行故障。其中,程序计数器、内存单元被异常改写导致程序执行分支异常跳转、关键变量的值严重超出正常范围的故障尤为严重。由于此类故障受外界环境因素影响,故障发生的内容及发生的内存地址随机性较强,因此无法通过常规的软件测试方法来发现和处理此类故障。As a core unit on a chip, a CPU (Central Processing Unit, central processing unit) is widely used in various fields. Taking electric power, aerospace and other fields as examples, due to the particularity of the environment where power systems and aerospace equipment are located, the CPU is extremely vulnerable to factors such as electromagnetic interference, line crosstalk, and space single particles, resulting in program operation failures. Among them, the program counter and memory unit are abnormally rewritten, causing the program execution branch to jump abnormally, and the value of key variables seriously exceeds the normal range. Faults are particularly serious. Because such faults are affected by external environmental factors, the content of the fault and the memory address where it occurs are relatively random, so it is impossible to discover and deal with such faults through conventional software testing methods.
通常,编译器将软件程序编译为一系列的二进制指令流序列,CPU按照指令流的顺序,对指令流序列依次进行取指、译码、执行操作完成对指令的完整执行。CPU只有按照指令流顺序依次执行指令,才能保证程序的运行满足预期逻辑。如果程序流执行的顺序被打乱,比如部分指令未执行、分支跳转错误等,或者指令被改写为其他指令或者错误的指令,都会导致程序执行结果与预期不符,甚至引起整个系统运行异常。所以,有必要对程序流的执行过程进行实时监控,以便及时发现程序运行中的异常情况并做出相应处理,确保整个系统程序安全、可靠地运行。Usually, the compiler compiles the software program into a series of binary instruction stream sequences, and the CPU sequentially fetches, decodes, and executes the instruction stream sequences according to the order of the instruction streams to complete the complete execution of the instructions. Only when the CPU executes instructions sequentially in accordance with the order of the instruction stream can it ensure that the operation of the program meets the expected logic. If the execution sequence of the program flow is disrupted, such as some instructions not executed, branch jump errors, etc., or instructions are rewritten to other instructions or wrong instructions, the program execution results will not match expectations, and even cause the entire system to run abnormally. Therefore, it is necessary to monitor the execution process of the program flow in real time, so as to detect abnormalities in the program operation in time and deal with them accordingly, so as to ensure the safe and reliable operation of the entire system program.
程序运行时,在执行一条跳转指令后会转向执行另一条指令,这个过程称为一次控制流转移。一个基本块是一个依次顺序执行的指令序列,基于基本块,程序可以表示为由基本块以及连接基本块之间的有向边所构成的控制流图,其中的有向边表示实际的程序控制流转移。如果从基本块Bi到基本块Bj有一条边,则表示程序中存在Bi到Bj的路由,Bi记为Bj的前驱基本块,Bj记为Bi的后继基本块。基于基本块和程序控制流图,合法的程序控制流有下面两层含义:(1)基本块内部的程序控制流。由于基本块内部指令是按照顺序执行的,程序的执行(控制流)只能从基本块的第一条语句进入,从基本块的最后一条语句离开,所以除了基本块的最后一条指令外,每个指令只有唯一的后继指令;(2)基本块之间的程序控制流转移。对于基本块的最后一条指令,其后继指令也可能有多个,但都必须是控制流图中该最后一条指令所属基本块的后继基本块的第一条指令。When the program is running, after executing a jump instruction, it will turn to execute another instruction. This process is called a control flow transfer. A basic block is a sequence of instructions executed sequentially. Based on the basic block, the program can be expressed as a control flow graph composed of basic blocks and directed edges connecting the basic blocks. The directed edges represent the actual program control. stream transfer. If there is an edge from basic block Bi to basic block Bj, it means that there is a route from Bi to Bj in the program, Bi is recorded as the predecessor basic block of Bj, and Bj is recorded as the successor basic block of Bi. Based on the basic block and the program control flow graph, the legal program control flow has the following two meanings: (1) The program control flow inside the basic block. Since the internal instructions of the basic block are executed in sequence, the execution (control flow) of the program can only enter from the first statement of the basic block and leave from the last statement of the basic block, so except for the last instruction of the basic block, every Each instruction has only one successor instruction; (2) Program control flow transfer between basic blocks. For the last instruction of the basic block, there may be multiple subsequent instructions, but all must be the first instruction of the subsequent basic block of the basic block to which the last instruction belongs in the control flow graph.
相关技术中,通常采用以下两种方法来对程序流的运行情况进行监控:In related technologies, the following two methods are usually used to monitor the operation of the program flow:
(1)采用CPU内部看门狗或外部系统看门狗模块来监控软件的运行情况。看门狗电路具有计时功能,如果在规定的时间内程序没有对计数值进行初始化,则认为程序执行异常,此时看门狗电路会输出中断或复位信号。然而,基于看门狗的程序流监控方法只能确认程序有没有进入异常,不能确认程序流是否真正按照预期执行。例如对于跳转指令异常导致程序流执行时跳过部分指令未执行的情况,无法进行监控。且只要清零的定时任务还在正常运行,那么其他地方的错误就不会被发现。有可能在触发看门狗复位信号之前,程序已经在错误状态下运行了较长的一段时间了,这段时间输出的错误数据可能会给用户带来一定损失。(1) Use the CPU internal watchdog or external system watchdog module to monitor the operation of the software. The watchdog circuit has a timing function. If the program does not initialize the count value within the specified time, it is considered that the program execution is abnormal. At this time, the watchdog circuit will output an interrupt or reset signal. However, the program flow monitoring method based on the watchdog can only confirm whether the program has entered an exception, but cannot confirm whether the program flow is actually executed as expected. For example, it is impossible to monitor the situation where some instructions are skipped and not executed when the program flow is executed due to the exception of the jump instruction. And as long as the cleared scheduled task is still running normally, errors in other places will not be discovered. It is possible that the program has been running in an error state for a long period of time before the watchdog reset signal is triggered, and the error data output during this period may bring certain losses to the user.
(2)为每一个被监控的子程序额外添加一个标志值字段,如果该子程序被调用,子程序按照事先约定的算法计算对应的标志值字段,然后主程序将子程序计算出来的标志值与预期的标准值进行比对校验,如果相同,则认定子程序在程序流中的执行满足预期,否则认定程序流中子程序执行错误。然而,该方法只能确认子程序否被调用及程序流中计算标志值字段功能的程序流是否正常执行,对于子程序本身功能逻辑未执行或误执行的情况并不能很好的进行监控,导致对程序流的监控效果并不是很好,且可能增加较多的冗余代码开销。(2) Add an additional flag value field for each monitored subroutine. If the subroutine is called, the subroutine calculates the corresponding flag value field according to the algorithm agreed in advance, and then the main program calculates the flag value calculated by the subroutine Compare and check with the expected standard value, if they are the same, it is determined that the execution of the subroutine in the program flow meets expectations, otherwise it is determined that the execution of the subroutine in the program flow is wrong. However, this method can only confirm whether the subroutine is called and whether the program flow of the function of calculating the flag value field in the program flow is normally executed. The monitoring effect on the program flow is not very good, and more redundant code overhead may be added.
为了解决相关技术中程序流运行过程监控方式所引起的问题,有效提高监控效果,有必要提出一种程序流监控方法、编译方法、装置、处理器及计算机设备,通过对CPU架构指令集扩展程序流监控指令,通过设置签名校验指令,在程序基本块内通过数据流校验指令确保基本块内指令间数据流的完整性,在程序基本块之间通过控制流校验指令确保程序基本块转移跳转的控制流的正确性。然后分别实现支持程序流监控指令执行的CPU微架构和支持程序流监控指令生成的编译工具链。通过软硬件结合的程序流监控方法,在数据流和控制流上对程序流进行双重监控,可以有效检测由于电磁干扰、线路串扰等因素影响而导致的程序执行行为与预期不符的问题。通过程序流监控检测到程序执行发生错误后会立即触发异常,这样应用软件可以及时采取模块复位或者整体复位等应对措施,确保程序运行的正确性。In order to solve the problems caused by the monitoring method of the program flow running process in the related technology and effectively improve the monitoring effect, it is necessary to propose a program flow monitoring method, compiling method, device, processor and computer equipment, by extending the program to the CPU architecture instruction set Flow monitoring instruction, by setting the signature verification instruction, in the program basic block, through the data flow verification instruction to ensure the integrity of the data flow between the instructions in the basic block, and through the control flow verification instruction between the program basic blocks to ensure the program basic block The correctness of the control flow of the transfer jump. Then implement the CPU microarchitecture supporting the execution of program flow monitoring instructions and the compiling tool chain supporting the generation of program flow monitoring instructions. Through the program flow monitoring method combining software and hardware, the program flow is double-monitored on the data flow and control flow, which can effectively detect the problem that the program execution behavior does not match the expected due to the influence of electromagnetic interference, line crosstalk and other factors. After program flow monitoring detects an error in program execution, an exception will be triggered immediately, so that the application software can take countermeasures such as module reset or overall reset in time to ensure the correctness of program operation.
本说明书实施方式提供一种程序流监控方法,程序流对应有控制流和数据流;控制流包括若干基本块;基本块头部在编译阶段插入有控制流校验指令,以及基本块尾部在链接阶段插入有数据流校验指令。其中,数据流校验指令是在链接阶段对编译阶段中插入基本块尾部的占位指令进行替换而插入的。参考图1所示,该程序流监控方法可以包括以下步骤。The embodiment of this specification provides a program flow monitoring method. The program flow corresponds to a control flow and a data flow; the control flow includes several basic blocks; Stages are inserted with data flow verification instructions. Wherein, the data flow checking instruction is inserted in the linking stage by replacing the occupying instruction inserted at the end of the basic block in the compiling stage. Referring to FIG. 1 , the program flow monitoring method may include the following steps.
S110、在程序流运行到当前基本块的情况下,执行当前基本块内的控制流校验指令以校验控制流的正确性。S110. When the program flow runs to the current basic block, execute the control flow verification instruction in the current basic block to verify the correctness of the control flow.
其中,控制流用于表示程序中基本块之间的有向跳转,控制流校验指令可以是包含基本块签名信息的签名校验指令。可以理解的是,程序可以表示为由基本块以及连接基本块之间的有向边所构成的控制流图,其中的有向边表示实际的程序控制流转移。如果从基本块Bi到基本块Bj有一条有向边,则表示程序中存在Bi到Bj的路由,Bi记为Bj的前驱基本块,Bj记为Bi的后继基本块。因此,在程序内可以以基本块为程序流监控校验单位,在基本块之间跳转时进行校验,确保控制流的正确性。Wherein, the control flow is used to represent a directed jump between basic blocks in the program, and the control flow verification instruction may be a signature verification instruction including basic block signature information. It can be understood that a program can be represented as a control flow graph composed of basic blocks and directed edges connecting the basic blocks, where the directed edges represent the actual transfer of program control flow. If there is a directed edge from basic block Bi to basic block Bj, it means that there is a route from Bi to Bj in the program, Bi is recorded as the predecessor basic block of Bj, and Bj is recorded as the successor basic block of Bi. Therefore, in the program, the basic block can be used as the program flow monitoring and verification unit, and the verification is performed when jumping between basic blocks to ensure the correctness of the control flow.
当前基本块用于表示程序流中当前准备执行或当前正在执行的基本块。可以理解的是,由于基本块是程序中顺序执行的指令构成的最大连续块,其中除最后一条指令外其他指令都不能是程序控制指令(程序控制指令能够改变程序原来执行顺序的指令,通常包括条件分支指令、无条件跳转指令、函数调用指令和函数返回指令等),除第一条指令外其他指令都不能是程序控制指令的转移目标。因此,可以在基本块的头部插入控制流校验指令,用于在程序流运行到当前基本块的情况下,在该当前基本块的第一条指令执行之前执行对应的控制流校验指令,以校验该当前基本块是否为跳转到该当前基本块的前驱基本块的正确跳转目标,并在校验该前驱基本块跳转错误的情况下可以直接触发异常,由应用程序进行相应处理。进一步地,编译器可以根据跳转指令来对基本块进行划分和标记,并在此基础上构建程序的控制流图。通过将控制流校验指令加入CPU的扩展指令集,并通过扩展编译工具链,使编译器在程序代码编译阶段将控制流校验指令插入基本块的可执行代码指令序列中,以使校验指令的插入和执行不受高级编程语言的语法等影响。The current basic block is used to represent the basic block that is currently ready to be executed or is currently being executed in the program flow. It can be understood that since the basic block is the largest continuous block composed of sequentially executed instructions in the program, all instructions except the last instruction cannot be program control instructions (program control instructions that can change the original execution order of the program, usually include Conditional branch instructions, unconditional jump instructions, function call instructions, function return instructions, etc.), except for the first instruction, other instructions cannot be the transfer target of the program control instruction. Therefore, a control flow verification instruction can be inserted at the head of the basic block, which is used to execute the corresponding control flow verification instruction before the first instruction of the current basic block is executed when the program flow runs to the current basic block. , to check whether the current basic block is the correct jump target of the predecessor basic block that jumps to the current basic block, and can directly trigger an exception in the case of checking that the predecessor basic block jumps incorrectly, which is performed by the application program Treat accordingly. Furthermore, the compiler can divide and mark the basic blocks according to the jump instructions, and build the control flow graph of the program on this basis. By adding the control flow verification instruction to the extended instruction set of the CPU, and by extending the compilation tool chain, the compiler inserts the control flow verification instruction into the executable code instruction sequence of the basic block during the program code compilation stage, so that the verification The insertion and execution of instructions are not affected by the syntax of high-level programming languages, etc.
具体地,控制流校验指令中的签名信息可以包括每个基本块及其对应的合法前驱基本块的签名信息。当程序流运行到当前基本块时,通过将该当前基本块的控制流校验指令中的签名信息与跳转至该当前基本块的前驱基本块的签名信息进行校验,以检测该前驱基本块是否发生了跳转错误。通过在基本块的头部插入控制流校验指令,使程序流执行至某基本块时,可以立即验证跳转至该基本块的前驱基本块的跳转是否正确,如此,一方面可以校验程序流运行过程中每一个基本块的跳转正确性,从而校验控制流的正确性,另一方面在验证跳转错误的情况下立即触发异常,以避免后续错误指令执行了一段时间后才被发现,避免更大的程序错误。Specifically, the signature information in the control flow verification instruction may include the signature information of each basic block and its corresponding legal predecessor basic block. When the program flow runs to the current basic block, the signature information in the control flow verification instruction of the current basic block is verified with the signature information of the predecessor basic block that jumps to the current basic block to detect the predecessor basic block. Whether the block has a jump error. By inserting a control flow verification instruction at the head of a basic block, when the program flow reaches a certain basic block, it can immediately verify whether the jump to the predecessor basic block of the basic block is correct. In this way, on the one hand, it can be verified The jump correctness of each basic block during the program flow is running, so as to verify the correctness of the control flow. On the other hand, in the case of verifying the jump error, an exception is immediately triggered to avoid subsequent wrong instructions that are executed after a period of time. be found, avoiding larger program errors.
S120、在当前基本块运行至数据流校验指令的情况下,确定当前基本块的所有指令的CRC签名值,以校验数据流的完整性。S120. When the current basic block runs to a data flow verification instruction, determine the CRC signature values of all instructions of the current basic block, so as to verify the integrity of the data flow.
其中,数据流用于表示基本块内部各指令的顺序执行,数据流校验指令可以是包含基本块内所有指令的CRC签名值的签名校验指令。可以理解的是,基本块内的各指令执行没有分支跳转的干扰,因此,可以在基本块的尾部插入数据流校验指令,以用于在程序执行完当前基本块,即将进入下一个基本块时,立即校验该当前基本块内指令序列的完整性,并在校验不通过的情况下可以直接触发异常,由应用程序进行相应处理,避免后续指令序列在程序流中发生异常的情况下继续错误地执行。Wherein, the data flow is used to represent the sequential execution of each instruction in the basic block, and the data flow verification instruction may be a signature verification instruction including CRC signature values of all instructions in the basic block. It can be understood that the execution of each instruction in the basic block does not interfere with the branch jump. Therefore, a data flow verification instruction can be inserted at the end of the basic block to be used to enter the next basic block after the program executes the current basic block. block, immediately verify the integrity of the instruction sequence in the current basic block, and if the verification fails, an exception can be directly triggered, and the application program will handle it accordingly to avoid the occurrence of exceptions in the program flow for subsequent instruction sequences Continue to execute incorrectly.
在一些情况中,由于计算基本块的CRC签名值需要用到基本块中指令的二进制机器码,因此,在程序代码编译阶段生成的汇编文件中,仅在基本块尾部插入占位指令,使数据流校验指令可以通过占位指令进行占位。在程序代码链接阶段,汇编器已将汇编文件中的汇编指令转换为目标文件中的二进制指令码,可以通过CRC校验算法等计算基本块内所有指令的CRC签名值,以填充到数据流校验指令中,并使用该数据流校验指令对基本块尾部的占位指令进行替换,以实现插入数据流校验指令。In some cases, since the calculation of the CRC signature value of the basic block requires the binary machine code of the instruction in the basic block, in the assembly file generated in the program code compilation stage, only placeholder instructions are inserted at the end of the basic block to make the data The flow verification instruction can be occupied by a placeholder instruction. In the program code link stage, the assembler has converted the assembly instructions in the assembly file into binary instruction codes in the object file, and can calculate the CRC signature value of all instructions in the basic block through the CRC check algorithm to fill in the data stream checksum. In the verification instruction, and use the data flow verification instruction to replace the placeholder instruction at the end of the basic block, so as to insert the data flow verification instruction.
需要说明的是,数据流校验指令中包含的CRC签名值为基本块内所有指令未执行时的原始静态校验CRC签名值。上述实施方式中,在当前基本块运行至数据流校验指令的情况下确定的CRC签名值,为当前基本块的所有指令执行时的动态校验CRC签名值。It should be noted that the CRC signature value included in the data flow verification instruction is the original static verification CRC signature value when all instructions in the basic block are not executed. In the above embodiments, the CRC signature value determined when the current basic block runs to the data flow verification instruction is the dynamic verification CRC signature value when all instructions of the current basic block are executed.
具体地,在当前基本块内的指令执行时,动态计算该当前基本块内的所有指令的动态CRC签名值。在当前基本块内的程序指令执行完,即将跳转到下一个基本块时,执行该当前基本块尾部的数据流校验指令,将数据流校验指令中的静态校验CRC签名值与计算得到的该基本块的动态校验CRC签名值进行比较,如果一致,则该基本块内的指令序列完整;如果不一致,则该基本块内的指令序列被篡改,此时可以直接触发异常,由应用程序进行相应处理。Specifically, when the instructions in the current basic block are executed, the dynamic CRC signature values of all the instructions in the current basic block are dynamically calculated. When the program instruction in the current basic block is executed and is about to jump to the next basic block, execute the data flow verification instruction at the end of the current basic block, and compare the static verification CRC signature value in the data flow verification instruction with the calculated The obtained dynamic verification CRC signature value of the basic block is compared, if consistent, the instruction sequence in the basic block is complete; if inconsistent, the instruction sequence in the basic block has been tampered with, and an exception can be directly triggered at this time, by The application processes accordingly.
上述实施方式中,基于将源代码文件经过预处理、编译、汇编、链接生成最终的可执行程序文件的四个阶段,在经过编译阶段生成的汇编代码中先插入控制流校验指令,然后在经过汇编阶段生成的二进制指令码中插入数据流校验指令。一方面,可以避免由于控制流校验指令插入导致基本块内所有指令的原始静态CRC签名值需重新计算的问题;另一方面,插入的控制流校验指令也需要列入基本块内,进行基本块内指令执行的正确性校验,以确保程序流以及新增的控制流校验指令序列的完整性和正确性。In the above embodiment, based on the four stages of generating the final executable program file through preprocessing, compiling, assembling and linking the source code file, the control flow verification instruction is first inserted into the assembly code generated in the compiling stage, and then the A data flow verification instruction is inserted into the binary instruction code generated by the assembly stage. On the one hand, it can avoid the problem that the original static CRC signature value of all instructions in the basic block needs to be recalculated due to the insertion of the control flow verification instruction; on the other hand, the inserted control flow verification instruction also needs to be included in the basic block to perform The correctness check of the instruction execution in the basic block to ensure the integrity and correctness of the program flow and the newly added control flow check instruction sequence.
在一些实施方式中,基本块分配有静态签名;若干基本块包括当前基本块、跳转至当前基本块的前驱基本块。参考图2所示,执行当前基本块内的控制流校验指令以校验控制流的正确性的方法,可以包括以下步骤。In some embodiments, a basic block is assigned a static signature; several basic blocks include a current basic block, a predecessor basic block that jumps to the current basic block. Referring to FIG. 2 , the method for executing the control flow verification instruction in the current basic block to verify the correctness of the control flow may include the following steps.
S210、获取前驱基本块的补偿签名和前驱基本块的运行时签名。S210. Obtain the compensation signature of the predecessor basic block and the runtime signature of the predecessor basic block.
其中,补偿签名是基于前驱基本块的静态签名与前驱基准基本块的静态签名的异或计算结果确定的;前驱基准基本块与前驱基本块处于同一控制级别,前驱基准基本块为前驱基本块所处的控制级别上的基准基本块。补偿签名可以是调整签名。Among them, the compensation signature is determined based on the XOR calculation result of the static signature of the predecessor basic block and the static signature of the predecessor basic block; The reference basic block at the control level at . The compensation signature may be an adjustment signature.
具体地,静态签名可以是为每个基本块依次分配的从0开始递增的自然数或者连续的素数等。运行时签名可以是程序流运行至当前基本块时,对当前基本块进行控制流校验并在校验通过后保存的该当前基本块的静态签名,用于该当前基本块跳转至下一个基本块时的控制流校验。Specifically, the static signature may be a natural number incremented from 0 or a continuous prime number allocated sequentially to each basic block. The runtime signature can be the static signature of the current basic block that is saved after the control flow verification of the current basic block when the program flow runs to the current basic block, and is used for the current basic block to jump to the next Control flow checks for basic blocks.
在一些情况中,一个当前基本块可能对应有可以合法跳转至该当前基本块的多个不同的合法前驱基本块。当程序流从任意合法前驱基本块运行到该当前基本块时,根据对应的任意合法前驱基本块的签名信息,都需要能够使该当前基本块的控制流校验通过。由于程序运行过程中无法确定跳转到当前基本块的前驱基本块是哪一个,如果将所有合法前驱基本块的签名信息加入当前基本块的控制流校验指令中,容易造成太多的冗余开销。因此,可以从该当前基本块的多个合法前驱基本块中选取一个前驱基准基本块,并通过各合法前驱基本块的静态签名与该前驱基准基本块的静态签名计算得到各合法前驱基本块对应的补偿签名,以弥合不同的合法前驱基本块的静态签名之间的差异。当程序流运行到该当前基本块时,通过包含前驱基准基本块的签名信息的控制流校验指令,结合跳转至该当前基本块的前驱基本块的补偿签名,即可对该前驱基本块的跳转正确性进行校验。In some cases, a current basic block may correspond to multiple different legal predecessor basic blocks that can legally jump to the current basic block. When the program flow runs from any legal predecessor basic block to the current basic block, according to the signature information of any corresponding legal predecessor basic block, the control flow verification of the current basic block needs to pass. Since it is impossible to determine which predecessor basic block jumps to the current basic block during the running of the program, if the signature information of all legal predecessor basic blocks is added to the control flow verification instruction of the current basic block, it is easy to cause too much redundancy overhead. Therefore, a predecessor basic block can be selected from multiple legal predecessor basic blocks of the current basic block, and the corresponding legal predecessor basic blocks can be obtained by calculating the static signatures of each legal predecessor basic block and the static signature of the predecessor reference basic block. Compensation signatures to bridge the differences between the static signatures of different legal predecessor basic blocks. When the program flow reaches the current basic block, through the control flow verification instruction containing the signature information of the predecessor basic block, combined with the compensation signature of the predecessor basic block that jumps to the current basic block, the predecessor basic block can be Verify the correctness of the jump.
在一些实施例中,补偿签名的初始值为0,前驱基本块的补偿签名可以根据与其处于同一控制级别的前驱基准基本块的静态签名进行对应的调整。示例性地,基本块B1的静态签名为1,基本块B2的静态签名为2,基本块B3的静态签名为3,基本块B4的静态签名为4。基本块B1、基本块B2和基本块B3均为基本块B4的合法前驱基本块,且基本块B1为基本块B4的前驱基准基本块,则基本块B1的补偿签名为0。将基本块B2的静态签名与基本块B1的静态签名进行异或计算,可以得到基本块B2的补偿签名为3;将基本块B3的静态签名与基本块B1的静态签名进行异或计算,可以得到基本块B3的补偿签名为2。In some embodiments, the initial value of the compensation signature is 0, and the compensation signature of the predecessor basic block can be correspondingly adjusted according to the static signature of the predecessor reference basic block at the same control level. Exemplarily, the static signature of the basic block B1 is 1, the static signature of the basic block B2 is 2, the static signature of the basic block B3 is 3, and the static signature of the basic block B4 is 4. Basic block B1, basic block B2 and basic block B3 are all legal predecessor basic blocks of basic block B4, and basic block B1 is the predecessor reference basic block of basic block B4, then the compensation signature of basic block B1 is 0. Exclusive OR calculation of the static signature of the basic block B2 and the static signature of the basic block B1, the compensation signature of the basic block B2 can be obtained as 3; the XOR calculation of the static signature of the basic block B3 and the static signature of the basic block B1 can be The compensation signature of basic block B3 is 2.
在一些实施例中,一个当前基本块可能只对应有一个可以合法跳转至该当前基本块的前驱基本块,则该前驱基本块即为前驱基准基本块,可以得到该前驱基本块的补偿签名为0。在另一些实施例中,可以根据程序代码的控制流图确定该前驱基本块不与其他基本块拥有同一个后继基本块,可以理解的是,可以不使用该前驱基本块的补偿签名来对其跳转的目标基本块进行控制流校验,因此可以将该前驱基本块的补偿签名直接设置为0。In some embodiments, a current basic block may only correspond to a predecessor basic block that can legally jump to the current basic block, then the predecessor basic block is the predecessor reference basic block, and the compensation signature of the predecessor basic block can be obtained is 0. In some other embodiments, it can be determined according to the control flow graph of the program code that the predecessor basic block does not have the same successor basic block as other basic blocks. It can be understood that the compensation signature of the predecessor basic block may not be used to The target basic block of the jump is checked for control flow, so the compensation signature of the predecessor basic block can be directly set to 0.
需要说明的是,可以合法跳转至同一个基本块的多个不同的前驱基本块处于同一控制级别。It should be noted that multiple different predecessor basic blocks that can legally jump to the same basic block are at the same control level.
S220、确定当前基本块的静态签名和签名差。S220. Determine the static signature and signature difference of the current basic block.
其中,签名差是基于前驱基准基本块的静态签名与当前基本块的静态签名进行异或计算的结果确定的。Wherein, the signature difference is determined based on the XOR calculation result of the static signature of the predecessor basic block and the static signature of the current basic block.
具体地,对于一个当前基本块可能对应有可以合法跳转至该基本块的多个不同的前驱基本块的情况下,可以在当前基本块的多个合法前驱基本块中确定一个前驱基准基本块,从而可以将该前驱基准基本块的静态签名与该当前基本块的静态签名进行异或计算,确定该当前基本块的签名差。Specifically, in the case that a current basic block may correspond to multiple different predecessor basic blocks that can legally jump to the basic block, a predecessor basic block can be determined among the legal predecessor basic blocks of the current basic block , so that the static signature of the predecessor basic block and the static signature of the current basic block can be XOR calculated to determine the signature difference of the current basic block.
在一些实施例中,对于一个当前基本块只对应有可以合法跳转至该基本块的一个前驱基本块的情况下,前驱基准基本块即为该前驱基本块,将该前驱基本块的静态签名与该当前基本块的静态签名进行异或计算,可以确定该当前基本块的签名差。In some embodiments, when a current basic block only corresponds to a predecessor basic block that can legally jump to the basic block, the predecessor reference basic block is the predecessor basic block, and the static signature of the predecessor basic block Perform XOR calculation with the static signature of the current basic block to determine the signature difference of the current basic block.
S230、若前驱基本块的运行时签名、当前基本块的签名差、前驱基本块的补偿签名的异或计算结果等于当前基本块的静态签名,当前基本块通过控制流校验。S230. If the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static signature of the current basic block, the current basic block passes the control flow verification.
示例性地,基本块B1的静态签名为1,基本块B2的静态签名为2,基本块B3的静态签名为3,基本块B4的静态签名为4,基本块B5的静态签名为5,基本块B6的静态签名为6。基本块B1和基本块B2均为基本块B4的合法前驱基本块,且基本块B1为基本块B4的前驱基准基本块;基本块B3和基本块B5均为基本块B6的合法前驱基本块,且基本块B3为基本块B6的前驱基准基本块。通过计算可以得知,基本块B1的运行时签名为1,补偿签名为0;基本块B2的运行时签名为2,补偿签名为3;基本块B3的运行时签名为3,补偿签名为0;基本块B5的运行时签名为5,补偿签名为6。将基本块B1的静态签名与基本块B4的静态签名进行异或计算,可以得到基本块B4的签名差为5;将基本块B3的静态签名与基本块B6的静态签名进行异或计算,可以得到基本块B6的签名差为5。当程序流由基本块B1运行至基本块B4时,需要校验基本块B4是否为基本块B1的正确跳转目标。将基本块B4的签名差5、基本块B1的运行时签名1、基本块B1的补偿签名0进行异或计算,得到计算结果为4,与基本块B4的静态签名相等,则基本块B4通过控制流校验,即基本块B4为基本块B1的正确跳转目标,将基本块B4的运行时签名保存为4。Exemplarily, the static signature of basic block B1 is 1, the static signature of basic block B2 is 2, the static signature of basic block B3 is 3, the static signature of basic block B4 is 4, the static signature of basic block B5 is 5, and the basic Block B6 has a static signature of 6. Both basic block B1 and basic block B2 are legal predecessor basic blocks of basic block B4, and basic block B1 is the predecessor basic block of basic block B4; basic block B3 and basic block B5 are legal predecessor basic blocks of basic block B6, And the basic block B3 is the predecessor reference basic block of the basic block B6. Through calculation, it can be known that the runtime signature of the basic block B1 is 1, and the compensation signature is 0; the runtime signature of the basic block B2 is 2, and the compensation signature is 3; the runtime signature of the basic block B3 is 3, and the compensation signature is 0 ; Basic block B5 has a runtime signature of 5 and a compensation signature of 6. Exclusive OR calculation of the static signature of the basic block B1 and the static signature of the basic block B4, the signature difference of the basic block B4 can be obtained as 5; the XOR calculation of the static signature of the basic block B3 and the static signature of the basic block B6 can be The signature difference of basic block B6 is 5. When the program flow runs from the basic block B1 to the basic block B4, it is necessary to check whether the basic block B4 is the correct jump target of the basic block B1. XOR calculation is performed on the signature difference 5 of the basic block B4, the runtime signature 1 of the basic block B1, and the compensation signature 0 of the basic block B1, and the calculation result is 4, which is equal to the static signature of the basic block B4, and the basic block B4 passes Control flow verification, that is, the basic block B4 is the correct jump target of the basic block B1, and the runtime signature of the basic block B4 is saved as 4.
示例性地,当程序流由基本块B2运行至基本块B4时,需要校验基本块B4是否为基本块B2的正确跳转目标。将基本块B4的签名差5、基本块B2的运行时签名2、基本块B2的补偿签名3进行异或计算,得到计算结果为4,与基本块B4的静态签名相等,则基本块B4通过控制流校验,将基本块B4的运行时签名保存为4。Exemplarily, when the program flow runs from the basic block B2 to the basic block B4, it is necessary to check whether the basic block B4 is the correct jump target of the basic block B2. Exclusive OR calculation is performed on the signature difference 5 of the basic block B4, the runtime signature 2 of the basic block B2, and the compensation signature 3 of the basic block B2, and the calculation result is 4, which is equal to the static signature of the basic block B4, and the basic block B4 passes Control flow check, save the runtime signature of basic block B4 as 4.
示例性地,当程序流由基本块B3运行至基本块B4时,需要校验基本块B4是否为基本块B3的正确跳转目标。将基本块B4的签名差5、基本块B3的运行时签名3、基本块B3的补偿签名0进行异或计算,得到计算结果为6,与基本块B4的静态签名不相等,则基本块B4没有通过控制流校验,即基本块B4不是基本块B3的正确跳转目标,直接触发异常。Exemplarily, when the program flow runs from the basic block B3 to the basic block B4, it is necessary to check whether the basic block B4 is the correct jump target of the basic block B3. XOR calculation is performed on the signature difference 5 of the basic block B4, the runtime signature 3 of the basic block B3, and the compensation signature 0 of the basic block B3, and the calculation result is 6, which is not equal to the static signature of the basic block B4, then the basic block B4 The control flow check is not passed, that is, the basic block B4 is not the correct jump target of the basic block B3, and an exception is directly triggered.
上述实施方式中,对于具有多个合法前驱基本块的当前基本块,通过在多个合法前驱基本块中选定一个前驱基准基本块,并通过该前驱基准基本块的签名来调整其他合法前驱基本块的补偿签名,以确保程序流无论经由哪个合法前驱基本块运行到当前基本块时,结合对应的前驱基本块的补偿签名进行计算和校验,使该当前基本块均可以通过校验。In the above implementation, for the current basic block with multiple legal predecessor basic blocks, by selecting a predecessor basic block among the multiple legal predecessor basic blocks, and adjusting the signature of the predecessor basic block to adjust other legal predecessor basic blocks Compensation signature of the block to ensure that no matter which legal predecessor basic block the program flow runs to the current basic block, it is calculated and verified in combination with the compensation signature of the corresponding predecessor basic block, so that the current basic block can pass the verification.
在一些实施方式中,前驱基本块的数量为1时,前驱基本块为前驱基准基本块,补偿签名为0。In some implementations, when the number of the predecessor basic block is 1, the predecessor basic block is the predecessor reference basic block, and the compensation signature is 0.
在一些实施方式中,前驱基本块的数量大于1时,前驱基准基本块是基于前驱基本块所处的控制级别上的基本块的静态签名确定的。In some implementations, when the number of predecessor basic blocks is greater than 1, the predecessor reference basic block is determined based on the static signature of the basic block at the control level of the predecessor basic block.
示例性地,可以将处于同一控制级别上的各前驱基本块中具有最小值或最大值静态签名的前驱基本块确定为该控制级别上的基准基本块,即前驱基准基本块。Exemplarily, among the predecessor basic blocks at the same control level, the predecessor basic block with the minimum or maximum static signature may be determined as the reference basic block at the control level, that is, the predecessor reference basic block.
在一些实施方式中,若前驱基本块的运行时签名、当前基本块的签名差、前驱基本块的补偿签名的异或计算结果不等于当前基本块的静态签名,当前基本块不通过控制流校验,以触发异常。In some implementations, if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is not equal to the static signature of the current basic block, the current basic block does not pass the control flow verification. test to trigger an exception.
在一些实施方式中,在若前驱基本块的运行时签名、当前基本块的签名差、前驱基本块的补偿签名的异或计算结果等于当前基本块的静态签名,则当前基本块通过控制流校验之前,程序流监控方法还可以包括:基于签名差和当前基本块的静态签名,在当前基本块的头部插入有控制流校验指令。In some implementations, if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static signature of the current basic block, then the current basic block is checked by the control flow. Before verification, the program flow monitoring method may also include: inserting a control flow verification instruction at the head of the current basic block based on the signature difference and the static signature of the current basic block.
相应地,若前驱基本块的运行时签名、当前基本块的签名差、前驱基本块的补偿签名的异或计算结果等于当前基本块的静态签名,则当前基本块通过控制流校验,还可以包括:若前驱基本块的运行时签名、控制流校验指令中附带的签名差、前驱基本块的补偿签名的异或计算结果等于控制流校验指令中附带的静态签名,当前基本块通过控制流校验。Correspondingly, if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static signature of the current basic block, then the current basic block passes the control flow verification, and can also Including: if the runtime signature of the predecessor basic block, the signature difference attached to the control flow verification instruction, and the XOR calculation result of the compensation signature of the predecessor basic block are equal to the static signature attached to the control flow verification instruction, the current basic block passes the control Stream check.
具体地,设计控制流校验签名,包括基本块的静态签名、运行时签名、签名差和补偿签名,以记录每个基本块及其前驱基本块的签名信息用于控制流校验,避免基本块跳转到非后继基本块的头部。每个基本块的控制流校验指令中可以包含每个基本块的静态签名以及签名差。进一步地,设计运行时签名寄存器以存放基本块通过校验之后的运行时签名,用于由该基本块跳转至下一个基本块时的控制流校验;设计补偿签名寄存器以存放基本块的补偿签名,用于由该基本块跳转至下一个基本块时的控制流校验。Specifically, the control flow verification signature is designed, including the static signature of the basic block, the runtime signature, the signature difference and the compensation signature, so as to record the signature information of each basic block and its predecessor basic block for control flow verification, avoiding basic Block jumps to the head of a non-successor basic block. The control flow verification instruction of each basic block may contain the static signature and signature difference of each basic block. Further, the runtime signature register is designed to store the runtime signature after the basic block has passed the verification, which is used for the control flow verification when the basic block jumps to the next basic block; the compensation signature register is designed to store the basic block Compensation signature, used for control flow verification when jumping from this basic block to the next basic block.
示例性地,当程序流由基本块B1运行至基本块B2时,需要执行基本块B2的控制流校验指令,以校验基本块B2是否为基本块B1的正确跳转目标。B2的控制流校验指令中包含基本块B2的静态签名,以及根据基本块B2与其前驱基本块(前驱基准基本块)的静态签名计算得到的基本块B2的签名差。此时,运行时签名寄存器中存放有基本块B1的运行时签名,补偿签名寄存器中存放有基本块B1的补偿签名。执行基本块B2的控制流校验指令,从运行时寄存器中获取基本块B1的运行时签名,从补偿签名寄存器中获取基本块B1的补偿签名,从控制流校验指令中获取基本块B2的静态签名和签名差,对上述获取的运行时签名、补偿签名、签名差进行异或计算,并将得到的计算结果与基本块B2的静态签名进行比较。如果计算结果与基本块B2的静态签名一致,则基本块B2的控制流校验通过,基本块B1的跳转没有发生错误;如果计算结果与基本块B2的静态签名不一致,则基本块B2的控制流校验不通过,基本块B1的跳转发生错误,可以直接触发异常报警。Exemplarily, when the program flow runs from the basic block B1 to the basic block B2, the control flow verification instruction of the basic block B2 needs to be executed to verify whether the basic block B2 is the correct jump target of the basic block B1. The control flow verification instruction of B2 includes the static signature of basic block B2, and the signature difference of basic block B2 calculated based on the static signature of basic block B2 and its predecessor basic block (predecessor basic basic block). At this time, the runtime signature of the basic block B1 is stored in the runtime signature register, and the compensation signature of the basic block B1 is stored in the compensation signature register. Execute the control flow verification instruction of basic block B2, obtain the runtime signature of basic block B1 from the runtime register, obtain the compensation signature of basic block B1 from the compensation signature register, and obtain the signature of basic block B2 from the control flow verification instruction Static signature and signature difference, XOR calculation is performed on the runtime signature, compensation signature and signature difference obtained above, and the calculation result is compared with the static signature of the basic block B2. If the calculation result is consistent with the static signature of the basic block B2, the control flow verification of the basic block B2 passes, and no error occurs in the jump of the basic block B1; if the calculation result is inconsistent with the static signature of the basic block B2, the basic block B2’s If the control flow verification fails and the jump of the basic block B1 is wrong, an abnormal alarm can be directly triggered.
上述实施方式中,通过动态计算基本块的运行时签名,并将基本块的运行时签名作为变量或组件(如寄存器等)存储在程序运行过程中,用于动态验证基本块的控制流正确性,以实时确保程序执行过程的正确性。In the above implementation, the runtime signature of the basic block is dynamically calculated, and the runtime signature of the basic block is stored as a variable or component (such as a register, etc.) in the running process of the program to dynamically verify the correctness of the control flow of the basic block , to ensure the correctness of the program execution process in real time.
在一些实施方式中,数据流校验指令中附带有静态校验签名,静态校验签名是基于编译器编译链接时当前基本块内所有指令的CRC签名值确定的。参考图3所示,确定当前基本块的所有指令的CRC签名值,以校验数据流的完整性,可以包括以下步骤。In some implementations, the data stream verification instruction is accompanied by a static verification signature, and the static verification signature is determined based on the CRC signature values of all instructions in the current basic block when the compiler compiles and links. Referring to FIG. 3 , determining the CRC signature values of all instructions of the current basic block to verify the integrity of the data stream may include the following steps.
S310、计算当前基本块内所有指令的CRC签名值,得到动态校验签名。S310. Calculate CRC signature values of all instructions in the current basic block to obtain dynamic verification signatures.
S320、若当前基本块完成执行时的动态校验签名与静态校验签名一致,当前基本块通过数据流校验。S320. If the dynamic verification signature when the execution of the current basic block is completed is consistent with the static verification signature, the current basic block passes the data stream verification.
其中,动态校验签名可以是当前基本块内所有指令执行后的CRC签名值。Wherein, the dynamic verification signature may be a CRC signature value after execution of all instructions in the current basic block.
具体地,设计数据流校验签名,包括静态校验签名和动态校验签名,以记录每个基本块内部的指令编码信息,避免基本块内部指令以及基本块中在编译阶段添加的控制流校验指令被非法篡改。进一步地,设计动态校验签名寄存器以存放基本块运行时基本块内所有指令的动态校验签名,用于该基本块运行结束时的数据流校验。Specifically, the data flow verification signature is designed, including static verification signature and dynamic verification signature, to record the instruction encoding information inside each basic block, avoiding the internal instruction of the basic block and the control flow verification added in the basic block during the compilation stage. The verification instruction has been illegally tampered with. Furthermore, the dynamic verification signature register is designed to store the dynamic verification signatures of all instructions in the basic block when the basic block is running, and is used for data flow verification at the end of the basic block running.
示例性地,当基本块B1内的指令执行时,包括基本块B1头部的控制流校验指令执行时,可以根据CRC校验算法等动态计算基本块B1内的所有指令的动态校验签名,并存放在动态校验签名寄存器中。当基本块B1内的指令执行完即将跳转至下一个基本块时,执行基本块B1尾部的数据流校验指令,从动态校验签名寄存器中获取基本块B1内所有指令的动态校验签名,从基本块B1的数据流校验指令中获取基本块B1内所有指令的静态校验签名,将动态校验签名和静态校验签名进行比较,如果一致,则基本块B1内各指令序列完整;如果不一致,则基本块B1内的指令被篡改,可以直接触发异常报警。Exemplarily, when the instructions in the basic block B1 are executed, and the control flow verification instructions including the header of the basic block B1 are executed, the dynamic verification signatures of all instructions in the basic block B1 can be dynamically calculated according to the CRC verification algorithm, etc. , and stored in the dynamic verification signature register. When the instruction in the basic block B1 is executed and is about to jump to the next basic block, execute the data flow verification instruction at the end of the basic block B1, and obtain the dynamic verification signature of all instructions in the basic block B1 from the dynamic verification signature register , Obtain the static verification signatures of all instructions in the basic block B1 from the data flow verification instructions of the basic block B1, compare the dynamic verification signatures with the static verification signatures, if they are consistent, the instruction sequences in the basic block B1 are complete ; If inconsistent, the instructions in the basic block B1 have been tampered with, which can directly trigger an abnormal alarm.
上述实施方式中,通过动态计算基本块内所有指令的动态校验签名,并将动态校验签名作为变量或组件(如寄存器等)存储在程序运行过程中,用于动态验证基本块内所有指令序列的正确性和完整性,以进一步确保程序执行过程的正确性和完整性。In the above embodiment, the dynamic verification signature of all instructions in the basic block is dynamically calculated, and the dynamic verification signature is stored as a variable or component (such as a register, etc.) in the running process of the program to dynamically verify all instructions in the basic block The correctness and integrity of the sequence to further ensure the correctness and integrity of the program execution process.
在一些实施方式中,执行当前基本块内的控制流校验指令以校验控制流的正确性的方法还可以包括:若当前基本块完成执行时的动态校验签名与静态校验签名不一致,当前基本块不通过数据流校验以触发异常。In some embodiments, the method of executing the control flow verification instruction in the current basic block to verify the correctness of the control flow may further include: if the dynamic verification signature when the current basic block completes execution is inconsistent with the static verification signature, The current basic block does not pass the data flow check to trigger an exception.
在一些实施方式中,控制流包括函数控制流,函数控制流包括入口基本块和出口基本块。入口基本块在编译阶段插入有签名信息保存指令,出口基本块在编译阶段插入有签名信息恢复指令。参考图4所示,程序流监控方法还可以包括以下步骤。In some implementations, the control flow includes a function control flow that includes an entry basic block and an exit basic block. The entry basic block inserts a signature information preservation instruction in the compilation stage, and the exit basic block inserts a signature information recovery instruction in the compilation stage. Referring to Fig. 4, the program flow monitoring method may further include the following steps.
S410、在函数调用时执行签名信息保存指令,以支持运行时签名和补偿签名的压栈操作。S410. Execute the signature information saving instruction when the function is called, so as to support the push operation of the runtime signature and the compensation signature.
S420、在调用的函数返回时执行签名信息恢复指令,以支持保存的运行时签名和补偿签名的弹栈操作。S420. Execute a signature information restoration instruction when the called function returns, so as to support the pop-up operation of the saved runtime signature and compensation signature.
其中,签名信息保存指令用于在函数控制流的入口基本块中保存通过函数调用跳转到该函数控制流的原函数控制流中的基本块的签名信息。签名信息恢复指令用于在函数控制流的出口基本块中恢复通过签名信息保存指令保存的原函数控制流中的基本块的签名信息。Wherein, the signature information saving instruction is used to save the signature information of the basic block in the original function control flow jumping to the function control flow through the function call in the entry basic block of the function control flow. The signature information restoration instruction is used to restore the signature information of the basic block in the original function control flow saved by the signature information preservation instruction in the exit basic block of the function control flow.
可以理解的是,程序是由若干个函数组成的,函数调用可以看作是原函数控制流中断,跳转进入被调用的函数控制流的一种特殊的控制流转移。为了避免被调用的函数控制流中的基本块对执行该调用的原函数控制流中的基本块的控制流校验过程产生影响,需要在被调用函数的函数控制流的入口基本块中保存调用该函数的原函数控制流中的基本块的签名信息,并在该被调用函数的函数控制流的出口基本块中恢复已保存的原函数控制流中的基本块的签名信息,以使被调用函数中的出口基本块执行结束并返回原函数时,可以继续进行原函数控制流中的基本块的控制流校验。It can be understood that a program is composed of several functions, and a function call can be regarded as a special control flow transfer that interrupts the control flow of the original function and jumps into the control flow of the called function. In order to prevent the basic block in the control flow of the called function from affecting the control flow verification process of the basic block in the control flow of the original function that executes the call, it is necessary to save the call in the entry basic block of the function control flow of the called function The signature information of the basic block in the original function control flow of the function, and restore the saved signature information of the basic block in the original function control flow in the exit basic block of the function control flow of the called function, so that the called When the exit basic block in the function finishes executing and returns to the original function, the control flow check of the basic block in the control flow of the original function can be continued.
具体地,可以在函数控制流的入口基本块的头部插入签名信息保存指令,在函数控制流的出口基本块的尾部插入签名信息恢复指令。进一步地,可以设置签名栈,以使签名信息保存指令执行后,可以将对应的签名信息通过对签名栈执行压栈操作进行保存,以及使签名信息恢复指令执行后,可以将对应的通过签名信息保存指令保存的签名信息通过对签名栈执行弹栈操作进行恢复。Specifically, a signature information saving instruction may be inserted at the head of the entry basic block of the function control flow, and a signature information restoration instruction may be inserted at the end of the exit basic block of the function control flow. Furthermore, the signature stack can be set so that after the signature information saving instruction is executed, the corresponding signature information can be saved by performing a push operation on the signature stack, and after the signature information recovery instruction is executed, the corresponding passing signature information can be saved. The signature information saved by the save instruction is restored by popping the signature stack.
示例性地,在函数FUN2被函数FUN1调用时,程序流通过函数FUN1中的函数调用指令运行至函数FUN2,函数FUN1的函数控制流发生中断。首先执行函数FUN2的入口基本块的签名信息保存指令,将函数FUN1的函数控制流中断处的基本块的运行时签名和补偿签名压入签名栈进行保存,其次将当前运行时签名寄存器和补偿签名寄存器中的签名值初始化,以用于进行函数FUN2的函数控制流中的基本块的控制流校验。当函数FUN2执行结束时,程序流通过函数FUN2的返回指令返回至函数FUN1,需要将函数FUN1的函数控制流恢复。首先执行函数FUN2的出口基本块的签名信息恢复指令,将签名栈中保存的函数FUN1的函数控制流中断处的基本块的运行时签名和补偿签名进行弹栈操作取出,其次将当前运行时签名寄存器中的签名值设置为该运行时签名,将补偿签名寄存器中的签名值设置为该补偿签名,以用于恢复函数FUN2返回后的函数FUN1的函数控制流中的基本块的控制流校验。Exemplarily, when the function FUN2 is called by the function FUN1, the program flow runs to the function FUN2 through the function call instruction in the function FUN1, and the function control flow of the function FUN1 is interrupted. First execute the signature information saving instruction of the entry basic block of function FUN2, push the runtime signature and compensation signature of the basic block at the interruption of the function control flow of function FUN1 into the signature stack for saving, and then save the current runtime signature register and compensation signature The signature value in the register is initialized to be used for the control flow check of the basic block in the function control flow of the function FUN2. When the execution of the function FUN2 ends, the program flow returns to the function FUN1 through the return instruction of the function FUN2, and the function control flow of the function FUN1 needs to be restored. First execute the signature information restoration instruction of the exit basic block of function FUN2, pop the runtime signature and compensation signature of the basic block at the interrupted function control flow of the function FUN1 saved in the signature stack, and take out the stack operation, and then remove the current runtime signature The signature value in the register is set to the runtime signature, and the signature value in the compensation signature register is set to the compensation signature, which is used to restore the control flow check of the basic block in the function control flow of the function FUN1 after the function FUN2 returns .
在一些实施方式中,程序流监控方法还可以包括:增加全局监控的编译器参数,以对应用程序的全部运行过程进行监控。In some embodiments, the program flow monitoring method may further include: adding global monitoring compiler parameters to monitor the entire running process of the application program.
其中,编译器参数可以是程序流监控开关的参数和/或程序流监控范围的关键字代码。Wherein, the compiler parameter may be a parameter of a program flow monitoring switch and/or a keyword code of a program flow monitoring scope.
具体地,在被监控的应用程序代码中添加全局监控开关的参数和/或全局监控的关键字代码,使扩展的具有程序流监控功能的编译器工具链能够识别并自动生成控制流校验指令和数据流校验指令,且能够将生成的校验指令自动插入应用程序代码的基本块中,并最终生成全部应用程序代码范围内的指令序列的可执行代码。Specifically, add the parameters of the global monitoring switch and/or the keyword code of the global monitoring in the monitored application code, so that the extended compiler tool chain with program flow monitoring function can recognize and automatically generate control flow verification instructions and data flow verification instructions, and the generated verification instructions can be automatically inserted into the basic blocks of the application program code, and finally the executable code of the instruction sequence within the scope of the entire application program code is generated.
在一些实施方式中,程序流监控方法还可以包括:针对源代码中的指定函数,在指定函数的指定位置增加程序流检测标记以及编译器参数,以对指定函数的运行过程进行监控。In some implementations, the program flow monitoring method may further include: for a specified function in the source code, adding a program flow detection flag and a compiler parameter at a specified position of the specified function, so as to monitor the running process of the specified function.
其中,程序流检测标记可以是描述性前缀,用于标记需要进行程序流监控的函数。示例性地,对于应用程序代码中指定的需要监控运行过程的函数,可以在函数定义时的函数名前增加描述性前缀,用于标记该函数定义后紧挨着的函数需要进行程序流监控。Wherein, the program flow detection mark may be a descriptive prefix, which is used to mark functions that need to be monitored by program flow. Exemplarily, for a function specified in the application code that needs to monitor the running process, a descriptive prefix can be added before the function name when the function is defined to mark that the function immediately after the function definition needs to be monitored by the program flow.
进一步地,可以通过编译器参数调整来改变程序流检测标记的功能。示例性地,通过编译器参数调整,使程序流检测标记用于标记不需要进行程序流监控的函数,并可以在包含该指定函数的较大的程序流监控范围内,在该函数定义时的函数名前增加对应的程序流检测标记,用于标记该函数定义后紧挨着的函数需要从该程序流监控范围内排除。Furthermore, the function of the program flow detection flag can be changed by adjusting compiler parameters. Exemplarily, by adjusting compiler parameters, the program flow detection flag is used to mark functions that do not need to be monitored by program flow, and within a larger program flow monitoring scope that includes the specified function, when the function is defined The corresponding program flow detection flag is added before the function name, which is used to mark that the function immediately following the function definition needs to be excluded from the scope of the program flow monitoring.
在一些实施方式中,程序流监控方法还可以包括:在源代码中通过括号将需要监控的目标源码括起来;在目标源码的指定位置处增加制导语句,以对目标源码的运行过程进行监控。In some embodiments, the program flow monitoring method may further include: enclosing the target source code to be monitored through parentheses in the source code; adding guidance statements at specified positions of the target source code to monitor the running process of the target source code.
其中,制导语句可以是具有编译器可以识别的语法规则和行动规则的代码语句,用于标记需要进行程序流监控的目标源码。示例性地,对于应用程序代码中指定的需要监控运行过程的目标源码,可以使用大括号对“{}”将目标源码括起来,并在左大括号“{”的前面增加制导语句,用于标记制导语句后面紧跟着的大括号对“{}”内的源码需要进行程序流监控。Wherein, the guidance statement may be a code statement with grammatical rules and action rules that can be recognized by the compiler, and is used to mark the target source code that needs to be monitored by the program flow. Exemplarily, for the target source code specified in the application code that needs to monitor the running process, the target source code can be enclosed by curly bracket pairs "{}", and a guidance statement can be added in front of the left curly bracket "{" for The curly braces immediately following the tag-directed statement require program flow monitoring for the source code within "{}".
进一步地,可以通过编译器参数调整来改变制导语句的功能。示例性地,通过编译器参数调整,使制导语句用于标记不需要进行程序流监控的目标源码,并可以在包含该目标源码的较大的程序流监控范围内,使用大括号对“{}”将该目标源码括起来,并在左大括号“{”的前面增加对应的制导语句,用于标记该标记制导语句后面紧跟着的大括号对“{}”内的源码需要从该程序流监控范围内排除。Further, the function of the guidance statement can be changed by adjusting compiler parameters. Exemplarily, through the adjustment of compiler parameters, the guidance statement is used to mark the target source code that does not need to be monitored by the program flow, and within the scope of larger program flow monitoring that includes the target source code, use curly braces to "{} "Enclose the target source code, and add the corresponding guidance statement in front of the left curly brace "{", which is used to mark the source code in the pair of curly braces "{}" immediately following the guidance statement of the mark needs to be obtained from the program Excluded from flow monitoring.
本说明书实施方式提供一种程序代码编译方法,程序代码对应有程序控制流图,且程序控制流图中具有若干基本块。参考图5a所示,该程序代码编译方法可以包括以下步骤。The implementation mode of this specification provides a program code compiling method, the program code corresponds to a program control flow graph, and the program control flow graph has several basic blocks. Referring to Fig. 5a, the program code compiling method may include the following steps.
S510、在编译阶段,在基本块头部插入控制流校验指令,在基本块尾部插入占位指令。S510. In the compilation phase, insert a control flow verification instruction at the head of the basic block, and insert a placeholder instruction at the end of the basic block.
其中,控制流校验指令用于在程序代码对应的程序流运行到任意基本块时校验控制流的正确性。Wherein, the control flow verification instruction is used to verify the correctness of the control flow when the program flow corresponding to the program code runs to any basic block.
S520、在链接阶段,将占位指令对应的指令码替换为数据流校验指令。S520. In the linking stage, replace the instruction code corresponding to the placeholder instruction with a data flow verification instruction.
其中,数据流校验指令用于在程序代码对应的程序流运行到任意基本块的数据流校验指令时,确定任意基本块的所有指令的CRC签名值,以校验数据流的完整性。Wherein, the data flow verification instruction is used to determine the CRC signature value of all instructions of any basic block when the program flow corresponding to the program code runs to the data flow verification instruction of any basic block, so as to verify the integrity of the data flow.
需要说明的是,关于上述实施方式中控制流校验指令和数据流校验指令的描述,请参考本说明书中关于程序流监控方法中控制流校验指令和数据流校验指令的描述,具体这里不再赘述。It should be noted that, for the description of the control flow verification instruction and the data flow verification instruction in the above embodiment, please refer to the description of the control flow verification instruction and the data flow verification instruction in the program flow monitoring method in this specification. I won't go into details here.
具体地,扩展编译工具链,修改编译器,由编译器对程序代码划分基本块并构建控制流图,并在程序代码的编译阶段,一方面根据设计的控制流校验过程,由编译器生成对应的控制流校验指令,并将控制流校验指令插入基本块的可执行指令序列中;另一方面由编译器在基本块尾部插入占位指令,用于在后续程序代码的链接阶段,由汇编器将该占位指令替换为数据流校验指令。示例性地,修改编译器,使编译器首先创建控制流校验指令和数据流校验指令的占位指令的语句,其次将创建好的控制流校验指令和占位指令的语句插入到基本块的语句序列中,然后对控制流图进行更新。在一些实施例中,可以在编译器对程序代码划分基本块并构建控制流图之后,在GIMPLE树转化为RTL树之前执行插入语句。示例性地,参考图5b所示,为根据上述实施例对GCC编译器进行修改后的编译器系统结构。Specifically, the compilation tool chain is extended, the compiler is modified, and the compiler divides the program code into basic blocks and constructs a control flow graph. During the compilation phase of the program code, on the one hand, according to the designed control flow verification process, the compiler generates Corresponding control flow verification instruction, and insert the control flow verification instruction into the executable instruction sequence of the basic block; on the other hand, the compiler inserts a placeholder instruction at the end of the basic block for the linking stage of the subsequent program code, The placeholder instruction is replaced by a data flow verification instruction by the assembler. Exemplarily, the compiler is modified so that the compiler first creates the statement of the control flow verification instruction and the placeholder instruction of the data flow verification instruction, and then inserts the created statement of the control flow verification instruction and the placeholder instruction into the basic block's sequence of statements, and then updates the control flow graph. In some embodiments, the insert statement may be executed before the GIMPLE tree is converted into an RTL tree after the compiler divides the program code into basic blocks and builds a control flow graph. Exemplarily, referring to FIG. 5 b , it is a compiler system structure after modifying the GCC compiler according to the above-mentioned embodiment.
具体地,修改汇编器,首先在指令列表中加入相应扩展指令的定义,然后编写指令处理函数,使汇编器能够根据不同的指令在对应指令的二进制机器码的各个域中进行填充。同时,在汇编器将汇编文件翻译成二进制机器码的时候,通过对基本块内所有指令的二进制机器码进行CRC校验算法处理,得到基本块的静态校验签名,以填充到数据流校验指令中对应的域中。在程序代码的链接阶段,即在汇编器将汇编文件翻译成二进制机器码后,在基本块的尾部将占位指令替换为包含该签名值的数据流校验指令。Specifically, modify the assembler, first add the definition of the corresponding extended instruction in the instruction list, and then write the instruction processing function, so that the assembler can fill in each field of the binary machine code of the corresponding instruction according to different instructions. At the same time, when the assembler translates the assembly file into binary machine code, the binary machine code of all instructions in the basic block is processed by the CRC check algorithm to obtain the static check signature of the basic block to fill in the data flow check In the corresponding field in the instruction. In the link stage of the program code, that is, after the assembler translates the assembly file into binary machine code, the placeholder instruction is replaced with a data flow verification instruction containing the signature value at the end of the basic block.
示例性地,根据当前基本块的签名差d、静态签名S以及补偿签名Dc,设置控制流校验指令的汇编格式为CTRLSIG_M d, S, Dc。其中,操作数d、操作数S和操作数Dc都是源操作数且都是常数,由程序代码编译时预先计算生成并放入该指令,不需要从通用寄存器中读源操作数。该指令执行控制流校验的过程为:Exemplarily, according to the signature difference d of the current basic block, the static signature S and the compensation signature D c , the assembly format of the control flow verification instruction is set as CTRLSIG_M d, S, D c . Wherein, operand d, operand S and operand D c are all source operands and are all constants, which are pre-calculated and put into the instruction when the program code is compiled, and do not need to read source operands from general-purpose registers. The process of performing control flow verification by this instruction is:
首先,计算当前基本块的运行时签名:Gc=G'⊕op(d)⊕D'。First, the runtime signature of the current basic block is calculated: Gc = G'⊕op(d)⊕D'.
其中,op操作表示从指令编码中取立即数,G'表示运行时签名寄存器中保存的前驱基本块的运行时签名,D'表示补偿签名寄存器中保存的前驱基本块的补偿签名。Among them, the op operation means fetching the immediate value from the instruction code, G' means the runtime signature of the predecessor basic block saved in the runtime signature register, and D' means the compensation signature of the predecessor basic block saved in the compensation signature register.
其次,校验当前基本块的运行时签名,若G'⊕op(d)⊕D'≠op(S),则触发异常,并在异常原因寄存器中写入本指令;若G'⊕op(d)⊕D'=op(S),则校验通过,将当前基本块的运行时签名Gc保存到运行时签名寄存器中,将当前基本块的补偿签名Dc保存到补偿签名寄存器中,即G'=Gc=op(S),D'=op(Dc),用于当前基本块跳转至下一个基本块时的控制流校验。图5c所示为控制流校验指令对应的一种编码格式。Secondly, verify the runtime signature of the current basic block, if G'⊕op(d)⊕D'≠op(S), an exception will be triggered, and this instruction will be written in the exception cause register; if G'⊕op( d)⊕D'=op(S), then the verification is passed, the runtime signature Gc of the current basic block is saved in the runtime signature register, and the compensation signature Dc of the current basic block is saved in the compensation signature register, That is, G'=G c =op(S), D'=op(D c ), used for control flow check when the current basic block jumps to the next basic block. Fig. 5c shows a coding format corresponding to the control flow checking instruction.
在一些实施例中,当前基本块仅有一个合法前驱基本块时,该前驱基本块的补偿签名为0。可以理解的是,这种情况下,当前基本块的控制流校验可以不使用该前驱基本块的补偿签名,因此,可以设置控制流校验指令的汇编格式为CTRLSIG_S d, S, Dc。该指令执行控制流校验的过程为:In some embodiments, when the current basic block has only one valid predecessor basic block, the compensation signature of the predecessor basic block is 0. It can be understood that, in this case, the control flow verification of the current basic block may not use the compensation signature of the predecessor basic block. Therefore, the assembly format of the control flow verification instruction can be set as CTRLSIG_S d, S, D c . The process of performing control flow verification by this instruction is:
首先,计算当前基本块的运行时签名Gc=G'⊕op(d)。First, the runtime signature G c =G'⊕op(d) of the current basic block is calculated.
其次,校验当前基本块的运行时签名,若G'⊕op(d)≠op(S),则触发异常,并在异常原因寄存器中写入本指令;若G'⊕op(d)=op(S),则校验通过,将当前基本块的运行时签名Gc保存到运行时签名寄存器中,将当前基本块的补偿签名Dc保存到补偿签名寄存器中,即G'=Gc=op(S),D'=op(Dc),用于当前基本块跳转至下一个基本块时的控制流校验。图5d所示为该控制流校验指令对应的一种编码格式。Secondly, verify the runtime signature of the current basic block, if G'⊕op(d)≠op(S), trigger an exception, and write this instruction in the exception cause register; if G'⊕op(d)= op(S), the verification is passed, the runtime signature G c of the current basic block is saved in the runtime signature register, and the compensation signature D c of the current basic block is saved in the compensation signature register, that is, G'=G c =op(S), D'=op(D c ), used for control flow check when the current basic block jumps to the next basic block. Fig. 5d shows a coding format corresponding to the control flow checking instruction.
示例性地,根据当前基本块中所有指令的静态校验签名C,设置数据流校验指令的汇编格式为CRCSIG C。其中,操作数C为源操作数,由汇编器生成目标文件时预先计算生成并放入该指令,不需要从通用寄存器中读源操作数。该指令执行数据流校验的过程为:首先计算当前基本块执行时所有指令的动态校验签名N,并保存在动态校验签名寄存器中。其次将动态校验签名寄存器中的动态校验签名N和静态校验签名C进行比较,若相等则当前基本块内的指令序列正确且完整,若不相等则触发异常。图5e所示为该数据流校验指令对应的一种编码格式。Exemplarily, according to the static verification signature C of all instructions in the current basic block, the assembly format of the data flow verification instruction is set to CRCSIG C. Among them, the operand C is the source operand, which is pre-calculated and put into the instruction when the assembler generates the object file, and does not need to read the source operand from the general-purpose register. The process of performing data flow verification by this instruction is as follows: first, calculate the dynamic verification signature N of all instructions when the current basic block is executed, and store it in the dynamic verification signature register. Secondly, compare the dynamic verification signature N in the dynamic verification signature register with the static verification signature C. If they are equal, the instruction sequence in the current basic block is correct and complete. If they are not equal, an exception will be triggered. FIG. 5e shows an encoding format corresponding to the data stream verification instruction.
进一步地,在一些实施例中,参考图5f所示,为在程序代码编译过程中分别在基本块中插入对应的控制流校验指令和数据流校验指令的过程示意图。该过程可以包括:Further, in some embodiments, refer to FIG. 5f , which is a schematic diagram of inserting corresponding control flow verification instructions and data flow verification instructions into basic blocks during program code compilation. The process can include:
在编译阶段,编译器在分析程序代码中的跳转关系,对程序代码划分基本块并构建控制流图后,首先可以为每个基本块分配对应的静态签名S。In the compilation phase, after the compiler analyzes the jump relationship in the program code, divides the program code into basic blocks and constructs a control flow graph, it can first assign a corresponding static signature S to each basic block.
其次,可以根据控制流图选择任意一个当前基本块,判断该当前基本块是否与其所处的同一控制级别中的其他基本块拥有同一个后继基本块。如果判断该当前基本块与其所处的同一控制级别中的其他基本块拥有同一个后继基本块,则在对应的控制级别中确定前驱基准基本块并计算该当前基本块的补偿签名Dc;否则,将该当前基本块的补偿签名Dc设置为0。Secondly, any current basic block can be selected according to the control flow graph, and it can be judged whether the current basic block has the same successor basic block as other basic blocks in the same control level. If it is judged that the current basic block and other basic blocks in the same control level have the same successor basic block, then determine the predecessor reference basic block in the corresponding control level and calculate the compensation signature D c of the current basic block; otherwise , and set the compensation signature D c of the current basic block to 0.
再次,判断该当前基本块是否拥有多个合法前驱基本块,如果判断为是,则在该当前基本块的头部插入控制流校验指令CTRLSIG_M d, S, Dc;如果判断为否,则在该当前基本块的头部插入控制流校验指令CTRLSIG_S d, S, Dc。Again, judge whether the current basic block has multiple legal predecessor basic blocks, if it is judged to be yes, then insert the control flow check instruction CTRLSIG_M d, S, D c at the head of the current basic block; if it is judged to be no, then Insert control flow check instructions CTRLSIG_S d, S, D c at the head of the current basic block.
然后,在该当前基本块的尾部插入用于数据流校验指令进行占位的占位指令。Then, at the end of the current basic block, insert a placeholder instruction for the data flow verification instruction to occupy the placeholder.
经过上述编译阶段的方法处理后的汇编文件中,包含控制流校验指令以及定位数据流校验指令的占位指令。再然后,汇编器将汇编文件中的汇编指令转换为目标文件中的二进制指令码,并根据指令处理函数填充对应的指令二进制机器码的各个域。汇编器根据CRC校验算法计算各基本块中所有指令的静态校验签名C,以填充到对应的数据流校验指令CRCSIG C中,并逐一更新对应的基本块尾部的占位指令。最后,链接器将目标文件链接起来,生成带有控制流校验指令和数据流校验指令的可执行程序文件。The assembly file processed by the method in the above compilation stage includes control flow verification instructions and placeholder instructions for locating data flow verification instructions. Then, the assembler converts the assembly instructions in the assembly file into binary instruction codes in the object file, and fills each field of the corresponding instruction binary machine code according to the instruction processing function. The assembler calculates the static verification signature C of all instructions in each basic block according to the CRC verification algorithm, fills it into the corresponding data flow verification instruction CRCSIG C, and updates the occupancy instructions at the end of the corresponding basic block one by one. Finally, the linker links the object files to generate an executable program file with control flow verification instructions and data flow verification instructions.
上述实施方式中,每个基本块仅插入一条控制流校验签名指令,且仅在尾部插入一条数据流校验指令,在满足程序流错误监控的同时,尽量减小签名指令带来的额外开销,可以使得应用程序在重新编译后的代码增加量最小化。In the above implementation, only one control flow verification signature instruction is inserted into each basic block, and only one data flow verification instruction is inserted at the end, which satisfies program flow error monitoring and minimizes the extra overhead caused by signature instructions , which can minimize the code increase of the application program after recompilation.
在一些实施方式中,控制流包括函数控制流,函数控制流包括入口基本块和出口基本块。程序代码编译方法还可以包括:在编译阶段,在入口基本块插入签名信息保存指令,以及,在出口基本块插入签名信息恢复指令。In some implementations, the control flow includes a function control flow that includes an entry basic block and an exit basic block. The program code compiling method may also include: inserting a signature information saving instruction into the entry basic block, and inserting a signature information restoration instruction into the exit basic block during the compilation stage.
其中,签名信息保存指令用于在函数调用时的运行时签名和调整签名的压栈操作,签名信息恢复指令用于在调用的函数返回之前的运行时签名和调整签名的弹栈操作。Among them, the signature information saving instruction is used for the runtime signature and the push operation of adjusting the signature when the function is called, and the signature information recovery instruction is used for the runtime signature and the stack pop operation of adjusting the signature before the called function returns.
需要说明的是,关于上述实施方式中签名信息保存指令和签名信息恢复指令的描述,请参考本说明书中关于程序流监控方法中签名信息保存指令和签名信息恢复指令的描述,具体这里不再赘述。It should be noted that, for the description of the signature information saving instruction and the signature information restoration instruction in the above embodiment, please refer to the description of the signature information preservation instruction and the signature information restoration instruction in the program flow monitoring method in this manual, and details will not be repeated here. .
具体地,扩展编译工具链,修改编译器,由编译器对程序代码划分基本块并构建控制流图,并在程序代码的编译阶段,由编译器生成的签名信息保存指令和签名信息恢复指令,并将签名信息保存指令插入到每个函数的入口基本块的可执行指令序列中,将签名信息恢复指令插入到每个函数的出口基本块的可执行指令序列中。Specifically, expand the compilation tool chain, modify the compiler, divide the program code into basic blocks by the compiler and build a control flow graph, and in the compilation phase of the program code, the signature information preservation instruction and signature information restoration instruction generated by the compiler, The signature information saving instruction is inserted into the executable instruction sequence of the entry basic block of each function, and the signature information restoration instruction is inserted into the executable instruction sequence of the exit basic block of each function.
示例性地,设置签名信息保存指令的汇编格式为PUSHSIG。该指令执行时将通过函数调用指令跳转至当前函数入口基本块的前一个函数基本块的运行时签名Gpf和补偿签名Dpf压入签名栈进行保存,同时初始化当前运行时签名寄存器中的运行时签名G和补偿签名寄存器中的补偿签名D。图6a所示为该签名信息保存指令对应的一种编码格式。Exemplarily, the assembly format of the set signature information saving instruction is PUSHSIG. When this instruction is executed, the runtime signature G pf and the compensation signature D pf of the previous function basic block jumping to the current function entry basic block through the function call instruction are pushed into the signature stack for preservation, and at the same time, the current runtime signature register is initialized Runtime signature G and compensation signature D in the compensation signature register. Fig. 6a shows a coding format corresponding to the signature information saving instruction.
示例性地,设置签名信息恢复指令的汇编格式为POPSIG。该指令执行时通过弹栈操作将签名栈中保存的前一个函数基本块的运行时签名Gpf和补偿签名Dpf取出,并将该运行时签名Gpf保存到运行时签名寄存器中,将该补偿签名Dpf保存到补偿签名寄存器中,即G=Gpf,D=Dpf。图6b所示为该签名信息恢复指令对应的一种编码格式。Exemplarily, the assembly format of the signature information recovery instruction is set as POPSIG. When this instruction is executed, the runtime signature G pf and the compensation signature D pf of the previous function basic block saved in the signature stack are taken out by popping the stack operation, and the runtime signature G pf is saved in the runtime signature register. The compensation signature D pf is saved in the compensation signature register, that is, G=G pf , D=D pf . Fig. 6b shows an encoding format corresponding to the signature information recovery instruction.
进一步地,在一些实施例中,参考图6c所示,为在程序代码编译过程中分别在函数中插入签名信息保存指令和签名信息恢复指令,在基本块中插入对应的控制流校验指令和数据流校验指令的过程示意图。其中,在编译阶段,编译器在对程序代码划分基本块并构建控制流图后,还可以为每个函数的入口基本块插入签名信息保存指令PUSHSIG,为每个函数的出口基本块插入签名信息恢复指令POPSIG。需要说明的是,关于图6c中所示的在基本块中插入对应的控制流校验指令和数据流校验指令的过程的描述,请参考本说明书中关于图5f所示的在基本块中插入对应的控制流校验指令和数据流校验指令的过程的描述,具体这里不再赘述。Further, in some embodiments, as shown in FIG. 6c, in order to insert signature information saving instructions and signature information restoration instructions into functions during program code compilation, insert corresponding control flow verification instructions and Schematic diagram of the process of the data flow verification instruction. Among them, in the compilation stage, after the compiler divides the program code into basic blocks and constructs the control flow graph, it can also insert the signature information saving instruction PUSHSIG for the entry basic block of each function, and insert the signature information for the exit basic block of each function Restore instruction POPSIG. It should be noted that, for the description of the process of inserting the corresponding control flow verification instruction and data flow verification instruction in the basic block shown in Figure 6c, please refer to the basic block shown in Figure 5f in this specification. The description of the process of inserting the corresponding control flow verification instruction and data flow verification instruction will not be repeated here.
根据本说明书中上述多个实施方式,通过在每个函数的入口基本块中插入一条保存签名指令,以及在每个出口基本块中插入一条恢复签名指令;在每个基本块中仅插入一条控制流校验签名指令,以及仅在每个基本块的尾部插入一条数据流校验指令,可以利用最少的扩展指令完成程序流监控的目标,尽量减小扩展指令给应用程序的存储和执行带来的额外开销。同时,每个基本块中一般包含6~10条指令,指令执行发生异常后可以很快被检测到,以确保将指令发生的错误限制在本基本块内或者下一个基本块起始位置。进一步地,本说明书实施方式中的编译方法是针对程序文件的text段新增一些用于程序流监控的扩展指令,编译后的整个可执行目标文件的大小增加比例一般在10%以内,对应用程序的运行影响较小。According to the above-mentioned multiple implementations in this specification, by inserting a save signature instruction in the entry basic block of each function, and inserting a restore signature instruction in each exit basic block; only one control instruction is inserted in each basic block Flow verification signature instructions, and only inserting a data flow verification instruction at the end of each basic block, can use the least extended instructions to complete the goal of program flow monitoring, and minimize the impact of extended instructions on the storage and execution of applications. additional overhead. At the same time, each basic block generally contains 6 to 10 instructions, and the abnormal execution of the instruction can be detected quickly to ensure that the error of the instruction is limited to the basic block or the starting position of the next basic block. Further, the compiling method in the embodiment of this specification is to add some extended instructions for program flow monitoring to the text section of the program file. The size increase ratio of the compiled executable target file is generally within 10%, which is not necessary for the application program. Operational impact is small.
本说明书实施方式提供一种处理器,处理器用于执行程序流,程序流对应有控制流和数据流;控制流包括若干基本块。参考图7所示,处理器700可以包括位于译码流水级的扩展译码电路710、位于写回流水级的控制流校验电路720和数据流校验电路730。The embodiments of this specification provide a processor, which is used to execute a program flow, and the program flow corresponds to a control flow and a data flow; the control flow includes several basic blocks. Referring to FIG. 7 , the processor 700 may include an extended decoding circuit 710 at the decoding pipeline level, a control flow checking circuit 720 and a data flow checking circuit 730 at the writing back pipeline level.
扩展译码电路710,用于译码控制流校验指令和数据流校验指令;其中,控制流校验指令是在编译阶段插入基本块头部的,数据流校验指令是在链接阶段对编译阶段中插入基本块尾部的占位指令进行替换而插入的。The extended decoding circuit 710 is used to decode the control flow check instruction and the data flow check instruction; wherein, the control flow check instruction is inserted into the header of the basic block during the compilation stage, and the data flow check instruction is inserted into the basic block header during the link stage. It is inserted by replacing the placeholder instruction inserted at the end of the basic block during the compilation phase.
控制流校验电路720,用于在程序流运行到当前基本块的情况下,执行当前基本块内的控制流校验指令以校验控制流的正确性。数据流校验电路730,用于在当前基本块运行至数据流校验指令的情况下,确定当前基本块的所有指令的CRC签名值,以校验数据流的完整性。The control flow verification circuit 720 is configured to execute the control flow verification instruction in the current basic block to verify the correctness of the control flow when the program flow runs to the current basic block. The data flow verification circuit 730 is configured to determine the CRC signature values of all instructions of the current basic block to verify the integrity of the data flow when the current basic block runs to the data flow verification instruction.
可以理解的是,根据“取指-译码-执行-访存-写回”的五个阶段的指令执行过程,处理器700上的指令流水线可以包括取指流水级、译码流水级、指令执行流水级、访存流水级和写回流水级。It can be understood that, according to the five-stage instruction execution process of "fetch-decode-execute-memory access-write-back", the instruction pipeline on the processor 700 may include an instruction fetch pipeline stage, a decode pipeline stage, an instruction Execution pipeline, fetch pipeline and write back pipeline.
具体地,扩展译码电路710可以用于在指令译码功能单元增加对扩展指令的译码逻辑。译码逻辑可以根据扩展指令的指令操作码位域的值对指令类型进行识别,以根据识别出的不同的指令类型产生对应的控制信号,并将控制信号沿着指令流水线之间的流水线级间寄存器向下传递,这些控制信号用于控制各级流水线的运算类型选择、操作数选择、结果选择等。示例性地,针对控制流校验指令、数据流校验指令、签名信息保存指令、签名信息恢复指令,扩展译码电路710至少需要产生如表1所示的控制信号。Specifically, the extended decoding circuit 710 may be used to add decoding logic for extended instructions to the instruction decoding functional unit. The decoding logic can identify the instruction type according to the value of the instruction opcode bit field of the extended instruction, so as to generate corresponding control signals according to the identified different instruction types, and send the control signals along the pipeline stages between the instruction pipelines The registers are passed down, and these control signals are used to control the operation type selection, operand selection, result selection, etc. of the pipeline at all levels. Exemplarily, for the control flow verification instruction, the data flow verification instruction, the signature information saving instruction, and the signature information restoration instruction, the extended decoding circuit 710 needs to generate at least the control signals shown in Table 1.
表1Table 1
其中,基本块起始信号可以用于指示基本块的起始位置,可以通过指令流水线传递给数据流校验电路730。中间签名可以包括控制流校验过程中需要通过计算得到的运行时签名,和/或通过从指令编码中取立即数得到的补偿签名。Wherein, the basic block start signal can be used to indicate the start position of the basic block, and can be transmitted to the data stream verification circuit 730 through the instruction pipeline. The intermediate signature may include a runtime signature that needs to be calculated during the control flow verification process, and/or a compensation signature that is obtained by fetching an immediate value from the instruction code.
示例性地,控制流校验电路720可以包括中间签名计算电路和中间签名比较电路。对应于控制流校验的CTRLSIG指令,可以在指令流水线的写回流水级设置中间签名计算电路、中间签名比较电路、运行时签名寄存器regG和补偿签名寄存器regD。中间签名计算电路可以根据由扩展译码电路710产生并沿指令流水线传递到写回级的控制信号,按照前述控制流校验的签名运算规则计算当前基本块的中间签名。中间签名比较电路可以将中间签名计算电路产生的中间签名按照CTRLSIG指令的语义,与指令中的操作数进行比较,如果比较结果是一致的,则将该中间签名中计算得到的运行时签名存入运行时签名寄存器regG中,将取立即数得到的补偿签名存入补偿签名寄存器regD中;如果比较结果是不一致的,则产生指令异常指示信号,由异常处理电路进行后续的异常处理。Exemplarily, the control flow verification circuit 720 may include an intermediate signature calculation circuit and an intermediate signature comparison circuit. Corresponding to the CTRLSIG instruction for control flow verification, an intermediate signature calculation circuit, an intermediate signature comparison circuit, a runtime signature register regG, and a compensation signature register regD can be set at the write-back pipeline stage of the instruction pipeline. The intermediate signature calculation circuit can calculate the intermediate signature of the current basic block according to the signature operation rule of the aforementioned control flow check according to the control signal generated by the extended decoding circuit 710 and transmitted to the write-back stage along the instruction pipeline. The intermediate signature comparison circuit can compare the intermediate signature generated by the intermediate signature calculation circuit with the operands in the instruction according to the semantics of the CTRLSIG instruction. If the comparison results are consistent, the runtime signature calculated in the intermediate signature is stored in the In the runtime signature register regG, the compensation signature obtained by fetching the immediate value is stored in the compensation signature register regD; if the comparison result is inconsistent, an instruction exception indication signal is generated, and the exception handling circuit performs subsequent exception handling.
示例性地,数据流校验电路730可以包括动态校验签名值计算电路和校验签名值比较电路。对应于数据流校验的CRCSIG指令,可以在指令流水线的写回流水级设置动态校验签名值计算电路、校验签名值比较电路和动态校验签名值寄存器regN。校验签名值比较电路可以接受当前正在执行的指令及指令码宽度指示信号,如果指令为16位宽,则可以在指令码的高位填充16个0,构成一个32位指令码,放入动态校验签名值计算电路中;如果指令为32位宽,则不需额外填充0,将此32位指令码放入动态校验签名值计算电路中。动态校验签名值计算电路对放入的指令动态计算校验签名值,采用的算法可以是CRC校验算法其他校验算法。在当前执行的指令不是CRCSIG指令时,将动态校验签名值计算电路的结果存入动态校验签名值寄存器regN;在当前指令是CRCSIG指令时,将动态校验签名值计算电路的结果与CRCSIG指令中的操作数进行比较,如果一致则指令执行正常,如果不一致,则产生指令异常信号,由异常处理电路进行后续的异常处理。Exemplarily, the data stream verification circuit 730 may include a dynamic verification signature value calculation circuit and a verification signature value comparison circuit. Corresponding to the CRCSIG instruction for data flow verification, a dynamic verification signature value calculation circuit, a verification signature value comparison circuit and a dynamic verification signature value register regN can be set at the write-back pipeline level of the instruction pipeline. The verification signature value comparison circuit can accept the instruction currently being executed and the instruction code width indication signal. If the instruction is 16-bit wide, 16 0s can be filled in the high bits of the instruction code to form a 32-bit instruction code, which is put into the dynamic calibration In the verification signature value calculation circuit; if the instruction is 32-bit wide, there is no need to add 0 additionally, and this 32-bit instruction code is put into the dynamic verification signature value calculation circuit. The dynamic verification signature value calculation circuit dynamically calculates the verification signature value for the input instruction, and the adopted algorithm may be CRC verification algorithm or other verification algorithms. When the currently executed instruction is not a CRCSIG instruction, the result of the dynamic verification signature value calculation circuit is stored in the dynamic verification signature value register regN; when the current instruction is a CRCSIG instruction, the result of the dynamic verification signature value calculation circuit is compared with the CRCSIG The operands in the instruction are compared. If they are consistent, the instruction is executed normally. If they are inconsistent, an instruction exception signal is generated, and the exception processing circuit performs subsequent exception processing.
进一步地,由于CRCSIG指令是对每一个基本块内的指令进行校验,当新的基本块开始执行的时候,需要重新开始计算基本块内的动态校验签名值。在一些实施例中,可以利用扩展译码电路710对CTRLSIG指令译码产生的当前基本块起始信号,用此信号清除掉动态校验签名值寄存器regN的值,以保存计算得到的当前基本块内指令的动态校验签名值,使上一个基本块的数据流校验不会影响到当前基本块的数据流校验。Furthermore, since the CRCSIG instruction is to verify the instructions in each basic block, when a new basic block starts to execute, it is necessary to restart the calculation of the dynamic verification signature value in the basic block. In some embodiments, the extended decoding circuit 710 can be used to decode the current basic block start signal generated by the CTRLSIG instruction, and use this signal to clear the value of the dynamic verification signature value register regN to save the calculated current basic block The dynamic verification signature value of the inner instruction, so that the data flow verification of the previous basic block will not affect the data flow verification of the current basic block.
在一些实施例中,在经控制流校验电路720或数据流校验电路730比较发现相应的签名不一致的情况下,将产生指令异常信号。为处理这些指令异常信号,可以设置异常收集和记录电路,使指令异常信号产生后能够被发送到该异常收集和记录电路。该电路可以将控制流校验异常、数据流校验异常等指令异常信号中的异常信息收集起来,并在与指令流水线原有的异常处理电路合并后,产生最终的指令异常指示。其中,指令流水线对异常的处理属于本领域的公知,具体这里不再赘述。In some embodiments, when the control flow verification circuit 720 or the data flow verification circuit 730 compares and finds that the corresponding signatures are inconsistent, an instruction exception signal will be generated. In order to handle these instruction abnormal signals, an abnormal collection and recording circuit can be set, so that the instruction abnormal signal can be sent to the abnormal collection and recording circuit after it is generated. The circuit can collect abnormal information in instruction abnormal signals such as control flow verification exception and data flow verification exception, and after merging with the original exception processing circuit of the instruction pipeline, generate the final instruction abnormal indication. Wherein, the processing of the exception by the instruction pipeline is well known in the art, and details are not repeated here.
上述实施方式中,在数据流校验电路中对不同长度的指令码进行填充0操作,使得用于计算动态校验签名值的指令码统一为32位,简化了数据流校验电路的复杂度,同时可以满足对不同长度的指令码计算校验值的需求。In the above embodiment, in the data flow verification circuit, the instruction codes of different lengths are filled with 0, so that the instruction codes used to calculate the dynamic verification signature value are unified into 32 bits, which simplifies the complexity of the data flow verification circuit. , and at the same time, it can meet the requirement of calculating the check value for instruction codes of different lengths.
在一些实施方式中,基本块分配有静态签名;若干基本块包括当前基本块、跳转至当前基本块的前驱基本块。参考图8a所示,处理器700可以设置有运行时签名寄存器810、补偿签名寄存器820。In some embodiments, a basic block is assigned a static signature; several basic blocks include a current basic block, a predecessor basic block that jumps to the current basic block. Referring to FIG. 8 a , the processor 700 may be provided with a runtime signature register 810 and a compensation signature register 820 .
运行时签名寄存器810存储有前驱基本块的运行时签名,补偿签名寄存器820存储有前驱基本块的补偿签名。其中,补偿签名是基于前驱基本块的静态签名与前驱基准基本块的静态签名的异或计算结果确定的;前驱基准基本块与前驱基本块处于同一控制级别,前驱基准基本块可以理解为前驱基本块所处的控制级别上的基准基本块。The runtime signature register 810 stores the runtime signature of the predecessor basic block, and the compensation signature register 820 stores the compensation signature of the predecessor basic block. Among them, the compensation signature is determined based on the XOR calculation result of the static signature of the precursor basic block and the static signature of the precursor basic block; the precursor reference basic block and the predecessor basic block are at the same control level, and the precursor reference basic block can be understood as The base basic block at the level of control in which the block resides.
控制流校验电路720还用于确定当前基本块的静态签名和签名差,若前驱基本块的运行时签名、当前基本块的签名差、前驱基本块的补偿签名的异或计算结果等于当前基本块的静态签名,当前基本块通过控制流校验。其中,签名差是基于前驱基准基本块的静态签名与当前基本块的静态签名进行异或计算的结果确定的。The control flow verification circuit 720 is also used to determine the static signature and signature difference of the current basic block, if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the current basic block The static signature of the block, the current basic block passes the control flow verification. Wherein, the signature difference is determined based on the XOR calculation result of the static signature of the predecessor basic block and the static signature of the current basic block.
示例性地,参考图8b所示,控制流校验电路720可以与运行时签名寄存器810和补偿签名寄存器820连接,控制流校验电路720还可以包括中间签名计算电路830和中间签名比较电路840。当执行指令CTRLSIG_M d, S, Dc时,中间签名计算电路830可以根据由扩展译码电路710产生并沿指令流水线传递到写回级的控制信号,按照前述控制流校验的签名运算规则计算当前基本块的运行时签名Gc=G'⊕op(d)⊕D',其中,op操作表示从指令编码中取立即数,G'表示从当前运行时签名寄存器810中取出的前驱基本块的运行时签名,D'表示从当前补偿签名寄存器820中取出的前驱基本块的补偿签名。中间签名比较电路840将计算产生的运行时签名Gc按照CTRLSIG_M指令的语义,与指令中的操作数进行比较,即比较G'⊕op(d)⊕D'与op(S)是否一致,如果比较结果是一致的,则将当前基本块的运行时签名Gc存入运行时签名寄存器810中,将取立即数得到的当前基本块的补偿签名op(D)存入补偿签名寄存器820中。Exemplarily, as shown in FIG. 8b, the control flow verification circuit 720 may be connected to the runtime signature register 810 and the compensation signature register 820, and the control flow verification circuit 720 may also include an intermediate signature calculation circuit 830 and an intermediate signature comparison circuit 840. . When the instruction CTRLSIG_M d, S, D c is executed, the intermediate signature calculation circuit 830 can calculate according to the control signal generated by the extended decoding circuit 710 and transmitted to the write-back stage along the instruction pipeline according to the signature operation rule of the aforementioned control flow check The runtime signature of the current basic block G c =G'⊕op(d)⊕D', where the op operation means fetching an immediate value from the instruction code, and G' denotes the predecessor basic block fetched from the current runtime signature register 810 D' represents the compensation signature of the predecessor basic block fetched from the current compensation signature register 820 . The intermediate signature comparison circuit 840 compares the calculated runtime signature G c with the operands in the instruction according to the semantics of the CTRLSIG_M instruction, that is, compares whether G'⊕op(d)⊕D' is consistent with op(S), if If the comparison result is consistent, the runtime signature G c of the current basic block is stored in the runtime signature register 810 , and the compensation signature op(D) of the current basic block obtained by fetching the immediate value is stored in the compensation signature register 820 .
在一些实施方式中,控制流包括函数控制流,函数控制流包括入口基本块和出口基本块;入口基本块在编译阶段插入有签名信息保存指令,出口基本块在编译阶段插入有签名信息恢复指令。参考图9a所示,处理器700可以设置有签名栈910。签名栈910用于在函数调用时执行签名信息保存指令,支持运行时签名和补偿签名的压栈操作;在调用的函数返回时执行签名信息恢复指令,支持保存的运行时签名和补偿签名的弹栈操作。In some implementations, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; the entry basic block is inserted with a signature information saving instruction during the compilation phase, and the exit basic block is inserted with a signature information restoration instruction during the compilation phase . Referring to FIG. 9 a , the processor 700 may be provided with a signature stack 910 . The signature stack 910 is used to execute the signature information saving instruction when the function is called, and supports the push operation of the runtime signature and the compensation signature; when the called function returns, it executes the signature information recovery instruction, and supports the saved runtime signature and compensation signature. stack operations.
示例性地,参考图9b所示,签名栈910可以与控制流校验电路720连接。在一些实施例中,签名栈910可以是通过设置专用的硬件存储电路,并将此存储电路按照栈的方式进行管理而实现的。当执行PUSHSIG指令时,将当前基本块的运行时签名G和补偿签名D存入签名栈910的栈顶寄存器;当执行POPSIG指令时,从当前的签名栈910的栈顶寄存器读出运行时签名G和补偿签名D,并对应地存入运行时签名寄存器810和补偿签名寄存器820中。通过设置专用的硬件存储电路来实现签名栈910,优点在于可以不需要设置软件进行额外的电路控制;缺点在于,由于对专用的硬件电路通常有容量的限制,且为了节省硬件电路,电路的面积通常都比较小,当程序执行时,如果需要压栈的签名信息超出了专用硬件电路的容量,则会产生栈溢出的问题,需要额外进行栈溢出的异常处理。Exemplarily, as shown in FIG. 9 b , the signature stack 910 may be connected to the control flow verification circuit 720 . In some embodiments, the signature stack 910 may be implemented by setting a dedicated hardware storage circuit and managing the storage circuit in a stack manner. When executing the PUSHSIG instruction, the runtime signature G and compensation signature D of the current basic block are stored in the top register of the signature stack 910; when the POPSIG instruction is executed, the runtime signature is read from the top register of the current signature stack 910 G and the compensation signature D are stored in the runtime signature register 810 and the compensation signature register 820 correspondingly. The signature stack 910 is implemented by setting a dedicated hardware storage circuit, which has the advantage of not needing to set software for additional circuit control; the disadvantage is that due to the limited capacity of the dedicated hardware circuit, and in order to save the hardware circuit, the area of the circuit Usually they are relatively small. When the program is executed, if the signature information that needs to be pushed on the stack exceeds the capacity of the dedicated hardware circuit, a stack overflow problem will occur, and additional stack overflow exception handling is required.
在另一些实施例中,签名栈910可以是通过软件设置在系统存储器中分配一部分空间用于签名信息的保存而实现的,相关的配置信息需要保存在软件可访问的配置寄存器中,如签名栈910的起始地址配置寄存器、签名栈910的容量配置寄存器、指向签名栈910的栈顶的寄存器等。当执行PUSHSIG指令时,根据当前指向签名栈910的栈顶的寄存器的值向系统存储器中写入当前基本块的运行时签名G和补偿签名D;当执行pop指令时,根据当前指向签名栈910的栈顶的寄存器的值从系统存储器中读出运行时签名G和补偿签名D,并对应地存入运行时签名寄存器810和补偿签名寄存器820中。通过软件设置来实现签名栈910,优点在于可以充分利用系统存储器的容量大的特点,以保存更多的签名信息;缺点在于需要在软件中预先对签名栈910进行配置。In other embodiments, the signature stack 910 can be realized by allocating a part of space in the system memory for saving signature information through software settings, and related configuration information needs to be saved in software-accessible configuration registers, such as the signature stack The initial address configuration register of 910, the capacity configuration register of signature stack 910, the register pointing to the top of the signature stack 910, and the like. When executing the PUSHSIG instruction, write the runtime signature G and compensation signature D of the current basic block into the system memory according to the value of the register pointing to the top of the stack 910 currently pointing to the signature stack 910; The value of the register at the top of the stack reads the runtime signature G and the compensation signature D from the system memory, and stores them into the runtime signature register 810 and the compensation signature register 820 correspondingly. The advantage of implementing the signature stack 910 through software settings is that the large capacity of the system memory can be fully utilized to store more signature information; the disadvantage is that the signature stack 910 needs to be pre-configured in the software.
在一些实施方式中,数据流校验指令中附带有静态校验签名,静态校验签名是基于编译器编译链接时当前基本块内所有指令的CRC签名值确定的。参考图10a所示,处理器700可以设置有CRC中间值寄存器1010。CRC中间值寄存器1010用于存储当前基本块运行至当前指令时所生成的中间CRC签名值。数据流校验电路730还用于计算当前基本块内所有指令的CRC签名值,得到动态校验签名;若当前基本块完成执行时的动态校验签名与静态校验签名一致,当前基本块通过数据流校验。In some implementations, the data stream verification instruction is accompanied by a static verification signature, and the static verification signature is determined based on the CRC signature values of all instructions in the current basic block when the compiler compiles and links. Referring to FIG. 10 a , the processor 700 may be provided with a CRC intermediate value register 1010 . The CRC intermediate value register 1010 is used to store the intermediate CRC signature value generated when the current basic block runs to the current instruction. The data flow verification circuit 730 is also used to calculate the CRC signature value of all instructions in the current basic block to obtain a dynamic verification signature; if the dynamic verification signature when the current basic block completes execution is consistent with the static verification signature, the current basic block passes Data flow verification.
具体地,当前基本块运行至当前指令时,可以计算该指令执行的动态校验签名值,作为中间CRC签名值存入CRC中间值寄存器1010中。在当前基本块中的所有指令执行结束后,CRC中间值寄存器1010中可以包括当前基本块内所有指令的动态校验签名值。Specifically, when the current basic block runs to the current instruction, the dynamic verification signature value executed by the instruction can be calculated and stored in the CRC intermediate value register 1010 as an intermediate CRC signature value. After the execution of all instructions in the current basic block is completed, the CRC intermediate value register 1010 may include dynamic check signature values of all instructions in the current basic block.
示例性地,参考图10b所示,数据流校验电路730可以与CRC中间值寄存器1010连接,数据流校验电路730还可以包括动态校验签名值计算电路1020和校验签名值比较电路1030。当基本块内的指令执行时,校验签名值比较电路1030可以接受当前正在执行的指令及指令码宽度指示信号,放入动态校验签名值计算电路1020中,动态校验签名值计算电路1020对放入的指令动态计算CRC签名值。在当前执行的指令不是CRCSIG指令时,将动态校验签名值计算电路1020计算的CRC签名值存入CRC中间值寄存器1010;在当前指令是CRCSIG指令时,将动态校验签名值计算电路1020计算的CRC签名值与CRCSIG指令中的操作数进行比较,如果一致则指令执行正常。Exemplarily, as shown in FIG. 10b, the data stream verification circuit 730 may be connected to the CRC intermediate value register 1010, and the data stream verification circuit 730 may also include a dynamic verification signature value calculation circuit 1020 and a verification signature value comparison circuit 1030 . When the instruction in the basic block is executed, the verification signature value comparison circuit 1030 can accept the currently executing instruction and the instruction code width indication signal, and put them into the dynamic verification signature value calculation circuit 1020, and the dynamic verification signature value calculation circuit 1020 The CRC signature value is dynamically calculated for the placed instruction. When the currently executed instruction is not a CRCSIG instruction, the CRC signature value calculated by the dynamic verification signature value calculation circuit 1020 is stored in the CRC intermediate value register 1010; when the current instruction is a CRCSIG instruction, the dynamic verification signature value calculation circuit 1020 is calculated. The CRC signature value is compared with the operand in the CRCSIG instruction, and if they are consistent, the instruction is executed normally.
在一些情况中,为了避免在调试过程中,通过上位机下发的软断电指令和其他调试指令被错误当做基本块的内部指令用于基本块内部指令的动态校验签名值的计算,从而干扰到基本块的数据流校验的结果,可以设置对上位机下发的调试指令的屏蔽机制,使上位机下发的调试指令不会参与到基本块的数据流校验过程中,从而使本说明书中加入扩展指令的程序可以支持各种调试动作而不会影响到数据流校验。In some cases, in order to avoid the soft power-off instruction issued by the host computer and other debugging instructions being mistakenly regarded as internal instructions of the basic block during the debugging process, they are used for the calculation of the dynamic verification signature value of the internal instructions of the basic block, thereby Interfering with the result of the data flow verification of the basic block, you can set a shielding mechanism for the debugging instructions issued by the host computer, so that the debugging instructions issued by the upper computer will not participate in the data flow verification process of the basic block, so that The programs added with extended instructions in this manual can support various debugging actions without affecting the data flow verification.
本说明书实施方式提供一种程序流监控装置,程序流对应有控制流和数据流;控制流包括若干基本块;基本块头部在编译阶段插入有控制流校验指令,以及基本块尾部在链接阶段插入有数据流校验指令。其中,数据流校验指令是在链接阶段对编译阶段中插入基本块尾部的占位指令进行替换而插入的。参考图11a所示,该程序流监控装置1100包括:控制流校验模块1110、数据流校验模块1120。The embodiment of this specification provides a program flow monitoring device. The program flow corresponds to a control flow and a data flow; the control flow includes several basic blocks; Stages are inserted with data flow verification instructions. Wherein, the data flow checking instruction is inserted in the linking stage by replacing the occupying instruction inserted at the end of the basic block in the compiling stage. Referring to FIG. 11 a , the program flow monitoring device 1100 includes: a control flow verification module 1110 and a data flow verification module 1120 .
控制流校验模块1110,用于在程序流运行到当前基本块的情况下,执行当前基本块内的控制流校验指令以校验控制流的正确性。The control flow verification module 1110 is configured to execute the control flow verification instruction in the current basic block to verify the correctness of the control flow when the program flow runs to the current basic block.
数据流校验模块1120,用于在当前基本块运行至数据流校验指令的情况下,确定当前基本块的所有指令的CRC签名值,以校验数据流的完整性。The data flow verification module 1120 is configured to determine the CRC signature values of all instructions of the current basic block to verify the integrity of the data flow when the current basic block runs to the data flow verification instruction.
在一些实施方式中,基本块分配有静态签名;若干基本块包括当前基本块、跳转至当前基本块的前驱基本块。控制流校验模块1110,还用于获取前驱基本块的补偿签名和前驱基本块的运行时签名;其中,补偿签名是基于前驱基本块的静态签名与前驱基准基本块的静态签名的异或计算结果确定的;前驱基准基本块为前驱基本块所处的控制级别上的基准基本块;确定当前基本块的静态签名和签名差;其中,签名差是基于前驱基准基本块的静态签名与当前基本块的静态签名进行异或计算的结果确定的;若前驱基本块的运行时签名、当前基本块的签名差、前驱基本块的补偿签名的异或计算结果等于当前基本块的静态签名,当前基本块通过控制流校验。In some embodiments, a basic block is assigned a static signature; several basic blocks include a current basic block, a predecessor basic block that jumps to the current basic block. The control flow verification module 1110 is also used to obtain the compensation signature of the predecessor basic block and the runtime signature of the predecessor basic block; wherein, the compensation signature is based on the XOR calculation of the static signature of the predecessor basic block and the static signature of the predecessor reference basic block The result is determined; the predecessor reference basic block is the reference basic block at the control level of the predecessor basic block; determine the static signature and signature difference of the current basic block; wherein, the signature difference is based on the static signature of the predecessor reference basic block and the current basic The XOR calculation result of the static signature of the block is determined; if the XOR calculation result of the runtime signature of the predecessor basic block, the signature difference of the current basic block, and the compensation signature of the predecessor basic block is equal to the static signature of the current basic block, the current basic block The block passes the control flow checksum.
在一些实施方式中,控制流校验模块1110,还用于基于签名差和当前基本块的静态签名,在当前基本块的头部插入有控制流校验指令;若前驱基本块的运行时签名、控制流校验指令中附带的签名差、前驱基本块的补偿签名的异或计算结果等于控制流校验指令中附带的静态签名,当前基本块通过控制流校验。In some implementations, the control flow verification module 1110 is further configured to insert a control flow verification instruction at the head of the current basic block based on the signature difference and the static signature of the current basic block; if the runtime signature of the predecessor basic block , the signature difference attached to the control flow verification instruction, and the XOR calculation result of the compensation signature of the predecessor basic block is equal to the static signature attached to the control flow verification instruction, and the current basic block passes the control flow verification.
在一些实施方式中,数据流校验指令中附带有静态校验签名,静态校验签名是基于编译器编译链接时当前基本块内所有指令的CRC签名值确定的。数据流校验模块1120,还用于计算当前基本块内所有指令的CRC签名值,得到动态校验签名;若当前基本块完成执行时的动态校验签名与静态校验签名一致,当前基本块通过数据流校验。In some implementations, the data stream verification instruction is accompanied by a static verification signature, and the static verification signature is determined based on the CRC signature values of all instructions in the current basic block when the compiler compiles and links. The data flow verification module 1120 is also used to calculate the CRC signature value of all instructions in the current basic block to obtain a dynamic verification signature; if the dynamic verification signature when the current basic block completes execution is consistent with the static verification signature, the current basic block Pass data flow check.
在一些实施方式中,控制流包括函数控制流,函数控制流包括入口基本块和出口基本块;入口基本块在编译阶段插入有签名信息保存指令,出口基本块在编译阶段插入有签名信息恢复指令。参考图11b所示,程序流监控装置1100还可以包括:签名压栈模块1130、签名弹栈模块1140。In some implementations, the control flow includes a function control flow, and the function control flow includes an entry basic block and an exit basic block; the entry basic block is inserted with a signature information saving instruction during the compilation phase, and the exit basic block is inserted with a signature information restoration instruction during the compilation phase . Referring to FIG. 11 b , the program flow monitoring apparatus 1100 may further include: a signature push module 1130 and a signature pop module 1140 .
签名压栈模块1130,用于在函数调用时执行签名信息保存指令,以支持运行时签名和补偿签名的压栈操作。The signature push module 1130 is configured to execute the signature information saving instruction when the function is called, so as to support the stack push operation of runtime signatures and compensation signatures.
签名弹栈模块1140,用于在调用的函数返回时执行签名信息恢复指令,以支持保存的运行时签名和补偿签名的弹栈操作。The signature stack popping module 1140 is configured to execute the signature information recovery instruction when the called function returns, so as to support the pop stack operation of the saved runtime signature and compensation signature.
本说明书实施方式提供一种程序代码编译装置,程序代码对应有程序控制流图,且程序控制流图中具有若干基本块。参考图12a所示,该程序代码编译装置1200包括:第一指令插入模块1210、占位指令替换模块1220。The embodiment of this specification provides a program code compiling device, the program code corresponds to a program control flow graph, and the program control flow graph has several basic blocks. Referring to FIG. 12 a , the program code compiling device 1200 includes: a first instruction insertion module 1210 and a placeholder instruction replacement module 1220 .
第一指令插入模块1210,用于在编译阶段,在基本块头部插入控制流校验指令,在基本块尾部插入占位指令。其中,控制流校验指令用于在程序代码对应的程序流运行到任意基本块时校验控制流的正确性。The first instruction inserting module 1210 is configured to insert a control flow checking instruction at the head of the basic block and insert a placeholder instruction at the end of the basic block during the compilation stage. Wherein, the control flow verification instruction is used to verify the correctness of the control flow when the program flow corresponding to the program code runs to any basic block.
占位指令替换模块1220,用于在链接阶段,将占位指令对应的指令码替换为数据流校验指令。其中,数据流校验指令用于在程序代码对应的程序流运行到任意基本块的数据流校验指令时,确定任意基本块的所有指令的CRC签名值,以校验数据流的完整性。The placeholder instruction replacement module 1220 is configured to replace the instruction code corresponding to the placeholder instruction with a data flow verification instruction in the linking stage. Wherein, the data flow verification instruction is used to determine the CRC signature value of all instructions of any basic block when the program flow corresponding to the program code runs to the data flow verification instruction of any basic block, so as to verify the integrity of the data flow.
在一些实施方式中,控制流包括函数控制流,函数控制流包括入口基本块和出口基本块。参考图12b所示,程序代码编译装置1200还可以包括:第二指令插入模块1230。In some implementations, the control flow includes a function control flow that includes an entry basic block and an exit basic block. Referring to FIG. 12 b , the program code compiling apparatus 1200 may further include: a second instruction inserting module 1230 .
第二指令插入模块1230,用于在编译阶段,在入口基本块插入签名信息保存指令,以及,在出口基本块插入签名信息恢复指令;其中,签名信息保存指令用于在函数调用时的运行时签名和调整签名的压栈操作,签名信息恢复指令用于在调用的函数返回之前的运行时签名和调整签名的弹栈操作。The second instruction insertion module 1230 is used to insert a signature information saving instruction in the entry basic block during the compilation phase, and insert a signature information restoration instruction in the exit basic block; wherein, the signature information preservation instruction is used for the runtime when the function is called The stack operation of signing and adjusting the signature, and the signature information recovery instruction is used for the pop-up operation of the runtime signature and adjusting the signature before the called function returns.
关于程序流监控装置和程序代码编译装置的具体限定可以参见上文中对于程序流监控方法和程序代码编译方法的限定,在此不再赘述。上述程序流监控装置和程序代码编译装置中的各个模块可全部或部分通过软件、硬件及其组合来实现。上述各模块可以硬件形式内嵌于或独立于计算机设备中的处理器中,也可以以软件形式存储于计算机设备中的存储器中,以便于处理器调用执行以上各个模块对应的操作。For the specific limitations of the program flow monitoring device and the program code compiling device, reference may be made to the limitations of the program flow monitoring method and the program code compiling method above, which will not be repeated here. Each module in the above-mentioned program flow monitoring device and program code compiling device can be fully or partially realized by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, and can also be stored in the memory of the computer device in the form of software, so that the processor can invoke and execute the corresponding operations of the above-mentioned modules.
本说明书实施方式还提供了一种计算机设备,参考图13所示,该计算机设备1300包括存储器1310和处理器1320,存储器1310存储有计算机程序1330,处理器1320执行计算机程序1330时实现前述任一项实施方式的程序流监控方法和/或程序代码编译方法的步骤。The embodiment of this specification also provides a computer device, as shown in FIG. The steps of the program flow monitoring method and/or the program code compiling method of the embodiment.
本说明书实施方式还提供了一种计算机可读存储介质,其上存储有计算机程序,计算机程序被处理器执行时实现前述任一项实施方式的程序流监控方法和/或程序代码编译方法的步骤。The embodiments of this specification also provide a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the program flow monitoring method and/or program code compiling method in any of the foregoing embodiments are implemented. .
根据本说明书的多个实施方式,通过基于扩展指令的软硬件结合的程序流监控方法,使得在无需对用户程序进行额外更改的情况下,可以实现全自动的程序流监控,且监控时延不超过一个基本块的指令数,时延很低,避免了指令执行发生异常后,后续指令继续执行的异常情况。According to multiple implementations of this specification, through the program flow monitoring method based on the combination of software and hardware based on extended instructions, full-automatic program flow monitoring can be realized without additional changes to the user program, and the monitoring time delay is short. The number of instructions exceeding one basic block has a very low delay, which avoids the abnormal situation that subsequent instructions continue to execute after an exception occurs in the execution of an instruction.
需要说明的是,在流程图中表示或在此以其他方式描述的逻辑和/或步骤,例如,可以被认为是用于实现逻辑功能的可执行指令的定序列表,可以具体实现在任何计算机可读介质中,以供指令执行系统、装置或设备(如基于计算机的系统、包括处理器的系统或其他可以从指令执行系统、装置或设备取指令并执行指令的系统)使用,或结合这些指令执行系统、装置或设备而使用。就本说明书而言,"计算机可读介质"可以是任何可以包含、存储、通信、传播或传输程序以供指令执行系统、装置或设备或结合这些指令执行系统、装置或设备而使用的装置。计算机可读介质的更具体的示例(非穷尽性列表)包括以下:具有一个或多个布线的电连接部(电子装置),便携式计算机盘盒(磁装置),随机存取存储器(RAM),只读存储器(ROM),可擦除可编辑只读存储器(EPROM或闪速存储器),光纤装置,以及便携式光盘只读存储器(CDROM)。另外,计算机可读介质甚至可以是可在其上打印所述程序的纸或其他合适的介质,因为可以例如通过对纸或其他介质进行光学扫描,接着进行编辑、解译或必要时以其他合适方式进行处理来以电子方式获得所述程序,然后将其存储在计算机存储器中。It should be noted that the logic and/or steps shown in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be embodied in any computer readable medium for use in an instruction execution system, apparatus, or device (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, apparatus, or device and execute instructions), or in combination with these Instructions are used to execute systems, devices, or equipment. For the purposes of this specification, a "computer-readable medium" may be any device that can contain, store, communicate, propagate or transmit a program for use in or in conjunction with an instruction execution system, device or device. More specific examples (non-exhaustive list) of computer-readable media include the following: electrical connection with one or more wires (electronic device), portable computer disk case (magnetic device), random access memory (RAM), Read Only Memory (ROM), Erasable and Editable Read Only Memory (EPROM or Flash Memory), Fiber Optic Devices, and Portable Compact Disc Read Only Memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program can be printed, as it may be possible, for example, by optically scanning the paper or other medium, followed by editing, interpreting, or other suitable processing if necessary. The program is processed electronically and stored in computer memory.
应当理解,本发明的各部分可以用硬件、软件、固件或它们的组合来实现。在上述实施方式中,多个步骤或方法可以用存储在存储器中且由合适的指令执行系统执行的软件或固件来实现。例如,如果用硬件来实现,和在另一实施方式中一样,可用本领域公知的下列技术中的任一项或他们的组合来实现:具有用于对数据信号实现逻辑功能的逻辑门电路的离散逻辑电路,具有合适的组合逻辑门电路的专用集成电路,可编程门阵列(PGA),现场可编程门阵列(FPGA)等。It should be understood that various parts of the present invention can be realized by hardware, software, firmware or their combination. In the embodiments described above, various steps or methods may be implemented by software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, it can be implemented by any one or combination of the following techniques known in the art: Discrete logic circuits, ASICs with suitable combinational logic gates, Programmable Gate Arrays (PGAs), Field Programmable Gate Arrays (FPGAs), etc.
在本说明书的描述中,参考术语“一个实施例”、“一些实施例”、“示例”、“具体示例”、或“一些示例”等的描述意指结合该实施例或示例描述的具体特征、结构、材料或者特点包含于本发明的至少一个实施例或示例中。在本说明书中,对上述术语的示意性表述不一定指的是相同的实施例或示例。而且,描述的具体特征、结构、材料或者特点可以在任何的一个或多个实施例或示例中以合适的方式结合。In the description of this specification, descriptions referring to the terms "one embodiment", "some embodiments", "example", "specific examples", or "some examples" mean that specific features described in connection with the embodiment or example , structure, material or characteristic is included in at least one embodiment or example of the present invention. In this specification, schematic representations of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials or characteristics described may be combined in any suitable manner in any one or more embodiments or examples.
此外,术语“第一”、“第二”仅用于描述目的,而不能理解为指示或暗示相对重要性或者隐含指明所指示的技术特征的数量。由此,限定有“第一”、“第二”的特征可以明示或者隐含地包括至少一个该特征。在本发明的描述中,“多个”的含义是至少两个,例如两个,三个等,除非另有明确具体的限定。In addition, the terms "first" and "second" are used for descriptive purposes only, and cannot be interpreted as indicating or implying relative importance or implicitly specifying the quantity of indicated technical features. Thus, the features defined as "first" and "second" may explicitly or implicitly include at least one of these features. In the description of the present invention, "plurality" means at least two, such as two, three, etc., unless otherwise specifically defined.
在本发明中,除非另有明确的规定和限定,术语“安装”、“相连”、“连接”、“固定”等术语应做广义理解,例如,可以是固定连接,也可以是可拆卸连接,或成一体;可以是机械连接,也可以是电连接;可以是直接相连,也可以通过中间媒介间接相连,可以是两个元件内部的连通或两个元件的相互作用关系,除非另有明确的限定。对于本领域的普通技术人员而言,可以根据具体情况理解上述术语在本发明中的具体含义。In the present invention, unless otherwise clearly specified and limited, terms such as "installation", "connection", "connection" and "fixation" should be understood in a broad sense, for example, it can be a fixed connection or a detachable connection , or integrated; it may be mechanically connected or electrically connected; it may be directly connected or indirectly connected through an intermediary, and it may be the internal communication of two components or the interaction relationship between two components, unless otherwise specified limit. Those of ordinary skill in the art can understand the specific meanings of the above terms in the present invention according to specific situations.
尽管上面已经示出和描述了本发明的实施例,可以理解的是,上述实施例是示例性的,不能理解为对本发明的限制,本领域的普通技术人员在本发明的范围内可以对上述实施例进行变化、修改、替换和变型。Although the embodiments of the present invention have been shown and described above, it can be understood that the above embodiments are exemplary and should not be construed as limiting the present invention, those skilled in the art can make the above-mentioned The embodiments are subject to changes, modifications, substitutions and variations.
Claims (27)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202310708268.XA CN116450402B (en) | 2023-06-15 | 2023-06-15 | Program flow monitoring method, compiling method, device, processor and computer equipment |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202310708268.XA CN116450402B (en) | 2023-06-15 | 2023-06-15 | Program flow monitoring method, compiling method, device, processor and computer equipment |
Publications (2)
| Publication Number | Publication Date |
|---|---|
| CN116450402A true CN116450402A (en) | 2023-07-18 |
| CN116450402B CN116450402B (en) | 2023-08-18 |
Family
ID=87134139
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202310708268.XA Active CN116450402B (en) | 2023-06-15 | 2023-06-15 | Program flow monitoring method, compiling method, device, processor and computer equipment |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN116450402B (en) |
Citations (11)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20060080014A1 (en) * | 2004-09-20 | 2006-04-13 | Andrea Di Palma | Monitoring method and system with trusted corrective actions |
| CN1905076A (en) * | 2006-06-30 | 2007-01-31 | 浙江大学 | Method for implementing dynamic storage error static detecting of embedded system |
| WO2008002350A1 (en) * | 2006-06-23 | 2008-01-03 | Microsoft Corporation | Securing software by enforcing data flow integrity |
| CN101763291A (en) * | 2009-12-30 | 2010-06-30 | 中国人民解放军国防科学技术大学 | Method for detecting error of program control flow |
| CN101944064A (en) * | 2010-10-12 | 2011-01-12 | 中国人民解放军国防科学技术大学 | Control flow error detection optimizing method based on reconstructed control flow graph |
| CN103345445A (en) * | 2013-07-02 | 2013-10-09 | 华中科技大学 | Security chip design method based on control flow detection and resistant to error injection attack |
| US20140053028A1 (en) * | 2012-08-16 | 2014-02-20 | International Business Machines Corporation | Anomaly detection at the level of run time data structures |
| CN104461886A (en) * | 2014-12-10 | 2015-03-25 | 深圳航天东方红海特卫星有限公司 | Control flow error detection method |
| WO2016087652A1 (en) * | 2014-12-05 | 2016-06-09 | Technische Universität Dresden | Method for processing data in order to ascertain if an error has occurred while running a program, and data processing arrangements for generating program code |
| US20170024304A1 (en) * | 2015-07-24 | 2017-01-26 | Infineon Technologies Ag | Method for determining an intergrity of an execution of a code fragment and a method for providing an abstracted representation of a program code |
| CN107038084A (en) * | 2016-02-03 | 2017-08-11 | 北京邮电大学 | A kind of program detecting method and device |
-
2023
- 2023-06-15 CN CN202310708268.XA patent/CN116450402B/en active Active
Patent Citations (13)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20060080014A1 (en) * | 2004-09-20 | 2006-04-13 | Andrea Di Palma | Monitoring method and system with trusted corrective actions |
| WO2008002350A1 (en) * | 2006-06-23 | 2008-01-03 | Microsoft Corporation | Securing software by enforcing data flow integrity |
| CN101473300A (en) * | 2006-06-23 | 2009-07-01 | 微软公司 | Securing software by enforcing data flow integrity |
| CN1905076A (en) * | 2006-06-30 | 2007-01-31 | 浙江大学 | Method for implementing dynamic storage error static detecting of embedded system |
| CN101763291A (en) * | 2009-12-30 | 2010-06-30 | 中国人民解放军国防科学技术大学 | Method for detecting error of program control flow |
| CN101944064A (en) * | 2010-10-12 | 2011-01-12 | 中国人民解放军国防科学技术大学 | Control flow error detection optimizing method based on reconstructed control flow graph |
| US20140053028A1 (en) * | 2012-08-16 | 2014-02-20 | International Business Machines Corporation | Anomaly detection at the level of run time data structures |
| CN103345445A (en) * | 2013-07-02 | 2013-10-09 | 华中科技大学 | Security chip design method based on control flow detection and resistant to error injection attack |
| WO2016087652A1 (en) * | 2014-12-05 | 2016-06-09 | Technische Universität Dresden | Method for processing data in order to ascertain if an error has occurred while running a program, and data processing arrangements for generating program code |
| CN104461886A (en) * | 2014-12-10 | 2015-03-25 | 深圳航天东方红海特卫星有限公司 | Control flow error detection method |
| US20170024304A1 (en) * | 2015-07-24 | 2017-01-26 | Infineon Technologies Ag | Method for determining an intergrity of an execution of a code fragment and a method for providing an abstracted representation of a program code |
| CN106372500A (en) * | 2015-07-24 | 2017-02-01 | 英飞凌科技股份有限公司 | Method for determining an intergrity of an execution of a code fragment and a method for providing an abstracted representation of a program code |
| CN107038084A (en) * | 2016-02-03 | 2017-08-11 | 北京邮电大学 | A kind of program detecting method and device |
Also Published As
| Publication number | Publication date |
|---|---|
| CN116450402B (en) | 2023-08-18 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| TWI803927B (en) | Processor, apparatus for protecting the processor, and method for protecting the processor | |
| US7596781B2 (en) | Register-based instruction optimization for facilitating efficient emulation of an instruction stream | |
| Werner et al. | Protecting the control flow of embedded processors against fault attacks | |
| JPH0444294B2 (en) | ||
| TW201734768A (en) | Branch instruction | |
| US9176739B2 (en) | System and method for checking run-time consistency for sequentially and non-sequentially fetched instructions | |
| US7036111B2 (en) | Code verification system and method | |
| TW201737060A (en) | Program loop control | |
| Zgheib et al. | A CFI verification system based on the RISC-V instruction trace encoder | |
| US8510713B1 (en) | Method and system for validating a disassembler | |
| CN119127309B (en) | Instruction processing system and instruction processing method | |
| CN116450402B (en) | Program flow monitoring method, compiling method, device, processor and computer equipment | |
| Benso et al. | Static analysis of SEU effects on software applications | |
| Michelland et al. | From low-level fault modeling (of a pipeline attack) to a proven hardening scheme | |
| Wojtczuk | UQBTng: A tool capable of automatically finding integer overflows in Win32 binaries | |
| CN105117201A (en) | Method and apparatus for data processing | |
| CN114489657B (en) | The system and process for compiling source code | |
| JP5821720B2 (en) | Startup control program and startup control method | |
| KR101968544B1 (en) | Method and apparatus for detecting vulnerability of software | |
| CN116521550A (en) | Compiler testing method, testing device and storage medium | |
| CN113761540A (en) | Verification method of BranchScope and its compiler protection method | |
| CN102411534A (en) | Breakpoint debugging method and debugger | |
| Vankeirsbilck et al. | Advancing control flow error detection techniques for embedded software using automated implementation and fault injection | |
| Schiffel et al. | Slice your bug: Debugging error detection mechanisms using error injection slicing | |
| API | Reference guide |
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 | ||
| GR01 | Patent grant | ||
| GR01 | Patent grant |