xref: /aosp_15_r20/trusty/kernel/lib/ubsan/enable.mk (revision 344aa361028b423587d4ef3fa52a23d194628137)
1*344aa361SAndroid Build Coastguard Worker#
2*344aa361SAndroid Build Coastguard Worker# Copyright (c) 2019, Google, Inc. All rights reserved
3*344aa361SAndroid Build Coastguard Worker#
4*344aa361SAndroid Build Coastguard Worker# Permission is hereby granted, free of charge, to any person obtaining
5*344aa361SAndroid Build Coastguard Worker# a copy of this software and associated documentation files
6*344aa361SAndroid Build Coastguard Worker# (the "Software"), to deal in the Software without restriction,
7*344aa361SAndroid Build Coastguard Worker# including without limitation the rights to use, copy, modify, merge,
8*344aa361SAndroid Build Coastguard Worker# publish, distribute, sublicense, and/or sell copies of the Software,
9*344aa361SAndroid Build Coastguard Worker# and to permit persons to whom the Software is furnished to do so,
10*344aa361SAndroid Build Coastguard Worker# subject to the following conditions:
11*344aa361SAndroid Build Coastguard Worker#
12*344aa361SAndroid Build Coastguard Worker# The above copyright notice and this permission notice shall be
13*344aa361SAndroid Build Coastguard Worker# included in all copies or substantial portions of the Software.
14*344aa361SAndroid Build Coastguard Worker#
15*344aa361SAndroid Build Coastguard Worker# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*344aa361SAndroid Build Coastguard Worker# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*344aa361SAndroid Build Coastguard Worker# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*344aa361SAndroid Build Coastguard Worker# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19*344aa361SAndroid Build Coastguard Worker# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20*344aa361SAndroid Build Coastguard Worker# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21*344aa361SAndroid Build Coastguard Worker# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*344aa361SAndroid Build Coastguard Worker#
23*344aa361SAndroid Build Coastguard Worker
24*344aa361SAndroid Build Coastguard Worker# Including this file in your project will enable UBSan.
25*344aa361SAndroid Build Coastguard Worker#
26*344aa361SAndroid Build Coastguard Worker# Modules other than the kernel wishing to use UBSan must link in this
27*344aa361SAndroid Build Coastguard Worker# runtime by adding it to MODULE_DEPS, conditional on the UBSAN_ENABLED
28*344aa361SAndroid Build Coastguard Worker# variable (which sindicates whether UBSan is on for the build).
29*344aa361SAndroid Build Coastguard Worker#
30*344aa361SAndroid Build Coastguard Worker# Userspace apps do not need to worry about this as this runtime is already
31*344aa361SAndroid Build Coastguard Worker# being pulled in by libc when needed.
32*344aa361SAndroid Build Coastguard Worker#
33*344aa361SAndroid Build Coastguard Worker# Modules wishing to opt out of UBSan can do so by adding
34*344aa361SAndroid Build Coastguard Worker# the contents of UBSAN_DISABLE to their MODULE_CFLAGS/MODULE_CPPFLAGS or by
35*344aa361SAndroid Build Coastguard Worker# adding to trusty/kernel/lib/ubsan/exemptlist.
36*344aa361SAndroid Build Coastguard Worker#
37*344aa361SAndroid Build Coastguard Worker# Example reasons to do this include:
38*344aa361SAndroid Build Coastguard Worker# * Contexts which cannot easily support the ubsan runtime (e.g. test-runner)
39*344aa361SAndroid Build Coastguard Worker# * External code that is comparatively difficult to change (e.g. boringssl)
40*344aa361SAndroid Build Coastguard Worker# * Code which is highly sensitive to modification (e.g. crypto or performance
41*344aa361SAndroid Build Coastguard Worker#   code) and is already thoroughly tested.
42*344aa361SAndroid Build Coastguard Worker#
43*344aa361SAndroid Build Coastguard Worker# If the code is trusty-owned, please consider either making the code UBSan
44*344aa361SAndroid Build Coastguard Worker# clean or using an __attribute__ decorator on a limited function with an
45*344aa361SAndroid Build Coastguard Worker# appropriate comment explaining why rather than disabling UBSan.
46*344aa361SAndroid Build Coastguard Worker#
47*344aa361SAndroid Build Coastguard Worker# The syntax for suppression is
48*344aa361SAndroid Build Coastguard Worker# __attribute__((no_sanitize("specific-sanitizer")))
49*344aa361SAndroid Build Coastguard Worker#
50*344aa361SAndroid Build Coastguard Worker# Please *DO NOT* use __attribute__((no_sanitize("undefined"))), as which
51*344aa361SAndroid Build Coastguard Worker# sanitizers it disables may expand with compiler revisions and makes it
52*344aa361SAndroid Build Coastguard Worker# harder for a reader to figure out which sanitizer is expected to generate
53*344aa361SAndroid Build Coastguard Worker# a false-positive in that code.
54*344aa361SAndroid Build Coastguard Worker
55*344aa361SAndroid Build Coastguard WorkerUBSAN_SANITIZERS ?= \
56*344aa361SAndroid Build Coastguard Worker    alignment \
57*344aa361SAndroid Build Coastguard Worker    bool \
58*344aa361SAndroid Build Coastguard Worker    builtin \
59*344aa361SAndroid Build Coastguard Worker    bounds \
60*344aa361SAndroid Build Coastguard Worker    enum \
61*344aa361SAndroid Build Coastguard Worker    float-cast-overflow \
62*344aa361SAndroid Build Coastguard Worker    float-divide-by-zero \
63*344aa361SAndroid Build Coastguard Worker    implicit-unsigned-integer-truncation \
64*344aa361SAndroid Build Coastguard Worker    implicit-signed-integer-truncation \
65*344aa361SAndroid Build Coastguard Worker    implicit-integer-sign-change \
66*344aa361SAndroid Build Coastguard Worker    integer-divide-by-zero \
67*344aa361SAndroid Build Coastguard Worker    pointer-overflow \
68*344aa361SAndroid Build Coastguard Worker    return \
69*344aa361SAndroid Build Coastguard Worker    shift \
70*344aa361SAndroid Build Coastguard Worker    signed-integer-overflow \
71*344aa361SAndroid Build Coastguard Worker    unreachable \
72*344aa361SAndroid Build Coastguard Worker    unsigned-integer-overflow \
73*344aa361SAndroid Build Coastguard Worker    vla-bound \
74*344aa361SAndroid Build Coastguard Worker
75*344aa361SAndroid Build Coastguard Worker# object-size only works at higher than -O0 and so is not enabled
76*344aa361SAndroid Build Coastguard Worker#
77*344aa361SAndroid Build Coastguard Worker# non-null sanitizers are not enabled because we are not using the annotations
78*344aa361SAndroid Build Coastguard Worker#
79*344aa361SAndroid Build Coastguard Worker# C++ sanitizers requiring full language features (e.g. RTTI or stdlib) are
80*344aa361SAndroid Build Coastguard Worker# not enabled
81*344aa361SAndroid Build Coastguard Worker
82*344aa361SAndroid Build Coastguard WorkerUBSAN_ENABLE := \
83*344aa361SAndroid Build Coastguard Worker    $(foreach san,$(UBSAN_SANITIZERS),-fsanitize=$(san)) \
84*344aa361SAndroid Build Coastguard Worker    -fsanitize-blacklist=trusty/kernel/lib/ubsan/exemptlist \
85*344aa361SAndroid Build Coastguard Worker
86*344aa361SAndroid Build Coastguard WorkerUBSAN_DISABLE := \
87*344aa361SAndroid Build Coastguard Worker    $(foreach san,$(UBSAN_SANITIZERS),-fno-sanitize=$(san))
88*344aa361SAndroid Build Coastguard Worker
89*344aa361SAndroid Build Coastguard WorkerGLOBAL_SHARED_COMPILEFLAGS += $(UBSAN_ENABLE) -DUBSAN_ENABLED
90*344aa361SAndroid Build Coastguard Worker
91*344aa361SAndroid Build Coastguard WorkerMODULES += trusty/kernel/lib/ubsan
92*344aa361SAndroid Build Coastguard WorkerUBSAN_ENABLED := true
93