F14AsanSupportTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Portability.h>
  17. #include <folly/container/F14Set.h>
  18. #include <folly/portability/GTest.h>
  19. using namespace folly;
  20. template <typename S>
  21. void useIteratorAfterInsertSmall() {
  22. S set;
  23. auto iter = set.insert(1).first;
  24. set.insert(2);
  25. EXPECT_EQ(*iter, 1);
  26. }
  27. template <typename S>
  28. void useIteratorAfterInsertLarge() {
  29. S set;
  30. set.insert(10);
  31. for (int i = 0; i < 1000; ++i) {
  32. auto iter = set.find(10);
  33. set.insert(i);
  34. if (i > 500) {
  35. EXPECT_EQ(*iter, 10);
  36. }
  37. }
  38. }
  39. template <typename S>
  40. void useReferenceAfterInsertSmall() {
  41. S set;
  42. auto& ref = *set.insert(1).first;
  43. set.insert(2);
  44. EXPECT_EQ(ref, 1);
  45. }
  46. template <typename S>
  47. void useReferenceAfterInsertLarge() {
  48. S set;
  49. set.insert(10);
  50. for (int i = 0; i < 1000; ++i) {
  51. auto& ref = *set.find(10);
  52. set.insert(i);
  53. if (i > 500) {
  54. EXPECT_EQ(ref, 10);
  55. }
  56. }
  57. }
  58. template <typename F>
  59. void repeat(F const& func) {
  60. for (int i = 0; i < 32; ++i) {
  61. func();
  62. }
  63. }
  64. template <typename F>
  65. void expectAsanFailure(F const& func) {
  66. if (kIsSanitizeAddress) {
  67. EXPECT_EXIT(
  68. repeat(func), testing::ExitedWithCode(1), ".*heap-use-after-free.*");
  69. }
  70. }
  71. TEST(F14AsanSupportTest, F14ValueIterInsertSmall) {
  72. expectAsanFailure(useIteratorAfterInsertSmall<F14ValueSet<int>>);
  73. }
  74. TEST(F14AsanSupportTest, F14NodeIterInsertSmall) {
  75. expectAsanFailure(useIteratorAfterInsertSmall<F14NodeSet<int>>);
  76. }
  77. TEST(F14AsanSupportTest, F14VectorIterInsertSmall) {
  78. expectAsanFailure(useIteratorAfterInsertSmall<F14VectorSet<int>>);
  79. }
  80. TEST(F14AsanSupportTest, F14FastIterInsertSmall) {
  81. expectAsanFailure(useIteratorAfterInsertSmall<F14FastSet<int>>);
  82. }
  83. TEST(F14AsanSupportTest, F14ValueIterInsertLarge) {
  84. expectAsanFailure(useIteratorAfterInsertLarge<F14ValueSet<int>>);
  85. }
  86. TEST(F14AsanSupportTest, F14NodeIterInsertLarge) {
  87. expectAsanFailure(useIteratorAfterInsertLarge<F14NodeSet<int>>);
  88. }
  89. TEST(F14AsanSupportTest, F14VectorIterInsertLarge) {
  90. expectAsanFailure(useIteratorAfterInsertLarge<F14VectorSet<int>>);
  91. }
  92. TEST(F14AsanSupportTest, F14FastIterInsertLarge) {
  93. expectAsanFailure(useIteratorAfterInsertLarge<F14FastSet<int>>);
  94. }
  95. TEST(F14AsanSupportTest, F14ValueRefInsertSmall) {
  96. expectAsanFailure(useReferenceAfterInsertSmall<F14ValueSet<int>>);
  97. }
  98. TEST(F14AsanSupportTest, F14VectorRefInsertSmall) {
  99. expectAsanFailure(useReferenceAfterInsertSmall<F14VectorSet<int>>);
  100. }
  101. TEST(F14AsanSupportTest, F14FastRefInsertSmall) {
  102. expectAsanFailure(useReferenceAfterInsertSmall<F14FastSet<int>>);
  103. }
  104. TEST(F14AsanSupportTest, F14ValueRefInsertLarge) {
  105. expectAsanFailure(useReferenceAfterInsertLarge<F14ValueSet<int>>);
  106. }
  107. TEST(F14AsanSupportTest, F14VectorRefInsertLarge) {
  108. expectAsanFailure(useReferenceAfterInsertLarge<F14VectorSet<int>>);
  109. }
  110. TEST(F14AsanSupportTest, F14FastRefInsertLarge) {
  111. expectAsanFailure(useReferenceAfterInsertLarge<F14FastSet<int>>);
  112. }
  113. TEST(F14AsanSupportTest, F14VectorErase) {
  114. F14VectorSet<int> set;
  115. set.insert(1);
  116. set.insert(2);
  117. set.insert(3);
  118. auto& v = *set.begin();
  119. EXPECT_EQ(v, 3);
  120. set.erase(2);
  121. if (kIsSanitizeAddress) {
  122. EXPECT_NE(v, 3);
  123. }
  124. }