Operating system-oriented native code compiling method, system and computer readable storage medium

By using a unified compilation pipeline to process source code with multiple security levels and multiple languages, sharing optimization processes, and imposing differentiated security constraints during the build phase, the problem of the separation between the kernel and application compilation infrastructure in the operating system is solved, improving system operating efficiency and security, and achieving a balance between security and efficiency.

CN123179384APending Publication Date: 2026-11-27谢成鸿
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611354137.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-02
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

The lack of a unified compilation pipeline in existing operating systems makes it impossible to automatically apply differentiated security constraints based on security levels during the code generation phase. This results in the kernel and applications being unable to share compilation optimization infrastructure, hardware isolation leading to performance degradation, poor performance of hot system call paths, and the inability to achieve cross-security level function inlining and linking optimization.

Method used

This paper presents a native code compilation method for operating systems. It processes source code with multiple security levels and multiple languages ​​through a unified compilation pipeline, shares optimization processes, applies differentiated security constraints only during the code generation stage, realizes the sharing of kernel and application compilation infrastructure, reduces the performance loss caused by hardware isolation, and improves system operating efficiency and security.

Benefits of technology

It enables the sharing of kernel and application compilation infrastructure, reduces optimization resource redundancy, reduces hardware isolation performance loss, improves system operating efficiency and security, ensures the running security of code at different levels, and achieves a balance between security and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123179384A_ABST
    Figure CN123179384A_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide an operating system-oriented native code compiling method and system and a computer readable storage medium. The method comprises: receiving a source code module with a security level, wherein different security levels correspond to different security constraints, and the security constraints of a low security level are higher than those of a high security level; compiling the source code module into a common intermediate representation through a shared language front end, the common intermediate representation containing a security level tag corresponding to the source code module, and the shared language front end accepting at least two different programming languages; performing an optimization operation on the common intermediate representation, the optimization operation being irrelevant to the security level of the source code module; and generating machine code according to the optimized common intermediate representation, the machine code including the security constraints corresponding to the security level. In this way, the running safety of code with different security levels can be ensured, and the balance between safety and efficiency can be achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to software security technologies for computer operating systems, and more particularly to native code compilation methods, systems, and computer-readable storage media for operating systems. Background Technology

[0002] Operating systems need to run programs with different security levels, such as the kernel, system services, and user applications, simultaneously. To ensure system security and operational efficiency, differentiated security constraints and unified compilation optimizations are required for code at different security levels. In existing technologies, operating system kernels and applications typically use independent compilation toolchains and pipelines. For example, the Linux system kernel and applications both use the GNU Compiler Collection (GCC), but security levels are not recognized during the code generation stage. The HarmonyOS operating system uses different development languages ​​at different levels, without forming a unified compilation pipeline. Although LLVM (Low Level Virtual Machine) supports multi-language front-ends and a unified back-end, it does not distinguish between security levels during code generation. Summary of the Invention

[0003] To address the problems existing in the prior art, this application provides a native code compilation method, system, and computer-readable storage medium for operating systems.

[0004] According to a first aspect of this application, a method for compiling native code for an operating system is provided. The method includes: Receive source code modules marked with security levels, where different security levels correspond to different security constraints, with lower security levels having higher security constraints than higher security levels; The source code module is compiled into a common intermediate representation through a shared language front-end. The common intermediate representation contains a security level label corresponding to the source code module. The shared language front-end accepts at least two different programming languages. An optimization operation is performed on the public intermediate representation, and the optimization operation is independent of the security level of the source code module; Machine code is generated based on the optimized public intermediate representation, and the machine code includes the security constraints corresponding to the security level.

[0005] Preferably, the optimization operations include at least: sparse conditional constant propagation, global value numbering, dead code elimination, aggregate scalar replacement, function inlining, loop optimization, de-virtualization, and boundary check elimination.

[0006] Preferably, the optimization operation on the common intermediate representation specifically includes: When it is identified that the source code module is a cross-level call and the cross-level call is a low-security-level code calling a high-security-level code, it is determined whether the called function is a function that can be safely inlined based on the intermediate representation; If so, the function code of the called function will be copied into the calling application code, and the inlined called function will automatically comply with the calling party's security constraints.

