public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v4] ipc/shmat: pick suitable base_addr before each testcase
@ 2012-10-30 10:52 Jan Stancek
  2012-10-30 11:03 ` Wanlong Gao
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Stancek @ 2012-10-30 10:52 UTC (permalink / raw)
  To: ltp-list

This test picked fixed address in setup() which was used throughout
the test. There has been reports that it can fail with EINVAL.

I could reproduce it by adding single printf in between
detach/attach:
http://article.gmane.org/gmane.linux.ltp/16480

This patch picks suitable base_addr before each test. So even if glibc
would mmap extra page with addr == base_addr picked by setup(),
new suitable address will be chosen.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 testcases/kernel/syscalls/ipc/shmat/shmat01.c      |   18 ++---------
 testcases/kernel/syscalls/ipc/shmat/shmat02.c      |   16 +--------
 testcases/kernel/syscalls/ipc/shmat/shmat_common.h |   33 ++++++++++++++++++++
 3 files changed, 38 insertions(+), 29 deletions(-)
 create mode 100644 testcases/kernel/syscalls/ipc/shmat/shmat_common.h

diff --git a/testcases/kernel/syscalls/ipc/shmat/shmat01.c b/testcases/kernel/syscalls/ipc/shmat/shmat01.c
index 6e4dee1..b64cf61 100644
--- a/testcases/kernel/syscalls/ipc/shmat/shmat01.c
+++ b/testcases/kernel/syscalls/ipc/shmat/shmat01.c
@@ -56,6 +56,7 @@
  */
 
 #include "ipcshm.h"
+#include "shmat_common.h"
 
 char *TCID = "shmat01";
 
@@ -108,6 +109,7 @@ int main(int ac, char **av)
 			/*
 			 * Use TEST macro to make the call
 			 */
+			base_addr = probe_free_addr();
 			errno = 0;
 			addr = shmat(*(TC[i].shmid), base_addr+TC[i].offset,
 				     TC[i].flags);
@@ -128,7 +130,7 @@ int main(int ac, char **av)
 			 * clean up things in case we are looping - in
 			 * this case, detach the shared memory
 			 */
-			if (shmdt((const void *)addr) == -1) {
+			if (shmdt(addr) == -1) {
 				tst_brkm(TBROK, cleanup,
 					 "Couldn't detach shared memory");
 			}
@@ -261,20 +263,6 @@ void setup(void)
 		tst_brkm(TBROK, cleanup, "Failed to create shared memory "
 			 "resource 1 in setup()");
 	}
-
-	/* Probe an available linear address for attachment */
-	if ((base_addr = shmat(shm_id_1, NULL, 0)) == (void *)-1) {
-		tst_brkm(TBROK, cleanup, "Couldn't attach shared memory");
-	}
-	if (shmdt((const void *)base_addr) == -1) {
-		tst_brkm(TBROK, cleanup, "Couldn't detach shared memory");
-	}
-
-	/* some architectures (e.g. parisc) are strange, so better always align to
-	 * next SHMLBA address. */
-	base_addr =
-	    (void *)(((unsigned long)(base_addr) + (SHMLBA - 1)) &
-		     ~(SHMLBA - 1));
 }
 
 /*
diff --git a/testcases/kernel/syscalls/ipc/shmat/shmat02.c b/testcases/kernel/syscalls/ipc/shmat/shmat02.c
index d4cca0e..af28d6c 100644
--- a/testcases/kernel/syscalls/ipc/shmat/shmat02.c
+++ b/testcases/kernel/syscalls/ipc/shmat/shmat02.c
@@ -55,6 +55,7 @@
 
 #include "ipcshm.h"
 #include <pwd.h>
+#include "shmat_common.h"
 
 char *TCID = "shmat02";
 char nobody_uid[] = "nobody";
@@ -125,6 +126,7 @@ int main(int ac, char **av)
 
 			setup_tc(i, tc);
 
+			base_addr = probe_free_addr();
 			errno = 0;
 			addr = shmat(*(tc->shmid), base_addr + tc->offset, 0);
 
@@ -180,20 +182,6 @@ void setup(void)
 	if ((shm_id_3 = shmget(shmkey2, INT_SIZE, IPC_CREAT|IPC_EXCL)) ==
 	    -1)
 		tst_brkm(TBROK|TERRNO, cleanup, "shmget #2 failed");
-
-	/* Probe an available linear address for attachment */
-	if ((base_addr = shmat(shm_id_2, NULL, 0)) == (void *)-1)
-		tst_brkm(TBROK|TERRNO, cleanup, "shmat #1 failed");
-
-	if (shmdt((const void *)base_addr) == -1)
-		tst_brkm(TBROK|TERRNO, cleanup, "shmat #2 failed");
-
-	/*
-	 * some architectures (e.g. parisc) are strange, so better always align
-	 * to next SHMLBA address
-	 */
-	base_addr =
-	    (void *)(((unsigned long)(base_addr) & ~(SHMLBA - 1)) + SHMLBA);
 }
 
 /*
diff --git a/testcases/kernel/syscalls/ipc/shmat/shmat_common.h b/testcases/kernel/syscalls/ipc/shmat/shmat_common.h
new file mode 100644
index 0000000..06cf5ab
--- /dev/null
+++ b/testcases/kernel/syscalls/ipc/shmat/shmat_common.h
@@ -0,0 +1,33 @@
+static key_t probe_key;
+
+void *probe_free_addr(void)
+{
+	void *p;
+	int ret;
+	int shm_id = -1;
+
+	if (probe_key == 0)
+		probe_key = getipckey();
+
+	/* create a shared memory resource with read and write permissions
+	 * We align this to SHMLBA so we should allocate at least
+	 * SHMLBA*2 in case SHMLBA > page_size. */
+	shm_id = shmget(probe_key, SHMLBA*2, SHM_RW | IPC_CREAT | IPC_EXCL);
+	if (shm_id == -1)
+		tst_brkm(TBROK, cleanup, "probe: shmget failed");
+
+	/* Probe an available linear address for attachment */
+	p = shmat(shm_id, NULL, 0);
+	if (p == (void *)-1)
+		tst_brkm(TBROK, cleanup, "probe: shmat failed");
+	ret = shmdt(p);
+	if (ret == -1)
+		tst_brkm(TBROK, cleanup, "probe: shmdt failed");
+
+	rm_shm(shm_id);
+
+	/* some architectures (e.g. parisc) are strange, so better always
+	 * align to next SHMLBA address. */
+	p = (void *)(((unsigned long)(p) + (SHMLBA - 1)) & ~(SHMLBA - 1));
+	return p;
+}
-- 
1.7.1


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-10-30 11:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-30 10:52 [LTP] [PATCH v4] ipc/shmat: pick suitable base_addr before each testcase Jan Stancek
2012-10-30 11:03 ` Wanlong Gao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox