Skip to contentSkip to conversation

PowerPC: Avoid segfault in cpu_dump_state

PATCH In review Fabien Chouteau 3 participants 12 messages May 14, 2012 – May 23, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PowerPC: Avoid segfault in cpu_dump_state

Quit if no log file is defined.

---
 target-ppc/translate.c |    4 ++++
 1 file changed, 4 insertions(+)
target-ppc/translate.c +4 -0
index cf59765..f17bd91 100644
@@ -9319,6 +9319,10 @@ void cpu_dump_state (CPUPPCState *env, FILE *f, fprintf_function cpu_fprintf,
 
     int i;
 
+    if (f == NULL) {
+        return;
+    }
+
     cpu_synchronize_state(env);
 
     cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 21 lines · Fabien Chouteau

Quit if no log file is defined.

---
 target-ppc/translate.c |    4 ++++

 1 file changed, 4 insertions(+)
target-ppc/translate.c +4 -0
index cf59765..f17bd91 100644
@@ -9319,6 +9319,10 @@ void cpu_dump_state (CPUPPCState *env, FILE *f, fprintf_function cpu_fprintf,
 
     int i;
 
+    if (f == NULL) {
+        return;
+    }
+
     cpu_synchronize_state(env);
 
Quoted 1 line

    cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "

target-ppc isn't the only one that doesn't check for a NULL f:
perhaps it would be better to say "you can't call this with a NULL FILE*" and fix whatever is calling it in that way?

-- PMM

#2 lore raw

PATCH 1/1 Avoid segfault in cpu_dump_state

Do not call cpu_dump_state if logfile is NULL.

---
 qemu-log.h |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
qemu-log.h +6 -1
index fccfb110..2cd5ffa 100644
@@ -51,7 +51,12 @@ extern int loglevel;
 /* Special cases: */
 
 /* cpu_dump_state() logging functions: */
-#define log_cpu_state(env, f) cpu_dump_state((env), logfile, fprintf, (f));
+#define log_cpu_state(env, f)                          \
+do {                                                   \
+    if (logfile != NULL) {                             \
+        cpu_dump_state((env), logfile, fprintf, (f));  \
+    }                                                  \
+ } while (0)
 #define log_cpu_state_mask(b, env, f) do {           \
       if (loglevel & (b)) log_cpu_state((env), (f)); \
   } while (0)
Signature · 1 line
1.7.9.5
#3 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

Andreas

Quoted 24 lines
---
 qemu-log.h |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
qemu-log.h +6 -1
index fccfb110..2cd5ffa 100644
@@ -51,7 +51,12 @@ extern int loglevel;
 /* Special cases: */
 
 /* cpu_dump_state() logging functions: */
-#define log_cpu_state(env, f) cpu_dump_state((env), logfile, fprintf, (f));
+#define log_cpu_state(env, f)                          \
+do {                                                   \
+    if (logfile != NULL) {                             \
+        cpu_dump_state((env), logfile, fprintf, (f));  \
+    }                                                  \
+ } while (0)
 #define log_cpu_state_mask(b, env, f) do {           \
       if (loglevel & (b)) log_cpu_state((env), (f)); \
   } while (0)
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 6 lines · Andreas Färber

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

Quoted 25 lines
Quoted 24 lines
---
 qemu-log.h |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
qemu-log.h +6 -1
index fccfb110..2cd5ffa 100644
@@ -51,7 +51,12 @@ extern int loglevel;
 /* Special cases: */
 
 /* cpu_dump_state() logging functions: */
-#define log_cpu_state(env, f) cpu_dump_state((env), logfile, fprintf, (f));
+#define log_cpu_state(env, f)                          \
+do {                                                   \
+    if (logfile != NULL) {                             \
+        cpu_dump_state((env), logfile, fprintf, (f));  \
+    }                                                  \
+ } while (0)
 #define log_cpu_state_mask(b, env, f) do {           \
       if (loglevel & (b)) log_cpu_state((env), (f)); \
   } while (0)
Signature · 1 line
Fabien Chouteau
#5 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 9 lines · Fabien Chouteau
Quoted 5 lines · Andreas Färber

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

The question is which of the following two options we want:
(1) callers should be guarding the calls to log_cpu_state() with checks for qemu_log_enabled() or qemu_loglevel_mask()
(2) log_cpu_state() does its own check for whether logging is enabled in the same way that qemu_log() and qemu_log_vprintf() do

At the moment most callers of log_cpu_state() do their own checks as per (1), but you could make an argument that we should switch to (2) instead.

-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 17 lines · Peter Maydell
Quoted 9 lines · Fabien Chouteau
Quoted 5 lines · Andreas Färber

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

The question is which of the following two options we want:
(1) callers should be guarding the calls to log_cpu_state() with checks for qemu_log_enabled() or qemu_loglevel_mask()
(2) log_cpu_state() does its own check for whether logging is enabled in the same way that qemu_log() and qemu_log_vprintf() do

Quoted 4 lines

At the moment most callers of log_cpu_state() do their own checks as per (1), but you could make an argument that we should switch to (2) instead.

