| 1 | //===-- copyprof_state.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 | /// This file declares the per-thread and per-object state structures used to |
| 10 | /// track special member function nesting and dynamic memory allocations. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef COPYPROF_STATE_H |
| 15 | #define COPYPROF_STATE_H |
| 16 | |
| 17 | #include "sanitizer_common/sanitizer_internal_defs.h" |
| 18 | |
| 19 | namespace __copyprof { |
| 20 | |
| 21 | // Determines the special member function (SMF) execution context of a thread, |
| 22 | // dictating how stores and allocations affect shadow memory. |
| 23 | // The first time control flow reaches the entry point of a special member |
| 24 | // function, the current SMF context is changed accordingly. |
| 25 | // A copy c'tor or copy assignment operator sets the context to `COPY`, a c'tor |
| 26 | // to `CTOR`, and a d'tor to `DTOR`. |
| 27 | // In the `COPY` context, shadow memory is marked as copy. |
| 28 | // In the `CTOR` context, shadow memory is marked as non-copy. |
| 29 | // In the `DTOR` context, shadow memory is not updated (to avoid false negatives |
| 30 | // during destruction), except that newly allocated or constructed memory is |
| 31 | // marked as copy, so that freeing or destroying temporaries does not cause |
| 32 | // false negatives either (see TemporaryObjectInDtorDoesNotSuppressReport). Note |
| 33 | // that newly created temporary objects inside d'tors would still check their |
| 34 | // shadow memory when destroyed, but if that is never updated when constructed, |
| 35 | // its state wouldn't be meaningful. That's why shadow memory is marked as copy |
| 36 | // in d'tors as well (see DISABLED_CopyModifiedByAnotherDtorIsNotReported for a |
| 37 | // contrived example where this leads to FPs). |
| 38 | // Once control flow leaves (any nested) special member functions, the context |
| 39 | // is set to `NONE`. In this state, any stores mark shadow memory as non-copy. |
| 40 | enum class SmfContext : u8 { |
| 41 | NONE, |
| 42 | CTOR, |
| 43 | COPY, |
| 44 | DTOR, |
| 45 | }; |
| 46 | |
| 47 | // Whether memory that is newly allocated or constructed in `ctx` is marked as |
| 48 | // copy (see `SmfContext`). |
| 49 | inline bool ShouldMarkNewMemoryAsCopy(SmfContext ctx) { |
| 50 | return ctx == SmfContext::COPY || ctx == SmfContext::DTOR; |
| 51 | } |
| 52 | |
| 53 | // CopyProf uses per-thread state to figure out whether control flow is |
| 54 | // currently inside a special member function, and adapts updating of shadow |
| 55 | // memory accordingly (see SmfContext). Since special member functions can |
| 56 | // nest arbitrarily, this state needs to be kept across function calls, so this |
| 57 | // state is stored in TLS. The nesting level counters are used to determine |
| 58 | // whether a top-level (i.e. the first in the call stack of special member |
| 59 | // functions) special member function has been reached. |
| 60 | struct PerThreadState { |
| 61 | u32 construct_nesting_level = 0; |
| 62 | u32 copy_nesting_level = 0; |
| 63 | u32 destruct_nesting_level = 0; |
| 64 | SmfContext smf_context = SmfContext::NONE; |
| 65 | // Whether all transitively reachable d'tors from the top-level d'tor have |
| 66 | // observed copies. |
| 67 | bool is_transitive_copy = false; |
| 68 | // When control flow enters a special member function, this is set to the |
| 69 | // `this` pointer of the current object. This is used to look up the |
| 70 | // per-object state outside of special member functions (e.g. when allocating |
| 71 | // memory). |
| 72 | const void* current_this_ptr = nullptr; |
| 73 | }; |
| 74 | |
| 75 | // Each copied object has an associated `PerObjectState`. |
| 76 | // Used for tracking the transitive size (size of self + all memory allocations) |
| 77 | // of the object. |
| 78 | struct PerObjectState { |
| 79 | void SetStaticSize(usize num_bytes); |
| 80 | void IncreaseTrackedSize(usize num_bytes); |
| 81 | usize GetTrackedSize() const; |
| 82 | |
| 83 | // Whether memory was dynamically allocated while making the copy. |
| 84 | bool did_allocate = false; |
| 85 | |
| 86 | // Total transitive size (self + all memory allocations) in bytes. |
| 87 | usize tracked_size = 0; |
| 88 | }; |
| 89 | |
| 90 | // The runtime is always linked into the main executable, so the state can be |
| 91 | // reached with the initial-exec model instead of paying for a __tls_get_addr |
| 92 | // call on every access. |
| 93 | __attribute__((tls_model( |
| 94 | "initial-exec" ))) extern THREADLOCAL PerThreadState __copyprof_state; |
| 95 | |
| 96 | } // namespace __copyprof |
| 97 | |
| 98 | #endif // COPYPROF_STATE_H |
| 99 | |