Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
7 changes: 3 additions & 4 deletions cmake/compiler/gcc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/datadog/platform_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions src/datadog/platform_util_aix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <pthread.h>
#include <sys/procfs.h>
#include <sys/utsname.h>
#include <unistd.h>

#include <cstring>
#include <fstream>
#include <utility>

#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<char*>(&info), sizeof(info))) {
return "unknown-service";
}
return std::string(info.pr_fname,
strnlen(info.pr_fname, sizeof(info.pr_fname)));
}
Optional<std::filesystem::path> 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> InMemoryFile::make(StringView) {
return Error{Error::Code::NOT_IMPLEMENTED,
"AIX has no sealed in-memory files"};
}

namespace container {
Optional<std::string> find_container_id(std::istream&) { return nullopt; }
Optional<ContainerID> get_id() { return nullopt; }
} // namespace container

} // namespace tracing
} // namespace datadog
49 changes: 47 additions & 2 deletions src/datadog/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <bitset>
#include <random>

#if defined(_AIX)
#include <pthread.h>

#include <cstdlib>
#endif

#include "hex.h"
#include "platform_util.h"

Expand Down Expand Up @@ -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<Uint64Generator*>(value);
}) != 0) {
std::abort();
}
}

~GeneratorStorage() {
delete static_cast<Uint64Generator*>(pthread_getspecific(key_));
pthread_key_delete(key_);
}

Uint64Generator& get() {
auto* generator = static_cast<Uint64Generator*>(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
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions test/test_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <array>
#include <set>
#include <thread>
#include <vector>

#if defined(_AIX) || defined(__linux__) || defined(__APPLE__)
#include <sys/wait.h>
#include <unistd.h>
#endif

#include "random.h"
#include "test.h"

using namespace datadog::tracing;

TEST_CASE("random generators survive thread exit", "[random]") {
std::array<std::array<std::uint64_t, 16>, 16> samples{};
std::vector<std::thread> threads;
for (auto& sample : samples) {
threads.emplace_back([&sample] {
for (auto& value : sample) value = random_uint64();
});
}
for (auto& thread : threads) thread.join();
std::set<std::uint64_t> 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
Loading