[PATCH] D156821: [CodeGen] [ubsan] Respect integer overflow handling in abs builtin

Artem Labazov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 11:07:33 PDT 2023


artem created this revision.
artem added a reviewer: craig.topper.
Herald added subscribers: Enna1, kbarton, nemanjai.
Herald added a project: All.
artem requested review of this revision.
Herald added projects: clang, Sanitizers.
Herald added subscribers: Sanitizers, cfe-commits.

Currenly both Clang and GCC support the following set of flags that control
code gen of signed overflow:

- -fwrapv: overflow is defined as in two-complement
- -ftrapv: overflow traps
- -fsanitize=signed-integer-overflow: if undefined (no -fwrapv), then overflow behaviour is controlled by UBSan runtime, overrides -ftrapv

Howerver, clang ignores these flags for __builtin_abs(int) and its higher-width
versions, so passing minimum integer value always causes poison.

The same holds for *abs(), which are not handled in frontend at all but folded
to llvm.abs.* intrinsics during InstCombinePass. The intrinsics are not
instrumented by UBSan, so the functions need special handling as well.

This patch does a few things:

- Handle *abs() in CGBuiltin the same way as __builtin_*abs()
- Clang now emits llvm.abs.* intrinsic where possible
- -fsanitize=builtin,signed-integer-overflow now properly instruments abs() with UBSan
- -fwrapv and -ftrapv handling for abs() is made consistent with GCC

Fixes #45129 and #45794


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156821

Files:
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/CodeGen/PowerPC/builtins-ppc-xlcompat-macros.c
  clang/test/CodeGen/abs-overflow.c
  clang/test/CodeGen/builtin-abs.c
  clang/test/CodeGenCXX/builtins.cpp
  compiler-rt/lib/ubsan/ubsan_handlers.cpp
  compiler-rt/lib/ubsan/ubsan_handlers.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156821.546148.patch
Type: text/x-patch
Size: 12717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230801/bdb7899e/attachment-0001.bin>


More information about the cfe-commits mailing list