---
name: pintos-page-allocator
description: Audits memory allocations to enforce the use of palloc_get_page(PAL_ZERO) over malloc, ensuring strict NULL checks.
license: MIT
metadata:
  version: "2.3.0"
  author: OpenCode
  priority: high
  category: auditing
---

# pintos-page-allocator

## Role

You are a Pintos kernel allocation auditor. You enforce AGENTS-compliant allocator selection and cleanup behavior across kernel code paths.

## Workflow

1. **Resolve Source Root**
   - Read `.env` and resolve `PINTOS_PATH`.
   - Default host source tree is `ZhangZimo1308280/src/`.
2. **Locate Allocation Sites**
   - Use `grep` in `ZhangZimo1308280/src/` for `malloc (`, `calloc (`, `realloc (`, `palloc_get_page (`, `palloc_get_multiple (`.
   - Exclude pure test files when requested scope is kernel runtime behavior.
3. **Classify by Allocation Granularity**
   - Page-granularity or page-table/frame related objects: prefer `palloc_*`.
   - Sub-page dynamic objects: `malloc/free` may be appropriate.
   - Align decisions with AGENTS memory rule: use `palloc_*` for page-sized allocations; `malloc/free` for sub-page allocations.
4. **Audit Error Paths**
   - Verify allocation failure handling (`NULL` checks where failure is expected).
   - Verify each allocation has matching release on every return/error path.
5. **Patch and Verify**
   - Apply minimal edits with `apply_patch`.
   - Compile impacted module, for example `make MODULE=userprog compile` or `make MODULE=vm compile`.

## Constraints

- Do not blanket-replace all `malloc/free` with `palloc_*`; choose allocator by granularity and subsystem usage.
- Do not modify `threads/malloc.c` allocator implementation unless explicitly requested.
- Preserve existing panic-vs-return semantics already established by local code, unless user asks for policy changes.
- Include required headers when introducing `palloc_*` (`threads/palloc.h`).
- If allocation intent is unclear, mark `[MANUAL REVIEW NEEDED]` rather than guessing.

## Project Conventions

- Memory policy from AGENTS:
  - `palloc_*` for page-granularity allocations.
  - `PAL_ZERO` when zeroed page memory is expected.
  - `malloc/free` for sub-page allocations where appropriate.
- Validate allocator return values on failure-capable code paths.
- Clean up acquired memory/resources on all exits.

## Examples

### Example 1: Audit `process.c` page allocations

**User:** "Audit page allocator usage in process setup"

**Assistant Plan:**
1. Read `.env`.
2. Search `ZhangZimo1308280/src/userprog/process.c` for `palloc_get_page (` and `malloc (`.
3. Confirm `kpage`/page-table allocations use `palloc_*` and are cleaned on failure.
4. Compile with `make MODULE=userprog compile`.

**Representative tool calls:**
- `read` on `.env`
- `grep` pattern `palloc_get_page\s*\(|malloc\s*\(` in `ZhangZimo1308280/src/userprog/process.c`
- `read` on `ZhangZimo1308280/src/userprog/process.c`
- `apply_patch` when needed
- `bash` command `make MODULE=userprog compile`
