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-guardrail-android-15-16kb--586

Android 15 16KB Memory Page Size Alignment Guardrail

Android 15 introduces support for 16KB physical memory page sizes alongside legacy 4KB pages. Native C/C++ libraries compiled with 4KB ELF segment alignment fail during dynamic loading (dlopen) and runtime memory mapping (mmap), causing fatal SIGSEGV crashes on 16KB kernel configurations. This guardrail enforces 16KB linker alignment across all CMake/NDK build scripts and validates third-party prebuilt shared libraries.

Prohibited Anti-Pattern: Compiling NDK Binaries or Bundling Vendor .so Files with Legacy 4KB ELF Segment Alignment

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Android 15 16KB Memory Page Size Alignment Guardrail - Android 15 introduces support for 16KB physical memory page sizes alongside legacy 4KB pages. Native C/C++ libraries compiled with 4KB ELF segment alignment fail during dynamic loading (dlopen) and runtime memory mapping (mmap), causing fatal SIGSEGV crashes on 16KB kernel configurations. This guardrail enforces 16KB linker alignment across all CMake/NDK build scripts and validates third-party prebuilt shared libraries.
globs: **/CMakeLists.txt, **/Android.mk, **/build.gradle, **/build.gradle.kts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/android-15-16kb-page-alignment-guardrail-android-15-16kb--586"
---

# Android 15 16KB Memory Page Size Alignment Guardrail

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Compiling NDK Binaries or Bundling Vendor .so Files with Legacy 4KB ELF Segment Alignment

## System Constraints
You are a Principal Android Systems Architect enforcing strict 16KB page-alignment compatibility for Android 15 (API 35+) across all native NDK toolchains and prebuilt libraries. Mandate explicit ELF linker alignment flags, eliminate hardcoded 4096-byte PAGE_SIZE assumptions in C/C++ sources, and ensure Gradle packaging guarantees 16KB uncompressed alignment for native shared objects.

## Strict Guidelines
- Engineers MUST configure target linker flags with '-Wl,-z,max-page-size=16384' in CMakeLists.txt, Android.mk, or Gradle externalNativeBuild configurations.
- Engineers MUST NOT hardcode 'PAGE_SIZE', '4096', or '12' (page shift) in C/C++ sources; runtime page size MUST be retrieved using 'sysconf(_SC_PAGESIZE)' or 'getpagesize()'.
- Engineers MUST audit all prebuilt third-party shared libraries using llvm-readelf or readelf to verify all LOAD segments have an alignment (Align) of at least 0x4000 (16384 bytes).
- Engineers MUST configure Android Gradle Plugin (AGP) 8.3.0+ or 8.5.1+ to ensure APK zip alignment preserves 16KB boundary alignment for uncompressed native libraries.
- Engineers MUST replace or recompile legacy third-party binary dependencies that fail 16KB alignment checks before merging to production branches.

## Bad vs Good Implementations
### CMake Native Linker Configuration for 16KB Alignment

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Omitting explicit ELF max-page-size linker flags causes Android NDK toolchains prior to r27 to default to 4KB page alignment, producing native shared objects that crash with dlopen/SIGSEGV errors on Android 15 devices running 16KB kernels.
```cmake
cmake_minimum_required(VERSION 3.22.1)
project("native_engine")

add_library(native_engine SHARED native_engine.cpp)
# Flaw: Omits explicit 16KB ELF alignment, defaulting NDK r26 and earlier to 4KB alignment
target_link_libraries(native_engine log)
```

#### Production Standard (Correct)
> **Standard**: Explicitly passing '-Wl,-z,max-page-size=16384' ensures the ELF program headers configure all PT_LOAD segments with an alignment of at least 0x4000 bytes, guaranteeing compatibility on both 4KB and 16KB Android kernels.
```cmake
cmake_minimum_required(VERSION 3.22.1)
project("native_engine")

add_library(native_engine SHARED native_engine.cpp)

# Production Guardrail: Explicitly enforce 16KB ELF segment alignment across NDK builds
target_link_options(native_engine PRIVATE "-Wl,-z,max-page-size=16384")
target_link_libraries(native_engine log)
```

