2009-07-01 01:33:32

by Yong Zhang

[permalink] [raw]
Subject: [PATCH] MIPS: o32 application running on 64bit kernel core dump

If an o32 application crashes and generates a core dump on
a 64 bit kernel, the core file will not be correctly
recognized. This is because ELF_CORE_COPY_REGS and
ELF_CORE_COPY_TASK_REGS are not correctly defined for o32
and will use the default register set which would
be CONFIG_64BIT in asm/elf.h.

So we'll switch to use the right register defines in
this situation by checking for WANT_COMPAT_REG_H and
use the right defines of ELF_CORE_COPY_REGS and
ELF_CORE_COPY_TASK_REGS.

Signed-off-by: Yong Zhang <[email protected]>
---
arch/mips/include/asm/elf.h | 4 ++++
arch/mips/include/asm/reg.h | 2 +-
arch/mips/kernel/binfmt_elfo32.c | 20 +++++++++++++++++---
3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/arch/mips/include/asm/elf.h b/arch/mips/include/asm/elf.h
index d58f128..7990694 100644
--- a/arch/mips/include/asm/elf.h
+++ b/arch/mips/include/asm/elf.h
@@ -316,9 +316,13 @@ extern void elf_dump_regs(elf_greg_t *, struct pt_regs *regs);
extern int dump_task_regs(struct task_struct *, elf_gregset_t *);
extern int dump_task_fpu(struct task_struct *, elf_fpregset_t *);

+#ifndef ELF_CORE_COPY_REGS
#define ELF_CORE_COPY_REGS(elf_regs, regs) \
elf_dump_regs((elf_greg_t *)&(elf_regs), regs);
+#endif
+#ifndef ELF_CORE_COPY_TASK_REGS
#define ELF_CORE_COPY_TASK_REGS(tsk, elf_regs) dump_task_regs(tsk, elf_regs)
+#endif
#define ELF_CORE_COPY_FPREGS(tsk, elf_fpregs) \
dump_task_fpu(tsk, elf_fpregs)

diff --git a/arch/mips/include/asm/reg.h b/arch/mips/include/asm/reg.h
index 634b55d..910e71a 100644
--- a/arch/mips/include/asm/reg.h
+++ b/arch/mips/include/asm/reg.h
@@ -69,7 +69,7 @@

#endif

-#ifdef CONFIG_64BIT
+#if defined(CONFIG_64BIT) && !defined(WANT_COMPAT_REG_H)

#define EF_R0 0
#define EF_R1 1
diff --git a/arch/mips/kernel/binfmt_elfo32.c b/arch/mips/kernel/binfmt_elfo32.c
index e1333d7..53bc6b4 100644
--- a/arch/mips/kernel/binfmt_elfo32.c
+++ b/arch/mips/kernel/binfmt_elfo32.c
@@ -53,6 +53,23 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
#define ELF_ET_DYN_BASE (TASK32_SIZE / 3 * 2)

#include <asm/processor.h>
+
+/*
+ * When this file is selected, we are definitely running a 64bit kernel.
+ * So using the right regs define in asm/reg.h
+ */
+#define WANT_COMPAT_REG_H
+
+/* These MUST be defined before elf.h gets included */
+extern void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs);
+#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
+#define ELF_CORE_COPY_TASK_REGS(_tsk, _dest) \
+({ \
+ int __res = 1; \
+ elf32_core_copy_regs((*_dest), (task_pt_regs(_tsk))); \
+ __res; \
+})
+
#include <linux/module.h>
#include <linux/elfcore.h>
#include <linux/compat.h>
@@ -110,9 +127,6 @@ jiffies_to_compat_timeval(unsigned long jiffies, struct compat_timeval *value)
value->tv_usec = rem / NSEC_PER_USEC;
}

