Skip to contentSkip to conversation

tests/utils.c: fix a memory corruption in find_word

PATCH In review Alexander Kanavin 3 participants 7 messages Sep 16, 2021 – Sep 23, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 tests/utils.c: fix a memory corruption in find_word

I also took the opportunity to correct a weird API that returns a result (or not), depending on some internal condition.

---
 tests/utils.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
tests/utils.c +18 -17
index 8fffc18..19657ee 100644
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
+#include <stdbool.h>
 
 #include <check.h>
 
@@ -61,16 +62,13 @@ static char *ptests_not_found[] = {
 
 static struct ptest_options EmptyOpts;
 
-static inline void
-find_word(int *found, const char *line, const char *word)
+static inline bool
+find_word(const char *line, const char *word)
 {
-
-	char *pivot = NULL;
-
-	pivot = strdup(line);
-	pivot[strlen(word)] = '\0';
-	if (strcmp(pivot, word) == 0) { *found = 1; }
-	free(pivot);
+	if (strncmp(line, word, strlen(word)) == 0)
+                return true;
+        else
+                return false;
 }
 
 static void test_ptest_expected_failure(struct ptest_list *, const unsigned int, char *,
@@ -206,18 +204,19 @@ search_for_timeout_and_duration(const int rp, FILE *fp_stdout)
 	const char *timeout_str = "TIMEOUT";
 	const char *duration_str = "DURATION";
 	char line_buf[PRINT_PTEST_BUF_SIZE];
-	int found_timeout = 0, found_duration = 0;
+	bool found_timeout = false, found_duration = false;
 	char *line = NULL;
 
 	ck_assert(rp != 0);
 
 	while ((line = fgets(line_buf, PRINT_PTEST_BUF_SIZE, fp_stdout)) != NULL) {
-		find_word(&found_timeout, line, timeout_str);
-		find_word(&found_duration, line, duration_str);
+		// once true, stay true
+		found_timeout = found_timeout ? found_timeout : find_word(line, timeout_str);
+		found_duration = found_duration ? found_duration : find_word(line, duration_str);
 	}
 
-	ck_assert(found_timeout == 1);
-	ck_assert(found_duration == 1);
+	ck_assert(found_timeout == true);
+	ck_assert(found_duration == true);
 }
 
 START_TEST(test_run_timeout_duration_ptest)
@@ -236,16 +235,18 @@ search_for_fail(const int rp, FILE *fp_stdout)
 {
         const char *fail_str = "ERROR: Exit status is 10";
         char line_buf[PRINT_PTEST_BUF_SIZE];
-        int found_fail = 0;
+        int found_fail = false;
         char *line = NULL;
 
         ck_assert(rp != 0);
 
         while ((line = fgets(line_buf, PRINT_PTEST_BUF_SIZE, fp_stdout)) != NULL) {
-		find_word(&found_fail, line, fail_str);
+		found_fail = find_word(line, fail_str);
+		if (found_fail == true)
+			break;
         }
 
-        ck_assert(found_fail == 1);
+        ck_assert(found_fail == true);
 }
 
 START_TEST(test_run_fail_ptest)
Signature · 1 line
2.33.0
#1 lore raw

PATCH 2/3 utils.c: handle test timeouts directly with poll()

if poll()'s timeout expires that means the test did not produce any output, which is exactly what we need to catch.

So there's no need to set up separate timeouts with signals and alarms, and this greatly simplifies more sophisticated processing of hanging tests (such as collecting overall system data).

---
 utils.c | 34 ++++++++++------------------------
 1 file changed, 10 insertions(+), 24 deletions(-)
utils.c +10 -24
index 128ff61..58c3aa1 100644
@@ -51,7 +51,6 @@
 #include "utils.h"
 
 #define GET_STIME_BUF_SIZE 1024
-#define WAIT_CHILD_POLL_TIMEOUT_MS 200
 #define WAIT_CHILD_BUF_MAX_SIZE 1024
 
 #define UNUSED(x) (void)(x)
@@ -296,7 +295,7 @@ read_child(void *arg)
 	pfds[1].events = POLLIN;
 
 	do {
-		r = poll(pfds, 2, WAIT_CHILD_POLL_TIMEOUT_MS);
+		r = poll(pfds, 2, _child_reader.timeout*1000);
 		if (r > 0) {
 			char buf[WAIT_CHILD_BUF_MAX_SIZE];
 			ssize_t n;
@@ -313,10 +312,10 @@ read_child(void *arg)
 					fwrite(buf, (size_t)n, 1, _child_reader.fps[1]);
 			}
 
-			/* Child output reset alarm */
-			alarm(0);
-			alarm(_child_reader.timeout);
-		}
+		} else if (r == 0) {
+			_child_reader.timeouted = 1;
+			kill(-_child_reader.pid, SIGKILL);
+                }
 
 		fflush(_child_reader.fps[0]);
 		fflush(_child_reader.fps[1]);
@@ -344,26 +343,11 @@ run_child(char *run_ptest, int fd_stdout, int fd_stderr)
 	/* exit(1); not needed? */
 }
 
-static void
-timeout_child_handler(int signo)
-{
-	UNUSED(signo);
-	_child_reader.timeouted = 1;
-	kill(-_child_reader.pid, SIGKILL);
-}
-
 static inline int
-wait_child(pid_t pid, unsigned int timeout)
+wait_child(pid_t pid)
 {
 	int status = -1;
 
-	_child_reader.timeout = timeout;
-	_child_reader.timeouted = 0;
-	_child_reader.pid = pid;
-
-	/* setup alarm to timeout based on std{out,err} in the child */
-	alarm(timeout);
-
 	waitpid(pid, &status, 0);
 	if (WIFEXITED(status))
 		status = WEXITSTATUS(status);
@@ -462,6 +446,8 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 		_child_reader.fds[1] = pipefd_stderr[0];
 		_child_reader.fps[0] = fp;
 		_child_reader.fps[1] = fp_stderr;
+		_child_reader.timeout = opts.timeout;
+		_child_reader.timeouted = 0;
 		rc = pthread_create(&tid, NULL, read_child, NULL);
 		if (rc != 0) {
 			fprintf(fp, "ERROR: Failed to create reader thread, %s\n", strerror(errno));
@@ -469,7 +455,6 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 			close(pipefd_stdout[1]);
 			break;
 		}
-		signal(SIGALRM, timeout_child_handler);
 
 		fprintf(fp, "START: %s\n", progname);
 		PTEST_LIST_ITERATE_START(head, p)
@@ -511,6 +496,7 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 			} else {
 				int status;
 
+				_child_reader.pid = child;
 				if (setpgid(child, pgid) == -1) {
 					fprintf(fp, "ERROR: setpgid() failed, %s\n", strerror(errno));
 				}
@@ -520,7 +506,7 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 				fprintf(fp, "BEGIN: %s\n", ptest_dir);
 
 
-				status = wait_child(child, opts.timeout);
+				status = wait_child(child);
 
 				entime = time(NULL);
 				duration = entime - sttime;
Signature · 1 line
2.33.0
#2 lore raw

PATCH 3/3 utils.c: add system data collection when a test gets stuck.

In reply to Alexander Kanavin

Currently, ptest-runner simply kills the offending test without further ado, which is not at all helpful when trying to figure out why it happens (especially if such hangs are intermittent and rare). There's now a script that gets executed before killing the test, so ideas on what to have in it are welcome.

---
 Makefile                         |  2 +-
 ptest-runner-collect-system-data |  5 +++++
 utils.c                          | 24 ++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100755 ptest-runner-collect-system-data
Makefile +1 -1
index a6372de..168cf5a 100644
@@ -43,7 +43,7 @@ $(TEST_EXECUTABLE): $(TEST_OBJECTS)
 	$(CC) $(LDFLAGS) $(TEST_OBJECTS) -o $@ $(TEST_LIBSTATIC) $(TEST_LDFLAGS)
 
 check: $(TEST_EXECUTABLE)
-	./$(TEST_EXECUTABLE) -d $(TEST_DATA)
+	PATH=.:$(PATH) ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
 
 .c.o:
 	$(CC) $(CFLAGS) -c $< -o $@
ptest-runner-collect-system-data new file +5 -0
new file mode 100755
index 0000000..5bfeaf3
@@ -0,0 +1,5 @@
+#!/bin/sh
+# Other ideas on what to do when a ptest gets stuck welcome.
+pstree -a -l
+df
+free
utils.c +24 -0
index 58c3aa1..a67ac11 100644
@@ -281,6 +281,27 @@ close_fds(void)
    	}
 }
 
