[PATCH v2 09/12] selftests/livepatch: Test where klp-prepare-patch adds #line directives

From
Song Liu <song@kernel.org>
Date
2026-10-28 21:46:24
Also in
linux-kselftest, lkml
Reply to
Song Liu, 2026-10-28
Add a test of where #line directives go:

- each group of changed lines in a hunk gets its own directive
- after removed lines, the directive skips over them
- files other than C sources and headers get none
- lines added at the end of a file get none, as nothing after them
  needs renumbering

Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
 .../livepatch/test_klp_build/Makefile         |   3 +-
 .../test_klp_build/test-line-directives.sh    | 175 ++++++++++++++++++
 2 files changed, 177 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh
diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 2818264fb31a..902a62bfca86 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/Makefile
+++ b/tools/testing/selftests/livepatch/test_klp_build/Makefile
@@ -4,7 +4,8 @@ TEST_PROGS := \
 	test-basic.sh \
 	test-independent-patches.sh \
 	test-cancelling-patches.sh \
-	test-new-and-deleted-files.sh
+	test-new-and-deleted-files.sh \
+	test-line-directives.sh
 
 TEST_PROGS_EXTENDED := functions.sh
 
diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh b/tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh
new file mode 100755
index 000000000000..9f599e64c330
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-line-directives.sh
@@ -0,0 +1,175 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(dirname "$0")/functions.sh"
+
+setup_tree
+
+# - each group of changed lines in a hunk gets its own #line directive
+
+start_test "two groups of changes in one hunk"
+
+add_file drivers/foo/groups.c <<'EOT'
+int foo(int x)
+{
+	int y = 0;
+
+	if (x < 0)
+		y = -1;
+	if (x > 100)
+		y = 100;
+	return y;
+}
+EOT
+
+add_patch 0001-groups.patch <<'EOT'
+--- a/drivers/foo/groups.c
++++ b/drivers/foo/groups.c
+@@ -1,9 +1,11 @@
+ int foo(int x)
+ {
+-	int y = 0;
++	int y = x;
+
+ 	if (x < 0)
+ 		y = -1;
++	else if (!x)
++		y = 1;
+ 	if (x > 100)
+ 		y = 100;
+ 	return y;
+EOT
+
+run_klp_prepare_patch 0001-groups.patch
+
+expect_patch <<'EOT'
+--- a/drivers/foo/groups.c
++++ b/drivers/foo/groups.c
+@@ -1,9 +1,13 @@
+ int foo(int x)
+ {
+-	int y = 0;
++	int y = x;
++#line 4
+
+ 	if (x < 0)
+ 		y = -1;
++	else if (!x)
++		y = 1;
++#line 7
+ 	if (x > 100)
+ 		y = 100;
+ 	return y;
+EOT
+
+# - after removed lines, the #line directive skips over them
+
+start_test "removed lines"
+
+add_file drivers/foo/remove.c <<'EOT'
+int foo(int x)
+{
+	if (x < 0)
+		return -1;
+	return x;
+}
+EOT
+
+add_patch 0001-remove.patch <<'EOT'
+--- a/drivers/foo/remove.c
++++ b/drivers/foo/remove.c
+@@ -1,6 +1,4 @@
+ int foo(int x)
+ {
+-	if (x < 0)
+-		return -1;
+ 	return x;
+ }
+EOT
+
+run_klp_prepare_patch 0001-remove.patch
+
+expect_patch <<'EOT'
+--- a/drivers/foo/remove.c
++++ b/drivers/foo/remove.c
+@@ -1,6 +1,5 @@
+ int foo(int x)
+ {
+-	if (x < 0)
+-		return -1;
++#line 5
+ 	return x;
+ }
+EOT
+
+# - only C sources and headers get #line directives
+
+start_test "change to a Makefile"
+
+add_file drivers/foo/Makefile <<'EOT'
+obj-y += foo.o
+obj-y += groups.o
+EOT
+
+add_patch 0001-makefile.patch <<'EOT'
+--- a/drivers/foo/Makefile
++++ b/drivers/foo/Makefile
+@@ -1,2 +1,3 @@
+ obj-y += foo.o
++obj-y += new.o
+ obj-y += groups.o
+EOT
+
+run_klp_prepare_patch 0001-makefile.patch
+
+expect_patch <<'EOT'
+--- a/drivers/foo/Makefile
++++ b/drivers/foo/Makefile
+@@ -1,2 +1,3 @@
+ obj-y += foo.o
++obj-y += new.o
+ obj-y += groups.o
+EOT
+
+# - lines added at the end of a file have nothing after them to renumber
+
+start_test "lines added at the end of a file"
+
+add_file drivers/foo/tail.c <<'EOT'
+int foo(void)
+{
+	return 0;
+}
+EOT
+
+add_patch 0001-tail.patch <<'EOT'
+--- a/drivers/foo/tail.c
++++ b/drivers/foo/tail.c
+@@ -2,3 +2,8 @@ int foo(void)
+ {
+ 	return 0;
+ }
++
++int bar(void)
++{
++	return 1;
++}
+EOT
+
+run_klp_prepare_patch 0001-tail.patch
+
+expect_patch <<'EOT'
+--- a/drivers/foo/tail.c
++++ b/drivers/foo/tail.c
+@@ -2,3 +2,8 @@ int foo(void)
+ {
+ 	return 0;
+ }
++
++int bar(void)
++{
++	return 1;
++}
+EOT
+
+exit 0
-- 
2.53.0-Meta
Thread (18 messages) 18 messages, 4 authors, last reply 4d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help