Skip to content

Commit 7a85d95

Browse files
jckingmagicus
authored andcommitted
8298448: UndefinedBehaviorSanitizer
Reviewed-by: erikj, ihse
1 parent 26890d1 commit 7a85d95

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

make/autoconf/configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ JDKOPT_SETUP_CODE_COVERAGE
215215
# AddressSanitizer
216216
JDKOPT_SETUP_ADDRESS_SANITIZER
217217

218+
# UndefinedBehaviorSanitizer
219+
JDKOPT_SETUP_UNDEFINED_BEHAVIOR_SANITIZER
220+
218221
###############################################################################
219222
#
220223
# Check dependencies for external and internal libraries.

make/autoconf/jdk-options.m4

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,41 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_ADDRESS_SANITIZER],
445445
AC_SUBST(ASAN_ENABLED)
446446
])
447447

448+
###############################################################################
449+
#
450+
# UndefinedBehaviorSanitizer
451+
#
452+
AC_DEFUN_ONCE([JDKOPT_SETUP_UNDEFINED_BEHAVIOR_SANITIZER],
453+
[
454+
UTIL_ARG_ENABLE(NAME: ubsan, DEFAULT: false, RESULT: UBSAN_ENABLED,
455+
DESC: [enable UndefinedBehaviorSanitizer],
456+
CHECK_AVAILABLE: [
457+
AC_MSG_CHECKING([if UndefinedBehaviorSanitizer (ubsan) is available])
458+
if test "x$TOOLCHAIN_TYPE" = "xgcc" ||
459+
test "x$TOOLCHAIN_TYPE" = "xclang"; then
460+
AC_MSG_RESULT([yes])
461+
else
462+
AC_MSG_RESULT([no])
463+
AVAILABLE=false
464+
fi
465+
],
466+
IF_ENABLED: [
467+
# GCC reports lots of likely false positives for stringop-truncation and format-overflow.
468+
# Silence them for now.
469+
UBSAN_CFLAGS="-fsanitize=undefined -fsanitize=float-divide-by-zero -Wno-stringop-truncation -Wno-format-overflow -fno-omit-frame-pointer -DUNDEFINED_BEHAVIOR_SANITIZER"
470+
UBSAN_LDFLAGS="-fsanitize=undefined -fsanitize=float-divide-by-zero"
471+
JVM_CFLAGS="$JVM_CFLAGS $UBSAN_CFLAGS"
472+
JVM_LDFLAGS="$JVM_LDFLAGS $UBSAN_LDFLAGS"
473+
CFLAGS_JDKLIB="$CFLAGS_JDKLIB $UBSAN_CFLAGS"
474+
CFLAGS_JDKEXE="$CFLAGS_JDKEXE $UBSAN_CFLAGS"
475+
CXXFLAGS_JDKLIB="$CXXFLAGS_JDKLIB $UBSAN_CFLAGS"
476+
CXXFLAGS_JDKEXE="$CXXFLAGS_JDKEXE $UBSAN_CFLAGS"
477+
LDFLAGS_JDKLIB="$LDFLAGS_JDKLIB $UBSAN_LDFLAGS"
478+
LDFLAGS_JDKEXE="$LDFLAGS_JDKEXE $UBSAN_LDFLAGS"
479+
])
480+
AC_SUBST(UBSAN_ENABLED)
481+
])
482+
448483
################################################################################
449484
#
450485
# Static build support. When enabled will generate static

make/autoconf/spec.gmk.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ ifeq ($(ASAN_ENABLED), yes)
455455
endif
456456
endif
457457

458+
# UndefinedBehaviorSanitizer
459+
UBSAN_ENABLED:=@UBSAN_ENABLED@
460+
458461
# Necessary additional compiler flags to compile X11
459462
X_CFLAGS:=@X_CFLAGS@
460463
X_LIBS:=@X_LIBS@

make/common/NativeCompilation.gmk

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,19 @@ define SetupNativeCompilationBody
701701
$$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
702702
endif
703703

704+
ifeq ($$($1_TYPE), EXECUTABLE)
705+
ifeq ($(UBSAN_ENABLED), true)
706+
# We need to set the default options for UBSan. This needs to be included in every executable.
707+
# Rather than copy and paste code to everything with a main function, we add an additional
708+
# source file to every executable that exports __ubsan_default_options.
709+
ifneq ($$(filter %.cpp %.cc, $$($1_SRCS)), )
710+
$1_SRCS += $(TOPDIR)/make/data/ubsan/ubsan_default_options.cpp
711+
else
712+
$1_SRCS += $(TOPDIR)/make/data/ubsan/ubsan_default_options.c
713+
endif
714+
endif
715+
endif
716+
704717
# Calculate the expected output from compiling the sources
705718
$1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
706719
$1_EXPECTED_OBJS := $$(addprefix $$($1_OBJECT_DIR)/, $$($1_EXPECTED_OBJS_FILENAMES))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
#ifndef UNDEFINED_BEHAVIOR_SANITIZER
27+
#error "Build misconfigured, preprocessor macro UNDEFINED_BEHAVIOR_SANITIZER should be defined"
28+
#endif
29+
30+
#ifndef __has_attribute
31+
#define __has_attribute(x) 0
32+
#endif
33+
34+
#if (defined(__GNUC__) && !defined(__clang__)) || __has_attribute(visibility)
35+
#define ATTRIBUTE_DEFAULT_VISIBILITY __attribute__((visibility("default")))
36+
#else
37+
#define ATTRIBUTE_DEFAULT_VISIBILITY
38+
#endif
39+
40+
#if (defined(__GNUC__) && !defined(__clang__)) || __has_attribute(used)
41+
#define ATTRIBUTE_USED __attribute__((used))
42+
#else
43+
#define ATTRIBUTE_USED
44+
#endif
45+
46+
// Override weak symbol exposed by UBSan to override default options. This is called by UBSan
47+
// extremely early during library loading, before main is called. We need to override the default
48+
// options because by default UBSan only prints a warning for each occurrence. We want jtreg tests
49+
// to fail when undefined behavior is encountered. We also want a full stack trace for the offending
50+
// thread so it is easier to track down. You can override these options by setting the environment
51+
// variable UBSAN_OPTIONS.
52+
ATTRIBUTE_DEFAULT_VISIBILITY ATTRIBUTE_USED const char* __ubsan_default_options() {
53+
return "halt_on_error=1,print_stacktrace=1";
54+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
extern "C" {
27+
28+
#include "./ubsan_default_options.c"
29+
30+
} // extern "C"

0 commit comments

Comments
 (0)