Skip to content

Kit and bindings: fixes from a full AddressSanitizer run - #57

Merged
ppmpreetham merged 4 commits into
mainfrom
fix/kit-checks
Nov 22, 2026
Merged

ppmpreetham merged 4 commits into
mainfrom
fix/kit-checks

Conversation

@ppmpreetham

@ppmpreetham ppmpreetham commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

What does this change?

Boundary checks in the C kit, and what a full AddressSanitizer + LeakSanitizer run over the whole project found.

Kit (kit/):

  • BK_node_add refuses to put a node under its own descendant. That made the tree a cycle, which every walk (layout, drawing, free) followed forever. It returns 0 now, as it already did for a node added to itself.
  • BK_menubutton_set_items only acts on "menubutton" nodes. On another node it used n->ext as menu data: it overwrote a list's or canvas's own data, and leaked on a node with none. Every caller (Python components, Qt, tkinter, Lua) passes menubuttons, so none of them change.
  • No stale raster after a render (use-after-free). BK_render left its raster in the kit's global draw state. A host that freed it and then called BK_draw_widget set a clip on freed memory; Lua's test hit this. The draw state now names a raster only while it is drawn into. BUI_kit.h documents that the context keeps its last frame for the eyedropper.

Bindings:

  • Lua: ctx:render keeps one raster per context, freed with it, instead of a temporary per call. The eyedropper read the freed one, and an incremental render returned only the changes on a blank frame. test_widgets.lua's canvas check passed only because of that blank frame (its canvas was never packed, so it was 0×0); it now packs the canvas and tests what it says.
  • Python: the Dear ImGui module keeps its types' names, slots and getset tables in process-lifetime storage instead of dropped allocations.

Tests:

  • anim_test: kills a timeline that kept playing on a dead stack object (a stack-buffer-overflow in the next test).
  • ime_test: frees its tree.
  • Rust tests/table.rs: frees the clipboard string it read.

Docs and the changelog cover the user-visible parts.

How was it tested?

AddressSanitizer + LeakSanitizer on Linux (WSL). System libraries' own leaks are suppressed (Mesa/EGL, CUDA, GStreamer and its plugins, fontconfig); the unsuppressed first run showed none of them have BUI frames.

Part Result
The whole CMake project (kit, GPU, core, Dear ImGui, Python and Lua bindings) via ctest 41 of 43 pass
cargo test -p blenderui -p blenderui-imgui, the kit built with clang's ASan 201 tests pass, doctests included
Go binding (feat/go-bindings, go test -asan, the kit with ASan) Passes, no reports
Ruby (#58), ASan + leaks, native windows 31 tests pass, no BUI leaks

The two ctest failures:

  • bui_video_test / buipy_video: wall-clock timing checks fail under the load of a parallel ASan run; the test passes 5/5 alone, memory 0/0.
  • A ~49 KB leak inside a GStreamer plugin that is unloaded before exit, so ASan names a different library each run; no BUI frames.

bui_video_test also failed once on main without these changes (32 guarded-alloc blocks left after video) and has not reproduced since.

Checklist

  • CHANGELOG.md updated (Unreleased, Fixed)
  • Docs updated (BUI_kit.h, docs/c-api.md, the website's C API page)
  • No new files

Summary by CodeRabbit

  • Bug Fixes
    • Adding a node to itself or one of its descendants now returns 0 without changing the node tree.
    • Setting menu items on a node that is not a menu button now leaves it unchanged.
    • Rendering now restores the previous drawing state after each frame.
  • New Features
    • The most recently rendered image remains available for eyedropper use until another raster is rendered in the context or the context is destroyed.
    • Lua contexts reuse their rendered image across calls and report its actual dimensions.
  • Documentation
    • Clarified that adding a child already in a node tree moves it to the new parent, and that an index of -1 appends it.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

The changes add guards for node insertion and menu-button updates, adjust raster handling in core and Lua rendering, retain generated Python type metadata, and add resource cleanup in tests. API comments, documentation, and the changelog describe these changes.

Changes: Kit API behavior

Layer / File(s) Summary
Node insertion guard and documentation
kit/include/BUI_kit.h, kit/intern/kit_core.c, website/src/content/docs/c/c-api.mdx, docs/c-api.md, CHANGELOG.md
BK_node_add rejects additions that would create a cycle. The header and website documentation describe moving a child with an existing parent and appending at index -1. The Nodes row in docs/c-api.md has no observable content change.
Menu-button item guard
kit/include/BUI_kit.h, kit/intern/kit_widgets.c, CHANGELOG.md
BK_menubutton_set_items returns without changing items when the node is null or is not a menu button. The header comment and changelog describe this behavior.
Render raster state and lifetime
kit/include/BUI_kit.h, kit/intern/kit_core.c, CHANGELOG.md
BK_render restores the prior draw raster after rendering. The header and changelog describe how long the context retains its last frame.

Lua context rendering

Layer / File(s) Summary
Context-owned raster rendering and cleanup
bindings/lua/src/bui_internal.h, bindings/lua/src/bui_lua.c, bindings/lua/src/bui_context.c, bindings/lua/tests/test_widgets.lua, CHANGELOG.md
Lua contexts initialize and retain a raster for ctx:render. Rendering returns the raster’s actual dimensions and pixel data. Explicit free and garbage collection use shared context and raster cleanup. The canvas test assigns packed geometry.

Python type metadata

Layer / File(s) Summary
Generated type metadata storage
bindings/python/src/buiimgui_rt.h, CHANGELOG.md
bi_make_type stores generated type names, get/set entries, slots, and type specifications in a static deque.

Test resource cleanup

Layer / File(s) Summary
Test resource release
tests/anim_test.c, tests/ime_test.c, bindings/rust/blenderui/tests/table.rs
The animation test kills its timeline before release. The IME test frees its root node. The Rust clipboard test frees the C-allocated string after copying it.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant LuaContext
  participant Raster
  participant BK_render
  LuaContext->>Raster: create once and retain
  LuaContext->>BK_render: render frame into raster
  BK_render-->>LuaContext: return rendered-region count
  LuaContext-->>LuaContext: return raster pixels and dimensions
Loading

Suggested reviewers: domain, concprog

Merge Risk: Low · up to c0ad0

The changes harden node insertion, menu-button updates and raster lifetime. A rare nested render from a configure handler could leave the eyedropper sampling an undrawn frame. The risk is low and mostly relates to documented behavior, so it is mergeable with owner awareness.

Security architecture risk: Minimal · up to c0ad0

The inspected changes tighten native API checks and align frame ownership with rendering and cleanup. No introduced or worsened risk materially affecting security, failure containment, or ownership was established. Native callers must still keep the last-rendered raster alive.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected exposure is native memory and frame data within the embedding process. Lua-controlled dimensions reach rendering, and returned pixels are copied into a Lua string. No new privilege transition or cross-service reachability was identified in this path.

Trust Boundaries and Controls

  • observed — Native hosts remain responsible for the borrowed frame's lifetime; the eyedropper's coordinate checks do not validate object lifetime. Lua now enforces the required lifetime through context-owned storage, while exposing copied pixels rather than the internal raster pointer.

Resilience and Maintainability Implications

  • observed — The no-damage return still passes through the public wrapper's draw-raster restoration. Lua retries raster creation when its pointer remains null, and explicit release and garbage collection share cleanup that clears the context and raster pointers after freeing them.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names a real part of the changeset: it says the PR contains fixes from an AddressSanitizer run in the kit and bindings. The changes cover BK_node_add cycle rejection, BK_menubutton_set_items…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names a real part of the changeset: it says the PR contains fixes from an AddressSanitizer run in the kit and bindings. The changes cover BK_node_add cycle rejection, BK_menubutton_set_items guarding, BK_render raster lifetime, Lua ctx:render raster ownership, Python type storage, and test cleanup. The title does not name a single primary change, and 'fixes from a full AddressSanitizer run' describes the process more than the code changes. It is still on topic and specific enough for a teammate to see the scope. The title is therefore partially related and passes.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@ppmpreetham ppmpreetham mentioned this pull request Nov 22, 2026
9 of 10 tasks
@ppmpreetham ppmpreetham changed the title Kit: refuse tree cycles and menus on non-menu nodes Kit and bindings: fixes from a full AddressSanitizer run Nov 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Additional comments: 1

kit/intern/kit_core.c-2304-2304: 1

2304-2304: Minor · Functional Correctness · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Do not retain an undrawn outer raster.

If a configure handler renders the same context into B, that nested render can clear the dirty state. The outer partial render into A can then return without drawing A, but this assignment retains A. The eyedropper reads A instead of the frame drawn into B. Ensure the outer render redraws A when a nested render consumes its damage.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 43e5db51-efc3-475a-8be2-233ac7e6c4d7
Commits

Reviewing files that changed from the base of the PR and between 32cb3f2 and c0ad04e.

Files selected for processing: 1

  • kit/intern/kit_core.c

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@ppmpreetham
ppmpreetham merged commit aed3b88 into main Nov 22, 2026
24 checks passed
@ppmpreetham
ppmpreetham deleted the fix/kit-checks branch November 22, 2026 16:46
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