Commit Graph

178 Commits

Author SHA1 Message Date
Konstantin Kondrashov
82e6c831e7 fix(esp_event): free queued legacy cleanup ctx on loop delete
When a loop is deleted while an internal legacy "cleanup" event is still
queued (posted by a deferred self-unregistration from within a handler),
esp_event_loop_delete() drained the queue but only freed the post payload,
leaking the heap copy of the handler context allocated for the legacy path.

Free ctx->handler_ctx for queued legacy cleanup events while draining the
queue, mirroring the cleanup done in esp_event_loop_run().

Add a regression test that leaves a legacy cleanup event queued and asserts
no memory is leaked on loop deletion.
2026-07-21 15:33:47 +03:00
Konstantin Kondrashov
e8ffb477a0 fix(esp_event): clear running_task before releasing mutex on tick timeout
When esp_event_loop_run() exited via the ticks-expired break path,
loop->running_task was left pointing to the current task handle.
Any subsequent trylock in esp_event_handler_unregister_with_internal()
would see a stale non-NULL running_task and take the wrong code path.
2026-07-21 15:25:58 +03:00
Konstantin Kondrashov
9d2d32524b fix(esp_event): prevent UAF race between post and loop delete (SEC-222)
esp_event_post_to() could access loop->queue / loop->mutex after
esp_event_loop_delete() freed them when both ran concurrently.

Introduce esp_event_loop_state_t with:
- posts_in_flight: reference-count incremented atomically (under
  state.lock spinlock) before touching any loop resources, decremented
  on every exit path via goto on_err.
- deleting: atomic_bool set by esp_event_loop_delete() to block new
  posts from entering the critical section.

esp_event_loop_delete() sets deleting=true, then busy-waits (releasing
and re-acquiring loop->mutex each tick) until posts_in_flight reaches
zero before proceeding with teardown.

esp_event_isr_post_to() performs a lock-free atomic_load of deleting as
a best-effort guard; ISR context cannot participate in the spinlock
protocol but the window is documented and accepted.
2026-07-21 15:25:58 +03:00
Konstantin Kondrashov
736275e562 fix(esp_event): skip dispatch for internal cleanup events (SEC-221)
After processing an esp_event_handler_cleanup sentinel, execution fell
through into the regular dispatch block. Every loop-level (ANY_BASE/
ANY_ID) handler was invoked with base="cleanup" and event_data pointing
at the internal esp_event_remove_handler_context_t struct.

Consequences:
- Information disclosure: internal handler addresses and loop instance
  pointer are exposed to every loop-level handler.
- UAF: if a handler stores event_data for later use, post_instance_delete
  frees the ctx, turning the stored pointer into a dangling reference.
- Logic corruption: handlers that switch on base with a default branch
  misbehave on every unregister anywhere in the system.

Fix: wrap the regular dispatch block in an else clause so it is skipped
entirely for cleanup events. post_instance_delete, ticks accounting, and
xSemaphoreGiveRecursive remain in the shared tail executed for both paths.

Closes SEC_221
2026-07-21 15:25:57 +03:00
Konstantin Kondrashov
4ab4d5b894 fix(esp_event): use recursive mutex API in handler unregister (SEC-220)
1) loop->mutex is created with xSemaphoreCreateRecursiveMutex(). FreeRTOS
requires that recursive mutexes are only acquired and released with
xSemaphoreTakeRecursive / xSemaphoreGiveRecursive.

esp_event_handler_unregister_with_internal() used the non-recursive
xSemaphoreTake(loop->mutex, 0) / xSemaphoreGive(loop->mutex) in the fast
path. The non-recursive Take bypasses uxRecursiveCallCount bookkeeping;
if the same task subsequently takes the mutex recursively (e.g. re-entry
from a handler or a follow-up register), the call count drifts. The
non-recursive Give then unconditionally drops the holder, allowing another
task to acquire the mutex while the original task still believes it holds
the lock — a full lock violation on the handler list leading to UAF and
potential RCE on attacker-driven event floods.

Fix: replace xSemaphoreTake/xSemaphoreGive with the Recursive variants in
the fast (try-take with timeout 0) path of unregister_with_internal.

2) avoid use-after-free when unregistering handler from a callback