[0007] Preferably, the optimization operation on the common intermediate representation further includes: If not, a jump function is generated; the jump function performs permission verification on the caller, switches to the security domain of the callee, calls the called function, and after execution, switches back to the security domain of the caller and returns the result to the caller.

[0008] Preferably, the machine code includes security constraints corresponding to at least two security levels.

[0009] Preferably, the security levels, from highest to lowest, include: kernel hardware level, kernel service level, system service level, and user application level.

[0010] Preferably, the security constraints include: hardware access permissions, memory access constraints, and isolation measures.

[0011] Preferably, the shared language front-end is capable of parsing source code modules corresponding to the C, C++, TypeScript, and JavaScript programming languages.

[0012] On the other hand, the computer-readable storage medium provided in the embodiments of this application stores a computer program, which, when executed by a processor, implements the steps of the aforementioned method.

[0013] Furthermore, this application provides a native code compilation system for an operating system, comprising: a source code receiving module, a language front-end compilation module, an intermediate representation optimization module, and a machine code generation module, wherein: The source code receiving module is used to receive source code modules marked with security levels, wherein different security levels correspond to different security constraints, and the security constraints of lower security levels are higher than those of higher security levels. The language front-end compilation module is used to compile the source code module into a common intermediate representation through the shared language front-end. The common intermediate representation contains a security level label corresponding to the source code module. The shared language front-end accepts at least two different programming languages. The intermediate representation optimization module is used to perform optimization operations on the common intermediate representation, and the optimization operations are independent of the security level of the source code module. The machine code generation module is used to generate machine code based on the optimized common intermediate representation, wherein the machine code includes security constraints corresponding to the security level.

[0014] Compared with existing technologies, the native code compilation method for operating systems provided in this embodiment can compile code of different security levels and different programming languages ​​in a unified manner, solving the problem of low running efficiency when the operating system needs to run programs of different security levels such as kernel, system services and user applications at the same time.

[0015] It should be understood that the description in the Summary Section is not intended to limit the key or essential features of the embodiments of this application, nor is it intended to restrict the scope of this application. Other features of this application will become readily apparent from the following description. Attached Figure Description

[0016] The above and other features, advantages, and aspects of the embodiments of this application will become more apparent from the accompanying drawings and the following detailed description. The drawings are provided for a better understanding of the invention and are not intended to limit the scope of the application. In the drawings, the same or similar reference numerals denote the same or similar elements, wherein: Figure 1 A flowchart illustrating a native code compilation method for an operating system according to an embodiment of this application is shown; Figure 2 A flowchart illustrating the optimization operation in an operating system-oriented native code compilation method according to an embodiment of this application is shown; Figure 3 A block diagram of an operating system-oriented native code compilation system according to an embodiment of this application is shown. Detailed Implementation

[0017] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0018] In existing operating system compilation schemes, the kernel and user-space applications use independent toolchains. For example, in seL4 (a formally verified microkernel operating system), the kernel and applications use different compilation pipelines. Compilers such as LLVM and GCC are unaware of security levels during code generation. GraalVM (a general-purpose virtual machine) supports multi-language runtimes but uses Just-In-Time (JIT) compilation technology, which cannot support Ahead-Of-Time (AOT) compilation of the operating system kernel. This leads to the following technical problems: First, the kernel and applications cannot share compilation optimization infrastructure, resulting in redundant configuration of optimization resources; second, the security boundary between the kernel and applications depends on hardware privilege level switching, resulting in high context switching overhead; third, cross-security level function inlining and linking optimization cannot be implemented, leading to poor performance of system call hot paths; fourth, driver and other code are not sandboxed during compilation, making them prone to system crashes when running in kernel space.

[0019] The core technical problem with the above solution is the lack of a unified compilation pipeline, which makes it impossible to automatically apply differentiated security constraints based on security levels during the code generation phase.

