xref: /llvm-project/libunwind/test/frameheadercache_test.pass.cpp (revision 282b3eb72372f6a0139923d326b05fc16d86c9fa)
1*282b3eb7SLouis Dionne // -*- C++ -*-
2*282b3eb7SLouis Dionne //===----------------------------------------------------------------------===//
3*282b3eb7SLouis Dionne //
4*282b3eb7SLouis Dionne // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*282b3eb7SLouis Dionne // See https://llvm.org/LICENSE.txt for license information.
6*282b3eb7SLouis Dionne // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*282b3eb7SLouis Dionne //
8*282b3eb7SLouis Dionne //===----------------------------------------------------------------------===//
9*282b3eb7SLouis Dionne 
10c53c2058SSterling Augustine // The other libunwind tests don't test internal interfaces, so the include path
11c53c2058SSterling Augustine // is a little wonky.
12c53c2058SSterling Augustine #include "../src/config.h"
13c53c2058SSterling Augustine 
14c53c2058SSterling Augustine // Only run this test under supported configurations.
153758b858SSterling Augustine 
1688bf133cSRyan Prichard #if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR) &&                                 \
1788bf133cSRyan Prichard     defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
18c53c2058SSterling Augustine 
19c53c2058SSterling Augustine #include <link.h>
20c53c2058SSterling Augustine #include <stdio.h>
21c53c2058SSterling Augustine 
22c53c2058SSterling Augustine // This file defines several of the data structures needed here,
23c53c2058SSterling Augustine // and includes FrameHeaderCache.hpp as well.
24c53c2058SSterling Augustine #include "../src/AddressSpace.hpp"
25c53c2058SSterling Augustine 
26c53c2058SSterling Augustine #define kBaseAddr 0xFFF000
27fb1abe00SRyan Prichard #define kTextSegmentLength 0xFF
28c53c2058SSterling Augustine 
29c53c2058SSterling Augustine using namespace libunwind;
30c53c2058SSterling Augustine 
main(int,char **)31504bc07dSLouis Dionne int main(int, char**) {
32c53c2058SSterling Augustine   FrameHeaderCache FHC;
33c53c2058SSterling Augustine   struct dl_phdr_info PInfo;
34c53c2058SSterling Augustine   memset(&PInfo, 0, sizeof(PInfo));
35c53c2058SSterling Augustine   // The cache itself should only care about these two fields--they
36c53c2058SSterling Augustine   // tell the cache to invalidate or not; everything else is handled
37c53c2058SSterling Augustine   // by AddressSpace.hpp.
38c53c2058SSterling Augustine   PInfo.dlpi_adds = 6;
39c53c2058SSterling Augustine   PInfo.dlpi_subs = 7;
40c53c2058SSterling Augustine 
41c53c2058SSterling Augustine   UnwindInfoSections UIS;
42c53c2058SSterling Augustine   UIS.dso_base = kBaseAddr;
43fb1abe00SRyan Prichard   UIS.text_segment_length = kTextSegmentLength;
44c53c2058SSterling Augustine   dl_iterate_cb_data CBData;
45c53c2058SSterling Augustine   // Unused by the cache.
46c53c2058SSterling Augustine   CBData.addressSpace = nullptr;
47c53c2058SSterling Augustine   CBData.sects = &UIS;
48c53c2058SSterling Augustine   CBData.targetAddr = kBaseAddr + 1;
49c53c2058SSterling Augustine 
50c53c2058SSterling Augustine   // Nothing present, shouldn't find.
51c53c2058SSterling Augustine   if (FHC.find(&PInfo, 0, &CBData))
52c53c2058SSterling Augustine     abort();
53c53c2058SSterling Augustine   FHC.add(&UIS);
54c53c2058SSterling Augustine   // Just added. Should find.
55c53c2058SSterling Augustine   if (!FHC.find(&PInfo, 0, &CBData))
56c53c2058SSterling Augustine     abort();
57c53c2058SSterling Augustine   // Cache is invalid. Shouldn't find.
58c53c2058SSterling Augustine   PInfo.dlpi_adds++;
59c53c2058SSterling Augustine   if (FHC.find(&PInfo, 0, &CBData))
60c53c2058SSterling Augustine     abort();
61c53c2058SSterling Augustine 
62c53c2058SSterling Augustine   FHC.add(&UIS);
63c53c2058SSterling Augustine   CBData.targetAddr = kBaseAddr - 1;
64c53c2058SSterling Augustine   // Shouldn't find something outside of the addresses.
65c53c2058SSterling Augustine   if (FHC.find(&PInfo, 0, &CBData))
66c53c2058SSterling Augustine     abort();
67c53c2058SSterling Augustine   // Add enough things to the cache that the entry is evicted.
68c53c2058SSterling Augustine   for (int i = 0; i < 9; i++) {
69fb1abe00SRyan Prichard     UIS.dso_base = kBaseAddr + (kTextSegmentLength * i);
70c53c2058SSterling Augustine     FHC.add(&UIS);
71c53c2058SSterling Augustine   }
72c53c2058SSterling Augustine   CBData.targetAddr = kBaseAddr;
73c53c2058SSterling Augustine   // Should have been evicted.
74c53c2058SSterling Augustine   if (FHC.find(&PInfo, 0, &CBData))
75c53c2058SSterling Augustine     abort();
76c53c2058SSterling Augustine   return 0;
77c53c2058SSterling Augustine }
7888bf133cSRyan Prichard 
793758b858SSterling Augustine #else
main(int,char **)80504bc07dSLouis Dionne int main(int, char**) { return 0;}
813758b858SSterling Augustine #endif
82