]> git.nickg.me.uk Git - nvc.git/commit
vhpi: Fix use-after-free when removing callbacks
authorSean Anderson <seanga2@gmail.com>
Sat, 20 May 2023 18:05:38 +0000 (14:05 -0400)
committerNick Gasson <nick@nickg.me.uk>
Sun, 21 May 2023 07:22:47 +0000 (07:22 +0000)
commita21a360e8950ef080fa062dcdd3310112a98193f
tree09b142f787492724f03c4aca1170de2a2d99339c
parent823283040f5617a6c1c6648a5379519e76e1280d
vhpi: Fix use-after-free when removing callbacks

Callbacks can generally be in a few states:

- Disabled or mature
- Actively executing
- Waiting to execute

For all callbacks except for AfterDelay, when the callback is waiting to
execute then clearing it will prevent further execution. However,
AfterDelay callbacks can be absent from the eventq_heap in the driverq
workqueue awaiting execution. Clearing these callbacks will not prevent
them from executing.

To work around this, make model_clear_timeout_cb return a boolean
indicating whether a callback was actually cleared. If no callback was
cleared and the callback is not currently executing, then the callback
is in a workqueue. In this case, just free the callback later.
Otherwise, we have actually removed a callback and we can be sure that
it will not execute, so free it now.

Fixes: d6e523a4 ("vhpi: Free callbacks in remove")
src/rt/model.c
src/rt/model.h
src/vhpi/vhpi-model.c