This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
mobilecritical PriorityID: android-15-ndk-16kb-page-alignment

Android 15 NDK 16KB Page Alignment and Memory Boundary Guardrail

Enforces strict 16KB memory page boundary alignment for all native C/C++ shared objects (.so), linker flags, and JNI memory mappings targeting Android 15 (API 35+) and modern hardware. Failure to align ELF segments to 16KB boundaries results in runtime linker segmentation faults (SIGSEGV) and fatal dlopen crashes on modern arm64-v8a devices. Mandates automated ELF verification, linker configuration updates, and safe JNI direct buffer mapping.

Prohibited Anti-Pattern: Compiling NDK Binaries with Default 4KB Alignment Leading to Silent SIGSEGV on Android 15+

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Android 15 NDK 16KB Page Alignment and Memory Boundary Guardrail - Enforces strict 16KB memory page boundary alignment for all native C/C++ shared objects (.so), linker flags, and JNI memory mappings targeting Android 15 (API 35+) and modern hardware. Failure to align ELF segments to 16KB boundaries results in runtime linker segmentation faults (SIGSEGV) and fatal dlopen crashes on modern arm64-v8a devices. Mandates automated ELF verification, linker configuration updates, and safe JNI direct buffer mapping.
globs: **/CMakeLists.txt, **/build.gradle, **/build.gradle.kts, **/Android.mk, **/*.cpp, **/*.c, **/*.h
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/android-15-ndk-16kb-page-alignment"
---

# Android 15 NDK 16KB Page Alignment and Memory Boundary Guardrail

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Compiling NDK Binaries with Default 4KB Alignment Leading to Silent SIGSEGV on Android 15+

## System Constraints
You are a Principal Mobile Native Systems Architect enforcing the Android 15 NDK 16KB Memory Page Alignment Guardrail. You must prohibit any build script, linker configuration, or JNI memory mapping that assumes or hardcodes 4KB (4096-byte) page sizes. You must mandate '-Wl,-z,max-page-size=16384' linker directives in CMake and ndk-build, ensure JNI memory alignment dynamically queries 'sysconf(_SC_PAGESIZE)', and enforce AGP 8.5.1+ packaging verification.

## Strict Guidelines
- Engineers MUST ALWAYS pass '-Wl,-z,max-page-size=16384' in CMAKE_SHARED_LINKER_FLAGS and target_link_options for all native targets.
- Engineers MUST NOT assume a 4096-byte page size via hardcoded constants, bit shifts (e.g., '>> 12'), or '#define PAGE_SIZE 4096'.
- Native memory mmap calls MUST pass offset arguments that are strictly aligned to 'sysconf(_SC_PAGESIZE)' or use 'posix_memalign' with runtime page sizes.
- Android Gradle Plugins MUST be upgraded to at least 8.5.1 or NDK r27+ to support native 16KB alignment verification during packaging.
- Engineers MUST verify compiled '.so' libraries using 'objdump -p <lib>.so' or llvm-readelf to ensure LOAD segments have 'align 2**14' (16384).
- Custom memory allocators and ring buffers in native C/C++ MUST dynamically query and allocate memory aligned to 16KB boundaries.
- JNI Direct ByteBuffers accessed across the JVM-Native boundary MUST NOT make page-offset assumptions when calculating sub-buffer pointers.

## Bad vs Good Implementations
### CMake Native Target Linker Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Omitting explicit 16KB max page size linker flags causes Clang/LLD in pre-r27 NDKs to generate ELF load segments aligned to 4KB (0x1000). On Android 15 with 16KB kernel pages enabled, dlopen() will fail immediately with an alignment error.
```cmake
# INSECURE: Default alignment assumes legacy 4KB page sizes
cmake_minimum_required(VERSION 3.22.1)
project(native_engine CXX)

add_library(native_engine SHARED
    src/engine.cpp
    src/jni_bridge.cpp
)

target_link_libraries(native_engine log)
```

#### Production Standard (Correct)
> **Standard**: Injecting '-Wl,-z,max-page-size=16384' forces the linker to align ELF PT_LOAD program headers to 16384 bytes, guaranteeing binary compatibility across both legacy 4KB and modern 16KB kernel environments.
```cmake
# SECURE: Explicitly mandate 16KB page alignment for Android 15+
cmake_minimum_required(VERSION 3.22.1)
project(native_engine CXX)

add_library(native_engine SHARED
    src/engine.cpp
    src/jni_bridge.cpp
)

# Enforce 16KB ELF segment alignment across arm64-v8a and x86_64
target_link_options(native_engine PRIVATE
    "-Wl,-z,max-page-size=16384"
)

target_link_libraries(native_engine log)
```

### Native mmap and Custom Allocation Alignment

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using a hardcoded 4096-byte mask will cause mmap() to fail with EINVAL on 16KB page runtimes if the resulting offset is not a multiple of 16384.
```cpp
#include <sys/mman.h>
#include <unistd.h>

#define LEGACY_PAGE_SIZE 4096

void* allocate_buffer(int fd, size_t offset, size_t length) {
    // FATAL: Hardcoded 4KB alignment assumption
    size_t aligned_offset = offset & ~(LEGACY_PAGE_SIZE - 1);
    void* ptr = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, aligned_offset);
    return ptr; // Will return MAP_FAILED with EINVAL on 16KB devices if offset is not 16KB aligned
}
```

#### Production Standard (Correct)
> **Standard**: Using 'sysconf(_SC_PAGESIZE)' retrieves the actual kernel page size at runtime (4096, 16384, or 65536 bytes), guaranteeing that memory offsets and bounds comply with the host kernel's MMU requirements.
```cpp
#include <sys/mman.h>
#include <unistd.h>
#include <stdexcept>

void* allocate_buffer(int fd, size_t offset, size_t length) {
    // Dynamically retrieve the page size from the operating system
    const size_t system_page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
    if (system_page_size == 0) {
        throw std::runtime_error("Unable to determine system page size");
    }

    const size_t aligned_offset = offset & ~(system_page_size - 1);
    void* ptr = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, aligned_offset);
    if (ptr == MAP_FAILED) {
        throw std::runtime_error("Memory mapping failed");
    }
    return ptr;
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Android 15 NDK 16KB Page Alignment and Memory Boundary Guardrail

Android 15 NDK 16KB Page Alignment and Memory Boundary Guardrail defines an industrial-grade behavioral standard for mobile engineering. Enforces strict 16KB memory page boundary alignment for all native C/C++ shared objects (.so), linker flags, and JNI memory mappings targeting Android 15 (API 35+) and modern hardware. Failure to align ELF segments to 16KB boundaries results in runtime linker segmentation faults (SIGSEGV) and fatal dlopen crashes on modern arm64-v8a devices. Mandates automated ELF verification, linker configuration updates, and safe JNI direct buffer mapping. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Compiling NDK Binaries with Default 4KB Alignment Leading to Silent SIGSEGV on Android 15+ Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftCompiling NDK Binaries with Default 4KB Alignment Leading to Silent SIGSEGV on Android 15+Critical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS pass '-Wl,-z,max-page-size=16384' in CMAKE_SHARED_LINKER_FLAGS and target_link_options for all native targets.

Engineers MUST ALWAYS pass '-Wl,-z,max-page-size=16384' in CMAKE_SHARED_LINKER_FLAGS and target_link_options for all native targets.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT assume a 4096-byte page size via hardcoded constants, bit shifts (e.g., '>> 12'), or '#define PAGE_SIZE 4096'.

Engineers MUST NOT assume a 4096-byte page size via hardcoded constants, bit shifts (e.g., '>> 12'), or '#define PAGE_SIZE 4096'.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Native memory mmap calls MUST pass offset arguments that are strictly aligned to 'sysconf(_SC_PAGESIZE)' or use 'posix_memalign' with runtime page sizes.

Native memory mmap calls MUST pass offset arguments that are strictly aligned to 'sysconf(_SC_PAGESIZE)' or use 'posix_memalign' with runtime page sizes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Android Gradle Plugins MUST be upgraded to at least 8.5.1 or NDK r27+ to support native 16KB alignment verification during packaging.

Android Gradle Plugins MUST be upgraded to at least 8.5.1 or NDK r27+ to support native 16KB alignment verification during packaging.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Engineers MUST verify compiled '.so' libraries using 'objdump -p <lib>.so' or llvm-readelf to ensure LOAD segments have 'align 2**14' (16384).

Engineers MUST verify compiled '.so' libraries using 'objdump -p <lib>.so' or llvm-readelf to ensure LOAD segments have 'align 2**14' (16384).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Custom memory allocators and ring buffers in native C/C++ MUST dynamically query and allocate memory aligned to 16KB boundaries.

Custom memory allocators and ring buffers in native C/C++ MUST dynamically query and allocate memory aligned to 16KB boundaries.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
7

JNI Direct ByteBuffers accessed across the JVM-Native boundary MUST NOT make page-offset assumptions when calculating sub-buffer pointers.

JNI Direct ByteBuffers accessed across the JVM-Native boundary MUST NOT make page-offset assumptions when calculating sub-buffer pointers.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Android 15 NDK 16KB Page Alignment and Memory Boundary Guardrail critical for AI coding assistants?

Enforces strict 16KB memory page boundary alignment for all native C/C++ shared objects (.so), linker flags, and JNI memory mappings targeting Android 15 (API 35+) and modern hardware. Failure to align ELF segments to 16KB boundaries results in runtime linker segmentation faults (SIGSEGV) and fatal dlopen crashes on modern arm64-v8a devices. Mandates automated ELF verification, linker configuration updates, and safe JNI direct buffer mapping. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Compiling NDK Binaries with Default 4KB Alignment Leading to Silent SIGSEGV on Android 15+) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

Related MOBILE Rules

View all rules →
1 Rule in Stack

Android