[opt] Do not compare optimized binary with an invalidated buffer (#2999)

This commit is contained in:
Jakub Kuderski 2019-10-30 10:01:28 -07:00 committed by GitHub
parent 7e2cba6a52
commit f893d4d41d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -560,23 +560,26 @@ bool Optimizer::Run(const uint32_t* original_binary,
return false;
}
optimized_binary->clear();
context->module()->ToBinary(optimized_binary, /* skip_nop = */ true);
#ifndef NDEBUG
if (status == opt::Pass::Status::SuccessWithoutChange) {
std::vector<uint32_t> optimized_binary_with_nop;
context->module()->ToBinary(&optimized_binary_with_nop,
/* skip_nop = */ false);
auto changed = optimized_binary_with_nop.size() != original_binary_size ||
memcmp(optimized_binary_with_nop.data(), original_binary,
original_binary_size) != 0;
assert(!changed &&
"Binary unexpectedly changed despite optimizer saying there was no "
"change");
assert(optimized_binary_with_nop.size() == original_binary_size &&
"Binary size unexpectedly changed despite the optimizer saying "
"there was no change");
assert(memcmp(optimized_binary_with_nop.data(), original_binary,
original_binary_size) == 0 &&
"Binary content unexpectedly changed despite the optimizer saying "
"there was no change");
}
#endif // !NDEBUG
// Note that |original_binary| and |optimized_binary| may share the same
// buffer and the below will invalidate |original_binary|.
optimized_binary->clear();
context->module()->ToBinary(optimized_binary, /* skip_nop = */ true);
return true;
}