From: Stephen Hemminger <stephen@networkplumber.org>
To: ajit.khaparde@broadcom.com, somnath.kotur@broadcom.com
Cc: dev@dpdk.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [dpdk-dev] [PATCH 1/6] eal: add portable way to check for math overflow
Date: Tue, 3 Mar 2020 09:59:33 -0800 [thread overview]
Message-ID: <20200303175938.14292-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20200303175938.14292-1-stephen@networkplumber.org>
Clang and recent versions of GCC has builtin functions to do most math
operations and check for wraparound. On most architectures this is a
just a math operation followed by a branch on carry set.
But DPDK needs to be able to handle older GCC versions, and other
compilers so a wrapper macro is needed.
Chose to use a macro instead of inline functions to avoid having
to write lots of variants for each numeric type.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/librte_eal/common/Makefile | 2 +-
lib/librte_eal/common/include/rte_overflow.h | 74 ++++++++++++++++++++
2 files changed, 75 insertions(+), 1 deletion(-)
create mode 100644 lib/librte_eal/common/include/rte_overflow.h
diff --git a/lib/librte_eal/common/Makefile b/lib/librte_eal/common/Makefile
index c2c6d92cd377..3794128b75b7 100644
--- a/lib/librte_eal/common/Makefile
+++ b/lib/librte_eal/common/Makefile
@@ -13,7 +13,7 @@ INC += rte_tailq.h rte_interrupts.h rte_alarm.h
INC += rte_string_fns.h rte_version.h
INC += rte_eal_memconfig.h
INC += rte_hexdump.h rte_devargs.h rte_bus.h rte_dev.h rte_class.h
-INC += rte_option.h
+INC += rte_option.h rte_overflow.h
INC += rte_pci_dev_feature_defs.h rte_pci_dev_features.h
INC += rte_malloc.h rte_keepalive.h rte_time.h
INC += rte_service.h rte_service_component.h
diff --git a/lib/librte_eal/common/include/rte_overflow.h b/lib/librte_eal/common/include/rte_overflow.h
new file mode 100644
index 000000000000..d61791371029
--- /dev/null
+++ b/lib/librte_eal/common/include/rte_overflow.h
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2020 Microsoft Corp
+ */
+#ifndef RTE_OVERFLOW_H_
+#define RTE_OVERFLOW_H_
+/**
+ * @file
+ *
+ * Math functions with overflow checking.
+ * Wrappers for the __builtin_XXX_overflow functions that exist
+ * in recent versions of GCC and CLANG but may not exist
+ * in older compilers. They are macros to allow use with any
+ * size of unsigned number.
+ *
+ * See:
+ * https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
+ * https://github.com/nemequ/portable-snippets/tree/master/safe-math
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdbool.h>
+
+#if defined(__has_builtin)
+# if __has_builtin(__builtin_add_overflow)
+# define RTE_HAVE_BUILTIN_OVERFLOW
+# endif
+#elif defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 5000)
+# define RTE__HAVE_BUILTIN_OVERFLOW
+#endif
+
+/**
+ * Safely add two bit unsigned numbers
+ * @param a
+ * One operand
+ * @param b
+ * Other operand
+ * @param res
+ * Pointer to the where result of a + b is stored.
+ * Must not be NULL
+ * @return
+ * return true if the result overflows and is therefore truncated.
+ */
+#ifdef RTE_HAVE_BUILTIN_OVERFLOW
+#define rte_add_overflow(a, b, res) __builtin_add_overflow(a, b, res)
+#else
+#define rte_add_overflow(a, b, res) ({ *res = a + b; *res < a; })
+#endif
+
+/**
+ * Safely multiply two unsigned numbers
+ * @param a
+ * One operand
+ * @param b
+ * Other operand
+ * @param res
+ * Pointer to the where result of a + b is stored.
+ * Must not be NULL
+ * @return
+ * return true if the result overflows and is therefore truncated.
+ */
+#ifdef RTE_HAVE_BUILTIN_OVERFLOW
+#define rte_mul_overflow(a, b, res) __builtin_mul_overflow(a, b, res)
+#else
+#define rte_mul_overflow(a, b, res) ({ *res = a * b; *res < a; })
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* RTE_OVERFLOW_H_ */
--
2.20.1
next prev parent reply other threads:[~2020-03-03 17:59 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-03 17:59 [dpdk-dev] [PATCH 0/6] net/bnxt: bounds checking patches Stephen Hemminger
2020-03-03 17:59 ` Stephen Hemminger [this message]
2020-03-03 22:28 ` [dpdk-dev] [PATCH 1/6] eal: add portable way to check for math overflow Dmitry Kozlyuk
2020-03-03 17:59 ` [dpdk-dev] [PATCH 2/6] net/bnxt: fix potential data race Stephen Hemminger
2020-03-03 18:13 ` [dpdk-dev] [EXTERNAL] " Christopher Ertl
2020-03-03 18:16 ` Stephen Hemminger
2020-03-03 17:59 ` [dpdk-dev] [PATCH 3/6] net/bnxt: avoid potential out of bounds read Stephen Hemminger
2020-03-03 17:59 ` [dpdk-dev] [PATCH 4/6] net/bnxt: check for integer overflow in buffer sizing Stephen Hemminger
2020-03-03 17:59 ` [dpdk-dev] [PATCH 5/6] net/bnxt: add integer underflow check Stephen Hemminger
2020-03-03 17:59 ` [dpdk-dev] [PATCH 6/6] net/bnxt: sanitize max_l2_ctx Stephen Hemminger
2020-03-31 11:47 ` [dpdk-dev] [PATCH 0/6] net/bnxt: bounds checking patches Ferruh Yigit
2020-03-31 17:52 ` Ajit Khaparde
2020-03-31 18:04 ` Stephen Hemminger
2020-10-19 22:28 ` Thomas Monjalon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200303175938.14292-2-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=ajit.khaparde@broadcom.com \
--cc=dev@dpdk.org \
--cc=somnath.kotur@broadcom.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).