From 24b651b465f002d8814e20492fa0e8baf6766d1f Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@openairinterface.org>
Date: Sun, 19 Jun 2022 17:31:57 +0200
Subject: [PATCH] Provide build_oai option UBSan

---
 cmake_targets/CMakeLists.txt |  6 ++++++
 cmake_targets/build_oai      | 12 +++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/cmake_targets/CMakeLists.txt b/cmake_targets/CMakeLists.txt
index 25e10931c7e..6446ced4bde 100644
--- a/cmake_targets/CMakeLists.txt
+++ b/cmake_targets/CMakeLists.txt
@@ -271,6 +271,12 @@ if (SANITIZE_ADDRESS)
   # wrong in the pthread library, or something subtly wrong in this CMakeLists.txt. Use Ubuntu 20.04 instead.
 endif ()
 
+add_boolean_option(SANITIZE_UNDEFINED False "enable the undefined behavior sanitizer (UBSan)")
+if (SANITIZE_UNDEFINED)
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
+endif ()
+
 add_definitions("-DASN_DISABLE_OER_SUPPORT")
 
 #########################
diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index 5a201fcfef9..16a0d559dd7 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -171,8 +171,12 @@ Options:
    Skip build for shared libraries to reduce compilation time when building frequently for debugging purposes
 --ninja
   Tell cmake to use the Ninja build system. Without, will generate make files
+--sanitize
+  Shortcut for usage of --sanitize-address --sanitize-undefined
 --sanitize-address | -fsanitize=address
-   Enable the address sanitizer on all targets
+  Enable the address sanitizer on all targets
+--sanitize-undefined | -fsanitize=undefined
+  Enable the undefined behavior sanitizer on all targets
 --ittiSIM
   Makes the itti simulator
 -h | --help
@@ -451,10 +455,16 @@ function main() {
             CMAKE_CMD="$CMAKE_CMD -GNinja"
             MAKE_CMD=ninja
             shift;;
+        --sanitize)
+            CMAKE_CMD="$CMAKE_CMD -DSANITIZE_ADDRESS=True -DSANITIZE_UNDEFINED=True"
+            shift;;
         --sanitize-address | -fsanitize=address)
             grep -sq "Ubuntu 18.04" /etc/os-release && echo_error "Bug in OS with this option, see CMakeLists.txt"
             CMAKE_CMD="$CMAKE_CMD -DSANITIZE_ADDRESS=True"
             shift;;
+        --sanitize-undefined | -fundefined=address)
+            CMAKE_CMD="$CMAKE_CMD -DSANITIZE_UNDEFINED=True"
+            shift;;
         --ittiSIM)
             ittiSIM=1
             CMAKE_CMD="$CMAKE_CMD -DITTI_SIM=True"
-- 
GitLab