Skip to content

Commit

Permalink
Fix asan of overflow of chrono
Browse files Browse the repository at this point in the history
Signed-off-by: owentou <[email protected]>
  • Loading branch information
owent committed Feb 14, 2022
1 parent a273ac3 commit 1cafb98
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 26 deletions.
111 changes: 89 additions & 22 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,16 @@ class ResponseHandler : public http_client::EventHandler
// If any failure event occurs, release the condition variable to unblock main thread
switch (state)
{
case http_client::SessionState::CreateFailed:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: session create failed");
break;
case http_client::SessionState::CreateFailed: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: session create failed.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

case http_client::SessionState::Created:
if (console_debug_)
Expand All @@ -176,9 +183,16 @@ class ResponseHandler : public http_client::EventHandler
}
break;

case http_client::SessionState::ConnectFailed:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: connection failed");
break;
case http_client::SessionState::ConnectFailed: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: connection failed.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

case http_client::SessionState::Connected:
if (console_debug_)
Expand All @@ -194,9 +208,16 @@ class ResponseHandler : public http_client::EventHandler
}
break;

case http_client::SessionState::SendFailed:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: request send failed");
break;
case http_client::SessionState::SendFailed: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: request send failed.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

case http_client::SessionState::Response:
if (console_debug_)
Expand All @@ -205,17 +226,38 @@ class ResponseHandler : public http_client::EventHandler
}
break;

case http_client::SessionState::SSLHandshakeFailed:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: SSL handshake failed");
break;
case http_client::SessionState::SSLHandshakeFailed: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: SSL handshake failed.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

case http_client::SessionState::TimedOut:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: request time out");
break;
case http_client::SessionState::TimedOut: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: request time out.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

case http_client::SessionState::NetworkError:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: network error");
break;
case http_client::SessionState::NetworkError: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: network error.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

case http_client::SessionState::ReadError:
if (console_debug_)
Expand All @@ -231,9 +273,16 @@ class ResponseHandler : public http_client::EventHandler
}
break;

case http_client::SessionState::Cancelled:
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Session state: (manually) cancelled\n");
break;
case http_client::SessionState::Cancelled: {
std::stringstream error_message;
error_message << "[OTLP HTTP Client] Session state: (manually) cancelled.";
if (!reason.empty())
{
error_message.write(reason.data(), reason.size());
}
OTEL_INTERNAL_LOG_ERROR(error_message.str());
}
break;

default:
break;
Expand Down Expand Up @@ -753,9 +802,27 @@ bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept
http_client_->FinishAllSessions();
}

// ASAN will report chrono: runtime error: signed integer overflow: A + B cannot be represented
// in type 'long int' here. So we reset timeout to meet signed long int limit here.
if (timeout > std::chrono::microseconds::zero())
{
auto max_timeout = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::time_point::max() - std::chrono::steady_clock::now());
if (timeout >= max_timeout)
{
timeout = std::chrono::microseconds::zero();
}
max_timeout = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::time_point::max() - std::chrono::system_clock::now());
if (timeout >= max_timeout)
{
timeout = std::chrono::microseconds::zero();
}
}

// Wait for all the sessions to finish
std::unique_lock<std::mutex> lock(session_waker_lock_);
if (timeout == std::chrono::microseconds::zero())
if (timeout > std::chrono::microseconds::zero())
{
session_waker_.wait(lock, [this] {
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/test/otlp_http_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test, public HTTP_SERVER_NS::
{
return;
}
int port = server_.addListeningPort(14371);
int port = server_.addListeningPort(10371);
std::ostringstream os;
os << "localhost:" << port;
server_address_ = "http://" + os.str() + "/v1/traces";
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/test/otlp_http_log_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test,
{
return;
}
int port = server_.addListeningPort(14372);
int port = server_.addListeningPort(10372);
std::ostringstream os;
os << "localhost:" << port;
server_address_ = "http://" + os.str() + "/v1/logs";
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/logs/batch_log_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool BatchLogProcessor::Shutdown(std::chrono::microseconds timeout) noexcept
worker_thread_.join();
if (exporter_ != nullptr)
{
return exporter_->Shutdown();
return exporter_->Shutdown(timeout);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/trace/batch_span_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ bool BatchSpanProcessor::Shutdown(std::chrono::microseconds timeout) noexcept
worker_thread_.join();
if (exporter_ != nullptr)
{
return exporter_->Shutdown();
return exporter_->Shutdown(timeout);
}

return true;
Expand Down

0 comments on commit 1cafb98

Please sign in to comment.