Skip to contentSkip to conversation

dlopen()/dlclose() bugfix with testcase

PATCH Applied Henning Schild 2 participants 7 messages Feb 16, 2018 – Feb 20, 2018

lore
Share & tools
lore
#0 lore raw

cover dlopen()/dlclose() bugfix with testcase

In order to fix a segfault in a dlopen()/dlclose()/dlopen() sequence i wrote a testcase to reproduce the issue. That testcase also covers a few other dlopen-related things.
The testcase is intetgrated into smokey. Because a smokey-test is already a xenomai-application the actual test-program gets launched from the testcase. And that program is a non-xenomai application which gains xenomai-functionality with dlopen().
The series also contains preparation and fixes along the way (posix-fork).

Henning Schild (5):
smokey/posix-fork: fix binary path for configure --with-testdir smokey/posix-fork: actually wait for the process to exit smokey/posix-fork: move fork/exec functionality into libsmokey smokey/dlopen: introduce a new testcase boilerplate/setup: introduce destructors for __setup_call

 configure.ac                              |  2 +
 include/boilerplate/setup.h               |  7 +++
 include/smokey/smokey.h                   |  8 +++
 lib/boilerplate/setup.c                   |  6 +++
 lib/smokey/helpers.c                      | 31 +++++++++++
 testsuite/smokey/Makefile.am              |  4 ++
 testsuite/smokey/dlopen/Makefile.am       | 63 ++++++++++++++++++++++
 testsuite/smokey/dlopen/dlopen.c          | 44 ++++++++++++++++
 testsuite/smokey/dlopen/dlopentest.c      | 88 +++++++++++++++++++++++++++++++
 testsuite/smokey/dlopen/libalchemy-test.c | 65 +++++++++++++++++++++++
 testsuite/smokey/dlopen/libposix-test.c   | 32 +++++++++++
 testsuite/smokey/posix-fork/Makefile.am   |  5 +-
 testsuite/smokey/posix-fork/posix-fork.c  | 33 ++----------
 13 files changed, 358 insertions(+), 30 deletions(-)
 create mode 100644 testsuite/smokey/dlopen/Makefile.am
 create mode 100644 testsuite/smokey/dlopen/dlopen.c
 create mode 100644 testsuite/smokey/dlopen/dlopentest.c
 create mode 100644 testsuite/smokey/dlopen/libalchemy-test.c
 create mode 100644 testsuite/smokey/dlopen/libposix-test.c
Signature · 1 line
2.16.1
#1 lore raw

PATCH 1/5 smokey/posix-fork: fix binary path for configure --with-testdir

./configure allows to place test binaries in a non-default location. Change the testcase to still find the "smokey" executable.

---
 testsuite/smokey/posix-fork/Makefile.am  | 5 +++--
 testsuite/smokey/posix-fork/posix-fork.c | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
testsuite/smokey/posix-fork/Makefile.am +3 -2
index c4232cfd3..516c243be 100644
@@ -5,6 +5,7 @@ libposix_fork_a_SOURCES = posix-fork.c
 
 CCLD = $(top_srcdir)/scripts/wrap-link.sh $(CC)
 
-libposix_fork_a_CPPFLAGS = 	\
-	@XENO_USER_CFLAGS@	\
+libposix_fork_a_CPPFLAGS =			\
+	@XENO_USER_CFLAGS@			\
+	-DXENO_TEST_DIR='"$(XENO_TEST_DIR)"'	\
 	-I$(top_srcdir)/include
testsuite/smokey/posix-fork/posix-fork.c +1 -1
index bb809e176..8a44510b3 100644
@@ -47,7 +47,7 @@ static int run_posix_fork(struct smokey_test *t, int argc, char *const argv[])
 		 * Re-exec ourselves without running any test, this is
 		 * enough for creating a shadow context.
 		 */
-		execl(CONFIG_XENO_PREFIX "/bin/smokey", "smokey", NULL);
+		execl(XENO_TEST_DIR "/smokey", "smokey", NULL);
 		_exit(99);
 	default:
 		req.tv_sec = 0;
Signature · 1 line
2.16.1
#2 lore raw

PATCH 2/5 smokey/posix-fork: actually wait for the process to exit

In reply to Henning Schild

