From d888df967347dca4267976f488b0af5ffa17a58d Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Mon, 13 Nov 2023 12:11:23 +0800 Subject: [PATCH] note: add ringbuffer aligned access handle Fix ubsan warning that writes need to be aligned to memory boundaries when writing data Signed-off-by: yinshengkai --- drivers/note/note_driver.c | 4 ++-- drivers/note/noteram_driver.c | 6 +++--- include/nuttx/sched_note.h | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/note/note_driver.c b/drivers/note/note_driver.c index 70c8c2dd03..a710c4ee0f 100644 --- a/drivers/note/note_driver.c +++ b/drivers/note/note_driver.c @@ -585,10 +585,10 @@ static void note_record_taskname(pid_t pid, FAR const char *name) ti = (FAR struct note_taskname_info_s *) &g_note_taskname.buffer[g_note_taskname.head]; - ti->size = tilen; + ti->size = NOTE_ALIGN(tilen); ti->pid = pid; strlcpy(ti->name, name, namelen + 1); - g_note_taskname.head += tilen; + g_note_taskname.head += ti->size; } #endif diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c index f6db8ee064..edd84d84a9 100644 --- a/drivers/note/noteram_driver.c +++ b/drivers/note/noteram_driver.c @@ -295,7 +295,7 @@ static void noteram_remove(FAR struct noteram_driver_s *drv) /* Get the length of the note at the tail index */ - length = drv->ni_buffer[tail]; + length = NOTE_ALIGN(drv->ni_buffer[tail]); DEBUGASSERT(length <= noteram_length(drv)); /* Increment the tail index to remove the entire note from the circular @@ -387,7 +387,7 @@ static ssize_t noteram_get(FAR struct noteram_driver_s *drv, remaining--; } - drv->ni_read = read; + drv->ni_read = NOTE_ALIGN(read); return notelen; } @@ -594,7 +594,7 @@ static void noteram_add(FAR struct note_driver_s *driver, space = space < notelen ? space : notelen; memcpy(drv->ni_buffer + head, note, space); memcpy(drv->ni_buffer, buf + space, notelen - space); - drv->ni_head = noteram_next(drv, head, notelen); + drv->ni_head = noteram_next(drv, head, NOTE_ALIGN(notelen)); spin_unlock_irqrestore_wo_note(&drv->lock, flags); } diff --git a/include/nuttx/sched_note.h b/include/nuttx/sched_note.h index eb0ce3d804..60063f2eac 100644 --- a/include/nuttx/sched_note.h +++ b/include/nuttx/sched_note.h @@ -51,6 +51,9 @@ * Pre-processor Definitions ****************************************************************************/ +#define NOTE_ALIGN(a) (((a) + sizeof(uintptr_t) - 1) & \ + ~(sizeof(uintptr_t) - 1)) + /* Provide defaults for some configuration settings (could be undefined with * old configuration files) */