[PATCH] D121852: [scudo] Use templated builtins to avoid assumptions on SCUDO_WORDSIZE

Dominic Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 16 14:09:10 PDT 2022


ddcc created this revision.
ddcc added reviewers: vitalybuka, eugenis, cryptoad, mcgrathr, hctim.
Herald added a project: All.
ddcc requested review of this revision.
Herald added a project: Sanitizers.

Platforms may define uintptr_t differently, so just use template overrides to select the appropriate builtin


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121852

Files:
  compiler-rt/lib/scudo/standalone/wrappers_c_checks.h


Index: compiler-rt/lib/scudo/standalone/wrappers_c_checks.h
===================================================================
--- compiler-rt/lib/scudo/standalone/wrappers_c_checks.h
+++ compiler-rt/lib/scudo/standalone/wrappers_c_checks.h
@@ -45,19 +45,32 @@
 // Returns true if calloc(Size, N) overflows on Size*N calculation. Use a
 // builtin supported by recent clang & GCC if it exists, otherwise fallback to a
 // costly division.
-inline bool checkForCallocOverflow(uptr Size, uptr N, uptr *Product) {
-#if __has_builtin(__builtin_umull_overflow) && (SCUDO_WORDSIZE == 64U)
-  return __builtin_umull_overflow(Size, N, Product);
-#elif __has_builtin(__builtin_umul_overflow) && (SCUDO_WORDSIZE == 32U)
-  return __builtin_umul_overflow(Size, N, Product);
-#else
+template <typename T = scudo::uptr>
+inline bool checkForCallocOverflow(T Size, T N, T *Product) {
   *Product = Size * N;
   if (!Size)
     return false;
   return (*Product / Size) != N;
-#endif
 }
 
+#if __has_builtin(__builtin_umull_overflow)
+template <>
+inline bool checkForCallocOverflow<unsigned long>(unsigned long Size,
+                                                  unsigned long N,
+                                                  unsigned long *Product) {
+  return __builtin_umull_overflow(Size, N, Product);
+}
+#endif
+
+#if __has_builtin(__builtin_umul_overflow)
+template <>
+inline bool checkForCallocOverflow<unsigned int>(unsigned int Size,
+                                                 unsigned int N,
+                                                 unsigned int *Product) {
+  return __builtin_umul_overflow(Size, N, Product);
+}
+#endif
+
 // Returns true if the size passed to pvalloc overflows when rounded to the next
 // multiple of PageSize.
 inline bool checkForPvallocOverflow(uptr Size, uptr PageSize) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121852.415976.patch
Type: text/x-patch
Size: 1822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220316/6c3695be/attachment.bin>


More information about the llvm-commits mailing list