The comment says that it would be enough to just trigger the chain of events. This test did not even fail if the binary to execute was not there, not sure how much of the chain was exercised.
This patch makes the parent wait for what actually happens and should return errors, should they happen. It prepares for another patch using fork/exec.

---
 testsuite/smokey/posix-fork/posix-fork.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
testsuite/smokey/posix-fork/posix-fork.c +16 -7
index 8a44510b3..d88ac527d 100644
@@ -10,6 +10,8 @@
 #include <unistd.h>
 #include <errno.h>
 #include <error.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <xeno_config.h>
 #include <boilerplate/libc.h>
 #include <smokey/smokey.h>
@@ -37,9 +39,11 @@ smokey_test_plugin(posix_fork,
  */
 static int run_posix_fork(struct smokey_test *t, int argc, char *const argv[])
 {
-	struct timespec req;
+	int wstatus = 0;
+	pid_t pid;
 
-	switch (do_fork()) {
+	pid = do_fork();
+	switch (pid) {
 	case -1:
 		error(1, errno, "fork/vfork");
 	case 0:
@@ -48,12 +52,17 @@ static int run_posix_fork(struct smokey_test *t, int argc, char *const argv[])
 		 * enough for creating a shadow context.
 		 */
 		execl(XENO_TEST_DIR "/smokey", "smokey", NULL);
-		_exit(99);
+		error(1, errno, "execl %s/smokey", XENO_TEST_DIR);
 	default:
-		req.tv_sec = 0;
-		req.tv_nsec = 20000000;
-		clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
+		waitpid(pid, &wstatus, 0);
 	}
 
-	return 0;
+	if WIFEXITED(wstatus)
+		return WEXITSTATUS(wstatus);
+
+	if WIFSIGNALED(wstatus)
+		fprintf(stderr, "%s %s\n",
+			strsignal(WTERMSIG(wstatus)),
+			WCOREDUMP(wstatus) ? "(core dumped)" : "");
+	return 1;
 }
Signature · 1 line
2.16.1
#3 lore raw

PATCH 3/5 smokey/posix-fork: move fork/exec functionality into libsmokey

In reply to Henning Schild

One of the following patches will use that function as well.

---
 include/smokey/smokey.h                  |  8 ++++++
 lib/smokey/helpers.c                     | 31 +++++++++++++++++++++++
 testsuite/smokey/posix-fork/posix-fork.c | 42 ++++----------------------------
 3 files changed, 44 insertions(+), 37 deletions(-)
include/smokey/smokey.h +8 -0
index 835156335..5fea66c70 100644
@@ -25,6 +25,12 @@
 #include <copperplate/clockobj.h>
 #include <xenomai/init.h>
 
+#ifdef HAVE_FORK
+#define do_fork fork
+#else
+#define do_fork vfork
+#endif
+
 #define SMOKEY_INT(__name) {		\
 	 .name = # __name,		\
 	 .parser = smokey_int,		\
@@ -230,6 +236,8 @@ int smokey_barrier_timedwait(struct smokey_barrier *b,
 			     struct timespec *ts);
   
 void smokey_barrier_release(struct smokey_barrier *b);
+
+int smokey_fork_exec(const char *path, const char *arg);
 	
 #ifdef __cplusplus
 }
lib/smokey/helpers.c +31 -0
index 2b9985d87..fd1fca354 100644
@@ -20,7 +20,11 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <error.h>
 #include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <boilerplate/ancillaries.h>
 #include <smokey/smokey.h>
 
@@ -239,3 +243,30 @@ void smokey_barrier_release(struct smokey_barrier *b)
 	__RT(pthread_cond_broadcast(&b->barrier));
 	__RT(pthread_mutex_unlock(&b->lock));
 }
+
+int smokey_fork_exec(const char *path, const char *arg)
+{
+	int wstatus = 0;
+	pid_t pid;
+
+	pid = do_fork();
+	switch (pid) {
+	case -1:
+		error(1, errno, "fork/vfork");
+	case 0:
+		execl(path, arg, NULL);
+		error(1, errno, "execl %s", path);
+	default:
+		waitpid(pid, &wstatus, 0);
+	}
+
+	if WIFEXITED(wstatus)
+		return WEXITSTATUS(wstatus);
+
+	if WIFSIGNALED(wstatus)
+		fprintf(stderr, "%s %s\n",
+			strsignal(WTERMSIG(wstatus)),
+			WCOREDUMP(wstatus) ? "(core dumped)" : "");
+	return 1;
+
+}
testsuite/smokey/posix-fork/posix-fork.c +5 -37

58 lines not on this page · raw message

Signature · 1 line
2.16.1
#4 lore raw

PATCH 4/5 smokey/dlopen: introduce a new testcase

In reply to Henning Schild

This commit introduces a new testcase into the smokey framework. The test covers some aspects of adding xenomai-functionality to applications with dlopen(). It also tests initialization and tunables. This test also shows a bug in xenomai, so the test will actually fail. The next commit will fix that again.

---
 configure.ac                              |  2 +
 testsuite/smokey/Makefile.am              |  4 ++
 testsuite/smokey/dlopen/Makefile.am       | 63 ++++++++++++++++++++++
 testsuite/smokey/dlopen/dlopen.c          | 44 ++++++++++++++++
 testsuite/smokey/dlopen/dlopentest.c      | 88 +++++++++++++++++++++++++++++++
 testsuite/smokey/dlopen/libalchemy-test.c | 65 +++++++++++++++++++++++
 testsuite/smokey/dlopen/libposix-test.c   | 32 +++++++++++
 7 files changed, 298 insertions(+)
 create mode 100644 testsuite/smokey/dlopen/Makefile.am
 create mode 100644 testsuite/smokey/dlopen/dlopen.c
 create mode 100644 testsuite/smokey/dlopen/dlopentest.c
 create mode 100644 testsuite/smokey/dlopen/libalchemy-test.c
 create mode 100644 testsuite/smokey/dlopen/libposix-test.c
configure.ac +2 -0
index bb239a9f2..330c7664f 100644
@@ -717,6 +717,7 @@ else
 	AC_DEFINE_UNQUOTED(CONFIG_XENO_TLS_MODEL,"initial-exec",[TLS model])
 	XENO_LIB_LDFLAGS="-Wl,-z -Wl,nodlopen"
 fi
