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