[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH v3 02/17] include/uk/arch: assert for undefined behavior in ffs-functions



The behavior of bitops functions in unikraft is undefined, if the
first/last bit is searched, while none is set (zero is passed as an
argument). This patch adds an assertion if argument is 0

Signed-off-by: Yuri Volchkov <yuri.volchkov@xxxxxxxxx>
---
 include/uk/arch/x86_64/atomic.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/include/uk/arch/x86_64/atomic.h b/include/uk/arch/x86_64/atomic.h
index c48d5bd..7bab838 100644
--- a/include/uk/arch/x86_64/atomic.h
+++ b/include/uk/arch/x86_64/atomic.h
@@ -30,6 +30,16 @@
 #error Do not include this header directly
 #endif
 
+#ifdef CONFIG_LIBUKDEBUG
+#include <uk/assert.h>
+#define __ukarch_atomic_assert(x) UK_ASSERT(x)
+#else
+#define __ukarch_atomic_assert(x) \
+       do {               \
+       } while (0)
+#endif /* CONFIG_LIBUKDEBUG */
+
+
 /**
  * ukarch_ffs - find first (lowest) set bit in word.
  * @word: The word to search
@@ -38,6 +48,8 @@
  */
 static inline unsigned int ukarch_ffs(unsigned int word)
 {
+       __ukarch_atomic_assert(word);
+
        __asm__("bsfl %1,%0"
                : "=r" (word)
                : "rm" (word));
@@ -52,6 +64,8 @@ static inline unsigned int ukarch_ffs(unsigned int word)
  */
 static inline unsigned int ukarch_fls(unsigned int word)
 {
+       __ukarch_atomic_assert(word);
+
        __asm__("bsrl %1,%0"
                : "=r" (word)
                : "rm" (word));
@@ -66,6 +80,8 @@ static inline unsigned int ukarch_fls(unsigned int word)
  */
 static inline unsigned long ukarch_ffsl(unsigned long word)
 {
+       __ukarch_atomic_assert(word);
+
        __asm__("bsfq %1,%0"
                : "=r" (word)
                : "rm" (word));
@@ -80,6 +96,8 @@ static inline unsigned long ukarch_ffsl(unsigned long word)
  */
 static inline unsigned long ukarch_flsl(unsigned long word)
 {
+       __ukarch_atomic_assert(word);
+
        __asm__("bsrq %1,%0"
                : "=r" (word)
                : "rm" (word));
-- 
2.18.0


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.