/

Native AOT Support

Shield provides specialized protection for applications using .NET's Native AOT compilation technology, enabling robust security while maintaining AOT compatibility.


Understanding Native AOT

Native AOT is Microsoft's latest approach to Ahead-of-Time compilation for .NET:

  • Creates fully self-contained native applications
  • Eliminates the need for JIT compilation at runtime
  • Produces smaller deployments with faster startup
  • Has specific constraints around reflection and code generation
  • Available in .NET 7+ and is the successor to CoreRT

Native AOT applications present unique protection challenges due to their compilation model and reflection limitations.


Native AOT Protection Workflow

When protecting Native AOT applications, Shield follows a specialized workflow:

  1. Pre-AOT Protection: Shield applies compatible protections before Native AOT compilation
  2. Trimming-Aware Transformations: Applies obfuscation compatible with the trimmer
  3. Metadata Preservation: Ensures required metadata for AOT compilation is preserved
  4. Post-Compilation Protection: Optional binary protection for the compiled native executable
graph LR
    A[.NET Assembly] --> B[Shield Protection]
    B --> C[Protected Assembly]
    C --> D[Native AOT Compilation]
    D --> E[Native Executable]
    E --> F[Optional Binary Protection]

For Native AOT applications, we recommend the following protections:

{
  "protections": {
    "rename": {
      "rename_public": false,
      "rename_arguments": true
    },
    "control_flow_advance": {
      "intensity": 6
    },
    "constants_encryption": {},
    "constants_mutation": {},
    "virtualization": {}
  }
}

The following protections should be avoided with Native AOT:

  • Anti-Debugger
  • Invalid Metadata
  • Invalid Code
  • Any protection that heavily relies on reflection

Handling Reflection in Native AOT

Shield automatically preserves necessary reflection-related attributes and metadata for Native AOT compilation. This includes:

  • DynamicallyAccessedMembers attributes
  • UnconditionalSuppressMessage attributes
  • Required metadata for serialization
  • Runtime type information needed by Native AOT

You can use the [Obfuscation] attribute to fine-tune protection:

[Obfuscation(Exclude = false, Feature = "-control_flow_advance")]
public void ReflectionUsedMethod() { }

Troubleshooting Native AOT Protection

Common Issues and Solutions

IssueSolution
Missing reflection dataAdd appropriate DynamicallyAccessedMembers attributes
Failed trimmingDisable trimming in your Native AOT configuration
Startup crashesVerify no serialization-related issues
Increased binary sizeAdjust string encryption settings

Always test your protected Native AOT application thoroughly across all target platforms and architectures. Native AOT behavior can vary between different platforms.


Best Practices

  1. Start simple: Begin with basic protection and increase gradually
  2. Test thoroughly: Verify functionality across all target platforms
  3. Be cautious with reflection: Explicitly mark types used with reflection
  4. Disable trimming: Avoid using trimming with protected Native AOT applications
  5. Layer protection: Use both IL-level protection (Shield) and binary protection measures

For more information on performance considerations, see Performance Impact.

Previous
Cross-platform Strategy