Skip to content

Commit 0edfc3b

Browse files
committed
Print warning if incompatible alignment option chosen
Users repeatedly report segmentation faults coming from Memory.h in the Eigen code (aligned_free/handmade_aligned_free). This is because PCL uses Eigen's handmade_aligned_malloc but user code does not use the corresponding handmade_aligned_free (or the other way around), due to chosen SSE/AVX flags and the resulting alignment requirements. PCL's CMake config handles this by automatically setting SSE/AVX options in the user project as they are used in PCL, but not everyone uses CMake. This commit adds a check within PCL's header files for this kind of incompatibility. At first I considered solving theses incompatibilities automatically by setting EIGEN_MALLOC_ALREADY_ALIGNED and EIGEN_MAX_ALIGN_BYTES, but that would have been more complex and error-prone, so I decided only for checking and warning. I would have preferred `#warning` which however is not available on MSVC. `#pragma message` is an option on MSVC, but this is too easily overlooked IMO. Users have the option to silence this by defining the `PCL_SILENCE_MALLOC_WARNING` macro (in case of false positive).
1 parent 75ef8c5 commit 0edfc3b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,21 @@ if(WITH_SYSTEM_CJSON)
440440
endif()
441441
endif()
442442

443+
set(CMAKE_REQUIRED_LIBRARIES Eigen3::Eigen) # so that Eigen/Core is found below
444+
CHECK_CXX_SOURCE_COMPILES("
445+
#include <Eigen/Core>
446+
#if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
447+
#error Eigen will not use handmade_aligned_malloc (which is fine, we just throw an error here to make this compilation fail)
448+
#endif
449+
int main() { return 0; }"
450+
PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC)
451+
if (PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC) # CHECK_CXX_SOURCE_COMPILES does not necessarily set this to 0 or 1
452+
set(PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC 1)
453+
else()
454+
set(PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC 0)
455+
endif()
456+
unset(CMAKE_REQUIRED_LIBRARIES)
457+
443458
### ---[ Create the config.h file
444459
set(pcl_config_h_in "${CMAKE_CURRENT_SOURCE_DIR}/pcl_config.h.in")
445460
set(pcl_config_h "${CMAKE_CURRENT_BINARY_DIR}/include/pcl/pcl_config.h")

common/include/pcl/memory.h

+23
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,36 @@
4444
*/
4545

4646
#include <pcl/type_traits.h> // for has_custom_allocator
47+
#include <pcl/pcl_config.h> // for PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
4748

4849
#include <Eigen/Core> // for EIGEN_MAKE_ALIGNED_OPERATOR_NEW
4950

5051
#include <memory> // for std::allocate_shared, std::dynamic_pointer_cast, std::make_shared, std::shared_ptr, std::static_pointer_cast, std::weak_ptr
5152
#include <type_traits> // for std::enable_if_t, std::false_type, std::true_type
5253
#include <utility> // for std::forward
5354

55+
#if !defined(PCL_SILENCE_MALLOC_WARNING)
56+
#if PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
57+
// EIGEN_DEFAULT_ALIGN_BYTES and EIGEN_MALLOC_ALREADY_ALIGNED will be set after including Eigen/Core
58+
// this condition is the same as in the function aligned_malloc in Memory.h in the Eigen code
59+
#if (defined(EIGEN_DEFAULT_ALIGN_BYTES) && EIGEN_DEFAULT_ALIGN_BYTES==0) || (defined(EIGEN_MALLOC_ALREADY_ALIGNED) && EIGEN_MALLOC_ALREADY_ALIGNED)
60+
#if defined(_MSC_VER)
61+
#error "Potential runtime error due to aligned malloc mismatch! You likely have to compile your code with AVX enabled or define EIGEN_MAX_ALIGN_BYTES=32 (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
62+
#else // defined(_MSC_VER)
63+
#warning "Potential runtime error due to aligned malloc mismatch! You likely have to compile your code with AVX enabled or define EIGEN_MAX_ALIGN_BYTES=32 (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
64+
#endif // defined(_MSC_VER)
65+
#endif
66+
#else // PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
67+
#if (defined(EIGEN_DEFAULT_ALIGN_BYTES) && EIGEN_DEFAULT_ALIGN_BYTES!=0) && (defined(EIGEN_MALLOC_ALREADY_ALIGNED) && !EIGEN_MALLOC_ALREADY_ALIGNED)
68+
#if defined(_MSC_VER)
69+
#error "Potential runtime error due to aligned malloc mismatch! PCL was likely compiled without AVX support but you enabled AVX for your code (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
70+
#else // defined(_MSC_VER)
71+
#warning "Potential runtime error due to aligned malloc mismatch! PCL was likely compiled without AVX support but you enabled AVX for your code (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
72+
#endif // defined(_MSC_VER)
73+
#endif
74+
#endif // PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
75+
#endif // !defined(PCL_SILENCE_MALLOC_WARNING)
76+
5477
/**
5578
* \brief Macro to signal a class requires a custom allocator
5679
*

pcl_config.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#endif //PCL_MINOR_VERSION
3636
#endif
3737

38+
#define PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC ${PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC}
39+
3840
#cmakedefine HAVE_OPENNI 1
3941

4042
#cmakedefine HAVE_OPENNI2 1

0 commit comments

Comments
 (0)