[Spice-commits] 2 commits - common/log.h common/quic.c common/quic_family_tmpl.c tests/test-logging.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Feb 7 13:26:42 UTC 2020


 common/log.h              |    6 ++++++
 common/quic.c             |   26 +++++++++-----------------
 common/quic_family_tmpl.c |    4 +---
 tests/test-logging.c      |   28 ++++++++++++++++++++++++++++
 4 files changed, 44 insertions(+), 20 deletions(-)

New commits:
commit 87e2db97989dee8f1b32d299d23c8d7f25571218
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Sun Dec 15 15:45:47 2019 +0000

    Reuse new spice_extra_assert macro
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Francesco Giudici <fgiudici at redhat.com>

diff --git a/common/quic.c b/common/quic.c
index f87b1ae..55a5d6c 100644
--- a/common/quic.c
+++ b/common/quic.c
@@ -279,9 +279,7 @@ static const BYTE lzeroes[256] = {
 /* count leading zeroes */
 static unsigned int cnt_l_zeroes(const unsigned int bits)
 {
-    if (spice_extra_checks) {
-        spice_assert(bits != 0);
-    }
+    spice_extra_assert(bits != 0);
 #if defined(__GNUC__) && __GNUC__ >= 4
     return __builtin_clz(bits);
 #else
@@ -405,10 +403,9 @@ static inline void encode(Encoder *encoder, unsigned int word, unsigned int len)
 {
     int delta;
 
-    if (spice_extra_checks) {
-        spice_assert(len > 0 && len < 32);
-        spice_assert(!(word & ~bppmask[len]));
-    }
+    spice_extra_assert(len > 0 && len < 32);
+    spice_extra_assert(!(word & ~bppmask[len]));
+
     if ((delta = ((int)encoder->io_available_bits - len)) >= 0) {
         encoder->io_available_bits = delta;
         encoder->io_word |= word << encoder->io_available_bits;
@@ -420,10 +417,8 @@ static inline void encode(Encoder *encoder, unsigned int word, unsigned int len)
     encoder->io_available_bits = 32 - delta;
     encoder->io_word = word << encoder->io_available_bits;
 
-    if (spice_extra_checks) {
-        spice_assert(encoder->io_available_bits < 32);
-        spice_assert((encoder->io_word & bppmask[encoder->io_available_bits]) == 0);
-    }
+    spice_extra_assert(encoder->io_available_bits < 32);
+    spice_extra_assert((encoder->io_word & bppmask[encoder->io_available_bits]) == 0);
 }
 
 static inline void encode_32(Encoder *encoder, unsigned int word)
@@ -446,9 +441,8 @@ static inline void read_io_word(Encoder *encoder)
     if (G_UNLIKELY(encoder->io_now == encoder->io_end)) {
         more_io_words(encoder);
     }
-    if (spice_extra_checks) {
-        spice_assert(encoder->io_now < encoder->io_end);
-    }
+    spice_extra_assert(encoder->io_now < encoder->io_end);
+
     encoder->io_next_word = GUINT32_FROM_LE(*(encoder->io_now));
     encoder->io_now++;
 }
@@ -457,9 +451,7 @@ static inline void decode_eatbits(Encoder *encoder, int len)
 {
     int delta;
 
-    if (spice_extra_checks) {
-        spice_assert(len > 0 && len < 32);
-    }
+    spice_extra_assert(len > 0 && len < 32);
     encoder->io_word <<= len;
 
     if ((delta = ((int)encoder->io_available_bits - len)) >= 0) {
diff --git a/common/quic_family_tmpl.c b/common/quic_family_tmpl.c
index 9b29560..8a5f7d2 100644
--- a/common/quic_family_tmpl.c
+++ b/common/quic_family_tmpl.c
@@ -101,9 +101,7 @@ static void FNAME(update_model)(CommonState *state, s_bucket * const bucket,
 
 static s_bucket *FNAME(find_bucket)(Channel *channel, const unsigned int val)
 {
-    if (spice_extra_checks) {
-        spice_assert(val < (0x1U << BPC));
-    }
+    spice_extra_assert(val < (0x1U << BPC));
 
     return channel->_buckets_ptrs[val];
 }
commit e761c2d4de27f9beac97fd563bebc09b94b91a71
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Sun Dec 15 15:09:24 2019 +0000

    log: Add spice_extra_assert
    
    This macro was suggested to simplify hot path expensive checks
    which should be disable in production environments.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Francesco Giudici <fgiudici at redhat.com>

diff --git a/common/log.h b/common/log.h
index 201c87a..f38e80e 100644
--- a/common/log.h
+++ b/common/log.h
@@ -99,6 +99,12 @@ enum { spice_extra_checks = 1 };
 enum { spice_extra_checks = 0 };
 #endif
 
+#define spice_extra_assert(x) G_STMT_START {            \
+    if (!spice_extra_checks || G_LIKELY(x)) { } else {  \
+        spice_error("assertion `%s' failed", #x);       \
+    }                                                   \
+} G_STMT_END
+
 SPICE_END_DECLS
 
 #endif // H_SPICE_COMMON_LOG
diff --git a/tests/test-logging.c b/tests/test-logging.c
index 22afe1f..d151990 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -267,6 +267,30 @@ static void test_spice_g_messages_debug_all(void)
     g_test_trap_assert_stderr("*g_message\n*other_message\n");
 }
 
+/* Checks that spice_assert() aborts if condition fails */
+static void test_spice_assert(void)
+{
+    if (g_test_subprocess()) {
+        spice_assert(1 == 2);
+        return;
+    }
+    g_test_trap_subprocess(NULL, 0, 0);
+    g_test_trap_assert_failed();
+    g_test_trap_assert_stderr("*assertion `1 == 2' failed*");
+}
+
+/* Checks that spice_extra_assert() aborts if condition fails */
+static void test_spice_extra_assert(void)
+{
+    if (g_test_subprocess()) {
+        spice_extra_assert(1 == 2);
+        return;
+    }
+    g_test_trap_subprocess(NULL, 0, 0);
+    g_test_trap_assert_failed();
+    g_test_trap_assert_stderr("*assertion `1 == 2' failed*");
+}
+
 static void handle_sigabrt(int sig G_GNUC_UNUSED)
 {
     _Exit(1);
@@ -298,6 +322,10 @@ int main(int argc, char **argv)
     g_test_add_func("/spice-common/spice-fatal-return-if-fail", test_spice_fatal_return_if_fail);
     g_test_add_func("/spice-common/spice-non-fatal-greturn-if-fail", test_spice_non_fatal_g_return_if_fail);
     g_test_add_func("/spice-common/spice-fatal-warning", test_spice_fatal_warning);
+    g_test_add_func("/spice-common/spice-assert", test_spice_assert);
+    if (spice_extra_checks) {
+        g_test_add_func("/spice-common/spice-extra-assert", test_spice_extra_assert);
+    }
 
     return g_test_run();
 }


More information about the Spice-commits mailing list