Method, device and computer readable storage medium for generating compilation optimization combined with sandbox security constraints
By generating and propagating security assertions during the compilation optimization process, and generating sandbox constraint instructions hierarchically based on security assertions, the redundant checks and performance losses caused by the independence of compilation optimization and sandbox constraints in existing technologies are solved. This achieves positive coupling between compilation optimization and sandbox security protection, reduces performance overhead, and ensures security.
Patent Information
- Application Number
- CN202611354094.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-02
- Publication Date
- 2026-11-24
AI Technical Summary
In existing native code sandbox protection technologies, compilation optimization and sandbox constraint instruction insertion are independent, resulting in redundant checks and fixed performance losses, making it impossible to achieve positive coupling between compilation optimization and sandbox security protection.
By generating and propagating security assertions during the compilation optimization process, and generating sandbox constraint instructions based on the hierarchical generation of security assertions, positive coupling between compilation optimization and sandbox security constraints is achieved. The higher the optimization strength, the higher the security assertion coverage and the lower the redundancy of the sandbox constraint instructions.
It significantly reduces the performance overhead of native code sandbox protection while ensuring the effectiveness of security constraints on memory access, and solves the problems of redundant checks and fixed performance loss.
Smart Images

Figure CN123157218A_ABST
Abstract
Description
Technical Field
[0001] This application relates to compiler optimization and system security protection technologies, and more particularly to a method, apparatus, and computer-readable storage medium for jointly generating compiler optimization and sandbox security constraints for native code. Background Technology
[0002] In the field of native code security, to avoid security risks such as memory access exceeding limits, existing technologies often employ sandboxing techniques (such as Google NaCl) to uniformly insert sandbox constraint instructions into native code after compilation optimization, thereby limiting the scope of memory access and ensuring code execution security. Taking Google NaCl as an example, its core idea is to insert sandbox constraint instructions based on SFI (Software Fault Isolation) into native code. All memory access operations are masked or bounds-checked, thus preventing out-of-bounds access. However, the above technical solutions have significant drawbacks: the insertion of sandbox constraint instructions is completely independent of the compilation optimization process and is unaware of the analysis results of compilation optimization. Even if a certain memory access has been proven safe through compilation optimization (such as boundary check elimination of BCE), redundant sandbox constraint instructions will still be inserted, resulting in a fixed performance loss (approximately 5-15%) during code execution. Furthermore, increasing the optimization strength cannot reduce the overhead of sandbox security protection, failing to achieve positive synergy between compilation optimization and sandbox security protection. Summary of the Invention
[0003] This application provides a method, apparatus, and computer-readable storage medium for jointly generating compiler optimization and sandbox security constraints, which addresses the problem in existing native code sandbox protection technologies where compiler optimization and sandbox constraint instruction insertion are independent, leading to redundant checks, fixed performance losses, and a lack of positive coupling.
[0004] On the one hand, embodiments of this application provide a method for jointly generating compilation optimization and sandbox security constraints, the method comprising: Receive source code and compile the source code into an intermediate representation; One or more optimization passes are performed on the intermediate representation, each optimization pass producing an optimized intermediate representation transformation result and a security assertion characterizing the provable security attributes of memory access operations; when multiple optimization passes are performed, the security assertions are propagated and accumulated between the multiple optimization passes; Select a code generation strategy with corresponding overhead based on the security assertion for each memory access operation; Based on the optimized intermediate representation transformation result and the selected code generation strategy, native machine code with hierarchical sandbox security constraints is generated.
[0005] Preferably, the code generation strategy includes: If the memory access operation has a security assertion with full security proof, then a direct memory access instruction with zero overhead is generated. If the memory access operation has a security assertion with partial security proof, then memory access code containing a single address constraint instruction is generated; If there is no security assertion for the memory access operation with a security proof, then memory access code containing a complete sequence of boundary checks is generated.
[0006] Preferably, the security assertion is a typed predicate appended to the intermediate representation value. The typed predicate is a predefined Boolean condition used to determine the security attributes of memory access, and includes at least one or more of the following: Address access range assertions that characterize the range of address accesses; Constant address assertions representing constant addresses, generated by constant propagation; Eliminate the cyclic access constraint assertion of the output by boundary checks; Stack-local object assertions representing stack-local objects, generated from escape analysis; Address alias assertions that represent address aliases generated by global value numbers.
[0007] Preferably, the security assertion further includes a user assertion type. When there are explicit annotations in the source code that characterize memory access security attributes, the corresponding optimized pass generates a security assertion corresponding to the user assertion type.
[0008] Preferably, the method further includes: During the propagation of multiple optimized security assertions, multiple security assertion types for the same memory access operation are combined. When there are multiple security assertions corresponding to a memory access, the security assertion of the memory access is marked as a strong proof assertion; for memory accesses with strong proof assertions, exemption constraint instructions are generated during code generation.
[0009] Preferably, the method further includes: Accumulated security assertions are stored using a hash structure, with intermediate representation value references used as retrieval keys to associate the corresponding security assertions; when determining the security assertion for each memory access operation, the target security assertion is read through the hash index.
[0010] Preferably, the method further includes: Receive the incremental portion resulting from the modification of the source code; The incremental part is compiled to obtain an incremental intermediate representation. An optimization pass is performed on the incremental intermediate representation to generate a new security assertion. The original security assertion is updated based on the new security assertion.
[0011] Preferably, the method further includes: In the process of selecting a code generation strategy, a threshold dynamic adjustment logic is adopted, and the judgment criteria for memory access proof are adjusted according to the security coverage as follows: When the overall security coverage is high, the judgment criteria should be relaxed and the number of constraint instructions inserted should be reduced. When overall security coverage is low, tighten the judgment criteria and strengthen the constraints on memory access.
[0012] On the other hand, embodiments of this application also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method described above.
[0013] Furthermore, embodiments of this application provide a combined compilation optimization and sandbox security constraint generation apparatus, the apparatus comprising: a source code receiving module, an intermediate representation generation module, an optimized pass execution module, a strategy selection module, and a machine code generation module, wherein: The source code receiving module is used to receive source code; The intermediate representation generation module is used to compile the source code into an intermediate representation; The optimization pass execution module is used to perform one or more optimization passes on the intermediate representation. Each optimization pass produces an optimized intermediate representation transformation result and a security assertion characterizing the provable security attributes of memory access operations. When multiple optimization passes are executed, the security assertion is propagated and accumulated between the multiple optimization passes. The strategy selection module is used to select a code generation strategy with corresponding overhead based on the security assertion of each memory access operation. The machine code generation module is used to generate native machine code with hierarchical sandbox security constraints based on the optimized intermediate representation transformation result and the selected code generation strategy.
[0014] Compared with the prior art, the technical effect of the embodiments of this application is that by extending the compilation optimization to simultaneously produce optimization results and security assertions, and generating sandbox constraint instructions based on the hierarchical generation of security assertions, the positive coupling of compilation optimization and sandbox security constraints is achieved. The higher the optimization strength, the higher the security assertion coverage, and the lower the redundancy of sandbox constraint instructions, thereby significantly reducing the performance overhead of native code sandbox protection, while ensuring the effectiveness of memory access security constraints, and solving the problems of redundant checks and fixed performance loss in the prior art. Attached Figure Description
[0015] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings: Figure 1 A flowchart illustrating the overall process of the compilation optimization and sandbox security constraint joint generation method provided in this application embodiment; Figure 2 This is a schematic diagram of the optimization pass input / output structure in the joint optimization and security analysis phase of this application embodiment; Figure 3 A flowchart illustrating the overall process of taking measures for different optimization directions in the embodiments of this application; Figure 4 This is a flowchart illustrating the logical decision-making process of the hierarchical code generation strategy in this application embodiment; Figure 5 This is a structural block diagram of the compilation optimization and sandbox security constraint joint generation device provided in the embodiments of this application. Detailed Implementation
[0016] The technical solution of this application will be clearly and completely described below with reference to the concepts in the embodiments of this application. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0017] As mentioned earlier, the core process of existing native code sandbox protection technologies (such as Google NaCl) is to first compile and optimize the source code (including conventional optimizations such as constant propagation, boundary check elimination, and escape analysis) to generate optimized intermediate representations or machine code; then, a sandbox constraint instruction insertion step is executed independently to uniformly add masks or boundary check instructions to all memory access operations, thereby limiting the scope of memory access. The main problem with this process is that compilation optimization and sandbox constraint insertion are two completely independent stages, and the analysis results of the optimization stage (such as a certain memory access being proven safe) cannot be used in the sandbox constraint insertion stage.
[0018] Specifically, taking array access `arr[i]` as an example, even if boundary checks have proven that the range of values for `i` in the loop is always less than the length of `arr`, and the memory range of `arr` is within the sandbox safety domain, existing technologies will still insert mask instructions (such as `addr=base + (i & MASK)`) for this array access. This redundant instruction increases the clock cycles of code execution, leading to performance loss. Furthermore, regardless of how much the strength of compiler optimization is increased (such as adding loop unrolling, global value numbering, etc.), the insertion logic and number of sandbox constraint instructions remain unchanged. The benefits of optimization cannot be converted into a reduction in security protection overhead, forming a non-positive coupling state where "the stronger the optimization, the unchanged security overhead." Ultimately, this results in a fixed performance loss in the native code under security protection, which cannot be improved through optimization.
[0019] To address the problems existing in the prior art, embodiments of this application provide a method for jointly generating compiler optimizations and sandbox security constraints, primarily targeting native code. See also Figure 1 The diagram illustrates the overall flowchart of the method, which includes the following steps: Step S11: Receive the source code and compile it into an intermediate representation (IR); The input raw source code (such as TypeScript or C++ code, compiled by the xlang compiler) is sequentially compiled into a high-level intermediate representation (HIR) and a middle-level intermediate representation (MidIR). The intermediate representation (IR) is the compiler's abstract syntactic structure description of the source code and is the core object of subsequent optimization processing. Different levels of IR correspond to different levels of code representation during the compilation process. HIR is closer to the source code structure, while MidIR has undergone preliminary standardization to facilitate optimization processing. For example, the MidIR generated after compiling the C++ code "int arr[4]; int x = arr[3];" will clearly represent core information such as "base address of array arr, index 3, memory read operation", providing a foundation for subsequent optimization and assertion generation.
[0020] Step S12: Perform one or more optimization passes on the intermediate representation IR to generate and propagate security assertions. The input and output of each optimization pass include the optimization result of the optimization pass and the corresponding security assertion. In this step, "optimization pass" is a specialized term in the compiler field, also known as "pass" or simply "pass". In compiler technology, an optimization pass is an independent processing unit that performs a complete scan of the native code (intermediate representation IR in this embodiment) to optimize the code. The output of the previous optimization pass can serve as the input for the next. Multiple optimization passes are executed sequentially in a pipelined manner, forming the compiler's optimization pipeline. In this embodiment, optimization passes are characterized by completeness, functional specialization, pipeline connectivity, and scalability. Regarding "completeness," each optimization pass completely traverses all intermediate representation code without segmentation. Regarding "functional specialization," a single optimization pass focuses on a single type of compiler optimization action (such as constant propagation, boundary check elimination, escape analysis, etc.). Regarding "pipeline connectivity," multiple optimization passes are primarily executed sequentially, with the output of a preceding optimization pass equal to the input of a subsequent optimization pass, forming a standard component of the compiler pipeline. Regarding "scalability," the number of optimization passes can be increased or decreased, and the execution order adjusted as needed; that is, the optimization intensity can be adjusted as required. These characteristics will be further explained with examples in the subsequent descriptions of this embodiment. Two key concepts to distinguish here are: optimization passes are not the same as compilation phases. The compilation phase is a macroscopic term encompassing IR generation, optimization, and code generation, while an optimization pass is a subdivided execution unit within the optimization phase. A single compilation optimization phase can contain dozens of sequentially executed optimization passes. Furthermore, optimization passes are not the same as optimization strategies. Optimization strategies are technical approaches (such as constant propagation strategies), while optimization passes are the implementation of those strategies in the executable code units of the compiler.
[0021] Having understood the concept of "optimized pass," this embodiment of the application performs multiple optimized passes on the intermediate representation IR (which may be MidIR in this embodiment) obtained in step S11. See also... Figure 2 The figure illustrates the input-output logic of the joint optimization and security analysis phases. In this embodiment, each traditional optimization pass is expanded from a single-output form of "input_IR → output_IR" to a dual-output form of "(input_IR, input_assertion) → (output_IR, output_assertion)". That is, based on the traditional optimization pass output, the input of the current optimization pass includes at least the optimization results of the previous optimization pass and the corresponding security assertions, and the output also includes at least the security assertions corresponding to the accumulated results of the previous optimization pass and the results of the current optimization operation. Here, input_assertions are the security assertions accumulated from the previous optimization pass (initially the initial security assertions), and output_assertions are the security assertions added by the current optimization pass based on its own analysis results.
[0022] In the embodiments of this application, a safety assertion is a typed predicate attached to an IR value, which is used to characterize the provable safety attributes of memory access operations. The so-called "typed" means that, different from free strings and custom remarks, each type of predicate corresponds to predefined semantics and fields, belongs to strongly typed structured data, and can be automatically parsed and matched by the compiler without manual interpretation. The so-called "attached to an IR value" indicates that the typed predicate is auxiliary metadata of the IR value, bound to the IR value, and flows along with the IR in the optimization pipeline, without changing the original code execution logic, and only serves as a basis for compiler safety analysis. In the embodiments of this application, safety assertions can include multiple types, and several types are listed below for illustration: ·InBounds: indicates that the memory access at a certain address is provably within the safe domain [base_addr, base_addr+size); ·ConstantAddr: indicates that a certain value is a compile-time known constant address; ·Aliased: indicates that two memory accesses point to the same address, and if one is safe, the other is also safe; ·LoopBounded: indicates that all memory accesses in the loop body are constrained by induction variables; ·StackLocal: indicates that the object is a stack local variable and is located within the safe domain.
[0023] Each optimization pass generates a corresponding safety assertion according to its own optimization logic: (1) Sparse Conditional Constant Propagation (SCCP): during the optimization process, variables are replaced with constant values. If the memory access address is "safe domain base address + constant offset" and the constant offset is smaller than the size of the safe domain, an InBounds assertion is generated (the proof source is SCCP). For example, for arr[3] (the length of arr is ≥4, and the size of the safe domain is ≥4), the SCCP optimization parses the index value as a constant, and then generates an InBounds(addr=arr+3, proof=SCCP) assertion; (2) Bound Check Elimination (BCE): during the optimization process, redundant boundary check statements (such as if (i < len)) are eliminated. If the value range of the induction variable i in the loop is [0, len) and len ≤ the size of the safe domain, a LoopBounded assertion is generated (the proof source is BCE). For example, for the loop for (let i = 0; i < arr.length; i++) arr[i], after the BCE optimization eliminates the boundary check, a LoopBounded(loop_id=xxx, bound=arr.length) assertion is generated; (3) Escape Analysis: During optimization, heap allocation is downgraded to stack allocation. If the object does not escape the current function and is allocated on the stack within the safe region, a StackLocal assertion is generated (the proof comes from EscapeAnalysis). For example, for the code let p = new Point(1,2); return px;, escape analysis determines that p is a stack local variable and generates a StackLocal(obj=p) assertion; (4) Global Value Numbering (GVN): During optimization, redundant calculations are eliminated. If the IR values of two memory accesses have the same global value number, and one of them already has an InBounds assertion, then an Aliased assertion is generated (the proof comes from GVN). For example, for "x = arr[i]; ... y = arr[i];", GVN determines that the two accesses point to the same address. If the access of x already has an InBounds assertion, then an Aliased(a=x, b=y) assertion is generated, and the access of y inherits the InBounds property. (5) Loop Unroll: During the optimization process, the loop body is copied N times. If the original loop has the LoopBounded assertion, then all the unrolled loop body copies inherit the assertion (the proof comes from LoopUnroll).
[0024] During each optimization pass, security assertions propagate forward through the optimization pipeline: each optimization pass receives assertions accumulated from all previous passes, can consume existing assertions to achieve further optimization (such as eliminating redundant security check analysis based on Aliased assertions), produce new assertions (such as InBounds assertions generated by SCCP), or enhance existing assertions (such as upgrading partially proven assertions to full proofs).
[0025] Step S13: Based on the optimized IR and the security assertion, perform hierarchical code generation and output native machine code with sandbox security protection capabilities; Using the optimized IR and accumulated security assertions obtained in step S12 as input, during the conversion stage from MidIR to machine code, the security assertion database is queried for each memory access instruction, and the corresponding code generation strategy is selected according to the assertion type: (1) If the memory access operation has a fully proven InBounds assertion (such as a fully verified security source such as SCCP, BCE, etc.), then a normal load / store instruction is generated, with no additional security constraint instructions and an overhead of 0; (2) If the memory access operation has partial security proof (such as the upper bound of the address is known but not fully proven to be within the security domain), then the memory access code containing the address mask instruction is generated in the form of load [base + (offset & MASK)] (MASK = security domain size - 1, the security domain size must be a power of 2 or rounded up), which only adds 1 AND instruction and has an overhead of about 0.3 nanoseconds; (3) If the memory access operation does not have any safety assertions, then code containing a complete boundary check sequence is generated in the form cmp offset, size; jae trap; load [base + offset], adding 2-3 instructions and costing about 1 nanosecond.
[0026] The aforementioned steps generate native machine code with sandbox security capabilities, balancing performance and memory access security. If an executable file (an executable native program) is also needed, a file header, relocation information, and corresponding linking and encapsulation steps must be added to the native machine code. This application primarily describes the compilation process; after generating the native machine code, an instruction stream can be formed that can be directly executed by the CPU.
[0027] Step S14: Verify the generated native machine code and generate a security coverage report; Based on the aforementioned steps, this embodiment of the application may also select to perform static scanning verification on the raw machine code generated in step S13 to confirm that each memory access operation either has a corresponding security assertion coverage or has generated a corresponding constraint instruction; at the same time, it verifies that there are no original system call instructions (such as int 0x80 / svc / syscall) in the machine code, and that all jump targets are within the scope of legal code; finally, a security coverage report is generated, which counts the proportion of memory accesses that have been proven to be safe to all memory accesses, and the specific content will be further detailed later.
[0028] Compared with the prior art, the embodiments of this application achieve deep coupling between compilation optimization and security assertion generation by extending the optimization pass to a dual-output form. The security assertions can accurately capture the memory access security attributes proven during the optimization process. In the code generation stage, the hierarchical strategy based on security assertions avoids inserting redundant constraint instructions for proven safe memory accesses, directly eliminating the performance loss of this part of the operation; for partially proven safe operations, only low-overhead mask instructions are inserted, rather than a complete boundary check sequence, further reducing overhead.
[0029] To illustrate the aforementioned technical effects, let's take the example of accessing a loop array. As is well known, existing technologies insert a complete boundary check sequence (2-3 instructions) for each array access within a loop. However, in this solution, the LoopBounded assertion generated by BCE optimization is recognized during code generation, and all accesses within the loop generate ordinary load / store instructions with no additional overhead. In this scenario alone, the performance loss of loop execution can be reduced from approximately 15% to 0. Simultaneously, the increased optimization strength directly increases the coverage of safety assertions. For example, adding loop unrolling optimization passes allows the unrolled loop body to inherit the original loop's LoopBounded assertion, further expanding the scope of overhead-free memory access. This achieves a positive coupling of "stronger optimization → more safety assertions → lower sandbox overhead," completely resolving the lack of synergy between optimization and security protection in existing technologies.
[0030] While the aforementioned embodiments of this application can solve the main problems existing in the prior art, several unresolved issues may still exist after the technical solution is implemented. These mainly manifest in the following ways: First, for custom scenarios where developers are clearly aware that memory access is absolutely safe, the existing assertion system lacks a dedicated entry point, preventing developers from actively marking security attributes. Such accesses will still be subject to additional constraint instructions, resulting in unnecessary overhead. Second, the strength of security assertion proofs from a single source is limited. Multiple optimization analysis conclusions, when superimposed, cannot be merged into a high-confidence strong proof. The hierarchical strategy is conservative, still retaining some low-overhead mask instructions. Third, as the compilation scale and the number of assertions increase, the linear assertion retrieval method will lengthen compilation time and reduce overall compilation efficiency. Based on these issues, the embodiments of this application can further optimize assertion enhancement and cross-domain composite aspects, especially in terms of assertion system, composite rules, and retrieval methods. See also... Figure 3 The figure illustrates a flowchart of measures taken in three optimization directions based on the foregoing embodiments of this application. Specific steps include: Step S21: Expand the proof source type of the security assertion; In the security assertion definition in step S12, a new UserAssert proof source type is added (user-provided assertions, as unsafe escape ports, can become user assertion types); when the user explicitly marks the security attributes of a certain memory access in the source code (such as unsafe_assert arr[i] in [base, base+size)), the corresponding optimized pass will generate an InBounds assertion and mark the proof source as UserAssert.
[0031] Step S22: Implement cross-over composition enhancement of assertions; In the assertion propagation process of step S12, a new assertion composition logic is added: if an InBounds assertion of a memory access is composed of multiple proof sources (such as simultaneously satisfying SCCP constant propagation and GVN alias proof), then the assertion is marked as "strong proof"; in the hierarchical code generation of step S13, the assertion of "strong proof" adopts the zero-overhead strategy first, and even if some boundary conditions are not fully verified, the insertion of mask instructions can be exempted.
[0032] Step S23: Optimize assertion storage and query efficiency; The security assertions accumulated in step S12 are used to build a hash table indexed by IR values. The key of the hash table is an IR value reference, and the value is a list of all security assertions corresponding to that IR value. In the assertion query stage of step S13, the assertions of the target memory access are obtained quickly by directly using the hash table index, reducing the query time complexity from O(n) to O(1) (n is the total number of assertions).
[0033] As can be seen from the foregoing, existing technologies lack entry points for user-defined security assertions. Even for memory accesses that the user is clearly aware of are safe, redundant constraint instructions are still inserted. The newly added UserAssert type in this optimized solution not only meets the flexible configuration needs of special scenarios but also incorporates user assertions into a unified security management system through the assertion mechanism, avoiding the security risks of completely unsafe operations. Furthermore, the cross-over composite enhancement of assertions in this optimized solution further improves the effectiveness of security assertions. The exemption strategy for "strong proof" assertions can further reduce overhead in some scenarios while ensuring security. The optimization of the assertion hash table solves the problem of low query efficiency when a large number of assertions accumulate: in the compilation of large projects containing tens of thousands of IR values, the existing linear query method increases compilation time by about 5%. This optimization reduces query time to negligible levels, ensuring that compilation efficiency is not affected by the increase in the number of assertions.
[0034] The aforementioned technical solutions address prominent issues in existing technologies. However, they share a common characteristic: all solutions are geared towards full-scale compilation scenarios. They exhibit significant shortcomings in incremental compilation scenarios involving daily iterative development and partial module modifications. These shortcomings are primarily manifested in the following ways: First, after partial code modifications, existing mechanisms require re-execution of full optimization, full assertion generation, and full sandbox code generation. Incremental compilation takes nearly as long as full-scale compilation, severely impacting development and debugging efficiency. Second, fixed code generation strategy thresholds cannot adapt to business scenarios with varying security levels. High-security scenarios may overly relax the strategy, while low-overhead scenarios may overly tighten constraints. Third, assertion judgments during the compilation phase are based solely on static analysis and cannot be combined with the actual program runtime state. A small number of boundary scenarios may exhibit assertion judgment biases, affecting security or performance. Therefore, the embodiments of this application can further optimize incremental compilation, dynamic strategies, and runtime feedback.
[0035] For incremental compilation optimization, this application embodiment constructs an incremental update mechanism for security assertions. In the incremental compilation scenario (only some modules of the source code are modified), step S11 only recompiles the modified modules to generate incremental IRs; in step S12, only the incremental IRs are optimized, and incremental updates are performed based on the original assertion library: security assertions of unmodified modules are retained, assertions of modified modules are updated only, invalid assertions are deleted (such as assertions invalidated due to variable name changes), and new assertions of modified modules are added.
[0036] For dynamic strategies, this application embodiment adopts a method of dynamically adjusting the code generation strategy threshold. See also... Figure 4 The diagram illustrates the logic of hierarchical code generation and dynamic threshold determination. Specifically, based on step S13, a new logic for dynamically adjusting the policy threshold is added: According to the security coverage report results (see the previous steps for generating the security coverage report), if the overall security coverage is ≥90%, the threshold for determining "partial proof" is relaxed (e.g., assertions with only loop range constraints are allowed to be classified as partial proofs), further reducing the number of mask instructions inserted; if the security coverage is <60%, the threshold is tightened to ensure the constraint strength of core memory access. This scheme uses security coverage to characterize the completeness of security proofs for memory access. When the security coverage is high, the threshold for code generation is relaxed, reducing the insertion of constraint instructions; when the security coverage is low, the threshold is tightened, strengthening the security constraints of memory access.
[0037] Regarding the runtime feedback mechanism, this application embodiment adopts a runtime verification feedback mode for security assertions. Specifically, lightweight runtime assertion verification instructions are added to the generated machine code for a small number of critical memory access operations (such as accesses near security domain boundaries). When the runtime detects a discrepancy between the assertion and the actual memory access, relevant information is recorded and fed back to the compilation stage. The optimization pass in step S12 will adjust the assertion generation logic based on this feedback (such as optimizing the accuracy of inductive variable range analysis).
[0038] As can be seen from the foregoing, existing technologies require re-execution of full optimization and sandbox constraint insertion during incremental compilation, resulting in compilation time close to that of full compilation. However, the incremental assertion update mechanism of this optimization solution only handles assertions of modified modules, reducing incremental compilation time to less than 30% of full compilation time, significantly improving compilation efficiency during the development phase. The dynamic threshold adjustment logic in this optimization direction can flexibly balance security and performance based on actual security coverage: further reducing overhead in high-coverage scenarios and ensuring security in low-coverage scenarios, avoiding the limitations of a single strategy. The runtime verification feedback in this optimization direction forms a closed-loop optimization of "compile-run-compile," continuously improving the accuracy of assertion generation: for example, if runtime discovers that a loop's LoopBounded assertion does not cover boundary conditions, the compilation phase will optimize the inductive variable analysis logic of BCE, resulting in more accurate assertions for that loop in subsequent compilations, further improving security coverage and reducing long-term performance overhead.
[0039] The foregoing description details the method embodiments provided in this application. In fact, this application also provides corresponding apparatus embodiments. See [link to relevant documentation]. Figure 5 The diagram illustrates a structural block diagram of an embodiment of the device. The compiler optimization and sandbox security constraint joint generation device U50 of this application embodiment is primarily designed for native code and specifically comprises: a source code receiving module U51, an intermediate representation generation module U52, an optimized pass execution module U53, a strategy selection module U54, and a machine code generation module U55, wherein: The source code receiving module U51 is used to receive source code. The intermediate representation generation module U52 is used to compile the source code into an intermediate representation; The optimization pass execution module U53 is used to perform one or more optimization passes on the intermediate representation. Each optimization pass produces an optimized intermediate representation transformation result and a security assertion characterizing the provable security attributes of memory access operations. When multiple optimization passes are executed, the security assertion is propagated and accumulated between the multiple optimization passes. The strategy selection module U54 is used to select a code generation strategy with corresponding overhead based on the security assertion for each memory access operation; The machine code generation module U55 is used to generate native machine code with hierarchical sandbox security constraints based on the optimized intermediate representation transformation result and the selected code generation strategy.
[0040] Based on the same principle, the aforementioned device embodiments can also be optimized, incorporating further modules. For example, the device may further include a verification module U56, used to perform static scanning verification on the generated native machine code to confirm that each memory access operation is covered by security assertions or protected by constraint instructions. The aforementioned strategy selection module U54 can be configured to: generate zero-overhead direct memory access instructions for memory access operations with complete security proofs; generate memory access code containing a single address mask instruction for memory access operations with partial security proofs; and generate memory access code containing a complete boundary check sequence for memory access operations without security proofs. The aforementioned security assertions can also be typed predicates appended to intermediate representation IR values, including at least constant address assertions generated by constant propagation, loop range assertions generated by boundary check elimination, stack local assertions generated by escape analysis, and alias assertions generated by value numbering. Increasing the number or intensity of optimization passes during optimization pass execution monotonically increases the security assertion coverage and monotonically decreases the number of constraint instructions in the generated code. Both the aforementioned device embodiments and the further optimized technical functional modules can achieve the same technical effects as the aforementioned method embodiments. To avoid repetition, they will not be elaborated here.
[0041] Furthermore, the technical solution of this application can be formed on a computer-readable storage medium to form a computer program. The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
[0042] 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 in some cases as a completely hardware embodiment, or as a completely software embodiment, or as an embodiment combining 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.
[0043] 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.
[0044] 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.
[0045] 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.
[0046] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0047] 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.
[0048] Computer-readable media include both permanent and non-permanent, removable and non-removable media that can store information by 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 media, such as modulated data signals and carrier waves.
[0049] 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.
[0050] 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 jointly generating compiler optimization and sandbox security constraints, characterized in that, The method includes: Receive source code and compile the source code into an intermediate representation; One or more optimization passes are performed on the intermediate representation, each optimization pass producing an optimized intermediate representation transformation result and a security assertion characterizing the provable security attributes of memory access operations; when multiple optimization passes are performed, the security assertions are propagated and accumulated between the multiple optimization passes; Select a code generation strategy with corresponding overhead based on the security assertion for each memory access operation; Based on the optimized intermediate representation transformation result and the selected code generation strategy, native machine code with hierarchical sandbox security constraints is generated.
2. The method as described in claim 1, characterized in that, The code generation strategy includes: If the memory access operation has a security assertion with full security proof, then a direct memory access instruction with zero overhead is generated. If the memory access operation has a security assertion with partial security proof, then memory access code containing a single address constraint instruction is generated; If there is no security assertion for the memory access operation with a security proof, then memory access code containing a complete sequence of boundary checks is generated.
3. The method as described in claim 1, characterized in that, The security assertion is a typed predicate appended to the intermediate representation value. The typed predicate is a predefined Boolean condition used to determine the security attributes of memory access, and includes at least one or more of the following: Address access range assertions that characterize the range of address accesses; Constant address assertions representing constant addresses, generated by constant propagation; Eliminate the cyclic access constraint assertion of the output by boundary checks; Stack-local object assertions representing stack-local objects, generated from escape analysis; Address alias assertions that represent address aliases generated by global value numbers.
4. The method as described in claim 1, characterized in that, The security assertion also includes a user assertion type. When there are explicit annotations in the source code that characterize memory access security attributes, the corresponding optimized pass generates a security assertion corresponding to the user assertion type.
5. The method as described in claim 1, characterized in that, The method further includes: During the propagation of multiple optimized security assertions, multiple security assertion types for the same memory access operation are combined. When there are multiple security assertions corresponding to a memory access, the security assertion of the memory access is marked as a strong proof assertion; for memory accesses with strong proof assertions, exemption constraint instructions are generated during code generation.
6. The method as described in claim 1, characterized in that, The method further includes: Accumulated security assertions are stored in a hash table, with the intermediate representation value reference used as the retrieval key to associate the corresponding security assertion; when determining the security assertion for each memory access operation, the target security assertion is read through the hash index.
7. The method as described in claim 1, characterized in that, The method further includes: Receive the modified incremental source code; The incremental source code is compiled to obtain an incremental intermediate representation. An optimization pass is performed on the incremental intermediate representation to generate a new security assertion. The original security assertion is updated based on the new security assertion.
8. The method as described in claim 1, characterized in that, The method further includes: In the process of selecting a code generation strategy, a threshold dynamic adjustment logic is adopted, and the judgment criteria for memory access proof are adjusted according to the security coverage as follows: When the overall security coverage is high, the judgment criteria should be relaxed and the number of constraint instructions inserted should be reduced. When overall security coverage is low, tighten the judgment criteria and strengthen the constraints on memory access.
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 combined compiler optimization and sandbox security constraint generation apparatus, characterized in that, The device includes: a source code receiving module, an intermediate representation generation module, an optimized pass execution module, a strategy selection module, and a machine code generation module, wherein: The source code receiving module is used to receive source code; The intermediate representation generation module is used to compile the source code into an intermediate representation; The optimization pass execution module is used to perform one or more optimization passes on the intermediate representation. Each optimization pass produces an optimized intermediate representation transformation result and a security assertion characterizing the provable security attributes of memory access operations. When multiple optimization passes are executed, the security assertion is propagated and accumulated between the multiple optimization passes. The strategy selection module is used to select a code generation strategy with corresponding overhead based on the security assertion of each memory access operation. The machine code generation module is used to generate native machine code with hierarchical sandbox security constraints based on the optimized intermediate representation transformation result and the selected code generation strategy.
