open-telemetry / opentelemetry-cpp

The OpenTelemetry C++ Client
https://opentelemetry.io/
Apache License 2.0
849 stars 403 forks source link

Runtime error of string memory allocating #2256

Closed ivgotcrazy closed 1 year ago

ivgotcrazy commented 1 year ago

Describe your environment

Windows/C++17/Visual Studio

CMake parameters as below: image

Steps to reproduce

simple code as below, just call the GetTracer method:

namespace trace = opentelemetry::trace; typedef nostd::shared_ptr TracerNSP;

TracerNSP GetTracer(const std::string& tracer_name) { auto provider = trace::Provider::GetTracerProvider(); return provider->GetTracer(tracer_name, OPENTELEMETRY_SDK_VERSION); }

What is the expected behavior? Success

What is the actual behavior?

A runtime error was triggered:

image

ucrtbased.dll!00007fff30112e65()    Unknown
ucrtbased.dll!00007fff30113003()    Unknown
ucrtbased.dll!00007fff3012ab0d()    Unknown
ucrtbased.dll!00007fff30129a90()    Unknown
vcruntime140_1d.dll!00007fff4cfd223a()  Unknown
vcruntime140_1d.dll!00007fff4cfd2ec5()  Unknown
vcruntime140_1d.dll!00007fff4cfd2f57()  Unknown
vcruntime140_1d.dll!00007fff4cfd6ddb()  Unknown
tracer.dll!__GSHandlerCheck_EH4(_EXCEPTION_RECORD * ExceptionRecord, void * EstablisherFrame, _CONTEXT * ContextRecord, _DISPATCHER_CONTEXT * DispatcherContext) Line 73    C++
ntdll.dll!00007fff6f8923af()    Unknown
ntdll.dll!00007fff6f8414b4()    Unknown
ntdll.dll!00007fff6f890ebe()    Unknown
KernelBase.dll!00007fff6d1acf19()   Unknown
vcruntime140d.dll!00007fff3762b760()    Unknown
tracer.dll!__scrt_throw_std_bad_alloc() Line 36 C++
tracer.dll!operator new(unsigned __int64 size) Line 53  C++
tracer.dll!std::_Default_allocate_traits::_Allocate(const unsigned __int64 _Bytes) Line 78  C++
tracer.dll!std::_Allocate_manually_vector_aligned<std::_Default_allocate_traits>(const unsigned __int64 _Bytes) Line 134    C++
tracer.dll!std::_Allocate<16,std::_Default_allocate_traits,0>(const unsigned __int64 _Bytes) Line 229   C++
tracer.dll!std::allocator<char>::allocate(const unsigned __int64 _Count) Line 835   C++
tracer.dll!std::string::_Construct<1,char const *>(const char * const _Arg, const unsigned __int64 _Count) Line 2771    C++
tracer.dll!std::string::basic_string<char,std::char_traits<char>,std::allocator<char>><std::basic_string_view<char,std::char_traits<char>>,0>(const std::basic_string_view<char,std::char_traits<char>> & _Right, const std::allocator<char> & _Al) Line 3026   C++

tracer.dll!opentelemetry::v1::sdk::instrumentationscope::InstrumentationScope::InstrumentationScope(std::basic_string_view<char,std::char_traits> name, std::basic_string_view<char,std::char_traits> version, std::basic_string_view<char,std::char_traits> schema_url, opentelemetry::v1::sdk::common::AttributeMap && attributes) Line 140 C++ tracer.dll!opentelemetry::v1::sdk::instrumentationscope::InstrumentationScope::Create(std::basic_string_view<char,std::char_traits> name, std::basic_string_view<char,std::char_traits> version, std::basic_string_view<char,std::char_traits> schema_url, opentelemetry::v1::sdk::common::AttributeMap && attributes) Line 44 C++ tracer.dll!opentelemetry::v1::sdk::trace::TracerProvider::GetTracer(std::basic_string_view<char,std::char_traits> library_name, std::basic_string_view<char,std::char_traits> library_version, std::basic_string_view<char,std::char_traits> schema_url) Line 82 C++ tracer.dll!`anonymous namespace'::GetTracer(const std::string & tracer_name) Line 43 C++ tracer.dll!jukey::com::TraceMgr::StartSpan(const std::string & span_name) Line 144 C++ test-tracer.exe!main(int argc, char argv) Line 33 C++ [External Code]

Additional context image

ivgotcrazy commented 1 year ago

This problem has been bothering me for a long time. It looks like a binary compatibility issue. Do I need to set a certain compilation option? Does anybody has good ideas or suggestions?

ThomsonTan commented 1 year ago

@ivgotcrazy is this still valid? If yes, is it possible to share the dump file and .pdb files for the related component?

marcalff commented 1 year ago

A possible reason to have binary compatibility issues is related to:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")

opentelemetry-cpp sets this flag when building the library.

An application compiling with the opentelemetry-cpp header files also needs to set this flag.

When not set, the build will contain C++98 code compiled in the application, linked with C++11/14/17/20/23 code depending on the compiler used, when linking the SDK.

Mixing different implementations is likely to produce a corrupt binary, leading to crashes.

marcalff commented 1 year ago

Closing.

Binary compatibility issue is suspected to be caused by missing option /Zc:__cplusplus in Visual Studio.