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

Android 15 16KB ELF & Memory Page Boundary Alignment Invariant

Android 15 devices support 16KB physical memory page sizes to boost performance on modern ARM64 silicon. Shared libraries (.so) compiled with legacy 4KB ELF segment boundaries trigger fatal runtime dlopen errors or SIGBUS/SIGSEGV memory aborts. All NDK toolchains, linker scripts, packaging pipelines, and native memory management routines must explicitly guarantee 16KB page alignment.

Prohibited Anti-Pattern: Hardcoded 4096-Byte Page Constants and Legacy 4KB ELF Segment Alignment in Native NDK Binaries

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Android 15 16KB ELF & Memory Page Boundary Alignment Invariant - Android 15 devices support 16KB physical memory page sizes to boost performance on modern ARM64 silicon. Shared libraries (.so) compiled with legacy 4KB ELF segment boundaries trigger fatal runtime dlopen errors or SIGBUS/SIGSEGV memory aborts. All NDK toolchains, linker scripts, packaging pipelines, and native memory management routines must explicitly guarantee 16KB page alignment.
globs: **/CMakeLists.txt, **/Android.mk, **/build.gradle, **/build.gradle.kts, **/*.cpp, **/*.c, **/*.h
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/android-15-16kb-page-alignment-invariant-android-15-16kb--630"
---

# Android 15 16KB ELF & Memory Page Boundary Alignment Invariant

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Hardcoded 4096-Byte Page Constants and Legacy 4KB ELF Segment Alignment in Native NDK Binaries

## System Constraints
You are a Principal Mobile Systems Architect enforcing the Android 15 16KB Page Alignment Memory Allocation Invariant across all native NDK code, build configurations, and packaging steps. Mandate that every compiled shared object (.so), custom allocator, mmap invocation, and ELF segment adheres to 16KB alignment boundaries to prevent runtime dlopen failures and SIGBUS segmentation faults on Android 15+ kernels.

## Strict Guidelines
- Engineers MUST ALWAYS pass linker flag `-Wl,-z,max-page-size=16384` to both C and C++ target link steps in CMake or ndk-build when targeting NDK r26 or lower; NDK r27+ handles this automatically by default.
- Engineers MUST NEVER use compile-time macros like `PAGE_SIZE`, `PAGE_MASK`, or hardcoded `4096` in C/C++ native source code; page sizes MUST be retrieved at runtime using `sysconf(_SC_PAGESIZE)` or `getpagesize()`.
- Engineers MUST ALWAYS align native memory allocations mapped via `mmap()` or allocated with `posix_memalign()` to multiples of the dynamic runtime page size (`sysconf(_SC_PAGESIZE)`).
- Services MUST verify prebuilt 3rd-party vendor `.so` files using the Android NDK `check_elf_alignment.sh` script or `llvm-objdump -p lib.so | grep LOAD` during CI pipelines.
- Components NEVER bundle unaligned native binaries inside APKs; Android Gradle Plugin (AGP) version 8.3+ or explicit `zipalign -p 16384` MUST be enforced across all APK packaging artifacts.
- Custom memory arenas and ring buffers MUST dynamically round buffer sizes and offsets up using bitwise alignment formulas: `(size + pageSize - 1) & ~(pageSize - 1)`.

## Bad vs Good Implementations
### C++ Native Memory Mapping and Page Size Detection

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Assuming a 4096-byte page size causes mmap to receive invalid alignment offsets or boundaries on 16KB Android 15 kernels, leading to memory faults, memory corruption, or EINVAL errors.
```cpp
#include <sys/mman.h>
#include <unistd.h>

// ANTI-PATTERN: Hardcoded 4096-byte assumption or PAGE_SIZE macro
#define STATIC_PAGE_SIZE 4096

void* allocate_aligned_buffer(size_t payload_size) {
    // Fails on Android 15 16KB kernels with SIGBUS / EINVAL
    size_t aligned_size = (payload_size + STATIC_PAGE_SIZE - 1) & ~(STATIC_PAGE_SIZE - 1);
    void* ptr = mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
    return ptr;
}
```

#### Production Standard (Correct)
> **Standard**: Dynamically queries the kernel runtime page size via sysconf(_SC_PAGESIZE) and rounds mapped buffer sizes to the operational page boundary (4KB, 16KB, or 64KB).
```cpp
#include <sys/mman.h>
#include <unistd.h>
#include <cstddef>

// PRODUCTION FIX: Dynamic page discovery with bitwise boundary rounding
void* allocate_aligned_buffer(size_t payload_size) {
    const long page_size = sysconf(_SC_PAGESIZE);
    if (page_size <= 0) {
        return nullptr;
    }
    const size_t alignment = static_cast<size_t>(page_size);
    const size_t aligned_size = (payload_size + alignment - 1) & ~(alignment - 1);
    
    void* ptr = mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
    if (ptr == MAP_FAILED) {
        return nullptr;
    }
    return ptr;
}
```

### CMake Linker Configuration for 16KB ELF Max Page Size

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Shared libraries linked with 4KB ELF segment alignment fail dlopen loading on Android 15 16KB systems with linker error 'library has invalid segment alignment'.
```cmake
# ANTI-PATTERN: Default or legacy 4KB linker flags in CMakeLists.txt
cmake_minimum_required(VERSION 3.22.1)
project(native_engine)

add_library(native_engine SHARED native_engine.cpp)
# Omitting max-page-size or setting max-page-size=4096 produces 4KB ELF alignments
```

#### Production Standard (Correct)
> **Standard**: Instructs the GNU gold or LLVM lld linker to align all ELF segments to 16384 bytes, allowing the library to be loaded transparently on both 4KB and 16KB Android kernels.
```cmake
# PRODUCTION FIX: Explicit 16KB ELF boundary alignment
cmake_minimum_required(VERSION 3.22.1)
project(native_engine)

add_library(native_engine SHARED native_engine.cpp)

# Enforce 16KB max page size across target link options
target_link_options(native_engine PRIVATE
    "-Wl,-z,max-page-size=16384"
)
```

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 16KB ELF & Memory Page Boundary Alignment Invariant

Android 15 16KB ELF & Memory Page Boundary Alignment Invariant defines an industrial-grade behavioral standard for mobile engineering. Android 15 devices support 16KB physical memory page sizes to boost performance on modern ARM64 silicon. Shared libraries (.so) compiled with legacy 4KB ELF segment boundaries trigger fatal runtime dlopen errors or SIGBUS/SIGSEGV memory aborts. All NDK toolchains, linker scripts, packaging pipelines, and native memory management routines must explicitly guarantee 16KB page alignment. 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: Hardcoded 4096-Byte Page Constants and Legacy 4KB ELF Segment Alignment in Native NDK Binaries 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 DriftHardcoded 4096-Byte Page Constants and Legacy 4KB ELF Segment Alignment in Native NDK BinariesCritical
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 linker flag `-Wl,-z,max-page-size=16384` to both C and C++ target link steps in CMake or ndk-build when targeting NDK r26 or lower

Engineers MUST ALWAYS pass linker flag `-Wl,-z,max-page-size=16384` to both C and C++ target link steps in CMake or ndk-build when targeting NDK r26 or lower; NDK r27+ handles this automatically by default.

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 NEVER use compile-time macros like `PAGE_SIZE`, `PAGE_MASK`, or hardcoded `4096` in C/C++ native source code

Engineers MUST NEVER use compile-time macros like `PAGE_SIZE`, `PAGE_MASK`, or hardcoded `4096` in C/C++ native source code; page sizes MUST be retrieved at runtime using `sysconf(_SC_PAGESIZE)` or `getpagesize()`.

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

Engineers MUST ALWAYS align native memory allocations mapped via `mmap()` or allocated with `posix_memalign()` to multiples of the dynamic runtime page size (`sysconf(_SC_PAGESIZE)`).

Engineers MUST ALWAYS align native memory allocations mapped via `mmap()` or allocated with `posix_memalign()` to multiples of the dynamic runtime page size (`sysconf(_SC_PAGESIZE)`).

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

Services MUST verify prebuilt 3rd-party vendor `.so` files using the Android NDK `check_elf_alignment.sh` script or `llvm-objdump -p lib.so | grep LOAD` during CI pipelines.

Services MUST verify prebuilt 3rd-party vendor `.so` files using the Android NDK `check_elf_alignment.sh` script or `llvm-objdump -p lib.so | grep LOAD` during CI pipelines.

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

Components NEVER bundle unaligned native binaries inside APKs

Components NEVER bundle unaligned native binaries inside APKs; Android Gradle Plugin (AGP) version 8.3+ or explicit `zipalign -p 16384` MUST be enforced across all APK packaging artifacts.

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 arenas and ring buffers MUST dynamically round buffer sizes and offsets up using bitwise alignment formulas

Custom memory arenas and ring buffers MUST dynamically round buffer sizes and offsets up using bitwise alignment formulas: `(size + pageSize - 1) & ~(pageSize - 1)`.

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 16KB ELF & Memory Page Boundary Alignment Invariant critical for AI coding assistants?

Android 15 devices support 16KB physical memory page sizes to boost performance on modern ARM64 silicon. Shared libraries (.so) compiled with legacy 4KB ELF segment boundaries trigger fatal runtime dlopen errors or SIGBUS/SIGSEGV memory aborts. All NDK toolchains, linker scripts, packaging pipelines, and native memory management routines must explicitly guarantee 16KB page alignment. 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 (Hardcoded 4096-Byte Page Constants and Legacy 4KB ELF Segment Alignment in Native NDK Binaries) 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