aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYordan Karadzhov (VMware) <y.karadz@gmail.com>2021-04-28 16:47:24 +0300
committerYordan Karadzhov (VMware) <y.karadz@gmail.com>2021-05-11 15:49:29 +0300
commite489a3b247aae8a3a556504ce6bf0da37190a99b (patch)
tree62c2889247e711f7599d55d669bb111fb5601022
parentebbb8feabb22dc6697dc92f25722e0c3c261b6ea (diff)
downloadkernel-shark-e489a3b247aae8a3a556504ce6bf0da37190a99b.tar.gz
kernel-shark: Fix KS_DEFINE_PLUGIN_CONTEXT macro
The KS_DEFINE_PLUGIN_CONTEXT macro implements methods that are used to deal with plugin-specific context objects. However, the methods defined by the macro (as it is now) are static and cannot be used in plugins, built from multiple source files. If we want the methods to be declared in a header and used in different source files, we have to make sure that those methods are not visible outside of the corresponding plugin. The opposite can be dangerous in the case when the macro is used in multiple plugins and those plugins are loaded together. In such a case the symbol resolving will fail, resulting in undefined behavior. Link: https://lore.kernel.org/linux-trace-devel/20210428134730.187533-4-y.karadz@gmail.com Fixing: 15df009 (kernel-shark: Add KS_DEFINE_PLUGIN_CONTEXT macro) Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
-rw-r--r--src/libkshark-plugin.h22
-rw-r--r--src/plugins/sched_events.c3
-rw-r--r--src/plugins/sched_events.h3
3 files changed, 22 insertions, 6 deletions
diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
index c1106163..752dbeb2 100644
--- a/src/libkshark-plugin.h
+++ b/src/libkshark-plugin.h
@@ -24,6 +24,8 @@ extern "C" {
/* Quiet warnings over documenting simple structures */
//! @cond Doxygen_Suppress
+#define __hidden __attribute__((visibility ("hidden")))
+
#define _MAKE_STR(x) #x
#define MAKE_STR(x) _MAKE_STR(x)
@@ -364,11 +366,14 @@ int kshark_handle_all_dpis(struct kshark_data_stream *stream,
__ok; \
}) \
-/** General purpose macro defining methods for adding plugin context. */
+/**
+ * General purpose macro defining methods for adding plugin context.
+ * Do not use this macro in header files.
+ */
#define KS_DEFINE_PLUGIN_CONTEXT(type) \
static type **__context_handler; \
static ssize_t __n_streams = -1; \
-static inline type *__init(int sd) \
+__hidden type *__init(int sd) \
{ \
type *obj; \
if (__n_streams < 0 && sd < KS_DEFAULT_NUM_STREAMS) { \
@@ -388,7 +393,7 @@ static inline type *__init(int sd) \
__context_handler[sd] = obj; \
return obj; \
} \
-static inline void __close(int sd) \
+__hidden void __close(int sd) \
{ \
if (sd < 0) { \
free(__context_handler); \
@@ -398,13 +403,22 @@ static inline void __close(int sd) \
free(__context_handler[sd]); \
__context_handler[sd] = NULL; \
} \
-static inline type *__get_context(int sd) \
+__hidden type *__get_context(int sd) \
{ \
if (sd < 0 || sd >= __n_streams) \
return NULL; \
return __context_handler[sd]; \
} \
+/**
+ * General purpose macro declaring the methods for adding plugin context.
+ * To be used in header files.
+ */
+#define KS_DECLARE_PLUGIN_CONTEXT_METHODS(type) \
+type *__init(int sd); \
+void __close(int sd); \
+type *__get_context(int sd); \
+
#ifdef __cplusplus
}
#endif // __cplusplus
diff --git a/src/plugins/sched_events.c b/src/plugins/sched_events.c
index ac4a7bf4..57983225 100644
--- a/src/plugins/sched_events.c
+++ b/src/plugins/sched_events.c
@@ -73,6 +73,9 @@ int plugin_sched_get_prev_state(ks_num_field_t field)
return (field & mask) >> PREV_STATE_SHIFT;
}
+/** A general purpose macro is used to define plugin context. */
+KS_DEFINE_PLUGIN_CONTEXT(struct plugin_sched_context);
+
static bool plugin_sched_init_context(struct kshark_data_stream *stream,
struct plugin_sched_context *plugin_ctx)
{
diff --git a/src/plugins/sched_events.h b/src/plugins/sched_events.h
index 78cfda02..2c540fd1 100644
--- a/src/plugins/sched_events.h
+++ b/src/plugins/sched_events.h
@@ -53,8 +53,7 @@ struct plugin_sched_context {
struct kshark_data_container *sw_data;
};
-/** A general purpose macro is used to define plugin context. */
-KS_DEFINE_PLUGIN_CONTEXT(struct plugin_sched_context);
+KS_DECLARE_PLUGIN_CONTEXT_METHODS(struct plugin_sched_context)
/** The type of the data field stored in the kshark_data_container object. */
typedef int64_t ks_num_field_t;