Skip to content

Conversation

@michpappas
Copy link
Member

@michpappas michpappas commented Aug 8, 2025

uk_event handlers often omit using one of the designated macro values, and return an integer instead. This is particularly problematic when returning zero to signify that the event was handled, as event.h assigns zero to UK_EVENT_NOT_HANDLED.

Convert uk_event handler return values to enum to catch such errors at compile-time. Introduce a UK_EVENT_ERROR state and an additional parameter in the handler prototype that handlers must populate before returning UK_EVENT_ERROR.

Prerequisite checklist

  • Read the contribution guidelines regarding submitting new changes to the project;
  • Tested your changes against relevant architectures and platforms;
  • Ran the checkpatch.uk on your commit series before opening this PR;
  • Updated relevant documentation.

Base target

  • Architecture(s): N/A
  • Platform(s): N/A
  • Application(s): N/A

Additional configuration

Description of changes

@github-actions github-actions bot added arch/arm arch/arm64 area/include Part of include/uk area/lib Internal Unikraft Microlibrary area/plat Unikraft Patform lang/c Issues or PRs to do with C/C++ lib/syscall_shim lib/ukboot lib/ukdebug plat/common Common to all platforms labels Aug 8, 2025
Copy link
Member

@mogasergiu mogasergiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a couple of missed type updated I think

@michpappas michpappas force-pushed the michpappas/ukevent_return_enum branch from 472282d to 6f779b3 Compare August 12, 2025 12:23
@github-actions github-actions bot added arch/x86_64 lib/posix-process Information about system parameters plat/xen Unikraft for Xen labels Aug 12, 2025
@michpappas michpappas changed the title include/uk/event: Convert handler return values to enum include/uk/event: Make handler and raise return values type-safe Aug 12, 2025
uk_event handlers often omit using one of the designated macro values,
and return an integer instead. This is particularly problematic when
returning zero to signify that the event was handled, as event.h
assigns zero to UK_EVENT_NOT_HANDLED.

Convert uk_event handler return values to enum to catch such errors
at compile-time. Introduce a UK_EVENT_ERROR state and an additional
parameter in the handler prototype that handlers must populate before
returning UK_EVENT_ERROR.

Checkpatch-ignore: MACRO_ARG_REUSE
Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Checkpatch-Ignore: LONG_LINE
Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Signed-off-by: Michalis Pappas <[email protected]>
Update event handlers and event raising calls to use the new
enum return type and error parameter.

Checkpatch-ignore: MACRO_ARG_REUSE
Checkpatch-ignore: MACRO_ARG_PRECEDENCE
Checkpatch-ignore: UNNECESSARY_PARENTHESES
Signed-off-by: Michalis Pappas <[email protected]>
@michpappas michpappas force-pushed the michpappas/ukevent_return_enum branch from 6f779b3 to b37f342 Compare August 12, 2025 12:32
@michpappas michpappas requested a review from mogasergiu August 12, 2025 12:42
Copy link
Member

@mogasergiu mogasergiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it feels viscerally wrong whenever I end up having to add functions with lateral effect and can't help but wonder if there is a better way. But, yeah, for now I can't see a better way than this that would fit all requirements either...
So I guess we can stick with this or... maybe leave it for later and brainstorm some more. I would approve either way.

* else if (!rc)
* uk_pr_err("myevent not handled!\n");
* rc = uk_raise_event(myevent, &myevent_data, &error);
* if (unlikely(rc < UK_EVENT_ERROR))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop the unlikely from the documentation and leave as it was. Also the type of rc might need to be updated.

{
const uk_event_handler_t *itr;
int rc;
int ret = UK_EVENT_NOT_HANDLED;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ret should have been enum?

return UK_EVENT_HANDLED;

if (rc == UK_EVENT_HANDLED_CONT)
if (ret == UK_EVENT_HANDLED_CONT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this if statement is no longer necessary

#define uk_raise_event(event, data, error) \
({ _UK_EVT_IMPORT_EVENT(event); \
uk_raise_event_ptr(UK_EVENT_PTR(event), data); })
uk_raise_event_ptr(UK_EVENT_PTR(event), data, error); })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should enclose data and error between paranthesis? Also don't forget to now add the error argument documentation to uk_raise_event as well.

int pprocess_raise_execve_event(struct posix_process_execve_event_data *data)
enum uk_event_status
pprocess_raise_execve_event(struct posix_process_execve_event_data *event_data,
int event_error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant int *event_error? (You do have it in the header definition :D)

#if __INTERRUPTSAFE__ && CONFIG_LIBUKBOOT_SHUTDOWNREQ_HANDLER
static int shutdown_req_handler(void *data)
static
enum uk_event_status shutdown_req_handler(void *data, int *error __unused)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can update error to return value of rc.

#include <uk/print.h>

static int vmem_arch_pagefault(void *data)
static enum uk_event_status vmem_arch_pagefault(void *data, int *error __unused)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto (for x86 too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch/arm arch/arm64 arch/x86_64 area/include Part of include/uk area/lib Internal Unikraft Microlibrary area/plat Unikraft Patform lang/c Issues or PRs to do with C/C++ lib/posix-process Information about system parameters lib/syscall_shim lib/ukboot lib/ukdebug plat/common Common to all platforms plat/xen Unikraft for Xen

Projects

Status: 🧊 Icebox

Development

Successfully merging this pull request may close these issues.

2 participants