Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in adiv5_swd_scan #1799

Open
vlad230596 opened this issue Apr 12, 2024 · 0 comments
Open

Memory leak in adiv5_swd_scan #1799

vlad230596 opened this issue Apr 12, 2024 · 0 comments
Labels
Potential Bug A potential, unconfirmed or very special circumstance bug
Milestone

Comments

@vlad230596
Copy link

Hello,
I'm using blackmagic 1.10.2 to flash devices and found a memory leak. My investigation showed that the problem is within adiv5_swd_scan.
Blackmagic code is running on Nrf52840 and the Swd target is also Nrf52840.
The important condition for bug reproducing is exception during swd scan. There is some noise environment, so this can happen sometimes. To simulate this situation, you can add code that emulates errors when reading Swd, for example:

int gpio_get(int gpio, uint32_t pin){
    if(platform_time_ms() % 5000 == 0) {
        return !nrf_gpio_pin_read(pin);
    }
    return nrf_gpio_pin_read(pin);
}

Here is my test code:

    size_t successed = 0;
    size_t failed = 0;
    while(true) {
        TRY_CATCH(_e, EXCEPTION_ALL) {
            adiv5_swd_scan(0);
            ++successed;
        }
        if(_e.type != 0) {
            alDebug() << "Scan error " << _e.type << " " << _e.msg;
            ++failed;
        }
        TRY_CATCH(_e, EXCEPTION_ALL) {
            target_list_free();
        }
        if(_e.type != 0) {
            alDebug() << "Free Error " << _e.type << " " << _e.msg;
        }
        alDebug() << "Successed: " << successed << " Failed: " << failed << " FreeHeap: "  << xPortGetFreeHeapSize();
    }

Here is an example of output:

Successed: 262 Failed: 3 FreeHeap: 174176
Successed: 263 Failed: 3 FreeHeap: 174176
Successed: 264 Failed: 3 FreeHeap: 174176
Successed: 265 Failed: 3 FreeHeap: 174176
Successed: 266 Failed: 3 FreeHeap: 174176
Scan error 1 SWD parity error
Successed: 266 Failed: 4 FreeHeap: 174104
Successed: 267 Failed: 4 FreeHeap: 174104
Successed: 268 Failed: 4 FreeHeap: 174104
Successed: 269 Failed: 4 FreeHeap: 174104
Successed: 270 Failed: 4 FreeHeap: 174104
Successed: 271 Failed: 4 FreeHeap: 174104
Successed: 272 Failed: 4 FreeHeap: 174104
Successed: 273 Failed: 4 FreeHeap: 174104
Scan error 1 SWD invalid ACK
Successed: 273 Failed: 5 FreeHeap: 173984
Successed: 274 Failed: 5 FreeHeap: 173984
Successed: 275 Failed: 5 FreeHeap: 173984

As you can see, the size of free memory did not change during a successful scan and leaked with each error. Always 60 + 36 bytes or just 60 bytes(these numbers are from tracing all allocations).
Also, I found, that adiv5_debug_port_s *dp = calloc(1, sizeof(*dp)); has refcount 1 at the end of failed iterations.
Hope, it can helps. I am ready for any test to resolve this issue.

@dragonmux dragonmux added this to the v2.0 release milestone Apr 12, 2024
@dragonmux dragonmux added the Potential Bug A potential, unconfirmed or very special circumstance bug label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Potential Bug A potential, unconfirmed or very special circumstance bug
Projects
None yet
Development

No branches or pull requests

2 participants