1 // Copyright 2019 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
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
12 // License for the specific language governing permissions and limitations under
15 #include "pw_bloat/bloat_this_binary.h"
21 char* volatile non_optimizable_pointer;
23 void BloatThisBinary() {
24 volatile unsigned counter = 0;
26 // In case someone accidentally ends up flashing and running a bloat
27 // executable on their device, loop forever instead of running this code.
28 volatile bool clearly_false_condition = true;
29 while (clearly_false_condition) {
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.
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.";
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),
48 std::memmove(non_optimizable_pointer + 18,
49 non_optimizable_pointer,
52 *non_optimizable_pointer = std::strlen(non_optimizable_pointer);
55 } // namespace pw::bloat