monitor: Fix crashes when using HMP commands without CPU
PATCH v2 1/1 monitor: Fix crashes when using HMP commands without CPU
When running certain HMP commands ("info registers", "info cpustats", "nmi", "memsave" or dumping virtual memory) with the "none" machine, QEMU crashes with a segmentation fault. This happens because the "none" machine does not have any CPUs by default, but these HMP commands did not check for a valid CPU pointer yet. Add such checks now, so we get an error message about the missing CPU instead.
---
v2:
- Added more checks to cover "nmi" and "memsave", too
hmp.c | 8 +++++++- monitor.c | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-)
hmp.c +7 -1
@@ -1013,8 +1013,14 @@ void hmp_memsave(Monitor *mon, const QDict *qdict)const char *filename =qdict_get_str (qdict,"filename" );uint64_t addr =qdict_get_int (qdict,"val" );Error *err =NULL ; +int cpu_index =monitor_get_cpu_index (); -qmp_memsave (addr, size, filename, true,monitor_get_cpu_index (), &err); +if (cpu_index <0 ) { +monitor_printf (mon,"No CPU available\n" ); +return ; + } + +qmp_memsave (addr, size, filename, true, cpu_index, &err);hmp_handle_error (mon, &err); }
monitor.c +31 -6
@@ -1025,6 +1025,9 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state (cur_mon->mon_cpu);
@@ -1033,17 +1036,27 @@ CPUState *mon_get_cpu(void)CPUArchState *mon_get_cpu_env (void ) { -return mon_get_cpu ()->env_ptr; +CPUState *cs =mon_get_cpu (); + +return cs ? cs->env_ptr :NULL ; }int monitor_get_cpu_index (void ) { -return mon_get_cpu ()->cpu_index; +CPUState *cs =mon_get_cpu (); + +return cs ? cs->cpu_index :-1 ; }static void hmp_info_registers (Monitor *mon,const QDict *qdict) { -cpu_dump_state (mon_get_cpu (), (FILE *)mon, monitor_fprintf,CPU_DUMP_FPU ); +CPUState *cs =mon_get_cpu (); + +if (!cs) { +monitor_printf (mon,"No CPU available\n" ); +return ; + } +cpu_dump_state (cs, (FILE *)mon, monitor_fprintf,CPU_DUMP_FPU ); }static void hmp_info_jit (Monitor *mon,const QDict *qdict)
@@ -1076,7 +1089,13 @@ static void hmp_info_history(Monitor *mon, const QDict *qdict)static void hmp_info_cpustats (Monitor *mon,const QDict *qdict) { -cpu_dump_statistics (mon_get_cpu (), (FILE *)mon, &monitor_fprintf,0 ); +CPUState *cs =mon_get_cpu (); + +if (!cs) { +monitor_printf (mon,"No CPU available\n" ); +return ; + } +cpu_dump_statistics (cs, (FILE *)mon, &monitor_fprintf,0 ); }static void hmp_info_trace_events (Monitor *mon,const QDict *qdict)
@@ -1235,6 +1254,12 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,int l, line_size, i, max_digits, len;uint8_t buf[16 ];uint64_t v; +CPUState *cs =mon_get_cpu (); + +if (!cs && (format =='i' || !is_physical)) { +monitor_printf (mon,"Can not dump without CPU\n" ); +return ; + }if (format =='i' ) {int flags =0 ;
@@ -1264,7 +1289,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize, flags = msr_le <<16 ; flags |= env->bfd_mach;#endif -monitor_disas (mon,mon_get_cpu (), addr, count, is_physical, flags); +monitor_disas (mon, cs, addr, count, is_physical, flags);return ; }
@@ -1303,7 +1328,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,if (is_physical) {cpu_physical_memory_read (addr, buf, l); }else { -if (cpu_memory_rw_debug (mon_get_cpu (), addr, buf, l,0 ) <0 ) { +if (cpu_memory_rw_debug (cs, addr, buf, l,0 ) <0 ) {monitor_printf (mon," Cannot access memory\n" );break ; }
Signature · 1 line
1.8.3.1
Quoted 21 lines · * Thomas Huth ([email protected]) wrote:
When running certain HMP commands ("info registers", "info cpustats", "nmi", "memsave" or dumping virtual memory) with the "none" machine, QEMU crashes with a segmentation fault. This happens because the "none" machine does not have any CPUs by default, but these HMP commands did not check for a valid CPU pointer yet. Add such checks now, so we get an error message about the missing CPU instead.
Signed-off-by Thomas Huth <[email protected]>---
v2:
- Added more checks to cover "nmi" and "memsave", toohmp.c | 8 +++++++- monitor.c | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-)hmp.c +0
-0
hmp.c void hmp_memsave(Monitor *mon, const QDict *qdict) · line 1021
Quoted 10 lines
hmp.c +5
-1@@ -1013,8 +1013,14 @@ void hmp_memsave(Monitor *mon, const QDict *qdict)const char *filename =qdict_get_str (qdict,"filename" );uint64_t addr =qdict_get_int (qdict,"val" );Error *err =NULL ; +int cpu_index =monitor_get_cpu_index (); -qmp_memsave (addr, size, filename, true,monitor_get_cpu_index (), &err); +if (cpu_index <0 ) { +monitor_printf (mon,"No CPU available\n" ); +return ; + }
OK, that includes UNASSIGNED_CPU_INDEX.
Quoted 26 lines
hmp.c +2
-0+ +qmp_memsave (addr, size, filename, true, cpu_index, &err);hmp_handle_error (mon, &err); }monitor.c +5
-1@@ -1025,6 +1025,9 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state (cur_mon->mon_cpu);@@ -1033,17 +1036,27 @@ CPUState *mon_get_cpu(void)CPUArchState *mon_get_cpu_env (void ) { -return mon_get_cpu ()->env_ptr; +CPUState *cs =mon_get_cpu (); +
monitor.c CPUState *mon_get_cpu(void) · line 1049
Quoted 10 lines
monitor.c +4
-1+return cs ? cs->env_ptr :NULL ; }int monitor_get_cpu_index (void ) { -return mon_get_cpu ()->cpu_index; +CPUState *cs =mon_get_cpu (); + +return cs ? cs->cpu_index :-1 ; }
OK, do you think that should use UNASSIGNED_CPU_INDEX explicitly rather than -1 ?
Quoted 62 lines
monitor.c +22
-4static void hmp_info_registers (Monitor *mon,const QDict *qdict) { -cpu_dump_state (mon_get_cpu (), (FILE *)mon, monitor_fprintf,CPU_DUMP_FPU ); +CPUState *cs =mon_get_cpu (); + +if (!cs) { +monitor_printf (mon,"No CPU available\n" ); +return ; + } +cpu_dump_state (cs, (FILE *)mon, monitor_fprintf,CPU_DUMP_FPU ); }static void hmp_info_jit (Monitor *mon,const QDict *qdict)@@ -1076,7 +1089,13 @@ static void hmp_info_history(Monitor *mon, const QDict *qdict)static void hmp_info_cpustats (Monitor *mon,const QDict *qdict) { -cpu_dump_statistics (mon_get_cpu (), (FILE *)mon, &monitor_fprintf,0 ); +CPUState *cs =mon_get_cpu (); + +if (!cs) { +monitor_printf (mon,"No CPU available\n" ); +return ; + } +cpu_dump_statistics (cs, (FILE *)mon, &monitor_fprintf,0 ); }static void hmp_info_trace_events (Monitor *mon,const QDict *qdict)@@ -1235,6 +1254,12 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,int l, line_size, i, max_digits, len;uint8_t buf[16 ];uint64_t v; +CPUState *cs =mon_get_cpu (); + +if (!cs && (format =='i' || !is_physical)) { +monitor_printf (mon,"Can not dump without CPU\n" ); +return ; + }if (format =='i' ) {int flags =0 ;@@ -1264,7 +1289,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize, flags = msr_le <<16 ; flags |= env->bfd_mach;#endif -monitor_disas (mon,mon_get_cpu (), addr, count, is_physical, flags); +monitor_disas (mon, cs, addr, count, is_physical, flags);return ; }@@ -1303,7 +1328,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,if (is_physical) {cpu_physical_memory_read (addr, buf, l); }else { -if (cpu_memory_rw_debug (mon_get_cpu (), addr, buf, l,0 ) <0 ) { +if (cpu_memory_rw_debug (cs, addr, buf, l,0 ) <0 ) {monitor_printf (mon," Cannot access memory\n" );break ; }Signature · 1 line
1.8.3.1
I'm sure we'll find loads more similar cases where -M none breaks stuff.
(I see you've cc'd to trivial so I'll let them take it).
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 64 lines · Dr. David Alan Gilbert
Quoted 21 lines · * Thomas Huth ([email protected]) wrote:
When running certain HMP commands ("info registers", "info cpustats", "nmi", "memsave" or dumping virtual memory) with the "none" machine, QEMU crashes with a segmentation fault. This happens because the "none" machine does not have any CPUs by default, but these HMP commands did not check for a valid CPU pointer yet. Add such checks now, so we get an error message about the missing CPU instead.
Signed-off-by Thomas Huth <[email protected]>---
v2:
- Added more checks to cover "nmi" and "memsave", toohmp.c | 8 +++++++- monitor.c | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-)hmp.c +0
-0hmp.c void hmp_memsave(Monitor *mon, const QDict *qdict) · line 1021
Quoted 10 lines
hmp.c +5
-1@@ -1013,8 +1013,14 @@ void hmp_memsave(Monitor *mon, const QDict *qdict)const char *filename =qdict_get_str (qdict,"filename" );uint64_t addr =qdict_get_int (qdict,"val" );Error *err =NULL ; +int cpu_index =monitor_get_cpu_index (); -qmp_memsave (addr, size, filename, true,monitor_get_cpu_index (), &err); +if (cpu_index <0 ) { +monitor_printf (mon,"No CPU available\n" ); +return ; + }OK, that includes
UNASSIGNED_CPU_INDEX.Quoted 29 lines
hmp.c +2
-0+ +qmp_memsave (addr, size, filename, true, cpu_index, &err);hmp_handle_error (mon, &err); }monitor.c +6
-1@@ -1025,6 +1025,9 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state (cur_mon->mon_cpu);@@ -1033,17 +1036,27 @@ CPUState *mon_get_cpu(void)CPUArchState *mon_get_cpu_env (void ) { -return mon_get_cpu ()->env_ptr; +CPUState *cs =mon_get_cpu (); + +return cs ? cs->env_ptr :NULL ; }
Quoted 10 lines
monitor.c CPUState *mon_get_cpu(void) · line 1049
Quoted 7 lines
monitor.c +3
-1int monitor_get_cpu_index (void ) { -return mon_get_cpu ()->cpu_index; +CPUState *cs =mon_get_cpu (); + +return cs ? cs->cpu_index :-1 ; }OK, do you think that should use
UNASSIGNED_CPU_INDEXexplicitly rather than -1 ?
I wasn't aware of the fact that we've even got a macro for this ... I'll send a v3 with that change.
Quoted 1 line
Reviewed-by Dr. David Alan Gilbert <[email protected]>
Thanks for the review!
Quoted 1 line
I'm sure we'll find loads more similar cases where -M none breaks stuff.
I've added two more cases (migration and gdbstub) to the "Potentially easy bugs" section on http://qemu-project.org/BiteSizedTasks now. I think these are simple and easy tasks to get started with QEMU hacking...
Thomas