Peter Maydell <[email protected]> writes:

> As with add_cpuid_test(), the add_feature_test() function also
> allocates memory that is leaked if the test case is not run.  Fix
> this in the same way, by moving all the allocations into
> test_feature_flag() and passing it a pointer to a struct with the
> necessary parameters which is a compile-time constant.
>
> Signed-off-by: Peter Maydell <[email protected]>
> ---
>  tests/qtest/test-x86-cpuid-compat.c | 134 +++++++++++++++-------------
>  1 file changed, 72 insertions(+), 62 deletions(-)
>
> diff --git a/tests/qtest/test-x86-cpuid-compat.c 
> b/tests/qtest/test-x86-cpuid-compat.c
> index fc90a46d37..17c0965827 100644
> --- a/tests/qtest/test-x86-cpuid-compat.c
> +++ b/tests/qtest/test-x86-cpuid-compat.c
> @@ -94,8 +94,12 @@ static void test_cpuid_prop(const void *data)
>  
>  /* Parameters to a add_feature_test() test case */
>  typedef struct FeatureTestArgs {
> -    /* cmdline to start QEMU */
> -    const char *cmdline;
> +    /* Test name */
> +    const char *name;
> +    /* CPU type */
> +    const char *cpu;
> +    /* CPU features, may be NULL */
> +    const char *cpufeat;
>      /*
>       * cpuid-input-eax and cpuid-input-ecx values to look for,
>       * in "feature-words" and "filtered-features" properties.
> @@ -140,10 +144,17 @@ static void test_feature_flag(const void *data)
>  {
>      const FeatureTestArgs *args = data;
>      char *path;
> +    char *cmdline;
>      QList *present, *filtered;
>      uint32_t value;
>  
> -    qtest_start(args->cmdline);
> +    if (args->cpufeat) {
> +        cmdline = g_strdup_printf("-cpu %s,%s", args->cpu, args->cpufeat);
> +    } else {
> +        cmdline = g_strdup_printf("-cpu %s", args->cpu);
> +    }
> +
> +    qtest_start(cmdline);
>      path = get_cpu0_qom_path();
>      present = qobject_to(QList, qom_get(path, "feature-words"));
>      filtered = qobject_to(QList, qom_get(path, "filtered-features"));
> @@ -156,40 +167,7 @@ static void test_feature_flag(const void *data)
>      qobject_unref(present);
>      qobject_unref(filtered);
>      g_free(path);
> -    g_free((void *)args->cmdline);
> -    g_free((void *)data);
> -}
> -
> -/*
> - * Add test case to ensure that a given feature flag is set in
> - * either "feature-words" or "filtered-features", when running QEMU
> - * using cmdline
> - */
> -static void add_feature_test(const char *name, const char *cpu,
> -                             const char *cpufeat, uint32_t eax,
> -                             uint32_t ecx, const char *reg,
> -                             int bitnr, bool expected_value)
> -{
> -    FeatureTestArgs *args = g_new0(FeatureTestArgs, 1);
> -    char *cmdline;
> -
> -    if (!qtest_has_cpu_model(cpu)) {
> -        return;
> -    }
> -
> -    if (cpufeat) {
> -        cmdline = g_strdup_printf("-cpu %s,%s", cpu, cpufeat);
> -    } else {
> -        cmdline = g_strdup_printf("-cpu %s", cpu);
> -    }
> -
> -    args->cmdline = cmdline;
> -    args->in_eax = eax;
> -    args->in_ecx = ecx;
> -    args->reg = reg;
> -    args->bitnr = bitnr;
> -    args->expected_value = expected_value;
> -    qtest_add_data_func(name, args, test_feature_flag);
> +    g_free(cmdline);
>  }
>  
>  static void test_plus_minus_subprocess(void)
> @@ -381,6 +359,56 @@ static const CpuidTestArgs cpuid_tests[] = {
>      },
>  };
>  
> +/*
> + * Test cases to ensure that a given feature flag is set in
> + * either "feature-words" or "filtered-features", when running QEMU
> + * using cmdline
> + */
> +static const FeatureTestArgs feature_tests[] = {
> +    /* Test feature parsing */
> +    {
> +        "x86/cpuid/features/plus",
> +        "486", "+arat",
> +        6, 0, "EAX", 2, true,
> +    },
> +    {
> +        "x86/cpuid/features/minus",
> +        "pentium", "-mmx",
> +        1, 0, "EDX", 23, false,
> +    },
> +    {
> +        "x86/cpuid/features/on",
> +        "486", "arat=on",
> +        6, 0, "EAX", 2, true,
> +    },
> +    {
> +        "x86/cpuid/features/off",
> +        "pentium", "mmx=off",
> +        1, 0, "EDX", 23, false,
> +    },
> +
> +    {
> +        "x86/cpuid/features/max-plus-invtsc",
> +        "max" , "+invtsc",
> +        0x80000007, 0, "EDX", 8, true,
> +    },
> +    {
> +        "x86/cpuid/features/max-invtsc-on",
> +        "max", "invtsc=on",
> +        0x80000007, 0, "EDX", 8, true,
> +    },
> +    {
> +        "x86/cpuid/features/max-minus-mmx",
> +        "max", "-mmx",
> +        1, 0, "EDX", 23, false,
> +    },
> +    {
> +        "x86/cpuid/features/max-invtsc-on,mmx=off",
> +        "max", "mmx=off",
> +        1, 0, "EDX", 23, false,
> +    },
> +};
> +
>  int main(int argc, char **argv)
>  {
>      g_test_init(&argc, &argv, NULL);
> @@ -397,32 +425,14 @@ int main(int argc, char **argv)
>                              &cpuid_tests[i], test_cpuid_prop);
>      }
>  
> -    /* Test feature parsing */
> -    add_feature_test("x86/cpuid/features/plus",
> -                     "486", "+arat",
> -                     6, 0, "EAX", 2, true);
> -    add_feature_test("x86/cpuid/features/minus",
> -                     "pentium", "-mmx",
> -                     1, 0, "EDX", 23, false);
> -    add_feature_test("x86/cpuid/features/on",
> -                     "486", "arat=on",
> -                     6, 0, "EAX", 2, true);
> -    add_feature_test("x86/cpuid/features/off",
> -                     "pentium", "mmx=off",
> -                     1, 0, "EDX", 23, false);
>  
> -    add_feature_test("x86/cpuid/features/max-plus-invtsc",
> -                     "max" , "+invtsc",
> -                     0x80000007, 0, "EDX", 8, true);
> -    add_feature_test("x86/cpuid/features/max-invtsc-on",
> -                     "max", "invtsc=on",
> -                     0x80000007, 0, "EDX", 8, true);
> -    add_feature_test("x86/cpuid/features/max-minus-mmx",
> -                     "max", "-mmx",
> -                     1, 0, "EDX", 23, false);
> -    add_feature_test("x86/cpuid/features/max-invtsc-on,mmx=off",
> -                     "max", "mmx=off",
> -                     1, 0, "EDX", 23, false);
> +    for (int i = 0; i < ARRAY_SIZE(feature_tests); i++) {
> +        if (!qtest_has_cpu_model(feature_tests[i].cpu)) {
> +            continue;
> +        }
> +        qtest_add_data_func(feature_tests[i].name,
> +                            &feature_tests[i], test_feature_flag);
> +    }
>  
>      return g_test_run();
>  }

Reviewed-by: Fabiano Rosas <[email protected]>

Reply via email to