[LTP] ltp_clone alignment issues.

Carmelo AMOROSO
2010-09-27T13:34:22Z

[-- Attachment #1: Type: text/plain, Size: 1269 bytes --]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Folks,
I want to reopen an old discussion on ltp_clone and stack alignment
issue (see
http://sourceforge.net/mailarchive/message.php?msg_name=4B421480.1040400%40petalogix.com).
Indeed recently a commit has partially fixed a problem on ARM
(0056e395170eb8fc3ffbb22d7bd364fe47c2013e), but I think this should be
extended to all archs that have stack that grows downwards.

Indeed, as Jiri replied in that old thread, the value passed to clone as
child stack should be never accessed, because it is the topmost address
of the memory allocated for the child process (it's the previous stack
pointer).

So, in archs that do not like unaligned stack, using (stack - size - 1 )
will cause the process to be killed by a SIGBUS, on other archs, we are
just wasting one byte of the malloc-ed stack.

On my SH4 arch, the stack must be 4byte aligned (as in ARM).

Please, find attached a patch against master branch

Best regards,
Carmelo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkygnU0ACgkQoRq/3BrK1s+GugCgrrCDWRWknR36qdOFv6Qzg4Kl
tEAAn1lRyS/5RL2ymbrX4A3q+7L3w/Gs
=yBmF
-----END PGP SIGNATURE-----

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lib-Fix-ltp_clone-wrapper-stack-argument-for-all-arc.patch --]
[-- Type: text/x-patch; name="0001-lib-Fix-ltp_clone-wrapper-stack-argument-for-all-arc.patch", Size: 1797 bytes --]

From 9393da0a1a6959cc5d25f73be63baf8c3fae8762 Mon Sep 17 00:00:00 2001
From: Carmelo Amoroso <carmelo.amoroso@st.com>
Date: Mon, 27 Sep 2010 13:00:06 +0000
Subject: [PATCH] lib: Fix ltp_clone wrapper stack argument for all architectures

For all the architectures that have stack that grows downwards, the
stack argument passed to the clone function points to the topmost
address of the memory space set up for the child stack. So it is not
correct to move the stack down. The ABI guarantees that the function has
to create it's own stack frame.

Nevertheless, on some architectures that do not accept unaligned stack,
it causes the process to be killed by the kernel with a SIGBUS.
On architectures that safely handle unaligned stack, we are actually
reserving one byte less.

Signed-off-by: Carmelo Amoroso <carmelo.amoroso@st.com>
---
lib/cloner.c | 9 +++------
1 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/lib/cloner.c b/lib/cloner.c
index 6ad4a00..e53c9cf 100644
--- a/lib/cloner.c
+++ b/lib/cloner.c
@@ -59,16 +59,13 @@ ltp_clone(unsigned long clone_flags, int (*fn)(void *arg), void *arg,
ret = clone(fn, stack, clone_flags, arg);
#elif defined(__ia64__)
ret = clone2(fn, stack, stack_size, clone_flags, arg, NULL, NULL, NULL);
-#elif defined(__arm__)
+#else
/*
- * Stack size should be a multiple of 32 bit words
- * & stack limit must be aligned to a 32 bit boundary
+ * For archs where stack grows downwards, stack points to the topmost address
+ * of the memory space set up for the child stack.
*/
ret = clone(fn, (stack ? stack + stack_size : NULL),
clone_flags, arg);
-#else
- ret = clone(fn, (stack ? stack + stack_size - 1 : NULL),
- clone_flags, arg);
#endif

return ret;
--
1.7.1

[-- Attachment #3: Type: text/plain, Size: 276 bytes --]

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev

[-- Attachment #4: Type: text/plain, Size: 155 bytes --]