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

source code of libunwind/test/frameheadercache_test.pass.cpp