xref: /aosp_15_r20/system/unwinding/libunwindstack/tests/TestLocal.cpp (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include "TestUtils.h"
21 
22 // The loop in this function is only guaranteed to not be optimized away by the compiler
23 // if optimizations are turned off. This is partially because the compiler doesn't have
24 // any idea about the function since it is retrieved using dlsym.
25 //
26 // In an effort to defend against the compiler:
27 //  1. The loop iteration variable is volatile.
28 //  2. A call to this function should be wrapped in TestUtils::DoNotOptimize().
BusyWait()29 extern "C" int BusyWait() {
30   for (size_t i = 0; i < 1000000;) {
31     unwindstack::DoNotOptimize(i++);
32   }
33   return 0;
34 }
35 
36 // Do a loop that guarantees the terminating leaf frame will be in
37 // the this library and not a function from a different library.
WaitForever()38 extern "C" void WaitForever() {
39   bool run = true;
40   while (run) {
41     unwindstack::DoNotOptimize(run = true);
42   }
43 }
44