[torch/c10] Add C10_UBSAN_ENABLED macro and use it to disable SymInt_… (#127967)

Adds `C10_UBSAN_ENABLED` macro and use it to disable `SymIntTest::Overflows` (fails under `signed-integer-overflow` UBSAN check).

Also cleans up UBSAN guard in `jit/test_misc.cpp` to use `C10_UBSAN_ENABLED`  and the existing `C10_ASAN_ENABLED` instead of locally defining `HAS_ASANUBSAN`.

> NOTE: This should fix `SymIntTest::Overflows` failing under ubsan in fbcode too...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/127967
Approved by: https://github.com/atalman, https://github.com/d4l3k, https://github.com/malfet
This commit is contained in:
Kiuk Chung 2024-06-14 16:01:12 +00:00 committed by PyTorch MergeBot
parent ee140a198f
commit 8629939a51
3 changed files with 25 additions and 7 deletions

View File

@ -64,6 +64,25 @@
#define C10_ASAN_ENABLED 0
#endif
// Detect undefined-behavior sanitizer (UBSAN)
#undef C10_UBSAN_ENABLED
// for clang or gcc >= 14
// NB: gcc 14 adds support for Clang's __has_feature
// https://gcc.gnu.org/gcc-14/changes.html
// gcc < 14 doesn't have a macro for UBSAN
// (e.g. __SANITIZE_UNDEFINED__ does not exist in gcc)
// https://github.com/google/sanitizers/issues/765
#if defined(__has_feature)
#if ((__has_feature(undefined_behavior_sanitizer)))
#define C10_UBSAN_ENABLED 1
#endif
#endif
#if !defined(C10_UBSAN_ENABLED)
#define C10_UBSAN_ENABLED 0
#endif
// Disable the copy and assignment operator for a class. Note that this will
// disable the usage of the class in std containers.
#define C10_DISABLE_COPY_AND_ASSIGN(classname) \

View File

@ -2,6 +2,7 @@
#include <c10/core/SymInt.h>
#include <c10/core/SymNodeImpl.h>
#include <c10/macros/Macros.h>
using namespace c10;
#ifndef C10_MOBILE
@ -22,6 +23,8 @@ TEST(SymIntTest, CheckRange) {
EXPECT_FALSE(SymInt::check_range(INT64_MIN));
}
#if !C10_UBSAN_ENABLED
// This test fails signed-integer-overflow UBSAN check
TEST(SymIntTest, Overflows) {
const auto x = SymInt(INT64_MAX);
EXPECT_NE(-(x + 1), 0);
@ -30,5 +33,6 @@ TEST(SymIntTest, Overflows) {
EXPECT_NE(-y, 0);
EXPECT_NE(0 - y, 0);
}
#endif
#endif

View File

@ -6,6 +6,7 @@
#include <ATen/core/interned_strings.h>
#include <ATen/core/ivalue.h>
#include <ATen/core/jit_type_base.h>
#include <c10/macros/Macros.h>
#include <test/cpp/jit/test_utils.h>
#include <torch/csrc/jit/passes/remove_mutation.h>
#include <torch/csrc/jit/passes/tensorexpr_fuser.h>
@ -491,13 +492,7 @@ TEST(ControlFlowTest, Basic) {
ASSERT_EQ(256, run_binary("while_test", 2, 0));
}
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define HAS_ASANUBSAN 1
#endif
#endif
#ifndef HAS_ASANUBSAN
#if !(C10_ASAN_ENABLED || C10_UBSAN_ENABLED)
// This test fails vptr UBSAN checks
TEST(ProtoTest, Basic) {