[PATCH v3 linux-kselftest-test 3/6] kunit: add kunit_find_symbol() function for symbol lookup

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

 



In preparation for module support for kunit and kunit tests,
we need a way of retrieving non-exported symbols from the
core kernel and modules.  kunit_find_symbol() supports this.

Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx>
Signed-off-by: Knut Omang <knut.omang@xxxxxxxxxx>
---
 include/kunit/test.h  |  8 ++++++++
 lib/kunit/test-test.c | 19 +++++++++++++++++++
 lib/kunit/test.c      | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index dba4830..c645d18 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -339,6 +339,14 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
 
 void kunit_cleanup(struct kunit *test);
 
+/**
+ * kunit_find_symbol() - lookup un-exported symbol in kernel or modules.
+ * @sym: symbol name.
+ *
+ * Returns symbol or ERR_PTR value on error.
+ */
+void *kunit_find_symbol(const char *sym);
+
 #define kunit_printk(lvl, test, fmt, ...) \
 	printk(lvl "\t# %s: " fmt, (test)->name, ##__VA_ARGS__)
 
diff --git a/lib/kunit/test-test.c b/lib/kunit/test-test.c
index c4162a9..7f09dd0 100644
--- a/lib/kunit/test-test.c
+++ b/lib/kunit/test-test.c
@@ -330,3 +330,22 @@ static void kunit_resource_test_exit(struct kunit *test)
 	.test_cases = kunit_resource_test_cases,
 };
 kunit_test_suite(kunit_resource_test_suite);
+
+/*
+ * Find non-exported kernel symbol; we use the modules list as a safe
+ * choice that should always be present.
+ */
+static void kunit_find_symbol_kernel(struct kunit *test)
+{
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, kunit_find_symbol("modules"));
+}
+
+static struct kunit_case kunit_find_symbol_test_cases[] = {
+	KUNIT_CASE(kunit_find_symbol_kernel),
+};
+
+static struct kunit_suite kunit_find_symbol_test_suite = {
+	.name = "kunit-find-symbol",
+	.test_cases = kunit_find_symbol_test_cases,
+};
+kunit_test_suite(kunit_find_symbol_test_suite);
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 49ac5fe..a2b1b46 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -8,6 +8,7 @@
 
 #include <kunit/test.h>
 #include <kunit/try-catch.h>
+#include <linux/kallsyms.h>
 #include <linux/kernel.h>
 #include <linux/sched/debug.h>
 #include "string-stream-impl.h"
@@ -478,3 +479,38 @@ void kunit_cleanup(struct kunit *test)
 		kunit_resource_free(test, resource);
 	}
 }
+
+/*
+ * Support for looking up kernel/module internal symbols to enable testing.
+ */
+void *kunit_find_symbol(const char *sym)
+{
+	unsigned long (*modlookup)(const char *name);
+	unsigned long addr = 0;
+
+	if (!sym || strlen(sym) > KSYM_NAME_LEN)
+		return ERR_PTR(-EINVAL);
+
+	/*
+	 * Try for kernel-internal symbol first; fall back to modules
+	 * if that fails.
+	 */
+	addr = kallsyms_lookup_name(sym);
+	if (addr)
+		return (void *)addr;
+	modlookup = (void *)kallsyms_lookup_name("module_kallsyms_lookup_name");
+	if (modlookup)
+		addr = modlookup(sym);
+	if (addr)
+		return (void *)addr;
+
+#ifndef CONFIG_KALLSYMS_ALL
+	WARN_ONCE(true,
+		  "CONFIG_KALLSYMS_ALL is not set, so unexported symbols like '%s' are not available\n",
+		  sym);
+	return ERR_PTR(-ENOTSUPP);
+#else
+	WARN_ONCE(true, "symbol '%s' is not available\n", sym);
+#endif
+	return ERR_PTR(-ENOENT);
+}
-- 
1.8.3.1




[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux