From 4f4846e9351997094eb9257cec83a9019da4cd0c Mon Sep 17 00:00:00 2001
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Date: Mon, 17 Feb 2020 18:33:12 -0500
Subject: [PATCH 1/1] Fix: plugin-dev.h: Disable address sanitizer on pointer
 array section variables

The plugin header declares pointer variables in plugins meant to be
placed contiguously within our own sections, and then used as an array
of pointers when loading the plugin.

Address Sanitizer adds redzones around each variable, thus leading to
detection of a global buffer overflow.

Those redzones should not be placed within this section, because it
defeats its purpose. Therefore, teach asan not to add redzones
around those variables with an attribute.

Fixes: #1231

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
 include/babeltrace2/plugin/plugin-dev.h | 30 ++++++++++++++++++-------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/include/babeltrace2/plugin/plugin-dev.h b/include/babeltrace2/plugin/plugin-dev.h
index 71372536..2b358f6b 100644
--- a/include/babeltrace2/plugin/plugin-dev.h
+++ b/include/babeltrace2/plugin/plugin-dev.h
@@ -2660,13 +2660,20 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
 	};								\
 	static struct __bt_plugin_component_class_descriptor_attribute const * const __bt_plugin_##_type##_component_class_descriptor_attribute_##_id##_##_component_class_id##_##_attr_name##_ptr __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTES_ATTRS = &__bt_plugin_##_type##_component_class_descriptor_attribute_##_id##_##_component_class_id##_##_attr_name
 
+#if defined(__clang__) || defined (__GNUC__)
+# define __BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
+#else
+# define __BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
+#endif
+
 /*
  * Variable attributes for a plugin descriptor pointer to be added to
  * the plugin descriptor section (internal use).
  */
 #ifdef __APPLE__
 #define __BT_PLUGIN_DESCRIPTOR_ATTRS \
-	__attribute__((section("__DATA,btp_desc"), used))
+	__attribute__((section("__DATA,btp_desc"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_DESCRIPTOR_BEGIN_SYMBOL \
 	__start___bt_plugin_descriptors
@@ -2683,7 +2690,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
 #else
 
 #define __BT_PLUGIN_DESCRIPTOR_ATTRS \
-	__attribute__((section("__bt_plugin_descriptors"), used))
+	__attribute__((section("__bt_plugin_descriptors"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_DESCRIPTOR_BEGIN_SYMBOL \
 	__start___bt_plugin_descriptors
@@ -2702,7 +2710,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
  */
 #ifdef __APPLE__
 #define __BT_PLUGIN_DESCRIPTOR_ATTRIBUTES_ATTRS \
-	__attribute__((section("__DATA,btp_desc_att"), used))
+	__attribute__((section("__DATA,btp_desc_att"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_DESCRIPTOR_ATTRIBUTES_BEGIN_SYMBOL \
 	__start___bt_plugin_descriptor_attributes
@@ -2719,7 +2728,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
 #else
 
 #define __BT_PLUGIN_DESCRIPTOR_ATTRIBUTES_ATTRS \
-	__attribute__((section("__bt_plugin_descriptor_attributes"), used))
+	__attribute__((section("__bt_plugin_descriptor_attributes"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_DESCRIPTOR_ATTRIBUTES_BEGIN_SYMBOL \
 	__start___bt_plugin_descriptor_attributes
@@ -2738,7 +2748,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
  */
 #ifdef __APPLE__
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRS \
-	__attribute__((section("__DATA,btp_cc_desc"), used))
+	__attribute__((section("__DATA,btp_cc_desc"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_BEGIN_SYMBOL \
 	__start___bt_plugin_component_class_descriptors
@@ -2755,7 +2766,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
 #else
 
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRS \
-	__attribute__((section("__bt_plugin_component_class_descriptors"), used))
+	__attribute__((section("__bt_plugin_component_class_descriptors"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_BEGIN_SYMBOL \
 	__start___bt_plugin_component_class_descriptors
@@ -2775,7 +2787,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
  */
 #ifdef __APPLE__
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTES_ATTRS \
-	__attribute__((section("__DATA,btp_cc_desc_att"), used))
+	__attribute__((section("__DATA,btp_cc_desc_att"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTES_BEGIN_SYMBOL \
 	__start___bt_plugin_component_class_descriptor_attributes
@@ -2792,7 +2805,8 @@ struct __bt_plugin_component_class_descriptor_attribute const * const *__bt_get_
 #else
 
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTES_ATTRS \
-	__attribute__((section("__bt_plugin_component_class_descriptor_attributes"), used))
+	__attribute__((section("__bt_plugin_component_class_descriptor_attributes"), used)) \
+	__BT_PLUGIN_ATTRIBUTE_NO_SANITIZE_ADDRESS
 
 #define __BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTES_BEGIN_SYMBOL \
 	__start___bt_plugin_component_class_descriptor_attributes
-- 
2.17.1