The recursive try-lock introduced in SEC-220 succeeds re-entrantly when a
handler unregisters itself from within its own callback, causing the handler
node to be freed immediately while the dispatch loop still writes profiling
counters to it after the callback returns. Route the in-callback case to the
deferred cleanup path and only free directly once no dispatch is active.

Closes SEC_220
2026-07-21 15:25:57 +03:00
Konstantin Kondrashov
2c935ea861 fix(esp_event): protect is_handler_registered traversal with mutex (SEC-219)
esp_event_is_handler_registered() walked loop_nodes, base_nodes, id_nodes
and handler lists with no lock held, then released an unowned mutex at the
'out:' label via xSemaphoreGive().

Concurrent register/unregister/delete operations can free handler nodes
during the unlocked walk (SLIST UAF). The xSemaphoreGive on an unowned
recursive mutex corrupts the recursive call-count of any task that
legitimately holds the mutex.

Fix:
- Take loop->mutex with xSemaphoreTakeRecursive before the traversal.
- Replace xSemaphoreGive at the 'out:' label with xSemaphoreGiveRecursive
  so every exit path holds the mutex for exactly one balanced take/give.

Closes SEC_219
2026-07-21 15:25:57 +03:00
Konstantin Kondrashov
bc1b1a7f70 fix(esp_event): fix format string vulnerability in esp_event_dump (SEC-064)
fprintf(file, buf) is a format-string sink: if any registered event base
or handler name contains "%", fprintf interprets it as a format directive,
causing an information leak or crash.

Replace with fprintf(file, "%s", buf) so the buffer is always treated as
plain text regardless of its content.

Closes SEC_064
2026-07-06 15:05:53 +03:00
Sudeep Mohanty
f1d84e671f fix(esp_event/test): remove quotes around space-separated PRIV_REQUIRES list
The test app set priv_requires as a single quoted string ("esp_event unity")
instead of separate list items. The v1 build system silently splits on
spaces, but the v2 compat layer treats it as a single component name.
2026-05-21 10:05:47 +02:00
Fu Hanxi
ddc7e0cdf7 ci: move qemu test cli args alongside test scripts
remove redundant host_test marker
2026-03-26 10:56:15 +01:00
Marius Vikhammer
5f914189c2 ci(core): removed common_components from core test-apps dependencies 2026-01-26 11:47:23 +08:00
igor.udot
4c26ab876b ci: update build-test-rules to use common_components 2026-01-23 10:14:09 +08:00
Guillaume Souchere
974f610d11 Merge branch 'feat/esp-event-default-loop-in-ext-ram' into 'master'
feat(esp_event): Add option to allocate event loop in external RAM

Closes IDF-14468

See merge request espressif/esp-idf!43510
2026-01-09 09:43:11 +01:00
Guillaume Souchere
0c570d860d feat(esp_event): Add option to allocate event loop in external RAM 2026-01-08 10:38:43 +01:00
Michael (XIAO Xufeng)
c299c0b749 Merge branch 'refactor/remove_idf_test_component' into 'master'
refactor: Remove idf_test component

Closes IDF-12578

See merge request espressif/esp-idf!43733
2026-01-06 16:20:20 +08:00
Xiao Xufeng
73735f3e87 test: merge chip-specific performance data headers 2026-01-01 02:35:58 +08:00
Xiao Xufeng
438e07b30e refactor: Remove idf_test component
Split the idf_performance.h and target ver, which hold the performance
thresholds, into the headers of each testing.

In the past pytest also parse the common header to get the thresholds.
Now the logic is also removed. Performance thresholds are supposed to be
in the pytest scripts.
2026-01-01 02:26:42 +08:00
Guillaume Souchere
97cb925d47 fix(esp_event): Leak in setup / teardown and wonrg evaluation of variable 2025-12-31 14:07:34 +08:00
Marius Vikhammer
e87aee9bac ci(core): fixed esp_event and psram freertos not running properly in CI 2025-12-31 12:38:47 +08:00
Konstantin Kondrashov
889a381153 feat(esp_event): Update depends_components
Where esp_event is used as depends_components:
- components/esp_event/test_apps
- components/esp_event/host_test
- examples/system/esp_event
2025-11-25 19:23:55 +02:00
Jimmy Wennlund
e2a6653680 feat(esp_event): Allow an event carry more data without malloc
This is both a feature and an optimization.

Feature:
Adjustable size of the internal storage in esp_event queue, currently
used by ISR posting, as they wont be able to make a malloc.

Optimization:
When non-isr is posting an event, use the inernal storage in the struct
instead of always allocating a new heap for the data. Most events in
esp-idf only contains a few bytes event information, and we have that
allocation payed for anyway.

This solved in a big part our memory fragmentation issue, as events
happens freqvently and used to create small memory allocations for just
4 bytes, and then in the event handler we usually allocated a bigger
chunk of heap for our feature. When returning from the event handler,
the 4 byte allocation was freed, leaving a hole in the heap.

Merges: https://github.com/espressif/esp-idf/pull/17797
2025-11-05 17:46:00 +02:00
Marius Vikhammer
113d69f188 test(core): add WDT protection to detect stuck tests 2025-10-24 10:48:13 +08:00
Chen Chen
a4710cc206 refactor(driver): remove redundant driver dependencies
now the driver component only contains legacy code for i2c, twai and
touch sensor
2025-09-30 15:47:45 +08:00
Marius Vikhammer
81dff2c991 change(esp_event): removed unnecessary freertos header includes 2025-08-25 11:00:58 +08:00
Marek Fiala
9d35d63651 feat(cmake): Update minimum cmake version to 3.22 (whole repository) 2025-08-19 14:44:32 +02:00
Marius Vikhammer
bf84ab652a change(test_utils): moved test_utils component to tools/test_apps/components/ 2025-07-21 14:05:50 +08:00
Guillaume Souchere
2a17297d66 change(esp-event): Update the doxygen comment of esp_event_handler_register
Add a comment to specify what happens when registering a handler several times
to the same event.
2025-06-12 08:23:20 +02:00
igor.udot
daf2d31008 test: format all test scripts 2025-03-05 12:08:48 +08:00
David Cermak
881628c548 fix(esp_event): Fix minor no-ISR post regression
from 15f6775f5d
2025-02-03 12:26:35 +01:00
Guillaume Souchere
15f6775f5d fix(esp_event): Handler unregistration by itself issue
when esp_event_handler_unregister_with_internal cannot take
the loop mutex (e.g., when the handler unregisters itself),
create an event with a special base identifier and add it to
the queue of the corresponding loop to postpone the removal
of the handler from the list at a time when the loop mutex can be
successfully taken.
2025-01-29 13:37:27 +01:00
Guillaume Souchere
d8d3c50b3f fix(esp_event): Fix event loop profiling in handler_execute function
handler_execute function is looking to match the handler only in the
list of loop events but does not look in the base event handler list
nor the id event handler list. So unless the event handler is
registered to be triggered for all event bases and all event ids of
an event loop, its profiling fields (invoked and time) are not updated
when it is called.

This commit updates the search for the matching handler to also look
in base event list and ID event list.

Closes https://github.com/espressif/esp-idf/issues/15041
2025-01-29 13:17:00 +01:00
Marius Vikhammer
00a46fedbe ci(qemu): remove allowed to fail attribute from qemu tests 2024-07-26 15:36:32 +08:00
Jakob Hasse
7377b103b7 refactor(linux): remove explicit watchpoint configs
* These where necessary before to avoid enabling the
  watchpoint feature on Linux. Due to a recent change,
  these configurations became obsolete.
2024-06-28 18:01:37 +02:00
Jakob Hasse
11d77d54e8 docs(esp_event): Fixed mistake in API docs
Closes https://github.com/espressif/esp-idf/issues/13346
2024-03-11 11:06:18 +08:00
Erhan Kurubas
5f42493573 Merge branch 'astyle_format_coredump' into 'master'
refactor(espcoredump): format coredump component with astyle

See merge request espressif/esp-idf!29114
2024-02-20 17:59:23 +08:00
Erhan Kurubas
f4acf0b378 refactor(espcoredump): format coredump component with astyle 2024-02-19 21:57:28 +01:00
fl0wl0w
90d1dcfd76 feat(freertos): Introduced new Kconfig option CONFIG_FREERTOS_NUMBER_OF_CORES
This commit replaces the use of portNUM_PROCESSORS and configNUM_CORES
macros in all of ESP-IDF. These macros are needed to realize an SMP
scenario by fetching the number of active cores FreeRTOS is running on.
Instead, a new Kconfig option, CONFIG_FREERTOS_NUMBER_OF_CORES, has been
added as a proxy for the FreeRTOS config option, configNUMBER_OF_CORES.
This new commit is now used to realize an SMP scenario in various places
in ESP-IDF.