### Gradle Build Configuration and Dynamic Runtime Memory Mapping

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Gradle does not pass linker arguments to external builds, and the C++ code hardcodes 4096-byte PAGE_SIZE assumptions, corrupting mmap calculations and breaking pointer alignment on 16KB pages.
```kotlin
// build.gradle.kts
android {
    defaultConfig {
        ndk {
            abiFilters.addAll(listOf("arm64-v8a", "x86_64"))
        }
    }
}

// native_engine.cpp (anti-pattern)
// #define PAGE_SIZE 4096
// void* buffer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
// uintptr_t aligned = ((uintptr_t)buffer + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
```

#### Production Standard (Correct)
> **Standard**: Configures Gradle CMake arguments for flexible page sizes, sets max-page-size to 16384, enables modern uncompressed aligned APK packaging, and uses sysconf(_SC_PAGESIZE) for dynamic memory alignment.
```kotlin
// build.gradle.kts
android {
    defaultConfig {
        ndk {
            abiFilters.addAll(listOf("arm64-v8a", "x86_64"))
        }
        externalNativeBuild {
            cmake {
                arguments += "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
                cppFlags += "-Wl,-z,max-page-size=16384"
            }
        }
    }
    packaging {
        jniLibs {
            useLegacyPackaging = false
        }
    }
}

// native_engine.cpp (fix)
// long pageSize = sysconf(_SC_PAGESIZE);
// uintptr_t aligned = ((uintptr_t)buffer + (pageSize - 1)) & ~(pageSize - 1);
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Android 15 16KB Memory Page Size Alignment Guardrail

Android 15 16KB Memory Page Size Alignment Guardrail defines an industrial-grade behavioral standard for mobile engineering. Android 15 introduces support for 16KB physical memory page sizes alongside legacy 4KB pages. Native C/C++ libraries compiled with 4KB ELF segment alignment fail during dynamic loading (dlopen) and runtime memory mapping (mmap), causing fatal SIGSEGV crashes on 16KB kernel configurations. This guardrail enforces 16KB linker alignment across all CMake/NDK build scripts and validates third-party prebuilt shared libraries. 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 or Bundling Vendor .so Files with Legacy 4KB ELF Segment Alignment 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 or Bundling Vendor .so Files with Legacy 4KB ELF Segment AlignmentCritical
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 configure target linker flags with '-Wl,-z,max-page-size=16384' in CMakeLists.txt, Android.mk, or Gradle externalNativeBuild configurations.

Engineers MUST configure target linker flags with '-Wl,-z,max-page-size=16384' in CMakeLists.txt, Android.mk, or Gradle externalNativeBuild configurations.

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 hardcode 'PAGE_SIZE', '4096', or '12' (page shift) in C/C++ sources

Engineers MUST NOT hardcode 'PAGE_SIZE', '4096', or '12' (page shift) in C/C++ sources; runtime page size MUST be retrieved 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 audit all prebuilt third-party shared libraries using llvm-readelf or readelf to verify all LOAD segments have an alignment (Align) of at least 0x4000 (16384 bytes).

Engineers MUST audit all prebuilt third-party shared libraries using llvm-readelf or readelf to verify all LOAD segments have an alignment (Align) of at least 0x4000 (16384 bytes).

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

Engineers MUST configure Android Gradle Plugin (AGP) 8.3.0+ or 8.5.1+ to ensure APK zip alignment preserves 16KB boundary alignment for uncompressed native libraries.

Engineers MUST configure Android Gradle Plugin (AGP) 8.3.0+ or 8.5.1+ to ensure APK zip alignment preserves 16KB boundary alignment for uncompressed native libraries.

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 replace or recompile legacy third-party binary dependencies that fail 16KB alignment checks before merging to production branches.

Engineers MUST replace or recompile legacy third-party binary dependencies that fail 16KB alignment checks before merging to production branches.

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 Memory Page Size Alignment Guardrail critical for AI coding assistants?

Android 15 introduces support for 16KB physical memory page sizes alongside legacy 4KB pages. Native C/C++ libraries compiled with 4KB ELF segment alignment fail during dynamic loading (dlopen) and runtime memory mapping (mmap), causing fatal SIGSEGV crashes on 16KB kernel configurations. This guardrail enforces 16KB linker alignment across all CMake/NDK build scripts and validates third-party prebuilt shared libraries. 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 or Bundling Vendor .so Files with Legacy 4KB ELF Segment Alignment) 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