Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 1 | // Copyright 2021 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "Pragma.hpp" |
| 16 | #include "PragmaInternals.hpp" |
| 17 | |
| 18 | #include "Debug.hpp" |
| 19 | |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 20 | // The CLANG_NO_SANITIZE_MEMORY macro suppresses MemorySanitizer checks for |
| 21 | // use-of-uninitialized-data. It is used to decorate functions with known |
| 22 | // false positives. |
| 23 | #ifdef __clang__ |
| 24 | # define CLANG_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) |
| 25 | #else |
| 26 | # define CLANG_NO_SANITIZE_MEMORY |
| 27 | #endif |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 28 | |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | struct PragmaState |
| 32 | { |
Nicolas Capens | 80de201 | 2022-05-13 13:43:21 -0400 | [diff] [blame^] | 33 | bool memorySanitizerInstrumentation = true; |
Nicolas Capens | 79d4c6c | 2022-04-22 17:20:26 -0400 | [diff] [blame] | 34 | int optimizationLevel = 2; // Default |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | // The initialization of static thread-local data is not observed by MemorySanitizer |
| 38 | // when inside a shared library, leading to false-positive use-of-uninitialized-data |
| 39 | // errors: https://github.com/google/sanitizers/issues/1409 |
| 40 | // We work around this by assigning an initial value to it ourselves on first use. |
| 41 | // Note that since the flag to check whether this initialization has already been |
| 42 | // done is itself a static thread-local, we must suppress the MemorySanitizer check |
| 43 | // with a function attribute. |
| 44 | CLANG_NO_SANITIZE_MEMORY PragmaState &getPragmaState() |
| 45 | { |
| 46 | static thread_local bool initialized = false; |
| 47 | static thread_local PragmaState state; |
| 48 | |
| 49 | if(!initialized) |
| 50 | { |
| 51 | state = {}; |
| 52 | |
| 53 | initialized = true; |
| 54 | } |
| 55 | |
| 56 | return state; |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | namespace rr { |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 62 | |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 63 | void Pragma(BooleanPragmaOption option, bool enable) |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 64 | { |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 65 | PragmaState &state = ::getPragmaState(); |
| 66 | |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 67 | switch(option) |
| 68 | { |
| 69 | case MemorySanitizerInstrumentation: |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 70 | state.memorySanitizerInstrumentation = enable; |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 71 | break; |
| 72 | default: |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 73 | UNSUPPORTED("Unknown Boolean pragma option %d", int(option)); |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Nicolas Capens | 79d4c6c | 2022-04-22 17:20:26 -0400 | [diff] [blame] | 77 | void Pragma(IntegerPragmaOption option, int value) |
| 78 | { |
| 79 | PragmaState &state = ::getPragmaState(); |
| 80 | |
| 81 | switch(option) |
| 82 | { |
| 83 | case OptimizationLevel: |
| 84 | state.optimizationLevel = value; |
| 85 | break; |
| 86 | default: |
| 87 | UNSUPPORTED("Unknown integer pragma option %d", int(option)); |
| 88 | } |
| 89 | } |
| 90 | |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 91 | bool getPragmaState(BooleanPragmaOption option) |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 92 | { |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 93 | PragmaState &state = ::getPragmaState(); |
| 94 | |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 95 | switch(option) |
| 96 | { |
| 97 | case MemorySanitizerInstrumentation: |
Nicolas Capens | 314508f | 2021-07-01 01:23:28 -0400 | [diff] [blame] | 98 | return state.memorySanitizerInstrumentation; |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 99 | default: |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 100 | UNSUPPORTED("Unknown Boolean pragma option %d", int(option)); |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
Nicolas Capens | 79d4c6c | 2022-04-22 17:20:26 -0400 | [diff] [blame] | 105 | int getPragmaState(IntegerPragmaOption option) |
| 106 | { |
| 107 | PragmaState &state = ::getPragmaState(); |
| 108 | |
| 109 | switch(option) |
| 110 | { |
| 111 | case OptimizationLevel: |
| 112 | return state.optimizationLevel; |
| 113 | default: |
| 114 | UNSUPPORTED("Unknown integer pragma option %d", int(option)); |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 119 | ScopedPragma::ScopedPragma(BooleanPragmaOption option, bool enable) |
| 120 | { |
| 121 | oldState = BooleanPragma{ option, getPragmaState(option) }; |
| 122 | Pragma(option, enable); |
| 123 | } |
| 124 | |
Nicolas Capens | 79d4c6c | 2022-04-22 17:20:26 -0400 | [diff] [blame] | 125 | ScopedPragma::ScopedPragma(IntegerPragmaOption option, int value) |
| 126 | { |
| 127 | oldState = IntegerPragma{ option, getPragmaState(option) }; |
| 128 | Pragma(option, value); |
| 129 | } |
| 130 | |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 131 | ScopedPragma::~ScopedPragma() |
| 132 | { |
Nicolas Capens | 79d4c6c | 2022-04-22 17:20:26 -0400 | [diff] [blame] | 133 | if(std::holds_alternative<BooleanPragma>(oldState)) |
| 134 | { |
| 135 | auto &restore = std::get<BooleanPragma>(oldState); |
| 136 | Pragma(restore.option, restore.enable); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | auto &restore = std::get<IntegerPragma>(oldState); |
| 141 | Pragma(restore.option, restore.value); |
| 142 | } |
Nicolas Capens | 25c7598 | 2022-04-22 14:40:56 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Capens | c9991d4 | 2021-06-16 00:46:28 -0400 | [diff] [blame] | 145 | } // namespace rr |