[PATCH] D50937: [Sanitizer] implementing remaining function under OSX
David CARLIER via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 24 04:41:16 PDT 2018
devnexen updated this revision to Diff 162360.
devnexen added a comment.
rebasing
https://reviews.llvm.org/D50937
Files:
lib/sanitizer_common/sanitizer_mac.cc
lib/sanitizer_common/tests/sanitizer_common_test.cc
Index: lib/sanitizer_common/tests/sanitizer_common_test.cc
===================================================================
--- lib/sanitizer_common/tests/sanitizer_common_test.cc
+++ lib/sanitizer_common/tests/sanitizer_common_test.cc
@@ -354,7 +354,8 @@
EXPECT_STREQ("012345678", str.data());
}
-#if SANITIZER_LINUX
+#if SANITIZER_LINUX || SANITIZER_FREEBSD || \
+ SANITIZER_OPENBSD || SANITIZER_MAC || SANITIZER_IOS
TEST(SanitizerCommon, GetRandom) {
u8 buffer_1[32], buffer_2[32];
for (bool blocking : { false, true }) {
Index: lib/sanitizer_common/sanitizer_mac.cc
===================================================================
--- lib/sanitizer_common/sanitizer_mac.cc
+++ lib/sanitizer_common/sanitizer_mac.cc
@@ -69,6 +69,7 @@
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/stat.h>
+#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -1064,14 +1065,29 @@
// Do nothing.
}
-// FIXME: implement on this platform.
bool GetRandom(void *buffer, uptr length, bool blocking) {
- UNIMPLEMENTED();
+ if (!buffer || !length || length > 256)
+ return false;
+ uptr rnd = syscall(SYS_getentropy, buffer, length);
+ int rverrno = 0;
+ if (internal_iserror(rnd, &rverrno) && rverrno == EINVAL)
+ return false;
+ else if (rnd == 0)
+ return true;
+ // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
+ // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
+ uptr fd = internal_open("/dev/urandom", O_RDONLY);
+ if (internal_iserror(fd))
+ return false;
+ uptr res = internal_read(fd, buffer, length);
+ if (internal_iserror(res))
+ return false;
+ internal_close(fd);
+ return true;
}
-// FIXME: implement on this platform.
u32 GetNumberOfCPUs() {
- UNIMPLEMENTED();
+ return (u32)sysconf(_SC_NPROCESSORS_ONLN);
}
} // namespace __sanitizer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50937.162360.patch
Type: text/x-patch
Size: 1912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180824/b197dbc3/attachment.bin>
More information about the llvm-commits
mailing list