+AM_CONDITIONAL(CONFIG_XENO_LIBS_DLOPEN,[test x$CONFIG_XENO_LIBS_DLOPEN = xy])
 
 AC_MSG_CHECKING(whether to enable TLS support)
 AC_ARG_ENABLE([tls],
@@ -911,6 +912,7 @@ AC_CONFIG_FILES([ \
 	testsuite/spitest/Makefile \
 	testsuite/smokey/Makefile \
 	testsuite/smokey/arith/Makefile \
+	testsuite/smokey/dlopen/Makefile \
 	testsuite/smokey/sched-quota/Makefile \
 	testsuite/smokey/sched-tp/Makefile \
 	testsuite/smokey/setsched/Makefile \
testsuite/smokey/Makefile.am +4 -0
index ae60a441a..c6fe70bb5 100644
@@ -31,6 +31,10 @@ COBALT_SUBDIRS = 	\
 	vdso-access 	\
 	xddp
 
+if CONFIG_XENO_LIBS_DLOPEN
+COBALT_SUBDIRS += dlopen
+endif
+
 MERCURY_SUBDIRS =
 
 DIST_SUBDIRS = $(COBALT_SUBDIRS) $(MERCURY_SUBDIRS)
testsuite/smokey/dlopen/Makefile.am new file +63 -0

63 lines not on this page · raw message

testsuite/smokey/dlopen/dlopen.c new file +44 -0

44 lines not on this page · raw message

testsuite/smokey/dlopen/dlopentest.c new file +88 -0

88 lines not on this page · raw message

testsuite/smokey/dlopen/libalchemy-test.c new file +65 -0

65 lines not on this page · raw message

testsuite/smokey/dlopen/libposix-test.c new file +32 -0
new file mode 100644
index 000000000..fda01a533
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) Siemens AG, 2018
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *  
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <time.h>
+#include <stdio.h>
+
+int libposix_func(void);
+
+int libposix_func(void)
+{
+	struct timespec now;
+	return clock_gettime(CLOCK_REALTIME, &now);
+}
Signature · 1 line
2.16.1
#5 lore raw

PATCH 5/5 boilerplate/setup: introduce destructors for __setup_call

In reply to Henning Schild

Whit dlclose() we can actually invalidate the setup_list, it might end up containing pointers to memory that got unmapped with dlclose(). The next dlopen() will then cause a segfault.
Add destructors to that such referrences are remove from the setup_list when calling dlclose().
The previous commit added a testcase that fails because of that issue, with this commit the test runs fine.

---
 include/boilerplate/setup.h | 7 +++++++
 lib/boilerplate/setup.c     | 6 ++++++
 2 files changed, 13 insertions(+)
include/boilerplate/setup.h +7 -0
index 7df3cfecc..83af91f38 100644
@@ -78,10 +78,16 @@ struct setup_descriptor {
 #define __early_ctor		__attribute__ ((constructor(210)))
 #define __bootstrap_ctor	__attribute__ ((constructor(220)))
 
+#define __setup_dtor		__attribute__ ((destructor(200)))
+
 #define __setup_call(__name, __id)			\
 static __setup_ctor void __declare_ ## __name(void)	\
 {							\
 	__register_setup_call(&(__name), __id);		\
+}							\
+static __setup_dtor void __undeclare_ ## __name(void)	\
+{							\
+	__unregister_setup_call(&(__name), __id);	\
 }
 
 #define core_setup_call(__name)		__setup_call(__name, 0)
@@ -96,6 +102,7 @@ extern "C" {
 #endif
 
 void __register_setup_call(struct setup_descriptor *p, int id);
+void __unregister_setup_call(struct setup_descriptor *p, int id);
 
 extern pid_t __node_id;
 
lib/boilerplate/setup.c +6 -0
index f749828b9..9e4ee3410 100644
@@ -698,6 +698,12 @@ void __register_setup_call(struct setup_descriptor *p, int id)
 	pvlist_prepend(&p->__reserved.next, &setup_list);
 }
 
+void __unregister_setup_call(struct setup_descriptor *p, int id)
+{
+	pvlist_remove(&p->__reserved.next);
+}
+
+
 const char *get_program_name(void)
 {
 	return basename(__base_setup_data.arg0 ?: "program");
Signature · 1 line
2.16.1
#6 lore raw
In reply to Henning Schild
Quoted 28 lines · Henning Schild

In order to fix a segfault in a dlopen()/dlclose()/dlopen() sequence i wrote a testcase to reproduce the issue. That testcase also covers a few other dlopen-related things.
The testcase is intetgrated into smokey. Because a smokey-test is already a xenomai-application the actual test-program gets launched from the testcase. And that program is a non-xenomai application which gains xenomai-functionality with dlopen().
The series also contains preparation and fixes along the way (posix-fork).

Henning Schild (5):
smokey/posix-fork: fix binary path for configure --with-testdir smokey/posix-fork: actually wait for the process to exit smokey/posix-fork: move fork/exec functionality into libsmokey smokey/dlopen: introduce a new testcase boilerplate/setup: introduce destructors for __setup_call

 configure.ac                              |  2 +
 include/boilerplate/setup.h               |  7 +++
 include/smokey/smokey.h                   |  8 +++
 lib/boilerplate/setup.c                   |  6 +++
 lib/smokey/helpers.c                      | 31 +++++++++++
 testsuite/smokey/Makefile.am              |  4 ++
 testsuite/smokey/dlopen/Makefile.am       | 63 ++++++++++++++++++++++
 testsuite/smokey/dlopen/dlopen.c          | 44 ++++++++++++++++
 testsuite/smokey/dlopen/dlopentest.c      | 88 +++++++++++++++++++++++++++++++
 testsuite/smokey/dlopen/libalchemy-test.c | 65 +++++++++++++++++++++++
 testsuite/smokey/dlopen/libposix-test.c   | 32 +++++++++++
Quoted 9 lines
 testsuite/smokey/posix-fork/Makefile.am   |  5 +-
 testsuite/smokey/posix-fork/posix-fork.c  | 33 ++----------
 13 files changed, 358 insertions(+), 30 deletions(-)
 create mode 100644 testsuite/smokey/dlopen/Makefile.am
 create mode 100644 testsuite/smokey/dlopen/dlopen.c
 create mode 100644 testsuite/smokey/dlopen/dlopentest.c
 create mode 100644 testsuite/smokey/dlopen/libalchemy-test.c
 create mode 100644 testsuite/smokey/dlopen/libposix-test.c

Merged, thanks.

Signature · 1 line
Philippe.