1 // Copyright 2019 The Pigweed Authors
2 //
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
5 // the License at
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
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bloat/bloat_this_binary.h"
16
17 #include <cstring>
18
19 #include "pw_assert/assert.h"
20 #include "pw_assert/light.h"
21 #include "pw_log/log.h"
22
23 namespace pw::bloat {
24
25 char* volatile non_optimizable_pointer;
26
BloatThisBinary()27 void BloatThisBinary() {
28 volatile unsigned counter = 0;
29
30 // In case someone accidentally ends up flashing and running a bloat
31 // executable on their device, loop forever instead of running this code.
32 volatile bool clearly_false_condition = true;
33 while (clearly_false_condition) {
34 counter += 1;
35 }
36
37 // This code uses standard C/C++ functions such as memcpy to prevent them from
38 // showing up in size report deltas against a barebones base executable.
39 //
40 // This is done using garbage memory addresses as it consistently prevents the
41 // compiler from optimizing out parts of the code. Other approaches, such as a
42 // buffer, occasionally ran into optimization issues.
43 const char* s = "The quick brown fox jumps over the lazy dog.";
44
45 // Making the copy size large forces the compiler to generate a memcpy
46 // function instead of inlining it.
47 constexpr int kRandomLargeNumber = 2398;
48 std::memcpy(non_optimizable_pointer,
49 non_optimizable_pointer + std::strlen(s),
50 kRandomLargeNumber);
51
52 std::memmove(non_optimizable_pointer + 18,
53 non_optimizable_pointer,
54 kRandomLargeNumber);
55
56 *non_optimizable_pointer = std::strlen(non_optimizable_pointer);
57
58 // This code ensures users do not have to pay for the base cost of using
59 // asserts and logging.
60 PW_ASSERT(*non_optimizable_pointer);
61 PW_DASSERT(*non_optimizable_pointer);
62 PW_CHECK_INT_GE(*non_optimizable_pointer, 0, "Ensure this logic stays");
63 PW_DCHECK_INT_GE(*non_optimizable_pointer, 0, "Ensure this logic stays");
64 PW_LOG_INFO("We care about optimizing: %d", *non_optimizable_pointer);
65 }
66
67 } // namespace pw::bloat
68