1b082c360SDean Michael Berris //===- llvm/unittest/XRay/FDRBlockVerifierTest.cpp --------------*- C++ -*-===//
2b082c360SDean Michael Berris //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b082c360SDean Michael Berris //
7b082c360SDean Michael Berris //===----------------------------------------------------------------------===//
8b082c360SDean Michael Berris #include "llvm/Testing/Support/Error.h"
9b082c360SDean Michael Berris #include "llvm/XRay/BlockIndexer.h"
10b082c360SDean Michael Berris #include "llvm/XRay/BlockVerifier.h"
11b082c360SDean Michael Berris #include "llvm/XRay/FDRLogBuilder.h"
12b082c360SDean Michael Berris #include "llvm/XRay/FDRRecords.h"
13b082c360SDean Michael Berris #include "gmock/gmock.h"
14b082c360SDean Michael Berris #include "gtest/gtest.h"
15b082c360SDean Michael Berris
16b082c360SDean Michael Berris namespace llvm {
17b082c360SDean Michael Berris namespace xray {
18b082c360SDean Michael Berris namespace {
19b082c360SDean Michael Berris
20b082c360SDean Michael Berris using ::testing::SizeIs;
21b082c360SDean Michael Berris
TEST(FDRBlockVerifierTest,ValidBlocksV3)22b082c360SDean Michael Berris TEST(FDRBlockVerifierTest, ValidBlocksV3) {
23b082c360SDean Michael Berris auto Block0 = LogBuilder()
24b082c360SDean Michael Berris .add<BufferExtents>(80)
25b082c360SDean Michael Berris .add<NewBufferRecord>(1)
26b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
27b082c360SDean Michael Berris .add<PIDRecord>(1)
28d2c50408SDean Michael Berris .add<NewCPUIDRecord>(1, 2)
29b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
30b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
31b082c360SDean Michael Berris .consume();
32b082c360SDean Michael Berris auto Block1 = LogBuilder()
33b082c360SDean Michael Berris .add<BufferExtents>(80)
34b082c360SDean Michael Berris .add<NewBufferRecord>(1)
35b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
36b082c360SDean Michael Berris .add<PIDRecord>(1)
37d2c50408SDean Michael Berris .add<NewCPUIDRecord>(1, 2)
38b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
39b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
40b082c360SDean Michael Berris .consume();
41b082c360SDean Michael Berris auto Block2 = LogBuilder()
42b082c360SDean Michael Berris .add<BufferExtents>(80)
43b082c360SDean Michael Berris .add<NewBufferRecord>(2)
44b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
45b082c360SDean Michael Berris .add<PIDRecord>(1)
46d2c50408SDean Michael Berris .add<NewCPUIDRecord>(2, 2)
47b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
48b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
49b082c360SDean Michael Berris .consume();
50b082c360SDean Michael Berris BlockIndexer::Index Index;
51b082c360SDean Michael Berris BlockIndexer Indexer(Index);
52b082c360SDean Michael Berris for (auto B : {std::ref(Block0), std::ref(Block1), std::ref(Block2)}) {
53b082c360SDean Michael Berris for (auto &R : B.get())
54b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(R->apply(Indexer)));
55b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(Indexer.flush()));
56b082c360SDean Michael Berris }
57b082c360SDean Michael Berris
58b082c360SDean Michael Berris BlockVerifier Verifier;
59b082c360SDean Michael Berris for (auto &ProcessThreadBlocks : Index) {
60b082c360SDean Michael Berris auto &Blocks = ProcessThreadBlocks.second;
61b082c360SDean Michael Berris for (auto &B : Blocks) {
62b082c360SDean Michael Berris for (auto *R : B.Records)
63b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(R->apply(Verifier)));
64b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(Verifier.verify()));
65b082c360SDean Michael Berris Verifier.reset();
66b082c360SDean Michael Berris }
67b082c360SDean Michael Berris }
68b082c360SDean Michael Berris }
69b082c360SDean Michael Berris
TEST(FDRBlockVerifierTest,MissingPIDRecord)70b082c360SDean Michael Berris TEST(FDRBlockVerifierTest, MissingPIDRecord) {
71b082c360SDean Michael Berris auto Block = LogBuilder()
72b082c360SDean Michael Berris .add<BufferExtents>(20)
73b082c360SDean Michael Berris .add<NewBufferRecord>(1)
74b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
75d2c50408SDean Michael Berris .add<NewCPUIDRecord>(1, 2)
76b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
77b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
78b082c360SDean Michael Berris .consume();
79b082c360SDean Michael Berris BlockVerifier Verifier;
80b082c360SDean Michael Berris for (auto &R : Block)
81b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(R->apply(Verifier)));
82b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(Verifier.verify()));
83b082c360SDean Michael Berris }
84b082c360SDean Michael Berris
TEST(FDRBlockVerifierTest,MissingBufferExtents)85b082c360SDean Michael Berris TEST(FDRBlockVerifierTest, MissingBufferExtents) {
86b082c360SDean Michael Berris auto Block = LogBuilder()
87b082c360SDean Michael Berris .add<NewBufferRecord>(1)
88b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
89d2c50408SDean Michael Berris .add<NewCPUIDRecord>(1, 2)
90b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
91b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
92b082c360SDean Michael Berris .consume();
93b082c360SDean Michael Berris BlockVerifier Verifier;
94b082c360SDean Michael Berris for (auto &R : Block)
95b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(R->apply(Verifier)));
96b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(Verifier.verify()));
97b082c360SDean Michael Berris }
98b082c360SDean Michael Berris
TEST(FDRBlockVerifierTest,IgnoreRecordsAfterEOB)99b082c360SDean Michael Berris TEST(FDRBlockVerifierTest, IgnoreRecordsAfterEOB) {
100b082c360SDean Michael Berris auto Block = LogBuilder()
101b082c360SDean Michael Berris .add<NewBufferRecord>(1)
102b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
103d2c50408SDean Michael Berris .add<NewCPUIDRecord>(1, 2)
104b082c360SDean Michael Berris .add<EndBufferRecord>()
105b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
106b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
107b082c360SDean Michael Berris .consume();
108b082c360SDean Michael Berris BlockVerifier Verifier;
109b082c360SDean Michael Berris for (auto &R : Block)
110b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(R->apply(Verifier)));
111b082c360SDean Michael Berris ASSERT_FALSE(errorToBool(Verifier.verify()));
112b082c360SDean Michael Berris }
113b082c360SDean Michael Berris
TEST(FDRBlockVerifierTest,MalformedV2)114b082c360SDean Michael Berris TEST(FDRBlockVerifierTest, MalformedV2) {
115b082c360SDean Michael Berris auto Block = LogBuilder()
116b082c360SDean Michael Berris .add<NewBufferRecord>(1)
117b082c360SDean Michael Berris .add<WallclockRecord>(1, 2)
118d2c50408SDean Michael Berris .add<NewCPUIDRecord>(1, 2)
119b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
120b082c360SDean Michael Berris .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
121b082c360SDean Michael Berris .add<NewBufferRecord>(2)
122b082c360SDean Michael Berris .consume();
123b082c360SDean Michael Berris BlockVerifier Verifier;
124b082c360SDean Michael Berris
125b082c360SDean Michael Berris ASSERT_THAT(Block, SizeIs(6u));
126b082c360SDean Michael Berris EXPECT_THAT_ERROR(Block[0]->apply(Verifier), Succeeded());
127b082c360SDean Michael Berris EXPECT_THAT_ERROR(Block[1]->apply(Verifier), Succeeded());
128b082c360SDean Michael Berris EXPECT_THAT_ERROR(Block[2]->apply(Verifier), Succeeded());
129b082c360SDean Michael Berris EXPECT_THAT_ERROR(Block[3]->apply(Verifier), Succeeded());
130b082c360SDean Michael Berris EXPECT_THAT_ERROR(Block[4]->apply(Verifier), Succeeded());
131b082c360SDean Michael Berris EXPECT_THAT_ERROR(Block[5]->apply(Verifier), Failed());
132b082c360SDean Michael Berris }
133b082c360SDean Michael Berris
134b082c360SDean Michael Berris } // namespace
135b082c360SDean Michael Berris } // namespace xray
136b082c360SDean Michael Berris } // namespace llvm
137