-#undef ELF_CORE_COPY_REGS
-#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
-
void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs)
{
int i;
--
1.5.6.5


2009-07-01 19:09:27

by David Daney

[permalink] [raw]
Subject: Re: [PATCH] MIPS: o32 application running on 64bit kernel core dump

Yong Zhang wrote:
> If an o32 application crashes and generates a core dump on
> a 64 bit kernel, the core file will not be correctly
> recognized. This is because ELF_CORE_COPY_REGS and
> ELF_CORE_COPY_TASK_REGS are not correctly defined for o32
> and will use the default register set which would
> be CONFIG_64BIT in asm/elf.h.
>
> So we'll switch to use the right register defines in
> this situation by checking for WANT_COMPAT_REG_H and
> use the right defines of ELF_CORE_COPY_REGS and
> ELF_CORE_COPY_TASK_REGS.

This patch looks plausible. How was it tested?

Can you still obtain good core files with at 32-bit kernel?

Are usable core files obtained for all three ABIs on 64-bit kernels?

Other than that, I have only the one comment below.

Thanks,
David Daney


>
> Signed-off-by: Yong Zhang <[email protected]>
> ---
> arch/mips/include/asm/elf.h | 4 ++++
> arch/mips/include/asm/reg.h | 2 +-
> arch/mips/kernel/binfmt_elfo32.c | 20 +++++++++++++++++---
> 3 files changed, 22 insertions(+), 4 deletions(-)
>
> diff --git a/arch/mips/include/asm/elf.h b/arch/mips/include/asm/elf.h
> index d58f128..7990694 100644
> --- a/arch/mips/include/asm/elf.h
> +++ b/arch/mips/include/asm/elf.h
> @@ -316,9 +316,13 @@ extern void elf_dump_regs(elf_greg_t *, struct pt_regs *regs);
> extern int dump_task_regs(struct task_struct *, elf_gregset_t *);
> extern int dump_task_fpu(struct task_struct *, elf_fpregset_t *);
>
> +#ifndef ELF_CORE_COPY_REGS
> #define ELF_CORE_COPY_REGS(elf_regs, regs) \
> elf_dump_regs((elf_greg_t *)&(elf_regs), regs);
> +#endif
> +#ifndef ELF_CORE_COPY_TASK_REGS
> #define ELF_CORE_COPY_TASK_REGS(tsk, elf_regs) dump_task_regs(tsk, elf_regs)
> +#endif
> #define ELF_CORE_COPY_FPREGS(tsk, elf_fpregs) \
> dump_task_fpu(tsk, elf_fpregs)
>
> diff --git a/arch/mips/include/asm/reg.h b/arch/mips/include/asm/reg.h
> index 634b55d..910e71a 100644
> --- a/arch/mips/include/asm/reg.h
> +++ b/arch/mips/include/asm/reg.h
> @@ -69,7 +69,7 @@
>
> #endif
>
> -#ifdef CONFIG_64BIT
> +#if defined(CONFIG_64BIT) && !defined(WANT_COMPAT_REG_H)
>
> #define EF_R0 0
> #define EF_R1 1
> diff --git a/arch/mips/kernel/binfmt_elfo32.c b/arch/mips/kernel/binfmt_elfo32.c
> index e1333d7..53bc6b4 100644
> --- a/arch/mips/kernel/binfmt_elfo32.c
> +++ b/arch/mips/kernel/binfmt_elfo32.c
> @@ -53,6 +53,23 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
> #define ELF_ET_DYN_BASE (TASK32_SIZE / 3 * 2)
>
> #include <asm/processor.h>
> +
> +/*
> + * When this file is selected, we are definitely running a 64bit kernel.
> + * So using the right regs define in asm/reg.h
> + */
> +#define WANT_COMPAT_REG_H
> +
> +/* These MUST be defined before elf.h gets included */
> +extern void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs);
> +#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
> +#define ELF_CORE_COPY_TASK_REGS(_tsk, _dest) \
> +({ \
> + int __res = 1; \
> + elf32_core_copy_regs((*_dest), (task_pt_regs(_tsk))); \
> + __res; \

Why does __res exist? Can't you have that last line just be '1;'?

> +})
> +
> #include <linux/module.h>
> #include <linux/elfcore.h>
> #include <linux/compat.h>
> @@ -110,9 +127,6 @@ jiffies_to_compat_timeval(unsigned long jiffies, struct compat_timeval *value)
> value->tv_usec = rem / NSEC_PER_USEC;
> }
>
> -#undef ELF_CORE_COPY_REGS
> -#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
> -
> void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs)
> {
> int i;

2009-07-01 19:19:50

by Ralf Baechle

[permalink] [raw]
Subject: Re: [PATCH] MIPS: o32 application running on 64bit kernel core dump

On Wed, Jul 01, 2009 at 09:35:39AM +0800, Yong Zhang wrote:

> +/* These MUST be defined before elf.h gets included */

This sort of ordering bug seems to become a tradition. I think it may be
a good idea to insert a check like this:

#ifdef ELF_CORE_COPY_REGS
#error ELF_CORE_COPY_REGS should not be defined yet!
#endif

> +extern void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs);
> +#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
> +#define ELF_CORE_COPY_TASK_REGS(_tsk, _dest) \
> +({ \
> + int __res = 1; \
> + elf32_core_copy_regs((*_dest), (task_pt_regs(_tsk))); \

Be very careful with parentheses in macros. This line should probably
become:

elf32_core_copy_regs(*(_dest), task_pt_regs(_tsk)); \

The changes to the first argument to bullet prof the macro and the change
to the second one for cosmetic reasons.

Ralf

2009-07-01 23:47:34

by Ralf Baechle

[permalink] [raw]
Subject: Re: [PATCH] MIPS: o32 application running on 64bit kernel core dump

On Wed, Jul 01, 2009 at 09:35:39AM +0800, Yong Zhang wrote:

Patch applied with the one issue in the macro I pointed out earlier fixed.

Thanks,

Ralf

2009-07-02 01:23:27

by Yong Zhang

[permalink] [raw]
Subject: Re: [PATCH] MIPS: o32 application running on 64bit kernel core dump



David Daney wrote:
> Yong Zhang wrote:
>> If an o32 application crashes and generates a core dump on
>> a 64 bit kernel, the core file will not be correctly
>> recognized. This is because ELF_CORE_COPY_REGS and
>> ELF_CORE_COPY_TASK_REGS are not correctly defined for o32
>> and will use the default register set which would
>> be CONFIG_64BIT in asm/elf.h.
>>
>> So we'll switch to use the right register defines in
>> this situation by checking for WANT_COMPAT_REG_H and
>> use the right defines of ELF_CORE_COPY_REGS and
>> ELF_CORE_COPY_TASK_REGS.
>
> This patch looks plausible. How was it tested?
>
> Can you still obtain good core files with at 32-bit kernel?

Yeah, I also have tested 32-bit kernel. Actually this doesn't have any
side effect on that.

>
> Are usable core files obtained for all three ABIs on 64-bit kernels?

Tested for all three ABIs, and all does the right thing.
Testing code is below:
/* test.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <execinfo.h>
#include <sys/types.h>
#include <linux/unistd.h>
#include <errno.h>
#include <pthread.h>

#define MAX_THREADS 4

void foo7()
{
int *i=0;
char c =*i;
}

void foo6()
{
int c6=1000;
int i=9;
while(c6--)
{
i=i*9+1;
}
printf("inside foo6\n");
foo7();
}

void foo5()
{
int c5=1000;
int i=9;
while(c5--)
{
i=i*9+1;
}
printf("inside foo5\n");
foo6();
}

void foo4()
{
int c4=1000;
int i=9;
while(c4--)
{
i=i*9+1;
}
printf("inside foo4\n");
foo5();
}

void foo3()
{
int c3=1000;
int i=9;
while(c3--)
{
i=i*9+1;
}
printf("inside foo3\n");
foo4();
}

void foo2()
{
int c2=1000;
int i=9;
while (c2--)
{
i=i*9+1;
}
printf("inside foo2\n");
foo3();
}

void *foo1(void* arg)
{
sleep(10);
foo2();
}

int main()
{
int i=0;
pthread_t *threads;
pthread_attr_t pthread_attr;

printf("inside main\n");
threads=(pthread_t *)malloc(MAX_THREADS*sizeof(*threads));
pthread_attr_init(&pthread_attr);

for(i=0;i<MAX_THREADS;i++)
{
pthread_create(&threads[i],&pthread_attr,foo1,NULL);
}

for(i=0;i<MAX_THREADS;i++)
{
pthread_join(threads[i],NULL);
}

exit(1);
}
>
> Other than that, I have only the one comment below.
>
> Thanks,
> David Daney
>

<cut here>

>
>> +#define ELF_CORE_COPY_TASK_REGS(_tsk, _dest) \
>> +({ \
>> + int __res = 1; \
>> + elf32_core_copy_regs((*_dest), (task_pt_regs(_tsk))); \
>> + __res; \
>
> Why does __res exist? Can't you have that last line just be '1;'?

Sounds good. Just be '1;' is good.

Thanks,
Yong

>
>> +})
>> +
>> #include <linux/module.h>
>> #include <linux/elfcore.h>
>> #include <linux/compat.h>
>> @@ -110,9 +127,6 @@ jiffies_to_compat_timeval(unsigned long jiffies,
>> struct compat_timeval *value)
>> value->tv_usec = rem / NSEC_PER_USEC;
>> }
>>
>> -#undef ELF_CORE_COPY_REGS
>> -#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest,
>> _regs);
>> -
>> void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs)
>> {
>> int i;
>
>