diff mbox series

[1/7] xen/ubsan: provide helper for clang's -fsanitize=function

Message ID 20250313153029.93347-2-roger.pau@citrix.com (mailing list archive)
State New
Headers show
Series x86/ubsan: fix ubsan on clang + code fixes | expand

Commit Message

Roger Pau Monné March 13, 2025, 3:30 p.m. UTC
clang's -fsanitize=function relies on the presence of
__ubsan_handle_function_type_mismatch() to print the detection of indirect
calls of a function through a function pointer of the wrong type.

Implement the helper, inspired on the llvm ubsan lib implementation.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
 xen/common/ubsan/ubsan.c | 16 ++++++++++++++++
 xen/common/ubsan/ubsan.h |  5 +++++
 2 files changed, 21 insertions(+)

Comments

Andrew Cooper March 13, 2025, 5:18 p.m. UTC | #1
On 13/03/2025 3:30 pm, Roger Pau Monne wrote:
> clang's -fsanitize=function relies on the presence of
> __ubsan_handle_function_type_mismatch() to print the detection of indirect
> calls of a function through a function pointer of the wrong type.
>
> Implement the helper, inspired on the llvm ubsan lib implementation.
>
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

It's weird, but we're now ahead of Linux by two sanitisers (this, and
invalid_builtin visible in context).

~Andrew
diff mbox series

Patch

diff --git a/xen/common/ubsan/ubsan.c b/xen/common/ubsan/ubsan.c
index e99370322b44..7ebe4bfc14dc 100644
--- a/xen/common/ubsan/ubsan.c
+++ b/xen/common/ubsan/ubsan.c
@@ -546,3 +546,19 @@  void __ubsan_handle_invalid_builtin(struct invalid_builtin_data *data)
 
 	ubsan_epilogue(&flags);
 }
+
+void __ubsan_handle_function_type_mismatch(
+	struct function_type_mismatch_data *data, unsigned long val)
+{
+	unsigned long flags;
+
+	if (suppress_report(&data->location))
+		return;
+
+	ubsan_prologue(&data->location, &flags);
+
+	pr_err("call to function %ps through pointer to incorrect function type %s\n",
+		(void *)val, data->type->type_name);
+
+	ubsan_epilogue(&flags);
+}
diff --git a/xen/common/ubsan/ubsan.h b/xen/common/ubsan/ubsan.h
index 9c7f3b9b6c07..8987f9d45397 100644
--- a/xen/common/ubsan/ubsan.h
+++ b/xen/common/ubsan/ubsan.h
@@ -95,6 +95,11 @@  enum {
 	kind_clz,
 };
 
+struct function_type_mismatch_data {
+	struct source_location location;
+	struct type_descriptor *type;
+};
+
 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
 typedef __int128 s_max;
 typedef unsigned __int128 u_max;