bloaty_test.cc
Go to the documentation of this file.
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "test.h"
16 
17 TEST_F(BloatyTest, EmptyObjectFile) {
18  std::string file = "01-empty.o";
19  uint64_t size;
21 
22  // Empty .c file should result in a .o file with no vmsize.
23  RunBloaty({"bloaty", file});
24  EXPECT_EQ(top_row_->vmsize, 0);
25  EXPECT_EQ(top_row_->filesize, size);
26  EXPECT_GT(top_row_->sorted_children.size(), 1);
27 
28  // Same with segments (we fake segments on .o files).
29  RunBloaty({"bloaty", "-d", "segments", file});
30  EXPECT_EQ(top_row_->vmsize, 0);
31  EXPECT_EQ(top_row_->filesize, size);
32  EXPECT_GT(top_row_->sorted_children.size(), 1);
33 
34  // Same with symbols.
35  RunBloaty({"bloaty", "-d", "symbols", file});
36  EXPECT_EQ(top_row_->vmsize, 0);
37  EXPECT_EQ(top_row_->filesize, size);
38  EXPECT_GT(top_row_->sorted_children.size(), 1);
39 
40  // We can't run any of these targets against object files.
41  std::string errmsg = "can't use data source";
42  AssertBloatyFails({"bloaty", "-d", "compileunits", file}, errmsg);
43  AssertBloatyFails({"bloaty", "-d", "inlines", file}, errmsg);
44 }
45 
46 TEST_F(BloatyTest, SimpleObjectFile) {
47  std::string file = "02-simple.o";
48  uint64_t size;
50 
51  // Test "-n 0" which should return an unlimited number of rows.
52  RunBloaty({"bloaty", "-n", "0", file});
53  EXPECT_GT(top_row_->vmsize, 64);
54  EXPECT_LT(top_row_->vmsize, 300);
55  EXPECT_EQ(top_row_->filesize, size);
56  EXPECT_GT(top_row_->sorted_children.size(), 1);
57 
58  // Same with segments (we fake segments on .o files).
59  RunBloaty({"bloaty", "-d", "segments", file});
60  EXPECT_GT(top_row_->vmsize, 64);
61  EXPECT_LT(top_row_->vmsize, 300);
62  EXPECT_EQ(top_row_->filesize, size);
63  EXPECT_GT(top_row_->sorted_children.size(), 1);
64 
65  // For inputfiles we should get everything attributed to the input file.
66  RunBloaty({"bloaty", "-d", "inputfiles", file});
67  AssertChildren(*top_row_, {
68  std::make_tuple("02-simple.o", kUnknown, kUnknown)
69  });
70 
71  // For symbols we should get entries for all our expected symbols.
72  RunBloaty({"bloaty", "-d", "symbols", "-n", "40", "-s", "vm", file});
73  AssertChildren(*top_row_, {
74  std::make_tuple("func1", kUnknown, kSameAsVM),
75  std::make_tuple("func2", kUnknown, kSameAsVM),
76  std::make_tuple("bss_a", 8, 0),
77  std::make_tuple("data_a", 8, 8),
78  std::make_tuple("rodata_a", 8, 8),
79  std::make_tuple("bss_b", 4, 0),
80  std::make_tuple("data_b", 4, 4),
81  std::make_tuple("rodata_b", 4, 4),
82  });
83 
84  RunBloaty({"bloaty", "-d", "sections,symbols", "-n", "50", file});
85 
86  auto row = FindRow(".bss");
87  ASSERT_TRUE(row != nullptr);
88  AssertChildren(*row, {
89  std::make_tuple("bss_a", 8, 0),
90  std::make_tuple("bss_b", 4, 0),
91  });
92 
93  row = FindRow(".data");
94  ASSERT_TRUE(row != nullptr);
95  AssertChildren(*row, {
96  std::make_tuple("data_a", 8, 8),
97  std::make_tuple("data_b", 4, 4),
98  });
99 
100  row = FindRow(".rodata");
101  ASSERT_TRUE(row != nullptr);
102  AssertChildren(*row, {
103  std::make_tuple("rodata_a", 8, 8),
104  std::make_tuple("rodata_b", 4, 4),
105  });
106 }
107 
108 TEST_F(BloatyTest, SimpleArchiveFile) {
109  std::string file = "03-simple.a";
110  uint64_t size;
112 
113  RunBloaty({"bloaty", file});
114  EXPECT_GT(top_row_->vmsize, 8000);
115  EXPECT_LT(top_row_->vmsize, 12000);
116  //EXPECT_EQ(top_row_->filesize, size);
117  EXPECT_GT(top_row_->sorted_children.size(), 3);
118 
119  RunBloaty({"bloaty", "-d", "segments", file});
120  EXPECT_GT(top_row_->vmsize, 8000);
121  EXPECT_LT(top_row_->vmsize, 12000);
122  //EXPECT_EQ(top_row_->filesize, size);
123 
124  RunBloaty({"bloaty", "-d", "symbols", "-n", "40", "-s", "vm", file});
125  AssertChildren(*top_row_, {
126  std::make_tuple("bar_x", 4000, 4000),
127  std::make_tuple("foo_x", 4000, 0),
128  std::make_tuple("bar_func", kUnknown, kSameAsVM),
129  std::make_tuple("foo_func", kUnknown, kSameAsVM),
130  std::make_tuple("long_filename_x", 12, 12),
131  std::make_tuple("bar_y", 4, 4),
132  std::make_tuple("bar_z", 4, 0),
133  std::make_tuple("foo_y", 4, 0),
134  std::make_tuple("long_filename_y", 4, 4),
135  });
136 
137  RunBloaty({"bloaty", "-d", "armembers,symbols", file});
138  AssertChildren(*top_row_,
139  {
140  std::make_tuple("bar.o", kUnknown, kUnknown),
141  std::make_tuple("foo.o", kUnknown, kUnknown),
142  std::make_tuple("a_filename_longer_than_sixteen_chars.o",
143  kUnknown, kUnknown),
144  });
145 
146  auto row = FindRow("bar.o");
147  ASSERT_TRUE(row != nullptr);
148  AssertChildren(*row, {
149  std::make_tuple("bar_x", 4000, 4000),
150  std::make_tuple("bar_func", kUnknown, kSameAsVM),
151  std::make_tuple("bar_y", 4, 4),
152  std::make_tuple("bar_z", 4, 0),
153  });
154 
155  row = FindRow("foo.o");
156  ASSERT_TRUE(row != nullptr);
157  AssertChildren(*row, {
158  std::make_tuple("foo_x", 4000, 0),
159  std::make_tuple("foo_func", kUnknown, kSameAsVM),
160  std::make_tuple("foo_y", 4, 0),
161  });
162 
163  row = FindRow("a_filename_longer_than_sixteen_chars.o");
164  ASSERT_TRUE(row != nullptr);
165  AssertChildren(*row, {
166  std::make_tuple("long_filename_x", 12, 12),
167  std::make_tuple("long_filename_y", 4, 4),
168  });
169 }
170 
171 TEST_F(BloatyTest, SimpleSharedObjectFile) {
172  std::string file = "04-simple.so";
173  uint64_t size;
175 
176  RunBloaty({"bloaty", file});
177  EXPECT_GT(top_row_->vmsize, 8000);
178  EXPECT_LT(top_row_->vmsize, 12000);
179  EXPECT_EQ(top_row_->filesize, size);
180  EXPECT_GT(top_row_->sorted_children.size(), 3);
181 
182  RunBloaty({"bloaty", "-d", "segments", file});
183  EXPECT_GT(top_row_->vmsize, 8000);
184  EXPECT_LT(top_row_->vmsize, 12000);
185  EXPECT_EQ(top_row_->filesize, size);
186 
187  RunBloaty({"bloaty", "-d", "symbols", "-n", "50", file});
188  AssertChildren(*top_row_, {
189  std::make_tuple("bar_x", 4000, 4000),
190  std::make_tuple("foo_x", 4000, kUnknown),
191  std::make_tuple("bar_func", kUnknown, kSameAsVM),
192  std::make_tuple("foo_func", kUnknown, kSameAsVM),
193  });
194 }
195 
196 TEST_F(BloatyTest, SimpleBinary) {
197  std::string file = "05-binary.bin";
198  uint64_t size;
200 
201  RunBloaty({"bloaty", file});
202  EXPECT_GT(top_row_->vmsize, 8000);
203  EXPECT_LT(top_row_->vmsize, 12000);
204  EXPECT_EQ(top_row_->filesize, size);
205  EXPECT_GT(top_row_->sorted_children.size(), 3);
206 
207  RunBloaty({"bloaty", "-d", "segments", file});
208  EXPECT_GT(top_row_->vmsize, 8000);
209  EXPECT_LT(top_row_->vmsize, 12000);
210  EXPECT_EQ(top_row_->filesize, size);
211 
212  RunBloaty({"bloaty", "-d", "symbols", "-n", "50", "-s", "vm", file});
213  AssertChildren(*top_row_, {
214  std::make_tuple("bar_x", 4000, 4000),
215  std::make_tuple("foo_x", 4000, 0),
216  std::make_tuple("bar_func", kUnknown, kSameAsVM),
217  std::make_tuple("foo_func", kUnknown, kSameAsVM),
218  std::make_tuple("main", kUnknown, kSameAsVM),
219  std::make_tuple("bar_y", 4, 4),
220  std::make_tuple("bar_z", 4, 0),
221  std::make_tuple("foo_y", 4, 0)
222  });
223 
224  RunBloaty({"bloaty", "-d", "compileunits,symbols", file});
225  auto row = FindRow("bar.o.c");
226  ASSERT_TRUE(row != nullptr);
227 
228  // This only includes functions (not data) for now.
229  AssertChildren(*row, {
230  std::make_tuple("bar_x", 4000, kSameAsVM),
231  std::make_tuple("bar_func", kUnknown, kSameAsVM),
232  std::make_tuple("bar_y", kUnknown, kSameAsVM),
233  std::make_tuple("bar_z", kUnknown, kSameAsVM),
234  });
235 
236  row = FindRow("foo.o.c");
237  ASSERT_TRUE(row != nullptr);
238 
239  // This only includes functions (not data) for now.
240  AssertChildren(*row, {
241  std::make_tuple("foo_x", 4000, 0),
242  std::make_tuple("foo_func", kUnknown, kSameAsVM),
243  std::make_tuple("foo_y", kUnknown, kSameAsVM),
244  });
245 
246  RunBloaty({"bloaty", "-d", "sections,inlines", file});
247 }
248 
249 TEST_F(BloatyTest, InputFiles) {
250  std::string file1 = "05-binary.bin";
251  std::string file2 = "07-binary-stripped.bin";
252  uint64_t size1, size2;
253  ASSERT_TRUE(GetFileSize(file1, &size1));
254  ASSERT_TRUE(GetFileSize(file2, &size2));
255  RunBloaty({"bloaty", file1, file2, "-d", "inputfiles"});
256  AssertChildren(*top_row_,
257  {std::make_tuple(file1, kUnknown, static_cast<int>(size1)),
258  std::make_tuple(file2, kUnknown, static_cast<int>(size2))});
259 
260  // Should work with custom data sources.
261  bloaty::Options options;
263  filename: "05-binary.bin"
264  filename: "07-binary-stripped.bin"
265  custom_data_source {
266  name: "rewritten_inputfiles"
267  base_data_source: "inputfiles"
268  rewrite: {
269  pattern: "binary"
270  replacement: "binary"
271  }
272  }
273  data_source: "rewritten_inputfiles"
274  )", &options);
275 
276  RunBloatyWithOptions(options, bloaty::OutputOptions());
277  AssertChildren(*top_row_, {std::make_tuple("binary", kUnknown,
278  static_cast<int>(size1 + size2))});
279 }
280 
281 TEST_F(BloatyTest, DiffMode) {
282  RunBloaty({"bloaty", "06-diff.a", "--", "03-simple.a", "-d", "symbols"});
283  AssertChildren(*top_row_, {
284  std::make_tuple("foo_func", kUnknown, kSameAsVM),
285  std::make_tuple("foo_y", 4, 0)
286  });
287 }
288 
289 TEST_F(BloatyTest, SeparateDebug) {
290  RunBloaty({"bloaty", "--debug-file=05-binary.bin", "07-binary-stripped.bin",
291  "-d", "symbols"});
292 }
bloaty::RunBloaty
void RunBloaty(const InputFileFactory &factory, const std::string &data_source)
Definition: fuzz_target.cc:44
std::tr1::make_tuple
tuple make_tuple()
Definition: cares/cares/test/gmock-1.8.0/gtest/gtest.h:1619
options
double_dict options[]
Definition: capstone_test.c:55
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2036
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
bloaty::OutputOptions
Definition: bloaty.h:370
BloatyTest
Definition: bloaty/tests/test.h:72
google::protobuf::TextFormat::ParseFromString
static bool ParseFromString(const std::string &input, Message *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1485
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
GetFileSize
bool GetFileSize(const std::string &filename, uint64_t *size)
Definition: bloaty/tests/test.h:38
TEST_F
TEST_F(BloatyTest, EmptyObjectFile)
Definition: bloaty_test.cc:17
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
test.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48