You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/08/19 08:12:56 UTC

[GitHub] [incubator-nuttx] no1wudi opened a new pull request, #6881: debug: Introduce portion of UBSan for arm/risc-v

no1wudi opened a new pull request, #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881

   
   ## Summary
   
   ### Image size
   
   without UBSan
   ```
    text    data     bss     dec     hex filename
     85612     208  142258  228078   37aee nuttx
   ```
   
   with UBSan:
   ```
      text    data     bss     dec     hex filename
    194290   98164  208634  501088   7a560 nuttx
   ```
   
   ### Sample
   ```c
   int main(int argc, FAR char *argv[])
   {
     uint32_t ptr[32];
     printf("Hello, World!! %lu\n", ptr[64]);
     return 0;
   }
   ```
   Try to run this sample:
   ```
   nsh> hello
   ubsan_prologue: ================================================================================
   ubsan_prologue: UBSAN: array-index-out-of-bounds in hello_main.c:39:37
   __ubsan_handle_out_of_bounds: index 64 is out of range for type 'uint32_t [32]'
   ubsan_epilogue: ================================================================================
   Hello, World!! 1070182368
   nsh>
   ```
   
   ## Impact
   New feature
   ## Testing
   Custom boards
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#issuecomment-1221582553

   @no1wudi look like UBSan will influence KASan function prototype:
   ```
   Error: kasan/kasan.c:225:6: error: conflicting types for built-in function '__asan_report_load_n_noabort'; expected 'void(void *, long int)' [-Werror=builtin-declaration-mismatch]
     225 | void __asan_report_load_n_noabort(uintptr_t addr, size_t size)
   ```
   Let's change _asan_xxx to satisfy the compiler expectation.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950869035


##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);

Review Comment:
   ```suggestion
           index_str,  data->array_type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);

Review Comment:
   ```suggestion
            reason, loc->file_name, loc->line, loc->column);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+               rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align, uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+             "pointer of type %s failed",
+               align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+               align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+           offset ? "offset " : "",
+           1ul << (real_ptr ? ffsl(real_ptr) : 0),
+           real_ptr & (align - 1));

Review Comment:
   ```suggestion
            real_ptr & (align - 1));
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+               rhs_val_str, data->type->type_name);

Review Comment:
   ```suggestion
                rhs_val_str, data->type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)

Review Comment:
   ```suggestion
                                   FAR const char *reason)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+               rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align, uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+             "pointer of type %s failed",
+               align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+               align, data->type->type_name);

Review Comment:
   ```suggestion
                align, data->type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);

Review Comment:
   ```suggestion
               rhs_str,  type_bit_width(lhs_type), lhs_type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+               rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align, uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+             "pointer of type %s failed",
+               align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+               align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+           offset ? "offset " : "",
+           1ul << (real_ptr ? ffsl(real_ptr) : 0),
+           real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  FAR struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1ul << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)

Review Comment:
   change all rest of `_data` to `data`



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);

Review Comment:
   ```suggestion
                lhs_str, rhs_str, lhs_type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+               rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align, uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+             "pointer of type %s failed",
+               align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+               align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+           offset ? "offset " : "",
+           1ul << (real_ptr ? ffsl(real_ptr) : 0),

Review Comment:
   ```suggestion
            1ul << (real_ptr ? ffsl(real_ptr) : 0),
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,376 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+           reason, loc->file_name, loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void ubsan_prologue_epilogue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  ubsan_prologue(loc, reason);
+  ubsan_epilogue();
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+               type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+             " represented in type %s\n",
+               lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+               rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align, uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+             "pointer of type %s failed",
+               align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+               align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+           offset ? "offset " : "",

Review Comment:
   ```suggestion
            offset ? "offset " : "",
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950826807


##########
mm/Kconfig:
##########
@@ -191,6 +191,26 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+config MM_UBSAN_ALL
+	bool "Enable UBSan for the entire image"
+	depends on MM_UBSAN
+	default n

Review Comment:
   n->y



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,

Review Comment:
   ```suggestion
            g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct out_of_bounds_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));

Review Comment:
   remove the cast and merge to previous line



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)

Review Comment:
   add {}



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct overflow_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)

Review Comment:
   ```suggestion
   #define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");

Review Comment:
   remove



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  FAR const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  FAR struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  FAR struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  FAR struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  FAR struct source_location *location;
+  FAR struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct nonnull_arg_data
+{
+  struct source_location location;
+  struct source_location attr_location;
+  int arg_index;
+};
+
+struct out_of_bounds_data
+{
+  struct source_location location;
+  FAR struct type_descriptor *array_type;
+  FAR struct type_descriptor *index_type;
+};
+
+struct shift_out_of_bounds_data
+{
+  struct source_location location;
+  FAR struct type_descriptor *lhs_type;
+  FAR struct type_descriptor *rhs_type;
+};
+
+struct unreachable_data

Review Comment:
   remove and use source_location directly



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;

Review Comment:
   ```suggestion
     FAR struct type_mismatch_data_v1 *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);

Review Comment:
   Not fix yet.



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>

Review Comment:
   why need this header file



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);

Review Comment:
   ```suggestion
            g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct shift_out_of_bounds_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,

Review Comment:
   move " reason, loc->file_name," to the next line



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;

Review Comment:
   ```suggestion
     return *(FAR int64_t *)val;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));

Review Comment:
   remove the cast and merge to previous line



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =

Review Comment:
   ```suggestion
   static FAR const char *const g_type_check_kinds[] =
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "

Review Comment:
   add {}



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *lhs_type = data->lhs_type;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct alignment_assumption_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *rhs_type = data->rhs_type;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);

Review Comment:
   add {} for all if/else if/else



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "sub-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_mul_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "sub-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_mul_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "mul-overflow");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_load_invalid_value(FAR void *_data, FAR void *ptr)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "sub-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_mul_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "mul-overflow");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_load_invalid_value(FAR void *_data, FAR void *ptr)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "load-invalid-value");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_negate_overflow(FAR void *_data, FAR void *ptr)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "negate-overflow");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_pointer_overflow(FAR void *_data,
+                                     FAR void *ptr, FAR void *result)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
arch/xtensa/src/lx7/Toolchain.defs:
##########
@@ -47,6 +47,10 @@ ifeq ($(CONFIG_MM_KASAN),y)
   ARCHOPTIMIZATION += -fsanitize=kernel-address
 endif
 
+ifeq ($(CONFIG_MM_UBSAN_ALL),y)

Review Comment:
   add to arm64 and sim too



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");

Review Comment:
   remove



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "sub-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_mul_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "mul-overflow");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_load_invalid_value(FAR void *_data, FAR void *ptr)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "load-invalid-value");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_negate_overflow(FAR void *_data, FAR void *ptr)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");

Review Comment:
   remove



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/Makefile:
##########
@@ -31,6 +31,7 @@ include iob/Make.defs
 include circbuf/Make.defs
 include mempool/Make.defs
 include kasan/Make.defs
+include ubsan/Make.defs

Review Comment:
   Not fixed



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;

