target: Improve error reporting for CpuModelInfo member @props

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>
Message-ID: <20240305145919.2186971-4-armbru@redhat.com>
Acked-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
This commit is contained in:
Markus Armbruster 2024-03-05 15:59:17 +01:00
parent 68192a5ffd
commit 8934643a0e
5 changed files with 24 additions and 17 deletions

View File

@ -157,7 +157,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;

View File

@ -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;
}

View File

@ -128,6 +128,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;
@ -136,7 +137,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;
}
@ -197,7 +198,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);

View File

@ -97,7 +97,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;
@ -127,8 +127,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;
@ -215,7 +218,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;
@ -253,12 +256,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;
@ -330,13 +333,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;

View File

@ -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);