Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 1 | //===-- 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 Collingbourne | 4dd2057 | 2020-12-22 11:48:53 -0800 | [diff] [blame] | 14 | #include "memtag.h" |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 15 | |
| 16 | namespace scudo { |
| 17 | |
| 18 | enum class OptionBit { |
| 19 | MayReturnNull, |
| 20 | FillContents0of2, |
| 21 | FillContents1of2, |
| 22 | DeallocTypeMismatch, |
| 23 | DeleteSizeMismatch, |
| 24 | TrackAllocationStacks, |
| 25 | UseOddEvenTags, |
| 26 | UseMemoryTagging, |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 27 | AddLargeAllocationSlack, |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct 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 Collingbourne | 4dd2057 | 2020-12-22 11:48:53 -0800 | [diff] [blame] | 41 | template <typename Config> bool useMemoryTagging(Options Options) { |
| 42 | return allocatorSupportsMemoryTagging<Config>() && |
| 43 | Options.get(OptionBit::UseMemoryTagging); |
| 44 | } |
| 45 | |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 46 | struct AtomicOptions { |
Vitaly Buka | 5d3d727 | 2021-04-29 01:19:51 -0700 | [diff] [blame] | 47 | atomic_u32 Val = {}; |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 48 | |
Kostya Kortchinsky | a51a892 | 2020-11-02 14:27:11 -0800 | [diff] [blame] | 49 | Options load() const { return Options{atomic_load_relaxed(&Val)}; } |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 50 | |
| 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 Kortchinsky | a51a892 | 2020-11-02 14:27:11 -0800 | [diff] [blame] | 61 | u32 Opts = atomic_load_relaxed(&Val), NewOpts; |
Peter Collingbourne | a7174d5 | 2020-10-02 10:20:31 -0700 | [diff] [blame] | 62 | do { |
| 63 | NewOpts = Opts; |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 64 | NewOpts &= ~(3U << static_cast<u32>(OptionBit::FillContents0of2)); |
| 65 | NewOpts |= static_cast<u32>(FillContents) |
| 66 | << static_cast<u32>(OptionBit::FillContents0of2); |
Peter Collingbourne | a7174d5 | 2020-10-02 10:20:31 -0700 | [diff] [blame] | 67 | } while (!atomic_compare_exchange_strong(&Val, &Opts, NewOpts, |
| 68 | memory_order_relaxed)); |
Peter Collingbourne | 3cf6037 | 2020-09-24 17:01:24 -0700 | [diff] [blame] | 69 | } |
| 70 | }; |
| 71 | |
| 72 | } // namespace scudo |
| 73 | |
| 74 | #endif // SCUDO_OPTIONS_H_ |