summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Gallop <russell.gallop@sony.com>2020-09-10 17:14:04 +0100
committerRussell Gallop <russell.gallop@sony.com>2021-01-21 16:55:48 +0000
commit6deedb93bb508ffeb0c2fefe1d2a2c749aa7cc70 (patch)
treee97950a1124c9e1c2754d89f3c37bcdfb0e2fc9f
parent57617feaa9af7c20e06ba3d87d4e787f9f1a7a34 (diff)
downloadllvm-6deedb93bb508ffeb0c2fefe1d2a2c749aa7cc70.tar.gz
Steal implementations of getNumberOfCPUs, getThreadID, getRandom from sanitizer_win.cpp
Could I just steal the whole of sanitizer_win.cpp?
-rw-r--r--compiler-rt/lib/scudo/standalone/internal_defs.h3
-rw-r--r--compiler-rt/lib/scudo/standalone/wwindows.cpp54
2 files changed, 24 insertions, 33 deletions
diff --git a/compiler-rt/lib/scudo/standalone/internal_defs.h b/compiler-rt/lib/scudo/standalone/internal_defs.h
index 73b65b633e67..50339a9d7a10 100644
--- a/compiler-rt/lib/scudo/standalone/internal_defs.h
+++ b/compiler-rt/lib/scudo/standalone/internal_defs.h
@@ -146,6 +146,9 @@ typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
+
+typedef u64 tid_t;
+
// The following two functions have platform specific implementations.
void outputRaw(const char *Buffer);
void NORETURN die();
diff --git a/compiler-rt/lib/scudo/standalone/wwindows.cpp b/compiler-rt/lib/scudo/standalone/wwindows.cpp
index 233ab962c061..abad88da7cc7 100644
--- a/compiler-rt/lib/scudo/standalone/wwindows.cpp
+++ b/compiler-rt/lib/scudo/standalone/wwindows.cpp
@@ -28,6 +28,18 @@
#include <time.h>
//#include <unistd.h>
+#include <windows.h>
+#include <io.h>
+#include <psapi.h>
+#include <stdlib.h>
+
+// #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the
+// "Community Additions" comment on MSDN here:
+// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx
+#define SystemFunction036 NTAPI SystemFunction036
+#include <NTSecAPI.h>
+#undef SystemFunction036
+
namespace scudo {
uptr getPageSize() { /*return static_cast<uptr>(sysconf(_SC_PAGESIZE)); */ }
@@ -113,44 +125,20 @@ u64 getMonotonicTime() {
}
u32 getNumberOfCPUs() {
-// cpu_set_t CPUs;
-// // sched_getaffinity can fail for a variety of legitimate reasons (lack of
-// // CAP_SYS_NICE, syscall filtering, etc), in which case we shall return 0.
-// if (sched_getaffinity(0, sizeof(cpu_set_t), &CPUs) != 0)
-// return 0;
-// return static_cast<u32>(CPU_COUNT(&CPUs));
- return 0;
+ SYSTEM_INFO sysinfo = {};
+ GetNativeSystemInfo(&sysinfo);
+ return sysinfo.dwNumberOfProcessors;
}
-u32 getThreadID() {
- //return static_cast<u32>(syscall(SYS_gettid));
- return 0;
+tid_t getThreadID() {
+ return GetCurrentThreadId();
}
-
+#pragma comment(lib, "advapi32.lib")
// Blocking is possibly unused if the getrandom block is not compiled in.
bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
- return true;
-// if (!Buffer || !Length || Length > MaxRandomLength)
-// return false;
-// ssize_t ReadBytes;
-//#if defined(SYS_getrandom)
-//#if !defined(GRND_NONBLOCK)
-//#define GRND_NONBLOCK 1
-//#endif
-// // Up to 256 bytes, getrandom will not be interrupted.
-// ReadBytes =
-// syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
-// if (ReadBytes == static_cast<ssize_t>(Length))
-// return true;
-//#endif // defined(SYS_getrandom)
-// // 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.
-// const int FileDesc = open("/dev/urandom", O_RDONLY);
-// if (FileDesc == -1)
-// return false;
-// ReadBytes = read(FileDesc, Buffer, Length);
-// close(FileDesc);
-// return (ReadBytes == static_cast<ssize_t>(Length));
+ if (!Buffer || !Length || Length > 256)
+ return false;
+ return RtlGenRandom(Buffer, Length) != FALSE;
}
// Allocation free syslog-like API.