blob: 4e678651333467d82613456bc3070051a8b726b4 [file] [log] [blame]
Peter Collingbourne3cf60372020-09-24 17:01:24 -07001//===-- options.h -----------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef SCUDO_OPTIONS_H_
10#define SCUDO_OPTIONS_H_
11
12#include "atomic_helpers.h"
13#include "common.h"
Peter Collingbourne4dd20572020-12-22 11:48:53 -080014#include "memtag.h"
Peter Collingbourne3cf60372020-09-24 17:01:24 -070015
16namespace scudo {
17
18enum class OptionBit {
19 MayReturnNull,
20 FillContents0of2,
21 FillContents1of2,
22 DeallocTypeMismatch,
23 DeleteSizeMismatch,
24 TrackAllocationStacks,
25 UseOddEvenTags,
26 UseMemoryTagging,
Peter Collingbournecc3d4932020-12-21 18:39:03 -080027 AddLargeAllocationSlack,
Peter Collingbourne3cf60372020-09-24 17:01:24 -070028};
29
30struct Options {
31 u32 Val;
32
33 bool get(OptionBit Opt) const { return Val & (1U << static_cast<u32>(Opt)); }
34
35 FillContentsMode getFillContentsMode() const {
36 return static_cast<FillContentsMode>(
37 (Val >> static_cast<u32>(OptionBit::FillContents0of2)) & 3);
38 }
39};
40
Peter Collingbourne4dd20572020-12-22 11:48:53 -080041template <typename Config> bool useMemoryTagging(Options Options) {
42 return allocatorSupportsMemoryTagging<Config>() &&
43 Options.get(OptionBit::UseMemoryTagging);
44}
45
Peter Collingbourne3cf60372020-09-24 17:01:24 -070046struct AtomicOptions {
Vitaly Buka5d3d7272021-04-29 01:19:51 -070047 atomic_u32 Val = {};
Peter Collingbourne3cf60372020-09-24 17:01:24 -070048
Kostya Kortchinskya51a8922020-11-02 14:27:11 -080049 Options load() const { return Options{atomic_load_relaxed(&Val)}; }
Peter Collingbourne3cf60372020-09-24 17:01:24 -070050
51 void clear(OptionBit Opt) {
52 atomic_fetch_and(&Val, ~(1U << static_cast<u32>(Opt)),
53 memory_order_relaxed);
54 }
55
56 void set(OptionBit Opt) {
57 atomic_fetch_or(&Val, 1U << static_cast<u32>(Opt), memory_order_relaxed);
58 }
59
60 void setFillContentsMode(FillContentsMode FillContents) {
Kostya Kortchinskya51a8922020-11-02 14:27:11 -080061 u32 Opts = atomic_load_relaxed(&Val), NewOpts;
Peter Collingbournea7174d52020-10-02 10:20:31 -070062 do {
63 NewOpts = Opts;
Peter Collingbourne3cf60372020-09-24 17:01:24 -070064 NewOpts &= ~(3U << static_cast<u32>(OptionBit::FillContents0of2));
65 NewOpts |= static_cast<u32>(FillContents)
66 << static_cast<u32>(OptionBit::FillContents0of2);
Peter Collingbournea7174d52020-10-02 10:20:31 -070067 } while (!atomic_compare_exchange_strong(&Val, &Opts, NewOpts,
68 memory_order_relaxed));
Peter Collingbourne3cf60372020-09-24 17:01:24 -070069 }
70};
71
72} // namespace scudo
73
74#endif // SCUDO_OPTIONS_H_