[0020] To address the aforementioned issues, this application provides a native code compilation method for operating systems. It utilizes a unified compilation pipeline to handle source code across multiple security levels and languages, sharing optimization processes across all code levels. Differentiated security constraints are applied only during the code generation phase, enabling shared kernel and application compilation infrastructure. This reduces performance overhead caused by hardware isolation and improves system efficiency and security. (Reference) Figure 1 The figure shows a summary flowchart of the native code compilation method for the operating system according to an embodiment of this application.

[0021] In this embodiment, the operating system first receives multiple source code modules labeled with security levels. Different security levels correspond to different security constraints, with lower security levels having stricter constraints than higher security levels. That is, the higher the security level, the higher the access permissions the code has, and the fewer the corresponding security constraints; conversely, the lower the security level, the lower the permissions, and the more security constraints are imposed. For example, the security level can be determined based on the contents of configuration files or manifest files in the source code modules. Security constraints include hardware access permissions, memory access constraints, and isolation measures. Based on the different security constraints, the security levels are divided into four levels, from highest to lowest: kernel hardware level (Level 3), kernel service level (Level 2), system service level (Level 1), and user application level (Level 0).

[0022] The four security levels are explained below: I. Kernel Hardware Level (Level 3) The kernel hardware level is the highest-level, high-privilege level of the operating system. It is responsible for directly interfacing with hardware such as the CPU, memory, and peripherals, and is the underlying encapsulation of the entire system's hardware capabilities.

[0023] Access restrictions: No software fault isolation (SFI) sandbox constraints are imposed, allowing direct access to hardware registers and physical memory; privileged assembly instructions can be executed; not subject to software restrictions such as MMIO (Memory-Mapped Input-Output) whitelists or address masks.

[0024] Isolation method: Relying on CPU hardware privilege level isolation (x86 architecture Ring 0, ARM architecture EL1), relying on CPU hardware circuitry to prevent low-level code from intruding into the execution environment.

[0025] Executable operations at the kernel hardware level include: initializing the CPU interrupt and exception handling vector table; directly reading and writing hardware registers and physical memory addresses; completing physical memory paging, and creating and modifying page tables; low-level hardware device initialization, PCI device scanning, and clock hardware configuration; handling hardware interrupts and responding to hardware device reported signals. Typical examples include: physical memory initialization code, low-level interrupt handling code, and assembly code for reading and writing hardware registers.

[0026] II. Kernel Service Level (Level 2) Kernel service level refers to core system services that run in the kernel address space. They do not directly manipulate the raw hardware, but provide basic kernel logic capabilities to the upper layers, and lie between the hardware abstraction layer and ordinary system services.

[0027] Access restrictions: impose restricted software fault isolation constraints; configure MMIO (Memory Mapped Input / Output) whitelists to allow access only to hardware memory addresses registered in the whitelist; prohibit unrestricted raw pointers from traversing the entire kernel address space; hardware privileged assembly instructions cannot be executed directly, and hardware operations must call kernel hardware-level interfaces.

[0028] Isolation method: It shares the same kernel virtual address space with the kernel hardware level, and relies on the compiler to insert memory access verification and MMIO access verification during the compilation stage to achieve software isolation, without relying on CPU hardware privilege level switching.

[0029] Executable operations at the kernel service level include: thread and process scheduling, CPU time slice allocation; inter-process communication (IPC) message sending and receiving, message queue management; virtual memory management, handling page faults, and completing virtual-physical memory mapping; creation, destruction, and state maintenance of internal kernel objects; and receiving hardware events reported by the lower-level kernel hardware abstraction layer and providing kernel service interfaces to the upper layers. Typical examples include: the scheduler module, the IPC message management module, and the virtual memory manager.

[0030] III. System Service Level (Level 1) The system service level is the service layer that implements the business functions of the operating system. It includes business logic such as file, network, and peripheral driver management, and provides system capabilities to user applications.

[0031] Access restrictions: Enables full SFI software fault isolation sandbox; memory access uses base address + mask addressing; can only access memory ranges allocated within this security domain; can call kernel service level interfaces, but cannot directly access hardware; can be configured with independent resource quotas.

[0032] Isolation methods: Two options: Independent address space, isolated by hardware through the operating system's page tables; or software isolation within the same kernel address space, achieved through compiler SFI mask operations.

