2
0
mirror of https://github.com/boostorg/gil.git synced 2026-02-19 02:22:09 +00:00

Disable unaligned pointer access

Rename GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED
    to BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS.
Undefine BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS by default and document it.
If defined, issue warning or error, depending on target platform.

This changes how packed_channel_reference_base-based channels perform
access - aligned memory access by default.

This also fixes undefined behavior detected by UBSan in some test cases.
This commit is contained in:
Mateusz Łoskot
2018-06-22 00:21:17 +02:00
committed by Stefan Seefeld
parent 30c249de9e
commit cc6ba0ae72
2 changed files with 35 additions and 7 deletions

View File

@@ -344,7 +344,7 @@ protected:
static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 );
#ifdef GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED
#if defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
const bitfield_t& get_data() const { return *static_cast<const bitfield_t*>(_data_ptr); }
void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; }
#else

View File

@@ -1,6 +1,7 @@
/*
Copyright 2005-2007 Adobe Systems Incorporated
Copyright 2018 Mateusz Loskot <mateusz at loskot dot net>
Use, modification and distribution are subject to the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
@@ -14,7 +15,7 @@
#define GIL_CONFIG_HPP
////////////////////////////////////////////////////////////////////////////////////////
/// \file
/// \file
/// \brief GIL configuration file
/// \author Lubomir Bourdev and Hailin Jin \n
/// Adobe Systems Incorporated
@@ -22,13 +23,40 @@
////////////////////////////////////////////////////////////////////////////////////////
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#define GIL_VERSION "2.1.2"
// Enable GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED if your platform supports dereferencing on non-word memory boundary.
// Enabling the flag results in performance improvement
#if !defined(__hpux) && !defined(sun) && !defined(__sun) && !defined(__osf__)
#define GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED
#if defined(BOOST_GIL_DOXYGEN_ONLY)
/// \def BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS
/// \brief Define to allow unaligned memory access
/// Theoretically (or historically?) on platforms which support dereferencing on
/// non-word memory boundary, unaligned access may result in performance improvement.
/// \warning Unfortunately, this optimization may be a C/C++ strict aliasing rules
/// violation, if accessed data buffer has effective type that cannot be aliased
/// without leading to undefined behaviour.
#define BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS
#endif
#if defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
#if defined(sun) || defined(__sun) || \ // SunOS
defined(__osf__) || defined(__osf) || \ // Tru64
defined(_hpux) || defined(hpux) || \ // HP-UX
defined(__arm__) || defined(__ARM_ARCH) || \ // ARM
defined(_AIX) // AIX
#error Unaligned access strictly disabled for some UNIX platforms or ARM architecture
#elif defined(__i386__) || defined(__x86_64__) || defined(__vax__)
// The check for little-endian architectures that tolerate unaligned memory
// accesses is just an optimization. Nothing will break if it fails to detect
// a suitable architecture.
//
// Unfortunately, this optimization may be a C/C++ strict aliasing rules violation
// if accessed data buffer has effective type that cannot be aliased
// without leading to undefined behaviour.
BOOST_PRAGMA_MESSAGE("CAUTION: Unaligned access tolerated on little-endian may cause undefined behaviour")
#else
#error Unaligned access disabled for unknown platforms and architectures
#endif
#endif // defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
#endif