[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lyx-devel
Subject:    Clang's memory error detector.
From:       Peter_Kümmel <syntheticpp () gmx ! net>
Date:       2013-10-17 19:35:23
Message-ID: 52603BFB.6070601 () gmx ! net
[Download RAW message or body]

Attached a patch to build with Clang and with its "sanitize" 
flags.
It should help to detect wrong memory usage:

 
http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation

(Some sanitize flags are also available in GCC 4.7/4.8)


Short build recipe for Linux:

1. Build Clang
clone my Clang meta repository:

   git clone https://github.com/syntheticpp/Der-Clang.git

you should be automatically on branch release_33.
Then checkout llvm/clang:

   git submodule init
   git submodule update

Make a build folder and build clang:

   cmake ../Der-Clang/llvm
   make -j8
   make install


2. Build LyX with Clang:

   cmake -DLYX_CLANG=1 ../lyx-devel


Opening some help files didn't report any errors ;)

Peter

["0001-Add-option-to-build-on-Linux-with-Clang-s-sanitize-f.patch" (text/x-patch)]

From a305ac0ea77ae705805fb9685ba46cd8cc166926 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Peter Kümmel?= <syntheticpp@gmx.net>
Date: Thu, 17 Oct 2013 20:51:56 +0200
Subject: [PATCH] Add option to build on Linux with Clang's sanitize flags

---
 CMakeLists.txt                                 |   27 ++++-
 development/cmake/modules/Clang-sanitize.cmake |  131 ++++++++++++++++++++++++
 2 files changed, 153 insertions(+), 5 deletions(-)
 create mode 100644 development/cmake/modules/Clang-sanitize.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ccb4a6c..f1b5b1e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -45,10 +45,6 @@ set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
 # Supress regeneration
 set(CMAKE_SUPPRESS_REGENERATION FALSE)

-if(NOT help AND NOT HELP)
-	# 'project' triggers the searching for a compiler
-	project(${LYX_PROJECT})
-endif()

 if(UNIX)
 	set(ARCH_TRIPLET )
@@ -97,6 +93,7 @@ LYX_OPTION(QUIET            "Don't generate verbose makefiles" OFF \
ALL)  LYX_OPTION(INSTALL_PREFIX   "Install path for LyX" OFF ALL)
 LYX_OPTION(BUNDLE           "Build bundle  (experimental) " OFF ALL)
 LYX_OPTION(ENABLE_URLTESTS  "Enable for URL tests" OFF ALL)
+LYX_OPTION(CLANG            "Use Clang and enable sanitize options" OFF ALL)

 # GCC specific
 LYX_OPTION(PROFILE              "Build profile version" OFF GCC)
@@ -132,6 +129,16 @@ endif()

 # Check option dependencies

+if(LYX_CLANG)
+    set(CMAKE_C_COMPILER   "clang"   CACHE PATH "Clang C compiler" FORCE)
+    set(CMAKE_CXX_COMPILER "clang++" CACHE PATH "Clang C++ compiler" FORCE)
+endif()
+
+if(NOT help AND NOT HELP)
+	# 'project' triggers the searching for a compiler
+	project(${LYX_PROJECT})
+endif()
+
 if(LYX_DMG)
 	set(LYX_BUNDLE ON)
 	set(LYX_CPACK ON)
@@ -203,7 +210,17 @@ endif()


 set(LYX_GCC11_MODE)
-if(UNIX OR MINGW)
+
+
+if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+    include(Clang-sanitize)
+    set(clang_flags "--std=c++11 ${clang_flags}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${clang_flags}")
+    if(UNIX)
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
+    endif()
+elseif(CMAKE_COMPILER_IS_GNUCC)
 	execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE \
GCC_VERSION)  message(STATUS "Using GCC version ${GCC_VERSION}")
 	if(NOT GCC_VERSION VERSION_LESS 4.4)
