Skip to content

[bsp][bluetrum] validate AB32 ADC channels - #11962

Open
usualtreee wants to merge 1 commit into
RT-Thread:masterfrom
usualtreee:fix/ab32-adc-channel-validation
Open

usualtreee wants to merge 1 commit into
RT-Thread:masterfrom
usualtreee:fix/ab32-adc-channel-validation

Conversation

@usualtreee

Copy link
Copy Markdown
Contributor

Problem

The AB32 ADC driver maps the requested channel for the HAL conversion, but then uses the original unchecked channel to index the ADC data registers. Values outside 0 through 15 therefore select memory outside the sixteen-register window. The callback channel types also do not match struct rt_adc_ops.

Fixes #11948.

Changes

  • Match both ADC callback signatures to the public rt_adc_ops interface.
  • Return -RT_EINVAL for channels without a defined AB32 ADC mapping instead of silently falling back to channel 0.
  • Validate the mapping before starting a conversion or indexing the data registers.
  • Reject invalid channels in the enable callback without calling the HAL.

Valid channel mappings and the existing conversion polling behavior are unchanged.

Verification

  • A host test includes the real driver source, mocks only the HAL and the sixteen data registers, and uses the real public ADC interface header.
  • Before the fix, channel = 16 produced an AddressSanitizer global-buffer-overflow at the original register access.
  • After the fix, ASAN+UBSAN regression tests pass for all valid channels 0 through 15 and invalid channels -128, -3, -2, -1, 16, and 127.
  • Invalid conversions return -RT_EINVAL, do not call the HAL, and leave the output value unchanged. Invalid enable requests are also rejected without calling the HAL.
  • The modified driver compiles against the real RT-Thread and AB32 headers with incompatible callback pointer types treated as errors.
  • Cross-compilation with riscv64-unknown-elf-gcc 14.2.0 and the AB32 rv32imc/ilp32 flags produces a 32-bit RISC-V relocatable object for the driver.
  • git diff --check and changed-line checks with clang-format 22.1.8 pass locally.

Not tested on physical AB32 hardware. Only the driver object was cross-compiled; a complete firmware image was not built or linked.

@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix/ab32-adc-channel-validation
    Enter PR branch fix/ab32-adc-channel-validation in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix/ab32-adc-channel-validation 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/ab32-adc-channel-validation branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions Bot added the BSP label Oct 31, 2026

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RT-Thread AB32 ADC uses an unchecked channel as a register index

1 participant