Skip to content

Conversation

@jsarha
Copy link
Contributor

@jsarha jsarha commented Oct 22, 2025

Make IS_ALIGNED() safe for testing with alignment == 0. Without this fix the DSP will crash if alignemnet is 0.

This is why:

The current virtual heap implementation has this line:
assert(IS_ALIGNED(mem, align));

And mod_balloc() implementation looks like this:
static inline void *mod_balloc(struct processing_module *mod, size_t size)
{
return mod_balloc_align(mod, size, 0);
}

Which means, that without this fix any call to mod_balloc() would cause division by zero crash.

I think the correct place to fix this error is in IS_ALIGNED() macro. It should not crash if someone wants to check alignment to zero.

Make IS_ALIGNED() safe for testing with alignment == 0. Without this fix
the DSP will crash if alignemnet is 0.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Copilot AI review requested due to automatic review settings October 22, 2025 21:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a division-by-zero crash in the IS_ALIGNED() macro that occurs when checking alignment with a value of 0. The issue manifests when mod_balloc() calls mod_balloc_align() with align=0, which then triggers an assertion using IS_ALIGNED() that would perform a modulo operation by zero.

Key Changes:

  • Modified IS_ALIGNED() macro to handle zero alignment values safely by short-circuiting when alignment is 0

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

#include <zephyr/sys/util.h>
#endif

/* Align the number to the nearest alignment value */
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

The macro lacks documentation explaining the behavior when alignment is 0. Add a comment clarifying that zero alignment is treated as always aligned (returns true), which prevents division by zero in the modulo operation.

Suggested change
/* Align the number to the nearest alignment value */
/* Align the number to the nearest alignment value */
/* If alignment is zero, IS_ALIGNED returns true (treats all values as aligned).
* This prevents division by zero in the modulo operation.
*/

Copilot uses AI. Check for mistakes.
/* Align the number to the nearest alignment value */
#ifndef IS_ALIGNED
#define IS_ALIGNED(size, alignment) ((size) % (alignment) == 0)
#define IS_ALIGNED(size, alignment) (!(alignment) || (size) % (alignment) == 0)
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 we are using Zephyr version (included above) and this version of IS_ALIGNED wont be used (#ifndef IS_ALIGNED)

#ifdef __ZEPHYR__
#include <zephyr/sys/util.h>
#endif

btw alignment 0 is incorrect anyway so maybe this macro was called somewhere where it shouldnt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whether it is incorrect or not, is a matter of definition. But apparently this change is useless on its own, so I'll close it.

@jsarha
Copy link
Contributor Author

jsarha commented Oct 23, 2025

The Zephyr definition is the one that is used in SOF, not this. So this is an idle attempt to fix the issue.

@jsarha jsarha closed this Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants