Skip to content

Refactor: extract finalize_common() (stacked on #913)#917

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
hw-native-sys-bot:extract-finalize-common
May 30, 2026
Merged

Refactor: extract finalize_common() (stacked on #913)#917
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
hw-native-sys-bot:extract-finalize-common

Conversation

@hw-native-sys-bot
Copy link
Copy Markdown
Collaborator

Note: Stacked on top of #913. If #913 changes, this PR rebases. If #913 lands first, GitHub auto-rebases.

Summary

Moves the byte-identical body of finalize() from both onboard DeviceRunners into a shared DeviceRunnerBase::finalize_common(). Each arch's finalize() shrinks to a thin wrapper covering its arch-specific bits. Net -32 lines across the four touched files.

finalize_common() body (in base)

  • Destroy persistent AICPU/AICore streams (no pre-sync — rtStreamDestroy is the supported teardown path even on error-state streams).
  • kernel_args_.finalize_device_args() + reset aicore_bin_handle_ / binaries_loaded_.
  • Free + clear chip_callable_buffers_, orch_so_dedup_, callables_ (with hbg dlclose), aicpu_seen_callable_ids_, aicpu_dlopen_total_.
  • Release the three pooled arenas.
  • Free device_wall_dev_ptr_ before mem_alloc_.finalize().
  • mem_alloc_.finalize().
  • Reset block_dim_, worker_count_, aicore_kernel_binary_.

Arch-specific finalize() wrappers

  • a2a3: early return + attach → finalize_collectors() (4 shared + dep_gen) → finalize_common() → cached arena sizes reset → ACL-or-rt device reset → device_id_ reset.
  • a5: early return + attach → inline 4-collector finalize → finalize_common() → cached arena sizes reset → rtDeviceResetdevice_id_ reset.

One real behavior normalization (call out)

a2a3's device_wall_dev_ptr_ free order changes: it previously freed after mem_alloc_.finalize() and after the device reset, which routed the free through an already-finalized allocator on a torn-down device context — a latent no-op or UAF. It now matches a5's longstanding ordering (free before mem_alloc_.finalize()). The path was effectively dead before this PR (tests pass either way), but this fixes the ordering bug the refactor surfaced.

Verification

  • Both arches built clean (onboard + sim, both runtimes).
  • Local a2a3 onboard smoke (dummy_task, alternating_matmul_add, prepared_callable suite) — 8/8 passed in 15s.

Test plan

  • CI st-sim-a2a3 / st-sim-a5
  • CI st-onboard-a2a3 / st-onboard-a5
  • CI ut-a2a3 / ut-a5

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

Warning

Review limit reached

@hw-native-sys-bot, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 5 minutes and 45 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: df5e555b-6b7b-4703-8668-ac72602cae2b

📥 Commits

Reviewing files that changed from the base of the PR and between aa6ce64 and 41913e0.

📒 Files selected for processing (4)
  • src/a2a3/platform/onboard/host/device_runner.cpp
  • src/a5/platform/onboard/host/device_runner.cpp
  • src/common/platform/onboard/host/device_runner_base.cpp
  • src/common/platform/onboard/host/device_runner_base.h

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

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the DeviceRunner implementations for the a2a3 and a5 platforms by consolidating shared diagnostics collectors, enablement setters, and the common finalization routine into the DeviceRunnerBase class. This refactoring also resolves a latent use-after-free bug in a2a3 by ensuring the device_wall buffer is freed before the allocator is finalized. The review feedback correctly identifies that finalize_common() fails to capture and propagate error codes from stream destruction and kernel argument finalization as documented, and provides a code suggestion to address this issue.

Comment thread src/common/platform/onboard/host/device_runner_base.cpp Outdated
@hw-native-sys-bot hw-native-sys-bot force-pushed the extract-finalize-common branch from 12c94a1 to 41913e0 Compare May 30, 2026 09:39
…order

Move the byte-identical body of `finalize()` from a2a3 and a5 onboard
DeviceRunners into a shared `DeviceRunnerBase::finalize_common()`.
Each arch's `finalize()` shrinks to a thin wrapper handling only its
arch-specific bits (-32 net lines, identical behavior modulo one
ordering normalization noted below).

`finalize_common()` does the shared body:
- Destroy persistent AICPU/AICore streams (no pre-sync — supported
  teardown path even on error-state streams).
- `kernel_args_.finalize_device_args()` + `aicore_bin_handle_` /
  `binaries_loaded_` reset.
- Free + clear `chip_callable_buffers_`, `orch_so_dedup_`, `callables_`
  (with hbg dlclose), `aicpu_seen_callable_ids_`, `aicpu_dlopen_total_`.
- Release the three pooled arenas.
- Free `device_wall_dev_ptr_` BEFORE `mem_alloc_.finalize()`.
- `mem_alloc_.finalize()`.
- Reset `block_dim_`, `worker_count_`, `aicore_kernel_binary_`.

Each arch's `finalize()`:
- a2a3: early return + attach + `finalize_collectors()` (4 shared +
  dep_gen) + `finalize_common()` + cached arena sizes reset + ACL-or-rt
  device reset + `device_id_` reset.
- a5: early return + attach + inline 4-collector finalize +
  `finalize_common()` + cached arena sizes reset + `rtDeviceReset` +
  `device_id_` reset.

Normalization: device_wall_dev_ptr_ free order changes on a2a3 — it
previously freed AFTER `mem_alloc_.finalize()` AND after the device
reset, routing the free through an already-finalized allocator on a
torn-down device context (a latent no-op / UAF). It now matches a5's
ordering: before `mem_alloc_.finalize()`. No observable behavior
change in tests (the path was effectively dead), but this fixes a real
ordering bug the refactor surfaced.

Both arches built clean. a2a3 onboard smoke 8/8 in 15s.
@ChaoWao ChaoWao merged commit fbc5618 into hw-native-sys:main May 30, 2026
44 of 46 checks passed
@ChaoWao ChaoWao deleted the extract-finalize-common branch May 30, 2026 10:33
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.

2 participants