diff mbox series

[3/4] target: Improve error reporting for CpuModelInfo member @props

Message ID 20240305145919.2186971-4-armbru@redhat.com (mailing list archive)
State New, archived
Headers show
Series target: Improve error reporting for CpuModelInfo member @props | expand

Commit Message

Markus Armbruster March 5, 2024, 2:59 p.m. UTC
query-cpu-model-comparison, query-cpu-model-baseline, and
query-cpu-model-expansion take CpuModelInfo arguments.  Errors in
@props members of these arguments are reported for 'props', without
further context.  For instance, s390x rejects

    {"execute": "query-cpu-model-comparison", "arguments": {"modela": {"name": "z13", "props": {}}, "modelb": {"name": "z14", "props": []}}}

with

    {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'props', expected: object"}}

This is unusual; the common QAPI unmarshaling machinery would complain
about 'modelb.props'.  Our hand-written code to visit the @props
member neglects to provide the context.

Tweak it so it provides it.  The command above now fails with

    {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'modelb.props', expected: dict"}}

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 target/arm/arm-qmp-cmds.c        |  2 +-
 target/i386/cpu-sysemu.c         | 11 ++++++-----
 target/riscv/riscv-qmp-cmds.c    |  6 ++++--
 target/s390x/cpu_models_sysemu.c | 17 ++++++++++-------
 tests/qtest/arm-cpu-features.c   |  5 +++--
 5 files changed, 24 insertions(+), 17 deletions(-)

Comments

Daniel Henrique Barboza March 6, 2024, 1:05 p.m. UTC | #1
On 3/5/24 11:59, Markus Armbruster wrote:
> query-cpu-model-comparison, query-cpu-model-baseline, and
> query-cpu-model-expansion take CpuModelInfo arguments.  Errors in
> @props members of these arguments are reported for 'props', without
> further context.  For instance, s390x rejects
> 
>      {"execute": "query-cpu-model-comparison", "arguments": {"modela": {"name": "z13", "props": {}}, "modelb": {"name": "z14", "props": []}}}
> 
> with
> 
>      {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'props', expected: object"}}
> 
> This is unusual; the common QAPI unmarshaling machinery would complain
> about 'modelb.props'.  Our hand-written code to visit the @props
> member neglects to provide the context.
> 
> Tweak it so it provides it.  The command above now fails with
> 
>      {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'modelb.props', expected: dict"}}
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---


For RISC-V changes:

Acked-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>

>   target/arm/arm-qmp-cmds.c        |  2 +-
>   target/i386/cpu-sysemu.c         | 11 ++++++-----
>   target/riscv/riscv-qmp-cmds.c    |  6 ++++--
>   target/s390x/cpu_models_sysemu.c | 17 ++++++++++-------
>   tests/qtest/arm-cpu-features.c   |  5 +++--
>   5 files changed, 24 insertions(+), 17 deletions(-)
> 
> diff --git a/target/arm/arm-qmp-cmds.c b/target/arm/arm-qmp-cmds.c
> index 0167759730..0efbee8af8 100644
> --- a/target/arm/arm-qmp-cmds.c
> +++ b/target/arm/arm-qmp-cmds.c
> @@ -158,7 +158,7 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
>           Error *err = NULL;
>   
>           visitor = qobject_input_visitor_new(model->props);
> -        if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
> +        if (!visit_start_struct(visitor, "model.props", NULL, 0, errp)) {
>               visit_free(visitor);
>               object_unref(obj);
>               return NULL;
> diff --git a/target/i386/cpu-sysemu.c b/target/i386/cpu-sysemu.c
> index df91f97827..3f9093d285 100644
> --- a/target/i386/cpu-sysemu.c
> +++ b/target/i386/cpu-sysemu.c
> @@ -130,14 +130,15 @@ static void x86_cpu_to_dict_full(X86CPU *cpu, QDict *props)
>       }
>   }
>   
> -static void object_apply_props(Object *obj, QObject *props, Error **errp)
> +static void object_apply_props(Object *obj, QObject *props,
> +                               const char *props_arg_name, Error **errp)
>   {
>       Visitor *visitor;
>       QDict *qdict;
>       const QDictEntry *prop;
>   
>       visitor = qobject_input_visitor_new(props);
> -    if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
> +    if (!visit_start_struct(visitor, props_arg_name, NULL, 0, errp)) {
>           visit_free(visitor);
>           return;
>       }
> @@ -158,7 +159,7 @@ out:
>   
>   /* Create X86CPU object according to model+props specification */
>   static X86CPU *x86_cpu_from_model(const char *model, QObject *props,
> -                                  Error **errp)
> +                                  const char *props_arg_name, Error **errp)
>   {
>       X86CPU *xc = NULL;
>       X86CPUClass *xcc;
> @@ -172,7 +173,7 @@ static X86CPU *x86_cpu_from_model(const char *model, QObject *props,
>   
>       xc = X86_CPU(object_new_with_class(OBJECT_CLASS(xcc)));
>       if (props) {
> -        object_apply_props(OBJECT(xc), props, &err);
> +        object_apply_props(OBJECT(xc), props, props_arg_name, &err);
>           if (err) {
>               goto out;
>           }
> @@ -203,7 +204,7 @@ qmp_query_cpu_model_expansion(CpuModelExpansionType type,
>       QDict *props = NULL;
>       const char *base_name;
>   
> -    xc = x86_cpu_from_model(model->name, model->props, &err);
> +    xc = x86_cpu_from_model(model->name, model->props, "model.props", &err);
>       if (err) {
>           goto out;
>       }
> diff --git a/target/riscv/riscv-qmp-cmds.c b/target/riscv/riscv-qmp-cmds.c
> index 69dde0c3e7..ee7465297d 100644
> --- a/target/riscv/riscv-qmp-cmds.c
> +++ b/target/riscv/riscv-qmp-cmds.c
> @@ -129,6 +129,7 @@ static void riscv_obj_add_profiles_qdict(Object *obj, QDict *qdict_out)
>   }
>   
>   static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
> +                                           const char *props_arg_name,
>                                              Error **errp)
>   {
>       const QDict *qdict_in;
> @@ -137,7 +138,7 @@ static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
>       Error *local_err = NULL;
>   
>       visitor = qobject_input_visitor_new(props);
> -    if (!visit_start_struct(visitor, "props", NULL, 0, &local_err)) {
> +    if (!visit_start_struct(visitor, props_arg_name, NULL, 0, &local_err)) {
>           goto err;
>       }
>   
> @@ -198,7 +199,8 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
>       }
>   
>       if (model->props) {
> -        riscv_cpuobj_validate_qdict_in(obj, model->props, &local_err);
> +        riscv_cpuobj_validate_qdict_in(obj, model->props, "model.props",
> +                                       &local_err);
>           if (local_err) {
>               error_propagate(errp, local_err);
>               object_unref(obj);
> diff --git a/target/s390x/cpu_models_sysemu.c b/target/s390x/cpu_models_sysemu.c
> index ef19724888..8338084d00 100644
> --- a/target/s390x/cpu_models_sysemu.c
> +++ b/target/s390x/cpu_models_sysemu.c
> @@ -98,7 +98,7 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
>   }
>   
>   static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
> -                                Error **errp)
> +                                const char *info_arg_name, Error **errp)
>   {
>       Error *err = NULL;
>       const QDict *qdict;
> @@ -128,8 +128,11 @@ static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
>       }
>   
>       if (info->props) {
> +        g_autofree const char *props_name = g_strdup_printf("%s.props",
> +                                                            info_arg_name);
> +
>           visitor = qobject_input_visitor_new(info->props);
> -        if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
> +        if (!visit_start_struct(visitor, props_name, NULL, 0, errp)) {
>               visit_free(visitor);
>               object_unref(obj);
>               return;
> @@ -216,7 +219,7 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
>       bool delta_changes = false;
>   
>       /* convert it to our internal representation */
> -    cpu_model_from_info(&s390_model, model, &err);
> +    cpu_model_from_info(&s390_model, model, "model", &err);
>       if (err) {
>           error_propagate(errp, err);
>           return NULL;
> @@ -254,12 +257,12 @@ CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
>       S390CPUModel modela, modelb;
>   
>       /* convert both models to our internal representation */
> -    cpu_model_from_info(&modela, infoa, &err);
> +    cpu_model_from_info(&modela, infoa, "modela", &err);
>       if (err) {
>           error_propagate(errp, err);
>           return NULL;
>       }
> -    cpu_model_from_info(&modelb, infob, &err);
> +    cpu_model_from_info(&modelb, infob, "modelb", &err);
>       if (err) {
>           error_propagate(errp, err);
>           return NULL;
> @@ -331,13 +334,13 @@ CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
>       uint8_t max_gen;
>   
>       /* convert both models to our internal representation */
> -    cpu_model_from_info(&modela, infoa, &err);
> +    cpu_model_from_info(&modela, infoa, "modela", &err);
>       if (err) {
>           error_propagate(errp, err);
>           return NULL;
>       }
>   
> -    cpu_model_from_info(&modelb, infob, &err);
> +    cpu_model_from_info(&modelb, infob, "modelb", &err);
>       if (err) {
>           error_propagate(errp, err);
>           return NULL;
> diff --git a/tests/qtest/arm-cpu-features.c b/tests/qtest/arm-cpu-features.c
> index 1daceb2e31..9d6e6190d5 100644
> --- a/tests/qtest/arm-cpu-features.c
> +++ b/tests/qtest/arm-cpu-features.c
> @@ -213,7 +213,8 @@ static void assert_bad_props(QTestState *qts, const char *cpu_type)
>       error = resp_get_error(resp);
>       g_assert(error);
>       g_assert_cmpstr(error, ==,
> -                    "Invalid parameter type for 'props', expected: object");
> +                    "Invalid parameter type for 'model.props',"
> +                    " expected: object");
>       qobject_unref(resp);
>   }
>   
> @@ -446,7 +447,7 @@ static void test_query_cpu_model_expansion(const void *data)
>       assert_bad_props(qts, "max");
>       assert_error(qts, "foo", "The CPU type 'foo' is not a recognized "
>                    "ARM CPU type", NULL);
> -    assert_error(qts, "max", "Parameter 'props.not-a-prop' is unexpected",
> +    assert_error(qts, "max", "Parameter 'model.props.not-a-prop' is unexpected",
>                    "{ 'not-a-prop': false }");
>       assert_error(qts, "host", "The CPU type 'host' requires KVM", NULL);
>
diff mbox series

Patch

diff --git a/target/arm/arm-qmp-cmds.c b/target/arm/arm-qmp-cmds.c
index 0167759730..0efbee8af8 100644
--- a/target/arm/arm-qmp-cmds.c
+++ b/target/arm/arm-qmp-cmds.c
@@ -158,7 +158,7 @@  CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
         Error *err = NULL;
 
         visitor = qobject_input_visitor_new(model->props);
-        if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
+        if (!visit_start_struct(visitor, "model.props", NULL, 0, errp)) {
             visit_free(visitor);
             object_unref(obj);
             return NULL;
diff --git a/target/i386/cpu-sysemu.c b/target/i386/cpu-sysemu.c
index df91f97827..3f9093d285 100644
--- a/target/i386/cpu-sysemu.c
+++ b/target/i386/cpu-sysemu.c
@@ -130,14 +130,15 @@  static void x86_cpu_to_dict_full(X86CPU *cpu, QDict *props)
     }
 }
 
-static void object_apply_props(Object *obj, QObject *props, Error **errp)
+static void object_apply_props(Object *obj, QObject *props,
+                               const char *props_arg_name, Error **errp)
 {
     Visitor *visitor;
     QDict *qdict;
     const QDictEntry *prop;
 
     visitor = qobject_input_visitor_new(props);
-    if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
+    if (!visit_start_struct(visitor, props_arg_name, NULL, 0, errp)) {
         visit_free(visitor);
         return;
     }
@@ -158,7 +159,7 @@  out:
 
 /* Create X86CPU object according to model+props specification */
 static X86CPU *x86_cpu_from_model(const char *model, QObject *props,
-                                  Error **errp)
+                                  const char *props_arg_name, Error **errp)
 {
     X86CPU *xc = NULL;
     X86CPUClass *xcc;
@@ -172,7 +173,7 @@  static X86CPU *x86_cpu_from_model(const char *model, QObject *props,
 
     xc = X86_CPU(object_new_with_class(OBJECT_CLASS(xcc)));
     if (props) {
-        object_apply_props(OBJECT(xc), props, &err);
+        object_apply_props(OBJECT(xc), props, props_arg_name, &err);
         if (err) {
             goto out;
         }
@@ -203,7 +204,7 @@  qmp_query_cpu_model_expansion(CpuModelExpansionType type,
     QDict *props = NULL;
     const char *base_name;
 
-    xc = x86_cpu_from_model(model->name, model->props, &err);
+    xc = x86_cpu_from_model(model->name, model->props, "model.props", &err);
     if (err) {
         goto out;
     }
diff --git a/target/riscv/riscv-qmp-cmds.c b/target/riscv/riscv-qmp-cmds.c
index 69dde0c3e7..ee7465297d 100644
--- a/target/riscv/riscv-qmp-cmds.c
+++ b/target/riscv/riscv-qmp-cmds.c
@@ -129,6 +129,7 @@  static void riscv_obj_add_profiles_qdict(Object *obj, QDict *qdict_out)
 }
 
 static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
+                                           const char *props_arg_name,
                                            Error **errp)
 {
     const QDict *qdict_in;
@@ -137,7 +138,7 @@  static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
     Error *local_err = NULL;
 
     visitor = qobject_input_visitor_new(props);
-    if (!visit_start_struct(visitor, "props", NULL, 0, &local_err)) {
+    if (!visit_start_struct(visitor, props_arg_name, NULL, 0, &local_err)) {
         goto err;
     }
 
@@ -198,7 +199,8 @@  CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
     }
 
     if (model->props) {
-        riscv_cpuobj_validate_qdict_in(obj, model->props, &local_err);
+        riscv_cpuobj_validate_qdict_in(obj, model->props, "model.props",
+                                       &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             object_unref(obj);
diff --git a/target/s390x/cpu_models_sysemu.c b/target/s390x/cpu_models_sysemu.c
index ef19724888..8338084d00 100644
--- a/target/s390x/cpu_models_sysemu.c
+++ b/target/s390x/cpu_models_sysemu.c
@@ -98,7 +98,7 @@  CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 }
 
 static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
-                                Error **errp)
+                                const char *info_arg_name, Error **errp)
 {
     Error *err = NULL;
     const QDict *qdict;
@@ -128,8 +128,11 @@  static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,
     }
 
     if (info->props) {
+        g_autofree const char *props_name = g_strdup_printf("%s.props",
+                                                            info_arg_name);
+
         visitor = qobject_input_visitor_new(info->props);
-        if (!visit_start_struct(visitor, "props", NULL, 0, errp)) {
+        if (!visit_start_struct(visitor, props_name, NULL, 0, errp)) {
             visit_free(visitor);
             object_unref(obj);
             return;
@@ -216,7 +219,7 @@  CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
     bool delta_changes = false;
 
     /* convert it to our internal representation */
-    cpu_model_from_info(&s390_model, model, &err);
+    cpu_model_from_info(&s390_model, model, "model", &err);
     if (err) {
         error_propagate(errp, err);
         return NULL;
@@ -254,12 +257,12 @@  CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
     S390CPUModel modela, modelb;
 
     /* convert both models to our internal representation */
-    cpu_model_from_info(&modela, infoa, &err);
+    cpu_model_from_info(&modela, infoa, "modela", &err);
     if (err) {
         error_propagate(errp, err);
         return NULL;
     }
-    cpu_model_from_info(&modelb, infob, &err);
+    cpu_model_from_info(&modelb, infob, "modelb", &err);
     if (err) {
         error_propagate(errp, err);
         return NULL;
@@ -331,13 +334,13 @@  CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
     uint8_t max_gen;
 
     /* convert both models to our internal representation */
-    cpu_model_from_info(&modela, infoa, &err);
+    cpu_model_from_info(&modela, infoa, "modela", &err);
     if (err) {
         error_propagate(errp, err);
         return NULL;
     }
 
-    cpu_model_from_info(&modelb, infob, &err);
+    cpu_model_from_info(&modelb, infob, "modelb", &err);
     if (err) {
         error_propagate(errp, err);
         return NULL;
diff --git a/tests/qtest/arm-cpu-features.c b/tests/qtest/arm-cpu-features.c
index 1daceb2e31..9d6e6190d5 100644
--- a/tests/qtest/arm-cpu-features.c
+++ b/tests/qtest/arm-cpu-features.c
@@ -213,7 +213,8 @@  static void assert_bad_props(QTestState *qts, const char *cpu_type)
     error = resp_get_error(resp);
     g_assert(error);
     g_assert_cmpstr(error, ==,
-                    "Invalid parameter type for 'props', expected: object");
+                    "Invalid parameter type for 'model.props',"
+                    " expected: object");
     qobject_unref(resp);
 }
 
@@ -446,7 +447,7 @@  static void test_query_cpu_model_expansion(const void *data)
     assert_bad_props(qts, "max");
     assert_error(qts, "foo", "The CPU type 'foo' is not a recognized "
                  "ARM CPU type", NULL);
-    assert_error(qts, "max", "Parameter 'props.not-a-prop' is unexpected",
+    assert_error(qts, "max", "Parameter 'model.props.not-a-prop' is unexpected",
                  "{ 'not-a-prop': false }");
     assert_error(qts, "host", "The CPU type 'host' requires KVM", NULL);