Review Comment:
   ```suggestion
     FAR struct unreachable_data *data = _data;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,372 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a)-1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n", reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64,
+              (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64,
+              (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "unreachable");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "nonnull-arg");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "add-overflow");
+  _alert("calling __builtin_unreachable()\n");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  struct unreachable_data *data = _data;
+  ubsan_prologue(&data->location, "sub-overflow");
+  _alert("calling __builtin_unreachable()\n");

Review Comment:
   remove



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] no1wudi commented on a diff in pull request #6881: debug: Introduce portion of UBSan for arm/risc-v

Posted by GitBox <gi...@apache.org>.
no1wudi commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950792510


##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT

Review Comment:
   OK



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] no1wudi commented on a diff in pull request #6881: debug: Introduce portion of UBSan for arm/risc-v

Posted by GitBox <gi...@apache.org>.
no1wudi commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950793625


##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"

Review Comment:
   Not a dup, keep the origin definition with linux.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#issuecomment-1221559186

   let's add a option to enable -fsanitize-undefined-trap-on-error which could save the space significantly:
   ```
   size ubsan-basic-noubsan ubsan-basic ubsan-basic-trap-on-error
      text    data     bss     dec     hex filename
      1894     616       8    2518     9d6 ubsan-basic-noubsan
      2874     944       8    3826     ef2 ubsan-basic
      2054     616       8    2678     a76 ubsan-basic-trap-on-error
   ```
   Report from https://interrupt.memfault.com/blog/ubsan-trap


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 merged pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] no1wudi commented on a diff in pull request #6881: debug: Introduce portion of UBSan for arm/risc-v

Posted by GitBox <gi...@apache.org>.
no1wudi commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950793545


##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))

Review Comment:
   OK



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#issuecomment-1221722384

   > > Let's change _asan_xxx to satisfy the compiler expectation.
   > 
   > OK, let's do this in next commit ?
   
   Yes, but please append to this PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#issuecomment-1221873422

   @no1wudi please create a new PR to enable ubsan on sim:kasan.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] no1wudi commented on a diff in pull request #6881: debug: Introduce portion of UBSan for arm/risc-v

Posted by GitBox <gi...@apache.org>.
no1wudi commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950623250


##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT

Review Comment:
   Enable full UBSan may lead to a huge image that is hard to run on real device, so I think it's better to let the user determine the check item.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950854991


##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",

Review Comment:
   ```suggestion
                " represented in type %s\n",
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",

Review Comment:
   ```suggestion
                "pointer of type %s failed",
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);

Review Comment:
   ```suggestion
                align, offset, data->type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+            align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  FAR struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,

Review Comment:
   ```suggestion
         .alignment       = 1ul << data->log_alignment,
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+            align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  FAR struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)

Review Comment:
   remove loc(and _ from _data) and pass data to ubsan_prologue directly



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,

Review Comment:
   ```suggestion
                                            uintptr_t align, uintptr_t offset)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,

Review Comment:
   ```suggestion
            reason, loc->file_name, loc->line, loc->column);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);

Review Comment:
   ```suggestion
                rhs_val_str, data->type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);

Review Comment:
   ```suggestion
                type_bit_width(lhs_type), lhs_type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);

Review Comment:
   ```suggestion
                lhs_str, rhs_str, lhs_type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+            align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),

Review Comment:
   ```suggestion
            1ul << (real_ptr ? ffsl(real_ptr) : 0),
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+            align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  FAR struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "unreachable");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "nonnull-arg");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "add-overflow");
+  ubsan_epilogue();

Review Comment:
   let's add new function to avoid the code dup:
   ```
   ubsan_prologue_epilogue(data, "add-overflow");
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+            align, data->type->type_name);

