mbox series

[00/11] Add initial support for --enable-ubsan

Message ID 20250507142110.3452012-1-adhemerval.zanella@linaro.org
Headers show
Series Add initial support for --enable-ubsan | expand

Message

Adhemerval Zanella May 7, 2025, 2:17 p.m. UTC
This patchset is preliminary and incomplete to enable glibc to be built
with Undefined Behavior Sanitizer (UBSAN) [1]. UBSAN uses compiler
instrumentation that triggers internal callbacks when undefined behavior
happens at runtime.

This option is an extra debug and maintenance mode; the generated code
is suboptimal in size and performance.  It is enabled through a new
configure flag, --enable-ubsan, and implements only a subset of all
possible handlers/checks the compiler might generate (the one required
for glibc and the regression tests). Extra handlers might be required
for future C++ tests, and__ ubsan_handle_dynamic_type_cache_miss also
needs a proper implementation.

The ubsan handlers are exported from ld, since they are called on all
libraries and tests. It might interfere with ubsan from compiler runtime
(when programs are built with libubsan in shared mode), and this is
ccompletely untested and/or not supported at the moment.

There is no support for the UBSAN_OPTIONS environment variable, although
some options are supported through glibc.ubsan tunables. Currently,
glibc.ubsan.halt_on_errors can be used to avoid the process halt when
any UB handler is issued.

Not all targets are supported, nor have I checked the test suite on all
targets.  Also, I only checked with limited gcc versions (only gcc
14.2.1 and for some targets 15.0.0).  UBSAN builds on Linux for aarch64,
arm, hppa, i686, powerpc64, microblaze, mips64, loongarch64, sparc,
s390x, and x86_64.  The alpha and m68k would require to change to a
different code model (since relocations are truncated), while ork1,
sh4, and hurd would need to adjust the ld, so pull of abort/exit.

The make check shows a lot of regression on ARM and x86, I have a WIP
branch [2] with a lot of fixes that I plan to eventually send for
review.  It already shows an invalid write [3] and led to better
implementation for ilogb [4].

[1] https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
[2] https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/azanella/ubsan-undef
[3] https://patchwork.sourceware.org/project/glibc/patch/20250430194401.818624-1-adhemerval.zanella@linaro.org/
[4] https://patchwork.sourceware.org/project/glibc/list/?series=46808

Adhemerval Zanella (10):
  ubsan: Add initial support for -fsanitize=undefined
  riscv: Fix --enable-ubsan build failure on riscv
  locale: Fix --enable-ubsan build failure on some ABIs
  locate: Fix UB on memcpy call
  locale: Fix UB on insert_weights
  localte: Fix UB on collate_finish
  locale: Fix UB in elem_hash
  locale: Fix UB on add_locale_uint32_array
  argp: Fix shift bug
  elf: Fix UB on _dl_map_object_from_fd

Richard Henderson (1):
  elf: Adjust DT_EXTRATAGIDX to avoid undefined shifts

 INSTALL                                       |   9 +
 Makeconfig                                    |   6 +-
 argp/argp-parse.c                             |  15 +-
 config.h.in                                   |   3 +
 configure                                     |  30 ++
 configure.ac                                  |  18 +
 elf/Makefile                                  |  32 +-
 elf/Versions                                  |  19 +
 elf/dl-load.c                                 |   2 +-
 elf/dl-printf.c                               |   6 +
 elf/dl-tunables.list                          |   9 +
 elf/elf.h                                     |   2 +-
 elf/tst-_dl_addr_inside_object.c              |  12 +
 elf/ubsan_error.c                             |  57 +++
 elf/ubsan_handle_add_overflow.c               |  26 ++
 elf/ubsan_handle_builtin_unreachable.c        |  27 ++
 elf/ubsan_handle_divrem_overflow.c            |  40 +++
 elf/ubsan_handle_dynamic_type_cache_miss.c    |  28 ++
 elf/ubsan_handle_invalid_builtin.c            |  39 +++
 elf/ubsan_handle_load_invalid_value.c         |  33 ++
 elf/ubsan_handle_mul_overflow.c               |  26 ++
 elf/ubsan_handle_negate_overflow.c            |  34 ++
 elf/ubsan_handle_nonnull_arg.c                |  34 ++
 elf/ubsan_handle_nonnull_return_v1.c          |  34 ++
 elf/ubsan_handle_out_of_bounds.c              |  34 ++
 elf/ubsan_handle_overflow.c                   |  39 +++
 elf/ubsan_handle_pointer_overflow.c           |  62 ++++
 elf/ubsan_handle_shift_out_of_bounds.c        |  53 +++
 elf/ubsan_handle_sub_overflow.c               |  26 ++
 elf/ubsan_handle_type_mismatch_v1.c           |  75 ++++
 elf/ubsan_handle_vla_bound_not_positive.c     |  34 ++
 elf/ubsan_val_to_string.c                     | 189 ++++++++++
 elf/ubsan_vptr_type_cache.c                   |  21 ++
 iconv/iconvconfig.c                           |  13 +-
 include/libintl.h                             |   3 +
 include/sys/cdefs.h                           |   6 +
 include/ubsan.h                               | 327 ++++++++++++++++++
 locale/elem-hash.h                            |   2 +-
 locale/programs/charmap.c                     |   8 +-
 locale/programs/charmap.h                     |   7 +
 locale/programs/ld-collate.c                  |   5 +-
 locale/programs/locfile.c                     |   2 +
 locale/programs/locfile.h                     |   3 +-
 locale/programs/repertoire.c                  |   2 +-
 manual/install.texi                           |   8 +
 nss/test-netdb.c                              |   6 +
 posix/glob.c                                  |   5 +-
 resolv/res_send.c                             |  11 +-
 stdio-common/tst-printf-format-s.h            |   4 +
 stdio-common/tst-printf-format-vs.h           |   4 +
 stdlib/stdbit.h                               |   9 +-
 sysdeps/arm/Makefile                          |   5 +
 sysdeps/generic/ldconfig.h                    |   3 +-
 sysdeps/generic/ldsodefs.h                    |   3 +
 sysdeps/generic/symbol-hacks.h                |  36 ++
 .../powerpc64/multiarch/stpncpy-ppc64.c       |   3 +-
 sysdeps/unix/sysv/linux/riscv/setcontext.S    |   2 +-
 57 files changed, 1503 insertions(+), 48 deletions(-)
 create mode 100644 elf/ubsan_error.c
 create mode 100644 elf/ubsan_handle_add_overflow.c
 create mode 100644 elf/ubsan_handle_builtin_unreachable.c
 create mode 100644 elf/ubsan_handle_divrem_overflow.c
 create mode 100644 elf/ubsan_handle_dynamic_type_cache_miss.c
 create mode 100644 elf/ubsan_handle_invalid_builtin.c
 create mode 100644 elf/ubsan_handle_load_invalid_value.c
 create mode 100644 elf/ubsan_handle_mul_overflow.c
 create mode 100644 elf/ubsan_handle_negate_overflow.c
 create mode 100644 elf/ubsan_handle_nonnull_arg.c
 create mode 100644 elf/ubsan_handle_nonnull_return_v1.c
 create mode 100644 elf/ubsan_handle_out_of_bounds.c
 create mode 100644 elf/ubsan_handle_overflow.c
 create mode 100644 elf/ubsan_handle_pointer_overflow.c
 create mode 100644 elf/ubsan_handle_shift_out_of_bounds.c
 create mode 100644 elf/ubsan_handle_sub_overflow.c
 create mode 100644 elf/ubsan_handle_type_mismatch_v1.c
 create mode 100644 elf/ubsan_handle_vla_bound_not_positive.c
 create mode 100644 elf/ubsan_val_to_string.c
 create mode 100644 elf/ubsan_vptr_type_cache.c
 create mode 100644 include/ubsan.h