]> granicus.if.org Git - re2c/commitdiff
Fixed unaligned memory access.
authorUlya Trofimovich <skvadrik@gmail.com>
Mon, 31 Jul 2017 12:59:51 +0000 (13:59 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Mon, 31 Jul 2017 12:59:51 +0000 (13:59 +0100)
With CXXFLAGS='-fsanitize=undefined' GCC complains about unaligned access:
if custom allocator is used to allocate structs or other alignment-sensitive
things, then it must take care of the alignment (for example, add padding
to all unaligned blocks of memory which it allocates).

re2c/src/dfa/tcmd.h
re2c/src/re/re.h
re2c/src/util/slab_allocator.h

index d48890bbee7d8b63d191009c62a49d00446820fa..0badf03761f770b0266dd93d9c6ba64c4319c8d3 100644 (file)
@@ -33,7 +33,7 @@ static const tcid_t TCID0 = 0;
 
 class tcpool_t
 {
-       typedef slab_allocator_t<~0u, 4096> alc_t;
+       typedef slab_allocator_t<~0u, 4096, sizeof(void*)> alc_t;
        typedef lookup_t<const tcmd_t*> index_t;
 
        alc_t alc;
index e7472c9981e301218b4ce67763399773f4be5358..305c28b47324f107d1805a64fe57c27f9741542b 100644 (file)
@@ -15,7 +15,7 @@ namespace re2c
 
 struct RE
 {
-       typedef slab_allocator_t<~0u, 4096> alc_t;
+       typedef slab_allocator_t<~0u, 4096, sizeof(void*)> alc_t;
        enum type_t {NIL, SYM, ALT, CAT, ITER, TAG} type;
        union {
                const Range *sym;
index 4baa8c13740a133822ecdd6cb0286ee33985a8cb..f34600d9d170750eb8174dfacfc31e258947965b 100644 (file)
@@ -15,7 +15,8 @@
  * Works ~20 times faster, than linux's glibc allocator :]
  */
 template<uint32_t MAXIMUM_INLINE = 4 * 1024,
-       uint32_t SLAB_SIZE = 1024 * 1024>
+       uint32_t SLAB_SIZE = 1024 * 1024,
+       size_t ALIGN = 1>
 class slab_allocator_t
 {
        typedef std::vector<char*> slabs_t;
@@ -33,6 +34,9 @@ public:
        {
                char *result;
 
+               /* alignment */
+               size += ALIGN - size % ALIGN;
+
                /* very large objects */
                if (size > MAXIMUM_INLINE) {
                        result = static_cast<char*>(malloc(size));