Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
force use of CrtAllocator
Browse files Browse the repository at this point in the history
* asan announces "heap-use-after-free" with the default
  MemmoryPoolAllocator
  • Loading branch information
weigon committed Jun 14, 2018
1 parent 5f97b58 commit a48037d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/mock_server/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class MysqlServerMockFrontend {
mock_server_config.set("module_prefix", config_.module_prefix);

try {
loader_ = std::unique_ptr<mysql_harness::Loader>(new mysql_harness::Loader("server-mock", loader_config));
loader_.reset(new mysql_harness::Loader("server-mock", loader_config));
} catch (const std::runtime_error &err) {
throw std::runtime_error(std::string("init-loader failed: ") + err.what());
}
Expand Down
17 changes: 10 additions & 7 deletions src/mock_server/src/rest_mock_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ IMPORT_LOG_FUNCTIONS()
static constexpr const char kSectionName[] { "rest_mock_server" };
static constexpr const char kHttpHandlerUri[] { "^/api/v1/mock_server/globals/$" };

// AddressSanitizer gets confused by the default, MemoryPoolAllocator
// Solaris sparc also gets crashes
using JsonDocument = rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>;


using mysql_harness::ARCHITECTURE_DESCRIPTOR;
using mysql_harness::PluginFuncEnv;
Expand Down Expand Up @@ -114,7 +119,7 @@ class RestApiV1MockServer: public BaseRequestHandler {
auto data = body.pop_front(body.length());
std::string str_data(data.begin(), data.end());

rapidjson::Document body_doc;
JsonDocument body_doc;
body_doc.Parse(str_data.c_str());

if (body_doc.HasParseError()) {
Expand Down Expand Up @@ -159,17 +164,15 @@ class RestApiV1MockServer: public BaseRequestHandler {
rapidjson::StringBuffer json_buf;
{
rapidjson::Writer<rapidjson::StringBuffer> json_writer(json_buf);
rapidjson::Document json_doc;
JsonDocument json_doc;

json_doc.SetObject();

rapidjson::Document::AllocatorType& allocator = json_doc.GetAllocator();

auto shared_globals = MockServerComponent::getInstance().getGlobalScope();
auto all_globals = shared_globals->get_all();

for (auto &element: all_globals) {
rapidjson::Document value_doc;
JsonDocument value_doc;
value_doc.Parse(element.second.c_str()); // value is a json-value as string

if (value_doc.HasParseError()) {
Expand All @@ -178,9 +181,9 @@ class RestApiV1MockServer: public BaseRequestHandler {
}

json_doc.AddMember(
rapidjson::Value(element.first.c_str(), element.first.size(), allocator),
JsonValue(element.first.c_str(), element.first.size(), json_doc.GetAllocator()),
value_doc,
allocator);
json_doc.GetAllocator());
}

json_doc.Accept(json_writer);
Expand Down
33 changes: 30 additions & 3 deletions tests/component/test_rest_mock_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const std::string kMockServerInvalidRestUri = "/api/v1/mock_server/global/";
constexpr std::chrono::milliseconds kMockServerMaxRestEndpointWaitTime{1000};
constexpr std::chrono::milliseconds kMockServerMaxRestEndpointStepTime{50};

// AddressSanitizer gets confused by the default, MemoryPoolAllocator
// Solaris sparc also gets crashes
using JsonDocument = rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>;


class RestMockServerTest : public RouterComponentTest, public ::testing::Test {
protected:
TcpPortPool port_pool_;
Expand Down Expand Up @@ -115,13 +121,17 @@ TEST_F(RestMockServerTest, get_globals_empty) {
<< http_hostname << ":" << std::to_string(http_port)
<< " failed (early): "
<< req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

ASSERT_GT(req.get_response_code(), 0u)
<< "HTTP Request to "
<< http_hostname << ":" << std::to_string(http_port)
<< " failed: "
<< req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

EXPECT_EQ(req.get_response_code(), 200u);
Expand All @@ -135,7 +145,7 @@ TEST_F(RestMockServerTest, get_globals_empty) {

std::string json_payload(resp_body_content.begin(), resp_body_content.end());

rapidjson::Document json_doc;
JsonDocument json_doc;
json_doc.Parse(json_payload.c_str());

EXPECT_TRUE(!json_doc.HasParseError()) << json_payload;
Expand Down Expand Up @@ -228,13 +238,17 @@ TEST_F(RestMockServerTest, put_globals_no_json) {
<< http_hostname << ":" << std::to_string(http_port)
<< " failed (early): "
<< req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

ASSERT_GT(req.get_response_code(), 0u)
<< "HTTP Request to "
<< http_hostname << ":" << std::to_string(http_port)
<< " failed: "
<< req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

EXPECT_EQ(req.get_response_code(), 415u);
Expand Down Expand Up @@ -278,13 +292,17 @@ TEST_F(RestMockServerTest, put_globals_ok) {
<< http_hostname << ":" << std::to_string(http_port)
<< " failed (early): "
<< req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

ASSERT_GT(req.get_response_code(), 0u)
<< "HTTP Request to "
<< http_hostname << ":" << std::to_string(http_port)
<< " failed: "
<< req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

EXPECT_EQ(req.get_response_code(), 204u);
Expand Down Expand Up @@ -327,13 +345,17 @@ TEST_F(RestMockServerTest, put_globals_and_read_back) {
<< http_hostname << ":" << std::to_string(http_port)
<< " failed (early): "
<< put_req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

ASSERT_GT(put_req.get_response_code(), 0u)
<< "HTTP Request to "
<< http_hostname << ":" << std::to_string(http_port)
<< " failed: "
<< put_req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

EXPECT_EQ(put_req.get_response_code(), 204u);
Expand All @@ -350,13 +372,18 @@ TEST_F(RestMockServerTest, put_globals_and_read_back) {
<< http_hostname << ":" << std::to_string(http_port)
<< " failed (early): "
<< get_req.error_msg()
<< std::endl;
<< std::endl
<< server_mock.get_full_output()
<< std::endl
;

ASSERT_GT(get_req.get_response_code(), 0u)
<< "HTTP Request to "
<< http_hostname << ":" << std::to_string(http_port)
<< " failed: "
<< get_req.error_msg()
<< std::endl
<< server_mock.get_full_output()
<< std::endl;

EXPECT_EQ(get_req.get_response_code(), 200u);
Expand All @@ -370,7 +397,7 @@ TEST_F(RestMockServerTest, put_globals_and_read_back) {

std::string json_payload(get_resp_body_content.begin(), get_resp_body_content.end());

rapidjson::Document json_doc;
JsonDocument json_doc;
json_doc.Parse(json_payload.c_str());

EXPECT_TRUE(!json_doc.HasParseError());
Expand Down

0 comments on commit a48037d

Please sign in to comment.