WO2023173915A1 - Resource control method and apparatus - Google Patents

Resource control method and apparatus Download PDF

Info

Publication number
WO2023173915A1
WO2023173915A1 PCT/CN2023/071405 CN2023071405W WO2023173915A1 WO 2023173915 A1 WO2023173915 A1 WO 2023173915A1 CN 2023071405 W CN2023071405 W CN 2023071405W WO 2023173915 A1 WO2023173915 A1 WO 2023173915A1
Authority
WO
WIPO (PCT)
Prior art keywords
code
controlled
instruction
instructions
access
Prior art date
Application number
PCT/CN2023/071405
Other languages
French (fr)
Chinese (zh)
Inventor
李国柱
Original Assignee
华为技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为技术有限公司 filed Critical 华为技术有限公司
Publication of WO2023173915A1 publication Critical patent/WO2023173915A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Storage Device Security (AREA)

Abstract

The embodiments of the present application relate to the technical field of terminal devices. Provided are a resource control method and apparatus. The method comprises: before running a program which needs to be controlled, performing scanning on instructions in the program; and when it is found by means of scanning that the program comprises only instructions in a preset instruction set, limiting a resource access range of the instructions in the program. In this way, during the execution of the instructions in the program, the program can only perform resource access in the limited resource access range, and cannot access resources outside of the resource access range, thereby realizing security control of the program, and ensuring secure access to resources.

Description

资源控制方法及装置Resource control method and device
本申请要求于2022年03月17日提交中国国家知识产权局、申请号为202210262411.2、申请名称为“资源控制方法及装置”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on March 17, 2022, with application number 202210262411.2 and application title "Resource Control Method and Device", the entire content of which is incorporated into this application by reference. .
技术领域Technical field
本申请实施例涉及终端设备技术领域,尤其涉及一种资源控制方法及装置。The embodiments of the present application relate to the technical field of terminal equipment, and in particular, to a resource control method and device.
背景技术Background technique
为了信息的安全,在软件中,可对信息进行隔离。在对信息隔离时,计算机可通过对不同的软件代码赋予不同的角色,并基于角色来分配资源,以构成该角色的一个实例,从而保证不同的实例仅能访问对应角色实例(例如进程,容器,虚拟机等)被分配的资源,以实现信息的安全访问。For information security, information can be isolated in the software. When isolating information, a computer can assign different roles to different software codes and allocate resources based on the role to form an instance of the role, thereby ensuring that different instances can only access corresponding role instances (such as processes, containers , virtual machines, etc.) are allocated resources to achieve secure access to information.
为了实现信息的安全访问,对于任意一个角色实例,处理器在执行该角色实例的每个指令时,都需要通过查询对该角色实例所分配的资源地址列表,来确定该指令的访问地址是否在该角色实例的访问权限内,以决定是否执行该指令,显然,这将降低处理器的程序执行效率。In order to achieve safe access to information, for any role instance, when the processor executes each instruction of the role instance, it needs to query the resource address list assigned to the role instance to determine whether the access address of the instruction is in Within the access rights of the role instance to decide whether to execute the instruction, obviously, this will reduce the program execution efficiency of the processor.
发明内容Contents of the invention
为了解决上述技术问题,本申请提供一种资源控制方法及装置。在该方法中,在受控模块运行前,通过检测其指令只包括预设指令集的指令以及限制其指令的资源访问范围,在运行受控模块时,则无需对其指令的访问权限进行检查,从而提升程序执行效率。In order to solve the above technical problems, this application provides a resource control method and device. In this method, before the controlled module is run, by detecting that its instructions only include instructions of the preset instruction set and limiting the resource access scope of its instructions, there is no need to check the access rights of its instructions when running the controlled module. , thereby improving program execution efficiency.
在一种可能的实施方式中,本申请实施例提供一种资源控制方法,应用于资源控制装置,所述资源控制装置包括控制模块和受控模块,所述资源控制装置中的指令被划分为受控类指令和非受控类指令。所述方法包括:在受控模块运行之前,所述控制模块对所述受控模块内的指令进行扫描,检测所述受控模块内的指令是否均为受控类指令;在所述控制模块检测到所述受控模块内的指令均为所述受控类指令时,所述控制模块对所述受控模块内的每个指令分配目标资源访问范围。In a possible implementation, the embodiment of the present application provides a resource control method, which is applied to a resource control device. The resource control device includes a control module and a controlled module. Instructions in the resource control device are divided into Controlled instructions and uncontrolled instructions. The method includes: before the controlled module is run, the control module scans the instructions in the controlled module to detect whether the instructions in the controlled module are all controlled instructions; in the control module When it is detected that the instructions in the controlled module are all the controlled class instructions, the control module allocates a target resource access range to each instruction in the controlled module.
示例性的,资源控制装置内运行的指令被划分为两类,一类为受控类指令,另一类为非受控类指令。For example, the instructions executed in the resource control device are divided into two categories: one type is controlled type instructions, and the other type is uncontrolled type instructions.
资源控制装置可为中央处理器(CPU),或者任意一种处理器等,本申请对于资源控制装置的实现方式不做限制,为了便于说明,以资源控制装置实现为CPU为例进行说明。The resource control device may be a central processing unit (CPU), or any kind of processor. This application does not limit the implementation of the resource control device. For convenience of explanation, the resource control device is implemented as a CPU as an example.
示例性的,受控类指令可包括本申请的自定义指令集,其中,自定义指令集中的指令在被CPU执行时,只可以访问特定的资源范围,而无法访问特定资源范围之外的资源。换言之,自定义指令集中的指令在被CPU执行时被限制了资源访问范围。By way of example, the controlled instructions may include the custom instruction set of the present application. When the instructions in the custom instruction set are executed by the CPU, they can only access a specific resource range and cannot access resources outside the specific resource range. . In other words, the instructions in the custom instruction set have limited resource access scope when executed by the CPU.
可选地,受控类指令还可包括对非受控模块的代码和数据的安全无影响的一些传统 的预设指令(例如加法指令、减法指令等不需要访问内存资源的指令),这些对非受控模块中的代码和数据的安全无影响的预设指令可根据需要而灵活设置,本申请对此不做限制。Optionally, controlled instructions may also include some traditional preset instructions that have no impact on the security of the code and data of uncontrolled modules (such as addition instructions, subtraction instructions, and other instructions that do not require access to memory resources). Preset instructions that have no impact on the security of codes and data in uncontrolled modules can be flexibly set according to needs, and this application does not limit this.
可选地,在受控类指令包括与资源无关的预设指令(即不需要访问资源的指令,以资源为内存资源为例)的情况下,则控制模块无需对该预设指令分配目标资源访问范围,控制模块只需对受控模块内需要访问内存资源的每个受控类指令分配目标资源访问范围。这里以访问的资源为内存资源为例。Optionally, in the case where the controlled class instructions include preset instructions that are independent of resources (that is, instructions that do not require access to resources, taking the resources as memory resources as an example), the control module does not need to allocate target resources to the preset instructions. Access scope, the control module only needs to allocate the target resource access scope to each controlled class instruction that needs to access memory resources in the controlled module. Here, the accessed resource is a memory resource as an example.
受控类指令在被CPU执行时,CPU只可以访问控制模块对该受控类指令所分配的特定的资源访问范围,而无法访问特定资源访问范围之外的资源。换言之,受控类指令的语义(或者说编码)决定受控类指令在被CPU执行时被限制了资源访问范围。那么CPU在执行每个受控类指令时,均无需通过查页表等方式来检查当前运行的该受控类指令中的资源访问地址是否在当前运行的程序的资源访问权限范围内,以提升CPU的指令处理效率。When a controlled instruction is executed by the CPU, the CPU can only access the specific resource access range allocated by the control module to the controlled instruction, and cannot access resources outside the specific resource access range. In other words, the semantics (or encoding) of controlled instructions determines the resource access scope of controlled instructions when executed by the CPU. Then when the CPU executes each controlled class instruction, it does not need to check whether the resource access address in the currently running controlled class instruction is within the resource access permission range of the currently running program by looking up the page table, etc., in order to improve CPU instruction processing efficiency.
而CPU在执行非受控类指令时,不需要对非受控类指令中的资源访问地址进行处理,可按照传统的方式,检测该非受控类指令中的资源访问地址是否是预先分配给该受控模块的可访问的地址(例如查页表等检测方式),如果该资源访问地址是可访问的地址,则CPU按照该资源访问地址访问资源,如果该资源访问地址是不可访问的地址,则CPU中断执行该受控模块内的指令。When the CPU executes an uncontrolled instruction, it does not need to process the resource access address in the uncontrolled instruction. It can detect whether the resource access address in the uncontrolled instruction is pre-allocated in the traditional way. The accessible address of the controlled module (such as page lookup table and other detection methods). If the resource access address is an accessible address, the CPU accesses the resource according to the resource access address. If the resource access address is an inaccessible address , the CPU interrupts execution of instructions in the controlled module.
那么将CPU内的指令被划分为受控类指令和非受控类指令,可按照上述两类指令的不同执行方式进行执行,使得CPU可支持运行不受资源访问范围限制的模块(例如非受控模块,其中,非受控模块可用于调用受控模块),以及支持运行受资源访问范围限制的模块(例如上述受控模块),丰富了CPU可执行的指令的类型,并实现了对运行在CPU中的受控模块的资源范围的访问控制。Then the instructions in the CPU are divided into controlled instructions and uncontrolled instructions, which can be executed according to the different execution methods of the above two types of instructions, so that the CPU can support running modules that are not restricted by the resource access range (such as non-controlled instructions). control module, in which uncontrolled modules can be used to call controlled modules), and support the operation of modules restricted by resource access scope (such as the above-mentioned controlled modules), enriching the types of instructions executable by the CPU, and realizing the control of running Resource-wide access control of controlled modules in the CPU.
本申请实施例中,受控类指令在被CPU执行时,CPU只可以访问对该受控类指令所分配的有限的资源访问范围,那么在受控模块运行之前,由控制模块扫描受控模块内的指令是否均为受控类指令,可在受控模块运行之前,通过控制模块来实现对受控模块内指令类型的检查。若控制模块检测到受控模块内的指令均为受控类指令,则可以确定受控模块在运行时不会访问超出其资源访问范围的资源,以确保受控模块在运行时的资源安全访问。也即在受控模块运行前,通过控制模块对受控模块进行指令扫描,可以确定该受控模块是否是安全的,在确定受控模块安全的情况下,才会运行该受控模块。本申请实施例能够在受控模块运行前,实现对第三方提供的受控模块的安全检查,无需在受控模块运行过程中,实时检查每个待执行的指令是否安全,提升了对受控模块的运行效率。能够在指令源头实现对受控模块的资源访问范围的限制,无需在执行指令过程中判断指令所请求访问的资源是否超出资源访问范围。In the embodiment of the present application, when a controlled class instruction is executed by the CPU, the CPU can only access a limited resource access range allocated to the controlled class instruction. Then, before the controlled module is run, the control module scans the controlled module. Whether the instructions in the controlled module are all controlled instructions, the control module can be used to check the type of instructions in the controlled module before running the controlled module. If the control module detects that the instructions in the controlled module are all controlled instructions, it can be sure that the controlled module will not access resources beyond its resource access scope at runtime to ensure safe access to resources by the controlled module at runtime. . That is, before the controlled module is run, the control module performs an instruction scan on the controlled module to determine whether the controlled module is safe. The controlled module will be run only when it is determined that the controlled module is safe. The embodiments of this application can realize the security check of the controlled module provided by a third party before the controlled module is run. There is no need to check in real time whether each instruction to be executed is safe during the running of the controlled module, which improves the security of the controlled module. module operating efficiency. It is possible to limit the resource access scope of the controlled module at the source of the instruction, without having to judge whether the resource requested by the instruction exceeds the resource access scope during the execution of the instruction.
示例性的,以内存资源为例,在控制模块对所述受控模块内的每个指令分配目标资源访问范围时,控制模块可向CPU发送非受控类指令,CPU在执行该非受控类指令时,可对受控模块内的每个受控类指令,在内存中分配可访问的目标地址空间,以及确定该目标地址空间的目标地址参数。此外,控制模块还可向CPU发送非受控类指令,CPU在执行 该非受控类指令时,可将上述目标地址参数写入存储单元。从而实现对受控模块内的每个指令的目标资源访问范围的分配。For example, taking memory resources as an example, when the control module allocates a target resource access range to each instruction in the controlled module, the control module can send an uncontrolled instruction to the CPU, and the CPU executes the uncontrolled instruction. For each controlled class instruction in the controlled module, an accessible target address space can be allocated in the memory, and the target address parameters of the target address space can be determined. In addition, the control module can also send uncontrolled instructions to the CPU. When the CPU executes the uncontrolled instructions, the above target address parameters can be written into the storage unit. This achieves the allocation of the target resource access range for each instruction within the controlled module.
本实施例中,通过CPU执行非受控类指令来对受控模块内的每个受控类指令分配目标资源访问范围,其目的在于,如果受控类指令在被CPU执行时,CPU可对受控模块分配目标资源访问范围,那么受控模块就可通过发送通过扫描确认的受控类指令至CPU,来使CPU修改对受控模块内每条指令所分配的目标资源访问范围。例如,受控模块将目标资源访问范围修改为控制模块不允许受控模块访问的资源访问范围,使得受控模块的资源访问范围无法受控制模块所控制。从而无法达到通过控制模块来限制受控模块的目标资源访问范围的目的,使得受控模块容易访问到敏感内存资源(例如存储有用户账号数据等数据的内存空间)。那么本申请实施例中,控制模块作为对受控模块的资源访问范围的限制方,CPU可通过执行控制模块内的非受控类指令,来对受控模块内的每个受控类指令分配目标资源访问范围,使得受控模块的资源访问范围是受控制模块控制的,受控模块自身无法修改其自身可访问的目标资源访问范围,从而确保受控模块对资源的安全访问。In this embodiment, the target resource access range is allocated to each controlled class instruction in the controlled module by the CPU executing the uncontrolled class instruction. The purpose is that when the controlled class instruction is executed by the CPU, the CPU can The controlled module allocates the target resource access range, and then the controlled module can send controlled class instructions confirmed by scanning to the CPU, so that the CPU can modify the target resource access range allocated to each instruction in the controlled module. For example, the controlled module modifies the target resource access scope to a resource access scope that the control module does not allow the controlled module to access, so that the resource access scope of the controlled module cannot be controlled by the control module. Therefore, the purpose of limiting the target resource access range of the controlled module through the control module cannot be achieved, making it easy for the controlled module to access sensitive memory resources (such as memory space that stores user account data and other data). In this embodiment of the present application, the control module serves as a limiter on the resource access scope of the controlled module. The CPU can allocate each controlled class instruction in the controlled module by executing the uncontrolled class instructions in the control module. The target resource access scope makes the resource access scope of the controlled module controlled by the control module. The controlled module itself cannot modify the target resource access scope that it can access, thereby ensuring the safe access of resources by the controlled module.
示例性的,存储单元可内置于CPU,或者,存储单元为独立于CPU之外的外置芯片中的存储单元,CPU可访问外置芯片中的存储单元。For example, the storage unit may be built into the CPU, or the storage unit may be a storage unit in an external chip independent of the CPU, and the CPU may access the storage unit in the external chip.
示例性的,存储单元可以是寄存器(包括但不限于段寄存器等)、磁头等。For example, the storage unit may be a register (including but not limited to a segment register, etc.), a magnetic head, etc.
在控制模块写入目标地址参数至CPU中的存储单元后,非受控模块可调用受控模块,以使受控模块运行在CPU中。After the control module writes the target address parameter to the storage unit in the CPU, the uncontrolled module can call the controlled module so that the controlled module runs in the CPU.
以具体示例来说明:To illustrate with specific examples:
应用1(非受控模块的一个示例)中需要运行第三方开发的插件(受控模块的一个示例),以实现应用1的一项新功能,那么为了确保插件不会访问应用1中的敏感信息(例如用户信息等),应用1可内嵌本申请提供的控制程序(控制模块的一个示例)。Application 1 (an example of an uncontrolled module) needs to run a plug-in developed by a third party (an example of a controlled module) to implement a new function of Application 1, then in order to ensure that the plug-in does not access sensitive data in Application 1 Information (such as user information, etc.), application 1 can embed the control program provided by this application (an example of a control module).
该控制程序可在应用1运行该插件之前,扫描插件中的指令,在扫描到插件中的指令均为受控类指令时,说明插件是安全的。然后,控制程序可对插件中的每个受控类指令设置内存资源访问范围,控制程序具体可通过非受控类指令,来将限制内存访问空间的目标地址参数写入本申请的CPU中的段寄存器(存储单元的一个示例)。The control program can scan the instructions in the plug-in before application 1 runs the plug-in. When the scanned instructions in the plug-in are all controlled instructions, it means that the plug-in is safe. Then, the control program can set the memory resource access range for each controlled class instruction in the plug-in. The control program can specifically write the target address parameters that limit the memory access space into the CPU of this application through uncontrolled class instructions. Segment register (an example of a storage unit).
其中,将目标地址参数写入段寄存器的指令可为非受控类指令,而不可以是受控类指令,这样,可以避免上述插件在运行过程中,通过CPU执行受控类指令来修改段寄存器内的目标地址参数的情况。而目标地址参数可用于确定插件内的受控类指令可访问的目标地址空间,这样可以避免插件在运行过程中,篡改CPU内对应于该插件的指令的段寄存器内的数据,造成插件访问该目标地址空间之外的资源的情况。Among them, the instruction to write the target address parameter into the segment register can be an uncontrolled instruction, but not a controlled instruction. In this way, the above plug-in can be prevented from modifying the segment through the CPU executing controlled instructions during operation. The target address parameter in the register. The target address parameter can be used to determine the target address space accessible to controlled instructions in the plug-in. This can prevent the plug-in from tampering with the data in the segment register corresponding to the plug-in instruction in the CPU during operation, causing the plug-in to access the In the case of resources outside the target address space.
然后,应用1可以调用插件,使得插件运行后,向CPU发送受控类指令,以使CPU执行该受控类指令,对受控类指令中的原内存访问地址,按照段寄存器中的地址参数,在所限制的内存访问空间中确定目标内存访问地址,并在内存中访问该目标内存访问地址,以实现内存资源的限制访问。Then, application 1 can call the plug-in, so that after the plug-in is run, it sends a controlled class instruction to the CPU, so that the CPU executes the controlled class instruction, and accesses the original memory address in the controlled class instruction according to the address parameter in the segment register. , determine the target memory access address in the restricted memory access space, and access the target memory access address in the memory to achieve restricted access to memory resources.
这样,应用1在调用第三方开发的插件时,能够在使用插件的功能的同时,确保插件只可以在限制的内存地址空间中访问数据资源和代码资源,确保插件对应用1的资源的安全访问。In this way, when Application 1 calls a plug-in developed by a third party, it can use the functions of the plug-in while ensuring that the plug-in can only access data resources and code resources in a restricted memory address space, ensuring that the plug-in has safe access to Application 1's resources. .
在一种可能的实施方式中,所述资源控制装置还包括处理模块,所述控制模块对所述受控模块内的每个指令分配目标资源访问范围之后,所述方法还包括:所述处理模块对第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源;其中,所述第一访问地址为所述受控模块内当前运行的受控类指令中的资源访问地址;其中,所述处理后的第一访问地址,在所述当前运行的受控类指令对应的所述目标资源访问范围内。In a possible implementation, the resource control device further includes a processing module. After the control module allocates a target resource access range to each instruction in the controlled module, the method further includes: the processing The module processes the first access address and accesses the target resource according to the processed first access address; wherein the first access address is the resource access address in the controlled class instruction currently running in the controlled module; Wherein, the processed first access address is within the target resource access range corresponding to the currently running controlled class instruction.
示例性的,在受控模块运行过程中,处理模块可执行受控模块当前运行的受控类指令(例如目标指令),处理模块在执行该目标指令时,可将目标指令中的原内存访问地址映射至对该目标指令分配的目标地址空间内,得到该原内存访问地址在该目标地址空间中对应的目标内存访问地址;处理模块按照该目标内存访问地址来访问内存资源。For example, during the operation of the controlled module, the processing module can execute the controlled class instruction (such as the target instruction) currently executed by the controlled module. When executing the target instruction, the processing module can access the original memory in the target instruction. The address is mapped to the target address space allocated to the target instruction, and the target memory access address corresponding to the original memory access address in the target address space is obtained; the processing module accesses memory resources according to the target memory access address.
示例性的,处理模块可执行接收到的目标指令,在存储单元中读取对该受控模块内的该目标指令(一个受控类指令)所设置的目标地址参数,并按照预设算法,基于目标地址参数和目标指令中的原内存访问地址,来在该目标指令对应的目标地址空间中确定目标内存访问地址;处理模块在目标内存访问地址处访问内存资源。For example, the processing module can execute the received target instruction, read the target address parameters set for the target instruction (a controlled class instruction) in the controlled module in the storage unit, and follow the preset algorithm, Based on the target address parameter and the original memory access address in the target instruction, the target memory access address is determined in the target address space corresponding to the target instruction; the processing module accesses the memory resource at the target memory access address.
示例性的,在原内存访问地址超出目标地址空间时,则处理模块可按照该目标地址参数来将原内存访问地址的部分高位置零,使得处理后的目标内存访问地址在目标地址空间内。简单来描述,例如目标地址空间为1至100,原内存访问地址为1000,则可将原内存访问地址的地址长度缩小10倍,得到目标内存访问地址100,使得目标内存访问地址在目标地址空间内。For example, when the original memory access address exceeds the target address space, the processing module can set part of the high bits of the original memory access address to zero according to the target address parameter, so that the processed target memory access address is within the target address space. To describe it simply, for example, the target address space is 1 to 100 and the original memory access address is 1000. Then the address length of the original memory access address can be reduced by 10 times to obtain the target memory access address 100, so that the target memory access address is in the target address space. Inside.
在本申请实施例中,为了能够使目标指令在被处理模块执行时,只可以访问对该目标指令设定的目标地址空间内的资源,处理模块按照该目标指令的编码,在执行该目标指令时,可按照上述算法对原内存访问地址进行处理(例如多个高位地址置为零)。那么不论目标指令中的原内存访问地址是否在目标地址空间内,处理后的目标内存访问地址均在目标地址空间内。In the embodiment of the present application, in order to enable the target instruction to only access the resources in the target address space set for the target instruction when it is executed by the processing module, the processing module executes the target instruction according to the encoding of the target instruction. When, the original memory access address can be processed according to the above algorithm (for example, multiple high-order addresses are set to zero). Then regardless of whether the original memory access address in the target instruction is in the target address space, the processed target memory access address is in the target address space.
例如,即便受控模块的一段代码请求调用目标地址空间之外的某个地址1处的代码资源,处理模块在执行该段代码对应的受控类指令时,则可将地址1按照上述算法映射到该目标地址空间内的地址2。那么无论受控模块请求访问的资源的地址在哪里,处理模块在执行受控模块的目标指令时,都可在目标地址空间内找到一个与该原内存访问地址对应的目标内存访问地址,并访问该目标内存访问地址处的资源作为对原内存访问地址的访问结果。For example, even if a piece of code in the controlled module requests to call a code resource at a certain address 1 outside the target address space, when the processing module executes the controlled class instruction corresponding to the piece of code, address 1 can be mapped according to the above algorithm. to address 2 within the target address space. Then no matter where the address of the resource requested to be accessed by the controlled module is, when the processing module executes the target instruction of the controlled module, it can find a target memory access address corresponding to the original memory access address in the target address space, and access The resource at the target memory access address is used as the access result to the original memory access address.
这样,本申请实施例的处理模块在执行目标指令时,并不是直接按照该目标指令的原内存访问地址进行资源访问,而是找到该原内存访问地址在目标地址空间内映射的目标内存访问地址,并在目标内存地址处访问子。能够从目标指令的访问地址的维度进行受控模块可访问资源的限制,目标内存访问地址所指向的资源是准确且唯一的,那么这种精细化维度的资源限制方式,能够确保受控模块的任意指令所访问的资源是已经预先确定好的唯一资源。In this way, when the processing module of the embodiment of the present application executes the target instruction, it does not directly access the resource according to the original memory access address of the target instruction, but finds the target memory access address mapped by the original memory access address in the target address space. , and access the sub at the target memory address. The resources accessible to the controlled module can be restricted from the dimension of the access address of the target instruction. The resource pointed to by the target memory access address is accurate and unique. Then this refined dimension of resource restriction can ensure the access of the controlled module. The resources accessed by any instruction are the only resources that have been predetermined.
在一种可能的实施方式中,在所述控制模块检测到所述受控模块内的指令不均为所述受控类指令之后,该方法还包括:在所述控制模块检测到所述受控模块包括与第一预 设指令集匹配的第一指令、且所述第一指令为用于访问第一预设代码资源的指令时,所述控制模块对所述受控模块内的每个所述受控类指令,分配所述目标资源访问范围;其中,所述非受控类指令包括所述第一预设指令集;所述第一预设代码资源位于第一资源访问范围,且所述第一资源访问范围与所述目标资源访问范围不同。In a possible implementation, after the control module detects that the instructions in the controlled module are not uniformly the controlled class instructions, the method further includes: after the control module detects that the controlled module When the control module includes a first instruction that matches the first preset instruction set and the first instruction is an instruction for accessing a first preset code resource, the control module controls each of the controlled modules. The controlled class instructions allocate the target resource access range; wherein the uncontrolled class instructions include the first preset instruction set; the first preset code resource is located in the first resource access range, and The first resource access scope is different from the target resource access scope.
示例性的,非受控类指令可包括第一预设指令集和第二预设指令集,其中,第一预设指令集内的指令可为部分或全部的非受控类的跳转指令(jump指令)。第二预设指令集内的指令可为部分或全部的非受控类的跳转指令,本申请对此不做限制。For example, the uncontrolled class instructions may include a first preset instruction set and a second preset instruction set, wherein the instructions in the first preset instruction set may be part or all of the uncontrolled class jump instructions. (jump command). The instructions in the second preset instruction set may be part or all of the uncontrolled jump instructions, and this application does not limit this.
示例性的,第一预设指令集与第二预设指令集可以相同或不同,两个指令集可以存在相同的指令。For example, the first preset instruction set and the second preset instruction set may be the same or different, and the same instructions may exist in the two instruction sets.
示例性的,对于非受控类的跳转指令(即jump指令)也可以分为多种编码的jump指令,不同编码的jump指令虽然在被CPU执行时都可以实现代码资源的访问,但是在对代码资源的访问方式上可存在区别,本申请对此不做限制。那么这里的第一预设指令集和第二预设指令集内的指令则均为jump指令,但是这两个预设指令集之间的jump指令的编码可以存在区别。例如第一预设指令集中的指令为jump1指令至jump5指令。第二预设指令集中的指令为jump6指令至jump10指令。For example, uncontrolled jump instructions (i.e. jump instructions) can also be divided into jump instructions with multiple encodings. Although jump instructions with different encodings can access code resources when executed by the CPU, they cannot be accessed when executed by the CPU. There may be differences in the access methods to code resources, and this application does not impose restrictions on this. Then, the instructions in the first preset instruction set and the second preset instruction set here are all jump instructions, but the encoding of the jump instructions between these two preset instruction sets may be different. For example, the instructions in the first preset instruction set are jump1 instructions to jump5 instructions. The instructions in the second default instruction set are jump6 instructions to jump10 instructions.
在本实施例中,为了能够使受控代码可以获得一些非受控代码的服务(比如一个受控的插件执行,需要获得系统的时间),那么只需要确保受控代码中调用外部服务的非受控类指令,所访问的外部代码资源(即非受控代码中的代码资源)都是经过控制代码检查确认可以访问的第一预设代码资源,那么这些非受控类指令可以允许存在于受控代码中,这样,相比于添加代理代码的方案,本实施例在调用非受控代码中的外部函数时,只需要进行一次跳转,就可以实现受控代码对外部函数的调用。In this embodiment, in order to enable the controlled code to obtain some services of uncontrolled code (such as a controlled plug-in execution that needs to obtain the system time), it is only necessary to ensure that the controlled code calls the external service. For controlled instructions, the external code resources accessed (that is, code resources in uncontrolled code) are the first preset code resources that have been confirmed to be accessible through control code inspection, then these uncontrolled instructions can be allowed to exist in In the controlled code, in this way, compared with the solution of adding proxy code, in this embodiment, when calling an external function in the uncontrolled code, only one jump is needed to realize the call of the external function by the controlled code.
示例性的,控制代码通过处理模块在检测到受控代码包括非受控类的跳转指令,即与第一预设指令集匹配的第一指令,例如jump1指令至jump5指令中的至少一个指令,则可进一步检测第一指令的跳转目标是否满足预设条件,在受控代码中的每个第一指令的跳转目标均满足预设条件时,则说明所述第一指令为用于访问第一预设代码资源的指令。那么控制代码允许受控代码运行,否则提示出错,拒绝运行。Exemplarily, the control code detects through the processing module that the controlled code includes an uncontrolled jump instruction, that is, the first instruction that matches the first preset instruction set, such as at least one instruction from the jump1 instruction to the jump5 instruction. , then it can be further detected whether the jump target of the first instruction satisfies the preset condition. When the jump target of each first instruction in the controlled code satisfies the preset condition, it means that the first instruction is used for Instructions to access the first preset code resource. Then the control code allows the controlled code to run, otherwise it prompts an error and refuses to run.
示例性的,第一预设代码资源为控制代码,或者非受控代码,允许受控代码访问的位于目标资源访问范围之外的代码资源。For example, the first preset code resource is a control code, or an uncontrolled code, which is a code resource that is outside the access scope of the target resource and is allowed to be accessed by the controlled code.
第一预设代码资源为位于第一资源访问范围内的代码资源。第一资源访问范围(例如第四地址空间)的访问地址,与目标资源访问范围(即上述第一地址空间和第二地址空间)的访问地址不重叠。The first preset code resource is a code resource located within the first resource access range. The access address of the first resource access range (for example, the fourth address space) does not overlap with the access address of the target resource access range (that is, the first address space and the second address space).
示例性的,第一预设代码资源可以是第一预设函数。第一预设函数可以是一个或多个函数,本申请对此不做限制,那么该第一预设函数可为所述控制模块允许所述受控模块访问的、且不属于第一地址空间和第二地址空间的外部函数。For example, the first preset code resource may be a first preset function. The first preset function may be one or more functions. This application does not limit this. Then the first preset function may be one that the control module allows the controlled module to access and does not belong to the first address space. and external functions in the second address space.
示例性的,所述第一指令为用于访问所述第一预设函数的第一跳转类指令(例如jump1指令至jump5指令中的至少一个指令);Exemplarily, the first instruction is a first jump instruction (for example, at least one instruction from the jump1 instruction to the jump5 instruction) used to access the first preset function;
示例性的,受控代码中每个第一指令均为用于访问第一预设代码资源的指令,则控制代码可确定受控代码中每个第一指令的跳转目标满足预设条件,允许受控代码运行。For example, each first instruction in the controlled code is an instruction used to access the first preset code resource, then the control code can determine that the jump target of each first instruction in the controlled code satisfies the preset condition, Allow controlled code to run.
示例性的,所述第一预设代码资源包括第一预设函数,其中,所述第一预设函数为所述控制模块允许所述受控模块访问的函数。Exemplarily, the first preset code resource includes a first preset function, where the first preset function is a function that the control module allows the controlled module to access.
例如受控代码中每个jump指令所跳转至的外部函数均为第一预设函数,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。再如受控代码中每个jump指令的跳转地址(例如第四访问地址)均为第二预设地址,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。其中,第二预设地址为控制代码允许受控代码访问的第二预设函数的地址。For example, the external function to which each jump instruction in the controlled code jumps is the first preset function, then it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. Furthermore, if the jump address (for example, the fourth access address) of each jump instruction in the controlled code is the second preset address, it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. The second preset address is the address of the second preset function that the control code allows the controlled code to access.
在本申请实施例中,控制代码在扫描受控代码中的指令时,如果检测到非受控类指令,且非受控类指令均为与第一预设指令集匹配的非受控类的跳转指令(即第一指令),并且非受控类的跳转指令(第一指令)的跳转目标满足预设条件,例如受控代码中每个jump指令所跳转至的代码资源均为第一预设代码资源,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。那么控制代码可以允许受控代码运行。本实施例中受控代码在访问外部函数时,只需要进行一次外部跳转,不需要通过代理代码进行二次跳转,受控代码的执行速度更快。In the embodiment of the present application, when the control code detects uncontrolled instructions when scanning instructions in the controlled code, and the uncontrolled instructions are all uncontrolled instructions that match the first preset instruction set, Jump instruction (i.e., the first instruction), and the jump target of the uncontrolled jump instruction (the first instruction) satisfies the preset conditions, for example, the code resource to which each jump instruction in the controlled code jumps is As the first preset code resource, it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. The control code can then allow the controlled code to run. In this embodiment, when the controlled code accesses the external function, it only needs to perform one external jump and does not need to perform a second jump through the proxy code. The execution speed of the controlled code is faster.
在一种可能的实施方式中,所述第一预设代码资源包括第一预设函数,所述第一指令为用于访问所述第一预设函数的第一跳转类指令;In a possible implementation, the first preset code resource includes a first preset function, and the first instruction is a first jump instruction used to access the first preset function;
其中,所述第一预设函数为所述控制模块允许所述受控模块访问的函数。Wherein, the first preset function is a function that the control module allows the controlled module to access.
在本申请实施例中,控制代码可在通过处理模块扫描受控代码的指令之后,在检测到受控代码中的指令不均为受控类指令时,控制代码通过处理模块检测到受控代码包括的非受控类指令均为第一预设指令集中的第一指令,第一指令为用于访问第一预设函数的第一类跳转指令(例如short_jump指令)。控制代码在对受控代码扫描时,可实现对受控代码中非受控类指令的检测,检测非受控类指令均为非受控类的跳转指令,并且每个非受控类的跳转指令的跳转目标均为第一预设函数,则可以确保受控代码在访问第一地址空间和第二地址空间之外的代码资源时,所访问的代码资源是控制代码指定的第一预设函数,以确保受控代码对外部函数的限制性访问。本实施例中受控代码在访问外部函数时,只需要进行一次外部跳转,不需要通过代理代码进行二次外部跳转,CPU对受控代码的执行速度更快。In the embodiment of the present application, after the control code scans the instructions of the controlled code through the processing module, when it is detected that the instructions in the controlled code are not uniformly controlled instructions, the control code detects the controlled code through the processing module The included uncontrolled instructions are all first instructions in the first preset instruction set, and the first instructions are first type jump instructions (such as short_jump instructions) used to access the first preset function. When the control code scans the controlled code, it can detect the uncontrolled instructions in the controlled code. It detects that the uncontrolled instructions are all jump instructions of the uncontrolled class, and each uncontrolled class instruction The jump targets of the jump instructions are all the first preset functions, which can ensure that when the controlled code accesses code resources outside the first address space and the second address space, the code resources accessed are the third ones specified by the control code. A default function to ensure restricted access to external functions from controlled code. In this embodiment, when the controlled code accesses the external function, it only needs to perform one external jump and does not need to perform two external jumps through the proxy code. The CPU executes the controlled code faster.
在一种可能的实施方式中,所述受控模块内的所述受控类指令包括用于访问代码资源的第一类指令和用于访问数据资源的第二类指令,所述控制模块对所述受控模块内的每个所述受控类指令,分配所述目标资源访问范围,包括:所述控制模块对所述第一类指令分配第二资源访问范围;所述控制模块对所述第二类指令分配第三资源访问范围;其中,所述第二资源访问范围和所述第三资源访问范围之间的资源访问地址不同。In a possible implementation, the controlled class instructions in the controlled module include a first class of instructions for accessing code resources and a second class of instructions for accessing data resources, and the control module Allocating the target resource access range to each controlled class instruction in the controlled module includes: the control module allocates a second resource access range to the first class instruction; the control module assigns the target resource access range to the first class instruction; The second type of instruction allocates a third resource access scope; wherein the resource access addresses between the second resource access scope and the third resource access scope are different.
示例性的,控制代码(控制模块的一个示例)在对受控代码(受控模块的一个示例)中的第一类指令分配第二资源访问范围时,控制代码可对受控代码中的每个第一类指令分配第一地址空间,以及与该第一地址空间对应的第一地址参数。Exemplarily, when the control code (an example of a control module) allocates a second resource access scope to a first type of instruction in the controlled code (an example of a controlled module), the control code may assign the second resource access scope to each instruction in the controlled code. A first type instruction allocates a first address space and a first address parameter corresponding to the first address space.
其中,第一地址空间用于存储受控代码中受控类指令可访问的代码资源。The first address space is used to store code resources accessible to controlled class instructions in the controlled code.
示例性的,第一地址参数可用于确定第一地址空间的地址范围。For example, the first address parameter may be used to determine the address range of the first address space.
示例性的,控制代码在对受控代码中的第一类指令分配第三资源访问范围时,控制代码可对受控代码中的每个第二类指令分配第二地址空间,以及与该第二地址空间对应的第二地址参数。For example, when the control code allocates the third resource access scope to the first type of instructions in the controlled code, the control code can allocate the second address space to each second type of instruction in the controlled code, and communicate with the third type of instruction. The second address parameter corresponding to the second address space.
其中,第二地址空间用于存储受控代码中受控类指令可访问的数据资源。The second address space is used to store data resources accessible to controlled class instructions in the controlled code.
示例性的,第二地址参数可用于确定第二地址空间的地址范围。For example, the second address parameter may be used to determine the address range of the second address space.
示例性的,第一地址参数可包括code_base参数和code_limit参数。For example, the first address parameter may include a code_base parameter and a code_limit parameter.
其中,code_base参数可表示第一地址空间的起始地址(即首地址),code_limit参数可以是第一地址空间的地址范围参数(可理解为第一地址空间的长度),包括但不限于空间大小,或控制空间大小的二进制掩码等。Among them, the code_base parameter can represent the starting address of the first address space (i.e., the first address), and the code_limit parameter can be the address range parameter of the first address space (can be understood as the length of the first address space), including but not limited to the space size. , or a binary mask to control the size of the space, etc.
示例性的,控制代码对受控代码中的第一类指令所分配的第一地址空间可以是连续或者不连续的地址空间,本申请对此不做限制。For example, the first address space allocated by the control code to the first type of instruction in the controlled code may be a continuous or discontinuous address space, and this application does not limit this.
示例性的,第二地址参数可包括data_base参数和data_limit参数。For example, the second address parameter may include a data_base parameter and a data_limit parameter.
其中,data_base参数可表示第二地址空间的起始地址(即首地址),data_limit参数可以是第二地址空间的地址范围参数(可理解为第二地址空间的长度),包括但不限于空间大小,或控制空间大小的二进制掩码等。Among them, the data_base parameter can represent the starting address of the second address space (i.e., the first address), and the data_limit parameter can be the address range parameter of the second address space (can be understood as the length of the second address space), including but not limited to the space size. , or a binary mask to control the size of the space, etc.
示例性的,控制代码,对受控代码中的第二类指令所分配的第二地址空间可以是连续或者不连续的地址空间,本申请对此不做限制。For example, the second address space allocated by the control code to the second type of instructions in the controlled code may be a continuous or discontinuous address space, and this application does not limit this.
可选地,控制代码可将code_base参数和code_limit参数分别写入处理模块中的两个寄存器内,例如寄存器1和寄存器2,以及将data_base参数和data_limit参数分别写入处理模块中的另外两个寄存器内,例如寄存器3和寄存器4。Optionally, the control code can write the code_base parameter and the code_limit parameter respectively into two registers in the processing module, such as register 1 and register 2, and write the data_base parameter and data_limit parameter into two other registers in the processing module respectively. within, such as register 3 and register 4.
也就是说,第一地址参数和第二地址参数所对应的存储单元不同。That is to say, the storage units corresponding to the first address parameter and the second address parameter are different.
示例性的,上述寄存器可以是段寄存器,但是,本申请对于写入有第一地址参数或第二地址参数的存储单元的类型并不限制于寄存器,可以是处理模块内部的任何硬件存储单元,可选地,也可以是独立于处理模块之外的芯片内的硬件存储单元。For example, the above-mentioned register can be a segment register. However, this application is not limited to the type of storage unit in which the first address parameter or the second address parameter is written. It can be any hardware storage unit inside the processing module. Alternatively, it may also be a hardware storage unit in a chip that is independent of the processing module.
在本申请实施例中,第一地址空间和第二地址空间之间的地址不同。受控代码可通过第一类指令,来访问内存中存储在第一地址空间内的代码,以及通过第二类指令,来访问内存中存储在第二地址空间中的数据。如果第一地址空间和第二地址空间存在重叠的地址,那么重叠的地址可存储代码以及数据,那么受控代码可在控制代码扫描其指令均为受控类指令之后,通过例如store_short指令,来访问该重叠的地址,并修改该重复的地址处的代码,使得受控代码中的代码(也即指令)被修改为非受控类指令,从而能够在所限制的第一地址空间和第二地址空间之外进行数据或代码的访问。本申请通过对受控代码中的受控类指令,配置不同的第一地址空间和第二地址空间,以免受控代码在经扫描通过允许运行后,受控代码再次修改内部代码,使受控代码包括非受控类指令,以访问未分配的内存资源,从而避过控制代码的扫描。In this embodiment of the present application, the addresses between the first address space and the second address space are different. The controlled code can access the code stored in the first address space in the memory through the first type of instructions, and access the data stored in the second address space in the memory through the second type of instructions. If there are overlapping addresses in the first address space and the second address space, then the overlapping addresses can store code and data, and then the controlled code can use, for example, the store_short instruction after the control code scans that all the instructions are controlled class instructions. Access the overlapping address, and modify the code at the repeated address, so that the code (ie, instruction) in the controlled code is modified into an uncontrolled class instruction, so that it can be used between the restricted first address space and the second Access data or code outside the address space. This application configures different first address spaces and second address spaces for the controlled class instructions in the controlled code, so as to prevent the controlled code from modifying the internal code again after being scanned and allowed to run, making the controlled code The code includes uncontrolled instructions to access unallocated memory resources, thus evading scanning by the controlling code.
示例性的,处理模块在执行第一类指令时,可按照第二预设算法code_base+[code_limit(mask)OR addr],来在由code_base和code_limit限制的第一地址空间中,对该第一类指令中的原内存访问地址addr进行映射,得到限制在第一地 址空间内的目标访问地址。For example, when executing the first type of instruction, the processing module can follow the second preset algorithm code_base+[code_limit(mask)OR addr] to execute the first type of instruction in the first address space limited by code_base and code_limit. The original memory access address addr in the instruction is mapped to obtain the target access address limited to the first address space.
例如,第一地址空间为0至ffff,那么可以确定code_base=0x00000000,code_limit=0x0000ffff。For example, if the first address space is 0 to ffff, then it can be determined that code_base=0x00000000 and code_limit=0x0000ffff.
例如受控代码中的short_jump指令访问的第一访问地址addr1为0x12345678,addr1并不在第一地址空间0至ffff的范围内,那么本申请的处理模块在执行该short_jump指令时,可对code_limit和addr1进行与运算,使得addr的高8位变为0,得到addr2为0x00005678。其中,addr2的地址长度在code_limit限制的长度内。然后,处理模块计算code_base+addr2,得到第二访问地址addr3,这里code_base为0,因此,addr3=addr2,显然short_jump指令的跳转地址被限制在addr3(这里为0x00005678),这不会产生错误,而且能够使受控代码仅访问第一地址空间内的资源。其中,addr3在0至ffff的地址范围内(code_base和code_limit限制的第一地址空间)。For example, the first access address addr1 accessed by the short_jump instruction in the controlled code is 0x12345678, and addr1 is not in the range of 0 to ffff in the first address space. Then the processing module of this application can modify code_limit and addr1 when executing the short_jump instruction. Perform an AND operation so that the high 8 bits of addr become 0, and the result of addr2 is 0x00005678. Among them, the address length of addr2 is within the length limit of code_limit. Then, the processing module calculates code_base+addr2 and obtains the second access address addr3. Here code_base is 0. Therefore, addr3=addr2. Obviously, the jump address of the short_jump instruction is limited to addr3 (here is 0x00005678). This will not cause an error. Moreover, the controlled code can only access resources in the first address space. Among them, addr3 is in the address range from 0 to ffff (the first address space limited by code_base and code_limit).
示例性的,处理模块可从寄存器3和寄存器4中分别读取data_base参数和data_limit参数。For example, the processing module can read the data_base parameter and data_limit parameter from register 3 and register 4 respectively.
示例性的,data_base参数为第二地址空间的首地址,data_limit参数为控制第二地址空间大小的二进制掩码。For example, the data_base parameter is the first address of the second address space, and the data_limit parameter is a binary mask that controls the size of the second address space.
示例性的,处理模块在执行第二类指令时,可按照第四预设算法data_base+[data_limit(mask)OR addr],来得到该第二类指令中的内存访问地址addr,在由data_base和data_limit限制的第二地址空间中,对addr进行映射,得到可访问的内存地址。For example, when the processing module executes the second type of instruction, it can obtain the memory access address addr in the second type of instruction according to the fourth preset algorithm data_base+[data_limit(mask)OR addr]. After that, data_base and data_limit In the restricted second address space, addr is mapped to obtain the accessible memory address.
例如,第二地址空间为0至ffff,那么可以确定data_base=0x00000000,data_limit=0x0000ffff。For example, if the second address space is 0 to ffff, then it can be determined that data_base=0x00000000 and data_limit=0x0000ffff.
例如受控代码中的load_short指令访问的第三访问地址addr1为0x12345678,addr1并不在第二地址空间0至ffff的范围内,那么本申请的处理模块在执行该load_short指令时,可对data_limit和addr1进行与运算,使得addr的高8位变为0,得到addr2为0x00005678。其中,addr2的地址长度在data_limit限制的长度内。然后,处理模块计算data_base+addr2,得到第四访问地址addr3,这里data_base为0,因此,addr3=addr2,显然addr3在0至ffff的地址范围内(data_base和data_limit限制的第二地址空间)。For example, the third access address addr1 accessed by the load_short instruction in the controlled code is 0x12345678, and addr1 is not in the range of 0 to ffff in the second address space. Then the processing module of this application can modify data_limit and addr1 when executing the load_short instruction. Perform an AND operation so that the high 8 bits of addr become 0, and the result of addr2 is 0x00005678. Among them, the address length of addr2 is within the length limit of data_limit. Then, the processing module calculates data_base+addr2 and obtains the fourth access address addr3. Here data_base is 0. Therefore, addr3=addr2. Obviously addr3 is in the address range from 0 to ffff (the second address space restricted by data_base and data_limit).
相比于CPU处理每个指令,均进行查页表的遍历操作,本申请的CPU在执行受控类指令时,只需要对受控类指令中的原访问地址,按照相应算法,将该原访问地址的部分高位设置为0,从而将处理后的访问地址能够位于所限制的地址空间内,得到在限制的地址空间内的目标地址,计算目标地址的速度相比于查页表的速度更快,CPU的指令执行效率更高。Compared with the CPU that processes each instruction by performing a page lookup table traversal operation, when the CPU of this application executes a controlled class instruction, it only needs to access the original access address in the controlled class instruction and convert the original access address according to the corresponding algorithm. Part of the high bits of the access address are set to 0, so that the processed access address can be located in the restricted address space, and the target address in the restricted address space is obtained. The speed of calculating the target address is faster than that of looking up the page table. Fast, the CPU's instruction execution efficiency is higher.
在一种可能的实施方式中,所述非受控类指令包括第二预设指令集,所述控制模块包括代理子模块,所述代理子模块包括与所述第二预设指令集匹配的第二指令,其中,所述第二指令为用于访问第二预设代码资源的指令,所述第二预设代码资源位于第四资源访问范围内,所述第四资源访问范围与所述目标资源访问范围之间的资源访问地址不同;所述控制模块对所述受控模块内的每个指令分配目标资源访问范围之后,所述方法 还包括:所述控制模块将所述代理子模块写入至所述第二资源访问范围。In a possible implementation, the uncontrolled instructions include a second preset instruction set, the control module includes an agent submodule, and the agent submodule includes an agent that matches the second preset instruction set. A second instruction, wherein the second instruction is an instruction for accessing a second preset code resource, the second preset code resource is located within a fourth resource access scope, and the fourth resource access scope is the same as the second preset code resource. The resource access addresses between the target resource access scopes are different; after the control module allocates the target resource access scope to each instruction in the controlled module, the method further includes: the control module assigns the proxy submodule Write to the second resource access scope.
示例性的,控制代码可包括代理代码(代理子模块的一种示例),代理代码可包括至少一个非受控类的跳转指令(即jump指令),该非受控类的跳转指令在被处理模块执行时,处理模块可访问第二预设代码资源的地址。Exemplarily, the control code may include agent code (an example of an agent submodule). The agent code may include at least one uncontrolled class jump instruction (i.e., jump instruction). The uncontrolled class jump instruction is in When executed by the processing module, the processing module can access the address of the second preset code resource.
示例性的,代理代码中的非受控类的跳转指令,可为第二预设指令集中的jump6指令至jump10指令中的至少一个指令。For example, the uncontrolled class jump instruction in the agent code may be at least one instruction from the jump6 instruction to the jump10 instruction in the second preset instruction set.
第二预设代码资源为位于第四资源访问范围(例如第三地址空间)内的代码资源。第四资源访问范围的访问地址,与目标资源访问范围(即上述第一地址空间和第二地址空间)的访问地址不重叠。The second default code resource is a code resource located within a fourth resource access range (eg, a third address space). The access address of the fourth resource access range does not overlap with the access address of the target resource access range (ie, the above-mentioned first address space and the second address space).
示例性的,第二预设代码资源为控制代码,或者非受控代码,允许受控代码访问的位于目标资源访问范围之外的代码资源。For example, the second preset code resource is a control code, or an uncontrolled code, which is a code resource outside the target resource access scope that is allowed to be accessed by the controlled code.
示例性的,第二预设代码资源可以是第二预设函数。第二预设函数可以是一个或多个函数,本申请对此不做限制,那么该第二预设函数可为所述控制模块允许所述受控模块访问的外部函数。For example, the second preset code resource may be a second preset function. The second preset function may be one or more functions, which is not limited by this application. Then the second preset function may be an external function that the control module allows the controlled module to access.
在本申请实施例中,在控制代码中包括代理代码时,代理代码可配置有允许受控代码访问的非受控代码中的第二预设代码资源的非受控类的跳转指令,那么在对受控代码中的第一类指令,例如受控类的跳转指令,分配允许访问的第一地址空间以及第一地址参数时,可将代理代码也写入至受控代码可访问的第一地址空间中。那么控制代码在需要调用非受控代码中的第二预设函数时,可通过首先跳转至代理代码中的非受控类的跳转指令,再通过代理代码跳转至非受控代码中的第二预设代码资源,以实现对受控代码之外的外部代码资源的有限访问。In the embodiment of the present application, when the proxy code is included in the control code, the proxy code can be configured with a jump instruction of an uncontrolled class that allows the controlled code to access the second preset code resource in the uncontrolled code, then When allocating the first address space and the first address parameter that are allowed to be accessed to the first type of instruction in the controlled code, such as a jump instruction of the controlled type, the agent code can also be written into the area accessible to the controlled code. in the first address space. Then when the control code needs to call the second preset function in the uncontrolled code, it can first jump to the uncontrolled class in the agent code through the jump instruction, and then jump to the uncontrolled code through the agent code. A second preset code resource to achieve limited access to external code resources outside of the controlled code.
需要说明的是,在本申请实施例中,处理模块通过执行受控代码中受控类的跳转指令,来跳转至代理代码中,以访问代理代码中的非受控类的跳转指令,以通过该非受控类的跳转指令实现外部函数的访问。但是,受控代码中用于访问数据的第二类指令(例如受控类的内存访问指令,load_short指令)所访问的地址空间限制在第二地址空间,并不可以访问第一地址空间,从而不可以访问代理代码所处的地址空间,以防止受控代码通过第二类指令来修改代理代码中的代码,从而通过代理代码跳转至非法空间,防止受控代码非法调用未允许的服务。所谓非法空间即为控制代码不允许受控代码所访问的地址空间。It should be noted that in this embodiment of the application, the processing module jumps to the agent code by executing the jump instructions of the controlled classes in the controlled code to access the jump instructions of the uncontrolled classes in the agent code. , to achieve access to external functions through the jump instructions of this uncontrolled class. However, the address space accessed by the second type of instruction used to access data in the controlled code (such as the controlled type memory access instruction, load_short instruction) is limited to the second address space and cannot access the first address space. Therefore, The address space where the proxy code is located cannot be accessed to prevent the controlled code from modifying the code in the proxy code through the second type of instructions, thereby jumping to the illegal space through the proxy code, and preventing the controlled code from illegally calling unallowed services. The so-called illegal space is the address space that the control code does not allow the controlled code to access.
示例性的,第一预设代码资源与第二预设代码资源可以相同。那么受控代码可通过代理代码的方式,或,在受控代码内包括访问第一预设代码资源的非受控类的跳转指令的方式,来实现对外部代码资源的访问,本申请对此不做限制。For example, the first preset code resource and the second preset code resource may be the same. Then the controlled code can achieve access to the external code resources through proxy code, or by including a jump instruction of an uncontrolled class that accesses the first preset code resource within the controlled code. This application provides There is no restriction on this.
可选地,第一预设代码资源与第二预设代码资源可以不同。那么受控代码可通过代理代码的方式,和,在受控代码内包括访问第一预设代码资源的非受控类的跳转指令的方式,来实现对外部代码资源的访问,本申请对此不做限制。Optionally, the first preset code resource and the second preset code resource may be different. Then the controlled code can achieve access to the external code resources by using proxy code and including jump instructions of the uncontrolled class that access the first preset code resource within the controlled code. This application provides There is no restriction on this.
同理,第一预设函数与第二预设函数可以相同或不同,第一指令与第二指令也可以相同或不同。Similarly, the first preset function and the second preset function may be the same or different, and the first instruction and the second instruction may also be the same or different.
在一种可能的实施方式中,所述第二预设代码资源包括第二预设函数,所述第一类指令包括用于访问所述第二指令的第二跳转类指令;所述处理模块对所述第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源,包括:所述处理模块对所述第二跳转类指令中的第二访问地址进行处理,并按照处理后的第二访问地址访问所述第二指令;所述处理模块按照所述第二指令中的第三访问地址,访问所述第二预设函数;其中,所述第二预设函数为所述控制模块允许所述受控模块访问的函数。In a possible implementation, the second preset code resource includes a second preset function, and the first type of instructions includes a second jump type instruction used to access the second instruction; the processing The module processes the first access address, and accesses the target resource according to the processed first access address, including: the processing module processes the second access address in the second jump instruction, and accesses the target resource according to the processed first access address. The processed second access address accesses the second instruction; the processing module accesses the second preset function according to the third access address in the second instruction; wherein the second preset function is The control module allows the controlled module to access functions.
示例性的,第一类指令为受控代码中的受控类的跳转指令,例如用于访问所述第二指令的第二跳转类指令为short_jump指令,处理模块通过执行受控代码中的该short_jump指令,可将short_jump指令的跳转地址(即第二访问地址)按照上文所述的算法来将跳转地址的高位置零,从而将第二访问地址的地址长度缩短,以使处理后的第二访问地址在第一地址空间内代理代码所处的地址空间。那么处理模块可从处理后的第二访问地址读取到代理代码中的非受控类的跳转指令(即第二指令),例如jump6指令。那么处理模块可通过执行jump6指令进行代码跳转,可跳转到jump6指令对应的跳转地址(即上述第三访问地址)以访问第二预设函数,这样,就通过代理代码实现了受控代码对非受控代码中第二预设函数的访问。Exemplarily, the first type of instruction is a jump instruction of the controlled type in the controlled code. For example, the second type of jump instruction used to access the second instruction is a short_jump instruction. The processing module executes the controlled code in the controlled code. The short_jump instruction can set the jump address of the short_jump instruction (i.e. the second access address) according to the algorithm described above to zero the high bit of the jump address, thereby shortening the address length of the second access address so that The processed second access address is in the address space where the agent code is located in the first address space. Then the processing module can read the uncontrolled class jump instruction (ie, the second instruction) in the agent code from the processed second access address, such as the jump6 instruction. Then the processing module can perform a code jump by executing the jump6 instruction, and can jump to the jump address corresponding to the jump6 instruction (i.e., the above-mentioned third access address) to access the second preset function. In this way, controlled control is achieved through the agent code. Code access to a second preset function in uncontrolled code.
在本申请实施例中,控制代码可在通过处理模块扫描受控代码的指令之后,在检测到受控代码中的指令均为受控类指令时,控制代码可通过处理模块在受控代码的第一地址空间中写入代理代码,使得第一地址空间中不仅包括受控代码,还可包括代理代码,那么受控代码就可以通过受控类的跳转指令跳转至代理代码,并通过代理代码中的非受控类的跳转指令,跳转到非受控代码允许受控代码访问的固定函数的位置,例如第二预设函数的首地址。这样受控代码只能进入非受控代码指定的各个函数的首地址位置上,这些位置上的代码可以做合适的检查,再决定是否提供相关的功能,以确保对第二预设函数的访问安全。In the embodiment of the present application, after the control code scans the instructions of the controlled code through the processing module, when it is detected that the instructions in the controlled code are controlled class instructions, the control code can use the processing module to scan the instructions in the controlled code. The agent code is written in the first address space so that the first address space includes not only the controlled code but also the agent code. Then the controlled code can jump to the agent code through the jump instruction of the controlled class and pass The jump instruction of the uncontrolled class in the agent code jumps to the location of the fixed function that the uncontrolled code allows the controlled code to access, such as the first address of the second preset function. In this way, the controlled code can only enter the first address position of each function specified by the uncontrolled code. The code at these positions can make appropriate checks and then decide whether to provide relevant functions to ensure access to the second preset function. Safety.
在一种可能的实施方式中,本申请实施例提供一种资源控制装置。所述资源控制装置包括控制模块和受控模块,所述资源控制装置中的指令被划分为受控类指令和非受控类指令,所述控制模块用于:在受控模块运行之前,对所述受控模块内的指令进行扫描,检测所述受控模块内的指令是否均为受控类指令;在检测到所述受控模块内的指令均为所述受控类指令时,对所述受控模块内的每个指令分配目标资源访问范围。In a possible implementation, an embodiment of the present application provides a resource control device. The resource control device includes a control module and a controlled module. Instructions in the resource control device are divided into controlled instructions and uncontrolled instructions. The control module is used to: before the controlled module is run, Scan the instructions in the controlled module to detect whether the instructions in the controlled module are all controlled instructions; when it is detected that the instructions in the controlled module are all controlled instructions, Each instruction within the controlled module is assigned a target resource access scope.
在一种可能的实施方式中,所述资源控制装置还包括处理模块;所述处理模块,用于对第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源;其中,所述第一访问地址为所述受控模块内当前运行的受控类指令中的资源访问地址;其中,所述处理后的第一访问地址,在所述当前运行的受控类指令对应的所述目标资源访问范围内。In a possible implementation, the resource control device further includes a processing module; the processing module is used to process the first access address, and access the target resource according to the processed first access address; wherein, The first access address is the resource access address in the controlled class instruction currently running in the controlled module; wherein, the processed first access address is in all the controlled class instructions corresponding to the currently running Within the access scope of the above target resources.
在一种可能的实施方式中,所述控制模块,还用于在检测到所述受控模块内的指令不均为所述受控类指令之后,在检测到所述受控模块包括与第一预设指令集匹配的第一指令、且所述第一指令为用于访问第一预设代码资源的指令时,对所述受控模块内的每 个所述受控类指令,分配所述目标资源访问范围;其中,所述非受控类指令包括所述第一预设指令集;所述第一预设代码资源位于第一资源访问范围,且所述第一资源访问范围与所述目标资源访问范围不同。In a possible implementation, the control module is further configured to, after detecting that the instructions in the controlled module are not uniformly the controlled class instructions, detect that the controlled module includes the same When a first instruction matches a preset instruction set and the first instruction is an instruction for accessing a first preset code resource, allocate all controlled class instructions to each controlled class instruction in the controlled module. The target resource access scope; wherein the uncontrolled instructions include the first preset instruction set; the first preset code resource is located in the first resource access scope, and the first resource access scope is consistent with the first preset instruction set. The access scope of the above target resources is different.
在一种可能的实施方式中,所述第一预设代码资源包括第一预设函数,所述第一指令为用于访问所述第一预设函数的第一跳转类指令;其中,所述第一预设函数为所述控制模块允许所述受控模块访问的函数。In a possible implementation, the first preset code resource includes a first preset function, and the first instruction is a first jump instruction used to access the first preset function; wherein, The first preset function is a function that the control module allows the controlled module to access.
在一种可能的实施方式中,所述受控模块内的所述受控类指令包括:用于访问代码资源的第一类指令和用于访问数据资源的第二类指令;所述控制模块,具体用于:对所述第一类指令分配第二资源访问范围;对所述第二类指令分配第三资源访问范围;其中,所述第二资源访问范围和所述第三资源访问范围之间的资源访问地址不同。In a possible implementation, the controlled class instructions in the controlled module include: a first class of instructions for accessing code resources and a second class of instructions for accessing data resources; the control module , specifically used to: allocate a second resource access scope to the first type of instruction; allocate a third resource access scope to the second type of instruction; wherein the second resource access scope and the third resource access scope The resource access addresses are different between them.
在一种可能的实施方式中,所述非受控类指令包括第二预设指令集,所述控制模块包括代理子模块,所述代理子模块包括与所述第二预设指令集匹配的第二指令;其中,所述第二指令为用于访问第二预设代码资源的指令,所述第二预设代码资源位于第四资源访问范围内,且所述第四资源访问范围与所述目标资源访问范围之间的资源访问地址不同;所述控制模块,还用于将所述代理子模块写入至所述第二资源访问范围。In a possible implementation, the uncontrolled instructions include a second preset instruction set, the control module includes an agent submodule, and the agent submodule includes an agent that matches the second preset instruction set. A second instruction; wherein the second instruction is an instruction for accessing a second preset code resource, the second preset code resource is located within a fourth resource access range, and the fourth resource access range is consistent with the The resource access addresses between the target resource access scopes are different; the control module is also used to write the proxy sub-module to the second resource access scope.
在一种可能的实施方式中,所述第二预设代码资源包括第二预设函数,所述第一类指令包括用于访问所述第二指令的第二跳转类指令;所述处理模块,具体用于:对所述第二跳转类指令中的第二访问地址进行处理,并按照处理后的第二访问地址访问所述第二指令;按照所述第二指令中的第三访问地址,访问所述第二预设函数;其中,所述第二预设函数为所述控制模块允许所述受控模块访问的函数。In a possible implementation, the second preset code resource includes a second preset function, and the first type of instructions includes a second jump type instruction used to access the second instruction; the processing Module, specifically used for: processing the second access address in the second jump instruction, and accessing the second instruction according to the processed second access address; according to the third instruction in the second instruction. Access the address to access the second preset function; wherein the second preset function is a function that the control module allows the controlled module to access.
上述各实施方式的资源控制装置的效果,与上述各实施方式的资源控制方法的效果类似,这里不再赘述。The effects of the resource control apparatus in the above embodiments are similar to the effects of the resource control methods in the above embodiments, and will not be described again here.
在一种可能的实施方式中,本申请实施例提供一种资源控制装置。资源控制装置包括包括一个或多个接口电路和一个或多个处理器;所述接口电路用于从存储器接收信号,并向所述处理器发送所述信号,所述信号包括存储器中存储的计算机指令;当所述处理器执行所述计算机指令时,所述处理器可实现上述任意一种实施方式中的方法。In a possible implementation, an embodiment of the present application provides a resource control device. The resource control device includes one or more interface circuits and one or more processors; the interface circuit is used to receive signals from the memory and send the signals to the processor, where the signals include computer data stored in the memory. Instructions; when the processor executes the computer instructions, the processor can implement the method in any of the above implementations.
本实施方式的资源控制装置的效果,与上述各实施方式的资源控制方法的效果类似,这里不再赘述。The effect of the resource control device in this embodiment is similar to the effect of the resource control method in each of the above embodiments, and will not be described again here.
在一种可能的实施方式中,本申请实施例提供一种计算机可读存储介质。计算机可读存储介质存储有计算机程序,当计算机程序运行在计算机或处理器上时,使得计算机或处理器执行上述任意一种实施方式中的方法。In a possible implementation manner, embodiments of the present application provide a computer-readable storage medium. The computer-readable storage medium stores a computer program. When the computer program is run on a computer or processor, it causes the computer or processor to execute the method in any of the above embodiments.
本实施方式的计算机可读存储介质的效果,与上述各实施方式的资源控制方法的效 果类似,这里不再赘述。The effect of the computer-readable storage medium in this embodiment is similar to the effect of the resource control method in the above embodiments, and will not be described again here.
在一种可能的实施方式中,本申请实施例提供一种计算机程序产品。计算机程序产品包含软件程序,当软件程序被计算机或处理器执行时,使得上述任意一个实施方式中的方法被执行。In a possible implementation manner, embodiments of the present application provide a computer program product. The computer program product includes a software program. When the software program is executed by a computer or processor, the method in any of the above embodiments is executed.
本实施方式的计算机程序产品的效果,与上述各实施方式的资源控制方法的效果类似,这里不再赘述。The effect of the computer program product in this embodiment is similar to the effect of the resource control method in each of the above embodiments, and will not be described again here.
附图说明Description of the drawings
为了更清楚地说明本申请实施例的技术方案,下面将对本申请实施例的描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。In order to explain the technical solutions of the embodiments of the present application more clearly, the drawings needed to be used in the description of the embodiments of the present application will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present application. , for those of ordinary skill in the art, other drawings can also be obtained based on these drawings without exerting creative labor.
图1为示例性示出的代码隔离的示意图;Figure 1 is a schematic diagram illustrating exemplary code isolation;
图2a为示例性示出的进程隔离代码的示意图;Figure 2a is a schematic diagram of an exemplary process isolation code;
图2b为示例性示出的虚拟机隔离代码的示意图;Figure 2b is a schematic diagram of an exemplary virtual machine isolation code;
图2c为示例性示出的安全区隔离代码的示意图;Figure 2c is a schematic diagram of an exemplary safe zone isolation code;
图3a为示例性示出的系统框架结构示意图;Figure 3a is a schematic diagram of the system framework structure;
图3b为示例性示出的系统框架结构示意图;Figure 3b is a schematic diagram of the system framework structure;
图4a为示例性示出的系统的运行流程图;Figure 4a is an operational flow chart of the system shown in an exemplary manner;
图4b为示例性的示出的内存地址空间的示意图;Figure 4b is a schematic diagram of an exemplary memory address space;
图5a为示例性示出的系统的运行流程图;Figure 5a is an operational flow chart of the exemplary shown system;
图5b为示例性的示出的内存地址空间的示意图;Figure 5b is a schematic diagram of an exemplary memory address space;
图5c为示例性示出的受控代码的编译过程的示意图;Figure 5c is a schematic diagram of an exemplary compilation process of controlled code;
图6a为示例性示出的系统的运行流程图;Figure 6a is an operational flow chart of the exemplary system shown;
图6b为示例性的示出的内存地址空间的示意图;Figure 6b is a schematic diagram of an exemplary memory address space;
图6c为示例性示出的受控代码的编译过程的示意图;Figure 6c is a schematic diagram of an exemplary compilation process of controlled code;
图7为示例性示出的系统架构示意图;Figure 7 is a schematic diagram of an exemplary system architecture;
图8为本申请实施例提供的一种装置的结构示意图;Figure 8 is a schematic structural diagram of a device provided by an embodiment of the present application;
图9为本申请实施例提供的一种芯片的结构示意图。Figure 9 is a schematic structural diagram of a chip provided by an embodiment of the present application.
具体实施方式Detailed ways
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本申请保护的范围。The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the scope of protection of this application.
本文中术语“和/或”,仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。The term "and/or" in this article is just an association relationship that describes related objects, indicating that three relationships can exist. For example, A and/or B can mean: A exists alone, A and B exist simultaneously, and they exist alone. B these three situations.
本申请实施例的说明书和权利要求书中的术语“第一”和“第二”等是用于区别不同的对象,而不是用于描述对象的特定顺序。例如,第一目标对象和第二目标对象等是用于区别不同的目标对象,而不是用于描述目标对象的特定顺序。The terms “first” and “second” in the description and claims of the embodiments of this application are used to distinguish different objects, rather than to describe a specific order of objects. For example, the first target object, the second target object, etc. are used to distinguish different target objects, rather than to describe a specific order of the target objects.
在本申请实施例中,“示例性的”或者“例如”等词用于表示作例子、例证或说明。本申请实施例中被描述为“示例性的”或者“例如”的任何实施例或设计方案不应被解释为比其它实施例或设计方案更优选或更具优势。确切而言,使用“示例性的”或者“例如”等词旨在以具体方式呈现相关概念。In the embodiments of this application, words such as "exemplary" or "for example" are used to represent examples, illustrations or explanations. Any embodiment or design described as "exemplary" or "such as" in the embodiments of the present application is not to be construed as preferred or advantageous over other embodiments or designs. Rather, use of the words "exemplary" or "such as" is intended to present the concept in a concrete manner.
在本申请实施例的描述中,除非另有说明,“多个”的含义是指两个或两个以上。例如,多个处理单元是指两个或两个以上的处理单元;多个系统是指两个或两个以上的系统。In the description of the embodiments of this application, unless otherwise specified, the meaning of “plurality” refers to two or more. For example, multiple processing units refer to two or more processing units; multiple systems refer to two or more systems.
为了信息的安全,在软件中,可对信息进行隔离。在对信息隔离时,计算机可通过对不同的软件代码赋予不同的角色,并基于角色来分配资源,以构成该角色的一个实例,从而保证不同的实例仅能访问对应角色实例被分配的资源,以实现代码隔离,确保信息安全访问。For information security, information can be isolated in the software. When isolating information, the computer can assign different roles to different software codes and allocate resources based on the role to form an instance of the role, thereby ensuring that different instances can only access the resources assigned to the corresponding role instance. To achieve code isolation and ensure safe access to information.
示例性的,角色实例可包括但不限于进程,容器,虚拟机等。For example, role instances may include but are not limited to processes, containers, virtual machines, etc.
示例性的,为了便于理解,以角色实例为进程为例进行说明。例如在手机的安卓操作系统(简称Android)上运行一个应用程序(App,Application)。Android是手机供应商提供的,它代表一个角色。而App是App提供商提供的,那么为了信息安全,Android允许App只能访问电话功能,不能访问位置功能。为了实现对App的权限控制,Android在将中央处理器(CPU)的控制权交给App前,可修改CPU的权限状态,从操作系统权限修改为进程权限,其中,Android可对进程设置权限,以控制App可在进程权限内进行资源访问。示例性的,进程权限可包括访问电话权限,但是不包括访问位置权限。For example, for ease of understanding, the role instance is used as a process for illustration. For example, an application (App, Application) is run on the Android operating system (Android for short) of the mobile phone. Android is provided by the mobile phone vendor and represents a role. The App is provided by the App provider, so for the sake of information security, Android allows the App to only access the phone function but not the location function. In order to control the permissions of the App, Android can modify the permission status of the CPU from operating system permissions to process permissions before handing over the control of the central processing unit (CPU) to the App. Among them, Android can set permissions on the process. To control the App's resource access within process permissions. For example, process permissions may include access to the phone, but not access to the location.
App的代码在接管CPU之后,App的代码就只可以在进程权限内进行资源范围,而无法访问Android未分配给该App的进程的权限内的资源。那么App如果想要获得更高的权限,以访问进程权限之外的资源(例如访问位置信息),那么App只可以将CPU的控制权重新交给Android,由Android的代码重新接管CPU,然后,Android完成对位置信息的资源访问。After the App code takes over the CPU, the App code can only access resources within the process permissions, but cannot access resources within the permissions of the process that Android has not assigned to the App. Then if the App wants to obtain higher permissions to access resources beyond the process permissions (such as accessing location information), then the App can only return control of the CPU to Android, and the Android code will take over the CPU again, and then, Android completes resource access to location information.
通过以上示例可以看到,在对代码进行隔离时,可对CPU设计一个分层或者分类的状态,并由高特权级的代码(例如操作系统代码)控制低特权级代码(例如一般用户程序代码,如上述App的代码)的资源访问范围(例如电话访问权限)。并将CPU的权限从高特权级修改为低特权级,使CPU执行的代码跳转到低特权级的代码中,以使低特权级代码接管CPU。那么低特权级的代码只可以在被分配的权限范围内进行资源范围,无法访问权限外的资源。As can be seen from the above examples, when isolating code, a hierarchical or classified state can be designed for the CPU, and high-privileged code (such as operating system code) controls low-privileged code (such as general user program code) , such as the code of the above App) resource access scope (such as phone access permissions). And modify the permissions of the CPU from high privilege level to low privilege level, causing the code executed by the CPU to jump to the low privilege level code, so that the low privilege level code takes over the CPU. Then low-privileged code can only access resources within the assigned permissions, and cannot access resources outside the permissions.
如果低特权级的代码要离开低特权级的CPU状态,低特权级的代码只能主动(比如系统调用)或者被动(比如中断等)的放弃CPU的控制权,使得CPU执行的代码跳转到高特权级特定的代码位置上,同时将CPU的权限从低特权级修改为高特权级,以使高特权级权的代码重新获得CPU的控制权,高特级权的代码可在高特级权的权限范围内进行资源访问。If low-privilege code wants to leave the low-privilege CPU state, the low-privilege code can only give up control of the CPU actively (such as system calls) or passively (such as interrupts, etc.), causing the code executed by the CPU to jump to At a specific code location with a high privilege level, the CPU permissions are modified from a low privilege level to a high privilege level, so that the high privilege level code regains control of the CPU. The high privilege level code can be placed in the high privilege level. Access resources within the scope of authority.
那么高特级权的代码(例如操作系统代码)通过对低特级权的代码进行权限控制,可以确保低特级权的代码在资源访问上的安全性,例如上述App的操作行为的安全,避免App获取到用户的关键信息,例如读取用户的交易密码等。Then high-privilege code (such as operating system code) can ensure the security of resource access by low-privilege code by controlling the permissions of low-privilege code, such as the security of the operation behavior of the above-mentioned App, preventing the App from obtaining Get the user's key information, such as reading the user's transaction password, etc.
图1为示例性示出的代码隔离的示意图。Figure 1 is a schematic diagram illustrating exemplary code isolation.
应该理解的是,图1仅是一个范例,代码隔离的隔离结构可以具有比图中所示的更多的或者更少的层级。It should be understood that Figure 1 is only an example, and the isolation structure for code isolation may have more or fewer levels than shown in the figure.
如图1所示,CPU权限从高到底依次为全系统控制的权限、虚拟机的权限、操作系统权限、进程权限。As shown in Figure 1, the CPU permissions from high to low are system-wide control permissions, virtual machine permissions, operating system permissions, and process permissions.
其中,全系统控制代码在虚线箭头指向的全系统控制的权限内运行,操作系统管理代码在虚线箭头指向的操作系统的权限(这里为虚拟机中操作系统的权限)内运行,一般用户程序代码在虚线箭头指向的进程的权限内运行。那么从高到低的特级权的代码依次为全系统控制代码、虚拟机管理代码、操作系统管理代码和一般应用程序代码。Among them, the system-wide control code runs within the permissions of the system-wide control pointed by the dotted arrow, the operating system management code runs within the permissions of the operating system pointed by the dotted arrow (here, the permissions of the operating system in the virtual machine), and the general user program code Runs within the permissions of the process pointed to by the dotted arrow. Then the privilege codes from high to low are system-wide control code, virtual machine management code, operating system management code and general application code.
如图1所示,运行在进程的权限内的一般用户程序,如果希望访问进程的权限之外的资源,则进程需要交出CPU的控制权,将CPU的控制权交给更高特权级的操作系统;运行在操作系统的权限内的操作系统管理代码,如果希望访问操作系统的权限之外的资源,则虚拟机需要交出CPU的控制权,将CPU的控制权交给更高特权级的虚拟机;运行在虚拟机的权限内的虚拟机管理代码,如果希望访问虚拟机的权限之外的资源,则虚拟机需要交出CPU的控制权,将CPU的控制权交给更高特权级的全系统控制代码,以在全系统控制的权限内进行资源访问。As shown in Figure 1, if a general user program running within the permissions of the process wants to access resources outside the permissions of the process, the process needs to hand over control of the CPU to a higher-privileged program. Operating system; operating system management code running within the permissions of the operating system. If you want to access resources outside the permissions of the operating system, the virtual machine needs to hand over control of the CPU and hand over control of the CPU to a higher privilege level. Virtual machine; virtual machine management code running within the permissions of the virtual machine. If you want to access resources outside the permissions of the virtual machine, the virtual machine needs to hand over control of the CPU and hand over control of the CPU to higher privileges. level of system-wide control code to enable resource access within system-wide control permissions.
也就是说,如果低特权级的代码要离开低特权级的CPU状态,低特权级的代码只能主动(比如系统调用)或者被动(比如中断等)的放弃CPU的控制权,使得CPU执行的代码跳转到高特权级特定的代码位置上,同时将CPU的权限从低特权级修改为高特权级,以使高特权级权的代码重新获得CPU的控制权,高特级权的代码可在高特级权的权限范围内进行资源访问。In other words, if low-privilege level code wants to leave the low-privilege level CPU state, the low-privilege level code can only give up control of the CPU actively (such as system calls) or passively (such as interrupts, etc.), causing the CPU to execute The code jumps to a specific code location with a high privilege level, and at the same time changes the CPU permissions from a low privilege level to a high privilege level, so that the high privilege level code regains control of the CPU. The high privilege level code can be Access resources within the scope of high privileges.
示例性的,在图1中,全系统控制代码在全系统控制的权限内运行时,可对虚拟机的权限进行配置,以限制虚拟机的资源访问权限;虚拟机管理代码在虚拟机权限内运行时,可对操作系统的权限进行配置,以限制操作系统的资源访问权限;操作系统管理代码可在操作系统的权限内运行,可对进程的权限进行配置,以限制进程的资源访问权限。换言之,高特级权的代码可以对低特级权的代码分配权限,以对低特级权的代码的资源访问范围进行权限控制。For example, in Figure 1, when the system-wide control code runs within the permissions of the system-wide control, the permissions of the virtual machine can be configured to limit the resource access permissions of the virtual machine; the virtual machine management code runs within the permissions of the virtual machine. When running, the permissions of the operating system can be configured to limit the resource access permissions of the operating system; the operating system management code can run within the permissions of the operating system, and the permissions of the process can be configured to limit the resource access permissions of the process. In other words, high-privilege code can assign permissions to low-privilege code to control the resource access scope of low-privilege code.
示例性的,结合图1和图2a,高特级权的操作系统管理代码(运行在虚拟机中)可对运行有一般用户程序代码的进程的权限进行配置,以对进程的资源访问范围进行限制。其中,内存资源是较为重要的资源,不论是进程还是虚拟机或是操作系统等都可以访问内存资源。但是,对于可访问的内存资源的具体地址,操作系统管理代码可对进程进行限制,其中,操作系统管理代码可配置每个进程的页表,以设置进程可以访问的内存资源的地址范围。其中,页表中可以包括该进程可访问的内存地址的信息。For example, with reference to Figure 1 and Figure 2a, high-privilege operating system management code (running in a virtual machine) can configure the permissions of a process running general user program code to limit the resource access scope of the process. . Among them, memory resources are the more important resources. Whether it is a process, a virtual machine, or an operating system, it can access memory resources. However, the operating system management code can limit the specific addresses of the memory resources that can be accessed by the process. The operating system management code can configure the page table of each process to set the address range of the memory resources that the process can access. Among them, the page table can include information about the memory addresses that the process can access.
一般用户程序代码在进程的权限内运行,当进程发起该进程的权限之外的资源访问请求时,需要将CPU的控制权交给操作系统,由操作系统发起该资源访问请求。例如进 程发起访问系统资源的指令,其中,资源访问权限不在该进程的权限范围之内,该指令被禁止访问,那么可将CPU的权限从进程的权限修改为操作系统的权限,以及将CPU的代码指针从在该进程中运行的一般用户程序代码,指向操作系统管理代码,以使操作系统管理代码控制CPU,以实现对系统资源的访问。这样,可以确保进程在分配的资源访问权限内进行资源访问,一般用户程序代码被隔离在进程对应的权限范围内运行,不能越过权限范围访问没有被分配的资源。Generally, user program code runs within the permissions of a process. When a process initiates a resource access request beyond the permissions of the process, control of the CPU needs to be handed over to the operating system, which initiates the resource access request. For example, a process initiates an instruction to access system resources. If the resource access permission is not within the permissions of the process and the instruction is prohibited from accessing, then the permissions of the CPU can be modified from the permissions of the process to the permissions of the operating system, and the permissions of the CPU can be changed. The code pointer points from the general user program code running in the process to the operating system management code, so that the operating system management code controls the CPU to achieve access to system resources. In this way, it can be ensured that the process accesses resources within the assigned resource access permissions. Generally, user program code is isolated and runs within the permissions corresponding to the process, and cannot access unallocated resources beyond the permissions range.
但是,内存资源是进程的权限内可以访问的资源,而进程对内存资源可访问的地址是受该进程的页表限制的。那么CPU每次在接收到进程发起的内存访问指令时,均需要查询用户态中对该进程配置的页表,以确定对该进程分配的内存地址列表。如果该内存访问指令所访问的内存地址不在该内存地址列表之内,则CPU无法对该内存访问指令进行执行,可产生异常。However, memory resources are resources that can be accessed within the permissions of the process, and the addresses that a process can access to memory resources are limited by the page table of the process. Then every time the CPU receives a memory access instruction initiated by a process, it needs to query the page table configured for the process in user mode to determine the list of memory addresses allocated to the process. If the memory address accessed by the memory access instruction is not in the memory address list, the CPU cannot execute the memory access instruction and an exception may be generated.
对于通过进程来实现代码安全隔离的方案,CPU在执行进程发起的指令时,CPU为了确认高特级权代码确实分配了特定资源给低特级权的角色实例。每次低特级权的代码在访问资源的时候,CPU在运行该资源访问指令之前,必须对该角色实例所分配的资源进行检查(比如页表),例如确认对该进程分配的内存资源中是否包括该指令所指向的内存资源,那么这将降低CPU的执行效率。For the solution of implementing code security isolation through processes, when the CPU executes instructions initiated by the process, the CPU confirms that the high-privilege code has indeed allocated specific resources to the low-privilege role instance. Every time low-privilege code accesses a resource, the CPU must check the resources allocated to the role instance (such as the page table) before running the resource access instruction, for example, confirm whether the memory resources allocated to the process are Including the memory resources pointed to by this instruction, this will reduce the execution efficiency of the CPU.
示例性的,容器也是一种代码隔离方案,该方案可以看做为一种特殊的进程方案,它和硬件无关。操作系统可将多个进程成组看待,组内的多个进程之间相互请求的资源可以由CPU提供,不在进程之间的资源访问请求不提供,该方案与图2a的方案类似,容器内的进程不能越过容器的范围访问没有被分配的资源,从而保证容器内进程的代码是安全的。For example, containers are also a code isolation solution, which can be regarded as a special process solution that has nothing to do with hardware. The operating system can treat multiple processes into groups. The resources requested by multiple processes in the group can be provided by the CPU. Resource access requests that are not between processes are not provided. This solution is similar to the solution in Figure 2a. Within the container The process cannot access unallocated resources beyond the scope of the container, thereby ensuring that the code of the process within the container is safe.
同理,每次容器内进程中代码在访问资源的时候,CPU在运行该资源访问指令之前,必须对该容器所分配的资源进行检查(比如页表),例如确认对该容器分配的内存资源中是否包括该指令所指向的内存资源,那么这将降低CPU的执行效率。In the same way, every time the code in the process of the container accesses a resource, the CPU must check the resources allocated to the container (such as the page table) before running the resource access instruction, such as confirming the memory resources allocated to the container. Whether the memory resource pointed to by the instruction is included, then this will reduce the execution efficiency of the CPU.
示例性的,结合图1和图2b,虚拟机是一种代码隔离的方案,虚拟机的方案可以看做是图2a所示的进程隔离方案的叠加。可将操作系统和进程一起看做是一个被管理的对象,那么虚拟机管理代码可以看做是比图1和图2a中的操作系统管理代码具有更高特级权的管理者。虚拟机隔离的逻辑和进程隔离的逻辑类似,下面结合图2b进行简要阐述。For example, combining Figure 1 and Figure 2b, the virtual machine is a code isolation solution, and the virtual machine solution can be regarded as a superposition of the process isolation solution shown in Figure 2a. The operating system and the process can be regarded as a managed object, and the virtual machine management code can be regarded as a manager with higher privileges than the operating system management code in Figures 1 and 2a. The logic of virtual machine isolation is similar to the logic of process isolation, which is briefly explained below with reference to Figure 2b.
如图2b所示,操作系统代码在虚拟机的权限内运行。对于虚拟机的权限,物理芯片设备可以提供两个地址空间权限的配置,1级配置由虚拟机指定,2级配置由虚拟机管理代码指定。其中,对于2级配置,如图2b所示,虚拟机管理代码可对虚拟机的权限进行配置,以对虚拟机的资源访问范围进行限制。As shown in Figure 2b, the operating system code runs within the permissions of the virtual machine. Regarding the permissions of virtual machines, physical chip devices can provide two configurations of address space permissions. Level 1 configuration is specified by the virtual machine, and level 2 configuration is specified by the virtual machine management code. Among them, for level 2 configuration, as shown in Figure 2b, the virtual machine management code can configure the permissions of the virtual machine to limit the resource access scope of the virtual machine.
其中,内存资源是较为重要的资源,不论是进程还是虚拟机或是操作系统等都可以访问内存资源。但是,对于可访问的内存资源的具体地址,虚拟机管理代码可对虚拟机进行限制。如图2b所示,虚拟机管理代码可配置每个虚拟机的2级页表,以设置虚拟机可以访问的内存资源的地址范围。其中,2级页表中可以包括该虚拟机可访问的内存地址的信息,从而使虚拟机管理代码在2级页表上控制虚拟机对内存资源的地址访问权限,进而实现对运行在虚拟机中的操作系统的资源访问地址的范围控制。Among them, memory resources are the more important resources. Whether it is a process, a virtual machine, or an operating system, it can access memory resources. However, the virtual machine management code can place restrictions on the virtual machine as to the specific addresses of memory resources that can be accessed. As shown in Figure 2b, the virtual machine management code can configure each virtual machine's level 2 page table to set the address range of memory resources that the virtual machine can access. Among them, the level 2 page table can include information about the memory address that the virtual machine can access, so that the virtual machine management code controls the virtual machine's address access rights to memory resources on the level 2 page table, thereby realizing control over the memory resources running on the virtual machine. Range control of resource access addresses in the operating system.
运行在虚拟机的权限中的操作系统代码,对内存资源可访问的地址是受该虚拟机的页表限制的。那么CPU每次在接收到虚拟机发起的内存访问指令时,均需要查询对该虚拟机配置的页表,以确定对该虚拟机分配的内存地址列表。如果该内存访问指令所访问的内存地址不在该内存地址列表之内,则CPU无法对该内存访问指令进行执行,可产生异常。For operating system code running in the permissions of a virtual machine, the addresses accessible to memory resources are restricted by the virtual machine's page table. Then every time the CPU receives a memory access instruction initiated by a virtual machine, it needs to query the page table configured for the virtual machine to determine the list of memory addresses allocated to the virtual machine. If the memory address accessed by the memory access instruction is not in the memory address list, the CPU cannot execute the memory access instruction and an exception may be generated.
对于通过虚拟机来实现代码安全隔离的方案,CPU在执行虚拟机发起的指令时,CPU为了确认高特级权代码确实分配了特定资源给低特级权的角色实例。每次低特级权的代码在访问资源的时候,CPU在运行该资源访问指令之前,必须对该角色实例所分配的资源进行检查(比如页表),例如确认对该虚拟机分配的内存资源中是否包括该指令所指向的内存资源,那么这将降低CPU的执行效率。For the solution of implementing code security isolation through a virtual machine, when the CPU executes instructions initiated by the virtual machine, the CPU confirms that the high-privilege code has indeed allocated specific resources to the low-privilege role instance. Every time low-privilege code accesses a resource, the CPU must check the resources allocated to the role instance (such as the page table) before running the resource access instruction, such as confirming that the memory resources allocated to the virtual machine are Whether the memory resource pointed to by this instruction is included, this will reduce the execution efficiency of the CPU.
示例性的,结合图2c,安全区是一种代码隔离的方案,安全区的方案可以看做是图2b所示的虚拟机隔离方案的叠加。如图2c所示,代码可分为运行在安全区的安全区代码和运行在非安全区的非安全区代码。其中,安全区代码可包括虚拟机管理代码、操作系统代码和一般用户程序代码等。安全区代码可以在安全区的权限范围内运行。安全管理代码可以看做是比图2c中的虚拟机管理代码具有更高特级权的管理者。安全区的代码隔离的逻辑和图2a所示的进程隔离、图2b所示的虚拟机隔离的逻辑类似,下面结合图2c对与进程隔离和虚拟机隔离的方案的区别之处做简要阐述。For example, with reference to Figure 2c, the safe zone is a code isolation solution. The safe zone solution can be regarded as a superposition of the virtual machine isolation solution shown in Figure 2b. As shown in Figure 2c, the code can be divided into safe zone code running in the safe zone and non-safe zone code running in the non-safe zone. Among them, the security zone code may include virtual machine management code, operating system code, general user program code, etc. Security zone code can run within the permissions of the security zone. The security management code can be regarded as a manager with higher privileges than the virtual machine management code in Figure 2c. The logic of code isolation in the safe zone is similar to the logic of process isolation shown in Figure 2a and virtual machine isolation shown in Figure 2b. The following is a brief explanation of the differences between the process isolation and virtual machine isolation solutions in conjunction with Figure 2c.
在不同实现场景下,安全区的方案可存在区别。安全管理代码可用于设置安全区和非安全区的工作环境。例如在ARM CPU上,安全区可访问的内存不通过页表指定,而是硬件硬编码决定的,所以,安全区代码是否可以访问特定地址的内存,取决于该硬件硬编码。如果安全区代码接管CPU,并在运行安全区的权限范围内,则CPU对安全区代码发出的请求会添加一个安全标记,使得CPU发出的请求带有安全标记,否则CPU所发出的请求没有这个标记(例如CPU在运行非安全区代码)。CPU和内存之间具有总线,CPU通过总线发送写内存的请求至内存,总线相关逻辑可根据这个标记,决定是否允许这个请求被响应。如果该请求添加了标记,则该请求可以访问内存,如果该请求没有该标记,则丢弃该请求。那么安全管理代码可根据CPU发出的请求是否具有安全标记,来判断CPU可以执行的指令,或者在CPU发出地址访问请求时,可以检测是否有该地址的访问权限。In different implementation scenarios, safe zone solutions may differ. Security management codes can be used to set up secure and non-secure work environments. For example, on ARM CPUs, the memory accessible to the safe zone is not specified through the page table, but is determined by hardware hard coding. Therefore, whether the safe zone code can access the memory at a specific address depends on the hardware hard coding. If the security zone code takes over the CPU and is within the scope of authority to run the security zone, the request issued by the CPU to the security zone code will add a security mark, so that the request issued by the CPU has a security mark, otherwise the request issued by the CPU does not have this Flag (for example, the CPU is running non-safe zone code). There is a bus between the CPU and the memory. The CPU sends a request to write memory to the memory through the bus. The bus-related logic can decide whether to allow the request to be responded to based on this mark. If the request has the tag added, the request can access the memory, if the request does not have the tag, the request is discarded. Then the security management code can determine the instructions that the CPU can execute based on whether the request issued by the CPU has a security mark, or when the CPU issues an address access request, it can detect whether there is access permission to the address.
另一个区别之处在于安全区通常没有多个实例,进程方案可以有多个进程,虚拟机方案可以有多个虚拟机,但安全区可能只有一个,所以这种体系结构常常允许非安全区和安全区之间的代码直接互相调用,而不需要经过安全管理代码进行中转。其中,安全区和非安全区在互相调用时,只能调用对方固定的地址,且所调用的地址受安全管理代码的控制,从而维护了不同区域的安全。Another difference is that secure zones usually don't have multiple instances, process solutions can have multiple processes, and virtual machine solutions can have multiple virtual machines, but there may only be one secure zone, so this architecture often allows non-secure zones and Codes between security zones call each other directly without going through the security management code for transit. Among them, when the safe zone and the non-safe zone call each other, they can only call each other's fixed address, and the called address is controlled by the security management code, thereby maintaining the security of different areas.
那么安全区代码运行在CPU时,CPU发出的请求具有预设标记,安全管理代码在运行的时候,检查CPU发出的请求是否具有预设标记,以确定该请求可访问的资源范围,这种判断在CPU每次发出请求时都需要执行一次,这提高了运行成本。Then when the security zone code is running on the CPU, the request issued by the CPU has a preset tag. When the security management code is running, it checks whether the request issued by the CPU has a preset tag to determine the range of resources that the request can access. This judgment It needs to be executed every time the CPU makes a request, which increases running costs.
总结而言,传统的进程、容器、虚拟机、安全区等代码隔离方案在实施时,CPU在执行指令时,为了确认高特权级代码确实分配了特定资源给低特权级的角色实例。每次低特级权的代码在访问资源的时候,CPU在运行该资源访问指令之前,必须对该角色实例所 分配的资源进行检查(比如页表),例如确认对该虚拟机分配的内存资源中是否包括该指令所指向的内存资源,那么这将降低CPU的执行效率。In summary, when traditional code isolation solutions such as processes, containers, virtual machines, and safe zones are implemented, when the CPU executes instructions, it is necessary to confirm that high-privilege code has indeed allocated specific resources to low-privilege role instances. Every time low-privilege code accesses a resource, the CPU must check the resources allocated to the role instance (such as the page table) before running the resource access instruction, such as confirming that the memory resources allocated to the virtual machine are Whether the memory resource pointed to by this instruction is included, this will reduce the execution efficiency of the CPU.
在一些应用场景下,当程序需要使用第三方代码时,为了确保第三方代码在运行过程中,不会访问本程序中的一些关键信息,传统技术可通过对第三方代码创建进程、容器或虚拟机等角色实例的方式,来将第三方代码的资源访问范围限制在所属角色实例对应的权限内,以实现第三方代码的安全访问。其中,第三方代码对应的角色实例的权限,由更高特级权的代码所分配。传统技术的缺陷如上文所述,CPU每次执行第三方代码的指令,都需要进行权限和资源的检查,这将降低CPU的执行效率。In some application scenarios, when a program needs to use third-party code, in order to ensure that the third-party code does not access some key information in the program during operation, traditional technology can create processes, containers or virtualization for the third-party code. In this way, the resource access scope of third-party code is limited to the permissions corresponding to the role instance to which the third-party code belongs. Among them, the permissions of the role instance corresponding to the third-party code are assigned by the code with higher privileges. The shortcomings of traditional technology are as mentioned above. Every time the CPU executes instructions from third-party code, it needs to check permissions and resources, which will reduce the execution efficiency of the CPU.
为此,本申请提供了一种资源控制方法,该方法可在需要保证资源的安全访问的代码(简称受控代码,例如上述第三方代码)运行之前,对受控代码所使用的指令进行扫描。该方法在扫描到受控代码所使用的指令均是受控类指令后,其中,受控类指令在进行资源访问时只能在该受控代码的目标资源访问范围内进行资源访问,那么在受控代码运行过程中,CPU就不需要对受控代码发出的指令进行检查(例如查询对受控代码所属的进程所分配的资源)。那么受控代码所使用的指令不会超出受控代码的目标资源访问范围,从而提升CPU的执行效率。而且,在限制受控代码的资源访问范围时,不需要对受控代码创建相应的角色实例,例如进程、虚拟机等,以在该角色实例内对该受控代码进行权限限制。To this end, this application provides a resource control method that can scan the instructions used by the controlled code before the code that needs to ensure safe access to resources (referred to as controlled code, such as the above-mentioned third-party code) is run. . After this method scans that the instructions used by the controlled code are all controlled class instructions, where the controlled class instructions can only access resources within the target resource access range of the controlled code, then When the controlled code is running, the CPU does not need to check the instructions issued by the controlled code (such as querying the resources allocated to the process to which the controlled code belongs). Then the instructions used by the controlled code will not exceed the target resource access range of the controlled code, thereby improving the execution efficiency of the CPU. Moreover, when restricting the resource access scope of the controlled code, there is no need to create corresponding role instances, such as processes, virtual machines, etc., for the controlled code to restrict permissions on the controlled code within the role instance.
实施例1Example 1
示例性的,图3a和图3b为示例性示出的系统框架结构示意图。应该理解的是,图3a和图3b所示系统仅是一个范例,本申请的系统可以具有比图中所示的更多的或者更少的模块,可以组合两个或多个的模块,或者可以具有不同的模块配置。图3a、图3b中所示出的各种模块可以在包括一个或多个信号处理和/或专用集成电路在内的硬件、软件、或硬件和软件的组合中实现。Exemplarily, FIG. 3a and FIG. 3b are schematic diagrams of the system framework structure. It should be understood that the system shown in Figures 3a and 3b is only an example, and the system of the present application may have more or fewer modules than shown in the figures, two or more modules may be combined, or Can have different module configurations. The various modules shown in Figures 3a, 3b may be implemented in hardware, software, or a combination of hardware and software including one or more signal processing and/or application specific integrated circuits.
如图3a所示,本申请的提供的资源控制装置可包括控制模块以及受控模块,可选地包括处理模块,所述资源控制装置中的指令被划分为受控类指令和非受控类指令。该资源控制装置可执行下述实施例所述的方法。As shown in Figure 3a, the resource control device provided by the present application may include a control module and a controlled module, optionally including a processing module. Instructions in the resource control device are divided into controlled class instructions and uncontrolled class instructions. instruction. The resource control device can execute the method described in the following embodiments.
示例性的,资源控制装置可实现为中央处理器(CPU),或者任意一种处理器等,本申请对于资源控制装置的实现方式不做限制,为了便于说明,以资源控制装置实现为CPU为例进行说明。For example, the resource control device can be implemented as a central processing unit (CPU), or any kind of processor, etc. This application does not limit the implementation of the resource control device. For convenience of explanation, the resource control device is implemented as a CPU. Example to illustrate.
示例性的,处理模块可以是中央处理器中包括一个或多个信号处理和/或专用集成电路在内的硬件,处理模块可用于执行指令,处理模块可执行的指令被划分为受控类指令和非受控类指令,处理模块在执行指令时,并不区分指令类别,可按照指令的语义(或者说编码)来对指令进行解释执行。Exemplarily, the processing module may be hardware including one or more signal processing and/or application-specific integrated circuits in the central processing unit. The processing module may be used to execute instructions. The instructions executable by the processing module are divided into controlled class instructions. With uncontrolled instructions, the processing module does not distinguish between instruction types when executing instructions, and can interpret and execute instructions according to the semantics (or encoding) of the instructions.
其中,受控类指令在被处理模块执行时,处理模块只可以访问控制模块对该受控类指令所分配的特定的资源访问范围,而无法访问特定资源访问范围之外的资源。换言之,受控类指令的语义(或者说编码)决定,受控类指令在被处理模块执行时被限制了资源访问范围。那么处理模块在执行每个受控类指令时,均无需通过查页表等方式来检查当 前运行的该受控类指令中的资源访问地址是否在当前运行的程序的资源访问权限范围内,以提升处理模块的指令处理效率。When a controlled instruction is executed by the processing module, the processing module can only access the specific resource access range allocated by the control module to the controlled instruction, but cannot access resources outside the specific resource access range. In other words, the semantics (or encoding) of the controlled class instructions determine that the resource access scope of the controlled class instructions is restricted when executed by the processing module. Then when the processing module executes each controlled class instruction, it does not need to check whether the resource access address in the currently running controlled class instruction is within the resource access permission range of the currently running program by looking up the page table, etc. Improve the instruction processing efficiency of the processing module.
非受控类指令在被处理模块执行时,可按照该非受控类指令的资源访问地址进行资源访问,而无需对非受控类指令中的资源访问地址进行处理。可选地,处理模块可按照传统的查页表等方式,来确定该非受控类指令所处的进程(或虚拟机等)是否具有该访问地址的访问权限,以决定是否继续执行该非受控类指令。如果该资源访问地址是权限内的访问的地址,则处理模块按照该资源访问地址访问资源,如果该资源访问地址是权限外的访问的地址,则处理模块中断执行该受控模块内的指令。When an uncontrolled instruction is executed by the processing module, resource access can be performed according to the resource access address of the uncontrolled instruction without processing the resource access address in the uncontrolled instruction. Optionally, the processing module can determine whether the process (or virtual machine, etc.) in which the uncontrolled instruction is located has access rights to the access address in accordance with traditional page lookup methods, so as to decide whether to continue executing the uncontrolled instruction. Controlled instructions. If the resource access address is within the authority, the processing module accesses the resource according to the resource access address. If the resource access address is outside the authority, the processing module interrupts execution of the instructions in the controlled module.
如图3a所示,本申请所提供的CPU中的处理模块,可支持对受控类指令和非受控类指令这两类指令的执行,并对这两类指令以不同的方式进行执行。As shown in Figure 3a, the processing module in the CPU provided by this application can support the execution of two types of instructions, controlled instructions and uncontrolled instructions, and execute these two types of instructions in different ways.
示例性的,处理模块可为CPU中的硬件结构。For example, the processing module may be a hardware structure in a CPU.
示例性的,本申请可在CPU中的上述硬件结构做出改进,使得原本仅支持执行非受控类指令的CPU,在改进后可在硬件上支持受控类指令的解释执行,且CPU可对受控类指令和非受控类指令按照不同的方式进行解释执行。For example, the present application can make improvements to the above hardware structure in the CPU, so that the CPU that originally only supports the execution of uncontrolled instructions can support the interpretation and execution of controlled instructions in hardware after the improvement, and the CPU can Controlled instructions and uncontrolled instructions are interpreted and executed in different ways.
示例性的,以访问的资源为内存资源为例,对于非受控类指令,处理模块可按照非受控类指令中的内存访问地址,按照传统的方式(例如查页表)进行内存访问;而对于受控类指令,处理模块可对受控类指令中的内存访问地址,映射到特定的内存地址空间内,以在特定的内存地址空间内访问内存,以使受控类指令被CPU执行后所访问的资源有限。For example, taking the accessed resource as a memory resource, for uncontrolled instructions, the processing module can perform memory access according to the memory access address in the uncontrolled instruction in a traditional way (such as page table lookup); For controlled instructions, the processing module can map the memory access address in the controlled instruction to a specific memory address space to access the memory in the specific memory address space, so that the controlled instruction can be executed by the CPU. Access to resources is limited.
如图3a和图3b所示,CPU中运行的程序可选地包括受控模块、控制模块、非受控模块,在CPU运行上述三个模块中任意一个模块的过程中,处理模块可执行当前运行的模块中的指令,以实现当前运行的模块的功能。As shown in Figure 3a and Figure 3b, the program running in the CPU optionally includes a controlled module, a control module, and an uncontrolled module. While the CPU is running any one of the above three modules, the processing module can execute the current Directives in the running module to implement the functionality of the currently running module.
下面对上述三个模块之间的关系做简要描述:The following is a brief description of the relationship between the above three modules:
图3b所示,非受控模块,可用于调用第三方开发的受控模块,示例性的,非受控模块可运行在本申请提供的CPU中,以实现对受控模块的调用,示例性的,处理模块可用于执行非受控模块中的指令,非受控模块可包括非受控类指令,可选地,还可包括受控类指令,本申请对此不做限制。As shown in Figure 3b, the uncontrolled module can be used to call the controlled module developed by a third party. As an example, the uncontrolled module can be run in the CPU provided by this application to realize the call of the controlled module. , the processing module can be used to execute instructions in the uncontrolled module. The uncontrolled module can include uncontrolled instructions, and optionally, can also include controlled instructions. This application does not limit this.
示例性的,受控模块和非受控模块可看做一个模块(一个程序)中的两个部分。示例性的,非受控模块和受控模块可以是调用与被调用的关系。例如,非受控模块首先发起对受控模块的调用,在受控模块运行过程中,受控模块也可以调用非受控模块中的函数等以实现相应功能。For example, the controlled module and the uncontrolled module can be regarded as two parts of one module (a program). For example, the uncontrolled module and the controlled module may have a calling and called relationship. For example, the uncontrolled module first initiates a call to the controlled module. During the operation of the controlled module, the controlled module can also call functions in the uncontrolled module to implement corresponding functions.
此外,非受控模块和受控模块的开发平台不同,非受控模块并无法完全信任受控模块,以使其访问非受控模块或非受控模块所属模块的任何资源,为了确保受控模块在运行过程中,所执行的操作对非受控模块而言是可控的,非受控模块需要对受控模块所访问的资源进行限制。In addition, the development platforms of uncontrolled modules and controlled modules are different. The uncontrolled module cannot fully trust the controlled module so that it can access any resources of the uncontrolled module or the module to which the uncontrolled module belongs. In order to ensure that the controlled module During the running process of the module, the operations performed are controllable to the uncontrolled module, and the uncontrolled module needs to limit the resources accessed by the controlled module.
为此,本申请提供了上述控制模块,如图3b所示,非受控模块可安装或内嵌有本申请提供的控制模块,以通过控制模块来确保非受控模块在调用受控模块时,受控模块可访问有限的资源。示例性的,控制模块可包括非受控类指令,可选地进一步包括受控类 指令。To this end, this application provides the above-mentioned control module. As shown in Figure 3b, the uncontrolled module can be installed or embedded with the control module provided by this application to ensure that the uncontrolled module uses the control module when calling the controlled module. , controlled modules have access to limited resources. Exemplarily, the control module may include uncontrolled instructions, and optionally further include controlled instructions.
示例性的,如图3a和图3b所示,控制模块可运行在CPU中,控制模块可用于在非受控模块需要调用受控模块(例如调用函数等)时,在受控模块运行之前,控制模块可通过向处理模块发送非受控类指令,来使处理模块执行该非受控类指令,以实现对受控模块内的指令的扫描。控制模块可扫描受控模块内的指令是否均为受控类指令,在控制模块检测到受控模块内的指令均为受控类指令时,则控制模块可通过向处理模块发送非受控类指令的方式,来对受控模块内的每个指令分配目标资源访问范围(可包括目标地址空间)。这样在受控模块运行后,处理模块在执行受控模块中的受控类指令时,只可以在该目标地址空间内进行资源访问。示例性的,该资源可以是图3a所示的内存资源。For example, as shown in Figure 3a and Figure 3b, the control module can run in the CPU, and the control module can be used when the uncontrolled module needs to call the controlled module (such as calling a function, etc.), before the controlled module runs, The control module can send an uncontrolled instruction to the processing module, so that the processing module executes the uncontrolled instruction, so as to scan the instructions in the controlled module. The control module can scan whether the instructions in the controlled module are all controlled instructions. When the control module detects that the instructions in the controlled module are all controlled instructions, the control module can send uncontrolled instructions to the processing module. Instruction mode is used to allocate the target resource access range (which may include the target address space) to each instruction within the controlled module. In this way, after the controlled module is run, the processing module can only access resources in the target address space when executing the controlled class instructions in the controlled module. For example, the resource may be the memory resource shown in Figure 3a.
需要说明的是,对受控模块限制访问范围的资源并不限于内存资源,还可包括但不限于内存方式访问外设时外设内的资源。其中,内存中可存储的资源可包括代码和数据,那么内存资源可包括代码资源和数据资源。It should be noted that the resources that restrict the access range to the controlled module are not limited to memory resources, and may also include but are not limited to resources within the peripheral when accessing the peripheral in memory mode. The resources that can be stored in the memory may include code and data, and the memory resources may include code resources and data resources.
示例性的,在对受控模块所限制访问范围的资源为内存资源时,控制模块可对受控模块所访问的内存地址空间进行限制。For example, when the resources within the restricted access range of the controlled module are memory resources, the control module can restrict the memory address space accessed by the controlled module.
示例性的,在对受控模块所限制访问范围的资源为上述外设的资源时,控制模块可对受控模块所访问的外设中的地址空间进行限制。For example, when the resources within the restricted access range of the controlled module are resources of the above-mentioned peripheral device, the control module may restrict the address space in the peripheral device accessed by the controlled module.
为了便于说明,本申请各实施例以内存资源的访问限制为例进行说明,当资源为内存资源之外的其他可访问的资源时,方法同理,这里不再赘述。For ease of explanation, each embodiment of the present application takes the access restriction of memory resources as an example. When the resource is an accessible resource other than memory resources, the method is the same and will not be described again here.
示例性的,控制模块和受控模块可以是运行在本申请CPU中的软件程序。For example, the control module and the controlled module may be software programs running in the CPU of this application.
可选地,控制模块也可以运行在传统的CPU中,本申请对此不做限制。Optionally, the control module can also run on a traditional CPU, which is not limited by this application.
下面对受控类指令和非受控类指令进行介绍:The following is an introduction to controlled instructions and uncontrolled instructions:
示例性的,受控类指令可包括本申请的自定义指令集,其中,自定义指令集中的指令在被CPU执行时,只可以访问特定的资源范围,而无法访问特定资源范围之外的资源。换言之,自定义指令集中的指令在被CPU执行时被限制了资源访问范围。By way of example, the controlled instructions may include the custom instruction set of the present application. When the instructions in the custom instruction set are executed by the CPU, they can only access a specific resource range and cannot access resources outside the specific resource range. . In other words, the instructions in the custom instruction set have limited resource access scope when executed by the CPU.
可选地,受控类指令还可包括对非受控模块的代码和数据的安全无影响的一些传统的指令(例如加法指令、减法指令等不需要访问内存资源的指令),这些对非受控模块中的代码和数据的安全无影响的指令可根据需要而灵活设置,本申请对此不做限制。Optionally, controlled instructions may also include some traditional instructions that have no impact on the security of the code and data of uncontrolled modules (such as addition instructions, subtraction instructions, and other instructions that do not require access to memory resources), which have no impact on the security of uncontrolled modules. Instructions that do not affect the security of the code and data in the control module can be flexibly set according to needs, and this application does not impose restrictions on this.
示例性的,对于受控类指令中与资源无关预设指令(即不需要访问资源的指令,以资源为内存资源为例),则控制模块无需对该预设指令分配目标资源访问范围,控制模块只需对受控模块内需要访问内存资源的每个受控类指令分配目标资源访问范围。这里以访问的资源为内存资源为例。For example, for the resource-independent preset instructions in the controlled class instructions (that is, instructions that do not need to access resources, taking the resource as a memory resource as an example), the control module does not need to allocate a target resource access range to the preset instruction, and the control module The module only needs to assign a target resource access range to each controlled class instruction that needs to access memory resources in the controlled module. Here, the accessed resource is a memory resource as an example.
非受控类指令可以是除受控类指令之外的任意指令。Uncontrolled instructions can be any instructions except controlled instructions.
非受控类指令可包括但不限于:第三类指令和第四类指令。Uncontrolled instructions may include but are not limited to: third type instructions and fourth type instructions.
示例性的,第三类指令在被CPU执行时,CPU可访问代码以及执行该代码。For example, when the third type of instruction is executed by the CPU, the CPU can access the code and execute the code.
示例性的,第三类指令可包括但不限于:jump(跳转)指令,本申请对此不做限制。For example, the third type of instructions may include but is not limited to: jump instructions, which is not limited in this application.
其中,jump指令用于指令跳转,可用于函数调用的场景,能够用于访问代码,也是一种内存访问指令。Among them, the jump instruction is used for instruction jumps, can be used in function call scenarios, can be used to access code, and is also a memory access instruction.
示例性的,jump指令可归类为访问内存中代码的指令,jump指令可简称“非受控类 的跳转指令”。For example, a jump instruction can be classified as an instruction that accesses code in memory, and the jump instruction can be referred to as an "uncontrolled jump instruction".
示例性的,第四类指令在被CPU执行时,CPU可访问数据。For example, when the fourth type of instruction is executed by the CPU, the CPU can access data.
第四类指令可包括但不限于:load(加载)指令、store(存储)指令、堆栈访问指令等,本申请对此不做限制。The fourth type of instructions may include but is not limited to: load instructions, store instructions, stack access instructions, etc., which are not limited in this application.
其中,load指令在被执行时,CPU可按照指令中内存访问地址,将内存中的数据加载至CPU;store指令在被执行时,CPU可按照指令中的内存访问地址,将CPU中的数据写入内存。Among them, when the load instruction is executed, the CPU can load the data in the memory to the CPU according to the memory access address in the instruction; when the store instruction is executed, the CPU can write the data in the CPU according to the memory access address in the instruction. into memory.
堆栈访问指令可包括push(进栈)指令和pop(出栈)指令,其中,堆栈访问指令也是一种内存访问指令。The stack access instruction may include a push instruction and a pop instruction, where the stack access instruction is also a memory access instruction.
load指令、store指令、push指令、pop指令可归类为访问内存中数据的指令。Load instructions, store instructions, push instructions, and pop instructions can be classified as instructions for accessing data in memory.
如上文所述,受控类指令可包括本申请的自定义指令集,可选地还可包括对非受控模块的代码和数据的安全无影响的一些传统的指令,为了便于说明,后文各个实施例,以受控类指令为这里的自定义指令集为例进行说明。As mentioned above, controlled instructions may include the custom instruction set of this application, and optionally may also include some traditional instructions that have no impact on the security of the code and data of uncontrolled modules. For the convenience of explanation, the following text Each embodiment will be described by taking the controlled class instructions as the custom instruction set here as an example.
示例性的,对于受控类指令,自定义指令集的指令类型可包括但不限于:load_short指令、store_short指令,short_push指令、short_pop指令、short_jump指令。For example, for controlled instructions, the instruction types of the custom instruction set may include but are not limited to: load_short instruction, store_short instruction, short_push instruction, short_pop instruction, short_jump instruction.
以内存资源为例,自定义指令集中的指令只可以在特定的内存地址空间中访问数据或访问代码。Taking memory resources as an example, instructions in a custom instruction set can only access data or code in a specific memory address space.
其中,load_short指令在被CPU执行时,CPU可基于该load_short指令的语义来执行指令,从而按照预设算法将特定的内存地址空间中的数据加载至CPU;Among them, when the load_short instruction is executed by the CPU, the CPU can execute the instruction based on the semantics of the load_short instruction, thereby loading the data in the specific memory address space to the CPU according to the preset algorithm;
示例性的,load_short指令可分为多种编码的load_short指令,为了便于理解,可描述为load_shortN,N为正整数,不同编码的load_short指令对应的N取值不同。For example, the load_short instruction can be divided into load_short instructions of multiple encodings. For ease of understanding, it can be described as load_shortN, where N is a positive integer. Load_short instructions of different encodings have different N values.
不同编码的load_short指令,在被CPU执行时,CPU在将内存地址空间中的数据加载至CPU时,示例性的,加载过程可存在区别,但是不同编码的load_short指令在执行时均资源访问范围受限。同理,自定义指令集中的其他指令(例如store_short指令),也可以包括多种编码的同一类指令。When the load_short instructions with different encodings are executed by the CPU, when the CPU loads the data in the memory address space to the CPU, for example, the loading process may be different, but the resource access scope of the load_short instructions with different encodings is limited when executed. limit. In the same way, other instructions in the custom instruction set (such as the store_short instruction) can also include instructions of the same type in multiple encodings.
store_short指令在被CPU执行时,CPU可基于该store_short指令的语义来执行该store_short指令,以按照预设算法将CPU中的数据写入至特定的内存地址空间;When the store_short instruction is executed by the CPU, the CPU can execute the store_short instruction based on the semantics of the store_short instruction to write the data in the CPU to a specific memory address space according to the preset algorithm;
需要说明的是,load指令和load_short指令的指令编码不同,它们都是用于将内存中的数据加载至CPU的指令,但是load_short指令的资源范围范围受限;同理,store指令和store_short指令的指令编码不同,store_short指令的资源访问范围受限。It should be noted that the load instruction and the load_short instruction have different instruction encodings. They are both instructions for loading data in the memory into the CPU, but the resource range of the load_short instruction is limited; similarly, the store instruction and the store_short instruction The instruction encoding is different, and the resource access scope of the store_short instruction is limited.
同理,short_push指令、short_pop指令是相较于传统的push指令、pop指令,short_push指令、short_pop指令在被CPU执行时,CPU可按照本申请的预设算法,在特定的堆栈地址空间内进行进栈和出栈的指令,以在受限的堆栈地址空间内进行资源访问。In the same way, the short_push instruction and short_pop instruction are compared with the traditional push instruction and pop instruction. When the short_push instruction and short_pop instruction are executed by the CPU, the CPU can proceed in a specific stack address space according to the preset algorithm of this application. Stack and pop instructions to access resources within the restricted stack address space.
同理,short_jump指令是相较于传统的jump指令,short_jump指令在被CPU执行时,CPU可按照本申请的预设算法,在特定的内存地址空间中进行代码访问的指令。Similarly, the short_jump instruction is compared with the traditional jump instruction. When the short_jump instruction is executed by the CPU, the CPU can perform code access in a specific memory address space according to the preset algorithm of this application.
那么自定义指令集中的指令也可以分为两类指令:Then the instructions in the custom instruction set can also be divided into two types of instructions:
第一类指令在被CPU执行时,CPU可访问代码资源并运行该代码资源。When the first type of instructions is executed by the CPU, the CPU can access the code resources and run the code resources.
第一类指令可包括但不限于:short_jump指令。The first type of instructions may include but is not limited to: short_jump instructions.
示例性的,第一类指令可简称“受控类的跳转指令”。For example, the first type of instructions may be referred to as "controlled type jump instructions".
第二类指令在被CPU执行时,CPU可访问数据资源。When the second type of instructions is executed by the CPU, the CPU can access data resources.
第二类指令可包括但不限于:load_short指令、store_short指令,short_push指令、short_pop指令等。The second type of instructions may include but is not limited to: load_short instruction, store_short instruction, short_push instruction, short_pop instruction, etc.
示例性的,为了便于说明,后文以受控类指令即为上述自定义指令集,以对受控类指令限制的资源访问范围为内存资源访问范围为例进行说明,那么受控类指令则均为内存访问指令,可分为上述第一类指令和第二类指令。Illustratively, for the convenience of explanation, in the following description, the controlled class instructions are the above-mentioned custom instruction set, and the resource access range restricted by the controlled class instructions is the memory resource access range as an example. Then the controlled class instructions are They are all memory access instructions and can be divided into the first type of instructions and the second type of instructions mentioned above.
此外,为了行文简便,全文描述的“XX指令为用于访问代码资源的指令”,用于表示XX指令在被CPU(或者CPU中的处理模块)执行时,CPU(或者CPU中处理模块)可访问代码资源并运行该代码资源。同理,“XX指令为用于访问数据资源的指令”,用于表示XX指令在被CPU(或者CPU中的处理模块)执行时,CPU(或者CPU中处理模块)可访问数据资源。In addition, for the sake of simplicity, the "XX instruction is an instruction used to access code resources" described in the full text is used to indicate that when the XX instruction is executed by the CPU (or the processing module in the CPU), the CPU (or the processing module in the CPU) can Access the code resource and run the code resource. Similarly, "XX instruction is an instruction used to access data resources" is used to indicate that when the XX instruction is executed by the CPU (or the processing module in the CPU), the CPU (or the processing module in the CPU) can access the data resources.
继续参照图3a,控制模块可扫描受控模块中的指令是否均为受控类指令,示例性的,控制模块可包括上述自定义指令集,控制模块可通过扫描受控模块内的指令并与该自定义指令集内的指令进行对比,来检测受控模块内的指令是否均为受控类指令。Continuing to refer to Figure 3a, the control module can scan whether the instructions in the controlled module are all controlled instructions. For example, the control module can include the above-mentioned custom instruction set. The control module can scan the instructions in the controlled module and compare them with The instructions in the custom instruction set are compared to detect whether the instructions in the controlled module are all controlled instructions.
可选地,控制模块检测到受控模块内的指令均为自定义指令集内的指令,则控制模块可以确定受控模块内的指令均为受控类指令。Optionally, if the control module detects that the instructions in the controlled module are all instructions in the custom instruction set, the control module can determine that the instructions in the controlled module are all controlled instructions.
可选地,控制模块检测到受控模块内的指令中存在除该自定义指令集之外的指令,则控制模块可以确定受控模块内的指令不均为受控类指令。Optionally, if the control module detects that instructions in the controlled module include instructions other than the custom instruction set, the control module may determine that the instructions in the controlled module are not all controlled instructions.
示例性的,可参照图3a,在控制模块检测到受控模块中的指令均为受控类指令时,控制模块可通过向处理模块发送非受控类指令,来写入目标地址参数至处理模块(例如处理模块中的存储单元),以限制受控模块的目标资源访问范围,例如内存的目标地址空间。For example, referring to Figure 3a, when the control module detects that the instructions in the controlled module are all controlled instructions, the control module can write the target address parameters to the processing module by sending uncontrolled instructions to the processing module. module (such as a storage unit in a processing module) to limit the target resource access range of the controlled module, such as the target address space of memory.
具体而言,示例性的,在控制模块对所述受控模块内的每个指令分配目标资源访问范围时,控制模块可向处理模块发送非受控类指令,处理模块在执行该非受控类指令时,可对受控模块内的每个受控类指令,在内存中分配可访问的目标地址空间,以及确定该目标地址空间的目标地址参数。此外,控制模块还可向处理模块发送非受控类指令,处理模块在执行该非受控类指令时,可将上述目标地址参数写入存储单元。从而实现对受控模块内的每个指令的目标资源访问范围的分配。Specifically, for example, when the control module allocates a target resource access range to each instruction in the controlled module, the control module may send an uncontrolled instruction to the processing module, and the processing module executes the uncontrolled instruction. For each controlled class instruction in the controlled module, an accessible target address space can be allocated in the memory, and the target address parameters of the target address space can be determined. In addition, the control module can also send an uncontrolled instruction to the processing module, and when the processing module executes the uncontrolled instruction, the above target address parameter can be written into the storage unit. This achieves the allocation of the target resource access range for each instruction within the controlled module.
本实施例中,通过处理模块执行非受控类指令来对受控模块内的每个受控类指令分配目标资源访问范围,其目的在于,如果受控类指令在被处理模块执行时,处理模块可对受控模块分配目标资源访问范围,那么受控模块就可通过发送通过扫描确认的受控类指令至处理模块,来使处理模块修改对受控模块内每条指令所分配的目标资源访问范围。例如,受控模块将目标资源访问范围修改为控制模块不允许受控模块访问的资源访问范围,使得受控模块的资源访问范围无法受控制模块所控制。从而无法达到通过控制模块来限制受控模块的目标资源访问范围的目的,使得受控模块容易访问到敏感内存资源(例如存储有用户账号数据等数据的内存空间)。那么本申请实施例中,控制模块作为对受控模块的资源访问范围的限制方,处理模块可通过执行控制模块内的非受控类指令,来对受控模块内的每个受控类指令分配目标资源访问范围,使得受控模块的资源访问范围是 受控制模块控制的,受控模块自身无法修改其自身可访问的目标资源访问范围,从而确保受控模块对资源的安全访问。In this embodiment, the target resource access range is allocated to each controlled class instruction in the controlled module by executing the uncontrolled class instruction by the processing module. The purpose is that if the controlled class instruction is executed by the processing module, the processing The module can allocate the target resource access range to the controlled module, and then the controlled module can send controlled class instructions confirmed by scanning to the processing module, so that the processing module can modify the target resources allocated to each instruction in the controlled module. Access scope. For example, the controlled module modifies the target resource access scope to a resource access scope that the control module does not allow the controlled module to access, so that the resource access scope of the controlled module cannot be controlled by the control module. Therefore, the purpose of limiting the target resource access range of the controlled module through the control module cannot be achieved, making it easy for the controlled module to access sensitive memory resources (such as memory space that stores user account data and other data). In this embodiment of the present application, the control module serves as a limiter of the resource access scope of the controlled module, and the processing module can execute each controlled class instruction in the controlled module by executing the uncontrolled class instructions in the control module. Allocate the target resource access scope so that the resource access scope of the controlled module is controlled by the control module. The controlled module itself cannot modify the target resource access scope that it can access, thereby ensuring the safe access of resources by the controlled module.
可选地,用于存储该目标地址参数的存储单元,可以内置于CPU。Optionally, the storage unit used to store the target address parameter may be built into the CPU.
可选地,该存储单元可以内置于处理模块,或内置于CPU但外置于处理模块(其中,处理模块可与存储单元通信连接)。Alternatively, the storage unit can be built into the processing module, or built into the CPU but external to the processing module (wherein the processing module can be communicatively connected with the storage unit).
可选地,存储单元也可以是独立于CPU之外的外置芯片中的存储单元,处理模块可访问外置芯片中的存储单元。Optionally, the storage unit can also be a storage unit in an external chip that is independent of the CPU, and the processing module can access the storage unit in the external chip.
示例性的,存储单元可以是寄存器(包括但不限于段寄存器等)、磁头等。For example, the storage unit may be a register (including but not limited to a segment register, etc.), a magnetic head, etc.
考虑到非受控模块对所调用的不同受控模块所限制的资源访问范围可存在区别,该区别可根据需要而灵活配置。那么在受控模块运行在本申请的CPU之前(例如非受控模块调用受控模块之前),控制模块可运行在本申请的CPU中,并通过非受控类指令来更新存储单元中的目标地址参数,以确保非受控模块待调用的受控代码仅可以在该目标地址参数所限制的特定地址空间内访问内存资源。Considering that there may be differences in the resource access scopes restricted by different controlled modules called by uncontrolled modules, this difference can be flexibly configured as needed. Then before the controlled module runs in the CPU of this application (for example, before the uncontrolled module calls the controlled module), the control module can run in the CPU of this application and update the target in the storage unit through uncontrolled instructions. Address parameter to ensure that the controlled code to be called by the uncontrolled module can only access memory resources within the specific address space restricted by the target address parameter.
可选地,目标地址参数可包括多组地址参数,控制模块可对受控模块中的受控类指令,按照指令类型,来对不同类型的受控类指令,分配不同的地址参数,以使受控模块中不同类型的受控类指令在被CPU执行时,CPU所访问的地址空间存在区别。示例性的,一组地址参数对应于一个地址空间。Optionally, the target address parameters may include multiple sets of address parameters, and the control module may allocate different address parameters to different types of controlled instructions according to the instruction type in the controlled module, so that When different types of controlled instructions in the controlled module are executed by the CPU, the address space accessed by the CPU is different. For example, a set of address parameters corresponds to an address space.
可选地,在目标地址参数包括多组地址参数时,各组地址参数所存储至的存储单元可以相同或不同。Optionally, when the target address parameters include multiple sets of address parameters, the storage units where each set of address parameters are stored may be the same or different.
示例性的,目标地址参数可通过非受控类指令设置,而无法通过受控类指令设置,以避免受控模块通过发送受控类指令至处理模块,以修改其可访问的地址空间。For example, the target address parameter can be set through an uncontrolled class instruction but cannot be set through a controlled class instruction to prevent the controlled module from modifying its accessible address space by sending a controlled class instruction to the processing module.
示例性的,目标地址参数可在受控模块运行之前,由控制模块通过发送非受控类指令来写入至存储单元。For example, the target address parameter can be written to the storage unit by the control module by sending an uncontrolled instruction before the controlled module is run.
可选地,在受控模块运行过程中,控制程序无法通过非受控类指令,来更新对该受控模块中的受控类指令所分配的目标地址参数。Optionally, during the operation of the controlled module, the control program cannot update the target address parameters assigned to the controlled instructions in the controlled module through uncontrolled instructions.
可选地,在受控模块运行结束之后,控制程序可通过非受控类指令来将存储单元中针对该受控模块的目标地址参数清空,以便于处理模块执行下一个受控模块中的代码。Optionally, after the controlled module runs, the control program can clear the target address parameters of the controlled module in the storage unit through uncontrolled instructions, so that the processing module can execute the code in the next controlled module. .
在控制模块写入目标地址参数至处理模块,由处理模块将目标地址参数写入存储单元之后,非受控模块可调用受控模块,以使受控模块运行在CPU中。After the control module writes the target address parameters to the processing module, and the processing module writes the target address parameters into the storage unit, the uncontrolled module can call the controlled module so that the controlled module runs in the CPU.
在受控模块运行过程中,处理模块可执行受控模块当前运行的受控类指令(例如目标指令),处理模块在执行该目标指令时,可将目标指令中的原内存访问地址映射至该目标指令对应的目标地址空间内,得到该原内存访问地址在该目标地址空间中对应的目标内存访问地址;处理模块按照该目标内存访问地址来访问内存资源。During the operation of the controlled module, the processing module can execute the controlled class instruction (such as the target instruction) currently running by the controlled module. When executing the target instruction, the processing module can map the original memory access address in the target instruction to the In the target address space corresponding to the target instruction, the target memory access address corresponding to the original memory access address in the target address space is obtained; the processing module accesses memory resources according to the target memory access address.
示例性的,处理模块可对目标指令中的原内存访问地址进行处理,使得处理后的内存访问地址(即目标内存访问地址),在该目标指令对应的目标地址空间内,处理模块并按照目标内存访问地址来访问内存资源。For example, the processing module can process the original memory access address in the target instruction, so that the processed memory access address (ie, the target memory access address) is in the target address space corresponding to the target instruction, and the processing module performs the processing according to the target Memory access address to access memory resources.
示例性的,处理模块可执行接收到的目标指令,在存储单元中读取对该受控模块内的该目标指令(一个受控类指令)所设置的目标地址参数,并按照预设算法,基于目标 地址参数和目标指令中的原内存访问地址,来在该目标指令对应的目标地址空间中确定目标内存访问地址;处理模块在目标内存访问地址处访问内存资源。For example, the processing module can execute the received target instruction, read the target address parameters set for the target instruction (a controlled class instruction) in the controlled module in the storage unit, and follow the preset algorithm, Based on the target address parameter and the original memory access address in the target instruction, the target memory access address is determined in the target address space corresponding to the target instruction; the processing module accesses the memory resource at the target memory access address.
示例性的,在原内存访问地址超出目标地址空间时,则处理模块可按照该目标地址参数来将原内存访问地址的部分高位置零,使得处理后的目标内存访问地址在目标地址空间内。简单来描述,例如目标地址空间为1至100,原内存访问地址为1000,则可将原内存访问地址的地址长度缩小10倍,得到目标内存访问地址100,使得目标内存访问地址在目标地址空间内。For example, when the original memory access address exceeds the target address space, the processing module can set part of the high bits of the original memory access address to zero according to the target address parameter, so that the processed target memory access address is within the target address space. To describe it simply, for example, the target address space is 1 to 100 and the original memory access address is 1000. Then the address length of the original memory access address can be reduced by 10 times to obtain the target memory access address 100, so that the target memory access address is in the target address space. Inside.
在本申请实施例中,为了能够使目标指令在被处理模块执行时,只可以访问对该目标指令设定的目标地址空间内的资源,处理模块按照该目标指令的编码,在执行该目标指令时,可按照上述算法对原内存访问地址进行处理(例如多个高位地址置为零)。那么不论目标指令中的原内存访问地址是否在目标地址空间内,处理后的目标内存访问地址均在目标地址空间内。In the embodiment of the present application, in order to enable the target instruction to only access the resources in the target address space set for the target instruction when it is executed by the processing module, the processing module executes the target instruction according to the encoding of the target instruction. When, the original memory access address can be processed according to the above algorithm (for example, multiple high-order addresses are set to zero). Then regardless of whether the original memory access address in the target instruction is in the target address space, the processed target memory access address is in the target address space.
例如,即便受控模块的一段代码请求调用目标地址空间之外的某个地址1处的代码资源,处理模块在执行该段代码对应的受控类指令时,则可将地址1按照上述算法映射到该目标地址空间内的地址2。那么无论受控模块请求访问的资源的地址在哪里,处理模块在执行受控模块的目标指令时,都可在目标地址空间内找到一个与该原内存访问地址对应的目标内存访问地址,并访问该目标内存访问地址处的资源作为对原内存访问地址的访问结果。For example, even if a piece of code in the controlled module requests to call a code resource at a certain address 1 outside the target address space, when the processing module executes the controlled class instruction corresponding to the piece of code, address 1 can be mapped according to the above algorithm. to address 2 within the target address space. Then no matter where the address of the resource requested to be accessed by the controlled module is, when the processing module executes the target instruction of the controlled module, it can find a target memory access address corresponding to the original memory access address in the target address space, and access The resource at the target memory access address is used as the access result to the original memory access address.
这样,本申请实施例的处理模块在执行目标指令时,并不是直接按照该目标指令的原内存访问地址进行资源访问,而是找到该原内存访问地址在目标地址空间内映射的目标内存访问地址,并在目标内存地址处访问子。能够从目标指令的访问地址的维度进行受控模块可访问资源的限制,目标内存访问地址所指向的资源是准确且唯一的,那么这种精细化维度的资源限制方式,能够确保受控模块的任意指令所访问的资源是已经预先确定好的唯一资源。In this way, when the processing module of the embodiment of the present application executes the target instruction, it does not directly access the resource according to the original memory access address of the target instruction, but finds the target memory access address mapped by the original memory access address in the target address space. , and access the sub at the target memory address. The resources accessible to the controlled module can be restricted from the dimension of the access address of the target instruction. The resource pointed to by the target memory access address is accurate and unique. Then this refined dimension of resource restriction can ensure the access of the controlled module. The resources accessed by any instruction are the only resources that have been predetermined.
可选地,处理模块还可执行控制模块发送的非受控类指令,以在控制模块的内存访问权限内,访问内存中的任意地址空间。Optionally, the processing module can also execute uncontrolled instructions sent by the control module to access any address space in the memory within the memory access permissions of the control module.
可选地,在受控模块运行之前,如果控制模块扫描到受控模块包括非受控类指令时,则控制模块可提示错误,以拒绝执行受控模块,确保受控模块只可以访问由目标地址参数所限制的目标地址空间内的内存资源。Optionally, before the controlled module is run, if the control module scans that the controlled module includes uncontrolled instructions, the control module can prompt an error to refuse to execute the controlled module to ensure that the controlled module can only access the instructions provided by the target. The memory resources within the target address space restricted by the address parameter.
可选地,在受控模块运行之前,如果控制模块扫描到受控模块中包括非受控类指令时,并且,该非受控类指令为上述第三类指令(被处理模块执行时,处理模块可访问代码),例如非受控类的跳转指令。那么控制模块在检测到该非受控类的跳转指令所跳转访问的函数是预设函数(可包括下文所述的第一预设函数和第二预设函数)时,则也可以说明受控模块(例如插件)是安全的,控制模块允许受控模块运行。其中,预设函数为控制模块允许受控模块访问的在受控模块的资源访问范围之外的函数。Optionally, before the controlled module is run, if the control module scans that the controlled module includes an uncontrolled instruction, and the uncontrolled instruction is the above-mentioned third type instruction (when executed by the processing module, the processing module-accessible code), such as jump instructions for uncontrolled classes. Then when the control module detects that the function accessed by the jump instruction of the uncontrolled class is a preset function (which may include the first preset function and the second preset function described below), it can also explain Controlled modules (such as plug-ins) are safe, and control modules allow controlled modules to run. Among them, the preset function is a function that the control module allows the controlled module to access and is outside the resource access scope of the controlled module.
示例性的,图3a和图3b的系统,可结合以下示例来理解:Illustratively, the systems of Figure 3a and Figure 3b can be understood in conjunction with the following examples:
应用1(非受控模块的一个示例)中需要运行第三方开发的插件(受控模块的一个示例),以实现应用1的一项新功能,那么为了确保插件不会访问应用1中的敏感信息(例 如用户信息等),应用1可内嵌本申请提供的控制程序(控制模块的一个示例)。Application 1 (an example of an uncontrolled module) needs to run a plug-in developed by a third party (an example of a controlled module) to implement a new function of Application 1, then in order to ensure that the plug-in does not access sensitive data in Application 1 Information (such as user information, etc.), application 1 can embed the control program provided by this application (an example of a control module).
该控制程序可在应用1运行该插件之前,扫描插件中的指令,在扫描到插件中的指令均为受控类指令时,说明插件是安全的。然后,控制程序可对插件中的每个受控类指令设置内存资源访问范围,控制程序具体可通过非受控类指令,来将限制内存访问空间的地址参数写入本申请的CPU中的段寄存器(存储单元的一个示例)。The control program can scan the instructions in the plug-in before application 1 runs the plug-in. When the scanned instructions in the plug-in are all controlled instructions, it means that the plug-in is safe. Then, the control program can set the memory resource access range for each controlled class instruction in the plug-in. Specifically, the control program can write the address parameters that limit the memory access space into the segment in the CPU of this application through uncontrolled class instructions. Register (an example of a storage unit).
其中,将目标地址参数写入段寄存器的指令可为非受控类指令,而不可以是受控类指令,这样,可以避免上述插件在运行过程中,通过CPU执行受控类指令来修改段寄存器内的目标地址参数的情况。而目标地址参数可用于确定插件内的受控类指令可访问的目标地址空间,这样可以避免插件在运行过程中,篡改CPU内对应于该插件的指令的段寄存器内的数据,造成插件访问该目标地址空间之外的资源的情况。Among them, the instruction to write the target address parameter into the segment register can be an uncontrolled instruction, but not a controlled instruction. In this way, the above plug-in can be prevented from modifying the segment through the CPU executing controlled instructions during operation. The target address parameter in the register. The target address parameter can be used to determine the target address space accessible to controlled instructions in the plug-in. This can prevent the plug-in from tampering with the data in the segment register corresponding to the plug-in instruction in the CPU during operation, causing the plug-in to access the In the case of resources outside the target address space.
然后,应用1可以调用插件,使得插件运行后,向处理模块发送受控类指令,以使处理模块执行该受控类指令,对受控类指令中的原内存访问地址,按照段寄存器中的地址参数,在所限制的内存访问空间中确定目标内存访问地址,并在内存中访问该目标内存访问地址。Then, application 1 can call the plug-in, so that after the plug-in is run, it sends a controlled class instruction to the processing module, so that the processing module executes the controlled class instruction, and accesses the original memory address in the controlled class instruction according to the segment register. Address parameter, determine the target memory access address in the restricted memory access space, and access the target memory access address in the memory.
这样,应用1在调用第三方开发的插件时,能够在使用插件的功能的同时,确保插件只可以在限制的内存地址空间中访问数据资源和代码资源,确保插件对应用1的资源的安全访问。In this way, when Application 1 calls a plug-in developed by a third party, it can use the functions of the plug-in while ensuring that the plug-in can only access data resources and code resources in a restricted memory address space, ensuring that the plug-in has safe access to Application 1's resources. .
可选地,在控制程序扫描到插件中包括非受控指令时,说明插件不安全,控制程序可报错,以禁止插件运行。Optionally, when the control program scans and detects that the plug-in includes uncontrolled instructions, it means that the plug-in is unsafe, and the control program can report an error to prohibit the plug-in from running.
可选地,在控制程序扫描到插件中包括非受控类指令时,且该非受控类指令为非受控类的跳转指令,那么若该跳转指令所访问的函数是预设函数,则也可以说明插件是安全的。然后执行上述分配内存资源访问范围等操作,这里不再赘述。Optionally, when the control program scans that the plug-in includes an uncontrolled instruction, and the uncontrolled instruction is an uncontrolled jump instruction, then if the function accessed by the jump instruction is a preset function , it can also indicate that the plug-in is safe. Then perform the above operations such as allocating memory resource access ranges, which will not be described again here.
其中,预设函数为控制程序指定该插件可访问的外部函数,所谓外部函数即为非受控程序中的函数。Among them, the preset function specifies external functions accessible to the plug-in for the control program. The so-called external functions are functions in the uncontrolled program.
示例性的,受控模块为受控程序,该受控程序是编译后的程序(二进制数据文件,例如插件)。For example, the controlled module is a controlled program, and the controlled program is a compiled program (binary data file, such as a plug-in).
可选地,控制模块可包括编译模块,示例性的,编译模块为编译器。Optionally, the control module may include a compilation module. In an example, the compilation module is a compiler.
那么当第三方提供一款未编译的程序1时,非受控模块在调用该程序1之前,本申请的控制模块可通过编译器来对该程序1进行编译。示例性的,本申请的编译器,在对程序1内的高级语言的程序代码进行编译时,可按照受控类指令,来对程序代码进行编译,使得编译后的文件(后文也称指令文件)中只包括受控类指令。Then when a third party provides an uncompiled program 1, before the uncontrolled module calls the program 1, the control module of this application can compile the program 1 through the compiler. For example, when the compiler of this application compiles the high-level language program code in program 1, it can compile the program code according to the controlled class instructions, so that the compiled file (hereinafter also referred to as instructions) file) contains only controlled instructions.
示例性的,对于待编译的程序代码中表示将内存中的数据加载到CPU的代码,传统编译器可编译为load指令,本申请的编译器则编译为load_short指令。For example, for the code in the program code to be compiled that indicates loading data in the memory into the CPU, the traditional compiler can compile it into a load instruction, and the compiler of this application can compile it into a load_short instruction.
示例性的,对于待编译的程序代码中表示将CPU中的数据写入内存的代码,传统编译器可编译为store指令,本申请的编译器则编译为store_short指令。For example, for the code in the program code to be compiled that writes the data in the CPU into the memory, the traditional compiler can compile it into a store instruction, and the compiler of this application compiles it into a store_short instruction.
示例性的,对于待编译的程序代码中调用函数的程序代码,传统编译器可编译为jump指令,本申请的编译器编译为short_jump指令。For example, for the program code that calls a function in the program code to be compiled, the traditional compiler can compile it into a jump instruction, and the compiler of this application compiles it into a short_jump instruction.
示例性的,对于待编译的程序代码中堆栈访问数据的程序代码,传统编译器可编译 为push指令或pop指令,本申请的编译器编译为short_push指令或short_pop指令。For example, for the program code that accesses data on the stack in the program code to be compiled, the traditional compiler can compile it into a push instruction or a pop instruction, and the compiler of this application compiles it into a short_push instruction or a short_pop instruction.
示例性的,受控模块为插件(已经编译完成),那么控制模块在扫描插件后,如果扫描到非受控类指令,则说明该可能插件采用了非法编译器,即非本申请提供的编译器,或者,经本申请提供的编译器编译后,编译后的程序被手工篡改。那么控制模块可拒绝执行该插件。For example, if the controlled module is a plug-in (which has been compiled), then after the control module scans the plug-in, if it scans for uncontrolled instructions, it means that the plug-in may have used an illegal compiler, that is, a compilation not provided by this application. or, after being compiled by the compiler provided by this application, the compiled program was manually tampered with. The control module can then refuse to execute the plug-in.
示例性的,受控模块为插件,那么本申请的编译器可按照受控类指令来对插件进行编译,生成编译后的受控代码。那么控制模块在扫描受控代码中的指令时,可以确定受控代码中均为受控类指令,可以在非受控代码中运行该受控代码。For example, if the controlled module is a plug-in, then the compiler of this application can compile the plug-in according to the controlled class instructions and generate compiled controlled code. Then when the control module scans the instructions in the controlled code, it can determine that all the controlled codes are controlled instructions, and the controlled code can be run in the uncontrolled code.
可选地,受控模块也可以是不需要进行编译的模块,例如受控模块通过汇编语言编写,则在控制受控模块的资源访问范围时,可不使用本申请的编译器。Optionally, the controlled module may also be a module that does not require compilation. For example, the controlled module is written in assembly language, and the compiler of the present application may not be used when controlling the resource access scope of the controlled module.
需要说明的是,图3a中控制模块来扫描受控模块中的指令是否均为受控类指令的步骤时,运行该受控模块的CPU可为传统的任意一种CPU。但是,控制模块在执行图3a所示的发送非受控类指令至处理模块,以将限制受控模块的资源访问范围的目标地址参数,写入至存储单元的过程,则在本申请提供的CPU中实现。It should be noted that when the control module in Figure 3a scans whether the instructions in the controlled module are all controlled instructions, the CPU running the controlled module can be any traditional CPU. However, when the control module performs the process of sending uncontrolled instructions to the processing module shown in Figure 3a to write the target address parameters that limit the resource access range of the controlled module into the storage unit, the process provided by this application is implemented in CPU.
示例性的,同一个模块在不同环境下可为受控模块,也可以为非受控模块。For example, the same module can be a controlled module or an uncontrolled module in different environments.
例如,浏览器App在用户的手机上运行,但是浏览器App不是手机平台开发的,那么手机的操作系统代码可为非受控模块,该浏览器App的代码可为受控模块,为了限制浏览器App在手机上的资源访问范围,手机操作系统可内嵌本申请提供的控制模块。那么在操作系统运行该浏览器App之前,操作系统中的控制模块可扫描浏览器App是否均为受控类指令,如果是,则允许浏览器App运行,否则报错不允许浏览器App运行,以确保操作系统的资源的安全访问。For example, if the browser App runs on the user's mobile phone, but the browser App is not developed for the mobile phone platform, then the operating system code of the mobile phone can be an uncontrolled module, and the code of the browser App can be a controlled module. In order to restrict browsing According to the resource access scope of the server App on the mobile phone, the mobile phone operating system can embed the control module provided by this application. Then before the operating system runs the browser App, the control module in the operating system can scan whether the browser App is a controlled class instruction. If so, the browser App is allowed to run. Otherwise, an error is reported and the browser App is not allowed to run. Ensure secure access to operating system resources.
再如,浏览器App希望加载一个动态库,调用动态库中的函数,那么浏览器App是非受控模块,该动态库是受控模块。浏览器App无法确保动态库中的代码是否越权访问浏览器App的内容。浏览器App为了确保动态库无法访问未分配给它的资源,浏览器App可内嵌有本申请提供的控制模块,以使浏览器App在调用任何第三方平台开发的程序时,该第三方平台开发的程序只可以使用受控类指令运行,使得第三方平台开发的程序可访问的资源内容是有限的。本例中,在浏览器App调用该动态库中的函数之前,浏览器App中的控制程序可扫描动态库中的函数是否均为受控类指令,如果是,则允许动态库中的函数运行,否则报错不允许动态库中的函数运行,以确保浏览器App的资源的安全访问。For another example, if the browser App wants to load a dynamic library and call functions in the dynamic library, then the browser App is an uncontrolled module and the dynamic library is a controlled module. The browser app cannot ensure whether the code in the dynamic library has unauthorized access to the content of the browser app. In order to ensure that the dynamic library cannot access resources not allocated to it, the browser App can embed the control module provided by this application so that when the browser App calls a program developed by any third-party platform, the third-party platform The developed programs can only be run using controlled class instructions, so the resource content accessible to programs developed on third-party platforms is limited. In this example, before the browser App calls the functions in the dynamic library, the control program in the browser App can scan whether the functions in the dynamic library are all controlled class instructions. If so, the functions in the dynamic library are allowed to run. , otherwise an error will be reported and the functions in the dynamic library will not be allowed to run to ensure safe access to the resources of the browser App.
本申请实施例中将CPU内的指令被划分为受控类指令和非受控类指令,可按照上述两类指令的不同执行方式进行执行,使得CPU可支持运行不受资源访问范围限制的模块(例如非受控模块,其中,非受控模块可用于调用受控模块),以及支持运行受资源访问范围限制的模块(例如上述受控模块),丰富了CPU可执行的指令的类型,并实现了对运行在CPU中的受控模块的资源范围的访问控制。In the embodiment of this application, the instructions in the CPU are divided into controlled instructions and uncontrolled instructions, which can be executed according to different execution methods of the above two types of instructions, so that the CPU can support running modules that are not restricted by resource access ranges. (such as uncontrolled modules, where uncontrolled modules can be used to call controlled modules), and support for running modules restricted by resource access scope (such as the above-mentioned controlled modules), enriching the types of instructions that the CPU can execute, and Implements access control to the resource range of controlled modules running in the CPU.
本申请实施例中,受控类指令在被CPU执行时,CPU只可以访问对该受控类指令所分配的有限的资源访问范围,那么在受控模块运行之前,由控制模块扫描受控模块内的指令是否均为受控类指令,可在受控模块运行之前,通过控制模块来实现对受控模块内指令类型的检查。若控制模块检测到受控模块内的指令均为受控类指令,则可以确定受控 模块在运行时不会访问超出其资源访问范围的资源,以确保受控模块在运行时的资源安全访问。也即在受控模块运行前,通过控制模块对受控模块进行指令扫描,可以确定该受控模块是否是安全的,在确定受控模块安全的情况下,才会运行该受控模块。本申请实施例能够在受控模块运行前,实现对第三方提供的受控模块的安全检查,无需在受控模块运行过程中,实时检查每个待执行的指令是否安全,提升了对受控模块的运行效率。In the embodiment of the present application, when a controlled class instruction is executed by the CPU, the CPU can only access a limited resource access range allocated to the controlled class instruction. Then, before the controlled module is run, the control module scans the controlled module. Whether the instructions in the controlled module are all controlled instructions, the control module can be used to check the type of instructions in the controlled module before running the controlled module. If the control module detects that the instructions in the controlled module are all controlled instructions, it can be sure that the controlled module will not access resources beyond its resource access scope at runtime to ensure safe access to resources by the controlled module at runtime. . That is, before the controlled module is run, the control module performs an instruction scan on the controlled module to determine whether the controlled module is safe. The controlled module will be run only when it is determined that the controlled module is safe. The embodiments of this application can realize the security check of the controlled module provided by a third party before the controlled module is run. There is no need to check in real time whether each instruction to be executed is safe during the running of the controlled module, which improves the security of the controlled module. module operating efficiency.
本申请的控制模块可用于内嵌代码的场景下,即一个角色实例(包括但不限于用户态进程、虚拟机、操作系统等)中的非受控模块,需要执行内嵌该非受控模块的受控模块的场景,内嵌在非受控模块中的控制模块可区分受控类指令和非受控类指令,以在受控模块运行之前扫描受控模块是否包括非受控类指令,来决定受控模块是否可被非受控模块调用而运行,能够实现对外部模块的安全性校验。处理模块可执行的指令被划分为受控类指令和非受控类指令,处理模块在执行受控类指令时,可在控制模块对该受控类指令所设置的有限的地址空间内进行资源访问,以确保外部模块对非受控模块的资源的安全访问。The control module of this application can be used in the scenario of embedded code, that is, an uncontrolled module in a role instance (including but not limited to user mode process, virtual machine, operating system, etc.), which needs to be executed to embed the uncontrolled module In the scenario of a controlled module, the control module embedded in the uncontrolled module can distinguish between controlled instructions and uncontrolled instructions to scan whether the controlled module includes uncontrolled instructions before running the controlled module. To determine whether the controlled module can be called and run by the uncontrolled module, it can implement security verification of external modules. The instructions executable by the processing module are divided into controlled instructions and uncontrolled instructions. When the processing module executes the controlled instructions, it can perform resources within the limited address space set by the control module for the controlled instructions. Access to ensure safe access by external modules to resources of uncontrolled modules.
实施例2Example 2
可选地,图4a示例性的示出了本申请的系统的运行流程图,结合于图3a和图3b,请参照图4a和图4b,控制模块可实现为控制代码,受控模块可实现为受控代码,非受控模块可实现为非受控代码。Optionally, Figure 4a exemplarily shows the operation flow chart of the system of the present application. Combined with Figures 3a and 3b, please refer to Figures 4a and 4b. The control module can be implemented as control code, and the controlled module can implement To be controlled code, uncontrolled modules can be implemented as uncontrolled code.
本申请的CPU包括处理模块,并且CPU可选地运行受控代码,可选地运行控制代码。处理模块可执行运行在CPU中的代码中的指令。The CPU of the present application includes a processing module, and the CPU optionally runs controlled code, and optionally runs control code. Processing modules execute instructions in code running in the CPU.
图4b示例性的示出了图4a实施例的内存地址空间的示意图。Figure 4b exemplarily shows a schematic diagram of the memory address space of the embodiment of Figure 4a.
在受控代码运行在本申请的CPU之前,CPU可运行控制代码的指令,那么控制代码通过处理模块来执行S101、S103以及S105。Before the controlled code runs on the CPU of the present application, the CPU can run instructions of the control code, and then the control code executes S101, S103 and S105 through the processing module.
S101,控制代码通过处理模块扫描受控代码中的指令,来确定受控代码中的指令是否均为受控类指令。S101, the control code scans the instructions in the controlled code through the processing module to determine whether the instructions in the controlled code are all controlled instructions.
示例性的,以App1调用第三方开发的插件1为例进行说明,App1中内嵌有本申请的控制代码,如图4b所示,App1的非受控代码在内存中的地址空间为d0至d3,其中,内嵌在非受控代码中的控制代码的地址空间为d1至d2,App1的非受控代码在进程1内运行。处理模块在进程1的权限内执行App1的非受控类指令,其中,d0至d3地址空间存储的是非受控类指令。As an example, take App1 calling plug-in 1 developed by a third party as an example. App1 has the control code of this application embedded in it. As shown in Figure 4b, the address space of the uncontrolled code of App1 in the memory is from d0 to d3, where the address space of the control code embedded in the uncontrolled code is d1 to d2, and the uncontrolled code of App1 is running in process 1. The processing module executes the uncontrolled instructions of App1 within the authority of process 1, where the address spaces d0 to d3 store uncontrolled instructions.
示例性的,非受控代码可包括非受控类指令,可选地也可以包括受控类指令,本申请对此不做限制。For example, the uncontrolled code may include uncontrolled instructions, and optionally may also include controlled instructions, which is not limited by this application.
示例性的,控制代码可向处理模块发出非受控类指令,以访问进程1权限内的任意地址空间。可选地,控制代码也可以包括受控类指令,本申请对此不做限制。For example, the control code can issue uncontrolled instructions to the processing module to access any address space within the permissions of process 1. Optionally, the control code may also include controlled instructions, which is not limited by this application.
示例性的,如图4b的虚线箭头所示,控制代码可访问内存中的地址空间d3至d4,地址空间d4至d5,地址空间d6至d7。需要说明的是,这只是示例,并不用于限制本申请。For example, as shown by the dotted arrow in Figure 4b, the control code can access address spaces d3 to d4, address spaces d4 to d5, and address spaces d6 to d7 in the memory. It should be noted that this is only an example and is not used to limit this application.
可选地,App1或控制代码,可将插件1的代码(即受控代码)写入内存中,以实现 处理模块从内存中读取控制代码的指令,以执行控制代码的指令来对插件1的指令的扫描。Optionally, App1 or the control code can write the code of plug-in 1 (ie, the controlled code) into the memory to implement the processing module to read the instructions of the control code from the memory to execute the instructions of the control code to control plug-in 1 Scanning of instructions.
其中,处理模块执行的指令为控制代码中的指令,该指令被处理模块执行时,处理模块可将插件1中的指令作为处理模块读取的数据进行扫描,扫描插件1中的指令是否均为受控类指令。Among them, the instructions executed by the processing module are instructions in the control code. When the instructions are executed by the processing module, the processing module can scan the instructions in plug-in 1 as the data read by the processing module, and scan whether the instructions in plug-in 1 are all Controlled instructions.
示例性的,如图4b所示,受控代码(例如插件1)在内存中存储的地址空间为d4至d5,那么控制代码可扫描地址空间d4至d5中的受控代码,以检测受控代码中的指令是否均为受控类指令。For example, as shown in Figure 4b, the address space of the controlled code (such as plug-in 1) stored in the memory is d4 to d5, then the control code can scan the controlled code in the address space d4 to d5 to detect the controlled code. Whether the instructions in the code are all controlled instructions.
可选地,在控制代码检测到受控代码中包括非受控类指令时,则提示错误,以拒绝执行受控代码。Optionally, when the control code detects that the controlled code includes uncontrolled instructions, an error is prompted to refuse to execute the controlled code.
示例性的,在控制代码检测到受控代码中的指令均为受控类指令时,则转至S103。For example, when the control code detects that the instructions in the controlled code are all controlled instructions, the process goes to S103.
S103,控制代码对受控代码确定第一地址空间和第一地址参数,以及第二地址空间和第二地址参数。S103: The control code determines the first address space and first address parameters, as well as the second address space and second address parameters for the controlled code.
如上述实施例所述,受控类指令可分为第一类指令和第二类指令。As described in the above embodiments, controlled instructions can be divided into first type instructions and second type instructions.
示例性的,以限制访问范围的资源为内存资源为例,第一类指令在被CPU执行时,CPU可访问内存中的代码资源并运行该代码资源。For example, taking the resource with limited access scope as a memory resource, when the first type of instruction is executed by the CPU, the CPU can access the code resource in the memory and run the code resource.
第一类指令可包括但不限于:short_jump指令。The first type of instructions may include but is not limited to: short_jump instructions.
第二类指令在被CPU执行时,CPU可访问数据资源。第二类指令包括但不限于:load_short指令、store_short指令,short_push指令、short_pop指令等。When the second type of instructions is executed by the CPU, the CPU can access data resources. The second type of instructions includes but is not limited to: load_short instruction, store_short instruction, short_push instruction, short_pop instruction, etc.
那么控制代码在对受控代码中的每个受控类指令分配目标资源访问范围时,控制代码可对受控代码中的第一类指令分配第二资源访问范围,对第二类指令分配第三资源访问范围。其中,所述第二资源访问范围和所述第三资源访问范围之间的资源访问地址不同。Then when the control code allocates the target resource access scope to each controlled class instruction in the controlled code, the control code can allocate the second resource access scope to the first class instruction in the controlled code, and assign the second resource access scope to the second class instruction. Three resource access scopes. Wherein, the resource access addresses between the second resource access scope and the third resource access scope are different.
示例性的,控制代码在对受控代码中的第一类指令分配第二资源访问范围时,控制代码可对受控代码中的每个第一类指令分配第一地址空间,以及与该第一地址空间对应的第一地址参数。For example, when the control code allocates the second resource access scope to the first type of instruction in the controlled code, the control code may allocate the first address space to each first type of instruction in the controlled code, and communicate with the first type of instruction in the controlled code. The first address parameter corresponding to an address space.
其中,第一地址空间用于存储受控代码中受控类指令可访问的代码资源。The first address space is used to store code resources accessible to controlled class instructions in the controlled code.
示例性的,第一地址参数可用于确定第一地址空间的地址范围。For example, the first address parameter may be used to determine the address range of the first address space.
示例性的,控制代码在对受控代码中的第一类指令分配第三资源访问范围时,控制代码可对受控代码中的每个第二类指令分配第二地址空间,以及与该第二地址空间对应的第二地址参数。For example, when the control code allocates the third resource access scope to the first type of instructions in the controlled code, the control code can allocate the second address space to each second type of instruction in the controlled code, and communicate with the third type of instruction. The second address parameter corresponding to the second address space.
其中,第二地址空间用于存储受控代码中受控类指令可访问的数据资源。The second address space is used to store data resources accessible to controlled class instructions in the controlled code.
示例性的,第二地址参数可用于确定第二地址空间的地址范围。For example, the second address parameter may be used to determine the address range of the second address space.
如图4b所示,在控制代码扫描受控代码之前,控制代码已经将受控代码写入至地址空间d4至d5,那么在控制代码检测到受控代码中的指令均为受控类指令时,可将地址空间d4至d5作为分配给受控代码的第一地址空间。As shown in Figure 4b, before the control code scans the controlled code, the control code has written the controlled code to the address space d4 to d5, then when the control code detects that the instructions in the controlled code are all controlled instructions , address spaces d4 to d5 can be used as the first address space allocated to the controlled code.
此外,控制代码还可在非受控代码可访问的内存的地址空间中,确定分配给受控代码的第二地址空间。Additionally, the control code may determine a second address space allocated to the controlled code within an address space of memory that is not accessible to the controlled code.
可选地,控制代码在对受控代码中的受控类指令,分配第一地址空间和第二地址空间时,对于所分配的地址空间的空间大小,可按照控制代码所内嵌至的非受控代码(例如控制代码所内嵌的上述App1)对该受控代码(例如插件1)所规定的预定大小的空间,或者根据受控代码的需求,来确定对受控代码所分配的第一地址空间和第二地址空间的大小。Optionally, when the control code allocates the first address space and the second address space to the controlled class instructions in the controlled code, the size of the allocated address space can be determined according to the size of the non-direct address space into which the control code is embedded. The controlled code (such as the above-mentioned App1 embedded in the control code) specifies a predetermined size space for the controlled code (such as plug-in 1), or determines the space allocated to the controlled code according to the needs of the controlled code. The size of the first address space and the second address space.
如图4b所示,第一地址空间为地址空间d4至d5,即受控代码可访问的代码所存储的地址空间;第二地址空间为地址空间d6至d7,即受控代码可访问的数据(简称“受控数据”)所存储的地址空间。As shown in Figure 4b, the first address space is the address space d4 to d5, that is, the address space where the code accessible to the controlled code is stored; the second address space is the address space d6 to d7, that is, the data accessible to the controlled code (referred to as "controlled data") is stored in the address space.
需要说明的是,第一地址空间和第二地址空间之间的地址不同。受控代码可通过第一类指令,来访问内存中存储在第一地址空间内的代码,以及通过第二类指令,来访问内存中存储在第二地址空间中的数据。如果第一地址空间和第二地址空间存在重叠的地址,那么重叠的地址可存储代码以及数据,那么受控代码可在控制代码扫描其指令均为受控类指令之后,通过例如store_short指令,来访问该重叠的地址,并修改该重复的地址处的代码,使得受控代码中的代码(也即指令)被修改为非受控类指令,从而能够在所限制的第一地址空间和第二地址空间之外进行数据或代码的访问。本申请通过对受控代码中的受控类指令,配置不同的第一地址空间和第二地址空间,以免受控代码在经扫描通过允许运行后,受控代码再次修改内部代码,使受控代码包括非受控类指令,以访问未分配的内存资源,从而避过控制代码的扫描。It should be noted that the addresses between the first address space and the second address space are different. The controlled code can access the code stored in the first address space in the memory through the first type of instructions, and access the data stored in the second address space in the memory through the second type of instructions. If there are overlapping addresses in the first address space and the second address space, then the overlapping addresses can store code and data, and then the controlled code can use, for example, the store_short instruction after the control code scans that all the instructions are controlled class instructions. Access the overlapping address, and modify the code at the repeated address, so that the code (ie, instruction) in the controlled code is modified into an uncontrolled class instruction, so that it can be used between the restricted first address space and the second Access data or code outside the address space. This application configures different first address spaces and second address spaces for the controlled class instructions in the controlled code, so as to prevent the controlled code from modifying the internal code again after being scanned and allowed to run, making the controlled code The code includes uncontrolled instructions to access unallocated memory resources, thus evading scanning by the controlling code.
示例性的,控制代码对受控代码中的第一类指令分配了第一地址空间之后,控制代码可确定第一地址空间对应的第一地址参数。For example, after the control code allocates the first address space to the first type of instruction in the controlled code, the control code may determine the first address parameter corresponding to the first address space.
示例性的,第一地址参数可用于确定第一地址空间的地址范围,可选地,第一地址参数用于对第一类指令中的第一访问地址,确定在该第一地址空间中映射的第二访问地址。Exemplarily, the first address parameter can be used to determine the address range of the first address space. Optionally, the first address parameter is used to determine the mapping of the first access address in the first type of instruction in the first address space. the second access address.
那么控制代码可基于第一地址空间的地址范围,按照第一预设算法来确定第一地址参数。Then the control code can determine the first address parameter according to the first preset algorithm based on the address range of the first address space.
需要说明的是,第一预设算法的具体算法的差异,可使得所确定的第一地址参数的参数类型也可存在区别,并申请对于第一地址参数的具体参数不做限制,对于基于第一地址空间的地址范围来确定第一地址参数的第一预设算法也不做限制。第一预设算法以及后续实施例提及的第二预设算法、第三预设算法及第四预设算法,均可以是任意一种能够实现限制受控代码的资源访问范围的功能的算法,本申请对于具体算法不做限制。It should be noted that the difference in the specific algorithm of the first preset algorithm may also cause the parameter type of the determined first address parameter to be different, and the application does not impose restrictions on the specific parameters of the first address parameter. The first preset algorithm for determining the first address parameter based on the address range of an address space is also not limited. The first preset algorithm and the second preset algorithm, the third preset algorithm and the fourth preset algorithm mentioned in subsequent embodiments can be any algorithm that can realize the function of limiting the resource access range of the controlled code. , this application does not impose any restrictions on the specific algorithm.
示例性的,第一地址参数可包括code_base参数和code_limit参数。For example, the first address parameter may include a code_base parameter and a code_limit parameter.
其中,code_base参数可表示第一地址空间的起始地址(即首地址),code_limit参数可以是第一地址空间的地址范围参数(可理解为第一地址空间的长度),包括但不限于空间大小,或控制空间大小的二进制掩码等。Among them, the code_base parameter can represent the starting address of the first address space (i.e., the first address), and the code_limit parameter can be the address range parameter of the first address space (can be understood as the length of the first address space), including but not limited to the space size. , or a binary mask to control the size of the space, etc.
示例性的,控制代码,对受控代码中的第一类指令所分配的第一地址空间可以是连续或者不连续的地址空间,本申请对此不做限制。For example, the first address space allocated by the control code to the first type of instructions in the controlled code may be a continuous or discontinuous address space, and this application does not limit this.
同理,处理模块在执行控制代码中的第一类指令时,也可以按照相应的第一预设算法,来基于data_base参数和data_limit参数,对第一类指令中携带的第一访问地址进 行计算,以得到限制在第一地址空间内的最终访问的第二访问地址。In the same way, when the processing module executes the first type of instruction in the control code, it can also calculate the first access address carried in the first type of instruction based on the data_base parameter and the data_limit parameter according to the corresponding first preset algorithm. , to obtain the second access address restricted to the final access within the first address space.
例如,图4b中受控代码的第一地址空间为连续的地址空间d4至d5。For example, the first address space of the controlled code in Figure 4b is the continuous address space d4 to d5.
示例性的,对于受控代码中的第一类指令在分配第一地址空间时,可对第一类指令中的不同指令,来分配相同或不同的第一地址空间。For example, when allocating the first address space to the first type of instructions in the controlled code, the same or different first address spaces may be allocated to different instructions in the first type of instructions.
同理,控制代码对受控代码中的第二类指令分配了第二地址空间之后,控制代码可确定第二地址空间对应的第二地址参数。Similarly, after the control code allocates the second address space to the second type of instruction in the controlled code, the control code can determine the second address parameter corresponding to the second address space.
控制代码确定第二地址空间对应的第二地址参数的原理,与上文描述的确定第一地址空间对应的第一地址参数的原理相同。The principle by which the control code determines the second address parameter corresponding to the second address space is the same as the principle described above for determining the first address parameter corresponding to the first address space.
示例性的,第二地址参数可用于确定第二地址空间的地址范围,可选地,第二地址参数用于对第二类指令中的第三访问地址,确定在该第二地址空间中映射的第四访问地址。Exemplarily, the second address parameter can be used to determine the address range of the second address space. Optionally, the second address parameter is used to determine the mapping in the second address space for the third access address in the second type of instruction. The fourth access address.
那么控制代码可基于第二地址空间的地址范围,按照第三预设算法,来确定第二地址参数。Then the control code can determine the second address parameter based on the address range of the second address space and according to the third preset algorithm.
需要说明的是,第三预设算法的具体算法的差异,可使得所确定的第二地址参数的参数类型也可存在区别,并申请对于第二地址参数的具体参数不做限制,对于基于第二地址空间的地址范围来确定第二地址参数的第三预设算法也不做限制。It should be noted that the difference in the specific algorithm of the third preset algorithm may also cause the parameter type of the determined second address parameter to be different, and the application does not impose restrictions on the specific parameters of the second address parameter. The third preset algorithm for determining the second address parameter based on the address range of the second address space is also not limited.
示例性的,第二地址参数可包括data_base参数和data_limit参数。For example, the second address parameter may include a data_base parameter and a data_limit parameter.
其中,data_base参数可表示第二地址空间的起始地址(即首地址),data_limit参数可以是第二地址空间的地址范围参数(可理解为第二地址空间的长度),包括但不限于空间大小,或控制空间大小的二进制掩码等。Among them, the data_base parameter can represent the starting address of the second address space (i.e., the first address), and the data_limit parameter can be the address range parameter of the second address space (can be understood as the length of the second address space), including but not limited to the space size. , or a binary mask to control the size of the space, etc.
示例性的,控制代码,对受控代码中的第二类指令所分配的第二地址空间可以是连续或者不连续的地址空间,本申请对此不做限制。For example, the second address space allocated by the control code to the second type of instructions in the controlled code may be a continuous or discontinuous address space, and this application does not limit this.
同理,处理模块在执行控制代码中的第二类指令时,也可以按照相应的第三预设算法,来基于data_base参数和data_limit参数,对第二类指令中携带的第三访问地址进行计算,以得到限制在第二地址空间内的最终访问的第四访问地址。In the same way, when the processing module executes the second type of instruction in the control code, it can also calculate the third access address carried in the second type of instruction based on the data_base parameter and the data_limit parameter according to the corresponding third preset algorithm. , to obtain the fourth access address restricted to the final access within the second address space.
S105,控制代码写入第一地址参数和第二地址参数至处理模块中不同的存储单元。S105. The control code writes the first address parameter and the second address parameter to different storage units in the processing module.
示例性的,控制代码可将code_base参数和code_limit参数分别写入处理模块中的两个寄存器内,例如寄存器1和寄存器2,以及将data_base参数和data_limit参数分别写入处理模块中的另外两个寄存器内,例如寄存器3和寄存器4。For example, the control code can write the code_base parameter and the code_limit parameter respectively into two registers in the processing module, such as register 1 and register 2, and write the data_base parameter and data_limit parameter into two other registers in the processing module respectively. within, such as register 3 and register 4.
也就是说,第一地址参数和第二地址参数所对应的存储单元不同。That is to say, the storage units corresponding to the first address parameter and the second address parameter are different.
示例性的,上述寄存器可以是段寄存器,但是,本申请对于写入有第一地址参数或第二地址参数的存储单元的类型并不限制于寄存器,可以是处理模块内部的任何硬件存储单元,可选地,也可以是独立于处理模块之外的芯片内的硬件存储单元。For example, the above-mentioned register can be a segment register. However, this application is not limited to the type of storage unit in which the first address parameter or the second address parameter is written. It can be any hardware storage unit inside the processing module. Alternatively, it may also be a hardware storage unit in a chip that is independent of the processing module.
另外,本申请对于控制代码将第一地址参数和第二地址参数写入存储单元的时机不做限制,可以先后写入,也可以同时写入。In addition, this application does not limit the timing when the control code writes the first address parameter and the second address parameter into the storage unit. They can be written sequentially or simultaneously.
需要说明的是,将第一地址参数和第二地址参数写入存储单元的指令为控制代码中的非受控类指令,换言之,受控程序中并不包括用于写入第一地址参数和第二地址参数至存储单元的非受控类指令。例如App1所调用的插件,插件中不包括修改code_base参 数和code_limit参数的非受控类指令,以及修改data_base参数和data_limit参数的非受控类指令,且插件也无法访问能够修改code_base参数和code_limit参数,以及data_base参数和data_limit参数的非受控类指令。It should be noted that the instructions for writing the first address parameter and the second address parameter into the storage unit are uncontrolled instructions in the control code. In other words, the controlled program does not include instructions for writing the first address parameter and the second address parameter. The second address parameter is an uncontrolled instruction to a storage location. For example, the plug-in called by App1 does not include uncontrolled instructions to modify the code_base parameters and code_limit parameters, and uncontrolled instructions to modify the data_base parameters and data_limit parameters, and the plug-in cannot access the ability to modify the code_base parameters and code_limit parameters. , as well as the uncontrolled class instructions of the data_base parameter and data_limit parameter.
示例性的,受控的堆栈访问指令(例如short_push指令和short_pop指令),也是受控类指令,控制代码对该受控的堆栈访问指令执行的操作,与上述示例中对内存访问的指令执行的操作类似,这里不再一一赘述。Exemplary controlled stack access instructions (such as short_push instructions and short_pop instructions) are also controlled class instructions. The operations performed by the control code on the controlled stack access instructions are the same as those performed on the memory access instructions in the above example. The operations are similar and will not be repeated here.
示例性的,控制代码可以对受控的堆栈访问指令分配地址空间,并确定该地址空间的地址参数,例如stack_base参数(表示栈的起始地址)和stack_limit参数(表示栈的空间的大小)。以及控制代码可将stack_base参数和stack_limit参数写入寄存器,使得处理模块在执行受控代码中的short_push指令或short_pop指令时,可对待访问的栈地址addr,在stack_base参数和stack_limit参数限制的栈的地址空间范围内确定最终访问的栈地址,并进行进栈,或出栈的操作。For example, the control code can allocate an address space for the controlled stack access instruction and determine the address parameters of the address space, such as the stack_base parameter (indicating the starting address of the stack) and the stack_limit parameter (indicating the size of the stack space). And the control code can write the stack_base parameter and stack_limit parameter into the register, so that when the processing module executes the short_push instruction or short_pop instruction in the controlled code, the stack address to be accessed addr, the address of the stack limited by the stack_base parameter and stack_limit parameter Determine the stack address finally accessed within the space range, and perform push or pop operations.
在S105之后,控制代码将允许受控代码运行,那么非受控代码可调用受控代码,以使得本申请的图3a中的CPU可运行受控代码,那么处理模块可执行受控代码的指令。After S105, the control code will allow the controlled code to run, then the uncontrolled code can call the controlled code, so that the CPU in Figure 3a of the present application can run the controlled code, then the processing module can execute the instructions of the controlled code .
对于上述S101至S105,以示例来说明:示例性的,可结合图4b来理解该示例,在App1调用插件1之前,App1的代码运行在进程1中,处理模块在进程1的权限内工作,那么处理模块可执行内嵌在App1中的控制代码中的非受控类指令,来执行以下操作:对插件1中的指令进行扫描,并在扫描确认均为受控类指令后,对插件1中的第一类指令分配可访问代码的第一地址空间d4至d5,以及对第二类指令分配可访问数据的第二地址空间d6至d7;以及确定第一地址空间的code_base参数和code_limit参数,第二地址空间的data_base参数和data_limit参数,并将这些参数写入处理模块的寄存器中,其中,不同参数可写入不同的寄存器。然后,App1可调用插件1,如果插件1(即受控代码)未写入内存,则处理模块可执行App1的非受控类指令,来将受控代码写入第一地址空间d4至d5。然后,经App1调用插件1,使得插件1运行后,处理模块可执行插件1中的受控类指令。For the above S101 to S105, an example is used to illustrate: This example can be understood in conjunction with Figure 4b. Before App1 calls plug-in 1, the code of App1 runs in process 1, and the processing module works within the permissions of process 1. Then the processing module can execute the uncontrolled instructions embedded in the control code in App1 to perform the following operations: scan the instructions in plug-in 1, and after the scan confirms that they are controlled instructions, The first type of instructions in allocate the first address space d4 to d5 of accessible code, and the second type of instructions allocate the second address space d6 to d7 of accessible data; and determine the code_base parameter and code_limit parameter of the first address space. , the data_base parameter and data_limit parameter of the second address space, and write these parameters into the register of the processing module, where different parameters can be written into different registers. Then, App1 can call plug-in 1. If plug-in 1 (ie, the controlled code) is not written into the memory, the processing module can execute the uncontrolled class instructions of App1 to write the controlled code into the first address space d4 to d5. Then, plug-in 1 is called through App1, so that after plug-in 1 is run, the processing module can execute the controlled class instructions in plug-in 1.
示例性的,如图4a所示,在S105之后,可选地包括S201、S203和S205。For example, as shown in Figure 4a, after S105, S201, S203 and S205 are optionally included.
S201,处理模块接收受控代码发送的用于访问代码资源的第一类指令。S201. The processing module receives the first type of instruction sent by the controlled code for accessing code resources.
示例性的,第一类指令为short_jump指令,该指令可携带第一访问地址addr。For example, the first type of instruction is the short_jump instruction, which can carry the first access address addr.
S203和S205:处理模块可执行该第一类指令,来从第二存储单元读取第一地址参数,然后,基于该第一地址参数和第一访问地址,来在第一地址空间中确定第二访问地址,并从位于第一地址空间内的第二访问地址处访问代码资源。S203 and S205: The processing module can execute the first type of instruction to read the first address parameter from the second storage unit, and then determine the first address parameter in the first address space based on the first address parameter and the first access address. two access addresses, and access code resources from the second access address located in the first address space.
示例性的,处理模块从第二访问地址处访问的代码资源为受控类的跳转指令,例如short_jump指令。For example, the code resource accessed by the processing module from the second access address is a jump instruction of a controlled class, such as a short_jump instruction.
示例性的,处理模块可从寄存器1和寄存器2中分别读取code_base参数和code_limit参数。For example, the processing module can read the code_base parameter and code_limit parameter from register 1 and register 2 respectively.
示例性的,code_base参数为第一地址空间的首地址,code_limit参数为控制第一地址空间大小的二进制掩码。For example, the code_base parameter is the first address of the first address space, and the code_limit parameter is a binary mask that controls the size of the first address space.
示例性的,处理模块在执行第一类指令时,可按照第二预设算法code_base+[code_limit(mask)OR addr],来在由code_base和code_limit限制的第一地址空间中,对该第一类指令中的原内存访问地址addr进行映射,得到限制在第一地址空间内的目标访问地址。For example, when executing the first type of instruction, the processing module can follow the second preset algorithm code_base+[code_limit(mask)OR addr] to execute the first type of instruction in the first address space limited by code_base and code_limit. The original memory access address addr in the instruction is mapped to obtain the target access address limited to the first address space.
例如,第一地址空间为0至ffff,那么可以确定code_base=0x00000000,code_limit=0x0000ffff。For example, if the first address space is 0 to ffff, then it can be determined that code_base=0x00000000 and code_limit=0x0000ffff.
例如受控代码中的short_jump指令访问的第一访问地址addr1为0x12345678,addr1并不在第一地址空间0至ffff的范围内,那么本申请的处理模块在执行该short_jump指令时,可对code_limit和addr1进行与运算,使得addr的高8位变为0,得到addr2为0x00005678。其中,addr2的地址长度在code_limit限制的长度内。然后,处理模块计算code_base+addr2,得到第二访问地址addr3,这里code_base为0,因此,addr3=addr2,显然short_jump指令的跳转地址被限制在addr3(这里为0x00005678),这不会产生错误,而且能够使受控代码仅访问第一地址空间内的资源。其中,addr3在0至ffff的地址范围内(code_base和code_limit限制的第一地址空间)。For example, the first access address addr1 accessed by the short_jump instruction in the controlled code is 0x12345678, and addr1 is not in the range of 0 to ffff in the first address space. Then the processing module of this application can modify code_limit and addr1 when executing the short_jump instruction. Perform an AND operation so that the high 8 bits of addr become 0, and the result of addr2 is 0x00005678. Among them, the address length of addr2 is within the length limit of code_limit. Then, the processing module calculates code_base+addr2 and obtains the second access address addr3. Here code_base is 0. Therefore, addr3=addr2. Obviously, the jump address of the short_jump instruction is limited to addr3 (here is 0x00005678). This will not cause an error. Moreover, the controlled code can only access resources in the first address space. Among them, addr3 is in the address range from 0 to ffff (the first address space limited by code_base and code_limit).
相比于CPU处理每个指令,均进行查页表的遍历操作,本申请的CPU在执行受控类指令时,只需要对受控类指令中的原访问地址,按照相应算法,将该原访问地址映射到所限制的地址空间内,可得到在限制的地址空间内的目标地址,计算目标地址的速度相比于查页表的速度更快,CPU的指令执行效率更高。Compared with the CPU that processes each instruction by performing a page lookup table traversal operation, when the CPU of this application executes a controlled class instruction, it only needs to access the original access address in the controlled class instruction and convert the original access address according to the corresponding algorithm. The access address is mapped to the restricted address space, and the target address in the restricted address space can be obtained. The speed of calculating the target address is faster than that of looking up the page table, and the CPU's instruction execution efficiency is higher.
需要说明的是,上述第一预设算法与这里的第二预设算法是相互对应的算法,在第一预设算法变化的情况下,第二预设算法同样发生变化。It should be noted that the above-mentioned first preset algorithm and the second preset algorithm here are algorithms that correspond to each other. When the first preset algorithm changes, the second preset algorithm also changes.
需要说明的是,本申请对于上述第一预设算法、第二预设算法以及第一地址参数的参数类型均不作限制,可采用传统的任意一种能够实现本申请的功能的算法,来将超出限制的地址范围的地址进行处理,使得处理后的地址在该地址范围内。It should be noted that this application does not limit the parameter types of the above-mentioned first preset algorithm, second preset algorithm and first address parameter. Any traditional algorithm that can realize the functions of this application can be used to convert the Addresses that exceed the restricted address range are processed so that the processed addresses are within the address range.
此外,本申请对于受控类指令中所携带的内存访问地址(例如上述第一访问地址)的数量也不做限制,可以是一个或多个第一访问地址,但不论是多少个内存访问地址,处理模块在执行该受控类指令时,均需要基于第二预设算法来对受控类指令所携带的第一访问地址进行计算,来在对该受控类指令所分配的内存的第一地址空间中,确定该最终可访问的第二访问地址。In addition, this application does not limit the number of memory access addresses (such as the above-mentioned first access address) carried in controlled instructions. It can be one or more first access addresses, but no matter how many memory access addresses there are When the processing module executes the controlled class instruction, it needs to calculate the first access address carried by the controlled class instruction based on the second preset algorithm to calculate the first access address of the memory allocated to the controlled class instruction. In an address space, the final accessible second access address is determined.
示例性的,例如S201中的第一类指令为short_jump指令,那么如图4b所示,受控代码可在受控代码内部进行跳转,例如受控代码内的函数调用。For example, if the first type of instruction in S201 is a short_jump instruction, then as shown in Figure 4b, the controlled code can jump within the controlled code, such as a function call within the controlled code.
本申请实施例的处理模块在接收到受控代码中的第一类指令,例如short_jump指令时,可在存储该short_jump指令的地址参数的段寄存器中(例如寄存器1和寄存器2)分别读取code_base参数和code_limit参数,以利用这两个参数对short_jump指令的访问地址(即跳转地址)进行处理,使得处理后的访问地址限制的第一地址空间,以实现代码资源的有限访问。When the processing module of the embodiment of the present application receives the first type of instruction in the controlled code, such as the short_jump instruction, it can respectively read code_base in the segment register (such as register 1 and register 2) that stores the address parameter of the short_jump instruction. parameter and code_limit parameter to use these two parameters to process the access address (i.e. jump address) of the short_jump instruction, so that the processed access address is the first address space restricted to achieve limited access to code resources.
可选地,如图4a所示,在S105之后,可选地包括S301、S303和S305。Optionally, as shown in Figure 4a, after S105, S301, S303 and S305 are optionally included.
S301,处理模块接收受控代码发送的用于访问数据资源的第二类指令。S301. The processing module receives the second type of instruction sent by the controlled code for accessing data resources.
示例性的,第二类指令为load_short指令,该指令可携带第二访问地址addr。For example, the second type of instruction is the load_short instruction, which can carry the second access address addr.
S303和S305:处理模块可执行该第二类指令,来从第二存储单元读取第二地址参数,然后,基于该第二地址参数和第三访问地址,来在第二地址空间中确定第四访问地址,并从位于第二地址空间内的第四访问地址处访问数据资源。S303 and S305: The processing module can execute the second type of instruction to read the second address parameter from the second storage unit, and then determine the second address parameter in the second address space based on the second address parameter and the third access address. four access addresses, and access the data resource from the fourth access address located in the second address space.
示例性的,处理模块可从寄存器3和寄存器4中分别读取data_base参数和data_limit参数。For example, the processing module can read the data_base parameter and data_limit parameter from register 3 and register 4 respectively.
示例性的,data_base参数为第二地址空间的首地址,data_limit参数为控制第二地址空间大小的二进制掩码。For example, the data_base parameter is the first address of the second address space, and the data_limit parameter is a binary mask that controls the size of the second address space.
示例性的,处理模块在执行第二类指令时,可按照第四预设算法data_base+[data_limit(mask)OR addr],来得到该第二类指令中的内存访问地址addr,在由data_base和data_limit限制的第二地址空间中,对addr进行映射,得到可访问的内存地址。For example, when the processing module executes the second type of instruction, it can obtain the memory access address addr in the second type of instruction according to the fourth preset algorithm data_base+[data_limit(mask)OR addr]. After that, data_base and data_limit In the restricted second address space, addr is mapped to obtain the accessible memory address.
例如,第二地址空间为0至ffff,那么可以确定data_base=0x00000000,data_limit=0x0000ffff。For example, if the second address space is 0 to ffff, then it can be determined that data_base=0x00000000 and data_limit=0x0000ffff.
例如受控代码中的load_short指令访问的第三访问地址addr1为0x12345678,addr1并不在第二地址空间0至ffff的范围内,那么本申请的处理模块在执行该load_short指令时,可对data_limit和addr1进行与运算,使得addr的高8位变为0,得到addr2为0x00005678。其中,addr2的地址长度在data_limit限制的长度内。然后,处理模块计算data_base+addr2,得到第四访问地址addr3,这里data_base为0,因此,addr3=addr2,显然addr3在0至ffff的地址范围内(data_base和data_limit限制的第二地址空间)。For example, the third access address addr1 accessed by the load_short instruction in the controlled code is 0x12345678, and addr1 is not in the range of 0 to ffff in the second address space. Then the processing module of this application can modify data_limit and addr1 when executing the load_short instruction. Perform an AND operation so that the high 8 bits of addr become 0, and the result of addr2 is 0x00005678. Among them, the address length of addr2 is within the length limit of data_limit. Then, the processing module calculates data_base+addr2 and obtains the fourth access address addr3. Here data_base is 0. Therefore, addr3=addr2. Obviously addr3 is in the address range from 0 to ffff (the second address space restricted by data_base and data_limit).
需要说明的是,基于第二地址空间在确定第二地址参数时所用的第三预设算法的区别,可使第二地址参数存在差别,那么处理模块在执行第二类指令(例如读内存指令load_short)时,在基于第二地址参数来对第二类指令中的第三访问地址进行计算,来得到在第二地址空间中的第四访问地址时,所用的第四预设算法也可存在区别,并不限制于上述算法。It should be noted that, based on the difference in the third preset algorithm used in determining the second address parameter in the second address space, the second address parameter may be different, then the processing module executes the second type of instruction (such as a memory read instruction). load_short), when calculating the third access address in the second type of instruction based on the second address parameter to obtain the fourth access address in the second address space, the fourth default algorithm used may also exist The difference is not limited to the above algorithms.
需要说明的是,本申请对于上述第三预设算法、第四预设算法以及第二地址参数的参数类型均不作限制,可采用传统的任意一种能够实现本申请的限制受控代码的资源访问范围的功能的算法,来作为本申请的算法。It should be noted that this application does not limit the parameter types of the above-mentioned third preset algorithm, fourth preset algorithm and second address parameter, and any traditional resource that can implement the restricted controlled code of the present application can be used. The algorithm that accesses the range function is used as the algorithm of this application.
此外,本申请对于受控类指令中所携带的内存访问地址(例如上述第三访问地址)的数量也不做限制,可以是一个或多个第三访问地址,但不论是多少个内存访问地址,处理模块在执行该受控类指令时,均需要基于第二预设算法来对受控类指令所携带的第三访问地址进行计算,来在对该受控类指令所分配的内存的第二地址空间中,确定该最终可访问的第访问地址。In addition, this application does not limit the number of memory access addresses (such as the above-mentioned third access address) carried in controlled instructions. It can be one or more third access addresses, but no matter how many memory access addresses there are When the processing module executes the controlled class instruction, it needs to calculate the third access address carried by the controlled class instruction based on the second preset algorithm to calculate the third access address of the memory allocated to the controlled class instruction. In the second address space, determine the finally accessible access address.
示例性的,例如S301中的第二类指令为load_short指令或store_short指令,那么如图4b所示,受控代码可访问地址空间d6至d7内的受控数据进行读内存或写内存的操作。For example, if the second type of instruction in S301 is a load_short instruction or a store_short instruction, then as shown in Figure 4b, the controlled code can access the controlled data in the address space d6 to d7 to read or write the memory.
本申请实施例的处理模块在接收到受控代码中的第二类指令,例如load_short指令或store_short指令时,可在存储该这两个指令的地址参数的段寄存器,例如寄存器3 和寄存器4中读取data_base参数和data_limit参数,以利用这两个参数对oadshort指令或store_short指令所请求访问的地址(即访问地址)进行处理,使得处理后的访问地址限制的第二地址空间,以实现数据资源的有限访问。When the processing module of the embodiment of the present application receives the second type of instruction in the controlled code, such as the load_short instruction or the store_short instruction, it can store the address parameters of the two instructions in the segment register, such as register 3 and register 4. Read the data_base parameter and data_limit parameter to use these two parameters to process the address (i.e., access address) requested by the oadshort instruction or the store_short instruction, so that the processed access address is limited to the second address space to implement data resources limited access.
示例性的,控制代码可对受控代码中第二类指令中的不同指令,分配不同的第二地址空间,使得受控代码对应的第二地址空间可为多个。For example, the control code may allocate different second address spaces to different instructions in the second type of instructions in the controlled code, so that there may be multiple second address spaces corresponding to the controlled code.
例如对load_short指令分配一组data_base参数1和data_limit参数1,以及对store_short指令分配一组data_base参数2和data_limit参数2,并且data_base参数1和data_limit参数1被处理模块写入寄存器21和寄存器22,data_base参数2和data_limit参数2被处理模块写入寄存器23和寄存器23。For example, the load_short instruction is assigned a set of data_base parameter 1 and data_limit parameter 1, and the store_short instruction is assigned a set of data_base parameter 2 and data_limit parameter 2, and the data_base parameter 1 and data_limit parameter 1 are written to register 21 and register 22 by the processing module, data_base Parameter 2 and data_limit parameter 2 are written to register 23 and register 23 by the processing module.
示例性的,以受控类指令中的load_short指令为例,load_short指令可划分为多种load_short指令,例如load_shortN,N=1,2,3…n中的任意一个数值。对于其他受控类指令原理类似,这里不再赘述。For example, taking the load_short instruction in the controlled class instruction as an example, the load_short instruction can be divided into multiple load_short instructions, such as load_shortN, N=any one of 1, 2, 3...n. The principles for other controlled instructions are similar and will not be described again here.
可选地,如上文所述load_short指令可包括CPU的执行方式不同的多种不同编码的load_short指令,那么控制代码可对不同编码的load_short指令分配不同的地址参数(包括data_base参数和data_limit参数),使得不同种load_short指令对应的地址参数的取值不同。进而使得不同种load_short指令可访问的地址空间不同。Optionally, as mentioned above, the load_short instruction can include multiple differently encoded load_short instructions with different execution methods of the CPU. Then the control code can assign different address parameters (including data_base parameters and data_limit parameters) to different encoded load_short instructions, This makes the address parameters corresponding to different types of load_short instructions have different values. This makes the address spaces accessible to different load_short instructions different.
可选地,控制代码也可以对一种load_short指令分配多组地址参数(即多组data_base参数和data_limit参数),以使得load_short指令可访问的地址空间是不连续的地址空间。Optionally, the control code can also assign multiple sets of address parameters (ie, multiple sets of data_base parameters and data_limit parameters) to a load_short instruction, so that the address space accessible by the load_short instruction is a discontinuous address space.
那么处理模块在接收到受控代码中的load_short指令时,就可以从寄存器21和寄存器22读取data_base参数1和data_limit参数1,以在data_base参数1和data_limit参数1限制的地址空间内执行数据读取。那么处理模块在接收到受控代码中的store_short指令时,就可以从寄存器231和寄存器23读取data_base参数2和data_limit参数2,以在data_base参数2和data_limit参数2限制的地址空间内执行数据写入。Then when the processing module receives the load_short instruction in the controlled code, it can read data_base parameter 1 and data_limit parameter 1 from register 21 and register 22 to perform data reading within the address space limited by data_base parameter 1 and data_limit parameter 1. Pick. Then when the processing module receives the store_short instruction in the controlled code, it can read data_base parameter 2 and data_limit parameter 2 from register 231 and register 23 to perform data writing within the address space limited by data_base parameter 2 and data_limit parameter 2. enter.
在本申请实施例中,可在受控模块运行之前,例如受控模块中的指令执行之前,由控制模块通过处理模块来对受控模块中的指令进行扫描,在确定受控模块中的指令只包括受控类指令时,则可允许受控代码运行。在受控模块运行在CPU中时,处理模块不需要对受控模块发送的需要执行的每个指令进行查页表等操作,可按照受控类指令,和非受控类指令各自的编码,以不同解释执行方式进行执行。本申请不仅可将对受控代码在运行时的安全判断放到了受控代码运行前,实现了对受控模块的安全检查,同时还可提升受控代码的运行效率,降低硬件的实现和运行成本。In the embodiment of the present application, before the controlled module is run, for example, before the instructions in the controlled module are executed, the control module scans the instructions in the controlled module through the processing module, and determines the instructions in the controlled module. When only controlled class instructions are included, controlled code is allowed to run. When the controlled module is running in the CPU, the processing module does not need to perform operations such as page table lookup for each instruction sent by the controlled module that needs to be executed. It can be coded according to the respective codes of controlled instructions and uncontrolled instructions. Execution with different interpretive execution methods. This application can not only place the security judgment on the controlled code when it is running before the controlled code is run, but also realize the security check of the controlled module. It can also improve the operating efficiency of the controlled code and reduce the implementation and operation of the hardware. cost.
实施例3Example 3
可选地,结合于图3a和图3b,图4a和图4b,图5a示例性的示出了本申请的系统的运行流程图,图5b示例性的示出了图5a实施例的内存地址空间的示意图。Optionally, combined with Figure 3a and Figure 3b, Figure 4a and Figure 4b, Figure 5a exemplarily shows the operation flow chart of the system of the present application, and Figure 5b exemplarily shows the memory address of the embodiment of Figure 5a Schematic diagram of space.
图4a和图5a中相同的步骤表示相同的含义,图4b和图5b中相同的图示和箭头也表示相同的含义,本实施例不再一一赘述,可参考图4a和图4b的实施例。The same steps in Figures 4a and 5a have the same meanings, and the same diagrams and arrows in Figures 4b and 5b also have the same meanings. This embodiment will not be described in detail one by one. Please refer to the implementation of Figures 4a and 4b. example.
示例性的,非受控代码可包括非受控类指令,可选地也可以包括受控类指令,本申请对此不做限制。非受控代码包括控制代码,那么控制代码可包括非受控类指令,可选地也可以包括非受控类指令,本申请对此不做限制。For example, the uncontrolled code may include uncontrolled instructions, and optionally may also include controlled instructions, which is not limited by this application. The uncontrolled code includes control code, then the control code may include uncontrolled instructions, and optionally may also include uncontrolled instructions, which is not limited in this application.
在本实施例中,对于允许运行的受控代码仍旧不可以包括非受控指令,只可以包括受控类指令,但是为了能够向受控代码提供非受控代码中的服务(例如获取系统时间的服务),那么本申请实施例可在受控代码的第一地址空间(例如code_base参数和code_limit参数所限定的内存地址空间)中补充代理代码(代理子模块的一种示例),该代理代码可用于跳转至第一地址空间之外的非受控代码中的第一预设地址。例如,受控代码在需要调用非受控代码中的函数(简称“外部函数”)时,可首先跳转到代理代码,然后,再通过代理代码跳转到所需要访问的外部函数,通过二次跳转来实现受控代码对外部函数的调用。In this embodiment, the controlled code that is allowed to run still cannot include uncontrolled instructions, but only controlled instructions. However, in order to be able to provide services in uncontrolled code to the controlled code (such as obtaining the system time) service), then the embodiment of the present application can supplement the agent code (an example of an agent sub-module) in the first address space of the controlled code (for example, the memory address space defined by the code_base parameter and the code_limit parameter). The agent code Can be used to jump to the first preset address in uncontrolled code outside the first address space. For example, when controlled code needs to call a function in uncontrolled code (referred to as "external function"), it can first jump to the proxy code, and then jump to the external function that needs to be accessed through the proxy code. Jump to implement the controlled code's call to the external function.
这里在介绍图5a和图5b之前,首先结合图5c对本实施例的受控代码的编译过程进行描述,如上文所述,本申请的编译器,可按照受控类指令,来对程序代码进行编译,使得编译后的指令文件中只包括受控类指令。Before introducing Figures 5a and 5b, the compilation process of the controlled code in this embodiment is first described in conjunction with Figure 5c. As mentioned above, the compiler of this application can compile the program code according to the controlled class instructions. Compile so that the compiled instruction file only includes controlled class instructions.
如图5c所示,在受控代码编译前,受控代码包括函数0的代码,函数0用于调用非受控代码中的函数1,示例性的,函数1是非受控代码允许受控代码访问的外部函数,那么利用传统的编译器对受控代码编译,则受控代码可包括非受控类的跳转指令。As shown in Figure 5c, before the controlled code is compiled, the controlled code includes the code of function 0, and function 0 is used to call function 1 in the uncontrolled code. For example, function 1 is an uncontrolled code that allows controlled code If you use a traditional compiler to compile the controlled code, the controlled code may include jump instructions of uncontrolled classes.
但是,本申请可采用本申请提供的编译器,来对受控代码进行编译,使得受控代码中的函数0编译为受控类的跳转指令,这里为指令0,该指令0在被处理模块执行时可访问地址1,该地址1可为在内存中,位于编译后的受控代码所处的地址空间(例如图5b所示的地址空间d4至d5)的尾部位置d5之后的地址,例如地址1为图5b中的d51,此时地址d51还未链入有代理代码中的指令,即当前地址d51的内容为空。However, this application can use the compiler provided by this application to compile the controlled code, so that function 0 in the controlled code is compiled into a jump instruction of the controlled class, here is instruction 0, and this instruction 0 is processed Address 1 can be accessed when the module is executed. This address 1 can be an address in the memory located after the tail position d5 of the address space where the compiled controlled code is located (for example, the address spaces d4 to d5 shown in Figure 5b). For example, address 1 is d51 in Figure 5b. At this time, address d51 has not been linked to instructions in the agent code, that is, the content of current address d51 is empty.
下面对代理代码进行简要描述,从指令的角度来看,代理代码是指令序列,代理代码中的各个指令均为非受控类的跳转指令,这些非受控类的跳转指令的跳转目标即为非受控代码中允许受控代码访问的外部函数的地址。那么代理代码可包括:经非受控代码(或者说控制代码)允许,受控代码可访问的外部函数(例如函数1)的非受控类的跳转指令,例如指令1’。The following is a brief description of the agent code. From the perspective of instructions, the agent code is a sequence of instructions. Each instruction in the agent code is a jump instruction of an uncontrolled class. The jump instructions of these uncontrolled classes are The transfer target is the address of an external function in uncontrolled code that is allowed to be accessed by controlled code. Then the agent code may include: an uncontrolled class jump instruction, such as instruction 1', of an external function (such as function 1) that the controlled code can access with the permission of the uncontrolled code (or control code).
在受控代码编译完成之后,则可将代理代码链入到内存中位于受控代码的尾部的位置,例如图5b所示的代理代码位于地址空间d5至d5’,这样,编译后的受控代码中的上述指令0在被处理模块执行时所访问的地址1处可存储有代理代码中的指令1’,内存的地址空间的存储图可参照图5b。指令1’的跳转目标即为非受控代码中的函数1的首地址,例如图5b中的地址d01。After the controlled code is compiled, the agent code can be linked into the memory at the end of the controlled code. For example, the agent code shown in Figure 5b is located in the address space d5 to d5'. In this way, the compiled controlled code The above instruction 0 in the code may store the instruction 1' in the agent code at the address 1 accessed when executed by the processing module. The storage map of the address space of the memory can be referred to Figure 5b. The jump target of instruction 1’ is the first address of function 1 in the uncontrolled code, such as address d01 in Figure 5b.
编译器可配置有代理代码中的非受控类的跳转指令的地址(在对受控代码编译时,该地址还未写入代理代码中的指令),与非受控代码中可访问的预设函数之间的映射关系,例如图5c中的地址1与函数1(也即编译后的指令1’)的映射关系。The compiler can be configured with the address of the jump instruction of the uncontrolled class in the agent code (when the controlled code is compiled, the address has not yet been written to the instruction in the agent code), and the address accessible in the uncontrolled code The mapping relationship between preset functions, such as the mapping relationship between address 1 and function 1 (that is, the compiled instruction 1') in Figure 5c.
以受控代码中所调用的外部函数均为预设函数为例进行说明,在对受控代码进行编译时,编译器可按照上述映射关系,将调用函数1的代码编译为指令0,指令0在被处理模块执行时,可访问地址1。这样,编译后的受控代码在访问外部函数时,可首先访问代 理代码中位于地址1处的非受控的跳转指令,例如指令1’。处理模块再通过执行指令1’,来访问非受控模块中函数1的首地址,以实现受控代码对非受控代码中函数1的访问。,需要说明的是,在对受控代码编译时,只需要确定代理代码中相应的非受控跳转指令即将写入至内存的地址,即可实现将受控代码中访问外部函数的代码,编译为受控类的跳转指令的目的。Take the external functions called in the controlled code as preset functions as an example. When compiling the controlled code, the compiler can compile the code calling function 1 into instruction 0 and instruction 0 according to the above mapping relationship. When executed by the processing module, address 1 is accessible. In this way, when the compiled controlled code accesses the external function, it can first access the uncontrolled jump instruction located at address 1 in the proxy code, such as instruction 1’. The processing module then accesses the first address of function 1 in the uncontrolled module by executing instruction 1', so that the controlled code can access function 1 in the uncontrolled code. ,It should be noted that when compiling the controlled ,code, you only need to determine the address where the ,corresponding uncontrolled jump instruction in the agent ,code is about to be written to the memory, so that the ,code that accesses the external function in the controlled ,code can be implemented. Purpose of jump instructions compiled into controlled classes.
而在受控代码运行之后,则控制代码对受控代码、代理代码均分配了地址空间,那么受控代码通过指令0就可以找到代理代码中指令1’在内存中的地址,从而通过指令1跳转到外部函数,这里的函数1。After the controlled code runs, the control code allocates address space to both the controlled code and the agent code. Then the controlled code can find the memory address of instruction 1' in the agent code through instruction 0, and then through instruction 1 Jump to the external function, here function 1.
需要说明的是,本申请对于代理代码的编译过程,以及生成过程不做限制,控制代码中已经预先设置有编译后的代理代码。It should be noted that this application does not impose any restrictions on the compilation process and generation process of the agent code, and the compiled agent code has been preset in the control code.
示例性的,非受控类指令可包括第一预设指令集和第二预设指令集,其中,第一预设指令集内的指令可为部分或全部的非受控类的跳转指令(jump指令)。第二预设指令集内的指令可为部分或全部的非受控类的跳转指令,本申请对此不做限制。For example, the uncontrolled class instructions may include a first preset instruction set and a second preset instruction set, wherein the instructions in the first preset instruction set may be part or all of the uncontrolled class jump instructions. (jump command). The instructions in the second preset instruction set may be part or all of the uncontrolled jump instructions, and this application does not limit this.
示例性的,第一预设指令集与第二预设指令集可以相同或不同,两个指令集可以存在相同的指令。For example, the first preset instruction set and the second preset instruction set may be the same or different, and the same instructions may exist in the two instruction sets.
示例性的,对于非受控类的跳转指令(即jump指令)也可以分为多种编码的jump指令,不同编码的jump指令虽然在被CPU执行时都可以实现代码资源的访问,但是在对代码资源的访问方式上可存在区别,本申请对此不做限制。那么这里的第一预设指令集和第二预设指令集内的指令则均为jump指令,但是这两个预设指令集之间的jump指令的编码可以存在区别。例如第一预设指令集中的指令为jump1指令至jump5指令。第二预设指令集中的指令为jump6指令至jump10指令。For example, uncontrolled jump instructions (i.e. jump instructions) can also be divided into jump instructions with multiple encodings. Although jump instructions with different encodings can access code resources when executed by the CPU, they cannot be accessed when executed by the CPU. There may be differences in the access methods to code resources, and this application does not impose restrictions on this. Then, the instructions in the first preset instruction set and the second preset instruction set here are all jump instructions, but the encoding of the jump instructions between these two preset instruction sets may be different. For example, the instructions in the first preset instruction set are jump1 instructions to jump5 instructions. The instructions in the second default instruction set are jump6 instructions to jump10 instructions.
示例性的,如图5a所示,控制代码可包括代理代码(代理子模块的一种示例),参照上述图5c的介绍可知,代理代码可包括至少一个非受控类的跳转指令(即第二指令),该非受控类的跳转指令在被处理模块执行时,处理模块可访问第二预设代码资源的地址。For example, as shown in Figure 5a, the control code may include agent code (an example of an agent sub-module). Referring to the introduction of Figure 5c above, it can be seen that the agent code may include at least one uncontrolled class jump instruction (i.e. second instruction), when the uncontrolled jump instruction is executed by the processing module, the processing module can access the address of the second preset code resource.
示例性的,代理代码中的非受控类的跳转指令,可为第二预设指令集中的jump6指令至jump10指令中的至少一个指令。For example, the uncontrolled class jump instruction in the agent code may be at least one instruction from the jump6 instruction to the jump10 instruction in the second preset instruction set.
第二预设代码资源为位于第四资源访问范围内的代码资源。第四资源访问范围的访问地址,与目标资源访问范围(即上述第一地址空间和第二地址空间)的访问地址不重叠。The second preset code resource is a code resource located within the access range of the fourth resource. The access address of the fourth resource access range does not overlap with the access address of the target resource access range (ie, the above-mentioned first address space and the second address space).
示例性的,第二预设代码资源为控制代码,或者非受控代码,允许受控代码访问的位于目标资源访问范围之外的代码资源。For example, the second preset code resource is a control code, or an uncontrolled code, which is a code resource outside the target resource access scope that is allowed to be accessed by the controlled code.
示例性的,第二预设代码资源可以是第二预设函数(例如图5c中非受控代码内的函数1)。第二预设函数可以是一个或多个函数,本申请对此不做限制,那么该第二预设函数可为所述控制模块允许所述受控模块访问的外部函数。For example, the second preset code resource may be a second preset function (for example, function 1 in the uncontrolled code in Figure 5c). The second preset function may be one or more functions, which is not limited by this application. Then the second preset function may be an external function that the control module allows the controlled module to access.
示例性的,可参照图5b,代理代码可包括非受控类指令,具体包括非受控类的跳转指令(即jump指令)。代理代码中的非受控类的跳转指令的访问地址(即跳转地址)为非受控代码中第二预设代码资源的第一预设地址,第一预设地址为控制代码允许受控代码可访问的外部函数的访问地址。For example, referring to Figure 5b, the agent code may include uncontrolled class instructions, specifically including uncontrolled class jump instructions (ie, jump instructions). The access address (i.e. jump address) of the uncontrolled class jump instruction in the agent code is the first preset address of the second preset code resource in the uncontrolled code, and the first preset address is the control code allowed to be accessed. The access address of the external function accessible to the control code.
下面结合图5a和图5b进行描述,在受控代码编译完成之后,在受控代码运行在本申请的CPU之前,控制代码可通过处理模块来执行S101、S103-1以及S105。Described below in conjunction with Figure 5a and Figure 5b, after the controlled code is compiled and before the controlled code is run on the CPU of the present application, the control code can execute S101, S103-1 and S105 through the processing module.
S101,控制代码通过处理模块扫描受控代码中的指令,来确定受控代码中的指令是否均为受控类指令。S101, the control code scans the instructions in the controlled code through the processing module to determine whether the instructions in the controlled code are all controlled instructions.
本实施例的S101与实施例2中的图4a中的S101相同,执行原理也相同,这里不再赘述。S101 in this embodiment is the same as S101 in Figure 4a in Embodiment 2, and the execution principle is also the same, which will not be described again here.
可选地,在控制代码检测到受控代码中包括非受控类指令时,则提示错误,以拒绝执行受控代码。Optionally, when the control code detects that the controlled code includes uncontrolled instructions, an error is prompted to refuse to execute the controlled code.
示例性的,在控制代码检测到受控代码中的指令均为受控类指令时,则转至S103-1。For example, when the control code detects that the instructions in the controlled code are all controlled instructions, the process goes to S103-1.
S103-1,控制代码根据受控代码和代理代码各自占用的地址长度之和,对受控代码确定第一地址空间以及第一地址参数;以及,控制代码对受控代码确定第二地址空间以及第二地址参数。S103-1, the control code determines the first address space and the first address parameter for the controlled code based on the sum of the address lengths occupied by the controlled code and the agent code; and, the control code determines the second address space and the second address space for the controlled code. Second address parameter.
其中,对受控代码中的用于访问数据的第二类指令,确定第二地址空间以及第二地址参数的方法,与实施例2中的S103的相同步骤的执行逻辑类似,这里不再赘述。Among them, for the second type of instruction used to access data in the controlled code, the method of determining the second address space and the second address parameter is similar to the execution logic of the same step of S103 in Embodiment 2, and will not be described again here. .
示例性的,如图5b所示,第二地址空间为内存中的地址空间d6至d7。For example, as shown in Figure 5b, the second address space is the address space d6 to d7 in the memory.
控制代码在对受控代码确定第一地址空间时,与实施例2的区别之处可对比图4b和图5b,在控制代码中不存在代理代码的情况下,对受控代码的访问代码的第一类指令分配的第一地址空间为地址空间d4至d5。在受控代码运行时,受控代码可被写入至地址空间d4至d5,使得受控代码可访问的是自己的代码,例如受控类的跳转指令。When the control code determines the first address space for the controlled code, the difference from Embodiment 2 can be compared with Figure 4b and Figure 5b. When there is no agent code in the control code, the access code for the controlled code The first address space allocated by the first type of instructions is address space d4 to d5. When the controlled code is running, the controlled code can be written to the address space d4 to d5, so that the controlled code can access its own code, such as a jump instruction of the controlled class.
而在本实施例中,如图5b所示,对受控代码中的第一类指令(即受控类的跳转指令)所分配的第一地址空间为地址空间d4至d5’,其中,第一地址空间的地址长度是受控代码所占的地址长度(例如d5-d4),与控制代码中的代理代码所占的地址长度(例如d5’-d5)之和。In this embodiment, as shown in Figure 5b, the first address space allocated to the first type of instruction in the controlled code (that is, the jump instruction of the controlled type) is the address space d4 to d5', where, The address length of the first address space is the sum of the address length occupied by the controlled code (for example, d5-d4) and the address length occupied by the agent code in the control code (for example, d5'-d5).
那么在S105之后,或者S103-1之后,控制代码可将控制代码内的代理代码链入(例如写入)至地址空间d5至d5’。在受控代码运行时(例如S105之后),控制代码可将受控代码写入地址空间地址空间d4至d5’。这里代理代码位于受控代码的尾部位置,这样受控代码可访问代码的第一地址空间内就不仅包括受控代码,还包括代理代码。Then after S105, or after S103-1, the control code can link (for example, write) the agent code in the control code to the address space d5 to d5'. When the controlled code is running (for example, after S105), the control code may write the controlled code into the address space address spaces d4 to d5′. Here, the agent code is located at the end of the controlled code, so that the first address space of the code accessible to the controlled code includes not only the controlled code, but also the agent code.
示例性的,控制代码可通过向处理模块发送非受控类指令,来将受控代码以及控制代码中的代理代码,设置写入至地址空间d4至d5’。那么本实施例中,对受控代码中的第一类指令所分配的第一地址空间,不仅可用于存储受控代码,还可用于存储代理代码,使得第一类指令在被处理模块执行而访问代码资源时,不仅可以访问受控代码自身的代码资源,还可访问代理代码的代码资源。For example, the control code can write the controlled code and the agent code in the control code to the address space d4 to d5' by sending an uncontrolled instruction to the processing module. In this embodiment, the first address space allocated to the first type of instructions in the controlled code can not only be used to store the controlled code, but also can be used to store the agent code, so that the first type of instructions are executed by the processing module. When accessing code resources, you can access not only the code resources of the controlled code itself, but also the code resources of the agent code.
需要说明的是,在图5b中,在内存地址空间中,代理代码位于受控代码的尾部,且存储受控代码的地址空间(d4至d5)与存储代理代码的地址空间(d5至d5’)之间是连续的地址空间,那么第一地址参数是一组,例如一组code_base参数和code_limit参数。It should be noted that in Figure 5b, in the memory address space, the agent code is located at the end of the controlled code, and the address space where the controlled code is stored (d4 to d5) is different from the address space where the agent code is stored (d5 to d5' ) is a continuous address space, then the first address parameter is a group, such as a group of code_base parameters and code_limit parameters.
但是,本申请对于第一地址空间中受控代码与代理代码之间的存储顺序不做限制,例如在第一地址空间中,代理代码可以存储在受控代码的头部、尾部以及受控代码内部 等位置。此外,存储代理代码的地址空间,与存储受控代码的地址空间之间可以是不连续的,那么第一地址参数可以是多组,例如多组code_base参数和code_limit参数。However, this application does not limit the storage order between the controlled code and the agent code in the first address space. For example, in the first address space, the agent code can be stored at the head, tail and controlled code of the controlled code. Internal location. In addition, the address space where the agent code is stored may be discontinuous with the address space where the controlled code is stored, and then the first address parameter may be multiple sets, such as multiple sets of code_base parameters and code_limit parameters.
那么受控代码中的受控类的跳转指令在进行代码跳转时,不仅可以在受控代码所处的地址空间d4至d5内跳转以访问代码,还可跳转至代理代码所处的地址空间d5至d5’内,以访问代理代码中的非受控类的跳转指令。进而受控代码可通过代理代码中的非受控类的跳转指令,跳转到非受控代码所处的地址空间d0至d3,以调用允许访问的外部函数。Then when the jump instruction of the controlled class in the controlled code performs a code jump, it can not only jump within the address space d4 to d5 where the controlled code is located to access the code, but also jump to the address space where the agent code is located. Jump instructions within the address space d5 to d5' to access uncontrolled classes in the agent code. Then, the controlled code can jump to the address space d0 to d3 where the uncontrolled code is located through the jump instruction of the uncontrolled class in the agent code to call the external functions that are allowed to be accessed.
继续参照图5a,在S103-1之后,转至S105,本实施例的S105,与实施例2中图4a中的S105原理相同,这里不再赘述。Continuing to refer to Figure 5a, after S103-1, go to S105. The principle of S105 in this embodiment is the same as that of S105 in Figure 4a in Embodiment 2, and will not be described again here.
在本申请实施例中,在控制代码中包括代理代码时,代理代码可配置有允许受控代码访问的非受控代码中的第二预设函数的非受控类的跳转指令,那么在对受控代码中的第一类指令,例如受控类的跳转指令,分配允许访问的第一地址空间以及第一地址参数时,可将代理代码也写入至受控代码可访问的第一地址空间中。那么控制代码在需要调用非受控代码中的第二预设函数时,可通过首先跳转至代理代码中的非受控类的跳转指令,再通过代理代码跳转至非受控代码中的第二预设函数,以实现对受控代码之外的外部函数的有限访问。In the embodiment of the present application, when proxy code is included in the control code, the proxy code can be configured with a jump instruction of an uncontrolled class of the second preset function in the uncontrolled code that allows the controlled code to access, then in For the first type of instruction in the controlled code, such as the jump instruction of the controlled class, when allocating the first address space and the first address parameter that are allowed to be accessed, the agent code can also be written to the third address space that is accessible to the controlled code. in an address space. Then when the control code needs to call the second preset function in the uncontrolled code, it can first jump to the uncontrolled class in the agent code through the jump instruction, and then jump to the uncontrolled code through the agent code. A second preset function to enable limited access to external functions outside of controlled code.
可选地,继续参照图5a和图5b,在S105之后,可选地包括S201、S203、S205以及S207-1。Optionally, continuing to refer to Figures 5a and 5b, after S105, S201, S203, S205 and S207-1 are optionally included.
S201,处理模块接收受控代码的用于访问代码资源的第一类指令。S201. The processing module receives the first type of instruction from the controlled code for accessing code resources.
示例性的,第一类指令包括用于访问代理代码中的所述第二指令(例如jump6指令)的第二跳转类指令。Exemplarily, the first type of instructions includes a second jump type instruction used to access the second instruction (for example, jump6 instruction) in the agent code.
示例性的,第二类跳转指令为short_jump指令,该指令可携带第一访问地址addr(也即原内存访问地址addr)。For example, the second type of jump instruction is a short_jump instruction, which can carry the first access address addr (that is, the original memory access address addr).
在S201之后,所述处理模块可对所述第二跳转类指令中的原内存访问地址进行处理,并按照处理后的访问地址访问所述第二指令,具体可通过S203和S205来实现:After S201, the processing module can process the original memory access address in the second jump instruction, and access the second instruction according to the processed access address. Specifically, this can be achieved through S203 and S205:
S203和S205:处理模块可执行该第一类指令,来从第二存储单元读取第一地址参数,然后,基于该第一地址参数和第一访问地址addr,来在第一地址空间中确定第二访问地址,并从代理代码中的第二访问地址处访问代码资源(即第二指令,jump6指令),这里为非受控类的跳转指令。S203 and S205: The processing module can execute the first type of instruction to read the first address parameter from the second storage unit, and then determine in the first address space based on the first address parameter and the first access address addr. The second access address, and access the code resource (ie the second instruction, jump6 instruction) from the second access address in the agent code, here is the jump instruction of the uncontrolled class.
本实施例的S201、S203以及S205,与实施例2中图4a中的S201、S203以及S205原理相同,这里不再赘述。The principles of S201, S203 and S205 in this embodiment are the same as those of S201, S203 and S205 in Figure 4a in Embodiment 2, and will not be described again here.
S207-1,处理模块按照非受控类的跳转指令的跳转地址,从内存的第三地址空间中调用外部函数。S207-1: The processing module calls the external function from the third address space of the memory according to the jump address of the uncontrolled class jump instruction.
示例性的,所述处理模块可执行第二指令,从而按照所述第二指令(例如jump6指令)中的访问地址(即跳转地址),访问所述第二预设函数;其中,所述第二预设函数为所述控制模块允许所述受控模块访问的函数。第二预设函数为在第三地址空间中的函数。Exemplarily, the processing module can execute the second instruction to access the second preset function according to the access address (ie, jump address) in the second instruction (for example, jump6 instruction); wherein, the The second preset function is a function that the control module allows the controlled module to access. The second preset function is a function in the third address space.
其中,处理模块通过第一地址空间中受控代码内的受控类的跳转指令,跳转到第一 地址空间中代理代码内的非受控类的跳转指令(位于第二访问地址);然后,处理模块执行该非受控类的跳转指令,该非受控类的跳转指令具有跳转地址,例如第五访问地址,那么处理模块可从第五访问地址处调用第二预设函数。Among them, the processing module jumps to the jump instruction of the uncontrolled class in the agent code in the first address space (located at the second access address) through the jump instruction of the controlled class in the controlled code in the first address space. ; Then, the processing module executes the jump instruction of the uncontrolled class. The jump instruction of the uncontrolled class has a jump address, such as the fifth access address. Then the processing module can call the second preset from the fifth access address. Let function.
其中,第五访问地址为第三地址空间中的地址,第三地址空间不同于对受控代码所分配的第一地址空间以及第二地址空间。The fifth access address is an address in a third address space, and the third address space is different from the first address space and the second address space allocated to the controlled code.
示例性的,第三地址空间为非受控代码的存储空间。For example, the third address space is a storage space for uncontrolled code.
示例性的,可结合参照图5b,例如本实施例中S201中的第一类指令为受控代码中的受控类的跳转指令,例如short_jump指令,处理模块通过执行受控代码中的该short_jump指令,可从地址空间d4至d5跳转到代理代码所处的地址空间d5至d5’中的第二访问地址d51。那么处理模块可从第二访问地址d51读取到代理代码中的非受控类的跳转指令,例如jump6指令。那么处理模块可通过执行从第二访问地址d51处读取到的jump6指令,进行代码跳转,跳转到jump6指令对应的跳转地址(即上述第五访问地址),例如jump6指令所调用的外部函数的地址d01,这样,就通过代理代码实现了受控代码对非受控代码中外部函数的访问。For example, referring to Figure 5b, for example, the first type of instruction in S201 in this embodiment is a jump instruction of a controlled type in the controlled code, such as a short_jump instruction, and the processing module executes the instruction in the controlled code. The short_jump instruction can jump from address space d4 to d5 to the second access address d51 in the address space d5 to d5' where the agent code is located. Then the processing module can read the jump instruction of the uncontrolled class in the agent code from the second access address d51, such as the jump6 instruction. Then the processing module can perform a code jump by executing the jump6 instruction read from the second access address d51, and jump to the jump address corresponding to the jump6 instruction (i.e., the fifth access address mentioned above), for example, the one called by the jump6 instruction The address of the external function is d01. In this way, the controlled code can access the external function in the uncontrolled code through the proxy code.
另外,需要说明的是,代理代码中的非受控类的跳转指令只可以跳转到非受控代码中的第二预设函数的地址。In addition, it should be noted that the jump instruction of the uncontrolled class in the agent code can only jump to the address of the second preset function in the uncontrolled code.
其中,第二预设函数为非受控代码或者控制代码允许受控代码访问的、且属于非受控代码中的指定函数。Wherein, the second preset function is an uncontrolled code or a designated function in the uncontrolled code that the control code allows the controlled code to access.
可选地,上述第五访问地址可为上述第二预设函数的首地址,这样,可以避免受控代码通过代理代码跳转至非受控代码中指定函数的函数体内部,以防止受控代码在指定函数的函数体内部进行非法操作,例如篡改非受控代码等。Optionally, the above-mentioned fifth access address may be the first address of the above-mentioned second preset function. In this way, the controlled code can be prevented from jumping to the inside of the function body of the specified function in the uncontrolled code through the proxy code to prevent the controlled code from jumping. The code performs illegal operations within the function body of the specified function, such as tampering with uncontrolled code, etc.
可选地,上述第五访问地址处的代码,可设置有逻辑检查代码。换言之,各个预设函数的首地址处可设置有逻辑检查代码,该逻辑检查代码可对访问该预设函数的请求进行相关检查,在检查通过后,则处理模块可执行预设函数的代码逻辑,以向受控代码提供相关的功能。Optionally, the code at the above-mentioned fifth access address may be provided with a logical check code. In other words, a logic check code can be set at the first address of each preset function. The logic check code can perform relevant checks on requests to access the preset function. After the check passes, the processing module can execute the code logic of the preset function. , to provide relevant functionality to controlled code.
那么受控代码在通过代理代码跳转到非受控代码中,由非受控代码指定的各个外部函数的首地址位置后,这些首地址位置上的代码可以在执行外部函数的代码逻辑之前进行检查,根据检查是否通过来确定是否提供再决定是否提供各个外部函数的访问功能。Then the controlled code jumps to the uncontrolled code through the proxy code. After the first address position of each external function specified by the uncontrolled code, the code at these first address positions can be executed before the code logic of the external function is executed. Check, and then determine whether to provide the access function of each external function based on whether the check passes.
例如App1调用插件1,插件1在访问App1中的预设函数时,请求获取用户名以及密码信息,那么该预设函数的首地址可设置有检查代码,该检查代码的代码逻辑可为对访问该预设函数的请求进行检查,如果请求所需要获取的信息包括预设信息(可以是敏感信息),例如密码,则检查结果为不通过检查,从而拒绝向插件1提供该预设函数的调用,拒绝方式可包括但不限于报错等;如果请求获取的信息不包括预设信息,则执行该预设函数的代码逻辑,以允许插件1调用该预设函数。For example, App1 calls plug-in 1. When plug-in 1 accesses the preset function in App1, it requests user name and password information. Then the first address of the preset function can be set with a check code, and the code logic of the check code can be access The request for this preset function is checked. If the information required for the request includes preset information (which may be sensitive information), such as a password, the check result is that the check fails, thereby refusing to provide the call to the preset function to plug-in 1. , the rejection method may include but is not limited to reporting an error, etc.; if the requested information does not include preset information, the code logic of the preset function will be executed to allow plug-in 1 to call the preset function.
再如,受控代码请求获取系统时间,而调用获取时间函数,在该函数的首地址处设置的逻辑检查代码的检查逻辑为在系统时间在预设时间段的情况下,允许访问该获取时间函数,则非受控代码可通过该逻辑检查代码,而在预设时间段内提供获取系统时间的功能。For another example, the controlled code requests to obtain the system time and calls the get time function. The check logic of the logic check code set at the first address of the function is to allow access to the get time when the system time is within the preset time period. function, uncontrolled code can check the code through this logic and provide the function of obtaining the system time within a preset time period.
可选地,代理代码中可包括控制代码提供的,且允许受控代码访问的全部预设函数的非受控类的跳转指令。Optionally, the agent code may include jump instructions of uncontrolled classes of all preset functions provided by the control code and allowing the controlled code to access.
可选地,代理代码中也可以包括控制代码提供的,且允许受控代码访问的第二预设函数的非受控类的跳转指令,其中,第二预设函数可以是控制代码允许受控代码访问的外部函数中的部分或全部预设函数。Optionally, the agent code may also include a jump instruction of an uncontrolled class of a second preset function provided by the control code that allows the controlled code to access, where the second preset function may be a second preset function that the control code allows the controlled code to access. Some or all of the preset functions in the external functions accessed by the control code.
需要说明的是,如图5b所示,在本申请实施例中,处理模块通过执行受控代码中受控类的跳转指令,来跳转至代理代码中,以访问代理代码中的非受控类的跳转指令,以通过该非受控类的跳转指令实现外部函数的访问。但是,受控代码中用于访问数据的第二类指令(例如受控类的内存访问指令,load_short指令)所访问的地址空间限制在第二地址空间d6至d7,并不可以访问第一地址空间,从而不可以访问代理代码所处的地址空间d5至d5’,以防止受控代码通过第二类指令来修改代理代码中的代码,从而通过代理代码跳转至非法空间,防止受控代码非法调用未允许的服务。所谓非法空间即为控制代码不允许受控代码所访问的地址空间。It should be noted that, as shown in Figure 5b, in this embodiment of the present application, the processing module jumps to the agent code by executing the jump instruction of the controlled class in the controlled code to access the uncontrolled code in the agent code. The jump instruction of the controlled class is used to achieve access to external functions through the jump instruction of the uncontrolled class. However, the address space accessed by the second type of instruction used to access data in the controlled code (such as the controlled type memory access instruction, load_short instruction) is limited to the second address space d6 to d7, and the first address cannot be accessed. space, so that the address space d5 to d5' where the agent code is located cannot be accessed to prevent the controlled code from modifying the code in the agent code through the second type of instructions, thereby jumping to the illegal space through the agent code, preventing the controlled code from Illegal call to an unallowed service. The so-called illegal space is the address space that the control code does not allow the controlled code to access.
此外,需要说明的是,用于访问代码的第一类指令,可使CPU在内存中取代码;用于访问数据的第二类指令,可使CPU在内存中读写数据。那么控制代码在通过处理模块对受控代码分配地址空间时,对第一类指令分配的第一地址空间不可与对第二类指令分配的第二地址空间存在地址重叠。In addition, it should be noted that the first type of instructions used to access code allows the CPU to fetch code in memory; the second type of instructions used to access data allows the CPU to read and write data in memory. Then, when the control code allocates an address space to the controlled code through the processing module, the first address space allocated to the first type of instructions cannot overlap with the second address space allocated to the second type of instructions.
另外,需要说明的是,本实施例3中,同样可包括实施例2中所述的S301、S303以及S305,原理类似,这里不再赘述。In addition, it should be noted that Embodiment 3 may also include S301, S303 and S305 described in Embodiment 2. The principles are similar and will not be described again here.
在本申请实施例中,控制代码可在通过处理模块扫描受控代码的指令之后,在检测到受控代码中的指令均为受控类指令时,控制代码可通过处理模块在受控代码的第一地址空间中写入代理代码,使得第一地址空间中不仅包括受控代码,还可包括代理代码,那么受控代码就可以通过受控类的跳转指令跳转至代理代码,并通过代理代码中的非受控类的跳转指令,跳转到非受控代码允许受控代码访问的固定函数的位置,例如第二预设函数的首地址。这样受控代码只能进入非受控代码指定的各个函数的首地址位置上,这些位置上的代码可以做合适的检查,再决定是否提供相关的功能,以确保对第二预设函数的访问安全。In the embodiment of the present application, after the control code scans the instructions of the controlled code through the processing module, when it is detected that the instructions in the controlled code are controlled class instructions, the control code can use the processing module to scan the instructions in the controlled code. The agent code is written in the first address space so that the first address space includes not only the controlled code but also the agent code. Then the controlled code can jump to the agent code through the jump instruction of the controlled class and pass The jump instruction of the uncontrolled class in the agent code jumps to the location of the fixed function that the uncontrolled code allows the controlled code to access, such as the first address of the second preset function. In this way, the controlled code can only enter the first address position of each function specified by the uncontrolled code. The code at these positions can make appropriate checks and then decide whether to provide relevant functions to ensure access to the second preset function. Safety.
实施例4Example 4
可选地结合于图3a和图3b,图4a和图4b,图5a和图5b,图6a和图6b示例性的示出了本申请的系统的运行流程图,图6b示例性的示出了图6a实施例的内存地址空间的示意图。Optionally combined with Figures 3a and 3b, Figures 4a and 4b, Figures 5a and 5b, Figures 6a and 6b exemplarily show the operation flow chart of the system of the present application, and Figure 6b exemplarily shows A schematic diagram of the memory address space in the embodiment of Figure 6a is shown.
图4a和图6a中相同的步骤表示相同的含义,图4b和图6b中相同的图示和箭头也表示相同的含义,本实施例不再一一赘述,可参考图4a和图4b的实施例。The same steps in Figures 4a and 6a represent the same meanings, and the same diagrams and arrows in Figures 4b and 6b also represent the same meanings. This embodiment will not be described in detail one by one. Please refer to the implementation of Figures 4a and 4b. example.
在本实施例中,为了能够使受控代码可以获得一些非受控代码的服务(比如一个受控的插件执行,需要获得系统的时间),那么只需要确保受控代码中调用外部服务的非受控类指令,所访问的外部函数(即非受控代码中的函数)都是经过控制代码检查确认可以访问的第一预设函数,那么这些非受控类指令可以允许存在于受控代码中,这样, 相比于实施例3,本实施例在调用非受控代码中的外部函数时,只需要进行一次跳转,就可以实现受控代码对外部函数的调用。In this embodiment, in order to enable the controlled code to obtain some services of uncontrolled code (such as a controlled plug-in execution that needs to obtain the system time), it is only necessary to ensure that the controlled code calls the external service. For controlled instructions, the external functions accessed (that is, functions in uncontrolled code) are the first preset functions that have been confirmed to be accessible by the control code check, then these uncontrolled instructions can be allowed to exist in the controlled code In this way, compared with Embodiment 3, when calling an external function in an uncontrolled code, this embodiment only needs to make one jump to realize the call of the external function by the controlled code.
示例性的,对于非受控类的跳转指令(即jump指令)也可以分为多种编码的jump指令,不同编码的jump指令虽然在被CPU执行时都可以实现代码资源的访问,但是在对代码资源的访问方式上可存在区别,本申请对此不做限制。那么本实施例中允许受控代码包括的非受控类的跳转指令可以是第一预设指令集中的至少一个jump指令。第一预设指令集与实施例3中的第二预设指令集内的指令则均为jump指令,但是这两个预设指令集之间的jump指令的编码可以存在区别。例如第一预设指令集中的指令为jump1指令至jump5指令。第二预设指令集中的指令为jump6指令至jump10指令。当然,在其他实施例中,第一预设指令集与第二预设指令集之间也可以存在相同的非受控类的跳转指令,本申请对此不做限制。For example, uncontrolled jump instructions (i.e. jump instructions) can also be divided into jump instructions with multiple encodings. Although jump instructions with different encodings can access code resources when executed by the CPU, they cannot be accessed when executed by the CPU. There may be differences in the access methods to code resources, and this application does not impose restrictions on this. Then, the jump instruction of the uncontrolled class that is allowed to be included in the controlled code in this embodiment may be at least one jump instruction in the first preset instruction set. The instructions in the first default instruction set and the second default instruction set in Embodiment 3 are all jump instructions, but the encoding of the jump instructions may be different between the two default instruction sets. For example, the instructions in the first preset instruction set are jump1 instructions to jump5 instructions. The instructions in the second default instruction set are jump6 instructions to jump10 instructions. Of course, in other embodiments, the same uncontrolled type of jump instructions may also exist between the first preset instruction set and the second preset instruction set, and this application does not limit this.
下面介绍本申请实施例的技术方案,控制代码在扫描受控代码时,可结合重定向表来实现对受控代码中非受控类指令的检查:The technical solution of the embodiment of this application is introduced below. When the control code scans the controlled code, it can combine the redirection table to check the uncontrolled instructions in the controlled code:
示例性的,可参照图6a,在受控代码运行在本申请的CPU之前,控制代码可通过处理模块来执行S101、S102、S103-2以及S105。For example, referring to Figure 6a, before the controlled code is run on the CPU of the present application, the control code can execute S101, S102, S103-2 and S105 through the processing module.
S101,控制代码通过处理模块扫描受控代码中的指令,来确定受控代码中的指令是否均为受控类指令。S101, the control code scans the instructions in the controlled code through the processing module to determine whether the instructions in the controlled code are all controlled instructions.
本实施例的S101与实施例2中的图4a中的S101相同,执行原理也相同,这里不再赘述。S101 in this embodiment is the same as S101 in Figure 4a in Embodiment 2, and the execution principle is also the same, which will not be described again here.
可选地,在控制模块检测到受控代码中的指令均为受控类指令时,具体执行逻辑可参照图4a和图5a的实施例,这里不再赘述。Optionally, when the control module detects that the instructions in the controlled code are all controlled instructions, the specific execution logic may refer to the embodiments of FIG. 4a and FIG. 5a , which will not be described again here.
在本实施例中,控制代码通过通过处理模块对受控代码中的指令进行扫描,控制模块可检测到受控代码包括非受控类指令。如上述实施例2提及的,非受控类指令可包括但不限于用于访问代码的第三类指令和用于访问数据的第四类指令。In this embodiment, the control code scans the instructions in the controlled code through the processing module, and the control module can detect that the controlled code includes uncontrolled instructions. As mentioned in Embodiment 2 above, the uncontrolled class instructions may include, but are not limited to, third class instructions for accessing code and fourth class instructions for accessing data.
那么本申请实施例旨在允许受控代码访问非受控代码的指定函数服务,可选地,控制代码在检测到受控代码包括第四类指令,例如load指令时,则提示错误,以拒绝执行受控代码。Then the embodiment of the present application is intended to allow controlled code to access designated function services of uncontrolled code. Optionally, when the control code detects that the controlled code includes a fourth type of instruction, such as a load instruction, it prompts an error to refuse Execute controlled code.
可选地,控制代码通过处理模块在检测到受控代码包括非受控类的跳转指令,即与第一预设指令集匹配的第一指令,例如jump1指令至jump5指令中的至少一个指令,则可进一步检测第一指令的跳转目标是否满足预设条件,在受控代码中的每个第一指令的跳转目标均满足预设条件时,则说明所述第一指令为用于访问第一预设代码资源的指令。那么控制代码允许受控代码运行,否则提示出错,拒绝运行。Optionally, the control code, through the processing module, detects that the controlled code includes an uncontrolled jump instruction, that is, the first instruction that matches the first preset instruction set, such as at least one instruction from the jump1 instruction to the jump5 instruction. , then it can be further detected whether the jump target of the first instruction satisfies the preset condition. When the jump target of each first instruction in the controlled code satisfies the preset condition, it means that the first instruction is used for Instructions to access the first preset code resource. Then the control code allows the controlled code to run, otherwise it prompts an error and refuses to run.
示例性的,第一预设代码资源为控制代码,或者非受控代码,允许受控代码访问的位于目标资源访问范围之外的代码资源。For example, the first preset code resource is a control code, or an uncontrolled code, which is a code resource that is outside the access scope of the target resource and is allowed to be accessed by the controlled code.
第一预设代码资源为位于第一资源访问范围内的代码资源。第一资源访问范围(例如第四地址空间)的访问地址,与目标资源访问范围(即上述第一地址空间和第二地址空间)的访问地址不重叠。The first preset code resource is a code resource located within the first resource access range. The access address of the first resource access range (for example, the fourth address space) does not overlap with the access address of the target resource access range (that is, the first address space and the second address space).
示例性的,第一预设代码资源可以是第一预设函数。第一预设函数可以是一个或多 个函数,本申请对此不做限制,那么该第一预设函数可为所述控制模块允许所述受控模块访问的、且不属于第一地址空间和第二地址空间的外部函数。For example, the first preset code resource may be a first preset function. The first preset function may be one or more functions. This application does not limit this. Then the first preset function may be one that the control module allows the controlled module to access and does not belong to the first address space. and external functions in the second address space.
示例性的,所述第一指令为用于访问所述第一预设函数的第一跳转类指令(例如jump1指令至jump5指令中的至少一个指令);Exemplarily, the first instruction is a first jump instruction (for example, at least one instruction from the jump1 instruction to the jump5 instruction) used to access the first preset function;
示例性的,受控代码中每个第一指令均为用于访问第一预设代码资源的指令,则控制代码可确定受控代码中每个第一指令的跳转目标满足预设条件,允许受控代码运行。For example, each first instruction in the controlled code is an instruction used to access the first preset code resource, then the control code can determine that the jump target of each first instruction in the controlled code satisfies the preset condition, Allow controlled code to run.
示例性的,所述第一预设代码资源包括第一预设函数,其中,所述第一预设函数为所述控制模块允许所述受控模块访问的函数。Exemplarily, the first preset code resource includes a first preset function, where the first preset function is a function that the control module allows the controlled module to access.
例如受控代码中每个jump指令所跳转至的外部函数均为第一预设函数,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。再如受控代码中每个jump指令的跳转地址(例如第四访问地址)均为第二预设地址,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。其中,第二预设地址为控制代码允许受控代码访问的第二预设函数的地址。For example, the external function to which each jump instruction in the controlled code jumps is the first preset function, then it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. Furthermore, if the jump address (for example, the fourth access address) of each jump instruction in the controlled code is the second preset address, it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. The second preset address is the address of the second preset function that the control code allows the controlled code to access.
示例性的,如图6c所示,当受控代码中包含了对受控代码之外的函数(即上述外部函数)的调用代码,其中,受控代码中的函数0的代码用于调用非受控代码中的函数1,则编译器(可以是传统的编译器)在对受控代码编译后,编译后的文件不仅可包括指令文件(即编译后的受控代码,表现为指令序列,指令序列可包括待执行指令),还可包括重定向表。For example, as shown in Figure 6c, when the controlled code contains calling code for functions outside the controlled code (ie, the above-mentioned external functions), the code of function 0 in the controlled code is used to call non-controlled code. Function 1 in the controlled code, then after the compiler (can be a traditional compiler) compiles the controlled code, the compiled file can not only include the instruction file (that is, the compiled controlled code, which is expressed as an instruction sequence, The sequence of instructions may include instructions to be executed) and may also include a redirection table.
示例性的,受控代码中的函数0编译为非受控类的跳转指令,例如指令00,由于在受控代码运行前无法确定所访问的函数1的地址,因此,在指令文件中,指令00的跳转地址为空地址。同样的,编译后的受控代码中每个非受控类的跳转指令的跳转地址均为空地址。For example, function 0 in the controlled code is compiled into an uncontrolled jump instruction, such as instruction 00. Since the address of the accessed function 1 cannot be determined before the controlled code is run, in the instruction file, The jump address of instruction 00 is a null address. Similarly, the jump address of each uncontrolled class jump instruction in the compiled controlled code is a null address.
该重定向表可以描述指令文件中需要调用外部函数的非受控类的跳转指令,在指令文件中的位置,以及该非受控类的跳转指令所需要跳转至的外部函数的标识(例如函数名)。并且,重定向表中的这些外部函数用于表示这些外部函数的地址,需要在受控代码运行前确定。示例性的,在指令文件中,这些非受控类的跳转指令的跳转地址均为空地址。The redirection table can describe the jump instruction of an uncontrolled class that needs to call an external function in the instruction file, its location in the instruction file, and the identification of the external function that the jump instruction of the uncontrolled class needs to jump to. (e.g. function name). Moreover, these external functions in the redirection table are used to represent the addresses of these external functions, which need to be determined before the controlled code is run. For example, in the instruction file, the jump addresses of these uncontrolled class jump instructions are all empty addresses.
然后,控制代码可以扫描编译后的受控代码,在扫描到受控代码中指令后,在检测到受控代码中的非受控类指令均为非受控类的跳转指令时,可通过查询重定向表,来确定非受控类的跳转指令所需要跳转至的外部函数的标识;然后控制代码可依据该标识来确定这些待访问的外部函数是否都是第一预设函数,来确定是否允许控制代码运行。Then, the control code can scan the compiled controlled code. After scanning the instructions in the controlled code, when it detects that the uncontrolled instructions in the controlled code are all uncontrolled jump instructions, it can pass Query the redirection table to determine the identification of the external function that the uncontrolled class jump instruction needs to jump to; then the control code can use the identification to determine whether these external functions to be accessed are all first preset functions. to determine whether the control code is allowed to run.
其中,如果控制代码中待访问的外部函数均为第一预设函数,则可以允许受控代码运行,否则拒绝受控代码运行,提示出错。Among them, if the external functions to be accessed in the control code are all first preset functions, the controlled code can be allowed to run; otherwise, the controlled code is refused to run and an error is prompted.
其中,第一预设函数为控制代码(或者说非受控代码)允许受控代码,在非受控代码中可访问的位于第四地址空间的指定函数。Wherein, the first preset function is a specified function located in the fourth address space that is accessible to the control code (or uncontrolled code) and is accessible to the controlled code in the uncontrolled code.
示例性的,在控制代码确定受控代码中待访问的外部函数均为一预设函数后,控制代码可从重定向表中读取调用该外部函数的非受控类的跳转指令在指令文件中的位置,例如图6c中指令00在指令文件中的位置。然后,控制代码按照该位置,将各需要调用 的外部函数的地址信息(例如首地址)写入受控代码中相应的非受控类的跳转指令中,来作为各非受控类的跳转指令的跳转地址。例如可将函数1的首地址写入指令文件中指令00以作为指令00的访问地址(即跳转地址)。For example, after the control code determines that the external functions to be accessed in the controlled code are all preset functions, the control code can read the jump instruction of the uncontrolled class that calls the external function from the redirection table in the instruction file. The position in the instruction file, such as the position of instruction 00 in the instruction file in Figure 6c. Then, according to the position, the control code writes the address information (such as the first address) of each external function that needs to be called into the jump instruction of the corresponding uncontrolled class in the controlled code as the jump instruction of each uncontrolled class. The jump address of the transfer instruction. For example, the first address of function 1 can be written into instruction 00 in the instruction file as the access address (ie, jump address) of instruction 00.
需要说明的是,本申请对于控制代码获取各外部函数的地址信息的具体方式不做限制。It should be noted that this application does not limit the specific way in which the control code obtains the address information of each external function.
S102,控制代码通过处理模块检测到受控代码中的非受控类指令为访问代码资源的非受控类指令,且非受控类指令的跳转目标满足预设条件,则转至执行S103-2。S102, the control code detects through the processing module that the uncontrolled instructions in the controlled code are uncontrolled instructions that access code resources, and the jump target of the uncontrolled instructions meets the preset conditions, then it goes to execution S103. -2.
示例性的,控制代码在通过处理模块检测到受控代码中的每个用于访问代码资源的非受控类指令(例如每个jump指令)的跳转地址(例如第四访问地址)都是第二预设地址内的地址,则可以允许控制代码运行,以执行S103-2。Exemplarily, when the control code detects through the processing module that the jump address (for example, the fourth access address) of each uncontrolled instruction (for example, each jump instruction) used to access code resources in the controlled code is The address within the second preset address can allow the control code to run to execute S103-2.
这里的第四访问地址是控制代码,按照重定向表,对受控代码中的非受控类的跳转指令的跳转地址更新后的地址(即允许访问的外部函数的地址)The fourth access address here is the control code. According to the redirection table, the updated address of the jump address of the jump instruction of the uncontrolled class in the controlled code (that is, the address of the external function that is allowed to be accessed)
或者,示例性的,控制代码在通过处理模块检测到受控代码中的每个用于访问代码资源的非受控类指令(例如jump指令)所需要跳转至的函数均为第一预设函数,则可以允许控制代码运行,以执行S103-2。Or, for example, when the control code detects through the processing module that each uncontrolled instruction (such as a jump instruction) in the controlled code used to access code resources needs to jump to a function, it is the first preset function. function, you can allow the control code to run to execute S103-2.
S103-2,控制代码通过处理模块对受控代码中的受控类指令,确定第一地址空间和第一地址参数,以及,对受控代码中的第二址空间和第二地址参数。S103-2: The control code determines the first address space and first address parameters for the controlled class instructions in the controlled code through the processing module, and determines the second address space and second address parameters in the controlled code.
其中,本步骤的原理与图4a实施例的S103类似,相同之处不再赘述。The principle of this step is similar to S103 in the embodiment of Figure 4a, and the similarities will not be described again.
示例性的,控制代码可对受控代码中用于访问代码资源的第一类指令,确定第一地址空间,以及确定第一地址空间的第一地址参数。以及控制代码可对受控代码中用于访问数据资源的第二类指令,确定第二地址空间,以及确定第二地址空间的第二地址参数。For example, the control code may determine the first address space and the first address parameter of the first address space for the first type of instruction in the controlled code used to access the code resource. And the control code can determine the second address space for the second type of instruction used to access the data resource in the controlled code, and determine the second address parameter of the second address space.
示例性的,如图6b所示,控制代码通过处理模块,对受控代码分配的第一地址空间为内存中地址空间d4至d5,第二地址空间为内存中的地址空间d6至d7。For example, as shown in Figure 6b, the control code passes through the processing module, and the first address space allocated to the controlled code is the address space d4 to d5 in the memory, and the second address space is the address space d6 to d7 in the memory.
示例性的,虽然控制代码包括非受控类的跳转指令,但是控制代码在对受控代码中的第一类指令分配第一地址空间时,仍旧根据包括非受控类的跳转指令的受控代码所占用的地址长度,来分配地址空间d4至d5。For example, although the control code includes jump instructions of an uncontrolled class, when the control code allocates the first address space to the first type of instructions in the controlled code, it still uses the method that includes the jump instructions of the uncontrolled class. The length of the address occupied by the controlled code is used to allocate address space d4 to d5.
那么在受控代码运行时,受控代码可被写入至地址空间d4至d5,使得受控代码不仅可访问的内部的受控类指令,还可访问内部的非受控类的跳转指令。Then when the controlled code is running, the controlled code can be written to the address space d4 to d5, so that the controlled code can not only access the internal controlled class instructions, but also access the internal uncontrolled class jump instructions .
那么在S105之后,在受控代码运行时,控制代码可将受控代码写入至第一地址空间,即地址空间d4至d5’。示例性的,控制代码可通过向处理模块发送非受控类指令,来将受控代码写入至地址空间d4至d5。那么本实施例中,对受控代码中的第一类指令所分配的第一地址空间,不仅可用于存储受控代码中的受控类指令,还可以用于存储受控代码中的非受控类的跳转指令。Then after S105, when the controlled code is running, the control code can write the controlled code into the first address space, that is, the address spaces d4 to d5'. For example, the control code may write the controlled code to the address spaces d4 to d5 by sending uncontrolled instructions to the processing module. In this embodiment, the first address space allocated to the first type of instructions in the controlled code can not only be used to store the controlled type instructions in the controlled code, but can also be used to store uncontrolled instructions in the controlled code. Control class jump instructions.
那么受控代码中的受控类的跳转指令在进行代码跳转时,如受控代码对应的访问代码的虚线箭头所示,受控代码在受控代码所处的地址空间d4至d5内跳转以访问代码资源时,不仅可以访问受控类指令,还可访问非受控类的跳转指令。进而受控代码可通过受控代码中的非受控类的跳转指令,跳转到非受控代码所处的地址空间d0至d3,以调用允许访问的外部函数。Then when the jump instruction of the controlled class in the controlled code performs a code jump, as shown by the dotted arrow of the access code corresponding to the controlled code, the controlled code is within the address space d4 to d5 where the controlled code is located. When you jump to access code resources, you can access not only controlled class instructions, but also jump instructions for uncontrolled classes. Furthermore, the controlled code can jump to the address space d0 to d3 where the uncontrolled code is located through the jump instruction of the uncontrolled class in the controlled code to call the external functions that are allowed to be accessed.
继续参照图6a,在S103-2之后,转至S105,本实施例的S105,与实施例2中图4a中的S105原理相同,这里不再赘述。Continuing to refer to Figure 6a, after S103-2, go to S105. The principle of S105 in this embodiment is the same as that of S105 in Figure 4a in Embodiment 2, and will not be described again here.
在本申请实施例中,控制代码在扫描受控代码中的指令时,如果检测到非受控类指令,且非受控类指令均为与第一预设指令集匹配的非受控类的跳转指令(即第一指令),并且非受控类的跳转指令(第一指令)的跳转目标满足预设条件,例如受控代码中每个jump指令所跳转至的外部函数均为第一预设函数,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。再如受控代码中每个jump指令的跳转地址均为第二预设地址,则可以确定受控代码中每个第一指令的跳转目标满足预设条件。其中,第一预设地址为控制代码允许受控代码访问的第一预设函数的地址。那么在受控代码中非受控类指令只包括非受控类的跳转指令,且非受控类的跳转指令的跳转目标满足预设条件,则控制代码可以允许受控代码运行。本实施例中受控代码在访问外部函数时,只需要进行一次外部跳转,不需要通过代理代码进行二次跳转,受控代码的执行速度更快。只是控制代码在扫描受控代码时,扫描步骤相对实施例3更复杂,需要检查受控代码中的每个非受控类指令的跳转目标是否满足预设条件。In the embodiment of the present application, when the control code detects uncontrolled instructions when scanning instructions in the controlled code, and the uncontrolled instructions are all uncontrolled instructions that match the first preset instruction set, The jump instruction (i.e. the first instruction), and the jump target of the uncontrolled jump instruction (the first instruction) satisfies the preset conditions, for example, the external function to which each jump instruction in the controlled code jumps is is the first preset function, it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. For another example, if the jump address of each jump instruction in the controlled code is the second preset address, it can be determined that the jump target of each first instruction in the controlled code satisfies the preset condition. The first preset address is the address of the first preset function that the control code allows the controlled code to access. Then, in the controlled code, the uncontrolled class instructions only include uncontrolled class jump instructions, and the jump target of the uncontrolled class jump instructions meets the preset conditions, then the control code can allow the controlled code to run. In this embodiment, when the controlled code accesses the external function, it only needs to perform one external jump and does not need to perform a second jump through the proxy code. The execution speed of the controlled code is faster. Only when the control code scans the controlled code, the scanning steps are more complicated than in Embodiment 3, and it is necessary to check whether the jump target of each uncontrolled instruction in the controlled code meets the preset conditions.
示例性的,控制代码在扫描受控代码中的指令时,如果检查到受控代码中不仅包括受控类指令,还包括能够跳转到非受控代码的非受控类的跳转指令,则控制代码可以对非受控类的跳转指令的跳转目标进行检查。如果控制代码检测到跳转目标与非受控代码允许受控代码访问的第一预设函数相匹配的,那么虽然受控代码中包括跳转至非受控代码的非受控类的跳转指令,但是非受控类的跳转指令只可以跳转到指定的外部函数的位置,则受控代码也是安全的,那么控制代码可允许受控代码运行。该方案可确保受控代码中所使用的非受控类指令的跳转目标都是经过检查的。For example, when the control code scans the instructions in the controlled code, if it is detected that the controlled code not only includes controlled class instructions, but also includes uncontrolled class jump instructions that can jump to uncontrolled code, Then the control code can check the jump target of the uncontrolled class jump instruction. If the control code detects that the jump target matches the first preset function that the uncontrolled code allows the controlled code to access, then although the controlled code includes a jump to the uncontrolled class of the uncontrolled code, instructions, but the jump instructions of uncontrolled classes can only jump to the location of the specified external function, then the controlled code is also safe, then the control code can allow the controlled code to run. This scheme ensures that the jump targets of uncontrolled instructions used in controlled code are checked.
示例性的,控制代码在扫描到受控代码中的非受控类的跳转指令后,可查询重定向表确定每一个非受控类的跳转指令所需要跳转至的外部函数,如果这些外部函数都是调用受控代码的非受控代码允许,该受控代码跳转的第一预设函数。那么控制代码可以利用重定向表,在受控代码中找到非受控类的跳转指令的位置,在相应位置写入相应外部函数的地址(例如起始地址)。For example, after scanning the uncontrolled class jump instructions in the controlled code, the control code can query the redirection table to determine the external function that each uncontrolled class jump instruction needs to jump to. If These external functions allow uncontrolled code to call controlled code, and the controlled code jumps to the first preset function. Then the control code can use the redirection table to find the location of the jump instruction of the uncontrolled class in the controlled code, and write the address of the corresponding external function (such as the starting address) at the corresponding location.
可选地,实施例3和实施例4可作为并列的实施例则以执行,实施例3和实施例4也可以结合来同时实现。Alternatively, Embodiment 3 and Embodiment 4 can be implemented as parallel embodiments, and Embodiment 3 and Embodiment 4 can also be combined and implemented simultaneously.
示例性的,实施例4的第一预设代码资源与实施例3的第二预设代码资源可以相同。那么受控代码可通过代理代码的方式(即实施例3的方式),或,在受控代码内包括访问第一预设代码资源的非受控类的跳转指令的方式(即实施例4的方式),来实现对外部代码资源的访问,本申请对此不做限制。For example, the first preset code resource in Embodiment 4 and the second preset code resource in Embodiment 3 may be the same. Then the controlled code can be in the form of proxy code (that is, the method in Embodiment 3), or by including a jump instruction of an uncontrolled class that accesses the first preset code resource in the controlled code (that is, in Embodiment 4). method) to achieve access to external code resources, this application does not impose restrictions on this.
可选地,实施例3和实施例4的第一预设代码资源与第二预设代码资源可以不同。那么受控代码可通过代理代码的方式(即实施例3的方式),和,在受控代码内包括访问第一预设代码资源的非受控类的跳转指令的方式(即实施例4的方式),来实现对外部代码资源的访问,本申请对此不做限制。Optionally, the first preset code resource and the second preset code resource in Embodiment 3 and Embodiment 4 may be different. Then the controlled code can pass through the agent code (that is, the method of Embodiment 3), and the controlled code includes a jump instruction of an uncontrolled class that accesses the first preset code resource (that is, Embodiment 4). method) to achieve access to external code resources, this application does not impose restrictions on this.
同理,第一预设函数与第二预设函数可以相同或不同,第一指令与第二指令也可以相同或不同。Similarly, the first preset function and the second preset function may be the same or different, and the first instruction and the second instruction may also be the same or different.
可选地,继续参照图6a和图6b,在S105之后,非受控代码可调用受控代码,以使受控代码运行在CPU中,该方法可选地包括S201、S203、S205以及S207-2。Optionally, continuing to refer to Figures 6a and 6b, after S105, the uncontrolled code may call the controlled code to cause the controlled code to run in the CPU. The method optionally includes S201, S203, S205 and S207- 2.
S201,处理模块接收受控代码发送的用于访问代码资源的第一类指令。S201. The processing module receives the first type of instruction sent by the controlled code for accessing code resources.
示例性的,第一类指令为short_jump指令,该指令可携带第一访问地址addr。For example, the first type of instruction is the short_jump instruction, which can carry the first access address addr.
S203和S205:处理模块可执行该第一类指令,来从第二存储单元读取第一地址参数,然后,基于该第一地址参数和第一访问地址,来在第一地址空间中确定第二访问地址,并从第二访问地址处访问非受控类的跳转指令。这里与实施例3的区别之处在于,第二访问地址位于受控代码内部,并非代理代码内部,从而受控代码中的指令只需要完成一次外部跳转,就可以实现外部函数的调用。S203 and S205: The processing module can execute the first type of instruction to read the first address parameter from the second storage unit, and then determine the first address parameter in the first address space based on the first address parameter and the first access address. Second access address, and access the uncontrolled class jump instruction from the second access address. The difference between this and Embodiment 3 is that the second access address is located inside the controlled code, not inside the agent code. Therefore, the instructions in the controlled code only need to complete an external jump to implement the call of the external function.
本实施例的S201、S203以及S205,与实施例2中图4a中的S201、S203以及S205原理相同,这里不再赘述。The principles of S201, S203 and S205 in this embodiment are the same as those of S201, S203 and S205 in Figure 4a in Embodiment 2, and will not be described again here.
S207-2,处理模块按照非受控类的跳转指令的跳转地址,从内存的第四地址空间中调用外部函数。S207-2: The processing module calls the external function from the fourth address space of the memory according to the jump address of the uncontrolled class jump instruction.
其中,处理模块通过第一地址空间中受控代码内的受控类的跳转指令,跳转到第一地址空间中受控代码内的非受控类的跳转指令(位于第二访问地址),该跳转过程属于一次内部跳转;然后,处理模块执行该非受控类的跳转指令,该非受控类的跳转指令具有跳转地址,例如第六访问地址,那么处理模块可从第六访问地址处调用第一预设函数。Among them, the processing module jumps to the jump instruction of the uncontrolled class in the controlled code in the first address space (located at the second access address) through the jump instruction of the controlled class in the controlled code in the first address space. ), the jump process belongs to an internal jump; then, the processing module executes the jump instruction of the uncontrolled class, and the jump instruction of the uncontrolled class has a jump address, such as the sixth access address, then the processing module The first preset function can be called from the sixth access address.
其中,第六访问地址为第四地址空间中的地址,第四地址空间不同于对受控代码所分配的第一地址空间以及第二地址空间。The sixth access address is an address in a fourth address space, and the fourth address space is different from the first address space and the second address space allocated to the controlled code.
示例性的,第四地址空间为非受控代码的存储空间。For example, the fourth address space is a storage space for uncontrolled code.
示例性的,可结合参照图6b,例如本实施例中S201中的第一类指令为受控代码中的受控类的跳转指令,例如short_jump指令,处理模块通过执行受控代码中的该short_jump指令,可从地址空间d4至d5内进行受控代码的内部跳转,例如跳转到地址空间d4至d41中的非受控类的跳转指令。那么处理模块可执行地址空间d4至d41中的非受控类的跳转指令(例如jump1指令)进行代码跳转,跳转到jump1指令对应的跳转地址(即上述第六访问地址),即非受控代码中的第一预设函数的地址,这样,就通过受控代码内部的非受控类的跳转指令,实现了受控代码对非受控代码中外部函数的访问。For example, referring to Figure 6b, for example, the first type of instruction in S201 in this embodiment is a jump instruction of a controlled class in the controlled code, such as a short_jump instruction, and the processing module executes the instruction in the controlled code. The short_jump instruction can perform an internal jump of controlled code from address space d4 to d5, such as a jump instruction to an uncontrolled class in address space d4 to d41. Then the processing module can execute the uncontrolled jump instructions (such as the jump1 instruction) in the address space d4 to d41 to perform code jumps and jump to the jump address corresponding to the jump1 instruction (i.e., the sixth access address mentioned above), that is The address of the first preset function in the uncontrolled code. In this way, the controlled code can access the external function in the uncontrolled code through the jump instruction of the uncontrolled class inside the controlled code.
另外,需要说明的是,受控代码中的非受控类的跳转指令只可以跳转到非受控代码中的第一预设函数的地址。In addition, it should be noted that the jump instruction of the uncontrolled class in the controlled code can only jump to the address of the first preset function in the uncontrolled code.
其中,第一预设函数的限制可参考实施例3中的相关描述,以及第六访问地址与实施例3中的第五访问地址同理,相关描述同样可参考实施例3,这里不再一一赘述。Among them, the limitations of the first preset function may refer to the relevant description in Embodiment 3, and the sixth access address is the same as the fifth access address in Embodiment 3. The relevant description may also refer to Embodiment 3, which will not be repeated here. Let’s not go into details.
另外,需要说明的是,本实施例4中,同样可包括实施例2中所述的S301、S303以及S305,原理类似,这里不再赘述。In addition, it should be noted that Embodiment 4 may also include S301, S303 and S305 described in Embodiment 2. The principles are similar and will not be described again here.
本实施例中受控代码在访问外部函数时,只需要进行一次外部跳转,不需要通过代理代码进行二次外部跳转,CPU对受控代码的执行速度更快。In this embodiment, when the controlled code accesses the external function, it only needs to perform one external jump and does not need to perform two external jumps through the proxy code. The CPU executes the controlled code faster.
实施例5Example 5
图7为示例性示出的本申请的中央处理器(CPU)的架构图,可对比于传统技术中的图2c来看本申请提供的图7所示的中央处理器的架构图。FIG. 7 is an exemplary architectural diagram of a central processing unit (CPU) of the present application. The architectural diagram of the central processor shown in FIG. 7 provided by the present application can be viewed compared with FIG. 2c in the traditional technology.
本申请的CPU可包括运行在CPU中的任意程序实体,程序实体的可包括但不限于:一般用户程序代码、操作系统代码、虚拟机管理代码等。The CPU of this application may include any program entity running in the CPU. Program entities may include but are not limited to: general user program code, operating system code, virtual machine management code, etc.
该程序实体可包括安全区代码(即非受控代码)和非安全区代码(即受控代码),这里的非安全区代码包括非安全区代码1和非安全区代码2。The program entity may include safe area code (ie, uncontrolled code) and non-safe area code (ie, controlled code). The non-safe area code here includes non-safe area code 1 and non-safe area code 2.
以程序实体为一般用户程序代码为例,一般用户程序(例如应用1)的代码在运行在进程中,在进程1中运行的应用1的代码为安全区代码,应用1需要调用动态库以及插件(调用动态库和插件的先后顺序不做限制),那么非安全区代码1可为这里的动态库,非安全区代码2可为这里的插件。Take the program entity as a general user program code as an example. The code of the general user program (such as application 1) is running in the process. The code of application 1 running in process 1 is the safe zone code. Application 1 needs to call dynamic libraries and plug-ins. (There is no restriction on the order in which dynamic libraries and plug-ins are called.) Then the non-safe area code 1 can be the dynamic library here, and the non-safe area code 2 can be the plug-in here.
但是,应用1无法判断该动态库以及插件中的代码是否越权访问应用1的资源(例如内存资源),则可在应用1内部内嵌本申请提供的控制代码。However, Application 1 cannot determine whether the code in the dynamic library and plug-in has unauthorized access to Application 1's resources (such as memory resources), so the control code provided by this application can be embedded inside Application 1.
这样,在应用1调用动态库或插件之前,运行在CPU中的控制代码可通过向处理模块发送非受控类指令,来对动态库中的代码进行扫描,以确定动态库中只有受控类指令;同理,对插件中的代码进行扫描,以确定插件中只有受控类指令。In this way, before application 1 calls the dynamic library or plug-in, the control code running in the CPU can scan the code in the dynamic library by sending uncontrolled class instructions to the processing module to determine that there are only controlled classes in the dynamic library. Instructions; similarly, scan the code in the plug-in to ensure that there are only controlled instructions in the plug-in.
进一步的,控制代码还可为动态库分配可访问的资源的地址空间,以及为插件分配可访问的资源的地址空间,但如果我们控制这个动态库只能使用受控指令,那么这个动态库可以访问的内容就是有限的。Furthermore, the control code can also allocate the address space of accessible resources for the dynamic library, and allocate the address space of accessible resources for the plug-in. However, if we control this dynamic library and can only use controlled instructions, then this dynamic library can Access to content is limited.
然后,安全区代码(例如这里的进程1)就可以调用非安全区代码1或非安全区代码2,以在应用1中嵌入非安全区代码1或非安全区代码2。那么非安全区代码1或非安全区代码2运行在CPU中,可向CPU中的处理模块发送受控类指令,以在有限的资源访问空间内进行资源的访问。这样,应用1可使用受控代码(即非安全区代码)提供的服务,但同时又确保了非安全区代码对应用1的资源的安全访问。Then, the secure zone code (such as process 1 here) can call the non-secure zone code 1 or non-secure zone code 2 to embed the non-secure zone code 1 or non-secure zone code 2 in application 1. Then the non-safe zone code 1 or the non-safe zone code 2 runs in the CPU and can send controlled instructions to the processing module in the CPU to access resources within the limited resource access space. In this way, Application 1 can use the services provided by the controlled code (that is, the non-security zone code), but at the same time, it ensures that the non-security zone code has safe access to the resources of Application 1.
从图7以及上述描述可以看到,在确保非安全区代码(即受控代码)的资源安全访问时,本申请不需要对该非安全区代码单独创建角色实例,例如进程、容器、虚拟机等。也不需要将程序中的安全区代码(即非受控代码)与所调用的非安全区代码(即受控代码)进行分离,以将非安全区代码隔离在进程、容器、虚拟机等。也就是说,安全区代码和非安全区代码处于同一程序实体内,安全区代码与非安全区代码之间没有进行任何隔离,本申请在非安全区代码运行在CPU之前,通过控制代码扫描非安全区代码中的指令,实现了非安全区代码对安全区代码的资源范围的有限访问。这样,在任意一个实体中,就可以隔离不同代码的资源访问权限,这样CPU无需为待隔离的非安全区代码单独再创建进程、虚拟机等角色实例,降低了CPU的信令开销。As can be seen from Figure 7 and the above description, when ensuring safe access to resources of non-safe zone code (that is, controlled code), this application does not need to create separate role instances, such as processes, containers, and virtual machines, for the non-safe zone code. wait. There is no need to separate the safe zone code (i.e., uncontrolled code) in the program from the called non-safe zone code (i.e., controlled code) to isolate the non-safe zone code in processes, containers, virtual machines, etc. That is to say, the safe area code and the non-safe area code are in the same program entity, and there is no isolation between the safe area code and the non-safe area code. This application scans the non-safe area code through the control code before the non-safe area code runs on the CPU. The instructions in the safe zone code enable the non-safe zone code to have limited access to the resource range of the safe zone code. In this way, in any entity, the resource access permissions of different codes can be isolated, so that the CPU does not need to create separate role instances such as processes and virtual machines for the non-safe zone code to be isolated, which reduces the signaling overhead of the CPU.
示例性的,本申请的上述方案可应用于Serverless(Serverless computing无服务器运算)场景。所谓Serverless,是一种云计算的场景,在这种场景中,用户可提供一个函数用于快速完成一个功能。Illustratively, the above solution of this application can be applied to Serverless (Serverless computing) scenarios. The so-called Serverless is a cloud computing scenario in which users can provide a function to quickly complete a function.
其中,用户不在云服务商上申请一台服务器,来持续运行该用户提供的某个服务。用户仅提供一个函数,该函数静态地存储在云服务商的某个存储上。其他的服务(比如Web前端)请求调用该函数,才会启动该函数,完成相关的计算。为了保证这个函数的计 算过程是安全,通常需要为这个函数准备一个进程、容器甚至虚拟机,来运行该函数,以避免该函数访问超出该函数的权限范围的资源。Among them, the user does not apply for a server on the cloud service provider to continue running a service provided by the user. The user only provides a function, which is statically stored on a certain storage of the cloud service provider. Only when other services (such as Web front-end) request to call this function will the function be started and related calculations completed. In order to ensure that the calculation process of this function is safe, it is usually necessary to prepare a process, container or even a virtual machine for this function to run the function to prevent the function from accessing resources beyond the permissions of the function.
但是,在本申请中,控制模块只要检查到该函数只包含受控类指令,那么该函数在运行过程中就无法访问超出其权限范围的资源。此外,该函数的计算过程可以在调用该函数的服务(例如上述Web前端)中完成,也可以在一个用于聚合的其他进程、容器或虚拟机中完成。只需要在运行该函数的空间中分配了该函数可用的有限资源,该函数就可以完成相关计算,而无需为了运行该函数再额外创建资源(例如进程、容器、虚拟机等)。其中,创建进程、容器、虚拟机等角色实例的成本较高。However, in this application, as long as the control module checks that the function only contains controlled instructions, the function will not be able to access resources beyond its scope of authority during operation. In addition, the calculation process of this function can be completed in the service that calls the function (such as the above-mentioned web front end), or it can be completed in another process, container or virtual machine used for aggregation. Only the limited resources available to the function are allocated in the space where the function is run, and the function can complete the relevant calculations without creating additional resources (such as processes, containers, virtual machines, etc.) in order to run the function. Among them, the cost of creating role instances such as processes, containers, and virtual machines is relatively high.
在一种可能的实施方式中,本申请实施例提供一种资源控制装置。资源控制装置包括控制模块和受控模块,资源控制装置中的指令被划分为受控类指令和非受控类指令,控制模块用于:在受控模块运行之前,对受控模块内的指令进行扫描,检测资源控制装置受控模块内的指令是否均为受控类指令;在检测到受控模块内的指令均为受控类指令时,对受控模块内的每个指令分配目标资源访问范围。该资源控制装置的结构和功能可参照图3a及其相关方法实施例的描述,这里不再赘述。In a possible implementation, an embodiment of the present application provides a resource control device. The resource control device includes a control module and a controlled module. The instructions in the resource control device are divided into controlled instructions and uncontrolled instructions. The control module is used to: before the controlled module runs, control the instructions in the controlled module. Scan and detect whether the instructions in the controlled module of the resource control device are all controlled instructions; when it is detected that the instructions in the controlled module are all controlled instructions, allocate target resources to each instruction in the controlled module. Access scope. The structure and function of the resource control device can be referred to the description of Figure 3a and its related method embodiments, and will not be described again here.
在一种可能的实施方式中,资源控制装置还包括处理模块;处理模块,用于对第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源;其中,第一访问地址为受控模块内当前运行的受控类指令中的资源访问地址;其中,处理后的第一访问地址,在当前运行的受控类指令对应的目标资源访问范围内。In a possible implementation, the resource control device further includes a processing module; a processing module configured to process the first access address and access the target resource according to the processed first access address; wherein the first access address is The resource access address in the controlled class instruction currently running in the controlled module; where the processed first access address is within the target resource access range corresponding to the currently running controlled class instruction.
在一种可能的实施方式中,控制模块,还用于在检测到受控模块内的指令不均为受控类指令之后,在检测到受控模块包括与第一预设指令集匹配的第一指令、且第一指令为用于访问第一预设代码资源的指令时,对受控模块内的每个受控类指令,分配目标资源访问范围;其中,非受控类指令包括第一预设指令集;第一预设代码资源位于第一资源访问范围,且第一资源访问范围与目标资源访问范围不同。In a possible implementation, the control module is further configured to, after detecting that the instructions in the controlled module are not uniformly controlled instructions, detect that the controlled module includes a third instruction that matches the first preset instruction set. When there is an instruction, and the first instruction is an instruction for accessing the first preset code resource, a target resource access range is allocated to each controlled class instruction in the controlled module; wherein, the uncontrolled class instructions include the first Default instruction set; the first default code resource is located in the first resource access scope, and the first resource access scope is different from the target resource access scope.
在一种可能的实施方式中,第一预设代码资源包括第一预设函数,第一指令为用于访问第一预设函数的第一跳转类指令;其中,第一预设函数为控制模块允许受控模块访问的函数。In a possible implementation, the first preset code resource includes a first preset function, and the first instruction is a first jump instruction used to access the first preset function; wherein the first preset function is The control module allows the controlled module to access functions.
在一种可能的实施方式中,受控模块内的受控类指令包括:用于访问代码资源的第一类指令和用于访问数据资源的第二类指令;控制模块,具体用于:对第一类指令分配第二资源访问范围;对第二类指令分配第三资源访问范围;其中,第二资源访问范围和第三资源访问范围之间的资源访问地址不同。In a possible implementation, the controlled class instructions in the controlled module include: a first class of instructions for accessing code resources and a second class of instructions for accessing data resources; a control module, specifically used for: The first type of instruction is assigned a second resource access scope; the second type of instruction is assigned a third resource access scope; wherein the resource access addresses between the second resource access scope and the third resource access scope are different.
在一种可能的实施方式中,非受控类指令包括第二预设指令集,控制模块包括代理子模块,代理子模块包括与第二预设指令集匹配的第二指令;其中,第二指令为用于访问第二预设代码资源的指令,第二预设代码资源位于第四资源访问范围内,且第四资源访问范围与目标资源访问范围之间的资源访问地址不同;控制模块,还用于将代理子模块写入至第二资源访问范围。In a possible implementation, the uncontrolled instructions include a second preset instruction set, the control module includes an agent submodule, and the agent submodule includes a second instruction that matches the second preset instruction set; wherein, the second The instruction is an instruction for accessing the second preset code resource, the second preset code resource is located within the fourth resource access range, and the resource access address between the fourth resource access range and the target resource access range is different; the control module, Also used to write the proxy submodule to the second resource access scope.
在一种可能的实施方式中,第二预设代码资源包括第二预设函数,第一类指令包括 用于访问第二指令的第二跳转类指令;处理模块,具体用于:对第二跳转类指令中的第二访问地址进行处理,并按照处理后的第二访问地址访问第二指令;按照第二指令中的第三访问地址,访问第二预设函数;其中,第二预设函数为控制模块允许受控模块访问的函数。In a possible implementation, the second preset code resource includes a second preset function, the first type of instruction includes a second jump type instruction for accessing the second instruction; the processing module is specifically configured to: The second access address in the second jump instruction is processed, and the second instruction is accessed according to the processed second access address; the second preset function is accessed according to the third access address in the second instruction; wherein, the second The preset functions are functions that the control module allows the controlled module to access.
上述各实施方式的资源控制装置的效果和实现方式,与上述各实施方式的方法的效果类似,这里不再赘述。The effects and implementation methods of the resource control devices in the above embodiments are similar to the effects of the methods in the above embodiments, and will not be described again here.
下面介绍本申请实施例提供的一种装置。如图8所示:The following introduces a device provided by the embodiment of the present application. As shown in Figure 8:
图8为本申请实施例提供的一种资源控制装置的结构示意图。如图8所示,该资源控制装置500可包括:处理器501、收发器505,可选的还包括存储器502。Figure 8 is a schematic structural diagram of a resource control device provided by an embodiment of the present application. As shown in Figure 8, the resource control device 500 may include: a processor 501, a transceiver 505, and optionally a memory 502.
所述收发器505可以称为收发单元、收发机、或收发电路等,用于实现收发功能。收发器505可以包括接收器和发送器,接收器可以称为接收机或接收电路等,用于实现接收功能;发送器可以称为发送机或发送电路等,用于实现发送功能。The transceiver 505 may be called a transceiver unit, a transceiver, a transceiver circuit, etc., and is used to implement transceiver functions. The transceiver 505 may include a receiver and a transmitter. The receiver may be called a receiver or a receiving circuit, etc., used to implement the receiving function; the transmitter may be called a transmitter, a transmitting circuit, etc., used to implement the transmitting function.
存储器502中可存储计算机程序或软件代码或指令504,该计算机程序或软件代码或指令504还可称为固件。处理器501可通过运行其中的计算机程序或软件代码或指令503,或通过调用存储器502中存储的计算机程序或软件代码或指令504,对MAC层和PHY层进行控制,以实现本申请各实施例提供的资源控制方法。其中,处理器501可以为中央处理器(central processing unit,CPU),存储器502例如可以为只读存储器(read-only memory,ROM),或为随机存取存储器(random access memory,RAM)。Computer program or software code or instructions 504 may be stored in the memory 502, which may also be referred to as firmware. The processor 501 can control the MAC layer and the PHY layer by running the computer program or software code or instructions 503 therein, or by calling the computer program or software code or instructions 504 stored in the memory 502 to implement various embodiments of the present application. Provided resource control methods. The processor 501 may be a central processing unit (CPU), and the memory 502 may be a read-only memory (ROM) or a random access memory (RAM).
本申请中描述的处理器501和收发器505可实现在集成电路(integrated circuit,IC)、模拟IC、射频集成电路RFIC、混合信号IC、专用集成电路(application specific integrated circuit,ASIC)、印刷电路板(printed circuit board,PCB)、电子设备等上。The processor 501 and transceiver 505 described in this application can be implemented in integrated circuits (ICs), analog ICs, radio frequency integrated circuits RFICs, mixed signal ICs, application specific integrated circuits (ASICs), printed circuits on printed circuit board (PCB), electronic equipment, etc.
上述资源控制装置500还可以包括天线506,该资源控制装置500所包括的各模块仅为示例说明,本申请不对此进行限制。The above-mentioned resource control device 500 may also include an antenna 506. Each module included in the resource control device 500 is only an example and is not limited by this application.
本申请中描述的资源控制装置的范围并不限于此,而且资源控制装置的结构可以不受图8的限制。资源控制装置可以是独立的设备或者可以是较大设备的一部分。例如所述资源控制装置的实现形式可以是:The scope of the resource control device described in this application is not limited thereto, and the structure of the resource control device may not be limited by FIG. 8 . The resource control device may be a stand-alone device or may be part of a larger device. For example, the implementation form of the resource control device may be:
(1)独立的集成电路IC,或芯片,或,芯片系统或子系统;(2)具有一个或多个IC的集合,可选的,该IC集合也可以包括用于存储数据,指令的存储部件;(3)可嵌入在其他设备内的模块;(4)电子设备等等;(5)其他等等。(1) An independent integrated circuit IC, or chip, or chip system or subsystem; (2) A collection of one or more ICs. Optionally, the IC collection may also include storage for storing data and instructions. Components; (3) Modules that can be embedded in other equipment; (4) Electronic equipment, etc.; (5) Others, etc.
对于资源控制装置的实现形式是芯片或芯片系统的情况,可参见图9所示的芯片的结构示意图。图9所示的芯片包括处理器601和接口602。其中,处理器601的数量可以是一个或多个,接口602的数量可以是多个。可选的,该芯片或芯片系统可以包括存储器603。For the case where the resource control device is implemented in the form of a chip or a chip system, please refer to the schematic structural diagram of the chip shown in FIG. 9 . The chip shown in Figure 9 includes a processor 601 and an interface 602. The number of processors 601 may be one or more, and the number of interfaces 602 may be multiple. Optionally, the chip or chip system may include memory 603 .
其中,上述方法实施例涉及的各步骤的所有相关内容均可以援引到资源控制装置的对应功能模块的功能描述,在此不再赘述。Wherein, all relevant contents of each step involved in the above method embodiment can be quoted from the functional description of the corresponding functional module of the resource control device, and will not be described again here.
基于相同的技术构思,本申请实施例还提供一种计算机可读存储介质,该计算机可读存储介质存储有计算机程序,该计算机程序包含至少一段代码,该至少一段代码可由 计算机或处理器执行,以控制计算机或处理器用以实现上述方法实施例。Based on the same technical concept, embodiments of the present application also provide a computer-readable storage medium. The computer-readable storage medium stores a computer program. The computer program includes at least one section of code. The at least one section of code can be executed by a computer or processor. A computer or processor is used to control the above method embodiments.
基于相同的技术构思,本申请实施例还提供一种计算机程序产品包括软件程序,当所述软件程序被计算机或处理器执行时,用以实现上述方法实施例。Based on the same technical concept, embodiments of the present application also provide a computer program product including a software program, which, when executed by a computer or processor, is used to implement the above method embodiments.
所述程序可以全部或者部分存储在与处理器封装在一起的存储介质上,也可以部分或者全部存储在不与处理器封装在一起的存储器上。The program may be stored in whole or in part on a storage medium packaged with the processor, or in part or in whole on a memory that is not packaged with the processor.
基于相同的技术构思,本申请实施例还提供一种资源控制装置,包括一个或多个接口电路和一个或多个处理器;所述接口电路用于从存储器接收信号,并向所述处理器发送所述信号,所述信号包括存储器中存储的计算机指令;当所述处理器执行所述计算机指令时,所述处理器用于实现上述方法实施例。Based on the same technical concept, embodiments of the present application also provide a resource control device, including one or more interface circuits and one or more processors; the interface circuit is used to receive signals from the memory and send them to the processor. The signal is sent, and the signal includes computer instructions stored in the memory; when the processor executes the computer instructions, the processor is used to implement the above method embodiment.
结合本申请实施例公开内容所描述的方法或者算法的步骤可以硬件的方式来实现,也可以是由处理器执行软件指令的方式来实现。软件指令可以由相应的软件模块组成,软件模块可以被存放于随机存取存储器(Random Access Memory,RAM)、闪存、只读存储器(Read Only Memory,ROM)、可擦除可编程只读存储器(Erasable Programmable ROM,EPROM)、电可擦可编程只读存储器(Electrically EPROM,EEPROM)、寄存器、硬盘、移动硬盘、只读光盘(CD-ROM)或者本领域熟知的任何其它形式的存储介质中。一种示例性的存储介质耦合至处理器,从而使处理器能够从该存储介质读取信息,且可向该存储介质写入信息。当然,存储介质也可以是处理器的组成部分。处理器和存储介质可以位于ASIC中。The steps of the methods or algorithms described in connection with the disclosure of the embodiments of this application can be implemented in hardware or by a processor executing software instructions. Software instructions can be composed of corresponding software modules. Software modules can be stored in random access memory (Random Access Memory, RAM), flash memory, read only memory (Read Only Memory, ROM), erasable programmable read only memory ( Erasable Programmable ROM (EPROM), electrically erasable programmable read-only memory (Electrically EPROM, EEPROM), register, hard disk, removable hard disk, compact disc (CD-ROM) or any other form of storage media well known in the art. An exemplary storage medium is coupled to the processor such that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be an integral part of the processor. The processor and storage media may be located in an ASIC.
本领域技术人员应该可以意识到,在上述一个或多个示例中,本申请实施例所描述的功能可以用硬件、软件、固件或它们的任意组合来实现。当使用软件实现时,可以将这些功能存储在计算机可读介质中或者作为计算机可读介质上的一个或多个指令或代码进行传输。计算机可读介质包括计算机存储介质和通信介质,其中通信介质包括便于从一个地方向另一个地方传送计算机程序的任何介质。存储介质可以是通用或专用计算机能够存取的任何可用介质。Those skilled in the art should realize that in one or more of the above examples, the functions described in the embodiments of the present application can be implemented using hardware, software, firmware, or any combination thereof. When implemented using software, the functions may be stored on or transmitted over as one or more instructions or code on a computer-readable medium. Computer-readable media includes computer storage media and communication media including any medium that facilitates transfer of a computer program from one place to another. Storage media can be any available media that can be accessed by a general purpose or special purpose computer.
上面结合附图对本申请的实施例进行了描述,但是本申请并不局限于上述的具体实施方式,上述的具体实施方式仅仅是示意性的,而不是限制性的,本领域的普通技术人员在本申请的启示下,在不脱离本申请宗旨和权利要求所保护的范围情况下,还可做出很多形式,均属于本申请的保护之内。The embodiments of the present application have been described above in conjunction with the accompanying drawings. However, the present application is not limited to the above-mentioned specific implementations. The above-mentioned specific implementations are only illustrative and not restrictive. Those of ordinary skill in the art will Inspired by this application, many forms can be made without departing from the purpose of this application and the scope protected by the claims, all of which fall within the protection of this application.

Claims (17)

  1. 一种资源控制方法,其特征在于,应用于资源控制装置,所述资源控制装置包括控制模块和受控模块,所述资源控制装置中的指令被划分为受控类指令和非受控类指令,所述方法包括:A resource control method, characterized in that it is applied to a resource control device. The resource control device includes a control module and a controlled module. Instructions in the resource control device are divided into controlled instructions and uncontrolled instructions. , the method includes:
    在所述受控模块运行之前,所述控制模块对所述受控模块内的指令进行扫描,检测所述受控模块内的指令是否均为受控类指令;Before the controlled module is run, the control module scans the instructions in the controlled module and detects whether the instructions in the controlled module are all controlled instructions;
    在所述控制模块检测到所述受控模块内的指令均为所述受控类指令时,所述控制模块对所述受控模块内的每个指令分配目标资源访问范围。When the control module detects that all instructions in the controlled module are the controlled class instructions, the control module allocates a target resource access range to each instruction in the controlled module.
  2. 根据权利要求1所述的方法,其特征在于,所述资源控制装置还包括处理模块,所述控制模块对所述受控模块内的每个指令分配目标资源访问范围之后,所述方法还包括:The method according to claim 1, characterized in that the resource control device further includes a processing module, and after the control module allocates a target resource access range to each instruction in the controlled module, the method further includes :
    所述处理模块对第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源;The processing module processes the first access address and accesses the target resource according to the processed first access address;
    其中,所述第一访问地址为所述受控模块内当前运行的受控类指令中的资源访问地址;Wherein, the first access address is the resource access address in the controlled class instruction currently running in the controlled module;
    其中,所述处理后的第一访问地址,在所述当前运行的受控类指令对应的所述目标资源访问范围内。Wherein, the processed first access address is within the target resource access range corresponding to the currently running controlled class instruction.
  3. 根据权利要求1或2所述的方法,其特征在于,在所述控制模块检测到所述受控模块内的指令不均为所述受控类指令之后,所述方法还包括:The method according to claim 1 or 2, characterized in that, after the control module detects that the instructions in the controlled module are not uniformly the controlled class instructions, the method further includes:
    在所述控制模块检测到所述受控模块包括与第一预设指令集匹配的第一指令、且所述第一指令为用于访问第一预设代码资源的指令时,所述控制模块对所述受控模块内的每个所述受控类指令,分配所述目标资源访问范围;When the control module detects that the controlled module includes a first instruction that matches the first preset instruction set and the first instruction is an instruction for accessing a first preset code resource, the control module Allocate the target resource access scope to each controlled class instruction in the controlled module;
    其中,所述非受控类指令包括所述第一预设指令集;Wherein, the uncontrolled instructions include the first preset instruction set;
    所述第一预设代码资源位于第一资源访问范围,且所述第一资源访问范围与所述目标资源访问范围不同。The first preset code resource is located in a first resource access scope, and the first resource access scope is different from the target resource access scope.
  4. 根据权利要求3所述的方法,其特征在于,所述第一预设代码资源包括第一预设函数,所述第一指令为用于访问所述第一预设函数的第一跳转类指令;The method of claim 3, wherein the first preset code resource includes a first preset function, and the first instruction is a first jump class used to access the first preset function. instruction;
    其中,所述第一预设函数为所述控制模块允许所述受控模块访问的函数。Wherein, the first preset function is a function that the control module allows the controlled module to access.
  5. 根据权利要求1至4中任意一项所述的方法,其特征在于,所述受控模块内的所述受控类指令包括用于访问代码资源的第一类指令和用于访问数据资源的第二类指令,所述控制模块对所述受控模块内的每个所述受控类指令,分配所述目标资源访问范围,包括:The method according to any one of claims 1 to 4, characterized in that the controlled class instructions in the controlled module include first class instructions for accessing code resources and first class instructions for accessing data resources. In the second type of instructions, the control module allocates the target resource access scope to each controlled type instruction in the controlled module, including:
    所述控制模块对所述第一类指令分配第二资源访问范围;The control module allocates a second resource access scope to the first type of instruction;
    所述控制模块对所述第二类指令分配第三资源访问范围;The control module allocates a third resource access scope to the second type of instruction;
    其中,所述第二资源访问范围和所述第三资源访问范围之间的资源访问地址不同。Wherein, the resource access addresses between the second resource access scope and the third resource access scope are different.
  6. 根据权利要求1至5中任意一项所述的方法,其特征在于,所述非受控类指令包括第二预设指令集,所述控制模块包括代理子模块,所述代理子模块包括与所述第二预设指令集匹配的第二指令,其中,所述第二指令为用于访问第二预设代码资源的指令,所述第二预设代码资源位于第四资源访问范围内,且所述第四资源访问范围与所述目标资源访问范围之间的资源访问地址不同;The method according to any one of claims 1 to 5, characterized in that the uncontrolled instructions include a second preset instruction set, the control module includes an agent sub-module, and the agent sub-module includes and The second instruction matched by the second preset instruction set, wherein the second instruction is an instruction for accessing a second preset code resource, and the second preset code resource is located within the fourth resource access range, And the resource access addresses between the fourth resource access scope and the target resource access scope are different;
    所述控制模块对所述受控模块内的每个指令分配目标资源访问范围之后,所述方法还包括:After the control module allocates a target resource access range to each instruction in the controlled module, the method further includes:
    所述控制模块将所述代理子模块写入至所述第二资源访问范围。The control module writes the proxy sub-module to the second resource access scope.
  7. 根据权利要求6所述的方法,其特征在于,所述第二预设代码资源包括第二预设函数,所述第一类指令包括用于访问所述第二指令的第二跳转类指令;The method of claim 6, wherein the second preset code resource includes a second preset function, and the first type of instructions includes a second jump type instruction for accessing the second instruction. ;
    所述处理模块对所述第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源,包括:The processing module processes the first access address and accesses the target resource according to the processed first access address, including:
    所述处理模块对所述第二跳转类指令中的第二访问地址进行处理,并按照处理后的第二访问地址访问所述第二指令;The processing module processes the second access address in the second jump instruction, and accesses the second instruction according to the processed second access address;
    所述处理模块按照所述第二指令中的第三访问地址,访问所述第二预设函数;The processing module accesses the second preset function according to the third access address in the second instruction;
    其中,所述第二预设函数为所述控制模块允许所述受控模块访问的函数。Wherein, the second preset function is a function that the control module allows the controlled module to access.
  8. 一种资源控制装置,其特征在于,所述资源控制装置包括控制模块和受控模块,所述资源控制装置中的指令被划分为受控类指令和非受控类指令;所述控制模块用于:A resource control device, characterized in that the resource control device includes a control module and a controlled module, and instructions in the resource control device are divided into controlled instructions and uncontrolled instructions; the control module uses At:
    在所述受控模块运行之前,对所述受控模块内的指令进行扫描,检测所述受控模块内的指令是否均为受控类指令;Before the controlled module is run, scan the instructions in the controlled module to detect whether the instructions in the controlled module are all controlled instructions;
    在检测到所述受控模块内的指令均为所述受控类指令时,对所述受控模块内的每个指令分配目标资源访问范围。When it is detected that the instructions in the controlled module are all the controlled class instructions, a target resource access range is assigned to each instruction in the controlled module.
  9. 根据权利要求8所述的装置,其特征在于,所述资源控制装置还包括处理模块;The device according to claim 8, wherein the resource control device further includes a processing module;
    所述处理模块,用于对第一访问地址进行处理,并按照处理后的第一访问地址访问目标资源;The processing module is used to process the first access address and access the target resource according to the processed first access address;
    其中,所述第一访问地址为所述受控模块内当前运行的受控类指令中的资源访问地址;Wherein, the first access address is the resource access address in the controlled class instruction currently running in the controlled module;
    其中,所述处理后的第一访问地址,在所述当前运行的受控类指令对应的所述目标资源访问范围内。Wherein, the processed first access address is within the target resource access range corresponding to the currently running controlled class instruction.
  10. 根据权利要求8或9所述的装置,其特征在于,The device according to claim 8 or 9, characterized in that,
    所述控制模块,还用于在检测到所述受控模块内的指令不均为所述受控类指令之后, 在检测到所述受控模块包括与第一预设指令集匹配的第一指令、且所述第一指令为用于访问第一预设代码资源的指令时,对所述受控模块内的每个所述受控类指令,分配所述目标资源访问范围;The control module is also configured to, after detecting that the instructions in the controlled module are not uniform instructions of the controlled type, detecting that the controlled module includes a first instruction set that matches the first preset instruction set. instruction, and when the first instruction is an instruction for accessing a first preset code resource, allocate the target resource access scope to each controlled class instruction in the controlled module;
    其中,所述非受控类指令包括所述第一预设指令集;Wherein, the uncontrolled instructions include the first preset instruction set;
    所述第一预设代码资源位于第一资源访问范围,且所述第一资源访问范围与所述目标资源访问范围不同。The first preset code resource is located in a first resource access scope, and the first resource access scope is different from the target resource access scope.
  11. 根据权利要求10所述的装置,其特征在于,所述第一预设代码资源包括第一预设函数,所述第一指令为用于访问所述第一预设函数的第一跳转类指令;The device according to claim 10, wherein the first preset code resource includes a first preset function, and the first instruction is a first jump class used to access the first preset function. instruction;
    其中,所述第一预设函数为所述控制模块允许所述受控模块访问的函数。Wherein, the first preset function is a function that the control module allows the controlled module to access.
  12. 根据权利要求8至11中任意一项所述的装置,其特征在于,所述受控模块内的所述受控类指令包括:用于访问代码资源的第一类指令和用于访问数据资源的第二类指令;The device according to any one of claims 8 to 11, characterized in that the controlled class instructions in the controlled module include: a first class instruction for accessing code resources and a first class instruction for accessing data resources. Category II instructions;
    所述控制模块,具体用于:The control module is specifically used for:
    对所述第一类指令分配第二资源访问范围;Allocate a second resource access scope to the first type of instruction;
    对所述第二类指令分配第三资源访问范围;Allocate a third resource access scope to the second type of instruction;
    其中,所述第二资源访问范围和所述第三资源访问范围之间的资源访问地址不同。Wherein, the resource access addresses between the second resource access scope and the third resource access scope are different.
  13. 根据权利要求8至12中任意一项所述的装置,其特征在于,所述非受控类指令包括第二预设指令集,所述控制模块包括代理子模块,所述代理子模块包括与所述第二预设指令集匹配的第二指令;The device according to any one of claims 8 to 12, wherein the uncontrolled instructions include a second preset instruction set, the control module includes an agent sub-module, and the agent sub-module includes The second instruction matched by the second preset instruction set;
    其中,所述第二指令为用于访问第二预设代码资源的指令,所述第二预设代码资源位于第四资源访问范围内,且所述第四资源访问范围与所述目标资源访问范围之间的资源访问地址不同;Wherein, the second instruction is an instruction for accessing a second preset code resource, the second preset code resource is located within a fourth resource access range, and the fourth resource access range is consistent with the target resource access range. Resource access addresses are different between ranges;
    所述控制模块,还用于将所述代理子模块写入至所述第二资源访问范围。The control module is also used to write the agent sub-module to the second resource access scope.
  14. 根据权利要求13所述的装置,其特征在于,所述第二预设代码资源包括第二预设函数,所述第一类指令包括用于访问所述第二指令的第二跳转类指令;The device according to claim 13, wherein the second preset code resource includes a second preset function, and the first type of instructions includes a second jump type instruction for accessing the second instruction. ;
    所述处理模块,具体用于:The processing module is specifically used for:
    对所述第二跳转类指令中的第二访问地址进行处理,并按照处理后的第二访问地址访问所述第二指令;Process the second access address in the second jump instruction, and access the second instruction according to the processed second access address;
    按照所述第二指令中的第三访问地址,访问所述第二预设函数;Access the second preset function according to the third access address in the second instruction;
    其中,所述第二预设函数为所述控制模块允许所述受控模块访问的函数。Wherein, the second preset function is a function that the control module allows the controlled module to access.
  15. 一种计算机可读存储介质,其特征在于,包括计算机程序,当所述计算机程序运行在计算机或处理器上时,使得所述计算机或所述处理器执行如权利要求1至7中任意一项所述的方法。A computer-readable storage medium, characterized by comprising a computer program, which when the computer program is run on a computer or processor, causes the computer or processor to execute any one of claims 1 to 7 the method described.
  16. 一种资源控制装置,其特征在于,包括一个或多个接口电路和一个或多个处理器;所述接口电路用于从存储器接收信号,并向所述处理器发送所述信号,所述信号包括存储器中存储的计算机指令;当所述处理器执行所述计算机指令时,所述处理器用于执行如权利要求1至7中任意一项所述的方法。A resource control device, characterized in that it includes one or more interface circuits and one or more processors; the interface circuit is used to receive a signal from a memory and send the signal to the processor, and the signal Comprising computer instructions stored in a memory; when the processor executes the computer instructions, the processor is configured to perform the method according to any one of claims 1 to 7.
  17. 一种计算机程序产品,其特征在于,所述计算机程序产品包括软件程序,当所述软件程序被计算机或处理器执行时,使得权利要求1至7任一项所述的方法的步骤被执行。A computer program product, characterized in that the computer program product includes a software program, which when executed by a computer or processor causes the steps of the method described in any one of claims 1 to 7 to be executed.
PCT/CN2023/071405 2022-03-17 2023-01-09 Resource control method and apparatus WO2023173915A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202210262411.2 2022-03-17
CN202210262411.2A CN116795525A (en) 2022-03-17 2022-03-17 Resource control method and device

Publications (1)

Publication Number Publication Date
WO2023173915A1 true WO2023173915A1 (en) 2023-09-21

Family

ID=88022171

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2023/071405 WO2023173915A1 (en) 2022-03-17 2023-01-09 Resource control method and apparatus

Country Status (2)

Country Link
CN (1) CN116795525A (en)
WO (1) WO2023173915A1 (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1298512A (en) * 1998-02-26 2001-06-06 太阳微系统公司 Stack-based access control
CN1700136A (en) * 2004-05-20 2005-11-23 英特尔公司 Processor expansion and software authentication
US7162743B1 (en) * 2001-10-04 2007-01-09 Hewlett-Packard Development Company, L.P. System and method of limiting access to protected hardware addresses and processor instructions
US20140041026A1 (en) * 2012-08-01 2014-02-06 SIFTEO, Inc. Hybrid Virtual Machine
CN109840410A (en) * 2017-12-28 2019-06-04 中国科学院计算技术研究所 The method and system of data isolation and protection in a kind of process
CN113886288A (en) * 2021-09-29 2022-01-04 南方科技大学 Resource access control method, system, equipment and storage medium based on ARM architecture

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1298512A (en) * 1998-02-26 2001-06-06 太阳微系统公司 Stack-based access control
US7162743B1 (en) * 2001-10-04 2007-01-09 Hewlett-Packard Development Company, L.P. System and method of limiting access to protected hardware addresses and processor instructions
CN1700136A (en) * 2004-05-20 2005-11-23 英特尔公司 Processor expansion and software authentication
US20140041026A1 (en) * 2012-08-01 2014-02-06 SIFTEO, Inc. Hybrid Virtual Machine
CN109840410A (en) * 2017-12-28 2019-06-04 中国科学院计算技术研究所 The method and system of data isolation and protection in a kind of process
CN113886288A (en) * 2021-09-29 2022-01-04 南方科技大学 Resource access control method, system, equipment and storage medium based on ARM architecture

Also Published As

Publication number Publication date
CN116795525A (en) 2023-09-22

Similar Documents

Publication Publication Date Title
US10114958B2 (en) Protected regions
US9710654B2 (en) Method for validating an untrusted native code module
US10198578B2 (en) Secure privilege level execution and access protection
US9112867B2 (en) Method for enforcing resource access control in computer systems
CN109359487B (en) Extensible security shadow storage and tag management method based on hardware isolation
US7380049B2 (en) Memory protection within a virtual partition
US7673109B2 (en) Restricting type access to high-trust components
JP4759059B2 (en) Page coloring that maps memory pages to programs
US11171983B2 (en) Techniques to provide function-level isolation with capability-based security
US20130283017A1 (en) Hard object: constraining control flow and providing lightweight kernel crossings
EP2963560A1 (en) Method for safely executing an untrusted native code module on a computing device
US20060047959A1 (en) System and method for secure computing
US20110126265A1 (en) Security for codes running in non-trusted domains in a processor core
KR20090010872A (en) Method and apparatus for managing access privileges in a cldc osgi environment
US7647629B2 (en) Hosted code runtime protection
CN112446032B (en) Trusted execution environment construction method, system and storage medium
US7512768B2 (en) Dynamically sharing a stack between different code segments
WO2023173915A1 (en) Resource control method and apparatus
US20210165588A1 (en) Method and system for controlling a switch in the execution mode of a processor
JP4638505B2 (en) Safe program interpretation method in electronic devices
US20210157612A1 (en) Encaching and sharing transformed libraries
Zeng et al. Refinement-based Modeling and Formal Verification for Multiple Secure Partitions of TrustZone.
JP2008521111A5 (en)
JP2009104555A (en) Method and apparatus for preventing alteration of software agent operating in vt environment

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 23769426

Country of ref document: EP

Kind code of ref document: A1