Review Comment:
   ```suggestion
                align, data->type->type_name);
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,387 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const char *const g_type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(FAR struct source_location *loc,
+                           FAR const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
+             reason, loc->file_name,
+             loc->line, loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         g_type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+    FAR struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    {
+      handle_null_ptr_deref(data);
+    }
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    {
+      handle_misaligned_access(data, ptr);
+    }
+  else
+    {
+      handle_object_size_mismatch(data, ptr);
+    }
+}
+
+static bool type_is_int(FAR struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(FAR struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(FAR struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(FAR struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(FAR struct type_descriptor *type,
+                              FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(FAR int64_t *)val;
+}
+
+static bool val_is_negative(FAR struct type_descriptor *type, FAR void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(FAR struct type_descriptor *type,
+                                 FAR void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(FAR char *str, size_t size,
+                          FAR struct type_descriptor *type,
+                          FAR void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%" PRId64, get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%" PRIu64, get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(FAR void *_data, FAR void *index)
+{
+  FAR struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(FAR void *_data,
+                                        FAR void *lhs, FAR void *rhs)
+{
+  FAR struct shift_out_of_bounds_data *data = _data;
+  FAR struct type_descriptor *rhs_type = data->rhs_type;
+  FAR struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    {
+      _alert("shift exponent %s is negative\n", rhs_str);
+    }
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    {
+      _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+            type_bit_width(lhs_type), lhs_type->type_name);
+    }
+  else if (val_is_negative(lhs_type, lhs))
+    {
+      _alert("left shift of negative value %s\n", lhs_str);
+    }
+  else
+    {
+      _alert("left shift of %s by %s places cannot be"
+            " represented in type %s\n",
+            lhs_str, rhs_str, lhs_type->type_name);
+    }
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(FAR void *_data,
+                                    FAR void *lhs, FAR void *rhs)
+{
+  FAR struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    {
+      _alert("division of %s by -1 cannot be represented in type %s\n",
+            rhs_val_str, data->type->type_name);
+    }
+  else
+    {
+      _alert("division by zero\n");
+    }
+}
+
+void __ubsan_handle_alignment_assumption(FAR void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  FAR struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    {
+      _alert("assumption of %u byte alignment (with offset of %u byte) for "
+            "pointer of type %s failed",
+            align, offset, data->type->type_name);
+    }
+  else
+    {
+      _alert("assumption of %u byte alignment for pointer of type %s failed",
+            align, data->type->type_name);
+    }
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         ((1UL) << (real_ptr ? __builtin_ctzl(real_ptr) : 0)),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(FAR struct type_mismatch_data *data,
+                                  FAR void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(FAR void *_data, FAR void *ptr)
+{
+  FAR struct type_mismatch_data_v1 *data = _data;
+  struct type_mismatch_data_common common_data =
+  {
+      .location        = &data->location,
+      .type            = data->type,
+      .alignment       = 1UL << data->log_alignment,
+      .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_builtin_unreachable(FAR void *_data)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "unreachable");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_nonnull_arg(FAR void *_data)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "nonnull-arg");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_add_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "add-overflow");
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_sub_overflow(FAR void *_data,
+                                 FAR void *lhs, FAR void *rhs)
+{
+  FAR struct source_location *loc = _data;
+  ubsan_prologue(loc, "sub-overflow");

Review Comment:
   ditto



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950956689


##########
mm/ubsan/Make.defs:
##########
@@ -0,0 +1,28 @@
+############################################################################
+# mm/kasan/Make.defs

Review Comment:
   kasan->ubsan



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6881: debug: Introduce portion of UBSan for arm/risc-v

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950661893


##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT

Review Comment:
   If so, we need list all possible USBSan option which is too boring. Let's add MM_UBSAN_ALL like PR #6885.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] no1wudi commented on pull request #6881: debug: Introduce portion of UBSan

Posted by GitBox <gi...@apache.org>.
no1wudi commented on PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#issuecomment-1221685281

   > Let's change _asan_xxx to satisfy the compiler expectation.
   OK, let's do this in next commit ?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6881: debug: Introduce portion of UBSan for arm/risc-v

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #6881:
URL: https://github.com/apache/incubator-nuttx/pull/6881#discussion_r950114834


##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV

Review Comment:
   remove, other arch may support 



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)

Review Comment:
   remove typeof(x) since c89 doesn't define it



##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT

Review Comment:
   remove, enable by -fsanitize=undefined from:
   https://bcain-llvm.readthedocs.io/projects/clang/en/latest/UndefinedBehaviorSanitizer/



##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT
+	bool "Checking that the result of a shift operation is not undefined"
+	default n
+	---help---
+	-fsanitize=shift
+
+config MM_UBSAN_INTEGER_DIV_ZERO

Review Comment:
   ditto



##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT
+	bool "Checking that the result of a shift operation is not undefined"
+	default n
+	---help---
+	-fsanitize=shift
+
+config MM_UBSAN_INTEGER_DIV_ZERO
+	bool "Checking integer division by zero as well as INT_MIN / -1 division"
+	default n
+	---help---
+	-fsanitize=integer-divide-by-zero
+
+config MM_UBSAN_NULL
+	bool "Checking of a null pointer or creation of a null reference"
+	default n
+	---help---
+	-fsanitize=null
+
+config MM_UBSAN_BOUNDS
+	bool "Checking out of bounds accesses"
+	default n
+	---help---
+	-fsanitize=bounds
+
+config MM_UBSAN_ALIGNMENT

Review Comment:
   ditto



##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT
+	bool "Checking that the result of a shift operation is not undefined"
+	default n
+	---help---
+	-fsanitize=shift
+
+config MM_UBSAN_INTEGER_DIV_ZERO
+	bool "Checking integer division by zero as well as INT_MIN / -1 division"
+	default n
+	---help---
+	-fsanitize=integer-divide-by-zero
+
+config MM_UBSAN_NULL
+	bool "Checking of a null pointer or creation of a null reference"
+	default n
+	---help---
+	-fsanitize=null
+
+config MM_UBSAN_BOUNDS
+	bool "Checking out of bounds accesses"
+	default n

Review Comment:
   n->y



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *type;
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *type;
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;

Review Comment:
   ```suggestion
     FAR struct source_location *location;
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct nonnull_arg_data
+{
+  struct source_location location;
+  struct source_location attr_location;
+  int arg_index;
+};
+
+struct out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *array_type;
+  struct type_descriptor *index_type;
+};
+
+struct shift_out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *lhs_type;
+  struct type_descriptor *rhs_type;
+};
+
+struct unreachable_data
+{
+  struct source_location location;
+};
+
+struct invalid_value_data
+{
+  struct source_location location;
+  struct type_descriptor *type;

Review Comment:
   ditto



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct nonnull_arg_data
+{
+  struct source_location location;
+  struct source_location attr_location;
+  int arg_index;
+};
+
+struct out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *array_type;
+  struct type_descriptor *index_type;
+};
+
+struct shift_out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *lhs_type;
+  struct type_descriptor *rhs_type;
+};
+
+struct unreachable_data
+{
+  struct source_location location;
+};
+
+struct invalid_value_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct alignment_assumption_data
+{
+  struct source_location location;
+  struct source_location assumption_location;
+  struct type_descriptor *type;

Review Comment:
   ditto



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct nonnull_arg_data
+{
+  struct source_location location;
+  struct source_location attr_location;
+  int arg_index;
+};
+
+struct out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *array_type;
+  struct type_descriptor *index_type;
+};
+
+struct shift_out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *lhs_type;
+  struct type_descriptor *rhs_type;

