arena_unittest.cc 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <google/protobuf/arena.h>
  31. #include <algorithm>
  32. #include <cstring>
  33. #include <memory>
  34. #include <string>
  35. #include <type_traits>
  36. #include <typeinfo>
  37. #include <vector>
  38. #include <google/protobuf/stubs/logging.h>
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/arena_test_util.h>
  41. #include <google/protobuf/test_util.h>
  42. #include <google/protobuf/unittest.pb.h>
  43. #include <google/protobuf/unittest_arena.pb.h>
  44. #include <google/protobuf/unittest_no_arena.pb.h>
  45. #include <google/protobuf/io/coded_stream.h>
  46. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  47. #include <google/protobuf/descriptor.h>
  48. #include <google/protobuf/extension_set.h>
  49. #include <google/protobuf/message.h>
  50. #include <google/protobuf/message_lite.h>
  51. #include <google/protobuf/repeated_field.h>
  52. #include <google/protobuf/wire_format_lite.h>
  53. #include <google/protobuf/unknown_field_set.h>
  54. #include <gtest/gtest.h>
  55. namespace google {
  56. using proto2_arena_unittest::ArenaMessage;
  57. using protobuf_unittest::TestAllTypes;
  58. using protobuf_unittest::TestAllExtensions;
  59. using protobuf_unittest::TestOneof2;
  60. using protobuf_unittest::TestEmptyMessage;
  61. namespace protobuf {
  62. class Notifier {
  63. public:
  64. Notifier() : count_(0) {}
  65. void Notify() {
  66. count_++;
  67. }
  68. int GetCount() {
  69. return count_;
  70. }
  71. private:
  72. int count_;
  73. };
  74. class SimpleDataType {
  75. public:
  76. SimpleDataType() : notifier_(NULL) {}
  77. void SetNotifier(Notifier* notifier) {
  78. notifier_ = notifier;
  79. }
  80. virtual ~SimpleDataType() {
  81. if (notifier_ != NULL) {
  82. notifier_->Notify();
  83. }
  84. };
  85. private:
  86. Notifier* notifier_;
  87. };
  88. // A simple class that does not allow copying and so cannot be used as a
  89. // parameter type without "const &".
  90. class PleaseDontCopyMe {
  91. public:
  92. explicit PleaseDontCopyMe(int value) : value_(value) {}
  93. int value() const { return value_; }
  94. private:
  95. int value_;
  96. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PleaseDontCopyMe);
  97. };
  98. // A class that takes four different types as constructor arguments.
  99. class MustBeConstructedWithOneThroughFour {
  100. public:
  101. MustBeConstructedWithOneThroughFour(
  102. int one, const char* two, const string& three,
  103. const PleaseDontCopyMe* four)
  104. : one_(one), two_(two), three_(three), four_(four) {}
  105. int one_;
  106. const char* const two_;
  107. string three_;
  108. const PleaseDontCopyMe* four_;
  109. private:
  110. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MustBeConstructedWithOneThroughFour);
  111. };
  112. // A class that takes eight different types as constructor arguments.
  113. class MustBeConstructedWithOneThroughEight {
  114. public:
  115. MustBeConstructedWithOneThroughEight(
  116. int one, const char* two, const string& three,
  117. const PleaseDontCopyMe* four, int five, const char* six,
  118. const string& seven, const string& eight)
  119. : one_(one), two_(two), three_(three), four_(four), five_(five),
  120. six_(six), seven_(seven), eight_(eight) {}
  121. int one_;
  122. const char* const two_;
  123. string three_;
  124. const PleaseDontCopyMe* four_;
  125. int five_;
  126. const char* const six_;
  127. string seven_;
  128. string eight_;
  129. private:
  130. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MustBeConstructedWithOneThroughEight);
  131. };
  132. TEST(ArenaTest, ArenaConstructable) {
  133. EXPECT_TRUE(Arena::is_arena_constructable<TestAllTypes>::type::value);
  134. EXPECT_TRUE(Arena::is_arena_constructable<const TestAllTypes>::type::value);
  135. EXPECT_FALSE(Arena::is_arena_constructable<
  136. protobuf_unittest_no_arena::TestNoArenaMessage>::type::value);
  137. EXPECT_FALSE(Arena::is_arena_constructable<Arena>::type::value);
  138. }
  139. TEST(ArenaTest, DestructorSkippable) {
  140. EXPECT_TRUE(Arena::is_destructor_skippable<TestAllTypes>::type::value);
  141. EXPECT_TRUE(Arena::is_destructor_skippable<const TestAllTypes>::type::value);
  142. EXPECT_FALSE(Arena::is_destructor_skippable<
  143. protobuf_unittest_no_arena::TestNoArenaMessage>::type::value);
  144. EXPECT_FALSE(Arena::is_destructor_skippable<Arena>::type::value);
  145. }
  146. TEST(ArenaTest, BasicCreate) {
  147. Arena arena;
  148. EXPECT_TRUE(Arena::Create<int32>(&arena) != NULL);
  149. EXPECT_TRUE(Arena::Create<int64>(&arena) != NULL);
  150. EXPECT_TRUE(Arena::Create<float>(&arena) != NULL);
  151. EXPECT_TRUE(Arena::Create<double>(&arena) != NULL);
  152. EXPECT_TRUE(Arena::Create<string>(&arena) != NULL);
  153. arena.Own(new int32);
  154. arena.Own(new int64);
  155. arena.Own(new float);
  156. arena.Own(new double);
  157. arena.Own(new string);
  158. arena.Own<int>(NULL);
  159. Notifier notifier;
  160. SimpleDataType* data = Arena::Create<SimpleDataType>(&arena);
  161. data->SetNotifier(&notifier);
  162. data = new SimpleDataType;
  163. data->SetNotifier(&notifier);
  164. arena.Own(data);
  165. arena.Reset();
  166. EXPECT_EQ(2, notifier.GetCount());
  167. }
  168. TEST(ArenaTest, CreateAndConstCopy) {
  169. Arena arena;
  170. const string s("foo");
  171. const string* s_copy = Arena::Create<string>(&arena, s);
  172. EXPECT_TRUE(s_copy != NULL);
  173. EXPECT_EQ("foo", s);
  174. EXPECT_EQ("foo", *s_copy);
  175. }
  176. TEST(ArenaTest, CreateAndNonConstCopy) {
  177. Arena arena;
  178. string s("foo");
  179. const string* s_copy = Arena::Create<string>(&arena, s);
  180. EXPECT_TRUE(s_copy != NULL);
  181. EXPECT_EQ("foo", s);
  182. EXPECT_EQ("foo", *s_copy);
  183. }
  184. TEST(ArenaTest, CreateAndMove) {
  185. Arena arena;
  186. string s("foo");
  187. const string* s_move = Arena::Create<string>(&arena, std::move(s));
  188. EXPECT_TRUE(s_move != NULL);
  189. EXPECT_TRUE(s.empty()); // NOLINT
  190. EXPECT_EQ("foo", *s_move);
  191. }
  192. TEST(ArenaTest, CreateWithFourConstructorArguments) {
  193. Arena arena;
  194. const string three("3");
  195. const PleaseDontCopyMe four(4);
  196. const MustBeConstructedWithOneThroughFour* new_object =
  197. Arena::Create<MustBeConstructedWithOneThroughFour>(
  198. &arena, 1, "2", three, &four);
  199. EXPECT_TRUE(new_object != NULL);
  200. ASSERT_EQ(1, new_object->one_);
  201. ASSERT_STREQ("2", new_object->two_);
  202. ASSERT_EQ("3", new_object->three_);
  203. ASSERT_EQ(4, new_object->four_->value());
  204. }
  205. TEST(ArenaTest, CreateWithEightConstructorArguments) {
  206. Arena arena;
  207. const string three("3");
  208. const PleaseDontCopyMe four(4);
  209. const string seven("7");
  210. const string eight("8");
  211. const MustBeConstructedWithOneThroughEight* new_object =
  212. Arena::Create<MustBeConstructedWithOneThroughEight>(
  213. &arena, 1, "2", three, &four, 5, "6", seven, eight);
  214. EXPECT_TRUE(new_object != NULL);
  215. ASSERT_EQ(1, new_object->one_);
  216. ASSERT_STREQ("2", new_object->two_);
  217. ASSERT_EQ("3", new_object->three_);
  218. ASSERT_EQ(4, new_object->four_->value());
  219. ASSERT_EQ(5, new_object->five_);
  220. ASSERT_STREQ("6", new_object->six_);
  221. ASSERT_EQ("7", new_object->seven_);
  222. ASSERT_EQ("8", new_object->eight_);
  223. }
  224. class PleaseMoveMe {
  225. public:
  226. explicit PleaseMoveMe(const string& value) : value_(value) {}
  227. PleaseMoveMe(PleaseMoveMe&&) = default;
  228. PleaseMoveMe(const PleaseMoveMe&) = delete;
  229. const string& value() const { return value_; }
  230. private:
  231. string value_;
  232. };
  233. TEST(ArenaTest, CreateWithMoveArguments) {
  234. Arena arena;
  235. PleaseMoveMe one("1");
  236. const PleaseMoveMe* new_object =
  237. Arena::Create<PleaseMoveMe>(&arena, std::move(one));
  238. EXPECT_TRUE(new_object);
  239. ASSERT_EQ("1", new_object->value());
  240. }
  241. TEST(ArenaTest, InitialBlockTooSmall) {
  242. // Construct a small (64 byte) initial block of memory to be used by the
  243. // arena allocator; then, allocate an object which will not fit in the
  244. // initial block.
  245. std::vector<char> arena_block(96);
  246. ArenaOptions options;
  247. options.initial_block = &arena_block[0];
  248. options.initial_block_size = arena_block.size();
  249. Arena arena(options);
  250. char* p = ::google::protobuf::Arena::CreateArray<char>(&arena, 96);
  251. uintptr_t allocation = reinterpret_cast<uintptr_t>(p);
  252. // Ensure that the arena allocator did not return memory pointing into the
  253. // initial block of memory.
  254. uintptr_t arena_start = reinterpret_cast<uintptr_t>(&arena_block[0]);
  255. uintptr_t arena_end = arena_start + arena_block.size();
  256. EXPECT_FALSE(allocation >= arena_start && allocation < arena_end);
  257. // Write to the memory we allocated; this should (but is not guaranteed to)
  258. // trigger a check for heap corruption if the object was allocated from the
  259. // initially-provided block.
  260. memset(p, '\0', 96);
  261. }
  262. TEST(ArenaTest, Parsing) {
  263. TestAllTypes original;
  264. TestUtil::SetAllFields(&original);
  265. // Test memory leak.
  266. Arena arena;
  267. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  268. arena_message->ParseFromString(original.SerializeAsString());
  269. TestUtil::ExpectAllFieldsSet(*arena_message);
  270. // Test that string fields have nul terminator bytes (earlier bug).
  271. EXPECT_EQ(strlen(original.optional_string().c_str()),
  272. strlen(arena_message->optional_string().c_str()));
  273. }
  274. TEST(ArenaTest, UnknownFields) {
  275. TestAllTypes original;
  276. TestUtil::SetAllFields(&original);
  277. // Test basic parsing into (populating) and reading out of unknown fields on
  278. // an arena.
  279. Arena arena;
  280. TestEmptyMessage* arena_message =
  281. Arena::CreateMessage<TestEmptyMessage>(&arena);
  282. arena_message->ParseFromString(original.SerializeAsString());
  283. TestAllTypes copied;
  284. copied.ParseFromString(arena_message->SerializeAsString());
  285. TestUtil::ExpectAllFieldsSet(copied);
  286. // Exercise UFS manual manipulation (setters).
  287. arena_message = Arena::CreateMessage<TestEmptyMessage>(&arena);
  288. arena_message->mutable_unknown_fields()->AddVarint(
  289. TestAllTypes::kOptionalInt32FieldNumber, 42);
  290. copied.Clear();
  291. copied.ParseFromString(arena_message->SerializeAsString());
  292. EXPECT_TRUE(copied.has_optional_int32());
  293. EXPECT_EQ(42, copied.optional_int32());
  294. // Exercise UFS swap path.
  295. TestEmptyMessage* arena_message_2 =
  296. Arena::CreateMessage<TestEmptyMessage>(&arena);
  297. arena_message_2->Swap(arena_message);
  298. copied.Clear();
  299. copied.ParseFromString(arena_message_2->SerializeAsString());
  300. EXPECT_TRUE(copied.has_optional_int32());
  301. EXPECT_EQ(42, copied.optional_int32());
  302. // Test field manipulation.
  303. TestEmptyMessage* arena_message_3 =
  304. Arena::CreateMessage<TestEmptyMessage>(&arena);
  305. arena_message_3->mutable_unknown_fields()->AddVarint(1000, 42);
  306. arena_message_3->mutable_unknown_fields()->AddFixed32(1001, 42);
  307. arena_message_3->mutable_unknown_fields()->AddFixed64(1002, 42);
  308. arena_message_3->mutable_unknown_fields()->AddLengthDelimited(1003);
  309. arena_message_3->mutable_unknown_fields()->DeleteSubrange(0, 2);
  310. arena_message_3->mutable_unknown_fields()->DeleteByNumber(1002);
  311. arena_message_3->mutable_unknown_fields()->DeleteByNumber(1003);
  312. EXPECT_TRUE(arena_message_3->unknown_fields().empty());
  313. }
  314. TEST(ArenaTest, Swap) {
  315. Arena arena1;
  316. Arena arena2;
  317. TestAllTypes* arena1_message;
  318. TestAllTypes* arena2_message;
  319. // Case 1: Swap(), no UFS on either message, both messages on different
  320. // arenas. Arena pointers should remain the same after swap.
  321. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  322. arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  323. arena1_message->Swap(arena2_message);
  324. EXPECT_EQ(&arena1, arena1_message->GetArena());
  325. EXPECT_EQ(&arena2, arena2_message->GetArena());
  326. // Case 2: Swap(), UFS on one message, both messages on different arenas.
  327. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  328. arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  329. arena1_message->mutable_unknown_fields()->AddVarint(1, 42);
  330. arena1_message->Swap(arena2_message);
  331. EXPECT_EQ(&arena1, arena1_message->GetArena());
  332. EXPECT_EQ(&arena2, arena2_message->GetArena());
  333. EXPECT_EQ(0, arena1_message->unknown_fields().field_count());
  334. EXPECT_EQ(1, arena2_message->unknown_fields().field_count());
  335. EXPECT_EQ(42, arena2_message->unknown_fields().field(0).varint());
  336. // Case 3: Swap(), UFS on both messages, both messages on different arenas.
  337. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  338. arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  339. arena1_message->mutable_unknown_fields()->AddVarint(1, 42);
  340. arena2_message->mutable_unknown_fields()->AddVarint(2, 84);
  341. arena1_message->Swap(arena2_message);
  342. EXPECT_EQ(&arena1, arena1_message->GetArena());
  343. EXPECT_EQ(&arena2, arena2_message->GetArena());
  344. EXPECT_EQ(1, arena1_message->unknown_fields().field_count());
  345. EXPECT_EQ(1, arena2_message->unknown_fields().field_count());
  346. EXPECT_EQ(84, arena1_message->unknown_fields().field(0).varint());
  347. EXPECT_EQ(42, arena2_message->unknown_fields().field(0).varint());
  348. }
  349. TEST(ArenaTest, ReflectionSwapFields) {
  350. Arena arena1;
  351. Arena arena2;
  352. TestAllTypes* arena1_message;
  353. TestAllTypes* arena2_message;
  354. // Case 1: messages on different arenas, only one message is set.
  355. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  356. arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  357. TestUtil::SetAllFields(arena1_message);
  358. const Reflection* reflection = arena1_message->GetReflection();
  359. std::vector<const FieldDescriptor*> fields;
  360. reflection->ListFields(*arena1_message, &fields);
  361. reflection->SwapFields(arena1_message, arena2_message, fields);
  362. EXPECT_EQ(&arena1, arena1_message->GetArena());
  363. EXPECT_EQ(&arena2, arena2_message->GetArena());
  364. string output;
  365. arena1_message->SerializeToString(&output);
  366. EXPECT_EQ(0, output.size());
  367. TestUtil::ExpectAllFieldsSet(*arena2_message);
  368. reflection->SwapFields(arena1_message, arena2_message, fields);
  369. arena2_message->SerializeToString(&output);
  370. EXPECT_EQ(0, output.size());
  371. TestUtil::ExpectAllFieldsSet(*arena1_message);
  372. // Case 2: messages on different arenas, both messages are set.
  373. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  374. arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  375. TestUtil::SetAllFields(arena1_message);
  376. TestUtil::SetAllFields(arena2_message);
  377. reflection->SwapFields(arena1_message, arena2_message, fields);
  378. EXPECT_EQ(&arena1, arena1_message->GetArena());
  379. EXPECT_EQ(&arena2, arena2_message->GetArena());
  380. TestUtil::ExpectAllFieldsSet(*arena1_message);
  381. TestUtil::ExpectAllFieldsSet(*arena2_message);
  382. // Case 3: messages on different arenas with different lifetimes.
  383. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  384. {
  385. Arena arena3;
  386. TestAllTypes* arena3_message = Arena::CreateMessage<TestAllTypes>(&arena3);
  387. TestUtil::SetAllFields(arena3_message);
  388. reflection->SwapFields(arena1_message, arena3_message, fields);
  389. }
  390. TestUtil::ExpectAllFieldsSet(*arena1_message);
  391. // Case 4: one message on arena, the other on heap.
  392. arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  393. TestAllTypes message;
  394. TestUtil::SetAllFields(arena1_message);
  395. reflection->SwapFields(arena1_message, &message, fields);
  396. EXPECT_EQ(&arena1, arena1_message->GetArena());
  397. EXPECT_EQ(nullptr, message.GetArena());
  398. arena1_message->SerializeToString(&output);
  399. EXPECT_EQ(0, output.size());
  400. TestUtil::ExpectAllFieldsSet(message);
  401. }
  402. TEST(ArenaTest, SetAllocatedMessage) {
  403. Arena arena;
  404. TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  405. TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
  406. nested->set_bb(118);
  407. arena_message->set_allocated_optional_nested_message(nested);
  408. EXPECT_EQ(118, arena_message->optional_nested_message().bb());
  409. protobuf_unittest_no_arena::TestNoArenaMessage no_arena_message;
  410. EXPECT_FALSE(no_arena_message.has_arena_message());
  411. no_arena_message.set_allocated_arena_message(NULL);
  412. EXPECT_FALSE(no_arena_message.has_arena_message());
  413. no_arena_message.set_allocated_arena_message(new ArenaMessage);
  414. EXPECT_TRUE(no_arena_message.has_arena_message());
  415. }
  416. TEST(ArenaTest, ReleaseMessage) {
  417. Arena arena;
  418. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  419. arena_message->mutable_optional_nested_message()->set_bb(118);
  420. std::unique_ptr<TestAllTypes::NestedMessage> nested(
  421. arena_message->release_optional_nested_message());
  422. EXPECT_EQ(118, nested->bb());
  423. TestAllTypes::NestedMessage* released_null =
  424. arena_message->release_optional_nested_message();
  425. EXPECT_EQ(NULL, released_null);
  426. }
  427. TEST(ArenaTest, SetAllocatedString) {
  428. Arena arena;
  429. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  430. string* allocated_str = new string("hello");
  431. arena_message->set_allocated_optional_string(allocated_str);
  432. EXPECT_EQ("hello", arena_message->optional_string());
  433. }
  434. TEST(ArenaTest, ReleaseString) {
  435. Arena arena;
  436. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  437. arena_message->set_optional_string("hello");
  438. std::unique_ptr<string> released_str(
  439. arena_message->release_optional_string());
  440. EXPECT_EQ("hello", *released_str);
  441. // Test default value.
  442. }
  443. TEST(ArenaTest, SwapBetweenArenasWithAllFieldsSet) {
  444. Arena arena1;
  445. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  446. {
  447. Arena arena2;
  448. TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  449. TestUtil::SetAllFields(arena2_message);
  450. arena2_message->Swap(arena1_message);
  451. string output;
  452. arena2_message->SerializeToString(&output);
  453. EXPECT_EQ(0, output.size());
  454. }
  455. TestUtil::ExpectAllFieldsSet(*arena1_message);
  456. }
  457. TEST(ArenaTest, SwapBetweenArenaAndNonArenaWithAllFieldsSet) {
  458. TestAllTypes non_arena_message;
  459. TestUtil::SetAllFields(&non_arena_message);
  460. {
  461. Arena arena2;
  462. TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  463. TestUtil::SetAllFields(arena2_message);
  464. arena2_message->Swap(&non_arena_message);
  465. TestUtil::ExpectAllFieldsSet(*arena2_message);
  466. TestUtil::ExpectAllFieldsSet(non_arena_message);
  467. }
  468. }
  469. TEST(ArenaTest, UnsafeArenaSwap) {
  470. Arena shared_arena;
  471. TestAllTypes* message1 = Arena::CreateMessage<TestAllTypes>(&shared_arena);
  472. TestAllTypes* message2 = Arena::CreateMessage<TestAllTypes>(&shared_arena);
  473. TestUtil::SetAllFields(message1);
  474. message1->UnsafeArenaSwap(message2);
  475. TestUtil::ExpectAllFieldsSet(*message2);
  476. }
  477. TEST(ArenaTest, SwapBetweenArenasUsingReflection) {
  478. Arena arena1;
  479. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  480. {
  481. Arena arena2;
  482. TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  483. TestUtil::SetAllFields(arena2_message);
  484. const Reflection* r = arena2_message->GetReflection();
  485. r->Swap(arena1_message, arena2_message);
  486. string output;
  487. arena2_message->SerializeToString(&output);
  488. EXPECT_EQ(0, output.size());
  489. }
  490. TestUtil::ExpectAllFieldsSet(*arena1_message);
  491. }
  492. TEST(ArenaTest, SwapBetweenArenaAndNonArenaUsingReflection) {
  493. TestAllTypes non_arena_message;
  494. TestUtil::SetAllFields(&non_arena_message);
  495. {
  496. Arena arena2;
  497. TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  498. TestUtil::SetAllFields(arena2_message);
  499. const Reflection* r = arena2_message->GetReflection();
  500. r->Swap(&non_arena_message, arena2_message);
  501. TestUtil::ExpectAllFieldsSet(*arena2_message);
  502. TestUtil::ExpectAllFieldsSet(non_arena_message);
  503. }
  504. }
  505. TEST(ArenaTest, ReleaseFromArenaMessageMakesCopy) {
  506. TestAllTypes::NestedMessage* nested_msg = NULL;
  507. string* nested_string = NULL;
  508. {
  509. Arena arena;
  510. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  511. arena_message->mutable_optional_nested_message()->set_bb(42);
  512. *arena_message->mutable_optional_string() = "Hello";
  513. nested_msg = arena_message->release_optional_nested_message();
  514. nested_string = arena_message->release_optional_string();
  515. }
  516. EXPECT_EQ(42, nested_msg->bb());
  517. EXPECT_EQ("Hello", *nested_string);
  518. delete nested_msg;
  519. delete nested_string;
  520. }
  521. #ifndef GOOGLE_PROTOBUF_NO_RTTI
  522. TEST(ArenaTest, ReleaseFromArenaMessageUsingReflectionMakesCopy) {
  523. TestAllTypes::NestedMessage* nested_msg = NULL;
  524. // Note: no string: reflection API only supports releasing submessages.
  525. {
  526. Arena arena;
  527. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  528. arena_message->mutable_optional_nested_message()->set_bb(42);
  529. const Reflection* r = arena_message->GetReflection();
  530. const FieldDescriptor* f = arena_message->GetDescriptor()->FindFieldByName(
  531. "optional_nested_message");
  532. nested_msg = static_cast<TestAllTypes::NestedMessage*>(
  533. r->ReleaseMessage(arena_message, f));
  534. }
  535. EXPECT_EQ(42, nested_msg->bb());
  536. delete nested_msg;
  537. }
  538. #endif // !GOOGLE_PROTOBUF_NO_RTTI
  539. TEST(ArenaTest, SetAllocatedAcrossArenas) {
  540. Arena arena1;
  541. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  542. TestAllTypes::NestedMessage* heap_submessage =
  543. new TestAllTypes::NestedMessage();
  544. heap_submessage->set_bb(42);
  545. arena1_message->set_allocated_optional_nested_message(heap_submessage);
  546. // Should keep same object and add to arena's Own()-list.
  547. EXPECT_EQ(heap_submessage,
  548. arena1_message->mutable_optional_nested_message());
  549. {
  550. Arena arena2;
  551. TestAllTypes::NestedMessage* arena2_submessage =
  552. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
  553. arena2_submessage->set_bb(42);
  554. arena1_message->set_allocated_optional_nested_message(arena2_submessage);
  555. EXPECT_NE(arena2_submessage,
  556. arena1_message->mutable_optional_nested_message());
  557. }
  558. TestAllTypes::NestedMessage* arena1_submessage =
  559. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena1);
  560. arena1_submessage->set_bb(42);
  561. TestAllTypes* heap_message = new TestAllTypes;
  562. heap_message->set_allocated_optional_nested_message(arena1_submessage);
  563. EXPECT_NE(arena1_submessage,
  564. heap_message->mutable_optional_nested_message());
  565. delete heap_message;
  566. }
  567. TEST(ArenaTest, SetAllocatedAcrossArenasWithReflection) {
  568. // Same as above, with reflection.
  569. Arena arena1;
  570. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  571. const Reflection* r = arena1_message->GetReflection();
  572. const Descriptor* d = arena1_message->GetDescriptor();
  573. const FieldDescriptor* msg_field = d->FindFieldByName(
  574. "optional_nested_message");
  575. TestAllTypes::NestedMessage* heap_submessage =
  576. new TestAllTypes::NestedMessage();
  577. heap_submessage->set_bb(42);
  578. r->SetAllocatedMessage(arena1_message, heap_submessage, msg_field);
  579. // Should keep same object and add to arena's Own()-list.
  580. EXPECT_EQ(heap_submessage,
  581. arena1_message->mutable_optional_nested_message());
  582. {
  583. Arena arena2;
  584. TestAllTypes::NestedMessage* arena2_submessage =
  585. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
  586. arena2_submessage->set_bb(42);
  587. r->SetAllocatedMessage(arena1_message, arena2_submessage, msg_field);
  588. EXPECT_NE(arena2_submessage,
  589. arena1_message->mutable_optional_nested_message());
  590. }
  591. TestAllTypes::NestedMessage* arena1_submessage =
  592. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena1);
  593. arena1_submessage->set_bb(42);
  594. TestAllTypes* heap_message = new TestAllTypes;
  595. r->SetAllocatedMessage(heap_message, arena1_submessage, msg_field);
  596. EXPECT_NE(arena1_submessage,
  597. heap_message->mutable_optional_nested_message());
  598. delete heap_message;
  599. }
  600. TEST(ArenaTest, AddAllocatedWithReflection) {
  601. Arena arena1;
  602. ArenaMessage* arena1_message = Arena::CreateMessage<ArenaMessage>(&arena1);
  603. const Reflection* r = arena1_message->GetReflection();
  604. const Descriptor* d = arena1_message->GetDescriptor();
  605. const FieldDescriptor* fd =
  606. d->FindFieldByName("repeated_import_no_arena_message");
  607. // Message with cc_enable_arenas = false;
  608. r->AddMessage(arena1_message, fd);
  609. r->AddMessage(arena1_message, fd);
  610. r->AddMessage(arena1_message, fd);
  611. EXPECT_EQ(3, r->FieldSize(*arena1_message, fd));
  612. // Message with cc_enable_arenas = true;
  613. fd = d->FindFieldByName("repeated_nested_message");
  614. r->AddMessage(arena1_message, fd);
  615. r->AddMessage(arena1_message, fd);
  616. r->AddMessage(arena1_message, fd);
  617. EXPECT_EQ(3, r->FieldSize(*arena1_message, fd));
  618. }
  619. TEST(ArenaTest, RepeatedPtrFieldAddClearedTest) {
  620. {
  621. RepeatedPtrField<TestAllTypes> repeated_field;
  622. EXPECT_TRUE(repeated_field.empty());
  623. EXPECT_EQ(0, repeated_field.size());
  624. // Ownership is passed to repeated_field.
  625. TestAllTypes* cleared = new TestAllTypes();
  626. repeated_field.AddCleared(cleared);
  627. EXPECT_TRUE(repeated_field.empty());
  628. EXPECT_EQ(0, repeated_field.size());
  629. }
  630. {
  631. RepeatedPtrField<TestAllTypes> repeated_field;
  632. EXPECT_TRUE(repeated_field.empty());
  633. EXPECT_EQ(0, repeated_field.size());
  634. // Ownership is passed to repeated_field.
  635. TestAllTypes* cleared = new TestAllTypes();
  636. repeated_field.AddAllocated(cleared);
  637. EXPECT_FALSE(repeated_field.empty());
  638. EXPECT_EQ(1, repeated_field.size());
  639. }
  640. }
  641. TEST(ArenaTest, AddAllocatedToRepeatedField) {
  642. // Heap->arena case.
  643. Arena arena1;
  644. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  645. for (int i = 0; i < 10; i++) {
  646. TestAllTypes::NestedMessage* heap_submessage =
  647. new TestAllTypes::NestedMessage();
  648. heap_submessage->set_bb(42);
  649. arena1_message->mutable_repeated_nested_message()->
  650. AddAllocated(heap_submessage);
  651. // Should not copy object -- will use arena_->Own().
  652. EXPECT_EQ(heap_submessage,
  653. &arena1_message->repeated_nested_message(i));
  654. EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb());
  655. }
  656. // Arena1->Arena2 case.
  657. arena1_message->Clear();
  658. for (int i = 0; i < 10; i++) {
  659. Arena arena2;
  660. TestAllTypes::NestedMessage* arena2_submessage =
  661. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
  662. arena2_submessage->set_bb(42);
  663. arena1_message->mutable_repeated_nested_message()->
  664. AddAllocated(arena2_submessage);
  665. // Should copy object.
  666. EXPECT_NE(arena2_submessage,
  667. &arena1_message->repeated_nested_message(i));
  668. EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb());
  669. }
  670. // Arena->heap case.
  671. TestAllTypes* heap_message = new TestAllTypes();
  672. for (int i = 0; i < 10; i++) {
  673. Arena arena2;
  674. TestAllTypes::NestedMessage* arena2_submessage =
  675. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
  676. arena2_submessage->set_bb(42);
  677. heap_message->mutable_repeated_nested_message()->
  678. AddAllocated(arena2_submessage);
  679. // Should copy object.
  680. EXPECT_NE(arena2_submessage,
  681. &heap_message->repeated_nested_message(i));
  682. EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb());
  683. }
  684. delete heap_message;
  685. // Heap-arena case for strings (which are not arena-allocated).
  686. arena1_message->Clear();
  687. for (int i = 0; i < 10; i++) {
  688. string* s = new string("Test");
  689. arena1_message->mutable_repeated_string()->
  690. AddAllocated(s);
  691. // Should not copy.
  692. EXPECT_EQ(s, &arena1_message->repeated_string(i));
  693. EXPECT_EQ("Test", arena1_message->repeated_string(i));
  694. }
  695. }
  696. TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) {
  697. // Heap->arena case.
  698. Arena arena1;
  699. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  700. const Reflection* r = arena1_message->GetReflection();
  701. const Descriptor* d = arena1_message->GetDescriptor();
  702. const FieldDescriptor* fd =
  703. d->FindFieldByName("repeated_nested_message");
  704. for (int i = 0; i < 10; i++) {
  705. TestAllTypes::NestedMessage* heap_submessage =
  706. new TestAllTypes::NestedMessage;
  707. heap_submessage->set_bb(42);
  708. r->AddAllocatedMessage(arena1_message, fd, heap_submessage);
  709. // Should not copy object -- will use arena_->Own().
  710. EXPECT_EQ(heap_submessage,
  711. &arena1_message->repeated_nested_message(i));
  712. EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb());
  713. }
  714. // Arena1->Arena2 case.
  715. arena1_message->Clear();
  716. for (int i = 0; i < 10; i++) {
  717. Arena arena2;
  718. TestAllTypes::NestedMessage* arena2_submessage =
  719. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
  720. arena2_submessage->set_bb(42);
  721. r->AddAllocatedMessage(arena1_message, fd, arena2_submessage);
  722. // Should copy object.
  723. EXPECT_NE(arena2_submessage,
  724. &arena1_message->repeated_nested_message(i));
  725. EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb());
  726. }
  727. // Arena->heap case.
  728. TestAllTypes* heap_message = new TestAllTypes;
  729. for (int i = 0; i < 10; i++) {
  730. Arena arena2;
  731. TestAllTypes::NestedMessage* arena2_submessage =
  732. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
  733. arena2_submessage->set_bb(42);
  734. r->AddAllocatedMessage(heap_message, fd, arena2_submessage);
  735. // Should copy object.
  736. EXPECT_NE(arena2_submessage,
  737. &heap_message->repeated_nested_message(i));
  738. EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb());
  739. }
  740. delete heap_message;
  741. }
  742. TEST(ArenaTest, ReleaseLastRepeatedField) {
  743. // Release from arena-allocated repeated field and ensure that returned object
  744. // is heap-allocated.
  745. Arena arena;
  746. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  747. for (int i = 0; i < 10; i++) {
  748. TestAllTypes::NestedMessage* nested =
  749. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena);
  750. nested->set_bb(42);
  751. arena_message->mutable_repeated_nested_message()->AddAllocated(nested);
  752. }
  753. for (int i = 0; i < 10; i++) {
  754. const TestAllTypes::NestedMessage *orig_submessage =
  755. &arena_message->repeated_nested_message(10 - 1 - i); // last element
  756. TestAllTypes::NestedMessage *released =
  757. arena_message->mutable_repeated_nested_message()->ReleaseLast();
  758. EXPECT_NE(released, orig_submessage);
  759. EXPECT_EQ(42, released->bb());
  760. delete released;
  761. }
  762. // Test UnsafeArenaReleaseLast().
  763. for (int i = 0; i < 10; i++) {
  764. TestAllTypes::NestedMessage* nested =
  765. Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena);
  766. nested->set_bb(42);
  767. arena_message->mutable_repeated_nested_message()->AddAllocated(nested);
  768. }
  769. for (int i = 0; i < 10; i++) {
  770. const TestAllTypes::NestedMessage *orig_submessage =
  771. &arena_message->repeated_nested_message(10 - 1 - i); // last element
  772. TestAllTypes::NestedMessage *released =
  773. arena_message->mutable_repeated_nested_message()->
  774. UnsafeArenaReleaseLast();
  775. EXPECT_EQ(released, orig_submessage);
  776. EXPECT_EQ(42, released->bb());
  777. // no delete -- |released| is on the arena.
  778. }
  779. // Test string case as well. ReleaseLast() in this case must copy the string,
  780. // even though it was originally heap-allocated and its pointer was simply
  781. // appended to the repeated field's internal vector, because the string was
  782. // placed on the arena's destructor list and cannot be removed from that list
  783. // (so the arena permanently owns the original instance).
  784. arena_message->Clear();
  785. for (int i = 0; i < 10; i++) {
  786. string* s = new string("Test");
  787. arena_message->mutable_repeated_string()->AddAllocated(s);
  788. }
  789. for (int i = 0; i < 10; i++) {
  790. const string* orig_element = &arena_message->repeated_string(10 - 1 - i);
  791. string* released = arena_message->mutable_repeated_string()->ReleaseLast();
  792. EXPECT_NE(released, orig_element);
  793. EXPECT_EQ("Test", *released);
  794. delete released;
  795. }
  796. }
  797. TEST(ArenaTest, UnsafeArenaReleaseAdd) {
  798. // Use unsafe_arena_release() and unsafe_arena_set_allocated() to transfer an
  799. // arena-allocated string from one message to another.
  800. const char kContent[] = "Test content";
  801. Arena arena;
  802. TestAllTypes* message1 = Arena::CreateMessage<TestAllTypes>(&arena);
  803. TestAllTypes* message2 = Arena::CreateMessage<TestAllTypes>(&arena);
  804. string* arena_string = Arena::Create<string>(&arena);
  805. *arena_string = kContent;
  806. message1->unsafe_arena_set_allocated_optional_string(arena_string);
  807. message2->unsafe_arena_set_allocated_optional_string(
  808. message1->unsafe_arena_release_optional_string());
  809. EXPECT_EQ(kContent, message2->optional_string());
  810. }
  811. TEST(ArenaTest, UnsafeArenaAddAllocated) {
  812. Arena arena;
  813. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  814. for (int i = 0; i < 10; i++) {
  815. string* arena_string = Arena::Create<string>(&arena);
  816. message->mutable_repeated_string()->UnsafeArenaAddAllocated(arena_string);
  817. EXPECT_EQ(arena_string, message->mutable_repeated_string(i));
  818. }
  819. }
  820. TEST(ArenaTest, UnsafeArenaRelease) {
  821. Arena arena;
  822. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  823. string* s = new string("test string");
  824. message->unsafe_arena_set_allocated_optional_string(s);
  825. EXPECT_TRUE(message->has_optional_string());
  826. EXPECT_EQ("test string", message->optional_string());
  827. s = message->unsafe_arena_release_optional_string();
  828. EXPECT_FALSE(message->has_optional_string());
  829. delete s;
  830. s = new string("test string");
  831. message->unsafe_arena_set_allocated_oneof_string(s);
  832. EXPECT_TRUE(message->has_oneof_string());
  833. EXPECT_EQ("test string", message->oneof_string());
  834. s = message->unsafe_arena_release_oneof_string();
  835. EXPECT_FALSE(message->has_oneof_string());
  836. delete s;
  837. }
  838. TEST(ArenaTest, OneofMerge) {
  839. Arena arena;
  840. TestAllTypes* message0 = Arena::CreateMessage<TestAllTypes>(&arena);
  841. TestAllTypes* message1 = Arena::CreateMessage<TestAllTypes>(&arena);
  842. message0->unsafe_arena_set_allocated_oneof_string(new string("x"));
  843. ASSERT_TRUE(message0->has_oneof_string());
  844. message1->unsafe_arena_set_allocated_oneof_string(new string("y"));
  845. ASSERT_TRUE(message1->has_oneof_string());
  846. EXPECT_EQ("x", message0->oneof_string());
  847. EXPECT_EQ("y", message1->oneof_string());
  848. message0->MergeFrom(*message1);
  849. EXPECT_EQ("y", message0->oneof_string());
  850. EXPECT_EQ("y", message1->oneof_string());
  851. delete message0->unsafe_arena_release_oneof_string();
  852. delete message1->unsafe_arena_release_oneof_string();
  853. }
  854. TEST(ArenaTest, ArenaOneofReflection) {
  855. Arena arena;
  856. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  857. const Descriptor* desc = message->GetDescriptor();
  858. const Reflection* refl = message->GetReflection();
  859. const FieldDescriptor* string_field = desc->FindFieldByName(
  860. "oneof_string");
  861. const FieldDescriptor* msg_field = desc->FindFieldByName(
  862. "oneof_nested_message");
  863. const OneofDescriptor* oneof = desc->FindOneofByName(
  864. "oneof_field");
  865. refl->SetString(message, string_field, "Test value");
  866. EXPECT_TRUE(refl->HasOneof(*message, oneof));
  867. refl->ClearOneof(message, oneof);
  868. EXPECT_FALSE(refl->HasOneof(*message, oneof));
  869. Message* submsg = refl->MutableMessage(message, msg_field);
  870. EXPECT_TRUE(refl->HasOneof(*message, oneof));
  871. refl->ClearOneof(message, oneof);
  872. EXPECT_FALSE(refl->HasOneof(*message, oneof));
  873. refl->MutableMessage(message, msg_field);
  874. EXPECT_TRUE(refl->HasOneof(*message, oneof));
  875. submsg = refl->ReleaseMessage(message, msg_field);
  876. EXPECT_FALSE(refl->HasOneof(*message, oneof));
  877. EXPECT_TRUE(submsg->GetArena() == NULL);
  878. delete submsg;
  879. }
  880. void TestSwapRepeatedField(Arena* arena1, Arena* arena2) {
  881. // Test "safe" (copying) semantics for direct Swap() on RepeatedPtrField
  882. // between arenas.
  883. RepeatedPtrField<TestAllTypes> field1(arena1);
  884. RepeatedPtrField<TestAllTypes> field2(arena2);
  885. for (int i = 0; i < 10; i++) {
  886. TestAllTypes* t = Arena::CreateMessage<TestAllTypes>(arena1);
  887. t->set_optional_string("field1");
  888. t->set_optional_int32(i);
  889. if (arena1 != NULL) {
  890. field1.UnsafeArenaAddAllocated(t);
  891. } else {
  892. field1.AddAllocated(t);
  893. }
  894. }
  895. for (int i = 0; i < 5; i++) {
  896. TestAllTypes* t = Arena::CreateMessage<TestAllTypes>(arena2);
  897. t->set_optional_string("field2");
  898. t->set_optional_int32(i);
  899. if (arena2 != NULL) {
  900. field2.UnsafeArenaAddAllocated(t);
  901. } else {
  902. field2.AddAllocated(t);
  903. }
  904. }
  905. field1.Swap(&field2);
  906. EXPECT_EQ(5, field1.size());
  907. EXPECT_EQ(10, field2.size());
  908. EXPECT_TRUE(string("field1") == field2.Get(0).optional_string());
  909. EXPECT_TRUE(string("field2") == field1.Get(0).optional_string());
  910. // Ensure that fields retained their original order:
  911. for (int i = 0; i < field1.size(); i++) {
  912. EXPECT_EQ(i, field1.Get(i).optional_int32());
  913. }
  914. for (int i = 0; i < field2.size(); i++) {
  915. EXPECT_EQ(i, field2.Get(i).optional_int32());
  916. }
  917. }
  918. TEST(ArenaTest, SwapRepeatedField) {
  919. Arena arena;
  920. TestSwapRepeatedField(&arena, &arena);
  921. }
  922. TEST(ArenaTest, SwapRepeatedFieldWithDifferentArenas) {
  923. Arena arena1;
  924. Arena arena2;
  925. TestSwapRepeatedField(&arena1, &arena2);
  926. }
  927. TEST(ArenaTest, SwapRepeatedFieldWithNoArenaOnRightHandSide) {
  928. Arena arena;
  929. TestSwapRepeatedField(&arena, NULL);
  930. }
  931. TEST(ArenaTest, SwapRepeatedFieldWithNoArenaOnLeftHandSide) {
  932. Arena arena;
  933. TestSwapRepeatedField(NULL, &arena);
  934. }
  935. TEST(ArenaTest, ExtensionsOnArena) {
  936. Arena arena;
  937. // Ensure no leaks.
  938. TestAllExtensions* message_ext =
  939. Arena::CreateMessage<TestAllExtensions>(&arena);
  940. message_ext->SetExtension(
  941. protobuf_unittest::optional_int32_extension, 42);
  942. message_ext->SetExtension(
  943. protobuf_unittest::optional_string_extension, string("test"));
  944. message_ext->MutableExtension(
  945. protobuf_unittest::optional_nested_message_extension)->set_bb(42);
  946. }
  947. TEST(ArenaTest, RepeatedFieldOnArena) {
  948. // Preallocate an initial arena block to avoid mallocs during hooked region.
  949. std::vector<char> arena_block(1024 * 1024);
  950. ArenaOptions options;
  951. options.initial_block = &arena_block[0];
  952. options.initial_block_size = arena_block.size();
  953. Arena arena(options);
  954. {
  955. internal::NoHeapChecker no_heap;
  956. // Fill some repeated fields on the arena to test for leaks. Also verify no
  957. // memory allocations.
  958. RepeatedField<int32> repeated_int32(&arena);
  959. RepeatedPtrField<TestAllTypes> repeated_message(&arena);
  960. for (int i = 0; i < 100; i++) {
  961. repeated_int32.Add(42);
  962. repeated_message.Add()->set_optional_int32(42);
  963. EXPECT_EQ(&arena, repeated_message.Get(0).GetArena());
  964. const TestAllTypes* msg_in_repeated_field = &repeated_message.Get(0);
  965. TestAllTypes* msg = repeated_message.UnsafeArenaReleaseLast();
  966. EXPECT_EQ(msg_in_repeated_field, msg);
  967. }
  968. // UnsafeArenaExtractSubrange (i) should not leak and (ii) should return
  969. // on-arena pointers.
  970. for (int i = 0; i < 10; i++) {
  971. repeated_message.Add()->set_optional_int32(42);
  972. }
  973. TestAllTypes* extracted_messages[5];
  974. repeated_message.UnsafeArenaExtractSubrange(0, 5, extracted_messages);
  975. EXPECT_EQ(&arena, repeated_message.Get(0).GetArena());
  976. EXPECT_EQ(5, repeated_message.size());
  977. }
  978. // Now, outside the scope of the NoHeapChecker, test ExtractSubrange's copying
  979. // semantics.
  980. {
  981. RepeatedPtrField<TestAllTypes> repeated_message(&arena);
  982. for (int i = 0; i < 100; i++) {
  983. repeated_message.Add()->set_optional_int32(42);
  984. }
  985. TestAllTypes* extracted_messages[5];
  986. // ExtractSubrange should copy to the heap.
  987. repeated_message.ExtractSubrange(0, 5, extracted_messages);
  988. EXPECT_EQ(NULL, extracted_messages[0]->GetArena());
  989. // We need to free the heap-allocated messages to prevent a leak.
  990. for (int i = 0; i < 5; i++) {
  991. delete extracted_messages[i];
  992. extracted_messages[i] = NULL;
  993. }
  994. }
  995. // Now check that we can create RepeatedFields/RepeatedPtrFields themselves on
  996. // the arena. They have the necessary type traits so that they can behave like
  997. // messages in this way. This is useful for higher-level generic templated
  998. // code that may allocate messages or repeated fields of messages on an arena.
  999. {
  1000. RepeatedPtrField<TestAllTypes>* repeated_ptr_on_arena =
  1001. Arena::CreateMessage< RepeatedPtrField<TestAllTypes> >(&arena);
  1002. for (int i = 0; i < 10; i++) {
  1003. // Add some elements and let the leak-checker ensure that everything is
  1004. // freed.
  1005. repeated_ptr_on_arena->Add();
  1006. }
  1007. RepeatedField<int>* repeated_int_on_arena =
  1008. Arena::CreateMessage< RepeatedField<int> >(&arena);
  1009. for (int i = 0; i < 100; i++) {
  1010. repeated_int_on_arena->Add(i);
  1011. }
  1012. }
  1013. arena.Reset();
  1014. }
  1015. #ifndef GOOGLE_PROTOBUF_NO_RTTI
  1016. TEST(ArenaTest, MutableMessageReflection) {
  1017. Arena arena;
  1018. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  1019. const Reflection* r = message->GetReflection();
  1020. const Descriptor* d = message->GetDescriptor();
  1021. const FieldDescriptor* field = d->FindFieldByName("optional_nested_message");
  1022. TestAllTypes::NestedMessage* submessage =
  1023. static_cast<TestAllTypes::NestedMessage*>(
  1024. r->MutableMessage(message, field));
  1025. TestAllTypes::NestedMessage* submessage_expected =
  1026. message->mutable_optional_nested_message();
  1027. EXPECT_EQ(submessage_expected, submessage);
  1028. EXPECT_EQ(&arena, submessage->GetArena());
  1029. const FieldDescriptor* oneof_field = d->FindFieldByName("oneof_nested_message");
  1030. submessage = static_cast<TestAllTypes::NestedMessage*>(
  1031. r->MutableMessage(message, oneof_field));
  1032. submessage_expected = message->mutable_oneof_nested_message();
  1033. EXPECT_EQ(submessage_expected, submessage);
  1034. EXPECT_EQ(&arena, submessage->GetArena());
  1035. }
  1036. #endif // !GOOGLE_PROTOBUF_NO_RTTI
  1037. void FillArenaAwareFields(TestAllTypes* message) {
  1038. string test_string = "hello world";
  1039. message->set_optional_int32(42);
  1040. message->set_optional_string(test_string);
  1041. message->set_optional_bytes(test_string);
  1042. message->mutable_optional_nested_message()->set_bb(42);
  1043. message->set_oneof_uint32(42);
  1044. message->mutable_oneof_nested_message()->set_bb(42);
  1045. message->set_oneof_string(test_string);
  1046. message->set_oneof_bytes(test_string);
  1047. message->add_repeated_int32(42);
  1048. // No repeated string: not yet arena-aware.
  1049. message->add_repeated_nested_message()->set_bb(42);
  1050. message->mutable_optional_lazy_message()->set_bb(42);
  1051. }
  1052. // Test: no allocations occur on heap while touching all supported field types.
  1053. TEST(ArenaTest, NoHeapAllocationsTest) {
  1054. // Allocate a large initial block to avoid mallocs during hooked test.
  1055. std::vector<char> arena_block(128 * 1024);
  1056. ArenaOptions options;
  1057. options.initial_block = &arena_block[0];
  1058. options.initial_block_size = arena_block.size();
  1059. Arena arena(options);
  1060. {
  1061. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  1062. FillArenaAwareFields(message);
  1063. }
  1064. arena.Reset();
  1065. }
  1066. TEST(ArenaTest, ParseCorruptedString) {
  1067. TestAllTypes message;
  1068. TestUtil::SetAllFields(&message);
  1069. TestParseCorruptedString<TestAllTypes, true>(message);
  1070. TestParseCorruptedString<TestAllTypes, false>(message);
  1071. }
  1072. #ifndef GOOGLE_PROTOBUF_NO_RTTI
  1073. // Test construction on an arena via generic MessageLite interface. We should be
  1074. // able to successfully deserialize on the arena without incurring heap
  1075. // allocations, i.e., everything should still be arena-allocation-aware.
  1076. TEST(ArenaTest, MessageLiteOnArena) {
  1077. std::vector<char> arena_block(128 * 1024);
  1078. ArenaOptions options;
  1079. options.initial_block = &arena_block[0];
  1080. options.initial_block_size = arena_block.size();
  1081. Arena arena(options);
  1082. const google::protobuf::MessageLite* prototype = &TestAllTypes::default_instance();
  1083. TestAllTypes initial_message;
  1084. FillArenaAwareFields(&initial_message);
  1085. string serialized;
  1086. initial_message.SerializeToString(&serialized);
  1087. {
  1088. google::protobuf::MessageLite* generic_message = prototype->New(&arena);
  1089. EXPECT_TRUE(generic_message != NULL);
  1090. EXPECT_EQ(&arena, generic_message->GetArena());
  1091. EXPECT_TRUE(generic_message->ParseFromString(serialized));
  1092. TestAllTypes* deserialized = static_cast<TestAllTypes*>(generic_message);
  1093. EXPECT_EQ(42, deserialized->optional_int32());
  1094. }
  1095. arena.Reset();
  1096. }
  1097. #endif // !GOOGLE_PROTOBUF_NO_RTTI
  1098. // RepeatedField should support non-POD types, and invoke constructors and
  1099. // destructors appropriately, because it's used this way by lots of other code
  1100. // (even if this was not its original intent).
  1101. TEST(ArenaTest, RepeatedFieldWithNonPODType) {
  1102. {
  1103. RepeatedField<string> field_on_heap;
  1104. for (int i = 0; i < 100; i++) {
  1105. *field_on_heap.Add() = "test string long enough to exceed inline buffer";
  1106. }
  1107. }
  1108. {
  1109. Arena arena;
  1110. RepeatedField<string> field_on_arena(&arena);
  1111. for (int i = 0; i < 100; i++) {
  1112. *field_on_arena.Add() = "test string long enough to exceed inline buffer";
  1113. }
  1114. }
  1115. }
  1116. // Align n to next multiple of 8
  1117. uint64 Align8(uint64 n) { return (n + 7) & -8; }
  1118. TEST(ArenaTest, SpaceAllocated_and_Used) {
  1119. ArenaOptions options;
  1120. options.start_block_size = 256;
  1121. options.max_block_size = 8192;
  1122. Arena arena_1(options);
  1123. EXPECT_EQ(0, arena_1.SpaceAllocated());
  1124. EXPECT_EQ(0, arena_1.SpaceUsed());
  1125. EXPECT_EQ(0, arena_1.Reset());
  1126. ::google::protobuf::Arena::CreateArray<char>(&arena_1, 320);
  1127. // Arena will allocate slightly more than 320 for the block headers.
  1128. EXPECT_LE(320, arena_1.SpaceAllocated());
  1129. EXPECT_EQ(Align8(320), arena_1.SpaceUsed());
  1130. EXPECT_LE(320, arena_1.Reset());
  1131. // Test with initial block.
  1132. std::vector<char> arena_block(1024);
  1133. options.initial_block = &arena_block[0];
  1134. options.initial_block_size = arena_block.size();
  1135. Arena arena_2(options);
  1136. EXPECT_EQ(1024, arena_2.SpaceAllocated());
  1137. EXPECT_EQ(0, arena_2.SpaceUsed());
  1138. EXPECT_EQ(1024, arena_2.Reset());
  1139. ::google::protobuf::Arena::CreateArray<char>(&arena_2, 55);
  1140. EXPECT_EQ(1024, arena_2.SpaceAllocated());
  1141. EXPECT_EQ(Align8(55), arena_2.SpaceUsed());
  1142. EXPECT_EQ(1024, arena_2.Reset());
  1143. // Reset options to test doubling policy explicitly.
  1144. options.initial_block = NULL;
  1145. options.initial_block_size = 0;
  1146. Arena arena_3(options);
  1147. EXPECT_EQ(0, arena_3.SpaceUsed());
  1148. ::google::protobuf::Arena::CreateArray<char>(&arena_3, 160);
  1149. EXPECT_EQ(256, arena_3.SpaceAllocated());
  1150. EXPECT_EQ(Align8(160), arena_3.SpaceUsed());
  1151. ::google::protobuf::Arena::CreateArray<char>(&arena_3, 70);
  1152. EXPECT_EQ(256 + 512, arena_3.SpaceAllocated());
  1153. EXPECT_EQ(Align8(160) + Align8(70), arena_3.SpaceUsed());
  1154. EXPECT_EQ(256 + 512, arena_3.Reset());
  1155. }
  1156. TEST(ArenaTest, Alignment) {
  1157. ::google::protobuf::Arena arena;
  1158. for (int i = 0; i < 200; i++) {
  1159. void* p = ::google::protobuf::Arena::CreateArray<char>(&arena, i);
  1160. GOOGLE_CHECK_EQ(reinterpret_cast<uintptr_t>(p) % 8, 0) << i << ": " << p;
  1161. }
  1162. }
  1163. TEST(ArenaTest, BlockSizeSmallerThanAllocation) {
  1164. for (size_t i = 0; i <= 8; ++i) {
  1165. ::google::protobuf::ArenaOptions opt;
  1166. opt.start_block_size = opt.max_block_size = i;
  1167. ::google::protobuf::Arena arena(opt);
  1168. *::google::protobuf::Arena::Create<int64>(&arena) = 42;
  1169. EXPECT_GE(arena.SpaceAllocated(), 8);
  1170. EXPECT_EQ(8, arena.SpaceUsed());
  1171. *::google::protobuf::Arena::Create<int64>(&arena) = 42;
  1172. EXPECT_GE(arena.SpaceAllocated(), 16);
  1173. EXPECT_EQ(16, arena.SpaceUsed());
  1174. }
  1175. }
  1176. TEST(ArenaTest, GetArenaShouldReturnTheArenaForArenaAllocatedMessages) {
  1177. ::google::protobuf::Arena arena;
  1178. ArenaMessage* message = Arena::CreateMessage<ArenaMessage>(&arena);
  1179. const ArenaMessage* const_pointer_to_message = message;
  1180. EXPECT_EQ(&arena, Arena::GetArena(message));
  1181. EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message));
  1182. }
  1183. TEST(ArenaTest, GetArenaShouldReturnNullForNonArenaAllocatedMessages) {
  1184. ArenaMessage message;
  1185. const ArenaMessage* const_pointer_to_message = &message;
  1186. EXPECT_EQ(NULL, Arena::GetArena(&message));
  1187. EXPECT_EQ(NULL, Arena::GetArena(const_pointer_to_message));
  1188. }
  1189. TEST(ArenaTest, AddCleanup) {
  1190. ::google::protobuf::Arena arena;
  1191. for (int i = 0; i < 100; i++) {
  1192. arena.Own(new int);
  1193. }
  1194. }
  1195. TEST(ArenaTest, UnsafeSetAllocatedOnArena) {
  1196. ::google::protobuf::Arena arena;
  1197. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  1198. EXPECT_FALSE(message->has_optional_string());
  1199. string owned_string = "test with long enough content to heap-allocate";
  1200. message->unsafe_arena_set_allocated_optional_string(&owned_string);
  1201. EXPECT_TRUE(message->has_optional_string());
  1202. message->unsafe_arena_set_allocated_optional_string(NULL);
  1203. EXPECT_FALSE(message->has_optional_string());
  1204. }
  1205. // A helper utility class to only contain static hook functions, some
  1206. // counters to be used to verify the counters have been called and a cookie
  1207. // value to be verified.
  1208. class ArenaHooksTestUtil {
  1209. public:
  1210. static void* on_init(::google::protobuf::Arena* arena) {
  1211. ++num_init;
  1212. int* cookie = new int(kCookieValue);
  1213. return static_cast<void*>(cookie);
  1214. }
  1215. static void on_allocation(const std::type_info* /*unused*/, uint64 alloc_size,
  1216. void* cookie) {
  1217. ++num_allocations;
  1218. int cookie_value = *static_cast<int*>(cookie);
  1219. EXPECT_EQ(kCookieValue, cookie_value);
  1220. }
  1221. static void on_reset(::google::protobuf::Arena* arena, void* cookie,
  1222. uint64 space_used) {
  1223. ++num_reset;
  1224. int cookie_value = *static_cast<int*>(cookie);
  1225. EXPECT_EQ(kCookieValue, cookie_value);
  1226. }
  1227. static void on_destruction(::google::protobuf::Arena* arena, void* cookie,
  1228. uint64 space_used) {
  1229. ++num_destruct;
  1230. int cookie_value = *static_cast<int*>(cookie);
  1231. EXPECT_EQ(kCookieValue, cookie_value);
  1232. delete static_cast<int*>(cookie);
  1233. }
  1234. static const int kCookieValue = 999;
  1235. static uint32 num_init;
  1236. static uint32 num_allocations;
  1237. static uint32 num_reset;
  1238. static uint32 num_destruct;
  1239. };
  1240. uint32 ArenaHooksTestUtil::num_init = 0;
  1241. uint32 ArenaHooksTestUtil::num_allocations = 0;
  1242. uint32 ArenaHooksTestUtil::num_reset = 0;
  1243. uint32 ArenaHooksTestUtil::num_destruct = 0;
  1244. const int ArenaHooksTestUtil::kCookieValue;
  1245. class ArenaOptionsTestFriend {
  1246. public:
  1247. static void Set(::google::protobuf::ArenaOptions* options) {
  1248. options->on_arena_init = ArenaHooksTestUtil::on_init;
  1249. options->on_arena_allocation = ArenaHooksTestUtil::on_allocation;
  1250. options->on_arena_reset = ArenaHooksTestUtil::on_reset;
  1251. options->on_arena_destruction = ArenaHooksTestUtil::on_destruction;
  1252. }
  1253. };
  1254. // Test the hooks are correctly called and that the cookie is passed.
  1255. TEST(ArenaTest, ArenaHooksSanity) {
  1256. ::google::protobuf::ArenaOptions options;
  1257. ArenaOptionsTestFriend::Set(&options);
  1258. // Scope for defining the arena
  1259. {
  1260. ::google::protobuf::Arena arena(options);
  1261. EXPECT_EQ(1, ArenaHooksTestUtil::num_init);
  1262. EXPECT_EQ(0, ArenaHooksTestUtil::num_allocations);
  1263. ::google::protobuf::Arena::Create<uint64>(&arena);
  1264. if (std::is_trivially_destructible<uint64>::value) {
  1265. EXPECT_EQ(1, ArenaHooksTestUtil::num_allocations);
  1266. } else {
  1267. EXPECT_EQ(2, ArenaHooksTestUtil::num_allocations);
  1268. }
  1269. arena.Reset();
  1270. arena.Reset();
  1271. EXPECT_EQ(2, ArenaHooksTestUtil::num_reset);
  1272. }
  1273. EXPECT_EQ(3, ArenaHooksTestUtil::num_reset);
  1274. EXPECT_EQ(1, ArenaHooksTestUtil::num_destruct);
  1275. }
  1276. } // namespace protobuf
  1277. } // namespace google