+static void
+collect_system_state(FILE* fout)
+{
+	char *cmd = "ptest-runner-collect-system-data";
+
+	char buf[1024];
+	FILE *fp;
+
+	if ((fp = popen(cmd, "r")) == NULL) {
+		fprintf(fout, "Error opening pipe!\n");
+	}
+
+	while (fgets(buf, 1024, fp) != NULL) {
+		fprintf(fout, "%s", buf);
+	}
+
+	if(pclose(fp))  {
+		fprintf(fout, "Command not found or exited with error status\n");
+	}
+}
+
 static void *
 read_child(void *arg)
 {
@@ -313,6 +334,9 @@ read_child(void *arg)
 			}
 
 		} else if (r == 0) {
+			// no output from the test after a timeout; the test is stuck, so collect
+			// as much data from the system as possible and kill the test
+			collect_system_state(_child_reader.fps[0]);
 			_child_reader.timeouted = 1;
 			kill(-_child_reader.pid, SIGKILL);
                 }
Signature · 1 line
2.33.0
#3 lore raw

Re: [yocto] [ptest-runner][PATCH 3/3] utils.c: add system data collection when a test gets stuck.

Quoted 33 lines · Alexander Kanavin

Currently, ptest-runner simply kills the offending test without further ado, which is not at all helpful when trying to figure out why it happens (especially if such hangs are intermittent and rare). There's now a script that gets executed before killing the test, so ideas on what to have in it are welcome.

---
 Makefile                         |  2 +-
 ptest-runner-collect-system-data |  5 +++++
 utils.c                          | 24 ++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100755 ptest-runner-collect-system-data
Makefile +1 -1
index a6372de..168cf5a 100644
@@ -43,7 +43,7 @@ $(TEST_EXECUTABLE): $(TEST_OBJECTS)
 	$(CC) $(LDFLAGS) $(TEST_OBJECTS) -o $@ $(TEST_LIBSTATIC) $(TEST_LDFLAGS)
 
 check: $(TEST_EXECUTABLE)
-	./$(TEST_EXECUTABLE) -d $(TEST_DATA)
+	PATH=.:$(PATH) ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
 
 .c.o:
 	$(CC) $(CFLAGS) -c $< -o $@
ptest-runner-collect-system-data new file +0 -0
new file mode 100755
index 0000000..5bfeaf3

ptest-runner-collect-system-data line 5

Quoted 5 lines
ptest-runner-collect-system-data new file +5 -0
@@ -0,0 +1,5 @@
+#!/bin/sh
+# Other ideas on what to do when a ptest gets stuck welcome.
+pstree -a -l
+df
+free

It is great to see this. I'd suggest dmesg in here since we've seen components of tests segfault before (e.g. lttng-relayd in lttng-tools).

Cheers,

Richard

#4 lore raw

Re: [yocto] [ptest-runner][PATCH 3/3] utils.c: add system data collection when a test gets stuck.

Cheers, will add now.

Also, dmesg will be collected and archived through a separate channel, and regardless of whether something is stuck or not :)
http://git.yoctoproject.org/cgit/cgit.cgi/poky-contrib/commit/?h=akanavin/package-version-updates&id=71141340d1adcbacbed9109fd2b98b212da132f0
http://git.yoctoproject.org/cgit/cgit.cgi/yocto-autobuilder-helper/commit/?h=contrib/akanavin/misc-fixes&id=19b88d68cf556cf5f9af50f5f3bacbd8b4180b4f

Alex

Quoted 55 lines · Richard Purdie
Quoted 1 line · Alexander Kanavin

Currently, ptest-runner simply kills the offending test without further

ado,

Quoted 2 lines

which is not at all helpful when trying to figure out why it happens (especially if such hangs are intermittent and rare). There's now a

