Files
node/test/cctest/test_trace_event.cc
Khaidi Chu 99f0ddef2e src: avoid union type-punning in trace values
Replace TraceValueUnion with memcpy-based object representation copying
to avoid reading inactive union members. Generalize integral values to
support all integral and enum types.

This follows the equivalent V8 fix in CL 1886918.

Refs: https://chromium-review.googlesource.com/c/v8/v8/+/1886918
Fixes: https://github.com/nodejs/node/issues/57033
Signed-off-by: XadillaX <i@2333.moe>
PR-URL: https://github.com/nodejs/node/pull/65933
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-09-17 13:43:58 +00:00

70 lines
1.8 KiB
C++

#include "gtest/gtest.h"
#ifndef V8_USE_PERFETTO
#include <cstdint>
#include <cstring>
#include <type_traits>
#include "tracing/trace_event.h"
namespace {
template <typename T>
T DecodeTraceValue(uint64_t value) {
static_assert(std::is_trivially_copyable_v<T>);
T result{};
std::memcpy(&result, &value, sizeof(result));
return result;
}
enum class TestEnum : uint16_t {
kValue = 42,
};
TEST(TraceEvent, SetIntegralTraceValue) {
unsigned char type;
uint64_t value;
node::tracing::SetTraceValue(false, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_BOOL, type);
EXPECT_EQ(uint64_t{0}, value);
node::tracing::SetTraceValue(-42, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_INT, type);
EXPECT_EQ(static_cast<uint64_t>(-42), value);
node::tracing::SetTraceValue(TestEnum::kValue, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_UINT, type);
EXPECT_EQ(uint64_t{42}, value);
}
TEST(TraceEvent, SetNonIntegralTraceValue) {
unsigned char type;
uint64_t value;
const double number = 1.25;
node::tracing::SetTraceValue(number, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_DOUBLE, type);
EXPECT_EQ(number, DecodeTraceValue<double>(value));
const void* pointer = &value;
node::tracing::SetTraceValue(pointer, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_POINTER, type);
EXPECT_EQ(pointer, DecodeTraceValue<const void*>(value));
const char* string = "trace value";
node::tracing::SetTraceValue(string, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_STRING, type);
EXPECT_EQ(string, DecodeTraceValue<const char*>(value));
const node::tracing::TraceStringWithCopy copied_string(string);
node::tracing::SetTraceValue(copied_string, &type, &value);
EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, type);
EXPECT_EQ(string, DecodeTraceValue<const char*>(value));
}
} // namespace
#endif // V8_USE_PERFETTO