7
31
Fork
You've already forked blackmagic
23

Use of alloca in gdb m-packet handling can be optimized out #2042

Closed
opened 2025年01月06日 13:05:21 +01:00 by perigoso · 2 comments
perigoso commented 2025年01月06日 13:05:21 +01:00 (Migrated from github.com)
Copy link

Looking at #2041 I noticed there is a alloca use later in the affected code (Line 153), and it can be optimized out by reusing the packet buffer.

Since we check previously that the requested memory will fit in the packet buffer after encoding we can use the packet buffer itself for the read safely. Care must be taken because later building the packet could possibly corrupt the read data (because each hex encoded byte will take 2 bytes in the buffer), but we can work around this by only using the upper half of the buffer, which should allow the encoding in place to be done safely.

Looking at #2041 I noticed there is a `alloca` use later in the affected code ([Line 153](https://github.com/ALTracer/blackmagic/blob/9c60b339dad849e8d554579a0c3141f9ed9c1ca0/src/gdb_main.c#L153)), and it can be optimized out by reusing the packet buffer. Since we check previously that the requested memory will fit in the packet buffer after encoding we can use the packet buffer itself for the read safely. Care must be taken because later building the packet could possibly corrupt the read data (because each hex encoded byte will take 2 bytes in the buffer), but we can work around this by only using the upper half of the buffer, which should allow the encoding in place to be done safely.
ALTracer commented 2025年01月06日 16:36:29 +01:00 (Migrated from github.com)
Copy link

If there was a secondary half-sized buffer for raw bytes, then I see how it could be handled easily. But alloca/VLA does not result in on-heap fragmentation, and I don't have BMF telemetry to confirm stack usage at every alloca callsite -- 4096 seemed good enough.

That said, I only aimed to restore functionality before the impending freeze for release-candidate, so I didn't look deeper in this (entire m-packet handler or adjacent similar handlers). If you can fully get rid of these stack allocations so that e.g. I could bump GDB packet buffer (compile-time setting) from 1024 to 4096 without blowing past stlinkv3 stack size (which is a USB HS device and benefits from 512 byte bulk packets) then I'd be happy to test it.

But then again, what are the usecases of Long block reads (and writes)? I can think of some, like performing RAM and Flash dumps, extracting ETF/MTB contents, scanning memory otherwise, and inspecting large (>512 byte) in-DUT arrays. This is usually automated with help of Eclipse/VSCode/etc. IDE or (gdb) dump binary memory, not launched by manual commands. compare-sections and blank-check features are processing data in BMD without sending contents to GDB, only checksums/"Is blank".

Unrelated, I also looked into trying to implement m-response RLE compression in BMF and that needed a second buffer for memcpy/memmove. I can push that branch online if you're interested. Because Hclk > USBFS clk > SWD clk, this can optionally speed up communications with the price of complicating packet logs.

If there was a secondary half-sized buffer for raw bytes, then I see how it could be handled easily. But `alloca`/VLA does not result in on-heap fragmentation, and I don't have BMF telemetry to confirm stack usage at every alloca callsite -- 4096 seemed good enough. That said, I only aimed to restore functionality before the impending freeze for release-candidate, so I didn't look deeper in this (entire m-packet handler or adjacent similar handlers). If you can fully get rid of these stack allocations so that e.g. I could bump GDB packet buffer (compile-time setting) from 1024 to 4096 without blowing past `stlinkv3` stack size (which is a USB HS device and benefits from 512 byte bulk packets) then I'd be happy to test it. *But then again, what are the usecases of Long block reads (and writes)? I can think of some, like performing RAM and Flash dumps, extracting ETF/MTB contents, scanning memory otherwise, and inspecting large (>512 byte) in-DUT arrays. This is usually automated with help of Eclipse/VSCode/etc. IDE or `(gdb) dump binary memory`, not launched by manual commands. `compare-sections` and blank-check features are processing data in BMD without sending contents to GDB, only checksums/"Is blank".* Unrelated, I also looked into trying to implement m-response RLE compression in BMF and that needed a second buffer for memcpy/memmove. I can push that branch online if you're interested. Because Hclk > USBFS clk > SWD clk, this can optionally speed up communications with the price of complicating packet logs.

Sounds good to us perigoso! Really like this plan as a way for getting rid of a rather nasty alloca() and getting some more memory safety.

Sounds good to us perigoso! Really like this plan as a way for getting rid of a rather nasty `alloca()` and getting some more memory safety.
Sign in to join this conversation.
No Branch/Tag specified
main
feature/bmda-remote-comms
ALTracer/feature/aarch64-ident
feature/better-meson-optimisation-handling
feature/am335x-support
feature/esp32-c3-support
feature/cortex-ar-software-breakpoints
feature/unit-testing
feature/windows-usb-serial-interface-naming
fix/bmp-external-spi
ALTracer/feature/bluepillplus-platform
ALTracer/feature/at32f43x-unrdp
feature/const-correctness
ALTracer/feature/fault_handlers
ALTracer/feature/hazard3-ice40-support
fix/ci-cleanup
ALTracer/fix/gdb-10-12-thread
ALTracer/feature/blackpill-f4-adc
ALTracer/fix/cortex-desc-allocfail-report
ALTracer/feature/spi-perf
ALTracer/feature/calibrate_swd
ALTracer/feature/blank-check
feature/avr
v2.0
v1.9
v1.10
v1.8
v2.1.0-rc1
v2.0.0
v2.0.0-rc2
v2.0.0-rc1
v1.9.3
v1.10.2
v1.10.1
v1.10.0
v1.10.0-rc1
v1.10.0-rc0
v1.9.2
v1.8.3
v1.9.1
v1.9.0
v1.9.0-rc1
v1.9.0-rc0
v1.8.2
v1.8.1
v1.8.0
v1.7.1
v1.7
v1.6.2-rc1
1.6.2-rc0
v1.6.1
v1.6
v1.6-rc1
v1.6-rc0
production_01
production_00
Labels
Clear labels
BMD App
Black Magic Debug App (aka. PC hosted) (not firmware)
BMP Firmware
Black Magic Probe Firmware (not PC hosted software)
Bug
Confirmed bug
Build system
Build system
Can't reproduce
Maintainers can't reproduce this problem
CI
Continuous Integration System
Contribution wanted
User contributions welcome
Documentation
Project documentation
Draft
Work in progress draft
Duplicate
This issue or pull request already exists
Enhancement
General project improvement
Feedback wanted
Requires additional submitter feedback
Foreign Host Board
Non Native hardware to runing Black Magic firmware on
GDB
Issue/PR related to GDB
Good first issue
Good for newcommers
HwIssue Mitigation
Solving or mitigating a Hardware issue in Software
Information Needed
Maintainers need more information
NativeHardware
Official Black Magic Debug Hardware
New Host Board
New hardware to run Black Magic firmware on
New Target
New debug target
Off Topic
Something that does not involve the project in any way
Potential Bug
A potential, unconfirmed or very special circumstance bug
Regression
Bug caused by a regression
User Interest Needed
More user interest required before consideration
User Testing Needed
Looking for user testing reports
Won't fix
Outside of the project scope or works as intended
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
blackmagic-debug/blackmagic#2042
Reference in a new issue
blackmagic-debug/blackmagic
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?