Eo: Made eo id for classes a bit more secure.
authorTom Hacohen <tom@stosb.com>
Tue, 11 Mar 2014 15:50:44 +0000 (15:50 +0000)
committerTom Hacohen <tom@stosb.com>
Tue, 11 Mar 2014 15:56:30 +0000 (15:56 +0000)
This patch sets the one before most significant bit on for classes. This
means that class ids are now very big, compared to the old ids which
were growing small integers (1, 2, 3...).
This makes accidental passing of integers (corrupted obj pointers) less
common.

@feature

src/lib/eo/eo.c
src/lib/eo/eo_ptr_indirection.x

index 26a91d2..7d16b68 100644 (file)
@@ -8,7 +8,6 @@
 #include "eo_ptr_indirection.h"
 #include "eo_private.h"
 
-/* The last id that should be reserved for statically allocated classes. */
 #define EO_CLASS_IDS_FIRST 1
 #define EO_OP_IDS_FIRST 1
 
@@ -42,9 +41,13 @@ static const Eo_Op_Description *_eo_op_id_desc_get(Eo_Op op);
 
 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
 
+/* We are substracting the mask here instead of "AND"ing because it's a hot path,
+ * it should be a valid class at this point, and this lets the compiler do 1
+ * substraction at compile time. */
+#define _UNMASK_ID(id) ((id) - MASK_CLASS_TAG)
 #define ID_CLASS_GET(id) ({ \
-      (_Eo_Class *) (((id <= _eo_classes_last_id) && (id > 0)) ? \
-      (_eo_classes[id - 1]) : NULL); \
+      (_Eo_Class *) (((_UNMASK_ID(id) <= _eo_classes_last_id) && (_UNMASK_ID(id) > 0)) ? \
+      (_eo_classes[_UNMASK_ID(id) - 1]) : NULL); \
       })
 
 static inline void
@@ -1012,7 +1015,7 @@ eo_class_new(const Eo_Class_Description *desc, const Eo_Class *parent_id, ...)
      }
 
    eina_spinlock_take(&_eo_class_creation_lock);
-   klass->header.id = ++_eo_classes_last_id;
+   klass->header.id = ++_eo_classes_last_id | MASK_CLASS_TAG;
      {
         /* FIXME: Handle errors. */
         size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
index 9bfd584..870d6a1 100644 (file)
@@ -101,6 +101,11 @@ typedef uint32_t Generation_Counter;
 #define MASK_ENTRY_ID         ((1 << BITS_ENTRY_ID) - 1)
 #define MASK_GENERATIONS      (MAX_GENERATIONS - 1)
 
+/* This only applies to classes. Used to artificially enlarge the class ids
+ * to reduce the likelihood of a clash with normal integers. */
+#define CLASS_TAG_SHIFT       (REF_TAG_SHIFT - 1)
+#define MASK_CLASS_TAG        (((Eo_Id) 1) << (CLASS_TAG_SHIFT))
+
 #define MEM_HEADER_SIZE       16
 #define MEM_PAGE_SIZE         4096
 #define MEM_MAGIC             0x3f61ec8a