Skip to content

Commit

Permalink
Fix memory corruption caused by new test in options_settable_test (fa…
Browse files Browse the repository at this point in the history
…cebook#6676)

Summary:
facebook#6668 added some new test code but it has a risk of memory corruption. Fix it
Pull Request resolved: facebook#6676

Test Plan: Run the test under ASAN and see it passes.

Reviewed By: ajkr

Differential Revision: D20937108

fbshipit-source-id: 22cc96bb02030df0a37a02e67a2cc37ca31ba22d
  • Loading branch information
siying authored and facebook-github-bot committed Apr 9, 2020
1 parent 6e6f807 commit e860f88
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions options/options_settable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -511,27 +511,39 @@ TEST_F(OptionsSettableTest, ColumnFamilyOptionsAllFieldsSettable) {
sizeof(std::vector<uint64_t>)},
};

// Construct two pieces of memory with controlled base. This is needed as
// otherwise padding bytes might not be filled.
// For all memory used for options, pre-fill every char. Otherwise, the
// padding bytes might be different so that byte-wise comparison doesn't
// general equal results even if objects are equal.
const char kMySpecialChar = 'x';
char* mcfo1_ptr = new char[sizeof(MutableCFOptions)];
MutableCFOptions* mcfo1 = new (mcfo1_ptr) MutableCFOptions();
FillWithSpecialChar(mcfo1_ptr, sizeof(MutableCFOptions),
kMutableCFOptionsBlacklist, 'x');
kMutableCFOptionsBlacklist, kMySpecialChar);
char* mcfo2_ptr = new char[sizeof(MutableCFOptions)];
MutableCFOptions* mcfo2 = new (mcfo2_ptr) MutableCFOptions();
FillWithSpecialChar(mcfo2_ptr, sizeof(MutableCFOptions),
kMutableCFOptionsBlacklist, 'x');
kMutableCFOptionsBlacklist, kMySpecialChar);

// A clean column family options is constructed after filling the same special
// char as the initial one. So that the padding bytes are the same.
char* cfo_clean_ptr = new char[sizeof(ColumnFamilyOptions)];
FillWithSpecialChar(cfo_clean_ptr, sizeof(ColumnFamilyOptions),
kColumnFamilyOptionsBlacklist);
rnd_filled_options.num_levels = 66;
*mcfo1 = MutableCFOptions(rnd_filled_options);
ColumnFamilyOptions cfo_back =
BuildColumnFamilyOptions(ColumnFamilyOptions(), *mcfo1);
*mcfo2 = MutableCFOptions(cfo_back);
ColumnFamilyOptions* cfo_clean = new (cfo_clean_ptr) ColumnFamilyOptions();

MutableCFOptions* mcfo1 =
new (mcfo1_ptr) MutableCFOptions(rnd_filled_options);
ColumnFamilyOptions cfo_back = BuildColumnFamilyOptions(*cfo_clean, *mcfo1);
MutableCFOptions* mcfo2 = new (mcfo2_ptr) MutableCFOptions(cfo_back);

ASSERT_TRUE(CompareBytes(mcfo1_ptr, mcfo2_ptr, sizeof(MutableCFOptions),
kMutableCFOptionsBlacklist));
delete[] mcfo1;
delete[] mcfo2;

cfo_clean->~ColumnFamilyOptions();
mcfo1->~MutableCFOptions();
mcfo2->~MutableCFOptions();
delete[] mcfo1_ptr;
delete[] mcfo2_ptr;
delete[] cfo_clean_ptr;
}
#endif // !__clang__
#endif // OS_LINUX || OS_WIN
Expand Down

0 comments on commit e860f88

Please sign in to comment.