[0033] The executable operations corresponding to the system service level include: Virtual File System (VFS) business logic, such as file opening, reading, writing, and closing; network protocol stack processing, including data packet sending and receiving, and TCP / UDP connection management; peripheral driver upper-layer business logic, such as sound card, USB device, and camera business processing; exposing standardized system call interfaces to upper-layer user applications; and receiving business requests from user applications and requesting underlying resources from the kernel service level. Typical examples include: file system service, network protocol stack service, and peripheral driver management service.

[0034] IV. User Application Level (Level 0) User application level is the lowest permission security level for ordinary terminal applications. It is aimed at end users, has the highest risk, and is subject to the strictest restrictions.

[0035] Access restrictions: Full sandbox restrictions, including SFI software fault isolation, API (Application Programming Interface) filtering, and capability control; memory access is subject to base address + mask addressing; only API interfaces authorized by the manifest configuration list can be called; hardware, file, and network access must be authorized by capability.

[0036] Isolation method: Independent virtual address space; each user application is allocated an independent GC (garbage collection) heap, and the garbage collection heaps of different applications are isolated from each other.

[0037] Executable operations at the user application level include: general business calculations and data processing; calling authorized system service interfaces to complete file reading and writing and network access; user interface rendering and user input event handling; direct access to physical memory and hardware registers is prohibited; and bypassing API filters to call privileged interfaces is not allowed. Typical examples include: desktop editors, browsers, mobile apps, and ordinary command-line applications.

[0038] Secondly, the source code module is compiled into a common intermediate representation through a shared language frontend. The common intermediate representation contains the security level label corresponding to the source code module. The shared language frontend accepts at least two different programming languages.

[0039] In this step, since the source code for different security levels uses different programming languages—for example, kernel hardware level (Level 3) source code is generally written in C / C++ inline assembly, kernel service level (Level 2) source code is generally written in C++, system service level (Level 1) source code is generally written in C++ or TypeScript, and user application level (Level 0) source code is generally written in TypeScript—the shared language frontend provided in this embodiment improves upon existing technologies by being able to handle multiple different languages. Specifically, the shared language frontend includes multiple language compilation frontends: Clang is an open-source C / C++ compilation frontend from the LLVM project; ts is a compilation frontend designed for TypeScript and JavaScript. Each type of source code is fed into its corresponding compilation frontend, parsed, and outputs a high-level intermediate representation (HIR) carrying security level metadata, which then enters a unified intermediate representation downgrading, optimization, and differentiated code generation process.

[0040] That is, in this embodiment, the shared language front-end can at least parse the source code modules corresponding to the C, C++, TypeScript or JavaScript programming languages.

[0041] It is important to note that the intermediate representation generated in this step must include the corresponding security level label, such as Level 3, Level 2, etc.

[0042] Furthermore, an optimization operation is performed on the public intermediate representation, which is independent of the security level of the source code module.

[0043] Once all programming languages ​​generate a common intermediate representation, optimization operations can be performed on it. These optimizations are independent of security levels. It's important to note that the output after processing by the shared language frontend is a high-level intermediate representation (HIR) carrying security level labels. HIRs retain a significant amount of high-level language syntactic sugar, resulting in large language differences. Therefore, a language-independent, standardized, and unified mid-level intermediate representation is needed as input to the unified optimization layer. This necessitates a downgrade process from the high-level intermediate representation to the mid-level intermediate representation (MidIR). This downgrade process involves rewriting and transforming the intermediate representation while preserving the program's semantics. The downgrade process includes desugaring various high-level language syntaxes, decomposing compound expressions into atomic instruction sequences, standardizing and unifying data types, flattening the control flow, and completely migrating the security level context labels carried by the high-level intermediate representation to the corresponding function and instruction nodes of the mid-level intermediate representation. The downgrade process does not perform code optimization operations; after the downgrade is completed, a language-independent intermediate representation is obtained, and then a shared optimization set is performed on the intermediate representation.