script

Quoted 1 line

that gets executed before killing the test, so ideas on what to have in

it

Quoted 15 lines

are welcome.

---
 Makefile                         |  2 +-
 ptest-runner-collect-system-data |  5 +++++
 utils.c                          | 24 ++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100755 ptest-runner-collect-system-data
Makefile +0 -0
index a6372de..168cf5a 100644

Makefile $(TEST_EXECUTABLE): $(TEST_OBJECTS) · line 43

Quoted 1 line
Makefile +0 -0
@@ -43,7 +43,7 @@ $(TEST_EXECUTABLE): $(TEST_OBJECTS)
      $(CC) $(LDFLAGS) $(TEST_OBJECTS) -o $@ $(TEST_LIBSTATIC)

$(TEST_LDFLAGS)

Review

Quoted 8 lines
Makefile +1 -1
 check: $(TEST_EXECUTABLE)
-     ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
+     PATH=.:$(PATH) ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
 
 .c.o:
      $(CC) $(CFLAGS) -c $< -o $@
(quoted diff) +0 -0

b/ptest-runner-collect-system-data

ptest-runner-collect-system-data line 5

Quoted 10 lines

new file mode 100755
index 0000000..5bfeaf3

ptest-runner-collect-system-data new file +5 -0
@@ -0,0 +1,5 @@
+#!/bin/sh
+# Other ideas on what to do when a ptest gets stuck welcome.
+pstree -a -l
+df
+free

It is great to see this. I'd suggest dmesg in here since we've seen components
of tests segfault before (e.g. lttng-relayd in lttng-tools).

Cheers,

Richard

#5 lore raw

Re: [ptest-runner][PATCH 3/3] utils.c: add system data collection when a test gets stuck.

In reply to Alexander Kanavin

Hi Alex,

Do you have a repo/branch for this patch?.

I'm having issues applying...

...

alimon@blackbox:~/upstream/ptest-runner2$ git am -3
~/Downloads/\[ptest-runner\]\[PATCH\ 3_3\]\ utils.c_\ add\ system\ data\
collection\ when\ a\ test\ gets\ stuck..eml
Applying: utils.c: add system data collection when a test gets stuck.
error: sha1 information is lacking or useless (utils.c).
error: could not build fake ancestor
Patch failed at 0001 utils.c: add system data collection when a test gets
stuck.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
...

Regards,
Anibal

Quoted 90 lines · Alexander Kanavin

Currently, ptest-runner simply kills the offending test without further ado,
which is not at all helpful when trying to figure out why it happens (especially if such hangs are intermittent and rare). There's now a script that gets executed before killing the test, so ideas on what to have in it are welcome.

---
 Makefile                         |  2 +-
 ptest-runner-collect-system-data |  5 +++++
 utils.c                          | 24 ++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100755 ptest-runner-collect-system-data
Makefile +1 -1
index a6372de..168cf5a 100644
@@ -43,7 +43,7 @@ $(TEST_EXECUTABLE): $(TEST_OBJECTS)
        $(CC) $(LDFLAGS) $(TEST_OBJECTS) -o $@ $(TEST_LIBSTATIC)
 $(TEST_LDFLAGS)
 
 check: $(TEST_EXECUTABLE)
-       ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
+       PATH=.:$(PATH) ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
 
 .c.o:
        $(CC) $(CFLAGS) -c $< -o $@
ptest-runner-collect-system-data new file +5 -0
new file mode 100755
index 0000000..5bfeaf3
@@ -0,0 +1,5 @@
+#!/bin/sh
+# Other ideas on what to do when a ptest gets stuck welcome.
+pstree -a -l
+df
+free
utils.c +21 -0
index 58c3aa1..a67ac11 100644
@@ -281,6 +281,27 @@ close_fds(void)
        }
 }
 
