Skip to content

Add heap_diag_format_triage_line for heap integrity reporting#10

Open
Zaid-maker wants to merge 2 commits intomainfrom
improve-coverage
Open

Add heap_diag_format_triage_line for heap integrity reporting#10
Zaid-maker wants to merge 2 commits intomainfrom
improve-coverage

Conversation

@Zaid-maker
Copy link
Copy Markdown
Owner

@Zaid-maker Zaid-maker commented Apr 1, 2026

Summary by CodeRabbit

  • New Features

    • Improved "Heap triage" diagnostic output: single-line status (OK/FAIL) plus detailed counters, formatted for clearer readability.
  • Tests

    • Added unit test asserting exact diagnostic output formatting.
    • Added runtime test covering zero-size allocation behavior.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

Extracted heap triage string formatting into a new public helper heap_diag_format_triage_line(...), replaced in-place formatting in shell_print_heap_triage() with that call, added a unit test for the formatter, a runtime test for kmalloc(0), and included sbuf in test link rules.

Changes

Cohort / File(s) Summary
Heap Diagnostics API
kernel/src/heap_diag.h, kernel/src/heap_diag.c
Added heap_diag_format_triage_line(char* buffer, uint32_t cap, const struct heap_integrity_report* report, int ok) and its implementation that builds a single "Heap triage" line into a caller buffer using sbuf_*, reading report counters with zero fallbacks.
Kernel Shell Use Site
kernel/src/kernel.c
Replaced manual sbuf_reset/sbuf_append_* formatting in shell_print_heap_triage() with a single call to the new heap_diag_format_triage_line() and print the resulting buffer.
Tests
kernel/tests/heap_diag_test.c, kernel/tests/heap_runtime_test.c
Added unit test asserting exact formatted triage output for a failure case; added runtime assertion that kmalloc(0u) returns null and integrated it into existing test ok checks.
Build/Test Makefile
kernel/Makefile
Test and coverage link steps updated to include sbuf object/source so the new formatter and sbuf helpers are linked into heap_diag_test builds.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 I nibble bytes and stitch a line so fine,
From many sbuf crumbs I shape a single sign.
OK or FAIL, the counters all parade,
One little function, a tidy diagnostic braid.
Hop, format, print — the triage is mine!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add heap_diag_format_triage_line for heap integrity reporting' accurately describes the main change: a new exported function is added to format heap triage information.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch improve-coverage

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
kernel/tests/heap_diag_test.c (1)

95-106: Consider adding coverage for remaining formatter branches.

This test is solid for the FAIL path. Add a small companion check for ok=1 and report=0 to lock in the fallback behavior introduced in heap_diag_format_triage_line.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kernel/tests/heap_diag_test.c` around lines 95 - 106, Add a small companion
test in heap_diag_test.c that constructs an empty struct heap_integrity_report
(all zero), calls heap_diag_format_triage_line(buffer, sizeof(buffer), &report,
1) and asserts the produced string matches the expected fallback (e.g. "Heap
triage: OK"); this exercises the ok=1/report=0 branch of
heap_diag_format_triage_line and locks in the fallback formatting behavior
introduced for that case.
kernel/src/kernel.c (1)

294-294: Include the declaring header for this call site (heap_diag.h).

The heap_diag_format_triage_line() function is declared in kernel/src/heap_diag.h but not included in this translation unit. Direct inclusion of the declaring header ensures signature mismatches are caught at compile time.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kernel/src/kernel.c` at line 294, Missing declaration include: add `#include`
"heap_diag.h" to the top of the translation unit so the call to
heap_diag_format_triage_line(...) is checked against its prototype; locate the
use of heap_diag_format_triage_line in kernel.c and insert the heap_diag.h
include alongside the other kernel headers to ensure signature mismatches are
caught at compile time.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@kernel/src/heap_diag.c`:
- Around line 146-149: The function heap_diag_format_triage_line currently calls
sbuf_reset(buffer) and sbuf_append_str without checking buffer or cap; guard the
start of heap_diag_format_triage_line by checking if buffer is NULL or cap is 0
and return early (or set a no-op behavior) to avoid dereferencing a
null/zero-length buffer, then only call sbuf_reset, sbuf_append_str and use len
when buffer and cap are valid; reference heap_diag_format_triage_line,
sbuf_reset, sbuf_append_str, buffer, cap and len to locate the change.
- Around line 146-161: The heap_diag.c test targets are missing the sbuf
implementation, causing undefined references to sbuf_reset, sbuf_append_str, and
sbuf_append_dec_u32; fix by updating the heap_runtime_test and
heap_integrity_test build targets to also compile/link src/sbuf.c alongside
heap_diag.c (mirror how the main kernel target includes both heap_diag.c and
sbuf.c) so that the sbuf symbols are provided to the tests.

---

Nitpick comments:
In `@kernel/src/kernel.c`:
- Line 294: Missing declaration include: add `#include` "heap_diag.h" to the top
of the translation unit so the call to heap_diag_format_triage_line(...) is
checked against its prototype; locate the use of heap_diag_format_triage_line in
kernel.c and insert the heap_diag.h include alongside the other kernel headers
to ensure signature mismatches are caught at compile time.

In `@kernel/tests/heap_diag_test.c`:
- Around line 95-106: Add a small companion test in heap_diag_test.c that
constructs an empty struct heap_integrity_report (all zero), calls
heap_diag_format_triage_line(buffer, sizeof(buffer), &report, 1) and asserts the
produced string matches the expected fallback (e.g. "Heap triage: OK"); this
exercises the ok=1/report=0 branch of heap_diag_format_triage_line and locks in
the fallback formatting behavior introduced for that case.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 09e14b55-2e93-462b-876a-1296adfe596a

📥 Commits

Reviewing files that changed from the base of the PR and between 80a9f16 and 5e5f2f0.

📒 Files selected for processing (5)
  • kernel/src/heap_diag.c
  • kernel/src/heap_diag.h
  • kernel/src/kernel.c
  • kernel/tests/heap_diag_test.c
  • kernel/tests/heap_runtime_test.c

Comment on lines +146 to +149
void heap_diag_format_triage_line(char* buffer, uint32_t cap, const struct heap_integrity_report* report, int ok) {
uint32_t len = 0u;
sbuf_reset(buffer);
sbuf_append_str(buffer, cap, &len, "Heap triage: ");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Guard buffer/cap before first write.

heap_diag_format_triage_line calls sbuf_reset(buffer) immediately, which dereferences buffer even when buffer == 0 or cap == 0.

Proposed fix
 void heap_diag_format_triage_line(char* buffer, uint32_t cap, const struct heap_integrity_report* report, int ok) {
+    if (buffer == 0 || cap == 0u) {
+        return;
+    }
+
     uint32_t len = 0u;
     sbuf_reset(buffer);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void heap_diag_format_triage_line(char* buffer, uint32_t cap, const struct heap_integrity_report* report, int ok) {
uint32_t len = 0u;
sbuf_reset(buffer);
sbuf_append_str(buffer, cap, &len, "Heap triage: ");
void heap_diag_format_triage_line(char* buffer, uint32_t cap, const struct heap_integrity_report* report, int ok) {
if (buffer == 0 || cap == 0u) {
return;
}
uint32_t len = 0u;
sbuf_reset(buffer);
sbuf_append_str(buffer, cap, &len, "Heap triage: ");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kernel/src/heap_diag.c` around lines 146 - 149, The function
heap_diag_format_triage_line currently calls sbuf_reset(buffer) and
sbuf_append_str without checking buffer or cap; guard the start of
heap_diag_format_triage_line by checking if buffer is NULL or cap is 0 and
return early (or set a no-op behavior) to avoid dereferencing a null/zero-length
buffer, then only call sbuf_reset, sbuf_append_str and use len when buffer and
cap are valid; reference heap_diag_format_triage_line, sbuf_reset,
sbuf_append_str, buffer, cap and len to locate the change.

Comment on lines +146 to +161
void heap_diag_format_triage_line(char* buffer, uint32_t cap, const struct heap_integrity_report* report, int ok) {
uint32_t len = 0u;
sbuf_reset(buffer);
sbuf_append_str(buffer, cap, &len, "Heap triage: ");
sbuf_append_str(buffer, cap, &len, ok ? "OK" : "FAIL");
sbuf_append_str(buffer, cap, &len, " scanned=");
sbuf_append_dec_u32(buffer, cap, &len, report != 0 ? report->blocks_scanned : 0u);
sbuf_append_str(buffer, cap, &len, " bad=");
sbuf_append_dec_u32(buffer, cap, &len, report != 0 ? report->corrupted_headers : 0u);
sbuf_append_str(buffer, cap, &len, " align=");
sbuf_append_dec_u32(buffer, cap, &len, report != 0 ? report->split_alignment_issues : 0u);
sbuf_append_str(buffer, cap, &len, " unmerged=");
sbuf_append_dec_u32(buffer, cap, &len, report != 0 ? report->adjacent_unmerged_free_pairs : 0u);
sbuf_append_str(buffer, cap, &len, " next=");
sbuf_append_dec_u32(buffer, cap, &len, report != 0 ? report->next_pointer_regressions : 0u);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Build manifests =="
fd -HI 'Makefile|makefile|CMakeLists.txt|meson.build|BUILD|BUILD.bazel|*.mk' .

echo
echo "== References to heap_diag.c / sbuf.c in build files =="
rg -n --iglob '*Makefile*' --iglob '*.mk' --iglob 'CMakeLists.txt' --iglob 'meson.build' --iglob 'BUILD*' 'heap_diag\.c|sbuf\.c'

echo
echo "== Source ownership sanity =="
fd -HI '^heap_diag\.c$|^sbuf\.c$' kernel/src

Repository: Zaid-maker/kernel

Length of output: 509


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Finding build configuration files =="
find . -type f \( -name "Makefile" -o -name "CMakeLists.txt" -o -name "meson.build" -o -name "BUILD" -o -name "*.mk" \) | head -20

echo
echo "== Check if sbuf.c and heap_diag.c exist =="
ls -la kernel/src/sbuf.c kernel/src/heap_diag.c 2>&1 || echo "Files not found"

echo
echo "== Search for heap_diag references in build files =="
find . -type f \( -name "Makefile" -o -name "CMakeLists.txt" -o -name "meson.build" -o -name "BUILD" -o -name "*.mk" \) -exec grep -l "heap_diag" {} \;

echo
echo "== Search for sbuf references in build files =="
find . -type f \( -name "Makefile" -o -name "CMakeLists.txt" -o -name "meson.build" -o -name "BUILD" -o -name "*.mk" \) -exec grep -l "sbuf" {} \;

echo
echo "== Check Makefile content if exists =="
if [ -f "Makefile" ]; then cat -n Makefile | head -50; fi

echo
echo "== Check kernel/src directory structure =="
ls -la kernel/src/ | head -30

Repository: Zaid-maker/kernel

Length of output: 2066


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Full kernel/Makefile content =="
cat -n kernel/Makefile

echo
echo "== Check heap_diag.c includes =="
head -20 kernel/src/heap_diag.c

echo
echo "== Check sbuf.h header =="
cat -n kernel/src/sbuf.h

Repository: Zaid-maker/kernel

Length of output: 7938


Fix test targets missing sbuf.c dependency.

The main kernel build (line 24) correctly includes both heap_diag.c and sbuf.c. However, test targets heap_runtime_test (line 70) and heap_integrity_test (line 71) compile heap_diag.c without linking sbuf.c, causing undefined reference errors for sbuf_reset, sbuf_append_str, and sbuf_append_dec_u32. Add src/sbuf.c to these two test targets.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kernel/src/heap_diag.c` around lines 146 - 161, The heap_diag.c test targets
are missing the sbuf implementation, causing undefined references to sbuf_reset,
sbuf_append_str, and sbuf_append_dec_u32; fix by updating the heap_runtime_test
and heap_integrity_test build targets to also compile/link src/sbuf.c alongside
heap_diag.c (mirror how the main kernel target includes both heap_diag.c and
sbuf.c) so that the sbuf symbols are provided to the tests.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
kernel/Makefile (2)

64-71: ⚠️ Potential issue | 🔴 Critical

Propagate src/sbuf.c to all test link commands that include src/heap_diag.c.

Good fix on Line 69, but Line 70 and Line 71 still link src/heap_diag.c without src/sbuf.c, which explains the linker failure reported at Line 70 in CI.

Proposed fix
-	$(HOST_CC) $(HOST_CFLAGS) tests/heap_runtime_test.c src/heap.c src/heap_diag.c tests/pmm_stub.c -o $(BUILD_DIR)/heap_runtime_test
-	$(HOST_CC) $(HOST_CFLAGS) -DHEAP_ENABLE_TEST_HOOKS tests/heap_integrity_test.c src/heap.c src/heap_diag.c tests/pmm_stub.c -o $(BUILD_DIR)/heap_integrity_test
+	$(HOST_CC) $(HOST_CFLAGS) tests/heap_runtime_test.c src/heap.c src/heap_diag.c src/sbuf.c tests/pmm_stub.c -o $(BUILD_DIR)/heap_runtime_test
+	$(HOST_CC) $(HOST_CFLAGS) -DHEAP_ENABLE_TEST_HOOKS tests/heap_integrity_test.c src/heap.c src/heap_diag.c src/sbuf.c tests/pmm_stub.c -o $(BUILD_DIR)/heap_integrity_test
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kernel/Makefile` around lines 64 - 71, The test link lines for
heap_runtime_test and heap_integrity_test include src/heap_diag.c but are
missing src/sbuf.c, causing unresolved symbols; update the Makefile test recipe
so the commands that build heap_runtime_test and heap_integrity_test also link
src/sbuf.c (preserving existing flags like -DHEAP_ENABLE_TEST_HOOKS for
heap_integrity_test) so both targets include src/heap.c, src/heap_diag.c and
src/sbuf.c during linking.

90-100: ⚠️ Potential issue | 🔴 Critical

Coverage link steps are still missing $(BUILD_DIR)/sbuf_cov.o for heap targets.

Line 98 is correct, but Line 99 and Line 100 also link heap_diag_cov.o and must include sbuf_cov.o; this matches the CI linker failure at Line 99.

Proposed fix
-	$(HOST_CC) $(HOST_COVERAGE_CFLAGS) $(BUILD_DIR)/heap_runtime_test_cov.o $(BUILD_DIR)/heap_cov.o $(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/pmm_stub_cov.o -o $(BUILD_DIR)/heap_runtime_test_cov
-	$(HOST_CC) $(HOST_COVERAGE_CFLAGS) $(BUILD_DIR)/heap_integrity_test_cov.o $(BUILD_DIR)/heap_cov.o $(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/pmm_stub_cov.o -o $(BUILD_DIR)/heap_integrity_test_cov
+	$(HOST_CC) $(HOST_COVERAGE_CFLAGS) $(BUILD_DIR)/heap_runtime_test_cov.o $(BUILD_DIR)/heap_cov.o $(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/sbuf_cov.o $(BUILD_DIR)/pmm_stub_cov.o -o $(BUILD_DIR)/heap_runtime_test_cov
+	$(HOST_CC) $(HOST_COVERAGE_CFLAGS) $(BUILD_DIR)/heap_integrity_test_cov.o $(BUILD_DIR)/heap_cov.o $(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/sbuf_cov.o $(BUILD_DIR)/pmm_stub_cov.o -o $(BUILD_DIR)/heap_integrity_test_cov
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kernel/Makefile` around lines 90 - 100, The two coverage link rules for the
heap tests (targets producing $(BUILD_DIR)/heap_runtime_test_cov and
$(BUILD_DIR)/heap_integrity_test_cov) are missing the sbuf coverage object;
update the link lines that currently list $(BUILD_DIR)/heap_cov.o
$(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/pmm_stub_cov.o to also include
$(BUILD_DIR)/sbuf_cov.o so the final link inputs are $(BUILD_DIR)/heap_cov.o
$(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/pmm_stub_cov.o $(BUILD_DIR)/sbuf_cov.o
for both the heap_runtime_test_cov and heap_integrity_test_cov targets.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@kernel/Makefile`:
- Around line 64-71: The test link lines for heap_runtime_test and
heap_integrity_test include src/heap_diag.c but are missing src/sbuf.c, causing
unresolved symbols; update the Makefile test recipe so the commands that build
heap_runtime_test and heap_integrity_test also link src/sbuf.c (preserving
existing flags like -DHEAP_ENABLE_TEST_HOOKS for heap_integrity_test) so both
targets include src/heap.c, src/heap_diag.c and src/sbuf.c during linking.
- Around line 90-100: The two coverage link rules for the heap tests (targets
producing $(BUILD_DIR)/heap_runtime_test_cov and
$(BUILD_DIR)/heap_integrity_test_cov) are missing the sbuf coverage object;
update the link lines that currently list $(BUILD_DIR)/heap_cov.o
$(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/pmm_stub_cov.o to also include
$(BUILD_DIR)/sbuf_cov.o so the final link inputs are $(BUILD_DIR)/heap_cov.o
$(BUILD_DIR)/heap_diag_cov.o $(BUILD_DIR)/pmm_stub_cov.o $(BUILD_DIR)/sbuf_cov.o
for both the heap_runtime_test_cov and heap_integrity_test_cov targets.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ef413c68-c58b-4d36-ae5e-6a6cdab82f79

📥 Commits

Reviewing files that changed from the base of the PR and between 5e5f2f0 and 954571d.

📒 Files selected for processing (1)
  • kernel/Makefile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant