@@ -119,6 +119,7 @@ struct am_event *am_event_allocate_x(int id, int size, int margin) {
119119
120120 memset (event , 0 , sizeof (* event ));
121121 event -> id = id ;
122+ event -> id_lsw = (uint32_t )id & AM_EVENT_ID_LSW_MASK ;
122123 event -> pool_index_plus_one =
123124 (unsigned )(i + 1 ) & AM_EVENT_POOL_INDEX_MASK ;
124125
@@ -142,6 +143,12 @@ static void am_event_free_unsafe(const struct am_event **event) {
142143
143144 struct am_event * e = AM_CAST (struct am_event * , * event );
144145 AM_ASSERT (e -> pool_index_plus_one <= AM_EVENT_POOLS_NUM_MAX );
146+ /*
147+ * Check if event is valid.
148+ * If the below assert hits, then the reason is likely
149+ * a use after free condition.
150+ */
151+ AM_ASSERT (((uint32_t )e -> id & AM_EVENT_ID_LSW_MASK ) == e -> id_lsw );
145152
146153 if (e -> ref_counter > 1 ) {
147154 -- e -> ref_counter ;
@@ -203,6 +210,16 @@ struct am_event *am_event_dup_x(
203210 const struct am_event_state * me = & am_event_state_ ;
204211 AM_ASSERT (me -> npools > 0 );
205212 AM_ASSERT (event -> id >= AM_EVT_USER );
213+ if (!am_event_is_static (event )) {
214+ /*
215+ * Check if event is valid.
216+ * If the below assert hits, then the reason is likely
217+ * a use after free condition.
218+ */
219+ AM_ASSERT (
220+ ((uint32_t )event -> id & AM_EVENT_ID_LSW_MASK ) == event -> id_lsw
221+ );
222+ }
206223 AM_ASSERT (margin >= 0 );
207224
208225 struct am_event * dup = am_event_allocate_x (event -> id , size , margin );
@@ -283,6 +300,12 @@ void am_event_inc_ref_cnt(const struct am_event *event) {
283300 }
284301
285302 AM_ASSERT (event -> ref_counter < AM_EVENT_REF_COUNTER_MASK );
303+ /*
304+ * Check if event is valid.
305+ * If the below assert hits, then the reason is likely
306+ * a use after free condition.
307+ */
308+ AM_ASSERT (((uint32_t )event -> id & AM_EVENT_ID_LSW_MASK ) == event -> id_lsw );
286309
287310 struct am_event * e = AM_CAST (struct am_event * , event );
288311 struct am_event_state * me = & am_event_state_ ;
@@ -331,6 +354,16 @@ static enum am_event_rc am_event_push_x(
331354 const int capacity = am_queue_get_capacity (queue );
332355 AM_ASSERT (margin < capacity );
333356 AM_ASSERT (push );
357+ if (!am_event_is_static (event )) {
358+ /*
359+ * Check if event is valid.
360+ * If the below assert hits, then the reason is likely
361+ * a use after free condition.
362+ */
363+ AM_ASSERT (
364+ ((uint32_t )event -> id & AM_EVENT_ID_LSW_MASK ) == event -> id_lsw
365+ );
366+ }
334367
335368 struct am_event * e = AM_CAST (struct am_event * , event );
336369 struct am_event_state * me = & am_event_state_ ;
0 commit comments