[compiler-rt] [compiler-rt] making getrandom call blocking. (PR #78340)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 16 12:19:17 PST 2024


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/78340

>From ddec1e4acf25c68f926af10e00e2390dcce7b511 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Tue, 16 Jan 2024 20:02:25 +0000
Subject: [PATCH 1/2] [compiler-rt] making getrandom call blocking.

except when `GRND_NONBLOCK` is present in the flags (on Linux).
---
 .../sanitizer_common_interceptors.inc               | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 77fa1b4965a7a4..663a7e95f44db4 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -9965,7 +9965,18 @@ INTERCEPTOR(void, sl_free, void *sl, int freeall) {
 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
-  SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
+#if SANITIZER_LINUX
+  // only on Linux GRND_NONBLOCK has an actual effect.
+  // in other platforms /dev/random == /dev/urandom
+  static const int grnd_nonblock = 1; 
+  SSIZE_T n;
+  if ((flags & grnd_nonblock))
+    n = REAL(getrandom)(buf, buflen, flags);
+  else
+    n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
+#else
+  SSIZE_T n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
+#endif
   if (n > 0) {
     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
   }

>From 4d94daa718873f60d9f601a115921921089496a4 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Tue, 16 Jan 2024 20:18:30 +0000
Subject: [PATCH 2/2] wrong assumption, it is GRND_RANDOM which differs b/w
 oses.

---
 .../lib/sanitizer_common/sanitizer_common_interceptors.inc | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 663a7e95f44db4..1b56bebac64e68 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -9965,18 +9965,13 @@ INTERCEPTOR(void, sl_free, void *sl, int freeall) {
 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
-#if SANITIZER_LINUX
-  // only on Linux GRND_NONBLOCK has an actual effect.
-  // in other platforms /dev/random == /dev/urandom
+  // If GRND_NONBLOCK is set in the flags, it is non blocking.
   static const int grnd_nonblock = 1; 
   SSIZE_T n;
   if ((flags & grnd_nonblock))
     n = REAL(getrandom)(buf, buflen, flags);
   else
     n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
-#else
-  SSIZE_T n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
-#endif
   if (n > 0) {
     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
   }



More information about the llvm-commits mailing list