[0044] The optimization processes in this embodiment include many types, such as Sparse Conditional Constant Propagation (SCCP), Global Value Numbering (GVN), Dead Code Elimination (DCE), Scalar Replacement of Aggregates (SROA), function inlining, loop optimization, de-virtualization, and Bounds Check Elimination (BCE). Moreover, they do not distinguish between security levels, and the kernel and applications can share the optimization infrastructure, reducing optimization resource redundancy.

[0045] It's important to note that optimization typically begins with pre-optimizations such as constant propagation and de-virtualization to determine the actual call target. Next, the validity of cross-security-level inlining is checked and the inlining is executed. Finally, the inlined code undergoes further optimizations such as dead code elimination, global value numbering, and boundary check elimination (e.g., ...). Figure 2 (As shown). Different security levels share a common optimization process, but when cross-level inlining, access permissions, or security domain changes are involved, the security level label must be checked.

[0046] Next, machine code is generated based on the optimized public intermediate representation, and the machine code includes the security constraints corresponding to the security level.

[0047] In this step, the code generator reads the security level label carried in the intermediate representation and generates machine code with corresponding constraints for different security levels: kernel hardware code has no sandbox constraints and supports direct hardware access; kernel service and system service code adopts Software Fault Isolation (SFI) constraints; user application code adopts full sandbox constraints, including address mask, application programming interface (API) filtering, and access control.

[0048] Machine code is a set of binary instructions that the CPU hardware can directly recognize and execute. It consists entirely of 0s and 1s, requiring no translation, virtual machine, or interpreter; the CPU can run it directly. Machine code is a sequence of binary instructions that the central processing unit (CPU) can directly recognize and execute. Source code written in high-level programming languages ​​cannot be directly executed by the CPU and needs to be compiled into machine code. In this application, the optimized intermediate representation (MidIR) is fed into a code generator. The code generator reads the security level context label and generates machine code with differentiated security verification instructions for different security levels. The machine code is then packaged with file metadata to obtain an executable binary file.

[0049] The foregoing summary has described the main content and essence of the embodiments of this application. This embodiment solves the problem of the separation between the kernel and application compilation pipelines by processing source code of all security levels through a unified compilation pipeline. All code shares the optimization infrastructure, reducing optimization resource redundancy. Constraints are applied differently according to security levels during the code generation stage, without relying on hardware privilege levels to achieve full system security isolation, reducing context switching overhead, while ensuring the runtime security of code at different levels, achieving a balance between security and efficiency.

[0050] In some embodiments, when the optimization operation is function inlining, performing the optimization operation on the common intermediate representation may specifically include: When it is identified that the source code module is a cross-level call and the cross-level call is a low-security-level code calling a high-security-level code, it is determined whether the called function is a function that can be safely inlined based on the intermediate representation; If so, the function code of the called function will be copied into the calling application code, and the inlined called function will automatically comply with the calling party's security constraints.

[0051] A function that can be safely inlined is code that, after being inlined, retains its original functionality within the caller's privileged scope. Therefore, a function that can be safely inlined must meet certain conditions: the called function must not include "privileged functions." "Privileged functions" are functions that involve operations such as kernel-protected memory access, kernel state modification, hardware access, privileged instructions, or privileged API calls. For example, when Level 0 calls Level 2, non-privileged code such as parameter validation, data translation, and address calculation can be inlined into Level 0 and inherit its security constraints; privileged code such as manipulating the kernel IPC queue, modifying scheduling states, and waking up threads cannot be inlined and must still be executed by Level 2 through a cross-security domain call gateway.

[0052] Therefore, a function that can be safely inlined must satisfy the following condition: A. Within the called higher-security-level function, there are no hardware privilege operations. Hardware privilege operations include directly reading and writing hardware registers, executing privileged assembly instructions, etc.; these operations cannot be overridden by software fault isolation constraints and cannot be executed in a lower-security-level context. B. All memory access instructions within the called function support software fault isolation rewriting. All read and write memory operations within the function can be rewritten as "base address + (pointer & mask)" in the caller's security domain. If the function contains raw hardware pointers not controlled by the mask or direct MMIO (memory-mapped input / output) access, then the condition is not met.

[0053] C. The called function will not modify external global state that is not under sandbox control. External global state that is not under sandbox control includes kernel global variables and hardware global state. Modifying such state after inlining to a lower-level context would cause uncontrollable system side effects, so the condition is not met.