[Sudeep Mohanty: Added new Kconfig option CONFIG_FREERTOS_NUMBER_OF_CORES]

Signed-off-by: Sudeep Mohanty <sudeep.mohanty@espressif.com>
2024-02-09 09:11:28 +01:00
Jakob Hasse
417de470ec change(esp_event): reformat files with astyle 2024-01-31 11:07:07 +08:00
Guillaume Souchere
7bb236254e Merge branch 'doc/update-esp-event-instance-register-descrition' into 'master'
docs(esp-event): Update description of esp_event_handler_instance_x

Closes IDFGH-11711

See merge request espressif/esp-idf!28063
2024-01-23 13:48:31 +08:00
Darian Leung
f50d83413e refactor(tools): Tidy up core component files copyright ignore
Some files that should have their copyrights checked are still placed on the
copyright ignore list.

- These entries have been tidied up
- Copyrights of those files have been updated.
2024-01-22 18:07:35 +08:00
Guillaume Souchere
8f407949c6 docs(esp-event): Update description of esp_event_handler_instance_x
Add a note to esp_event_handler_instance_register_with and
esp_event_handler_instance_register to specify that calling those
functions with the instance parameter set to NULL is equivalent to
calling esp_event_handler_register_with and esp_event_handler_register.

Closes https://github.com/espressif/esp-idf/issues/12818
2023-12-21 13:35:42 +01:00
Marius Vikhammer
8fe0f99368 change(system): enabled pthread, ringbuf and event tests 2023-12-21 11:09:42 +08:00
Ivan Grokhotkov
79a2c15477 ci(esp_event): upgrade host test to Catch2 as a component, fix build
- use espressif/catch2 component
- fix build issues after FreeRTOS upgrade
- enable test app build in CI
2023-11-29 12:38:46 +01:00
Sudeep Mohanty
d507a86285 feat(freertos): Exposed Kconfig option for configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES
This commit exposes the FreeRTOS List integrity check option
configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES via menuconfig.
2023-11-09 14:54:13 +01:00
Jakob Hasse
9aab41cde1 fix(esp_event): dedicated task test now waits for semaphore correctly 2023-11-02 11:49:45 +08:00
Chen Yudong
2e11919f70 fix(ci): change build-test-rules files folder 2023-09-20 19:17:06 +08:00
Jakob Hasse
8df4625c84 refactor(esp_event): improved esp_event unit tests
* Decomposed tests into atomic unit tests
* Made tests less time-dependent, hence more robust
  on different platforms (ESP32, QEMU, Linux)
* Ported most of the tests to linux
* Removed some redundant tests
* Fixed bug the tests discovered
* Simplified parts of the tests to be more clear
* Partially used C++ to simplify setup/teardown
* Unified setup/teardown in general
2023-09-12 17:10:18 +08:00
Jakob Hasse
342a5b428c fix(esp_event): made #include <stdint.h> explicit 2023-08-09 16:17:06 +08:00
Jakob Hasse
a703d7aa89 refactor(esp_event): improved tests to fail less frequently on QEMU
* improved setup/teardown to not put shared event system into
  inconsistent state
* reduced timing-dependency of several tests by using
  a semaphore instead of waiting for a guessed timeout
* Deactivated WDT (both Interrupt WDT, Task WDT) for QEMU tests
* Ignore esp_timer-based test for QEMU, CPU timing is different
  on ESP32 simulation in QEMU
2023-07-19 11:50:44 +08:00
Marius Vikhammer
91aaf8b1a0 ci(qemu): temporarily allow qemu tests to fail 2023-07-13 11:16:38 +08:00
Marius Vikhammer
cf838f44cb fix(core-system/esp event): Fixed missing error return documentation for esp_event_loop_create_default
ESP_ERR_INVALID_STATE was not listed as a possible return error code.

Closes https://github.com/espressif/esp-idf/issues/11745
2023-06-27 10:55:53 +08:00