diff --git a/development/cmake/modules/Clang-sanitize.cmake \
b/development/cmake/modules/Clang-sanitize.cmake new file mode 100644
index 0000000..dcd356d
--- /dev/null
+++ b/development/cmake/modules/Clang-sanitize.cmake
@@ -0,0 +1,131 @@
+# http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation
+
+list(APPEND clflg -fno-omit-frame-pointer)
+
+#set(sanitize-thread 1)
+
+if(NOT sanitize-thread)
+    list(APPEND clflg -fsanitize­dress)
+    # AddressSanitizer, a memory error detector.
+
+    list(APPEND clflg -fsanitize=init-order)
+    # Make AddressSanitizer check for dynamic initialization order problems.
+    # Implied by -fsanitize­dress.
+
+    list(APPEND clflg -fsanitize­dress-full)
+    # AddressSanitizer with all the experimental features listed below.
+endif()
+
+list(APPEND clflg -fsanitize=integer)
+# Enables checks for undefined or suspicious integer behavior.
+
+if(sanitize-thread)
+
+    # gdb configuration:
+    #  - .gdbinit: set disable-randomization off
+    #  - gdb -ex 'set disable-randomization off' --args <BINARY>
+    # TSAN_OPTIONS
+
+    list(APPEND clflg -fsanitize=thread)
+    # ThreadSanitizer, a data race detector.
+endif()
+
+#list(APPEND clflg -fsanitize=memory)
+# MemorySanitizer, an experimental detector of uninitialized reads. Not ready for \
widespread use. +
+#list(APPEND clflg -fsanitize=undefined) # Qt uses \
reinterpret_cast<T>(0)->staticMetaObject +# Fast and compatible undefined behavior \
checker. Enables the undefined behavior checks +# that have small runtime cost and no \
impact on address space layout or ABI. +# This includes all of the checks listed \
below other than unsigned-integer-overflow. +
+list(APPEND clflg -fsanitize=undefined-trap)
+# This includes all sanitizers included by -fsanitize=undefined, except those that \
require runtime support. +# This group of sanitizers are generally used in \
conjunction with the -fsanitize-undefined-trap-on-error flag, +# which causes traps \
to be emitted, rather than calls to runtime libraries. This includes all of the \
checks +# listed below other than unsigned-integer-overflow and vptr.
+
+list(APPEND clflg -fsanitize=alignment)
+# Use of a misaligned pointer or creation of a misaligned reference.
+
+list(APPEND clflg -fsanitize=bool)
+# Load of a bool value which is neither true nor false.
+
+list(APPEND clflg -fsanitize=bounds)
+# Out of bounds array indexing, in cases where the array bound can be statically \
determined. +
+list(APPEND clflg -fsanitize=enum)
+# Load of a value of an enumerated type which is not in the range of representable \
values for that enumerated type. +
+list(APPEND clflg -fsanitize=float-cast-overflow)
+# Conversion to, from, or between floating-point types which would overflow the \
destination. +
+list(APPEND clflg -fsanitize=float-divide-by-zero)
+# Floating point division by zero.
+
+list(APPEND clflg -fsanitize=integer-divide-by-zero)
+# Integer division by zero.
+
+list(APPEND clflg -fsanitize=null)
+# Use of a null pointer or creation of a null reference.
+
+list(APPEND clflg -fsanitize=object-size)
+# An attempt to use bytes which the optimizer can determine are not part of the \
object being accessed. +# The sizes of objects are determined using \
__builtin_object_size, and consequently may be able to +# detect more problems at \
higher optimization levels. +
+list(APPEND clflg -fsanitize=return)
+# In C++, reaching the end of a value-returning function without returning a value.
+
+list(APPEND clflg -fsanitize=shift)
+# Shift operators where the amount shifted is greater or equal to the promoted \
bit-width of the +# left hand side or less than zero, or where the left hand side is \
negative. For a signed left +# shift, also checks for signed overflow in C, and for \
unsigned overflow in C++. +
+list(APPEND clflg -fsanitize=signed-integer-overflow)
+# Signed integer overflow, including all the checks added by -ftrapv, and checking
+# for overflow in signed division (INT_MIN / -1).
+
+list(APPEND clflg -fsanitize=unreachable)
+# If control flow reaches __builtin_unreachable.
+
+list(APPEND clflg -fsanitize=unsigned-integer-overflow)
+# Unsigned integer overflows.
+
+list(APPEND clflg -fsanitize=vla-bound)
+# A variable-length array whose bound does not evaluate to a positive value.
+
+#list(APPEND clflg -fsanitize=vptr)
+# Use of an object whose vptr indicates that it is of the wrong dynamic type,
+# or that its lifetime has not begun or has ended. Incompatible with -fno-rtti.
+
+
+# Experimental features of AddressSanitizer (not ready for widespread use, require \
explicit -fsanitize­dress): +
+if(NOT sanitize-thread)
+    list(APPEND clflg -fsanitize=use-after-return)
+    # Check for use-after-return errors (accessing local variable after the function \
exit). +
+    list(APPEND clflg -fsanitize=use-after-scope)
+    # Check for use-after-scope errors (accesing local variable after it went out of \
scope). +endif()
+
+# Extra features of MemorySanitizer (require explicit -fsanitize=memory):
+
+#list(APPEND clflg -fsanitize-memory-track-origins)
+# Enables origin tracking in MemorySanitizer. Adds a second section to \
MemorySanitizer +# reports pointing to the heap or stack allocation the uninitialized \
bits came from. +# Slows down execution by additional 1.5x-2x.
+
+
+# The -fsanitize= argument must also be provided when linking, in order to link to \
the +# appropriate runtime library. It is not possible to combine the
+# -fsanitize­dress and -fsanitize=thread checkers in the same program.
+
+
+message(STATUS "Used sanitize flags:")
+set(clang_flags)
+foreach(it ${clflg})
+    message(STATUS "    ${it}")
+    set(clang_flags "${clang_flags} ${it}")
+endforeach()
+
--
1.7.10.4



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic