Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(540)

Unified Diff: sandbox/win/src/interception_unittest.cc

Issue 1342303003: Replace calls to rand_s with calls to RtlGenRandom (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix nit Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sandbox/win/src/interception.cc ('k') | sandbox/win/src/process_mitigations.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sandbox/win/src/interception_unittest.cc
diff --git a/sandbox/win/src/interception_unittest.cc b/sandbox/win/src/interception_unittest.cc
index 0fc9b7cbe2ebf6af7b6477222a6df0da996c5e8e..7ce5724bdc5e83542abc5499df3a7a09b30bdd7f 100644
--- a/sandbox/win/src/interception_unittest.cc
+++ b/sandbox/win/src/interception_unittest.cc
@@ -6,8 +6,12 @@
// The tests require private information so the whole interception.cc file is
// included from this file.
+#include <algorithm>
+#include <set>
+
#include <windows.h>
+#include "base/bits.h"
#include "base/memory/scoped_ptr.h"
#include "sandbox/win/src/interception.h"
#include "sandbox/win/src/interceptors.h"
@@ -17,6 +21,10 @@
namespace sandbox {
+namespace internal {
+size_t GetGranularAlignedRandomOffset(size_t size);
+}
+
// Walks the settings buffer, verifying that the values make sense and counting
// objects.
// Arguments:
@@ -75,6 +83,45 @@ void WalkBuffer(void* buffer, size_t size, int* num_dlls, int* num_functions,
}
}
+TEST(InterceptionManagerTest, GetGranularAlignedRandomOffset) {
+ std::set<size_t> sizes;
+
+ // 544 is current value of interceptions_.size() * sizeof(ThunkData) +
+ // sizeof(DllInterceptionData).
+ const size_t kThunkBytes = 544;
+
+ // ciel(log2(544)) = 10.
+ // Alignment must be 2^10 = 1024.
+ const size_t kAlignmentBits = base::bits::Log2Ceiling(kThunkBytes);
+ const size_t kAlignment = 1 << kAlignmentBits;
+
+ const size_t kAllocGranularity = 65536;
+
+ // Generate enough sample data to ensure there is at least one value in each
+ // potential bucket.
+ for (size_t i = 0; i < 1000000; i++)
+ sizes.insert(internal::GetGranularAlignedRandomOffset(kThunkBytes));
+
+ size_t prev_val = 0;
+ size_t min_val = kAllocGranularity;
+ size_t min_nonzero_val = kAllocGranularity;
+ size_t max_val = 0;
+
+ for (size_t val : sizes) {
+ ASSERT_LT(val, kAllocGranularity);
+ if (prev_val)
+ ASSERT_EQ(val - prev_val, kAlignment);
+ if (val)
+ min_nonzero_val = std::min(val, min_nonzero_val);
+ min_val = std::min(val, min_val);
+ prev_val = val;
+ max_val = std::max(val, max_val);
+ }
+ ASSERT_EQ(max_val, kAllocGranularity - kAlignment);
+ ASSERT_EQ(min_val, 0);
+ ASSERT_EQ(min_nonzero_val, kAlignment);
+}
+
TEST(InterceptionManagerTest, BufferLayout1) {
wchar_t exe_name[MAX_PATH];
ASSERT_NE(0u, GetModuleFileName(NULL, exe_name, MAX_PATH - 1));
« no previous file with comments | « sandbox/win/src/interception.cc ('k') | sandbox/win/src/process_mitigations.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698