[PATCH] add sparse support to detect endianness bug

Frederic Crozat fcrozat at suse.com
Fri Mar 16 03:59:04 PDT 2012


le16/32/64_t type should be used when storing little-endian value

header to integrate with sparse from Josh Triplett <josh at joshtriplett.org>
---
 src/journal/journal-def.h      |   74 +++++++++++++++++-----------------
 src/journal/journal-file.c     |   15 ++++---
 src/journal/journal-file.h     |    1 +
 src/journal/journal-internal.h |    2 +-
 src/journal/journald.c         |    5 +-
 src/journal/sd-journal.c       |   10 +++--
 src/journal/sparse-endian.h    |   87 ++++++++++++++++++++++++++++++++++++++++
 7 files changed, 143 insertions(+), 51 deletions(-)
 create mode 100644 src/journal/sparse-endian.h

diff --git a/src/journal/journal-def.h b/src/journal/journal-def.h
index 964e0c2..9cb8051 100644
--- a/src/journal/journal-def.h
+++ b/src/journal/journal-def.h
@@ -22,7 +22,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <inttypes.h>
+#include "sparse-endian.h"
 
 #include <systemd/sd-id128.h>
 
@@ -60,48 +60,48 @@ _packed_ struct ObjectHeader {
         uint8_t type;
         uint8_t flags;
         uint8_t reserved[6];
-        uint64_t size;
+        le64_t size;
         uint8_t payload[];
 };
 
 _packed_ struct DataObject {
         ObjectHeader object;
-        uint64_t hash;
-        uint64_t next_hash_offset;
-        uint64_t next_field_offset;
-        uint64_t entry_offset; /* the first array entry we store inline */
-        uint64_t entry_array_offset;
-        uint64_t n_entries;
+        le64_t hash;
+        le64_t next_hash_offset;
+        le64_t next_field_offset;
+        le64_t entry_offset; /* the first array entry we store inline */
+        le64_t entry_array_offset;
+        le64_t n_entries;
         uint8_t payload[];
 };
 
 _packed_ struct FieldObject {
         ObjectHeader object;
-        uint64_t hash;
-        uint64_t next_hash_offset;
-        uint64_t head_data_offset;
-        uint64_t tail_data_offset;
+        le64_t hash;
+        le64_t next_hash_offset;
+        le64_t head_data_offset;
+        le64_t tail_data_offset;
         uint8_t payload[];
 };
 
 _packed_ struct EntryItem {
-        uint64_t object_offset;
-        uint64_t hash;
+        le64_t object_offset;
+        le64_t hash;
 };
 
 _packed_ struct EntryObject {
         ObjectHeader object;
-        uint64_t seqnum;
-        uint64_t realtime;
-        uint64_t monotonic;
+        le64_t seqnum;
+        le64_t realtime;
+        le64_t monotonic;
         sd_id128_t boot_id;
-        uint64_t xor_hash;
+        le64_t xor_hash;
         EntryItem items[];
 };
 
 _packed_ struct HashItem {
-        uint64_t head_hash_offset;
-        uint64_t tail_hash_offset;
+        le64_t head_hash_offset;
+        le64_t tail_hash_offset;
 };
 
 _packed_ struct HashTableObject {
@@ -111,8 +111,8 @@ _packed_ struct HashTableObject {
 
 _packed_ struct EntryArrayObject {
         ObjectHeader object;
-        uint64_t next_entry_array_offset;
-        uint64_t items[];
+        le64_t next_entry_array_offset;
+        le64_t items[];
 };
 
 union Object {
@@ -145,21 +145,21 @@ _packed_ struct Header {
         sd_id128_t machine_id;
         sd_id128_t boot_id;
         sd_id128_t seqnum_id;
-        uint64_t arena_offset;
-        uint64_t arena_size;
-        uint64_t data_hash_table_offset;     /* for looking up data objects */
-        uint64_t data_hash_table_size;
-        uint64_t field_hash_table_offset;     /* for looking up field objects */
-        uint64_t field_hash_table_size;
-        uint64_t tail_object_offset;
-        uint64_t n_objects;
-        uint64_t n_entries;
-        uint64_t seqnum;
-        uint64_t first_seqnum;
-        uint64_t entry_array_offset;
-        uint64_t head_entry_realtime;
-        uint64_t tail_entry_realtime;
-        uint64_t tail_entry_monotonic;
+        le64_t arena_offset;
+        le64_t arena_size;
+        le64_t data_hash_table_offset;     /* for looking up data objects */
+        le64_t data_hash_table_size;
+        le64_t field_hash_table_offset;     /* for looking up field objects */
+        le64_t field_hash_table_size;
+        le64_t tail_object_offset;
+        le64_t n_objects;
+        le64_t n_entries;
+        le64_t seqnum;
+        le64_t first_seqnum;
+        le64_t entry_array_offset;
+        le64_t head_entry_realtime;
+        le64_t tail_entry_realtime;
+        le64_t tail_entry_monotonic;
 };
 
 #endif
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index 474dd5c..e803cec 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -793,8 +793,8 @@ static uint64_t journal_file_entry_array_n_items(Object *o) {
 }
 
 static int link_entry_into_array(JournalFile *f,
-                                 uint64_t *first,
-                                 uint64_t *idx,
+                                 le64_t *first,
+                                 le64_t *idx,
                                  uint64_t p) {
         int r;
         uint64_t n = 0, ap = 0, q, i, a, hidx;
@@ -857,9 +857,9 @@ static int link_entry_into_array(JournalFile *f,
 }
 
 static int link_entry_into_array_plus_one(JournalFile *f,
-                                          uint64_t *extra,
-                                          uint64_t *first,
-                                          uint64_t *idx,
+                                          le64_t *extra,
+                                          le64_t *first,
+                                          le64_t *idx,
                                           uint64_t p) {
 
         int r;
@@ -873,7 +873,7 @@ static int link_entry_into_array_plus_one(JournalFile *f,
         if (*idx == 0)
                 *extra = htole64(p);
         else {
-                uint64_t i;
+                le64_t i;
 
                 i = htole64(le64toh(*idx) - 1);
                 r = link_entry_into_array(f, first, &i, p);
@@ -2141,7 +2141,8 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6
         items = alloca(sizeof(EntryItem) * n);
 
         for (i = 0; i < n; i++) {
-                uint64_t le_hash, l, h;
+                uint64_t l, h;
+                le64_t le_hash;
                 size_t t;
                 void *data;
                 Object *u;
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
index 4d6e140..57d66ca 100644
--- a/src/journal/journal-file.h
+++ b/src/journal/journal-file.h
@@ -26,6 +26,7 @@
 
 #include <systemd/sd-id128.h>
 
+#include "sparse-endian.h"
 #include "journal-def.h"
 #include "util.h"
 
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
index e5914bf..17f1d31 100644
--- a/src/journal/journal-internal.h
+++ b/src/journal/journal-internal.h
@@ -35,7 +35,7 @@ typedef struct Match Match;
 struct Match {
         char *data;
         size_t size;
-        uint64_t le_hash;
+        le64_t le_hash;
 
         LIST_FIELDS(Match, matches);
 };
diff --git a/src/journal/journald.c b/src/journal/journald.c
index baad3ab..ebb4275 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -1251,6 +1251,7 @@ static void process_native_message(
                         p = e + 1;
                         continue;
                 } else {
+                        le64_t l_le;
                         uint64_t l;
                         char *k;
 
@@ -1259,8 +1260,8 @@ static void process_native_message(
                                 break;
                         }
 
-                        memcpy(&l, e + 1, sizeof(uint64_t));
-                        l = le64toh(l);
+                        memcpy(&l_le, e + 1, sizeof(uint64_t));
+                        l = le64toh(l_le);
 
                         if (remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
                             e[1+sizeof(uint64_t)+l] != '\n') {
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 86ac267..e9cd26e 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -108,7 +108,7 @@ static int same_field(const void *_a, size_t s, const void *_b, size_t t) {
 
 _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
         Match *m, *after = NULL;
-        uint64_t le_hash;
+        le64_t le_hash;
 
         if (!j)
                 return -EINVAL;
@@ -356,7 +356,7 @@ static int find_location(sd_journal *j, JournalFile *f, direction_t direction, O
                         Object *c, *d;
                         uint64_t cp, dp;
 
-                        r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp);
+                        r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), &d, &dp);
                         if (r <= 0)
                                 return r;
 
@@ -1349,7 +1349,8 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
 
         n = journal_file_entry_n_items(o);
         for (i = 0; i < n; i++) {
-                uint64_t p, l, le_hash;
+                uint64_t p, l;
+                le64_t le_hash;
                 size_t t;
 
                 p = le64toh(o->entry.items[i].object_offset);
@@ -1410,7 +1411,8 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
 
 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
         JournalFile *f;
-        uint64_t p, l, n, le_hash;
+        uint64_t p, l, n;
+        le64_t le_hash;
         int r;
         Object *o;
         size_t t;
diff --git a/src/journal/sparse-endian.h b/src/journal/sparse-endian.h
new file mode 100644
index 0000000..eb4dbf3
--- /dev/null
+++ b/src/journal/sparse-endian.h
@@ -0,0 +1,87 @@
+/* Copyright (c) 2012 Josh Triplett <josh at joshtriplett.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#ifndef SPARSE_ENDIAN_H
+#define SPARSE_ENDIAN_H
+
+#include <endian.h>
+#include <stdint.h>
+
+#ifdef __CHECKER__
+#define __bitwise __attribute__((bitwise))
+#define __force __attribute__((force))
+#else
+#define __bitwise
+#define __force
+#endif
+
+typedef uint16_t __bitwise le16_t;
+typedef uint16_t __bitwise be16_t;
+typedef uint32_t __bitwise le32_t;
+typedef uint32_t __bitwise be32_t;
+typedef uint64_t __bitwise le64_t;
+typedef uint64_t __bitwise be64_t;
+
+#undef htobe16
+#undef htole16
+#undef be16toh
+#undef le16toh
+#undef htobe32
+#undef htole32
+#undef be32toh
+#undef le32toh
+#undef htobe64
+#undef htole64
+#undef be64toh
+#undef le64toh
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define bswap_16_on_le(x) __bswap_16(x)
+#define bswap_32_on_le(x) __bswap_32(x)
+#define bswap_64_on_le(x) __bswap_64(x)
+#define bswap_16_on_be(x) (x)
+#define bswap_32_on_be(x) (x)
+#define bswap_64_on_be(x) (x)
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define bswap_16_on_le(x) (x)
+#define bswap_32_on_le(x) (x)
+#define bswap_64_on_le(x) (x)
+#define bswap_16_on_be(x) __bswap_16(x)
+#define bswap_32_on_be(x) __bswap_32(x)
+#define bswap_64_on_be(x) __bswap_64(x)
+#endif
+
+static inline le16_t htole16(uint16_t value) { return (le16_t __force) bswap_16_on_be(value); }
+static inline le32_t htole32(uint32_t value) { return (le32_t __force) bswap_32_on_be(value); }
+static inline le64_t htole64(uint64_t value) { return (le64_t __force) bswap_64_on_be(value); }
+
+static inline be16_t htobe16(uint16_t value) { return (be16_t __force) bswap_16_on_le(value); }
+static inline be32_t htobe32(uint32_t value) { return (be32_t __force) bswap_32_on_le(value); }
+static inline be64_t htobe64(uint64_t value) { return (be64_t __force) bswap_64_on_le(value); }
+
+static inline uint16_t le16toh(le16_t value) { return bswap_16_on_be((uint16_t __force)value); }
+static inline uint32_t le32toh(le32_t value) { return bswap_32_on_be((uint32_t __force)value); }
+static inline uint64_t le64toh(le64_t value) { return bswap_64_on_be((uint64_t __force)value); }
+
+static inline uint16_t be16toh(be16_t value) { return bswap_16_on_le((uint16_t __force)value); }
+static inline uint32_t be32toh(be32_t value) { return bswap_32_on_le((uint32_t __force)value); }
+static inline uint64_t be64toh(be64_t value) { return bswap_64_on_le((uint64_t __force)value); }
+
+#endif /* SPARSE_ENDIAN_H */
-- 
1.7.7


--=-70fn4sXa3ljB/0ADl1Vj--



More information about the systemd-devel mailing list