cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

device-introspect-test.c (9666B)


      1/*
      2 * Device introspection test cases
      3 *
      4 * Copyright (c) 2015 Red Hat Inc.
      5 *
      6 * Authors:
      7 *  Markus Armbruster <armbru@redhat.com>,
      8 *
      9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
     10 * See the COPYING file in the top-level directory.
     11 */
     12
     13/*
     14 * Covers QMP device-list-properties and HMP device_add help.  We
     15 * currently don't check that their output makes sense, only that QEMU
     16 * survives.  Useful since we've had an astounding number of crash
     17 * bugs around here.
     18 */
     19
     20#include "qemu/osdep.h"
     21#include "qemu-common.h"
     22#include "qapi/qmp/qstring.h"
     23#include "qapi/qmp/qdict.h"
     24#include "qapi/qmp/qlist.h"
     25#include "libqos/libqtest.h"
     26
     27const char common_args[] = "-nodefaults -machine none";
     28
     29static QList *qom_list_types(QTestState * qts, const char *implements,
     30                             bool abstract)
     31{
     32    QDict *resp;
     33    QList *ret;
     34    QDict *args = qdict_new();
     35
     36    qdict_put_bool(args, "abstract", abstract);
     37    if (implements) {
     38        qdict_put_str(args, "implements", implements);
     39    }
     40    resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
     41                     args);
     42    g_assert(qdict_haskey(resp, "return"));
     43    ret = qdict_get_qlist(resp, "return");
     44    qobject_ref(ret);
     45    qobject_unref(resp);
     46    return ret;
     47}
     48
     49/* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
     50static QDict *qom_type_index(QList *types)
     51{
     52    QDict *index = qdict_new();
     53    QListEntry *e;
     54
     55    QLIST_FOREACH_ENTRY(types, e) {
     56        QDict *d = qobject_to(QDict, qlist_entry_obj(e));
     57        const char *name = qdict_get_str(d, "name");
     58        qobject_ref(d);
     59        qdict_put(index, name, d);
     60    }
     61    return index;
     62}
     63
     64/* Check if @parent is present in the parent chain of @type */
     65static bool qom_has_parent(QDict *index, const char *type, const char *parent)
     66{
     67    while (type) {
     68        QDict *d = qdict_get_qdict(index, type);
     69        const char *p = d && qdict_haskey(d, "parent") ?
     70                        qdict_get_str(d, "parent") :
     71                        NULL;
     72
     73        if (!strcmp(type, parent)) {
     74            return true;
     75        }
     76
     77        type = p;
     78    }
     79
     80    return false;
     81}
     82
     83/* Find an entry on a list returned by qom-list-types */
     84static QDict *type_list_find(QList *types, const char *name)
     85{
     86    QListEntry *e;
     87
     88    QLIST_FOREACH_ENTRY(types, e) {
     89        QDict *d = qobject_to(QDict, qlist_entry_obj(e));
     90        const char *ename = qdict_get_str(d, "name");
     91        if (!strcmp(ename, name)) {
     92            return d;
     93        }
     94    }
     95
     96    return NULL;
     97}
     98
     99static QList *device_type_list(QTestState *qts, bool abstract)
    100{
    101    return qom_list_types(qts, "device", abstract);
    102}
    103
    104static void test_one_device(QTestState *qts, const char *type)
    105{
    106    QDict *resp;
    107    char *help, *escaped;
    108    GRegex *comma;
    109
    110    g_test_message("Testing device '%s'", type);
    111
    112    resp = qtest_qmp(qts, "{'execute': 'device-list-properties',"
    113                          " 'arguments': {'typename': %s}}",
    114               type);
    115    qobject_unref(resp);
    116
    117    comma = g_regex_new(",", 0, 0, NULL);
    118    escaped = g_regex_replace_literal(comma, type, -1, 0, ",,", 0, NULL);
    119    g_regex_unref(comma);
    120
    121    help = qtest_hmp(qts, "device_add \"%s,help\"", escaped);
    122    g_free(help);
    123    g_free(escaped);
    124}
    125
    126static void test_device_intro_list(void)
    127{
    128    QList *types;
    129    char *help;
    130    QTestState *qts;
    131
    132    qts = qtest_init(common_args);
    133
    134    types = device_type_list(qts, true);
    135    qobject_unref(types);
    136
    137    help = qtest_hmp(qts, "device_add help");
    138    g_free(help);
    139
    140    qtest_quit(qts);
    141}
    142
    143/*
    144 * Ensure all entries returned by qom-list-types implements=<parent>
    145 * have <parent> as a parent.
    146 */
    147static void test_qom_list_parents(QTestState *qts, const char *parent)
    148{
    149    QList *types;
    150    QListEntry *e;
    151    QDict *index;
    152
    153    types = qom_list_types(qts, parent, true);
    154    index = qom_type_index(types);
    155
    156    QLIST_FOREACH_ENTRY(types, e) {
    157        QDict *d = qobject_to(QDict, qlist_entry_obj(e));
    158        const char *name = qdict_get_str(d, "name");
    159
    160        g_assert(qom_has_parent(index, name, parent));
    161    }
    162
    163    qobject_unref(types);
    164    qobject_unref(index);
    165}
    166
    167static void test_qom_list_fields(void)
    168{
    169    QList *all_types;
    170    QList *non_abstract;
    171    QListEntry *e;
    172    QTestState *qts;
    173
    174    qts = qtest_init(common_args);
    175
    176    all_types = qom_list_types(qts, NULL, true);
    177    non_abstract = qom_list_types(qts, NULL, false);
    178
    179    QLIST_FOREACH_ENTRY(all_types, e) {
    180        QDict *d = qobject_to(QDict, qlist_entry_obj(e));
    181        const char *name = qdict_get_str(d, "name");
    182        bool abstract = qdict_haskey(d, "abstract") ?
    183                        qdict_get_bool(d, "abstract") :
    184                        false;
    185        bool expected_abstract = !type_list_find(non_abstract, name);
    186
    187        g_assert(abstract == expected_abstract);
    188    }
    189
    190    test_qom_list_parents(qts, "object");
    191    test_qom_list_parents(qts, "device");
    192    test_qom_list_parents(qts, "sys-bus-device");
    193
    194    qobject_unref(all_types);
    195    qobject_unref(non_abstract);
    196    qtest_quit(qts);
    197}
    198
    199static void test_device_intro_none(void)
    200{
    201    QTestState *qts = qtest_init(common_args);
    202    g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
    203    g_autofree char *qom_tree_end = NULL;
    204    g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
    205    g_autofree char *qtree_end = NULL;
    206
    207    test_one_device(qts, "nonexistent");
    208
    209    /* Make sure that really nothing changed in the trees */
    210    qom_tree_end = qtest_hmp(qts, "info qom-tree");
    211    g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
    212    qtree_end = qtest_hmp(qts, "info qtree");
    213    g_assert_cmpstr(qtree_start, ==, qtree_end);
    214
    215    qtest_quit(qts);
    216}
    217
    218static void test_device_intro_abstract(void)
    219{
    220    QTestState *qts = qtest_init(common_args);
    221    g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
    222    g_autofree char *qom_tree_end = NULL;
    223    g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
    224    g_autofree char *qtree_end = NULL;
    225
    226    test_one_device(qts, "device");
    227
    228    /* Make sure that really nothing changed in the trees */
    229    qom_tree_end = qtest_hmp(qts, "info qom-tree");
    230    g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
    231    qtree_end = qtest_hmp(qts, "info qtree");
    232    g_assert_cmpstr(qtree_start, ==, qtree_end);
    233
    234    qtest_quit(qts);
    235}
    236
    237static void test_device_intro_concrete(const void *args)
    238{
    239    QList *types;
    240    QListEntry *entry;
    241    const char *type;
    242    QTestState *qts = qtest_init(args);
    243    g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
    244    g_autofree char *qom_tree_end = NULL;
    245    g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
    246    g_autofree char *qtree_end = NULL;
    247
    248    types = device_type_list(qts, false);
    249
    250    QLIST_FOREACH_ENTRY(types, entry) {
    251        type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
    252                                 "name");
    253        g_assert(type);
    254        test_one_device(qts, type);
    255    }
    256
    257    /*
    258     * Some devices leave dangling pointers in QOM behind.
    259     * "info qom-tree" or "info qtree" have a good chance at crashing then.
    260     * Also make sure that the tree did not change.
    261     */
    262    qom_tree_end = qtest_hmp(qts, "info qom-tree");
    263    g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
    264
    265    qtree_end = qtest_hmp(qts, "info qtree");
    266    g_assert_cmpstr(qtree_start, ==, qtree_end);
    267
    268    qobject_unref(types);
    269    qtest_quit(qts);
    270    g_free((void *)args);
    271}
    272
    273static void test_abstract_interfaces(void)
    274{
    275    QList *all_types;
    276    QListEntry *e;
    277    QDict *index;
    278    QTestState *qts;
    279
    280    qts = qtest_init(common_args);
    281
    282    all_types = qom_list_types(qts, "interface", true);
    283    index = qom_type_index(all_types);
    284
    285    QLIST_FOREACH_ENTRY(all_types, e) {
    286        QDict *d = qobject_to(QDict, qlist_entry_obj(e));
    287        const char *name = qdict_get_str(d, "name");
    288
    289        /*
    290         * qom-list-types implements=interface returns all types
    291         * that implement _any_ interface (not just interface
    292         * types), so skip the ones that don't have "interface"
    293         * on the parent type chain.
    294         */
    295        if (!qom_has_parent(index, name, "interface")) {
    296            /* Not an interface type */
    297            continue;
    298        }
    299
    300        g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
    301    }
    302
    303    qobject_unref(all_types);
    304    qobject_unref(index);
    305    qtest_quit(qts);
    306}
    307
    308static void add_machine_test_case(const char *mname)
    309{
    310    char *path, *args;
    311
    312    path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname);
    313    args = g_strdup_printf("-M %s", mname);
    314    qtest_add_data_func(path, args, test_device_intro_concrete);
    315    g_free(path);
    316
    317    path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname);
    318    args = g_strdup_printf("-nodefaults -M %s", mname);
    319    qtest_add_data_func(path, args, test_device_intro_concrete);
    320    g_free(path);
    321}
    322
    323int main(int argc, char **argv)
    324{
    325    g_test_init(&argc, &argv, NULL);
    326
    327    qtest_add_func("device/introspect/list", test_device_intro_list);
    328    qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
    329    qtest_add_func("device/introspect/none", test_device_intro_none);
    330    qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
    331    qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
    332    if (g_test_quick()) {
    333        qtest_add_data_func("device/introspect/concrete/defaults/none",
    334                            g_strdup(common_args), test_device_intro_concrete);
    335    } else {
    336        qtest_cb_for_every_machine(add_machine_test_case, true);
    337    }
    338
    339    return g_test_run();
    340}