Skip to contentSkip to conversation

libx86: Remaining serialisation logic

PATCH Superseded Andrew Cooper 2 participants 10 messages Jan 4, 2019 – Feb 25, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover libx86: Remaining serialisation logic

I've lost count of exactly which version this is, due to multiple postings of various sub series, and several substantial rebases.

I think I've addressed all outstanding review content, so lets start from v1 again.

Andrew Cooper (2):
libx86: Introduce a helper to deserialise cpuid_policy objects tools/cpu-policy: Add unit tests and a fuzzing harness

Roger Pau Monné (1):
libx86: introduce a helper to deserialise msr_policy objects

 tools/fuzz/cpu-policy/.gitignore          |   1 +
 tools/fuzz/cpu-policy/Makefile            |  27 ++++
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 117 ++++++++++++++
 tools/tests/Makefile                      |   1 +
 tools/tests/cpu-policy/.gitignore         |   1 +
 tools/tests/cpu-policy/Makefile           |  27 ++++
 tools/tests/cpu-policy/test-cpu-policy.c  | 247 ++++++++++++++++++++++++++++++
 xen/include/xen/lib/x86/cpuid.h           |  23 ++-
 xen/include/xen/lib/x86/msr.h             |  21 +++
 xen/lib/x86/cpuid.c                       | 106 +++++++++++++
 xen/lib/x86/msr.c                         |  67 ++++++++
 xen/lib/x86/private.h                     |  14 ++
 12 files changed, 651 insertions(+), 1 deletion(-)
 create mode 100644 tools/fuzz/cpu-policy/.gitignore
 create mode 100644 tools/fuzz/cpu-policy/Makefile
 create mode 100644 tools/fuzz/cpu-policy/afl-policy-fuzzer.c
 create mode 100644 tools/tests/cpu-policy/.gitignore
 create mode 100644 tools/tests/cpu-policy/Makefile
 create mode 100644 tools/tests/cpu-policy/test-cpu-policy.c
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/3 libx86: Introduce a helper to deserialise cpuid_policy objects

As with the serialise side, Xen's copy_from_guest API is used, with a compatibility wrapper for the userspace build.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

---
 xen/include/xen/lib/x86/cpuid.h |  21 ++++++++
 xen/lib/x86/cpuid.c             | 106 ++++++++++++++++++++++++++++++++++++++++
 xen/lib/x86/private.h           |  14 ++++++
 3 files changed, 141 insertions(+)
xen/include/xen/lib/x86/cpuid.h +21 -0
index 22d43ef..767a33b 100644
@@ -319,6 +319,27 @@ typedef xen_cpuid_leaf_t cpuid_leaf_buffer_t[];
 int x86_cpuid_copy_to_buffer(const struct cpuid_policy *policy,
                              cpuid_leaf_buffer_t leaves, uint32_t *nr_entries);
 
+/**
+ * Unserialise a cpuid_policy object from an array of cpuid leaves.
+ *
+ * @param policy      The cpuid_policy to unserialise into.
+ * @param leaves      The array of leaves to unserialise from.
+ * @param nr_entries  The number of entries in 'leaves'.
+ * @param err_leaf    Optional hint filled on error.
+ * @param err_subleaf Optional hint filled on error.
+ * @returns -errno
+ *
+ * Reads at most CPUID_MAX_SERIALISED_LEAVES.  May return -ERANGE if an
+ * incoming leaf is out of range of cpuid_policy, in which case the optional
+ * err_* pointers are filled to aid diagnostics.
+ *
+ * No content validation of in-range leaves is performed.
+ */
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_entries, uint32_t *err_leaf,
+                               uint32_t *err_subleaf);
+
 #endif /* !XEN_LIB_X86_CPUID_H */
 
 /*
xen/lib/x86/cpuid.c +106 -0

112 lines not on this page · raw message

xen/lib/x86/private.h +14 -0
index 3ee99aa..e0ff2da 100644
@@ -12,6 +12,7 @@
 #include <asm/msr-index.h>
 
 #define copy_to_buffer_offset copy_to_guest_offset
+#define copy_from_buffer_offset copy_from_guest_offset
 
 #else
 
@@ -44,6 +45,19 @@ static inline bool test_bit(unsigned int bit, const void *vaddr)
     0;                                                  \
 })
 
+/* memcpy(), but with copy_from_guest_offset()'s API. */
+#define copy_from_buffer_offset(dst, src, index, nr)    \
+({                                                      \
+    const typeof(*(src)) *src_ = (src);                 \
+    typeof(*(dst)) *dst_ = (dst);                       \
+    typeof(index) index_ = (index);                     \
+    typeof(nr) nr_ = (nr), i_;                          \
+                                                        \
+    for ( i_ = 0; i_ < nr_; i_++ )                      \
+        dst_[i_] = src_[index_ + i_];                   \
+    0;                                                  \
+})
+
 #endif /* __XEN__ */
 
 #endif /* XEN_LIB_X86_PRIVATE_H */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH 1/3] libx86: Introduce a helper to deserialise cpuid_policy objects

Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 04.01.19 at 16:33, <[email protected]> wrote:
xen/include/xen/lib/x86/cpuid.h +21 -0
@@ -319,6 +319,27 @@ typedef xen_cpuid_leaf_t cpuid_leaf_buffer_t[];
 int x86_cpuid_copy_to_buffer(const struct cpuid_policy *policy,
                              cpuid_leaf_buffer_t leaves, uint32_t *nr_entries);
 
+/**
+ * Unserialise a cpuid_policy object from an array of cpuid leaves.
+ *
+ * @param policy      The cpuid_policy to unserialise into.
+ * @param leaves      The array of leaves to unserialise from.
+ * @param nr_entries  The number of entries in 'leaves'.
+ * @param err_leaf    Optional hint filled on error.
+ * @param err_subleaf Optional hint filled on error.
+ * @returns -errno
+ *
+ * Reads at most CPUID_MAX_SERIALISED_LEAVES.  May return -ERANGE if an
+ * incoming leaf is out of range of cpuid_policy, in which case the optional
+ * err_* pointers are filled to aid diagnostics.
+ *
+ * No content validation of in-range leaves is performed.
+ */
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_entries, uint32_t *err_leaf,
+                               uint32_t *err_subleaf);
+
 #endif /* !XEN_LIB_X86_CPUID_H */
 
 /*
xen/lib/x86/cpuid.c +0 -0
index 5a3159b..7fc4148 100644
@@ -233,6 +233,112 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy 
 *p,
     return 0;

xen/lib/x86/cpuid.c int x86_cpuid_copy_to_buffer(const struct cpuid_policy · line 244

Quoted 10 lines
xen/lib/x86/cpuid.c +8 -0
 }
 
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *p,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_entries, uint32_t *err_leaf,
+                               uint32_t *err_subleaf)
+{
+    unsigned int i;
+    xen_cpuid_leaf_t data;
+    struct cpuid_leaf *l = (void *)&data.a;

I'd find this cast a little less worrying if you used container_of(). But even then I dislike this well hidden assumption of similar layouts of struct cpuid_leaf and the latter parts of struct xen_cpuid_leaf.

Also it looks as if this could be a pointer to const.

xen/lib/x86/cpuid.c int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p, · line 246

Quoted 2 lines
xen/lib/x86/cpuid.c +2 -0
+    /*
+     * A well formed caller is expected pass an array with leaves in order,

... expected to pass ...

Quoted 16 lines
xen/lib/x86/cpuid.c +16 -0
+     * and without any repetitions.  However, due to per-vendor differences,
+     * and in the case of upgrade or levelled scenarios, we typically expect
+     * fewer than MAX leaves to be passed.
+     *
+     * Detecting repeated entries is prohibitively complicated, so we don't
+     * bother.  That said, one way or another if more than MAX leaves are
+     * passed, something is wrong.
+     */
+    if ( nr_entries > CPUID_MAX_SERIALISED_LEAVES )
+        return -E2BIG;
+
+    for ( i = 0; i < nr_entries; ++i )
+    {
+        if ( copy_from_buffer_offset(&data, leaves, i, 1) )
+            return -EFAULT;
+

xen/lib/x86/cpuid.c int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p, · line 272

Quoted 10 lines
xen/lib/x86/cpuid.c +10 -0
+        switch ( data.leaf )
+        {
+        case 0 ... ARRAY_SIZE(p->basic.raw) - 1:
+            switch ( data.leaf )
+            {
+            case 0x4:
+                if ( data.subleaf >= ARRAY_SIZE(p->cache.raw) )
+                    goto out_of_range;
+
+                p->cache.raw[data.subleaf] = *l;

Do you not want to use array_index_nospec() here and below?

Quoted 45 lines
xen/lib/x86/cpuid.c +45 -0
+                break;
+
+            case 0x7:
+                if ( data.subleaf >= ARRAY_SIZE(p->feat.raw) )
+                    goto out_of_range;
+
+                p->feat.raw[data.subleaf] = *l;
+                break;
+
+            case 0xb:
+                if ( data.subleaf >= ARRAY_SIZE(p->topo.raw) )
+                    goto out_of_range;
+
+                p->topo.raw[data.subleaf] = *l;
+                break;
+
+            case 0xd:
+                if ( data.subleaf >= ARRAY_SIZE(p->xstate.raw) )
+                    goto out_of_range;
+
+                p->xstate.raw[data.subleaf] = *l;
+                break;
+
+            default:
+                if ( data.subleaf != XEN_CPUID_NO_SUBLEAF )
+                    goto out_of_range;
+
+                p->basic.raw[data.leaf] = *l;
+                break;
+            }
+            break;
+
+        case 0x40000000:
+            if ( data.subleaf != XEN_CPUID_NO_SUBLEAF )
+                goto out_of_range;
+
+            p->hv_limit = l->a;
+            break;
+
+        case 0x40000100:
+            if ( data.subleaf != XEN_CPUID_NO_SUBLEAF )
+                goto out_of_range;
+
+            p->hv2_limit = l->a;
+            break;

xen/lib/x86/cpuid.c int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p, · line 327

Quoted 10 lines
xen/lib/x86/cpuid.c +10 -0
+
+        case 0x80000000 ... 0x80000000 + ARRAY_SIZE(p->extd.raw) - 1:
+            if ( data.subleaf != XEN_CPUID_NO_SUBLEAF )
+                goto out_of_range;
+
+            p->extd.raw[data.leaf & 0xffff] = *l;
+            break;
+
+        default:
+            goto out_of_range;

Any chance I could talk you into moving the label right here, eliminating the ugly (to me at least) error handling code after the main return point of the function?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH 1/3] libx86: Introduce a helper to deserialise cpuid_policy objects

Quoted 21 lines · Jan Beulich
Quoted 7 lines
xen/lib/x86/cpuid.c +0 -0
index 5a3159b..7fc4148 100644
@@ -233,6 +233,112 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy 
 *p,
     return 0;

xen/lib/x86/cpuid.c int x86_cpuid_copy_to_buffer(const struct cpuid_policy · line 244

Quoted 10 lines
xen/lib/x86/cpuid.c +8 -0
 }
 
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *p,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_entries, uint32_t *err_leaf,
+                               uint32_t *err_subleaf)
+{
+    unsigned int i;
+    xen_cpuid_leaf_t data;
+    struct cpuid_leaf *l = (void *)&data.a;

I'd find this cast a little less worrying if you used container_of(). But even then I dislike this well hidden assumption of similar layouts of struct cpuid_leaf and the latter parts of struct xen_cpuid_leaf.

Quoted 1 line

Also it looks as if this could be a pointer to const.

I've found a different way of doing this which actually compiles smaller (contrary to expectation).

Quoted 3 lines

xen/lib/x86/cpuid.c int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p, · line 246

Quoted 2 lines
xen/lib/x86/cpuid.c +2 -0
+    /*
+     * A well formed caller is expected pass an array with leaves in order,

... expected to pass ...

Fixed.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

PATCH 2/3 libx86: introduce a helper to deserialise msr_policy objects

In reply to Andrew Cooper

From: Roger Pau Monné <[email protected]>

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

---
 xen/include/xen/lib/x86/msr.h | 21 ++++++++++++++
 xen/lib/x86/msr.c             | 67 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
xen/include/xen/lib/x86/msr.h +21 -0
index e2cfbb1..6236622 100644
@@ -48,6 +48,27 @@ typedef xen_msr_entry_t msr_entry_buffer_t[];
 int x86_msr_copy_to_buffer(const struct msr_policy *policy,
                            msr_entry_buffer_t msrs, uint32_t *nr_entries);
 
+/**
+ * Unserialise an msr_policy object from an array of msrs.
+ *
+ * @param policy     The msr_policy object to unserialise into.
+ * @param msrs       The array of msrs to unserialise from.
+ * @param nr_entries The number of entries in 'msrs'.
+ * @param err_msr    Optional hint filled on error.
+ * @returns -errno
+ *
+ * Reads at most MSR_MAX_SERIALISED_ENTRIES.  May fail for a number of reasons
+ * based on the content in an individual 'msrs' entry, including the MSR index
+ * not being valid in the policy, the flags field being nonzero, or if the
+ * value provided would truncate when stored in the policy.  In such cases,
+ * the optional err_* pointer is filled in to aid diagnostics.
+ *
+ * No content validation is performed on the data stored in the policy object.
+ */
+int x86_msr_copy_from_buffer(struct msr_policy *policy,
+                             const msr_entry_buffer_t msrs, uint32_t nr_entries,
+                             uint32_t *err_msr);
+
 #endif /* !XEN_LIB_X86_MSR_H */
 
 /*
xen/lib/x86/msr.c +67 -0

73 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

Re: [PATCH 2/3] libx86: introduce a helper to deserialise msr_policy objects

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.19 at 16:33, <[email protected]> wrote:

From: Roger Pau Monné <[email protected]>

Where applicable, same comments here as for patch 1.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

PATCH 3/3 tools/cpu-policy: Add unit tests and a fuzzing harness

In reply to Andrew Cooper

The AFL harness currently notices that there are cases where we optimse the serialised stream by omitting data beyond the various maximum leaves.

Both sets of tests will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

---
 tools/fuzz/cpu-policy/.gitignore          |   1 +
 tools/fuzz/cpu-policy/Makefile            |  27 ++++
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 117 ++++++++++++++
 tools/tests/Makefile                      |   1 +
 tools/tests/cpu-policy/.gitignore         |   1 +
 tools/tests/cpu-policy/Makefile           |  27 ++++
 tools/tests/cpu-policy/test-cpu-policy.c  | 247 ++++++++++++++++++++++++++++++
 xen/include/xen/lib/x86/cpuid.h           |   2 +-
 8 files changed, 422 insertions(+), 1 deletion(-)
 create mode 100644 tools/fuzz/cpu-policy/.gitignore
 create mode 100644 tools/fuzz/cpu-policy/Makefile
 create mode 100644 tools/fuzz/cpu-policy/afl-policy-fuzzer.c
 create mode 100644 tools/tests/cpu-policy/.gitignore
 create mode 100644 tools/tests/cpu-policy/Makefile
 create mode 100644 tools/tests/cpu-policy/test-cpu-policy.c
tools/fuzz/cpu-policy/.gitignore new file +1 -0
new file mode 100644
index 0000000..b0e0bdf
@@ -0,0 +1 @@
+afl-policy-fuzzer
tools/fuzz/cpu-policy/Makefile new file +27 -0
new file mode 100644
index 0000000..a25778b
@@ -0,0 +1,27 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+.PHONY: all
+all: afl-policy-fuzzer
+
+.PHONY: clean
+clean:
+	$(RM) -f -- *.o .*.d .*.d2 afl-policy-fuzzer
+
+.PHONY: distclean
+distclean: clean
+	$(RM) -f -- *~
+
+.PHONY: install
+install: all
+
+.PHONY: uninstall
+
+CFLAGS += -Werror $(CFLAGS_xeninclude) -D__XEN_TOOLS__
+
+vpath %.c ../../../xen/lib/x86
+
+afl-policy-fuzzer: afl-policy-fuzzer.o msr.o cpuid.o
+	$(CC) $(CFLAGS) $^ -o $@
+
+-include $(DEPS_INCLUDE)
tools/fuzz/cpu-policy/afl-policy-fuzzer.c new file +117 -0

117 lines not on this page · raw message

tools/tests/Makefile +1 -0
index a9fc50d..067a380 100644
@@ -5,6 +5,7 @@ CFLAGS  += $(CFLAGS_libxenctrl)
 LDLIBS += $(LDLIBS_libxenctrl)
 
 SUBDIRS-y :=
+SUBDIRS-$(CONFIG_X86) += cpu-policy
 SUBDIRS-$(CONFIG_X86) += mce-test
 SUBDIRS-y += mem-sharing
 ifeq ($(XEN_TARGET_ARCH),__fixme__)
tools/tests/cpu-policy/.gitignore new file +1 -0
new file mode 100644
index 0000000..83bdb6b
@@ -0,0 +1 @@
+test-cpu-policy
tools/tests/cpu-policy/Makefile new file +27 -0
new file mode 100644
index 0000000..a029165
@@ -0,0 +1,27 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+.PHONY: all
+all: test-cpu-policy
+
+.PHONY: clean
+clean:
+	$(RM) -f -- *.o .*.d .*.d2 test-cpu-policy
+
+.PHONY: distclean
+distclean: clean
+	$(RM) -f -- *~
+
+.PHONY: install
+install: all
+
+.PHONY: uninstall
+
+CFLAGS += -Werror $(CFLAGS_xeninclude) -D__XEN_TOOLS__ -O3
+
+vpath %.c ../../../xen/lib/x86
+
+test-cpu-policy: test-cpu-policy.o msr.o cpuid.o
+	$(CC) $(CFLAGS) $^ -o $@
+
+-include $(DEPS_INCLUDE)
tools/tests/cpu-policy/test-cpu-policy.c new file +247 -0

247 lines not on this page · raw message

xen/include/xen/lib/x86/cpuid.h +1 -1
index 767a33b..95b37b6 100644
@@ -66,8 +66,8 @@ static inline void cpuid_count_leaf(
 #undef XCHG
 
 #define CPUID_GUEST_NR_BASIC      (0xdu + 1)
-#define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_CACHE      (5u + 1)
+#define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_TOPO       (1u + 1)
 #define CPUID_GUEST_NR_XSTATE     (62u + 1)
 #define CPUID_GUEST_NR_EXTD_INTEL (0x8u + 1)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [PATCH 3/3] tools/cpu-policy: Add unit tests and a fuzzing harness

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 04.01.19 at 16:33, <[email protected]> wrote:

The AFL harness currently notices that there are cases where we optimse the serialised stream by omitting data beyond the various maximum leaves.

Both sets of tests will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

Quoted 6 lines
---
 tools/fuzz/cpu-policy/.gitignore          |   1 +
 tools/fuzz/cpu-policy/Makefile            |  27 ++++
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 117 ++++++++++++++
 tools/tests/Makefile                      |   1 +
 tools/tests/cpu-policy/.gitignore         |   1 +

Did we somehow come to the conclusion that the central .gitignore at the root of the tree is not the way to go in the future?

Quoted 31 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +28 -0
@@ -0,0 +1,247 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xen-tools/libs.h>
+#include <xen/lib/x86/cpuid.h>
+#include <xen/lib/x86/msr.h>
+#include <xen/domctl.h>
+
+static void test_cpuid_serialise_success(void)
+{
+    static const struct test {
+        struct cpuid_policy p;
+        const char *name;
+        unsigned int nr_leaves;
+    } tests[] = {
+        {
+            .name = "empty policy",
+            .nr_leaves = 4,
+        },
+    };
+    unsigned int i;
+
+    printf("Testing CPUID serialise success:\n");

tools/tests/cpu-policy/test-cpu-policy.c line 38

Quoted 10 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +10 -0
+
+    for ( i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
+        const struct test *t = &tests[i];
+        unsigned int nr = t->nr_leaves;
+        xen_cpuid_leaf_t *leaves = malloc(nr * sizeof(*leaves));
+        int rc;
+
+        if ( !leaves )
+            goto test_done;

Shouldn't you leave some indication of the test not having got run?

Quoted 55 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +55 -0
+static void test_cpuid_deserialise_failure(void)
+{
+    static const struct test {
+        const char *name;
+        xen_cpuid_leaf_t leaf;
+    } tests[] = {
+        {
+            .name = "incorrect basic subleaf",
+            .leaf = { .leaf = 0, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect hv1 subleaf",
+            .leaf = { .leaf = 0x40000000, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect hv2 subleaf",
+            .leaf = { .leaf = 0x40000100, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect extd subleaf",
+            .leaf = { .leaf = 0x80000000, .subleaf = 0 },
+        },
+        {
+            .name = "OoB basic leaf",
+            .leaf = { .leaf = CPUID_GUEST_NR_BASIC },
+        },
+        {
+            .name = "OoB cache leaf",
+            .leaf = { .leaf = 0x4, .subleaf = CPUID_GUEST_NR_CACHE },
+        },
+        {
+            .name = "OoB feat leaf",
+            .leaf = { .leaf = 0x7, .subleaf = CPUID_GUEST_NR_FEAT },
+        },
+        {
+            .name = "OoB topo leaf",
+            .leaf = { .leaf = 0xb, .subleaf = CPUID_GUEST_NR_TOPO },
+        },
+        {
+            .name = "OoB xstate leaf",
+            .leaf = { .leaf = 0xd, .subleaf = CPUID_GUEST_NR_XSTATE },
+        },
+        {
+            .name = "OoB extd leaf",
+            .leaf = { .leaf = 0x80000000 | CPUID_GUEST_NR_EXTD },
+        },
+    };
+    unsigned int i;
+
+    printf("Testing CPUID deserialise failure:\n");
+
+    for ( i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
+        const struct test *t = &tests[i];
+        uint32_t err_leaf = ~0u, err_subleaf = ~0u;

tools/tests/cpu-policy/test-cpu-policy.c line 172

Quoted 10 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +10 -0
+        int rc;
+
+        rc = x86_cpuid_copy_from_buffer(NULL, &t->leaf, 1,
+                                        &err_leaf, &err_subleaf);
+
+        if ( rc != -ERANGE )
+        {
+            printf("  Test %s, expected rc %d, got %d\n",
+                   t->name, -ERANGE, rc);
+            continue;

Perhaps drop this? The subsequent test ought to apply regardless of error code.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [PATCH 3/3] tools/cpu-policy: Add unit tests and a fuzzing harness

Quoted 21 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 04.01.19 at 16:33, <[email protected]> wrote:

The AFL harness currently notices that there are cases where we optimse the serialised stream by omitting data beyond the various maximum leaves.

Both sets of tests will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

Quoted 6 lines
---
 tools/fuzz/cpu-policy/.gitignore          |   1 +
 tools/fuzz/cpu-policy/Makefile            |  27 ++++
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 117 ++++++++++++++
 tools/tests/Makefile                      |   1 +
 tools/tests/cpu-policy/.gitignore         |   1 +
Quoted 2 lines

Did we somehow come to the conclusion that the central .gitignore at the root of the tree is not the way to go in the future?

We've already got several examples in the tree.

andrewcoop@andrewcoop:/local/xen.git$ git ls-files | grep gitignore
.gitignore
tools/tests/vhpet/.gitignore
xen/tools/kconfig/.gitignore
xen/tools/kconfig/lxdialog/.gitignore
xen/xsm/flask/.gitignore

As for the pro's of using split ignores, fewer collisions for backported changes, and no forgetting to update the root .gitconfig when you move directories.

Quoted 35 lines
Quoted 35 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +31 -0
@@ -0,0 +1,247 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xen-tools/libs.h>
+#include <xen/lib/x86/cpuid.h>
+#include <xen/lib/x86/msr.h>
+#include <xen/domctl.h>
+
+static void test_cpuid_serialise_success(void)
+{
+    static const struct test {
+        struct cpuid_policy p;
+        const char *name;
+        unsigned int nr_leaves;
+    } tests[] = {
+        {
+            .name = "empty policy",
+            .nr_leaves = 4,
+        },
+    };
+    unsigned int i;
+
+    printf("Testing CPUID serialise success:\n");
+
+    for ( i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
Quoted 8 lines

tools/tests/cpu-policy/test-cpu-policy.c line 38

Quoted 7 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +7 -0
+        const struct test *t = &tests[i];
+        unsigned int nr = t->nr_leaves;
+        xen_cpuid_leaf_t *leaves = malloc(nr * sizeof(*leaves));
+        int rc;
+
+        if ( !leaves )
+            goto test_done;

Shouldn't you leave some indication of the test not having got run?

I've swapped this for a hard error.  Its not going to fail in practice.

Quoted 60 lines
Quoted 60 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +59 -0
+static void test_cpuid_deserialise_failure(void)
+{
+    static const struct test {
+        const char *name;
+        xen_cpuid_leaf_t leaf;
+    } tests[] = {
+        {
+            .name = "incorrect basic subleaf",
+            .leaf = { .leaf = 0, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect hv1 subleaf",
+            .leaf = { .leaf = 0x40000000, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect hv2 subleaf",
+            .leaf = { .leaf = 0x40000100, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect extd subleaf",
+            .leaf = { .leaf = 0x80000000, .subleaf = 0 },
+        },
+        {
+            .name = "OoB basic leaf",
+            .leaf = { .leaf = CPUID_GUEST_NR_BASIC },
+        },
+        {
+            .name = "OoB cache leaf",
+            .leaf = { .leaf = 0x4, .subleaf = CPUID_GUEST_NR_CACHE },
+        },
+        {
+            .name = "OoB feat leaf",
+            .leaf = { .leaf = 0x7, .subleaf = CPUID_GUEST_NR_FEAT },
+        },
+        {
+            .name = "OoB topo leaf",
+            .leaf = { .leaf = 0xb, .subleaf = CPUID_GUEST_NR_TOPO },
+        },
+        {
+            .name = "OoB xstate leaf",
+            .leaf = { .leaf = 0xd, .subleaf = CPUID_GUEST_NR_XSTATE },
+        },
+        {
+            .name = "OoB extd leaf",
+            .leaf = { .leaf = 0x80000000 | CPUID_GUEST_NR_EXTD },
+        },
+    };
+    unsigned int i;
+
+    printf("Testing CPUID deserialise failure:\n");
+
+    for ( i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
+        const struct test *t = &tests[i];
+        uint32_t err_leaf = ~0u, err_subleaf = ~0u;
+        int rc;
+
+        rc = x86_cpuid_copy_from_buffer(NULL, &t->leaf, 1,
+                                        &err_leaf, &err_subleaf);
Quoted 8 lines

tools/tests/cpu-policy/test-cpu-policy.c line 172

Quoted 6 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +6 -0
+
+        if ( rc != -ERANGE )
+        {
+            printf("  Test %s, expected rc %d, got %d\n",
+                   t->name, -ERANGE, rc);
+            continue;

Perhaps drop this? The subsequent test ought to apply regardless of error code.

The common case is no failures at all.  However, once something has gone wrong, spewing cascade errors gets in the way, rather than being helpful.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [PATCH 3/3] tools/cpu-policy: Add unit tests and a fuzzing harness

Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 25.02.19 at 12:56, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 04.01.19 at 16:33, <[email protected]> wrote:

The AFL harness currently notices that there are cases where we optimse the serialised stream by omitting data beyond the various maximum leaves.

Both sets of tests will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

Quoted 6 lines
---
 tools/fuzz/cpu-policy/.gitignore          |   1 +
 tools/fuzz/cpu-policy/Makefile            |  27 ++++
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 117 ++++++++++++++
 tools/tests/Makefile                      |   1 +
 tools/tests/cpu-policy/.gitignore         |   1 +
Quoted 2 lines

Did we somehow come to the conclusion that the central .gitignore at the root of the tree is not the way to go in the future?

We've already got several examples in the tree.

andrewcoop@andrewcoop:/local/xen.git$ git ls-files | grep gitignore
.gitignore
tools/tests/vhpet/.gitignore
xen/tools/kconfig/.gitignore
xen/tools/kconfig/lxdialog/.gitignore
xen/xsm/flask/.gitignore
Quoted 3 lines

As for the pro's of using split ignores, fewer collisions for backported changes, and no forgetting to update the root .gitconfig when you move directories.

Well, I certainly appreciate this, and my comment wasn't meant as plain opposition. At the time I was simply wondering whether the few instances we have already aren't more like an accident. And I didn't recall any discussion towards moving away from the centralized model.

Quoted 68 lines
Quoted 59 lines
Quoted 59 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +59 -0
+static void test_cpuid_deserialise_failure(void)
+{
+    static const struct test {
+        const char *name;
+        xen_cpuid_leaf_t leaf;
+    } tests[] = {
+        {
+            .name = "incorrect basic subleaf",
+            .leaf = { .leaf = 0, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect hv1 subleaf",
+            .leaf = { .leaf = 0x40000000, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect hv2 subleaf",
+            .leaf = { .leaf = 0x40000100, .subleaf = 0 },
+        },
+        {
+            .name = "incorrect extd subleaf",
+            .leaf = { .leaf = 0x80000000, .subleaf = 0 },
+        },
+        {
+            .name = "OoB basic leaf",
+            .leaf = { .leaf = CPUID_GUEST_NR_BASIC },
+        },
+        {
+            .name = "OoB cache leaf",
+            .leaf = { .leaf = 0x4, .subleaf = CPUID_GUEST_NR_CACHE },
+        },
+        {
+            .name = "OoB feat leaf",
+            .leaf = { .leaf = 0x7, .subleaf = CPUID_GUEST_NR_FEAT },
+        },
+        {
+            .name = "OoB topo leaf",
+            .leaf = { .leaf = 0xb, .subleaf = CPUID_GUEST_NR_TOPO },
+        },
+        {
+            .name = "OoB xstate leaf",
+            .leaf = { .leaf = 0xd, .subleaf = CPUID_GUEST_NR_XSTATE },
+        },
+        {
+            .name = "OoB extd leaf",
+            .leaf = { .leaf = 0x80000000 | CPUID_GUEST_NR_EXTD },
+        },
+    };
+    unsigned int i;
+
+    printf("Testing CPUID deserialise failure:\n");
+
+    for ( i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
+        const struct test *t = &tests[i];
+        uint32_t err_leaf = ~0u, err_subleaf = ~0u;
+        int rc;
+
+        rc = x86_cpuid_copy_from_buffer(NULL, &t->leaf, 1,
+                                        &err_leaf, &err_subleaf);
Quoted 8 lines

tools/tests/cpu-policy/test-cpu-policy.c line 172

Quoted 6 lines
tools/tests/cpu-policy/test-cpu-policy.c new file +6 -0
+
+        if ( rc != -ERANGE )
+        {
+            printf("  Test %s, expected rc %d, got %d\n",
+                   t->name, -ERANGE, rc);
+            continue;

Perhaps drop this? The subsequent test ought to apply regardless of error code.

Quoted 2 lines

The common case is no failures at all. However, once something has gone wrong, spewing cascade errors gets in the way, rather than being helpful.

"Cascade failures" to me are ones where the latter is a result of something earlier having gone wrong. Iirc this isn't the case with the subsequent test(s) here, unless something's fundamentally wrong. Seeing which particular case doesn't work plus all of the ones which do can also be helpful. But as indicated by me using "perhaps" in the original reply - I won't insist, as in the end something will need to be done anyway until everything succeeds.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel