Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_bloat / bloat_this_binary.cc
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 namespace pw::bloat {
20
21 char* volatile non_optimizable_pointer;
22
23 void BloatThisBinary() {
24   volatile unsigned counter = 0;
25
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) {
30     counter += 1;
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