xref: /aosp_15_r20/external/swiftshader/third_party/subzero/unittest/IceELFSectionTest.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker //===- unittest/IceELFSectionTest.cpp - ELF Section unit tests ------------===//
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker //                        The Subzero Code Generator
4*03ce13f7SAndroid Build Coastguard Worker //
5*03ce13f7SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*03ce13f7SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*03ce13f7SAndroid Build Coastguard Worker //
8*03ce13f7SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*03ce13f7SAndroid Build Coastguard Worker 
10*03ce13f7SAndroid Build Coastguard Worker #include <algorithm>
11*03ce13f7SAndroid Build Coastguard Worker 
12*03ce13f7SAndroid Build Coastguard Worker #include "gtest/gtest.h"
13*03ce13f7SAndroid Build Coastguard Worker 
14*03ce13f7SAndroid Build Coastguard Worker #include "IceDefs.h"
15*03ce13f7SAndroid Build Coastguard Worker #include "IceELFSection.h"
16*03ce13f7SAndroid Build Coastguard Worker 
17*03ce13f7SAndroid Build Coastguard Worker #include "llvm/Support/raw_os_ostream.h"
18*03ce13f7SAndroid Build Coastguard Worker 
19*03ce13f7SAndroid Build Coastguard Worker namespace Ice {
20*03ce13f7SAndroid Build Coastguard Worker namespace {
21*03ce13f7SAndroid Build Coastguard Worker 
22*03ce13f7SAndroid Build Coastguard Worker // Test string table layout for various permutations. Test that pop,
23*03ce13f7SAndroid Build Coastguard Worker // lollipop, and lipop are able to share data, while the other strings do not.
CheckStringTablePermLayout(const ELFStringTableSection & Strtab)24*03ce13f7SAndroid Build Coastguard Worker void CheckStringTablePermLayout(const ELFStringTableSection &Strtab) {
25*03ce13f7SAndroid Build Coastguard Worker   size_t pop_index = Strtab.getIndex("pop");
26*03ce13f7SAndroid Build Coastguard Worker   size_t pop_size = std::string("pop").size();
27*03ce13f7SAndroid Build Coastguard Worker   size_t lollipop_index = Strtab.getIndex("lollipop");
28*03ce13f7SAndroid Build Coastguard Worker   size_t lollipop_size = std::string("lollipop").size();
29*03ce13f7SAndroid Build Coastguard Worker   size_t lipop_index = Strtab.getIndex("lipop");
30*03ce13f7SAndroid Build Coastguard Worker   size_t lipop_size = std::string("lipop").size();
31*03ce13f7SAndroid Build Coastguard Worker   size_t pops_index = Strtab.getIndex("pops");
32*03ce13f7SAndroid Build Coastguard Worker   size_t pops_size = std::string("pops").size();
33*03ce13f7SAndroid Build Coastguard Worker   size_t unpop_index = Strtab.getIndex("unpop");
34*03ce13f7SAndroid Build Coastguard Worker   size_t unpop_size = std::string("unpop").size();
35*03ce13f7SAndroid Build Coastguard Worker   size_t popular_index = Strtab.getIndex("popular");
36*03ce13f7SAndroid Build Coastguard Worker   size_t popular_size = std::string("popular").size();
37*03ce13f7SAndroid Build Coastguard Worker   size_t strtab_index = Strtab.getIndex(".strtab");
38*03ce13f7SAndroid Build Coastguard Worker   size_t strtab_size = std::string(".strtab").size();
39*03ce13f7SAndroid Build Coastguard Worker   size_t shstrtab_index = Strtab.getIndex(".shstrtab");
40*03ce13f7SAndroid Build Coastguard Worker   size_t shstrtab_size = std::string(".shstrtab").size();
41*03ce13f7SAndroid Build Coastguard Worker   size_t symtab_index = Strtab.getIndex(".symtab");
42*03ce13f7SAndroid Build Coastguard Worker   size_t symtab_size = std::string(".symtab").size();
43*03ce13f7SAndroid Build Coastguard Worker 
44*03ce13f7SAndroid Build Coastguard Worker   // Check that some sharing exists.
45*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(pop_index, lollipop_index + (lollipop_size - pop_size));
46*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(lipop_index, lollipop_index + (lollipop_size - lipop_size));
47*03ce13f7SAndroid Build Coastguard Worker 
48*03ce13f7SAndroid Build Coastguard Worker   // Check that .strtab does not share with .shstrtab (the dot throws it off).
49*03ce13f7SAndroid Build Coastguard Worker   EXPECT_NE(strtab_index, shstrtab_index + (shstrtab_size - strtab_size));
50*03ce13f7SAndroid Build Coastguard Worker 
51*03ce13f7SAndroid Build Coastguard Worker   // Check contents make sense.
52*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(Strtab.getSectionData().slice(pop_index, pop_index + pop_size),
53*03ce13f7SAndroid Build Coastguard Worker             llvm::StringRef("pop"));
54*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(Strtab.getSectionData().slice(lollipop_index,
55*03ce13f7SAndroid Build Coastguard Worker                                           lollipop_index + lollipop_size),
56*03ce13f7SAndroid Build Coastguard Worker             llvm::StringRef("lollipop"));
57*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(Strtab.getSectionData().slice(pops_index, pops_index + pops_size),
58*03ce13f7SAndroid Build Coastguard Worker             llvm::StringRef("pops"));
59*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(
60*03ce13f7SAndroid Build Coastguard Worker       Strtab.getSectionData().slice(unpop_index, unpop_index + unpop_size),
61*03ce13f7SAndroid Build Coastguard Worker       llvm::StringRef("unpop"));
62*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(Strtab.getSectionData().slice(popular_index,
63*03ce13f7SAndroid Build Coastguard Worker                                           popular_index + popular_size),
64*03ce13f7SAndroid Build Coastguard Worker             llvm::StringRef("popular"));
65*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(
66*03ce13f7SAndroid Build Coastguard Worker       Strtab.getSectionData().slice(strtab_index, strtab_index + strtab_size),
67*03ce13f7SAndroid Build Coastguard Worker       llvm::StringRef(".strtab"));
68*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(Strtab.getSectionData().slice(shstrtab_index,
69*03ce13f7SAndroid Build Coastguard Worker                                           shstrtab_index + shstrtab_size),
70*03ce13f7SAndroid Build Coastguard Worker             llvm::StringRef(".shstrtab"));
71*03ce13f7SAndroid Build Coastguard Worker   EXPECT_EQ(
72*03ce13f7SAndroid Build Coastguard Worker       Strtab.getSectionData().slice(symtab_index, symtab_index + symtab_size),
73*03ce13f7SAndroid Build Coastguard Worker       llvm::StringRef(".symtab"));
74*03ce13f7SAndroid Build Coastguard Worker }
75*03ce13f7SAndroid Build Coastguard Worker 
76*03ce13f7SAndroid Build Coastguard Worker // Test that the order in which strings are added doesn't matter.
TEST(IceELFSectionTest,StringTableBuilderPermSeveral)77*03ce13f7SAndroid Build Coastguard Worker TEST(IceELFSectionTest, StringTableBuilderPermSeveral) {
78*03ce13f7SAndroid Build Coastguard Worker   std::vector<std::string> Strings;
79*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("pop");
80*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("lollipop");
81*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("lipop");
82*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("pops");
83*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("unpop");
84*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("popular");
85*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("a");
86*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("z");
87*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("foo");
88*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("bar");
89*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back(".text");
90*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back(".symtab");
91*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back(".strtab");
92*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back(".shstrtab");
93*03ce13f7SAndroid Build Coastguard Worker   Strings.push_back("_start");
94*03ce13f7SAndroid Build Coastguard Worker   const SizeT NumTests = 128;
95*03ce13f7SAndroid Build Coastguard Worker   const uint64_t RandomSeed = 12345; // arbitrary value for now
96*03ce13f7SAndroid Build Coastguard Worker   RandomNumberGenerator R(RandomSeed);
97*03ce13f7SAndroid Build Coastguard Worker   RandomNumberGeneratorWrapper RNG(R);
98*03ce13f7SAndroid Build Coastguard Worker   for (SizeT i = 0; i < NumTests; ++i) {
99*03ce13f7SAndroid Build Coastguard Worker     auto Str = std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
100*03ce13f7SAndroid Build Coastguard Worker     RandomShuffle(Strings.begin(), Strings.end(), RNG);
101*03ce13f7SAndroid Build Coastguard Worker     ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0);
102*03ce13f7SAndroid Build Coastguard Worker     for (auto &S : Strings) {
103*03ce13f7SAndroid Build Coastguard Worker       Strtab.add(S);
104*03ce13f7SAndroid Build Coastguard Worker     }
105*03ce13f7SAndroid Build Coastguard Worker     Strtab.doLayout();
106*03ce13f7SAndroid Build Coastguard Worker     CheckStringTablePermLayout(Strtab);
107*03ce13f7SAndroid Build Coastguard Worker   }
108*03ce13f7SAndroid Build Coastguard Worker }
109*03ce13f7SAndroid Build Coastguard Worker 
110*03ce13f7SAndroid Build Coastguard Worker // Test that adding duplicate strings is fine.
TEST(IceELFSectionTest,StringTableBuilderDuplicates)111*03ce13f7SAndroid Build Coastguard Worker TEST(IceELFSectionTest, StringTableBuilderDuplicates) {
112*03ce13f7SAndroid Build Coastguard Worker   auto Str = std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
113*03ce13f7SAndroid Build Coastguard Worker   ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0);
114*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("unpop");
115*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("pop");
116*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("lollipop");
117*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("a");
118*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("popular");
119*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("pops");
120*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("lipop");
121*03ce13f7SAndroid Build Coastguard Worker   Strtab.add(".strtab");
122*03ce13f7SAndroid Build Coastguard Worker   Strtab.add(".shstrtab");
123*03ce13f7SAndroid Build Coastguard Worker   Strtab.add(".symtab");
124*03ce13f7SAndroid Build Coastguard Worker 
125*03ce13f7SAndroid Build Coastguard Worker   Strtab.add(".symtab");
126*03ce13f7SAndroid Build Coastguard Worker   Strtab.add(".shstrtab");
127*03ce13f7SAndroid Build Coastguard Worker   Strtab.add(".strtab");
128*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("lipop");
129*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("pops");
130*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("popular");
131*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("a");
132*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("lollipop");
133*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("pop");
134*03ce13f7SAndroid Build Coastguard Worker   Strtab.add("unpop");
135*03ce13f7SAndroid Build Coastguard Worker 
136*03ce13f7SAndroid Build Coastguard Worker   Strtab.doLayout();
137*03ce13f7SAndroid Build Coastguard Worker   CheckStringTablePermLayout(Strtab);
138*03ce13f7SAndroid Build Coastguard Worker }
139*03ce13f7SAndroid Build Coastguard Worker 
140*03ce13f7SAndroid Build Coastguard Worker } // end of anonymous namespace
141*03ce13f7SAndroid Build Coastguard Worker } // end of namespace Ice
142