[0054] D. The called function does not invoke privileged APIs (Application Programming Interfaces). Privileged APIs are interfaces that require higher security privileges to execute. These interfaces cannot be constrained by low-level sandboxes and pose a risk of privilege escaping.

[0055] In this embodiment, the compiler counts the frequency of function calls and marks frequently called, high-security-level functions as hot path functions, such as kernel service inter-process communication (IPC) functions that are frequently called by user applications.

[0056] This allows high-security-level hot path functions to be inlined into low-security-level code. In other words, the compiler directly embeds the instructions of the high-security-level function into the call location of the low-security-level code, replacing the original function call instructions.

[0057] At this point, the memory access instructions within the inline function body automatically adapt to the caller's security constraints. Inline instructions inherit the context labels of the lower security level, and memory access instructions are automatically rewritten into a base address plus mask addressing format, following the caller's sandbox constraints. For example, after a kernel service function is inlined into a user application, it can only access the user application's legitimate memory regions.

[0058] In this embodiment, cross-level inlining achieves zero-overhead abstraction of system call hot paths, eliminating the performance loss of function calls and security domain switching. At the same time, inline code automatically inherits the constraints of lower security levels, which will not cause privilege escalation, thus improving running efficiency while ensuring system security.

[0059] Example 1: A Level 0 application calls a Level 2 IPC send function. Before inline: Level 0 Applications: call ipc_send(cap, msg) Before inlining, cross-level calls needed to be implemented using the trampoline function.

[0060] Before inlining, the original memory access instruction of the callee (originally Level 2) was: `store [ptr], value`. After this function body is inlined into the Level 0 application, the compiler automatically rewrites the above memory access instruction into the form that conforms to the Level 0 sandbox constraints (base address + mask addressing): `store [base_L0 + (ptr & MASK_L0)], value`. Here, `base_L0` represents the sandbox base address corresponding to the Level 0 security level, `MASK_L0` represents the address mask corresponding to the Level 0 security level, `ptr` represents the original access address, and `value` represents the data to be written. Through this rewriting, the memory access after inlining is restricted to the sandbox address space corresponding to the Level 0 security level, thus preserving the benefits of inlining optimization while maintaining the original security constraints of the caller.

[0061] The automatic rewriting of memory access instructions described above is achieved internally by the compiler through security level metadata carried by the intermediate representation (MID). MidIR not only records the original security level in the function definition, but each memory access instruction also carries an independent security level context label.

[0062] An example function definition node is as follows: FunctionDef { name: "ipc_send", level: SecurityLevel::KernelService,} This node is used to define the ipc_send function. The name field records the function name, and the level field takes the value "SecurityLevel::KernelService" to indicate that the function originally belongs to the kernel service level, i.e., Level 2. The node also contains other attributes of the function, which are omitted here.

[0063] An example memory load instruction node is as follows: Inst::Load { addr: v3, level_context: SecurityLevel::UserApp, } In this context, "v3" in the `addr` field represents the memory address to be accessed; the value "SecurityLevel::UserApp" in the `level_context` field indicates that the execution context of this memory access instruction is at the user application level, i.e., Level 0. The code generator reads this security level context, selects the corresponding sandbox constraint strategy accordingly, and generates machine code adapted to this security level.

[0064] When a kernel service level 2 function is inlined into a user application level 0 context, the compiler modifies the `level_context` field of all instructions in the inline copy to the user application level corresponding to the caller; the original security level recorded in the function definition node of the called source function remains unchanged. During the code generation phase, machine code with corresponding sandbox constraints is generated based on the security level context carried by each instruction.

[0065] In addition, machine code includes security constraints corresponding to at least two security levels. Security level labels enable fine-grained security control, support mixing code of different security levels within a single compilation unit, and ensure more precise enforcement of security constraints, adapting to complex cross-level call scenarios.

[0066] In addition to the examples above, it is also possible to implement Level 1 calls to Level 2, such as when a network card driver requests a physical memory buffer; and Level 0 calls to Level 1, such as when an app reads a local file. These scenarios do not contain privileged functions, therefore, inlining can be implemented.