Review Comment:
   ditto



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)

Review Comment:
   ```suggestion
     FAR struct type_mismatch_data_common *data, uintptr_t ptr)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,

Review Comment:
   ```suggestion
   static void handle_misaligned_access(FAR struct type_mismatch_data_common *data,
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%llu",
+            (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(void *_data, void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(void *_data, uintptr_t ptr,
+                                         uintptr_t align,
+                                         uintptr_t offset)
+{
+  struct alignment_assumption_data *data = _data;
+  uintptr_t real_ptr;
+
+  ubsan_prologue(&data->location, "alignment-assumption");
+
+  if (offset)
+    _alert("assumption of %u byte alignment (with offset of %u byte) for "
+           "pointer of type %s failed",
+           align, offset, data->type->type_name);
+  else
+    _alert("assumption of %u byte alignment for pointer of type %s failed",
+           align, data->type->type_name);
+
+  real_ptr = ptr - offset;
+  _alert("%saddress is %lu aligned, misalignment offset is %u bytes",
+         offset ? "offset " : "",
+         BIT(real_ptr ? __builtin_ctzl(real_ptr) : 0),
+         real_ptr & (align - 1));
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
+                                  void *ptr)
+{
+  struct type_mismatch_data_common common_data =
+  {
+    .location        = &data->location,
+    .type            = data->type,
+    .alignment       = data->alignment,
+    .type_check_kind = data->type_check_kind
+  };
+
+  ubsan_type_mismatch_common(&common_data, (uintptr_t)ptr);
+}
+
+void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)

Review Comment:
   ```suggestion
   void __ubsan_handle_type_mismatch_v1(FAR void *data_, FAR void *ptr)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);

Review Comment:
   add {}



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)

Review Comment:
   add FAR for all pointers



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%llu",
+            (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(void *_data, void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
+{
+  struct overflow_data *data = _data;
+  char rhs_val_str[40];
+
+  ubsan_prologue(&data->location, "division-overflow");
+
+  val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
+
+  if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
+    _alert("division of %s by -1 cannot be represented in type %s\n",
+           rhs_val_str, data->type->type_name);
+  else
+    _alert("division by zero\n");
+}
+
+void __ubsan_handle_alignment_assumption(void *_data, uintptr_t ptr,

Review Comment:
   ```suggestion
   void __ubsan_handle_alignment_assumption(FAR void *data_, uintptr_t ptr,
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%llu",
+            (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(void *_data, void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)

Review Comment:
   ```suggestion
   void __ubsan_handle_shift_out_of_bounds(FAR void *data, FAR void *lhs, FAR void *rhs)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));

Review Comment:
   ```suggestion
                    (int64_t)get_signed_val(type, value));
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;

Review Comment:
   add FAR for all pointer



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =

Review Comment:
   ```suggestion
   static FAR const *const g_type_check_kinds[] =
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%llu",
+            (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(void *_data, void *index)

Review Comment:
   ```suggestion
   void __ubsan_handle_out_of_bounds(FAR void *data_, FAR void *index)
   ```



##########
mm/Makefile:
##########
@@ -31,6 +31,7 @@ include iob/Make.defs
 include circbuf/Make.defs
 include mempool/Make.defs
 include kasan/Make.defs
+include ubsan/Make.defs

Review Comment:
   let's enable UBSAN in ./boards/sim/sim/sim/configs/kasan



##########
arch/risc-v/src/common/Toolchain.defs:
##########
@@ -67,6 +67,26 @@ ifeq ($(CONFIG_ARCH_COVERAGE),y)
   ARCHOPTIMIZATION += -fprofile-generate -ftest-coverage
 endif
 
+ifeq ($(CONFIG_MM_UBSAN_SHIFT),y)
+  ARCHOPTIMIZATION += -fsanitize=shift
+endif
+
+ifeq ($(CONFIG_MM_UBSAN_INTEGER_DIV_ZERO),y)
+  ARCHOPTIMIZATION += -fsanitize=integer-divide-by-zero
+endif
+
+ifeq ($(CONFIG_MM_UBSAN_NULL),y)
+  ARCHOPTIMIZATION += -fsanitize=null
+endif
+
+ifeq ($(CONFIG_MM_UBSAN_BOUNDS),y)
+  ARCHOPTIMIZATION += -fsanitize=bounds-strict
+endif
+
+ifeq ($(CONFIG_MM_UBSAN_ALIGNMENT),y)
+  ARCHOPTIMIZATION += -fsanitize=alignment
+endif

Review Comment:
   add to xtensa too



##########
arch/arm/src/arm/Toolchain.defs:
##########
@@ -83,6 +83,26 @@ ifeq ($(CONFIG_MM_KASAN),y)
   ARCHOPTIMIZATION += -fsanitize=kernel-address
 endif
 

Review Comment:
   let's add the master switch:
   ifeq ($(CONFIG_MM_UBSAN),y)
     ARCHOPTIMIZATION += -fsanitize=undefined
   endif



##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN

Review Comment:
   let's add MM_UBSAN_TRAP for -fsanitize-undefined-trap-on-error to save the code space



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct nonnull_arg_data
+{
+  struct source_location location;
+  struct source_location attr_location;
+  int arg_index;
+};
+
+struct out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *array_type;
+  struct type_descriptor *index_type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *index_type;
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct nonnull_arg_data
+{
+  struct source_location location;
+  struct source_location attr_location;
+  int arg_index;
+};
+
+struct out_of_bounds_data
+{
+  struct source_location location;
+  struct type_descriptor *array_type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *array_type;
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *type;
   ```



##########
mm/Kconfig:
##########
@@ -191,6 +191,49 @@ config MM_KASAN
 		bugs in native code. After turn on this option, Please
 		add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
 
+config MM_UBSAN
+	bool "Undefined Behavior Sanitizer"
+	default n
+	depends on ARCH_ARM || ARCH_RISCV
+	---help---
+		UBSan is a fast undefined behavior detector. UBSan modifies
+		the program at compile-time to catch various kinds of
+		undefined behavior during program execution
+
+if MM_UBSAN
+
+config MM_UBSAN_SHIFT
+	bool "Checking that the result of a shift operation is not undefined"
+	default n
+	---help---
+	-fsanitize=shift
+
+config MM_UBSAN_INTEGER_DIV_ZERO
+	bool "Checking integer division by zero as well as INT_MIN / -1 division"
+	default n
+	---help---
+	-fsanitize=integer-divide-by-zero
+
+config MM_UBSAN_NULL

Review Comment:
   ditto



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)

Review Comment:
   ```suggestion
   static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
   ```



##########
mm/ubsan/ubsan.h:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __MM_UBSAN_UBSAN_H
+#define __MM_UBSAN_UBSAN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum type_kind_e
+{
+  TYPE_KIND_INT     = 0,
+  TYPE_KIND_FLOAT   = 1,
+  TYPE_KIND_UNKNOWN = 0xffff
+};
+
+struct type_descriptor
+{
+  uint16_t type_kind;
+  uint16_t type_info;
+  char type_name[1];
+};
+
+struct source_location
+{
+  const char *file_name;
+  union
+    {
+      unsigned long reported;
+      struct
+        {
+          uint32_t line;
+          uint32_t column;
+        };
+    };
+};
+
+struct overflow_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+};
+
+struct type_mismatch_data
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned long alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_v1
+{
+  struct source_location location;
+  struct type_descriptor *type;
+  unsigned char log_alignment;
+  unsigned char type_check_kind;
+};
+
+struct type_mismatch_data_common
+{
+  struct source_location *location;
+  struct type_descriptor *type;

Review Comment:
   ```suggestion
     FAR struct type_descriptor *type;
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))

Review Comment:
   only one user, let's expend at caller instead?



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)

Review Comment:
   ```suggestion
   static void ubsan_prologue(FAR struct source_location *loc, FAR const char *reason)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"

Review Comment:
   dup?



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)

Review Comment:
   ```suggestion
     FAR struct type_mismatch_data_common *data, uintptr_t ptr)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%llu",
+            (uint64_t)get_unsigned_val(type, value));
+        }
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void __ubsan_handle_out_of_bounds(void *_data, void *index)
+{
+  struct out_of_bounds_data *data = _data;
+  char index_str[40];
+
+  ubsan_prologue(&data->location, "array-index-out-of-bounds");
+
+  val_to_string(index_str, sizeof(index_str), data->index_type, index);
+  _alert("index %s is out of range for type %s\n", index_str,
+         data->array_type->type_name);
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
+{
+  struct shift_out_of_bounds_data *data = _data;
+  struct type_descriptor *rhs_type = data->rhs_type;
+  struct type_descriptor *lhs_type = data->lhs_type;
+  char rhs_str[40];
+  char lhs_str[40];
+
+  ubsan_prologue(&data->location, "shift-out-of-bounds");
+
+  val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
+  val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
+
+  if (val_is_negative(rhs_type, rhs))
+    _alert("shift exponent %s is negative\n", rhs_str);
+
+  else if (get_unsigned_val(rhs_type, rhs) >= type_bit_width(lhs_type))
+    _alert("shift exponent %s is too large for %u-bit type %s\n", rhs_str,
+           type_bit_width(lhs_type), lhs_type->type_name);
+  else if (val_is_negative(lhs_type, lhs))
+    _alert("left shift of negative value %s\n", lhs_str);
+  else
+    _alert("left shift of %s by %s places cannot be"
+           " represented in type %s\n",
+           lhs_str, rhs_str, lhs_type->type_name);
+
+  ubsan_epilogue();
+}
+
+void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)

Review Comment:
   ```suggestion
   void __ubsan_handle_divrem_overflow(FAR void *data_, FAR void *lhs, FAR void *rhs)
   ```



##########
mm/ubsan/ubsan.c:
##########
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * mm/ubsan/ubsan.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <stdio.h>
+
+#include "ubsan.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
+
+#define BIT(nr) ((1UL) << (nr))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const char *const type_check_kinds[] =
+{
+  "load of",
+  "store to",
+  "reference binding to",
+  "member access within",
+  "member call on",
+  "constructor call on",
+  "downcast of",
+  "downcast of"
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void ubsan_prologue(struct source_location *loc, const char *reason)
+{
+  _alert("========================================"
+         "========================================\n");
+  _alert("UBSAN: %s in %s:%lu:%lu\n", reason, loc->file_name, loc->line,
+         loc->column);
+}
+
+static void ubsan_epilogue(void)
+{
+  _alert("========================================"
+         "========================================\n");
+}
+
+static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
+{
+  ubsan_prologue(data->location, "null-pointer-dereference");
+
+  _alert("%s null pointer of type %s\n",
+         type_check_kinds[data->type_check_kind], data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void handle_misaligned_access(struct type_mismatch_data_common *data,
+                                     uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "misaligned-access");
+
+  _alert("%s misaligned address %p for type %s\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr,
+         data->type->type_name);
+  _alert("which requires %ld byte alignment\n", data->alignment);
+
+  ubsan_epilogue();
+}
+
+static void handle_object_size_mismatch(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  ubsan_prologue(data->location, "object-size-mismatch");
+
+  _alert("%s address %p with insufficient space\n",
+         type_check_kinds[data->type_check_kind], (void *)ptr);
+  _alert("for an object of type %s\n", data->type->type_name);
+
+  ubsan_epilogue();
+}
+
+static void ubsan_type_mismatch_common(
+  struct type_mismatch_data_common *data, uintptr_t ptr)
+{
+  if (!ptr)
+    handle_null_ptr_deref(data);
+  else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
+    handle_misaligned_access(data, ptr);
+  else
+    handle_object_size_mismatch(data, ptr);
+}
+
+static bool type_is_int(struct type_descriptor *type)
+{
+  return type->type_kind == TYPE_KIND_INT;
+}
+
+static bool type_is_signed(struct type_descriptor *type)
+{
+  return type->type_info & 1;
+}
+
+static unsigned type_bit_width(struct type_descriptor *type)
+{
+  return 1 << (type->type_info >> 1);
+}
+
+static bool is_inline_int(struct type_descriptor *type)
+{
+  unsigned inline_bits = sizeof(uintptr_t) * 8;
+  unsigned bits = type_bit_width(type);
+
+  return bits <= inline_bits;
+}
+
+static int64_t get_signed_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      unsigned extra_bits = sizeof(int64_t) * 8 - type_bit_width(type);
+      uintptr_t ulong_val = (uintptr_t)val;
+
+      return ((int64_t)ulong_val) << extra_bits >> extra_bits;
+    }
+
+  return *(int64_t *)val;
+}
+
+static bool val_is_negative(struct type_descriptor *type, void *val)
+{
+  return type_is_signed(type) && get_signed_val(type, val) < 0;
+}
+
+static uint64_t get_unsigned_val(struct type_descriptor *type, void *val)
+{
+  if (is_inline_int(type))
+    {
+      return (uintptr_t)val;
+    }
+
+  return *(uint64_t *)val;
+}
+
+static void val_to_string(char *str, size_t size,
+                          struct type_descriptor *type,
+                          void *value)
+{
+  if (type_is_int(type))
+    {
+      if (type_is_signed(type))
+        {
+          snprintf(str, size, "%lld",
+            (int64_t)get_signed_val(type, value));
+        }
+      else
+        {
+          snprintf(str, size, "%llu",
+            (uint64_t)get_unsigned_val(type, value));

Review Comment:
   ```suggestion
                    (uint64_t)get_unsigned_val(type, value));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org