Skip to content

Commit

Permalink
Add Address Sanitizer support as build option. (#1114)
Browse files Browse the repository at this point in the history
- Add configure option for address sanitizer.
- Add unit test for address sanitizer.
- Enable address sanitizer support by default on distcheck.
  • Loading branch information
jwhui committed Jan 6, 2017
1 parent 69180fc commit 3e6546c
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -81,7 +81,7 @@ matrix:
- g++-6
- env: BUILD_TARGET="posix-distcheck" VERBOSE=1
os: linux
compiler: gcc
compiler: clang
- env: BUILD_TARGET="posix-32-bit" VERBOSE=1
os: linux
compiler: gcc
Expand Down
1 change: 1 addition & 0 deletions .travis/before_install.sh
Expand Up @@ -89,6 +89,7 @@ cd /tmp || die

[ $BUILD_TARGET != posix-distcheck ] || {
sudo apt-get install clang || die
sudo apt-get install llvm-3.4-runtime || die
}

[ $BUILD_TARGET != posix -o $CC != clang ] || {
Expand Down
2 changes: 2 additions & 0 deletions .travis/script.sh
Expand Up @@ -70,6 +70,8 @@ set -x
}

[ $BUILD_TARGET != posix-distcheck ] || {
export ASAN_SYMBOLIZER_PATH=`which llvm-symbolizer-3.4` || die
export ASAN_OPTIONS=symbolize=1 || die
BuildJobs=10 make -f examples/Makefile-posix distcheck || die
}

Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -31,6 +31,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
AM_MAKEFLAGS = --no-print-directory

AM_DISTCHECK_CONFIGURE_FLAGS = \
--enable-address-sanitizer \
--enable-cli \
--enable-ncp \
--enable-diag \
Expand Down
34 changes: 34 additions & 0 deletions configure.ac
Expand Up @@ -283,6 +283,39 @@ NL_ENABLE_OPTIMIZATION([yes])

AM_CONDITIONAL([OPENTHREAD_BUILD_OPTIMIZED], [test "${nl_cv_build_optimized}" = "yes"])

# Address Sanitizer

AC_MSG_CHECKING([whether to build with Address Sanitizer support])
AC_ARG_ENABLE(address-sanitizer,
[AS_HELP_STRING([--enable-address-sanitizer],[Enable Address Sanitizer support @<:@default=no@:>@.])],
[
case "${enableval}" in
no|yes)
enable_address_sanitizer=${enableval}
;;
*)
AC_MSG_ERROR([Invalid value ${enableval} for --enable-address-sanitizer])
;;
esac
],
[enable_address_sanitizer=no])
AC_MSG_RESULT(${enable_address_sanitizer})

AM_CONDITIONAL([OPENTHREAD_WITH_ADDRESS_SANITIZER], [test "${enable_address_sanitizer}" = "yes"])

if test "${enable_address_sanitizer}" = "yes" ; then

PROSPECTIVE_CFLAGS="-fsanitize=address"

# Check if the compilers support address sanitizer
AX_CHECK_COMPILER_OPTIONS([C], ${PROSPECTIVE_CFLAGS})
AX_CHECK_COMPILER_OPTIONS([C++], ${PROSPECTIVE_CFLAGS})

fi

#
# Code style
#
Expand Down Expand Up @@ -1039,6 +1072,7 @@ AC_MSG_NOTICE([
Build optimized libraries : ${nl_cv_build_optimized}
Build coverage libraries : ${nl_cv_build_coverage}
Build coverage reports : ${nl_cv_build_coverage_reports}
Address sanitizer support : ${enable_address_sanitizer}
Lcov : ${LCOV:--}
Genhtml : ${GENHTML:--}
Build tests : ${nl_cv_build_tests}
Expand Down
2 changes: 0 additions & 2 deletions examples/Makefile-posix
Expand Up @@ -56,8 +56,6 @@ CFLAGS += \

CXXFLAGS += \
$(COMMONCFLAGS) \
-fno-exceptions \
-fno-rtti \
$(NULL)

LDFLAGS += \
Expand Down
22 changes: 16 additions & 6 deletions tests/unit/Makefile.am
Expand Up @@ -86,23 +86,33 @@ check_PROGRAMS = \
test-toolchain \
$(NULL)

XFAIL_TESTS = \
$(NULL)

if OPENTHREAD_ENABLE_DIAG
check_PROGRAMS += \
test-diag \
check_PROGRAMS += \
test-diag \
$(NULL)
endif

if OPENTHREAD_ENABLE_NCP

COMMON_LDADD += \
$(top_builddir)/src/ncp/libopenthread-ncp.a \
COMMON_LDADD += \
$(top_builddir)/src/ncp/libopenthread-ncp.a \
$(NULL)

check_PROGRAMS += \
test-ncp-buffer \
check_PROGRAMS += \
test-ncp-buffer \
$(NULL)


endif # OPENTHREAD_ENABLE_NCP
if OPENTHREAD_WITH_ADDRESS_SANITIZER
check_PROGRAMS += test-address-sanitizer
XFAIL_TESTS += test-address-sanitizer
test_address_sanitizer_SOURCES = test_address_sanitizer.cpp
endif # OPENTHREAD_WITH_ADDRESS_SANITIZER


# Test applications and scripts that should be built and run when the
# 'check' target is run.
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/test_address_sanitizer.cpp
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>

#include "test_util.h"

#ifdef ENABLE_TEST_MAIN
int main(int argc, char *argv[])
{
(void)argv;
int stack_array[100];
int array_index = argc + 100;

for (int i = 0; i < 100; i++)
{
stack_array[i] = i;
}

printf("stack_array[%d] = %d\n", array_index, stack_array[array_index]);

return 0;
}
#endif

0 comments on commit 3e6546c

Please sign in to comment.