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

[BUG] xEventGroupSetBits returns garbage if another high priority thread freed container #1142

Closed
skotopes opened this issue Sep 9, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@skotopes
Copy link
Contributor

skotopes commented Sep 9, 2024

Describe the bug

https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/event_groups.c#L643

pxEventBits->uxEventBits used after xTaskResumeAll(); which will return garbage if another thread freed underlying container.

Target

  • Development board: any
  • Instruction Set Architecture: any
  • IDE and version: any
  • Toolchain and version: any

Host

  • Host OS: any
  • Version: any

To Reproduce

  • In high priority thread:
    • create event group
    • call xEventGroupWaitBits
    • delete event group
  • In low priority thread:
    • call xEventGroupSetBits
    • check return value

Expected behavior
Not returning garbage

Screenshots
none

Additional context
none

@skotopes skotopes added the bug Something isn't working label Sep 9, 2024
@skotopes skotopes changed the title [BUG] xEventGroupSetBits returns garbage is another high priority thread freed container [BUG] xEventGroupSetBits returns garbage if another high priority thread freed container Sep 9, 2024
@aggarg
Copy link
Member

aggarg commented Sep 10, 2024

This is a case of use after free and seems like an application bug. The application needs to ensure that no other task is using a resource before deleting it.

@skotopes
Copy link
Contributor Author

@aggarg I believe it's not: I'm using EventGroup to signal from the child thread to the parent thread that it exited. I'm not using this primitive anymore, but because xEventGroupSetBits dereferencing pxEventBits->uxEventBits I get garbage in return or mpu fault(we protect various memory regions).

In general value returned from xEventGroupSetBits is mess: depending on thread priorities and xEventGroupWaitBits flags you'll get completely random results. Copying pxEventBits->uxEventBits value before xTaskResumeAll will make more sense. Any reason not to do so?

@CookiePLMonster
Copy link

This is a case of use after free and seems like an application bug. The application needs to ensure that no other task is using a resource before deleting it.

Wouldn't the pattern mentioned by this bug report be reasonably common in apps? The pattern of

  1. Create/Wake up Thread B from Thread A and start it
  2. Wait for Thread B to finish work
  3. Free resources from Thread A

isn't anything out of ordinary, and it's more or less exactly what this issue refers to.

@aggarg
Copy link
Member

aggarg commented Sep 10, 2024

Copying pxEventBits->uxEventBits value before xTaskResumeAll will make more sense. Any reason not to do so?

We can do that. I'd still suggest to evaluate your design and see if you really need to free resources dynamically.

Would you like to raise a PR for this or would you like us to raise one?

@CookiePLMonster
Copy link

We can do that. I'd still suggest to evaluate your design and see if you really need to free resources dynamically.

The use case in question cannot use a globally allocated event group, in this case a local variable is used - so while it's not utilizing FreeRTOS' dynamic allocation, the fact the static buffer is placed on the stack still makes it somewhat dynamic.

@skotopes
Copy link
Contributor Author

Copying pxEventBits->uxEventBits value before xTaskResumeAll will make more sense. Any reason not to do so?

We can do that. I'd still suggest to evaluate your design and see if you really need to free resources dynamically.

Would you like to raise a PR for this or would you like us to raise one?

If it's ok with you I can make PR.

@aggarg
Copy link
Member

aggarg commented Sep 10, 2024

If it's ok with you I can make PR.

Yes, please do.

@aggarg
Copy link
Member

aggarg commented Sep 10, 2024

The use case in question cannot use a globally allocated event group, in this case a local variable is used - so while it's not utilizing FreeRTOS' dynamic allocation, the fact the static buffer is placed on the stack still makes it somewhat dynamic.

Thanks for explaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants