diff --git a/CMakeLists.txt b/CMakeLists.txt index dddf61585..089840f94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,6 +239,8 @@ if (WIN32) target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_windows.cpp) elseif (APPLE) target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_darwin.cpp) +elseif (CMAKE_SYSTEM_NAME STREQUAL "AIX") + target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_aix.cpp) elseif (UNIX) target_sources(dd-trace-cpp-objects PRIVATE src/datadog/platform_util_unix.cpp) else () diff --git a/cmake/compiler/gcc.cmake b/cmake/compiler/gcc.cmake index c0b02b7ce..0c9a30fce 100644 --- a/cmake/compiler/gcc.cmake +++ b/cmake/compiler/gcc.cmake @@ -27,10 +27,9 @@ target_compile_options(dd-trace-cpp-specs -pedantic ) -target_link_options(dd-trace-cpp-specs - INTERFACE - -Wl,-z,noexecstack -) +if(NOT CMAKE_SYSTEM_NAME STREQUAL "AIX") + target_link_options(dd-trace-cpp-specs INTERFACE -Wl,-z,noexecstack) +endif() if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13) target_compile_options(dd-trace-cpp-specs diff --git a/src/datadog/platform_util.h b/src/datadog/platform_util.h index 85677cf35..417423da4 100644 --- a/src/datadog/platform_util.h +++ b/src/datadog/platform_util.h @@ -16,6 +16,8 @@ # define DD_SDK_CPU_ARCH "x86" #elif defined(__aarch64__) || defined(_M_ARM64) # define DD_SDK_CPU_ARCH "arm64" +#elif defined(_AIX) && defined(_ARCH_PPC64) +# define DD_SDK_CPU_ARCH "ppc64" #else # define DD_SDK_CPU_ARCH "unknown" #endif diff --git a/src/datadog/platform_util_aix.cpp b/src/datadog/platform_util_aix.cpp new file mode 100644 index 000000000..c9ef931a9 --- /dev/null +++ b/src/datadog/platform_util_aix.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include "platform_util.h" + +namespace datadog { +namespace tracing { + +HostInfo get_host_info() { + struct utsname system_info{}; + HostInfo result; + if (uname(&system_info) != 0) return result; + result.os = "AIX"; + result.os_version = + std::string(system_info.version) + "." + system_info.release; + result.hostname = system_info.nodename; + result.cpu_architecture = DD_SDK_CPU_ARCH; + result.kernel_name = system_info.sysname; + result.kernel_version = system_info.version; + result.kernel_release = system_info.release; + return result; +} + +std::string get_hostname() { return get_host_info().hostname; } +int get_process_id() { return ::getpid(); } +std::string get_process_name() { + std::ifstream process_info("/proc/" + std::to_string(getpid()) + "/psinfo", + std::ios::binary); + psinfo_t info{}; + if (!process_info.read(reinterpret_cast(&info), sizeof(info))) { + return "unknown-service"; + } + return std::string(info.pr_fname, + strnlen(info.pr_fname, sizeof(info.pr_fname))); +} +Optional get_process_path() { return nullopt; } + +int at_fork_in_child(void (*on_fork)()) { + return pthread_atfork(nullptr, nullptr, on_fork); +} + +InMemoryFile::InMemoryFile(void* handle) : handle_(handle) {} +InMemoryFile::~InMemoryFile() {} +InMemoryFile::InMemoryFile(InMemoryFile&& other) { + std::swap(handle_, other.handle_); +} +InMemoryFile& InMemoryFile::operator=(InMemoryFile&& other) { + std::swap(handle_, other.handle_); + return *this; +} +bool InMemoryFile::write_then_seal(const std::string&) { return false; } +Expected InMemoryFile::make(StringView) { + return Error{Error::Code::NOT_IMPLEMENTED, + "AIX has no sealed in-memory files"}; +} + +namespace container { +Optional find_container_id(std::istream&) { return nullopt; } +Optional get_id() { return nullopt; } +} // namespace container + +} // namespace tracing +} // namespace datadog diff --git a/src/datadog/random.cpp b/src/datadog/random.cpp index 6d52df538..e0387f3ec 100644 --- a/src/datadog/random.cpp +++ b/src/datadog/random.cpp @@ -3,6 +3,12 @@ #include #include +#if defined(_AIX) +#include + +#include +#endif + #include "hex.h" #include "platform_util.h" @@ -34,13 +40,52 @@ class Uint64Generator { void seed_with_random() { generator_.seed(std::random_device{}()); } }; +#if defined(_AIX) +// The GNU AIX linker misallocates hidden C++ TLS common symbols. +class GeneratorStorage { + pthread_key_t key_; + + public: + GeneratorStorage() { + if (pthread_key_create(&key_, [](void* value) { + delete static_cast(value); + }) != 0) { + std::abort(); + } + } + + ~GeneratorStorage() { + delete static_cast(pthread_getspecific(key_)); + pthread_key_delete(key_); + } + + Uint64Generator& get() { + auto* generator = static_cast(pthread_getspecific(key_)); + if (!generator) { + generator = new Uint64Generator; + if (pthread_setspecific(key_, generator) != 0) { + delete generator; + std::abort(); + } + } + return *generator; + } +}; + +Uint64Generator& get_generator() { + static GeneratorStorage generators; + return generators.get(); +} +#else thread_local Uint64Generator thread_local_generator; +Uint64Generator& get_generator() { return thread_local_generator; } +#endif -void on_fork() { thread_local_generator.seed_with_random(); } +void on_fork() { get_generator().seed_with_random(); } } // namespace -std::uint64_t random_uint64() { return thread_local_generator(); } +std::uint64_t random_uint64() { return get_generator()(); } std::string uuid() { // clang-format off diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2167866f2..53d326b16 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,6 +33,7 @@ add_executable(tests test_msgpack.cpp test_otel_process_ctx.cpp test_platform_util.cpp + test_random.cpp test_parse_util.cpp test_smoke.cpp test_span.cpp diff --git a/test/test_random.cpp b/test/test_random.cpp new file mode 100644 index 000000000..84797a983 --- /dev/null +++ b/test/test_random.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +#if defined(_AIX) || defined(__linux__) || defined(__APPLE__) +#include +#include +#endif + +#include "random.h" +#include "test.h" + +using namespace datadog::tracing; + +TEST_CASE("random generators survive thread exit", "[random]") { + std::array, 16> samples{}; + std::vector threads; + for (auto& sample : samples) { + threads.emplace_back([&sample] { + for (auto& value : sample) value = random_uint64(); + }); + } + for (auto& thread : threads) thread.join(); + std::set distinct; + for (const auto& sample : samples) { + distinct.insert(sample.begin(), sample.end()); + } + CHECK(distinct.size() == samples.size() * samples.front().size()); +} + +#if defined(_AIX) || defined(__linux__) || defined(__APPLE__) +TEST_CASE("random generator is reseeded after fork", "[random]") { + (void)random_uint64(); + int descriptors[2]; + REQUIRE(pipe(descriptors) == 0); + const pid_t child = fork(); + REQUIRE(child >= 0); + if (child == 0) { + close(descriptors[0]); + alarm(10); + const auto value = random_uint64(); + const auto written = write(descriptors[1], &value, sizeof(value)); + _exit(written == sizeof(value) ? 0 : 1); + } + close(descriptors[1]); + const auto parent_value = random_uint64(); + std::uint64_t child_value{}; + const auto received = read(descriptors[0], &child_value, sizeof(child_value)); + close(descriptors[0]); + int status{}; + REQUIRE(waitpid(child, &status, 0) == child); + REQUIRE(WIFEXITED(status)); + REQUIRE(WEXITSTATUS(status) == 0); + REQUIRE(received == sizeof(child_value)); + CHECK(child_value != parent_value); +} +#endif