summaryrefslogtreecommitdiffstats
path: root/mozglue/tests/gtest/TestStackWalk.cpp
blob: cec21ffc5e42641608881bfa5cbd3791353a4852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// The header under test.
#include "mozilla/StackWalk.h"

#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"

#include <algorithm>

#include "gtest/gtest.h"

MOZ_EXPORT bool gStackWalkTesterDummy = true;

struct StackWalkTester;

// Descriptor of the recursive function calls wanted, and for each of them
// whether to perform tail call optimization or not.
struct CallInfo {
  int (*mFunc)(int aDepth, int aLastSkipped, int aIgnored,
               StackWalkTester& aTester);
  bool mTailCall;

  bool TailCall() {
#if defined(__i386__) || defined(MOZ_CODE_COVERAGE)
    // We can't make tail calls happen on i386 because all arguments to
    // functions are on the stack, so the stack pointer needs to be updated
    // before the call and restored after the call, so tail call optimization
    // never happens.
    // Similarly, code-coverage flags don't guarantee that tail call
    // optimization will happen.
    return false;
#else
    return mTailCall;
#endif
  }
};

struct PCRange {
  void* mStart;
  void* mEnd;
};

// PCRange pretty printer for gtest assertions.
std::ostream& operator<<(std::ostream& aStream, const PCRange& aRange) {
  aStream << aRange.mStart;
  aStream << "-";
  aStream << aRange.mEnd;
  return aStream;
}

// Allow to use EXPECT_EQ with a vector of PCRanges and a vector of plain
// addresses, allowing a more useful output when the test fails, showing
// both lists.
bool operator==(const std::vector<PCRange>& aRanges,
                const std::vector<void*>& aPtrs) {
  if (aRanges.size() != aPtrs.size()) {
    return false;
  }
  for (size_t i = 0; i < aRanges.size(); i++) {
    auto range = aRanges[i];
    auto ptr = reinterpret_cast<uintptr_t>(aPtrs[i]);
    if (ptr <= reinterpret_cast<uintptr_t>(range.mStart) ||
        ptr >= reinterpret_cast<uintptr_t>(range.mEnd)) {
      return false;
    }
  }
  return true;
}

struct StackWalkTester {
  // Description of the recursion of functions to perform for the testcase.
  std::vector<CallInfo> mFuncCalls;
  // Collection of PCs reported by MozStackWalk.
  std::vector<void*> mFramePCs;
  // Collection of PCs expected per what was observed while recursing.
  std::vector<PCRange> mExpectedFramePCs;
  // The aFirstFramePC value that will be passed to MozStackWalk.
  void* mFirstFramePC = nullptr;

  // Callback to be given to the stack walker.
  // aClosure should point at an instance of this class.
  static void StackWalkCallback(uint32_t aFrameNumber, void* aPC, void* aSP,
                                void* aClosure) {
    ASSERT_NE(aClosure, nullptr);
    StackWalkTester& tester = *reinterpret_cast<StackWalkTester*>(aClosure);
    tester.mFramePCs.push_back(aPC);
    EXPECT_EQ(tester.mFramePCs.size(), size_t(aFrameNumber))
        << "Frame number doesn't match";
  }

  // Callers of this function get a range of addresses with:
  // ```
  // label:
  //   recursion();
  //   AddExpectedPC(&&label);
  // ```
  // This intends to record the range from label to the return of AddExpectedPC.
  // The ideal code would be:
  // ```
  //   recursion();
  // label:
  //   AddExpectedPC(&&label);
  // ```
  // and we wouldn't need to keep ranges. But while this works fine with Clang,
  // GCC actually sometimes reorders code such the address received by
  // AddExpectedPC is the address *before* the recursion.
  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99784 Using a label before the
  // recursion and CallerPC() from a function call after the recursion makes it
  // less likely for things to go wrong.
  MOZ_NEVER_INLINE void AddExpectedPC(void* aPC) {
    mExpectedFramePCs.push_back({aPC, CallerPC()});
  }

  // Function intended to be called in sequence for recursion.
  // CallInfo lists are meant to contain a sequence of IntermediateCallback<1>,
  // IntermediateCallback<2>, etc.
  // aDepth is a counter of how deep the recursion has gone so far;
  // aLastSkipped is the depth of the last frame we want skipped in the
  // testcase; aIgnored is there to avoid the compiler merging both recursive
  // function calls, which would prevent tail call optimization happening on one
  // of them. aTester is the instance of this class for the testcase.
  template <int Id>
  MOZ_NEVER_INLINE MOZ_EXPORT static int IntermediateCallback(
      int aDepth, int aLastSkipped, int aIgnored, StackWalkTester& aTester) {
    auto& callInfo = aTester.mFuncCalls.at(aDepth + 1);
    if (aDepth == aLastSkipped) {
      aTester.mFirstFramePC = CallerPC();
    }
    if (aTester.mFuncCalls.at(aDepth).TailCall()) {
      return callInfo.mFunc(aDepth + 1, aLastSkipped, Id, aTester);
      // Since we're doing a tail call, we're not expecting this frame appearing
      // in the trace.
    }
  here:
    callInfo.mFunc(aDepth + 1, aLastSkipped, Id + 1, aTester);
    aTester.AddExpectedPC(&&here);
    return 0;
  }

#if defined(__clang__)
  __attribute__((no_sanitize("function")))
#endif
  MOZ_NEVER_INLINE MOZ_EXPORT static void
  LeafCallback(int aDepth, int aLastSkipped, int aIgnored,
               StackWalkTester& aTester) {
    if (aDepth == aLastSkipped) {
      aTester.mFirstFramePC = CallerPC();
    }
    if (aTester.mFuncCalls.at(aDepth).TailCall()) {
      // For the same reason that we have the aIgnored argument on these
      // callbacks, we need to avoid both MozStackWalk calls to be merged by the
      // compiler, so we use different values of aMaxFrames for that.
      return MozStackWalk(StackWalkTester::StackWalkCallback,
                          aTester.mFirstFramePC,
                          /*aMaxFrames*/ 19, &aTester);
      // Since we're doing a tail call, we're not expecting this frame appearing
      // in the trace.
    }
  here:
    MozStackWalk(StackWalkTester::StackWalkCallback, aTester.mFirstFramePC,
                 /*aMaxFrames*/ 20, &aTester);
    aTester.AddExpectedPC(&&here);
    // Because we return nothing from this function, simply returning here would
    // produce a tail-call optimization, which we explicitly don't want to
    // happen. So we add a branch that depends on an extern value to prevent
    // that from happening.
    MOZ_RELEASE_ASSERT(gStackWalkTesterDummy);
  }

  explicit StackWalkTester(std::initializer_list<CallInfo> aFuncCalls)
      : mFuncCalls(aFuncCalls) {}

  // Dump a vector of PCRanges as WalkTheStack would, for test failure output.
  // Only the end of the range is shown. Not ideal, but
  // MozFormatCodeAddressDetails only knows to deal with one address at a time.
  // The full ranges would be printed by EXPECT_EQ anyways.
  static std::string DumpFrames(std::vector<PCRange>& aFramePCRanges) {
    std::vector<void*> framePCs;
    framePCs.reserve(aFramePCRanges.size());
    for (auto range : aFramePCRanges) {
      framePCs.push_back(range.mEnd);
    }
    return DumpFrames(framePCs);
  }

  // Dump a vector of addresses as WalkTheStack would, for test failure output.
  static std::string DumpFrames(std::vector<void*>& aFramePCs) {
    size_t n = 0;
    std::string result;
    for (auto* framePC : aFramePCs) {
      char buf[1024];
      MozCodeAddressDetails details;
      result.append("  ");
      n++;
      if (MozDescribeCodeAddress(framePC, &details)) {
        int length =
            MozFormatCodeAddressDetails(buf, sizeof(buf), n, framePC, &details);
        result.append(buf, std::min(length, (int)sizeof(buf) - 1));
      } else {
        result.append("MozDescribeCodeAddress failed");
      }
      result.append("\n");
    }
    return result;
  }

  // Dump a description of the given test case.
  static std::string DumpFuncCalls(std::vector<CallInfo>& aFuncCalls) {
    std::string result;
    for (auto funcCall : aFuncCalls) {
      MozCodeAddressDetails details;
      result.append("  ");
      if (MozDescribeCodeAddress(reinterpret_cast<void*>(funcCall.mFunc),
                                 &details)) {
        result.append(details.function);
        if (funcCall.TailCall()) {
          result.append(" tail call");
        }
      } else {
        result.append("MozDescribeCodeAddress failed");
      }
      result.append("\n");
    }
    return result;
  }

  MOZ_EXPORT MOZ_NEVER_INLINE void RunTest(int aLastSkipped) {
    ASSERT_TRUE(aLastSkipped < (int)mFuncCalls.size());
    mFramePCs.clear();
    mExpectedFramePCs.clear();
    mFirstFramePC = nullptr;
    auto& callInfo = mFuncCalls.at(0);
  here:
    callInfo.mFunc(0, aLastSkipped, 0, *this);
    AddExpectedPC(&&here);
    if (aLastSkipped < 0) {
      aLastSkipped = mFuncCalls.size();
    }
    for (int i = (int)mFuncCalls.size() - 1; i >= aLastSkipped; i--) {
      if (!mFuncCalls.at(i).TailCall()) {
        mExpectedFramePCs.erase(mExpectedFramePCs.begin());
      }
    }
    mFramePCs.resize(std::min(mExpectedFramePCs.size(), mFramePCs.size()));
    EXPECT_EQ(mExpectedFramePCs, mFramePCs)
        << "Expected frames:\n"
        << DumpFrames(mExpectedFramePCs) << "Found frames:\n"
        << DumpFrames(mFramePCs)
        << "Function calls data (last skipped: " << aLastSkipped << "):\n"
        << DumpFuncCalls(mFuncCalls);
  }
};

TEST(TestStackWalk, StackWalk)
{
  const auto foo = StackWalkTester::IntermediateCallback<1>;
  const auto bar = StackWalkTester::IntermediateCallback<2>;
  const auto qux = StackWalkTester::IntermediateCallback<3>;
  const auto leaf = reinterpret_cast<int (*)(int, int, int, StackWalkTester&)>(
      StackWalkTester::LeafCallback);

  const std::initializer_list<CallInfo> tests[] = {
      {{foo, false}, {bar, false}, {qux, false}, {leaf, false}},
      {{foo, false}, {bar, true}, {qux, false}, {leaf, false}},
      {{foo, false}, {bar, false}, {qux, false}, {leaf, true}},
      {{foo, true}, {bar, false}, {qux, true}, {leaf, true}},
  };
  for (auto test : tests) {
    StackWalkTester tester(test);
    for (int i = -1; i < (int)test.size(); i++) {
      tester.RunTest(i);
    }
  }
}