#include "components/gwp_asan/client/gwp_asan.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <optional>
#include <string>
#include <string_view>
#include "base/allocator/partition_alloc_support.h"
#include "base/containers/flat_set.h"
#include "base/debug/crash_logging.h"
#include "base/feature_list.h"
#include "base/functional/callback_helpers.h"
#include "base/functional/function_ref.h"
#include "base/logging.h"
#include "base/metrics/field_trial_params.h"
#include "base/numerics/safe_math.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "build/build_config.h"
#include "components/crash/core/common/crash_key.h"
#include "components/gwp_asan/client/extreme_lightweight_detector_malloc_shims.h"
#include "components/gwp_asan/client/guarded_page_allocator.h"
#include "components/gwp_asan/client/gwp_asan_features.h"
#include "components/gwp_asan/client/lightweight_detector/poison_metadata_recorder.h"
#include "components/gwp_asan/client/sampling_helpers.h"
#include "components/gwp_asan/common/crash_key_name.h"
#include "partition_alloc/buildflags.h"
#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
#include "components/gwp_asan/client/lightweight_detector/malloc_shims.h"
#include "components/gwp_asan/client/sampling_malloc_shims.h"
#endif
#if PA_BUILDFLAG(USE_PARTITION_ALLOC)
#include "components/gwp_asan/client/lightweight_detector/partitionalloc_shims.h"
#include "components/gwp_asan/client/sampling_partitionalloc_shims.h"
#endif
namespace gwp_asan {
namespace internal {
namespace {
[[maybe_unused]] constexpr bool kCpuIs64Bit = …;
#else
false;
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
constexpr int kDefaultMaxAllocations = …;
constexpr int kDefaultMaxMetadata = …;
constexpr int kDefaultTotalPages = …;
constexpr int kDefaultAllocationSamplingMultiplier = …;
constexpr int kDefaultAllocationSamplingRange = …;
constexpr double kDefaultProcessSamplingProbability = …;
#elif BUILDFLAG(IS_ANDROID)
constexpr int kDefaultMaxAllocations = 70;
constexpr int kDefaultMaxMetadata = 255;
constexpr int kDefaultTotalPages = 512;
constexpr int kDefaultAllocationSamplingMultiplier = 2000;
constexpr int kDefaultAllocationSamplingRange = 20;
constexpr double kDefaultProcessSamplingProbability = 0.015;
#else
constexpr int kDefaultMaxAllocations = 70;
constexpr int kDefaultMaxMetadata = 255;
constexpr int kDefaultTotalPages = kCpuIs64Bit ? 2048 : kDefaultMaxMetadata * 2;
constexpr int kDefaultAllocationSamplingMultiplier = 1000;
constexpr int kDefaultAllocationSamplingRange = 16;
constexpr double kDefaultProcessSamplingProbability = 0.015;
#endif
constexpr int kDefaultProcessSamplingBoost2 = …;
#if defined(ARCH_CPU_64_BITS)
constexpr int kDefaultMaxLightweightMetadata = …;
#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
constexpr int kDefaultMaxTotalSize = …;
constexpr int kDefaultTotalSizeHighWaterMark = …;
constexpr int kDefaultTotalSizeLowWaterMark = …;
constexpr int kDefaultEvictionChunkSize = …;
constexpr int kDefaultEvictionTaskIntervalMs = …;
constexpr int kMaxMaxTotalSize = …;
constexpr int kMaxEvictionChunkSize = …;
constexpr int kMaxEvictionTaskIntervalMs = …;
#endif
#endif
BASE_FEATURE(…);
constexpr base::FeatureParam<LightweightDetectorMode>::Option
kLightweightUafDetectorModeOptions[] = …;
const base::FeatureParam<LightweightDetectorMode>
kLightweightUafDetectorModeParam{ … };
std::optional<int> GetIntParam(const base::Feature& feature,
const std::string& param,
int fallback,
std::string_view process_type,
base::FunctionRef<bool(int)> failure_condition) { … }
bool SampleProcess(const base::Feature& feature, bool boost_sampling) { … }
size_t AllocationSamplingFrequency(const base::Feature& feature,
std::string_view process_type) { … }
bool IsMutuallyExclusiveFeatureAllowed(const base::Feature& feature) { … }
}
GWP_ASAN_EXPORT std::optional<AllocatorSettings> GetAllocatorSettingsImpl(
const base::Feature& feature,
bool boost_sampling,
std::string_view process_type) { … }
GWP_ASAN_EXPORT std::optional<AllocatorSettings> GetAllocatorSettings(
const base::Feature& feature,
bool boost_sampling,
std::string_view process_type) { … }
bool MaybeEnableLightweightDetectorInternal(bool boost_sampling,
const char* process_type) { … }
}
void EnableForMalloc(bool boost_sampling, std::string_view process_type) { … }
void EnableForPartitionAlloc(bool boost_sampling,
std::string_view process_type) { … }
void MaybeEnableLightweightDetector(bool boost_sampling,
const char* process_type) { … }
void MaybeEnableExtremeLightweightDetector(bool boost_sampling,
std::string_view process_type) { … }
}