+static void
+collect_system_state(FILE* fout)
+{
+       char *cmd = "ptest-runner-collect-system-data";
+
+       char buf[1024];
+       FILE *fp;
+
+       if ((fp = popen(cmd, "r")) == NULL) {
+               fprintf(fout, "Error opening pipe!\n");
+       }
+
+       while (fgets(buf, 1024, fp) != NULL) {
+               fprintf(fout, "%s", buf);
+       }
+
+       if(pclose(fp))  {
+               fprintf(fout, "Command not found or exited with error
 status\n");
+       }
+}
+
 static void *
 read_child(void *arg)

{

utils.c +1 -0
@@ -313,6 +334,9 @@ read_child(void *arg)
                        }
 
                } else if (r == 0) {
+                       // no output from the test after a timeout; the

test is stuck, so collect
+ // as much data from the system as possible and
kill the test

+                       collect_system_state(_child_reader.fps[0]);
                        _child_reader.timeouted = 1;
                        kill(-_child_reader.pid, SIGKILL);
                 }

--
2.33.0

#6 lore raw

Re: [ptest-runner][PATCH 3/3] utils.c: add system data collection when a test gets stuck.

Not sure what happened here, I published the patches in
https://github.com/kanavin/ptest-runner2

Alex

Quoted 117 lines · Anibal Limon

Hi Alex,

Do you have a repo/branch for this patch?.

I'm having issues applying...

...

alimon@blackbox:~/upstream/ptest-runner2$ git am -3
~/Downloads/\[ptest-runner\]\[PATCH\ 3_3\]\ utils.c_\ add\ system\ data\
collection\ when\ a\ test\ gets\ stuck..eml
Applying: utils.c: add system data collection when a test gets stuck.
error: sha1 information is lacking or useless (utils.c).
error: could not build fake ancestor
Patch failed at 0001 utils.c: add system data collection when a test gets
stuck.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
...

Regards,
Anibal

Quoted 90 lines · Alexander Kanavin

Currently, ptest-runner simply kills the offending test without further ado,
which is not at all helpful when trying to figure out why it happens (especially if such hangs are intermittent and rare). There's now a script that gets executed before killing the test, so ideas on what to have in it are welcome.

---
 Makefile                         |  2 +-
 ptest-runner-collect-system-data |  5 +++++
 utils.c                          | 24 ++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100755 ptest-runner-collect-system-data
Makefile +1 -1
index a6372de..168cf5a 100644
@@ -43,7 +43,7 @@ $(TEST_EXECUTABLE): $(TEST_OBJECTS)
        $(CC) $(LDFLAGS) $(TEST_OBJECTS) -o $@ $(TEST_LIBSTATIC)
 $(TEST_LDFLAGS)
 
 check: $(TEST_EXECUTABLE)
-       ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
+       PATH=.:$(PATH) ./$(TEST_EXECUTABLE) -d $(TEST_DATA)
 
 .c.o:
        $(CC) $(CFLAGS) -c $< -o $@
ptest-runner-collect-system-data new file +5 -0
new file mode 100755
index 0000000..5bfeaf3
@@ -0,0 +1,5 @@
+#!/bin/sh
+# Other ideas on what to do when a ptest gets stuck welcome.
+pstree -a -l
+df
+free
utils.c +21 -0
index 58c3aa1..a67ac11 100644
@@ -281,6 +281,27 @@ close_fds(void)
        }
 }
 
+static void
+collect_system_state(FILE* fout)
+{
+       char *cmd = "ptest-runner-collect-system-data";
+
+       char buf[1024];
+       FILE *fp;
+
+       if ((fp = popen(cmd, "r")) == NULL) {
+               fprintf(fout, "Error opening pipe!\n");
+       }
+
+       while (fgets(buf, 1024, fp) != NULL) {
+               fprintf(fout, "%s", buf);
+       }
+
+       if(pclose(fp))  {
+               fprintf(fout, "Command not found or exited with error
 status\n");
+       }
+}
+
 static void *
 read_child(void *arg)

{

utils.c +1 -0
@@ -313,6 +334,9 @@ read_child(void *arg)
                        }
 
                } else if (r == 0) {
+                       // no output from the test after a timeout; the

test is stuck, so collect
+ // as much data from the system as possible and
kill the test

+                       collect_system_state(_child_reader.fps[0]);
                        _child_reader.timeouted = 1;
                        kill(-_child_reader.pid, SIGKILL);
                 }

--
2.33.0