[NFC][sanitizer] Rename ByteMap to Map

This commit is contained in:
Vitaly Buka 2021-10-10 12:37:31 -07:00
parent 74277e254c
commit 76b7784bcd
7 changed files with 123 additions and 76 deletions

View File

@ -98,7 +98,6 @@ set(SANITIZER_IMPL_HEADERS
sancov_flags.inc
sanitizer_addrhashmap.h
sanitizer_allocator.h
sanitizer_allocator_bytemap.h
sanitizer_allocator_checks.h
sanitizer_allocator_combined.h
sanitizer_allocator_interface.h
@ -137,6 +136,7 @@ set(SANITIZER_IMPL_HEADERS
sanitizer_flag_parser.h
sanitizer_flags.h
sanitizer_flags.inc
sanitizer_flat_map.h
sanitizer_freebsd.h
sanitizer_fuchsia.h
sanitizer_getauxval.h

View File

@ -14,6 +14,7 @@
#define SANITIZER_ALLOCATOR_H
#include "sanitizer_common.h"
#include "sanitizer_flat_map.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_lfstack.h"
#include "sanitizer_libc.h"
@ -43,12 +44,6 @@ void SetAllocatorOutOfMemory();
void PrintHintAllocatorCannotReturnNull();
// Allocators call these callbacks on mmap/munmap.
struct NoOpMapUnmapCallback {
void OnMap(uptr p, uptr size) const { }
void OnUnmap(uptr p, uptr size) const { }
};
// Callback type for iterating over chunks.
typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
@ -70,7 +65,6 @@ inline void RandomShuffle(T *a, u32 n, u32 *rand_state) {
#include "sanitizer_allocator_size_class_map.h"
#include "sanitizer_allocator_stats.h"
#include "sanitizer_allocator_primary64.h"
#include "sanitizer_allocator_bytemap.h"
#include "sanitizer_allocator_primary32.h"
#include "sanitizer_allocator_local_cache.h"
#include "sanitizer_allocator_secondary.h"

View File

@ -1,4 +1,4 @@
//===-- sanitizer_allocator_bytemap.h ---------------------------*- C++ -*-===//
//===-- sanitizer_flat_map.h ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@ -10,11 +10,26 @@
//
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_FLAT_MAP_H
#define SANITIZER_FLAT_MAP_H
#include "sanitizer_atomic.h"
#include "sanitizer_common.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_local_address_space_view.h"
#include "sanitizer_mutex.h"
namespace __sanitizer {
// Call these callbacks on mmap/munmap.
struct NoOpMapUnmapCallback {
void OnMap(uptr p, uptr size) const { }
void OnUnmap(uptr p, uptr size) const { }
};
// Maps integers in rage [0, kSize) to u8 values.
template <u64 kSize, typename AddressSpaceViewTy = LocalAddressSpaceView>
class FlatByteMap {
class FlatMap {
public:
using AddressSpaceView = AddressSpaceViewTy;
void Init() {
@ -43,7 +58,7 @@ class FlatByteMap {
template <u64 kSize1, u64 kSize2,
typename AddressSpaceViewTy = LocalAddressSpaceView,
class MapUnmapCallback = NoOpMapUnmapCallback>
class TwoLevelByteMap {
class TwoLevelMap {
public:
using AddressSpaceView = AddressSpaceViewTy;
void Init() {
@ -91,7 +106,7 @@ class TwoLevelByteMap {
if (!res) {
SpinMutexLock l(&mu_);
if (!(res = Get(idx))) {
res = (u8*)MmapOrDie(kSize2, "TwoLevelByteMap");
res = (u8*)MmapOrDie(kSize2, "TwoLevelMap");
MapUnmapCallback().OnMap(reinterpret_cast<uptr>(res), kSize2);
atomic_store(&map1_[idx], reinterpret_cast<uptr>(res),
memory_order_release);
@ -104,3 +119,15 @@ class TwoLevelByteMap {
StaticSpinMutex mu_;
};
template <u64 kSize, typename AddressSpaceViewTy = LocalAddressSpaceView>
using FlatByteMap = FlatMap<kSize, AddressSpaceViewTy>;
template <u64 kSize1, u64 kSize2,
typename AddressSpaceViewTy = LocalAddressSpaceView,
class MapUnmapCallback = NoOpMapUnmapCallback>
using TwoLevelByteMap =
TwoLevelMap<kSize1, kSize2, AddressSpaceViewTy, MapUnmapCallback>;
}
#endif

View File

@ -17,6 +17,7 @@ set(SANITIZER_UNITTESTS
sanitizer_common_test.cpp
sanitizer_deadlock_detector_test.cpp
sanitizer_flags_test.cpp
sanitizer_flat_map_test.cpp
sanitizer_format_interceptor_test.cpp
sanitizer_hash_test.cpp
sanitizer_ioctl_test.cpp

View File

@ -1412,69 +1412,6 @@ TEST(SanitizerCommon, SizeClassAllocator64VeryCompactReleaseFreeMemoryToOS) {
#endif // SANITIZER_CAN_USE_ALLOCATOR64
TEST(SanitizerCommon, TwoLevelByteMap) {
const u64 kSize1 = 1 << 6, kSize2 = 1 << 12;
const u64 n = kSize1 * kSize2;
TwoLevelByteMap<kSize1, kSize2> m;
m.Init();
for (u64 i = 0; i < n; i += 7) {
m.set(i, (i % 100) + 1);
}
for (u64 j = 0; j < n; j++) {
if (j % 7)
EXPECT_EQ(m[j], 0);
else
EXPECT_EQ(m[j], (j % 100) + 1);
}
m.TestOnlyUnmap();
}
template <typename AddressSpaceView>
using TestByteMapASVT =
TwoLevelByteMap<1 << 12, 1 << 13, AddressSpaceView, TestMapUnmapCallback>;
using TestByteMap = TestByteMapASVT<LocalAddressSpaceView>;
struct TestByteMapParam {
TestByteMap *m;
size_t shard;
size_t num_shards;
};
void *TwoLevelByteMapUserThread(void *param) {
TestByteMapParam *p = (TestByteMapParam*)param;
for (size_t i = p->shard; i < p->m->size(); i += p->num_shards) {
size_t val = (i % 100) + 1;
p->m->set(i, val);
EXPECT_EQ((*p->m)[i], val);
}
return 0;
}
TEST(SanitizerCommon, ThreadedTwoLevelByteMap) {
TestByteMap m;
m.Init();
TestMapUnmapCallback::map_count = 0;
TestMapUnmapCallback::unmap_count = 0;
static const int kNumThreads = 4;
pthread_t t[kNumThreads];
TestByteMapParam p[kNumThreads];
for (int i = 0; i < kNumThreads; i++) {
p[i].m = &m;
p[i].shard = i;
p[i].num_shards = kNumThreads;
PTHREAD_CREATE(&t[i], 0, TwoLevelByteMapUserThread, &p[i]);
}
for (int i = 0; i < kNumThreads; i++) {
PTHREAD_JOIN(t[i], 0);
}
EXPECT_EQ((uptr)TestMapUnmapCallback::map_count, m.size1());
EXPECT_EQ((uptr)TestMapUnmapCallback::unmap_count, 0UL);
m.TestOnlyUnmap();
EXPECT_EQ((uptr)TestMapUnmapCallback::map_count, m.size1());
EXPECT_EQ((uptr)TestMapUnmapCallback::unmap_count, m.size1());
}
TEST(SanitizerCommon, LowLevelAllocatorShouldRoundUpSizeOnAlloc) {
// When allocating a memory block slightly bigger than a memory page and
// LowLevelAllocator calls MmapOrDie for the internal buffer, it should round

View File

@ -0,0 +1,88 @@
//===-- sanitizer_flat_map_test.cpp ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_flat_map.h"
#include "gtest/gtest.h"
#include "sanitizer_common/tests/sanitizer_pthread_wrappers.h"
using namespace __sanitizer;
namespace {
struct TestMapUnmapCallback1 {
static int map_count, unmap_count;
void OnMap(uptr p, uptr size) const { map_count++; }
void OnUnmap(uptr p, uptr size) const { unmap_count++; }
};
int TestMapUnmapCallback1::map_count;
int TestMapUnmapCallback1::unmap_count;
TEST(FlatMapTest, TwoLevelByteMap) {
const u64 kSize1 = 1 << 6, kSize2 = 1 << 12;
const u64 n = kSize1 * kSize2;
TwoLevelByteMap<kSize1, kSize2> m;
m.Init();
for (u64 i = 0; i < n; i += 7) {
m.set(i, (i % 100) + 1);
}
for (u64 j = 0; j < n; j++) {
if (j % 7)
EXPECT_EQ(m[j], 0);
else
EXPECT_EQ(m[j], (j % 100) + 1);
}
m.TestOnlyUnmap();
}
template <typename AddressSpaceView>
using TestByteMapASVT =
TwoLevelByteMap<1 << 12, 1 << 13, AddressSpaceView, TestMapUnmapCallback1>;
using TestByteMap = TestByteMapASVT<LocalAddressSpaceView>;
struct TestByteMapParam {
TestByteMap *m;
size_t shard;
size_t num_shards;
};
static void *TwoLevelByteMapUserThread(void *param) {
TestByteMapParam *p = (TestByteMapParam *)param;
for (size_t i = p->shard; i < p->m->size(); i += p->num_shards) {
size_t val = (i % 100) + 1;
p->m->set(i, val);
EXPECT_EQ((*p->m)[i], val);
}
return 0;
}
TEST(FlatMapTest, ThreadedTwoLevelByteMap) {
TestByteMap m;
m.Init();
TestMapUnmapCallback1::map_count = 0;
TestMapUnmapCallback1::unmap_count = 0;
static const int kNumThreads = 4;
pthread_t t[kNumThreads];
TestByteMapParam p[kNumThreads];
for (int i = 0; i < kNumThreads; i++) {
p[i].m = &m;
p[i].shard = i;
p[i].num_shards = kNumThreads;
PTHREAD_CREATE(&t[i], 0, TwoLevelByteMapUserThread, &p[i]);
}
for (int i = 0; i < kNumThreads; i++) {
PTHREAD_JOIN(t[i], 0);
}
EXPECT_EQ((uptr)TestMapUnmapCallback1::map_count, m.size1());
EXPECT_EQ((uptr)TestMapUnmapCallback1::unmap_count, 0UL);
m.TestOnlyUnmap();
EXPECT_EQ((uptr)TestMapUnmapCallback1::map_count, m.size1());
EXPECT_EQ((uptr)TestMapUnmapCallback1::unmap_count, m.size1());
}
} // namespace

View File

@ -15,7 +15,6 @@ source_set("sources") {
"sanitizer_addrhashmap.h",
"sanitizer_allocator.cpp",
"sanitizer_allocator.h",
"sanitizer_allocator_bytemap.h",
"sanitizer_allocator_checks.cpp",
"sanitizer_allocator_checks.h",
"sanitizer_allocator_combined.h",
@ -60,6 +59,7 @@ source_set("sources") {
"sanitizer_flag_parser.h",
"sanitizer_flags.cpp",
"sanitizer_flags.h",
"sanitizer_flat_map.h",
"sanitizer_freebsd.h",
"sanitizer_fuchsia.cpp",
"sanitizer_fuchsia.h",