[0067] Continuing with Example 1, when it is determined that the called function is not a function that can be safely inlined (i.e., includes privileged functions), a jump function is generated. This jump function performs permission checks on the caller, switches to the callee's security domain, calls the called function, and after execution, switches back to the caller's security domain, returning the result to the caller. A security domain corresponds to a complete runtime context, which includes permission constraints and isolation measures under that security level.

[0068] For non-inlined cross-level calls, the compiler generates corresponding jump function. These jump functions are divided into ordinary cross-level jump functions and system call stubs, which handle calls from the user application level to the kernel service level and from the system service level to the kernel hardware level, respectively.

[0069] The jump function executes permission credentials verification and security domain switching, that is, first verifying the caller's permission credentials to confirm the legitimacy of the call, and then switching the base address, mask register or hardware privilege level of the security domain.

[0070] The jumper function calls the target function with a higher security level. After execution, it restores the original security domain. That is, after the target function finishes execution, the jumper function restores the runtime environment to the security level of the caller and returns the execution result.

[0071] In this embodiment, the compiler automatically generates a jump function, eliminating the need for manual writing of secure switching code and reducing human error. The jump function implements standardized secure switching, enforces call rules during compilation, further enhances system security, and prevents unauthorized access to kernel resources.

[0072] Example 2: A scenario where a user application (Level 0) calls a kernel service (Level 2) and the cross-level inlining condition is not met. trampoline_ipc_send: validate_capability(cap) switch_safety_domain(L0 → L2) call ipc_send_impl switch_safety_domain(L2 → L0) ret Example 2 illustrates how, when a user application (Level 0) calls a kernel service (Level 2) IPC message sending function and the cross-level inlining condition is not met, the compiler automatically generates the jumper function `trampoline_ipc_send` during the compilation phase. The jumper function first executes `validate_capability(cap)` to verify the authorization credentials passed by the caller; if the authorization verification fails, it directly returns an error result. If the verification passes, it executes `switch_safety_domain(L0→L2)`, which switches the security domain from the user application level to the kernel service level by adjusting the base address and mask registers for software fault isolation. This process does not change the CPU hardware privilege level. Then, it calls `ipc_send_impl` to execute the underlying implementation logic for IPC message sending. After execution, it executes `switch_safety_domain(L2→L0)` to restore the security domain to the user application level, and finally executes the `ret` instruction to return to the caller.

[0073] The compiler enforces symbol isolation during the compilation phase: Level 0 modules cannot import or reference the Level 2 internal function symbol `ipc_send_impl`, and user application-level code can only call the compiler-exposed jump functions, not directly call the kernel service-level low-level implementation functions. This symbol masking occurs during the compilation phase and is independent of runtime security domain switching operations.

[0074] Example 3: A scenario where a system service level (Level 1) calls a kernel hardware level (Level 3) service. stub_page_map: mov rax, SYSCALL_PAGE_MAP mov rdi, cap mov rsi, vaddr mov rdx, paddr syscall ret Example 3 illustrates a Level 1 system service level call to a Level 3 kernel hardware level. When performing page mapping operations, the compiler automatically generates a system call stub (stub_page_map). Within the stub, the `mov` assembly instruction stores the system call number `SYSCALL_PAGE_MAP` into the `rax` register, and sequentially loads the privilege credentials `cap`, virtual address `vaddr`, and physical address `paddr` into the `rdi`, `rsi`, and `rdx` parameter registers. Subsequently, the `syscall` hardware privilege level switch instruction is executed, triggering a CPU hardware privilege level transition from Level 1 to the Level 3 kernel hardware abstraction layer to execute the underlying page mapping logic. After kernel processing, the hardware returns to Level 1, executing the `ret` instruction to return to the Level 1 calling code. This scenario involves hardware privilege operations and does not meet the security requirements for cross-level inlining; therefore, the call can only be completed through the system call stub, and cross-level function inlining is not possible.