I think (2) is better, we do the check in one place and that's it. All call to log_cpu_state are safe. And as you said, it's already the way qemu_log and qemu_log_vprintf work.

Signature · 1 line
Fabien Chouteau
#7 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 7 lines · Fabien Chouteau
Quoted 6 lines · Peter Maydell

The question is which of the following two options we want:
(1) callers should be guarding the calls to log_cpu_state() with checks for qemu_log_enabled() or qemu_loglevel_mask()
(2) log_cpu_state() does its own check for whether logging is enabled in the same way that qemu_log() and qemu_log_vprintf() do

Quoted 7 lines
Quoted 3 lines

At the moment most callers of log_cpu_state() do their own checks as per (1), but you could make an argument that we should switch to (2) instead.

I think (2) is better, we do the check in one place and that's it. All call to log_cpu_state are safe. And as you said, it's already the way qemu_log and qemu_log_vprintf work.

Yeah, it seems reasonable to me. I think your commit message could be better though, since you're actually kind of changing an API here, not just fixing a segfault bug.

-- PMM

#8 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

In reply to Fabien Chouteau

Am 15.05.2012 18:08, schrieb Fabien Chouteau:

Quoted 10 lines · Andreas Färber
Quoted 6 lines

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

Ah, I see now that f is a different f here, logfile becomes log_cpu_state()'s f. Unfortunate naming.

Your fix looks OK then but I would recommend turning it into a static inline function to avoid the line breaks.

Andreas

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
---
 qemu-log.h |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
qemu-log.h +6 -1
index fccfb110..2cd5ffa 100644
@@ -51,7 +51,12 @@ extern int loglevel;
 /* Special cases: */
 
 /* cpu_dump_state() logging functions: */
-#define log_cpu_state(env, f) cpu_dump_state((env), logfile, fprintf, (f));
+#define log_cpu_state(env, f)                          \
+do {                                                   \
+    if (logfile != NULL) {                             \
+        cpu_dump_state((env), logfile, fprintf, (f));  \
+    }                                                  \
+ } while (0)
 #define log_cpu_state_mask(b, env, f) do {           \
       if (loglevel & (b)) log_cpu_state((env), (f)); \
   } while (0)
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#9 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 9 lines · Andreas Färber

Am 15.05.2012 18:08, schrieb Fabien Chouteau:

Quoted 8 lines · Andreas Färber
Quoted 6 lines

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

Quoted 9 lines
Quoted 2 lines

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

Ah, I see now that f is a different f here, logfile becomes log_cpu_state()'s f. Unfortunate naming.

Your fix looks OK then but I would recommend turning it into a static inline function to avoid the line breaks.

In this case I can rewrite all the macros in qemu-log.h to static inline.

Signature · 1 line
Fabien Chouteau
#10 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 13 lines · Fabien Chouteau
Quoted 12 lines · Andreas Färber

Am 15.05.2012 18:08, schrieb Fabien Chouteau:

Quoted 10 lines · Andreas Färber
Quoted 6 lines

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

Quoted 9 lines
Quoted 6 lines

Ah, I see now that f is a different f here, logfile becomes log_cpu_state()'s f. Unfortunate naming.

Your fix looks OK then but I would recommend turning it into a static inline function to avoid the line breaks.

In this case I can rewrite all the macros in qemu-log.h to static inline.

This is more complex than expected...

1 - GCC rejects inlined variadic functions

2 - Moving from macro to inline implies use of types defined in cpu.h (target_ulong, CPUArchState...), which I cannot include because qemu-log.h is used in tools (i.e. without cpu.h).

Conclusion: unless someone volunteer for a massive restructuring of qemu-log we have to keep the marcro for log_cpu_state.

Signature · 1 line
Fabien Chouteau
#11 lore raw

Re: [Qemu-devel] [PATCH] Avoid segfault in cpu_dump_state

Quoted 26 lines · Fabien Chouteau
Quoted 13 lines · Fabien Chouteau
Quoted 12 lines · Andreas Färber

Am 15.05.2012 18:08, schrieb Fabien Chouteau:

Quoted 10 lines · Andreas Färber
Quoted 6 lines

Am 15.05.2012 11:39, schrieb Fabien Chouteau:

Quoted 1 line

Do not call cpu_dump_state if logfile is NULL.

And where is log_cpu_state() being called from? Its caller is passing NULL already then.

No, logfile is a global variable. log_cpu_state() takes only CPUState and flags parameters.

Quoted 9 lines
Quoted 6 lines

Ah, I see now that f is a different f here, logfile becomes log_cpu_state()'s f. Unfortunate naming.

Your fix looks OK then but I would recommend turning it into a static inline function to avoid the line breaks.

In this case I can rewrite all the macros in qemu-log.h to static inline.

This is more complex than expected...

Quoted 9 lines

1 - GCC rejects inlined variadic functions

2 - Moving from macro to inline implies use of types defined in cpu.h (target_ulong, CPUArchState...), which I cannot include because qemu-log.h is used in tools (i.e. without cpu.h).

Conclusion: unless someone volunteer for a massive restructuring of qemu-log we have to keep the marcro for log_cpu_state.

So, are we good with the second patch?

Signature · 1 line
Fabien Chouteau