Alexei Frolov | e201676 | 2019-11-14 13:49:52 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Alexei Frolov | e201676 | 2019-11-14 13:49:52 -0800 | [diff] [blame] | 6 | // |
| 7 | // https://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, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Alexei Frolov | e201676 | 2019-11-14 13:49:52 -0800 | [diff] [blame] | 14 | |
| 15 | #include "pw_bloat/bloat_this_binary.h" |
| 16 | |
| 17 | #include <cstring> |
| 18 | |
| 19 | namespace pw::bloat { |
| 20 | |
| 21 | char* volatile non_optimizable_pointer; |
| 22 | |
| 23 | void BloatThisBinary() { |
Wyatt Hepler | cfe3526 | 2019-11-15 12:37:57 -0800 | [diff] [blame] | 24 | volatile unsigned counter = 0; |
| 25 | |
Alexei Frolov | e201676 | 2019-11-14 13:49:52 -0800 | [diff] [blame] | 26 | // In case someone accidentally ends up flashing and running a bloat |
Wyatt Hepler | cfe3526 | 2019-11-15 12:37:57 -0800 | [diff] [blame] | 27 | // executable on their device, loop forever instead of running this code. |
Alexei Frolov | e201676 | 2019-11-14 13:49:52 -0800 | [diff] [blame] | 28 | volatile bool clearly_false_condition = true; |
Wyatt Hepler | cfe3526 | 2019-11-15 12:37:57 -0800 | [diff] [blame] | 29 | while (clearly_false_condition) { |
| 30 | counter += 1; |
Alexei Frolov | e201676 | 2019-11-14 13:49:52 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // This code uses standard C/C++ functions such as memcpy to prevent them from |
| 34 | // showing up in size report deltas against a barebones base executable. |
| 35 | // |
| 36 | // This is done using garbage memory addresses as it consistently prevents the |
| 37 | // compiler from optimizing out parts of the code. Other approaches, such as a |
| 38 | // buffer, occasionally ran into optimization issues. |
| 39 | const char* s = "The quick brown fox jumps over the lazy dog."; |
| 40 | |
| 41 | // Making the copy size large forces the compiler to generate a memcpy |
| 42 | // function instead of inlining it. |
| 43 | constexpr int kRandomLargeNumber = 2398; |
| 44 | std::memcpy(non_optimizable_pointer, |
| 45 | non_optimizable_pointer + std::strlen(s), |
| 46 | kRandomLargeNumber); |
| 47 | |
| 48 | std::memmove(non_optimizable_pointer + 18, |
| 49 | non_optimizable_pointer, |
| 50 | kRandomLargeNumber); |
| 51 | |
| 52 | *non_optimizable_pointer = std::strlen(non_optimizable_pointer); |
| 53 | } |
| 54 | |
| 55 | } // namespace pw::bloat |