[0075] In addition to providing a native code compilation method for an operating system, this application also provides a native code compilation system for an operating system. The system includes: a source code receiving module, a language front-end compilation module, an intermediate representation optimization module, and a machine code generation module, wherein: The source code receiving module is used to receive source code modules marked with security levels, wherein different security levels correspond to different security constraints, and the security constraints of lower security levels are higher than those of higher security levels. The language front-end compilation module is used to compile the source code module into a common intermediate representation through the shared language front-end. The common intermediate representation contains a security level label corresponding to the source code module. The shared language front-end accepts at least two different programming languages. The intermediate representation optimization module is used to perform optimization operations on the common intermediate representation, and the optimization operations are independent of the security level of the source code module. The machine code generation module is used to generate machine code based on the optimized common intermediate representation, wherein the machine code includes security constraints corresponding to the security level.

[0076] Furthermore, the technical solution of this application can be formed on a computer-readable storage medium to form a computer program.

[0077] The embodiments of this application can achieve all the inventive objectives using the aforementioned software; therefore, the software portion has been primarily described. Those skilled in the art should understand that the embodiments of this application can be provided as an apparatus, system, or related computer program product. Therefore, this application can be implemented entirely in hardware, entirely in software, or in a combination of software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0078] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0079] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0080] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0081] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0082] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.

[0083] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.

[0084] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0085] The above description is merely an embodiment of this application and is not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.

Claims

1. A method for compiling native code for an operating system, characterized in that, The method includes: Receive source code modules marked with security levels, where different security levels correspond to different security constraints, with lower security levels having higher security constraints than higher security levels; The source code module is compiled into a common intermediate representation through a shared language front-end. The common intermediate representation contains a security level label corresponding to the source code module. The shared language front-end accepts at least two different programming languages. An optimization operation is performed on the public intermediate representation, and the optimization operation is independent of the security level of the source code module; Machine code is generated based on the optimized public intermediate representation, and the machine code includes the security constraints corresponding to the security level.

2. The method according to claim 1, characterized in that, The optimization operations include at least: sparse conditional constant propagation, global value numbering, dead code elimination, aggregate scalar replacement, function inlining, loop optimization, de-virtualization, and boundary check elimination.

3. The method according to claim 1, characterized in that, The optimization operation performed on the common intermediate representation specifically includes: When it is identified that the source code module is a cross-level call and the cross-level call is a low-security-level code calling a high-security-level code, the intermediate representation is used to determine whether the called function is a function that can be safely inlined. The function that can be safely inlined refers to code that can maintain its original function within the caller's permission scope after inlining. If so, the function code of the called function will be copied into the calling application code, and the inlined called function will automatically comply with the calling party's security constraints.

4. The method according to claim 3, characterized in that, The optimization operation on the public intermediate representation further includes: If not, a jump function is generated. The jump function performs permission verification on the caller, switches to the callee's security domain, calls the called function, and after execution, switches back to the caller's security domain and returns the result to the caller.

5. The method according to claim 3, characterized in that, The machine code includes security constraints corresponding to at least two security levels.

6. The method according to claim 1, characterized in that, The security levels, from highest to lowest, include: kernel hardware level, kernel service level, system service level, and user application level.

7. The method according to claim 1, characterized in that, The security constraints include: hardware access permissions, memory access constraints, and isolation measures.

8. The method according to claim 1, characterized in that, The shared language front-end is capable of parsing source code modules corresponding to at least the C, C++, TypeScript, and JavaScript programming languages.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method as described in any one of claims 1 to 8.

10. A native code compilation system for an operating system, characterized in that, The system includes: a source code receiving module, a language front-end compilation module, an intermediate representation optimization module, and a machine code generation module, wherein: The source code receiving module is used to receive source code modules marked with security levels, wherein different security levels correspond to different security constraints, and the security constraints of lower security levels are higher than those of higher security levels. The language front-end compilation module is used to compile the source code module into a common intermediate representation through the shared language front-end. The common intermediate representation contains a security level label corresponding to the source code module. The shared language front-end accepts at least two different programming languages. The intermediate representation optimization module is used to perform optimization operations on the common intermediate representation, and the optimization operations are independent of the security level of the source code module. The machine code generation module is used to generate machine code based on the optimized common intermediate representation, wherein the machine code includes security constraints corresponding to the security level.