Skip to content

Commit

Permalink
Put bit_cast<>() into the base:: namespace.
Browse files Browse the repository at this point in the history
This helps prevent ambiguous symbol issues in C++20 and should have been
the case anyway.

Bug: 1284275
Change-Id: I86210c7b1c8133e8c872930e9e1fdf86b663e104
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3615258
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Hongchan Choi <hongchan@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Owners-Override: Lei Zhang <thestig@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/main@{#998789}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed May 3, 2022
1 parent 7168ba9 commit cc88ac0
Show file tree
Hide file tree
Showing 37 changed files with 228 additions and 169 deletions.
4 changes: 4 additions & 0 deletions base/bit_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <string.h> // memcpy
#endif

namespace base {

// This is C++20's std::bit_cast<>(). It morally does what
// `*reinterpret_cast<Dest*>(&source)` does, but the cast/deref pair is
// undefined behavior, while bit_cast<>() isn't.
Expand Down Expand Up @@ -41,4 +43,6 @@ inline
#endif
}

} // namespace base

#endif // BASE_BIT_CAST_H_
2 changes: 1 addition & 1 deletion base/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ class BASE_EXPORT GSL_OWNER Value {
// SomeFunction(member);
// }
// });
operator double() const { return bit_cast<double>(v_); }
operator double() const { return base::bit_cast<double>(v_); }

private:
friend bool operator==(const DoubleStorage& lhs, const DoubleStorage& rhs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ TEST_F(TriggeredProfileResetterTest, HasDuplicateResetTrigger) {
FILETIME ft = {};
SetRegTimestampAndToolName(std::wstring(), &ft);
profile_->GetPrefs()->SetInt64(prefs::kLastProfileResetTimestamp,
bit_cast<int64_t, FILETIME>(ft));
base::bit_cast<int64_t, FILETIME>(ft));

TriggeredProfileResetter triggered_profile_resetter(profile_.get());
triggered_profile_resetter.Activate();
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/task_manager/sampling/shared_sampler_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const ProcessData* SeekInPreviousSnapshot(

// A wrapper function converting ticks (in units of 100 ns) to Time.
base::Time ConvertTicksToTime(uint64_t ticks) {
FILETIME ft = bit_cast<FILETIME, uint64_t>(ticks);
FILETIME ft = base::bit_cast<FILETIME, uint64_t>(ticks);
return base::Time::FromFileTime(ft);
}

Expand Down
3 changes: 2 additions & 1 deletion chromeos/hugepage_text/hugepage_text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ static int FilterElfHeader(struct dl_phdr_info* info, size_t size, void* data) {
for (int i = 0; i < info->dlpi_phnum; i++) {
if (info->dlpi_phdr[i].p_type == PT_LOAD &&
info->dlpi_phdr[i].p_flags == (PF_R | PF_X)) {
vaddr = bit_cast<void*>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
vaddr =
base::bit_cast<void*>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
segsize = info->dlpi_phdr[i].p_filesz;
RemapHugetlbTextWithOrderfileLayout(vaddr, segsize);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion chromeos/memory/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int ParseElfHeaderAndMlockBinaryText(struct dl_phdr_info* info,
if (info->dlpi_phdr[i].p_type == PT_LOAD &&
info->dlpi_phdr[i].p_flags == (PF_R | PF_X)) {
void* vaddr =
bit_cast<void*>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
base::bit_cast<void*>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
size_t segsize = info->dlpi_phdr[i].p_filesz;

ssize_t max_lockable_size = kCrOSLockMainProgramTextMaxSize.Get();
Expand Down
2 changes: 1 addition & 1 deletion components/assist_ranker/ranker_example_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int32_t StringToIntBits(const std::string& str) {
int32_t FloatToIntBits(float f) {
if (std::numeric_limits<float>::is_iec559) {
// Directly bit_cast if float follows ieee754 standard.
return bit_cast<int32_t>(f);
return base::bit_cast<int32_t>(f);
} else {
// Otherwise, manually calculate sign, exp and mantissa.
// For sign.
Expand Down
2 changes: 1 addition & 1 deletion components/autofill_assistant/browser/parse_jspb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool AppendFieldValue(const std::string& jspb_id_prefix,
// Encode these as floats (in a fixed32)
WriteTag(field_tag, WIRETYPE_FIXED32, out);
out->WriteLittleEndian32(
bit_cast<uint32_t>(static_cast<float>(value.GetDouble())));
base::bit_cast<uint32_t>(static_cast<float>(value.GetDouble())));
break;

case base::Value::Type::STRING: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ bool SegmentationUkmHelper::AddOutputsToUkm(
// static
int64_t SegmentationUkmHelper::FloatToInt64(float f) {
// Encode the float number in IEEE754 double precision.
return bit_cast<int64_t>(static_cast<double>(f));
return base::bit_cast<int64_t>(static_cast<double>(f));
}

} // namespace segmentation_platform
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace {
static const double kRoundingError = 1E-5;

float Int64ToFloat(int64_t encoded) {
return static_cast<float>(bit_cast<double>(encoded));
return static_cast<float>(base::bit_cast<double>(encoded));
}

void CompareEncodeDecodeDifference(float tensor) {
Expand Down
2 changes: 1 addition & 1 deletion components/visitedlink/common/visitedlink_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ VisitedLinkCommon::Fingerprint VisitedLinkCommon::ComputeURLFingerprint(
// on arbitrary alignment on some processors. This reinterpret_casts it
// down to a char array of the same size as fingerprint, and then does the
// bit cast, which amounts to a memcpy. This does not handle endian issues.
return bit_cast<Fingerprint, uint8_t[8]>(
return base::bit_cast<Fingerprint, uint8_t[8]>(
*reinterpret_cast<uint8_t(*)[8]>(&digest.a));
}

Expand Down
8 changes: 4 additions & 4 deletions components/viz/common/quads/render_pass_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ base::Value FilterOperationToDict(const cc::FilterOperation& filter) {
dict.SetKey("drop_shadow_offset",
PointToDict(filter.drop_shadow_offset()));
dict.SetIntKey("drop_shadow_color",
bit_cast<int>(filter.drop_shadow_color()));
base::bit_cast<int>(filter.drop_shadow_color()));
break;
case cc::FilterOperation::REFERENCE:
dict.SetStringKey("image_filter",
Expand Down Expand Up @@ -540,7 +540,7 @@ bool FilterOperationFromDict(const base::Value& dict,
}
filter.set_drop_shadow_offset(offset);
filter.set_drop_shadow_color(
bit_cast<SkColor>(drop_shadow_color.value()));
base::bit_cast<SkColor>(drop_shadow_color.value()));
} break;
case cc::FilterOperation::REFERENCE:
if (!image_filter)
Expand Down Expand Up @@ -1179,7 +1179,7 @@ void SurfaceDrawQuadToDict(const SurfaceDrawQuad* draw_quad,
DCHECK(dict);
dict->SetKey("surface_range", SurfaceRangeToDict(draw_quad->surface_range));
dict->SetIntKey("default_background_color",
bit_cast<int>(draw_quad->default_background_color));
base::bit_cast<int>(draw_quad->default_background_color));
dict->SetBoolKey("stretch_content",
draw_quad->stretch_content_to_fill_bounds);
dict->SetBoolKey("is_reflection", draw_quad->is_reflection);
Expand Down Expand Up @@ -1424,7 +1424,7 @@ bool SurfaceDrawQuadFromDict(const base::Value& dict,

draw_quad->SetAll(common.shared_quad_state, common.rect, common.visible_rect,
common.needs_blending, *surface_range,
bit_cast<SkColor>(*default_background_color),
base::bit_cast<SkColor>(*default_background_color),
*stretch_content, *is_reflection, *allow_merge);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ double ToDouble(uint64_t state0) {
// Exponent for double values for [1.0 .. 2.0)
static const uint64_t kExponentBits = uint64_t{0x3FF0000000000000};
uint64_t random = (state0 >> 12) | kExponentBits;
return bit_cast<double>(random) - 1;
return base::bit_cast<double>(random) - 1;
}

} // namespace
Expand Down
4 changes: 2 additions & 2 deletions content/browser/web_package/web_bundle_blob_data_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ void OnReadComplete(web_package::mojom::BundleDataSource::ReadCallback callback,
return;
}
std::vector<uint8_t> vec;
vec.assign(bit_cast<uint8_t*>(io_buf->data()),
bit_cast<uint8_t*>(io_buf->data()) + bytes_read);
vec.assign(base::bit_cast<uint8_t*>(io_buf->data()),
base::bit_cast<uint8_t*>(io_buf->data()) + bytes_read);
std::move(callback).Run(std::move(vec));
}

Expand Down
5 changes: 3 additions & 2 deletions gin/v8_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class TimeClamper {

private:
inline double ThresholdFor(double clamped_time) const {
uint64_t time_hash = MurmurHash3(bit_cast<int64_t>(clamped_time) ^ secret_);
uint64_t time_hash =
MurmurHash3(base::bit_cast<int64_t>(clamped_time) ^ secret_);
return clamped_time + kResolutionSeconds * ToDouble(time_hash);
}

Expand All @@ -166,7 +167,7 @@ class TimeClamper {
static const uint64_t kExponentBits = uint64_t{0x3FF0000000000000};
static const uint64_t kMantissaMask = uint64_t{0x000FFFFFFFFFFFFF};
uint64_t random = (value & kMantissaMask) | kExponentBits;
return bit_cast<double>(random) - 1;
return base::bit_cast<double>(random) - 1;
}

static inline uint64_t MurmurHash3(uint64_t value) {
Expand Down
8 changes: 4 additions & 4 deletions gpu/command_buffer/tests/gl_readback_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static float HalfToFloat32(uint16_t value) {
if (e == 0) {
if (m == 0) {
uint32_t result = s << 31;
return bit_cast<float>(result);
return base::bit_cast<float>(result);
} else {
while (!(m & 0x00000400)) {
m <<= 1;
Expand All @@ -111,18 +111,18 @@ static float HalfToFloat32(uint16_t value) {
} else if (e == 31) {
if (m == 0) {
uint32_t result = (s << 31) | 0x7f800000;
return bit_cast<float>(result);
return base::bit_cast<float>(result);
} else {
uint32_t result = (s << 31) | 0x7f800000 | (m << 13);
return bit_cast<float>(result);
return base::bit_cast<float>(result);
}
}

e = e + (127 - 15);
m = m << 13;

uint32_t result = (s << 31) | (e << 23) | m;
return bit_cast<float>(result);
return base::bit_cast<float>(result);
}

static GLuint CompileShader(GLenum type, const char *data) {
Expand Down
5 changes: 3 additions & 2 deletions gpu/ipc/service/gpu_watchdog_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,9 @@ base::ThreadTicks GpuWatchdogThread::GetWatchedThreadTime() {

// Need to bit_cast to fix alignment, then divide by 10 to convert
// 100-nanoseconds to microseconds.
int64_t user_time_us = bit_cast<int64_t, FILETIME>(user_time) / 10;
int64_t kernel_time_us = bit_cast<int64_t, FILETIME>(kernel_time) / 10;
int64_t user_time_us = base::bit_cast<int64_t, FILETIME>(user_time) / 10;
int64_t kernel_time_us =
base::bit_cast<int64_t, FILETIME>(kernel_time) / 10;

return base::ThreadTicks() +
base::Microseconds(user_time_us + kernel_time_us);
Expand Down
4 changes: 2 additions & 2 deletions net/filter/brotli_source_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class BrotliSourceStream : public FilterSourceStream {
if (decoding_status_ != DecodingStatus::DECODING_IN_PROGRESS)
return ERR_CONTENT_DECODING_FAILED;

const uint8_t* next_in = bit_cast<uint8_t*>(input_buffer->data());
const uint8_t* next_in = base::bit_cast<uint8_t*>(input_buffer->data());
size_t available_in = input_buffer_size;
uint8_t* next_out = bit_cast<uint8_t*>(output_buffer->data());
uint8_t* next_out = base::bit_cast<uint8_t*>(output_buffer->data());
size_t available_out = output_buffer_size;

BrotliDecoderResult result =
Expand Down
4 changes: 2 additions & 2 deletions net/filter/filter_source_stream_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void CompressGzip(const char* source,
dest_left -= sizeof(gzip_header);
}

zlib_stream.next_in = bit_cast<Bytef*>(source);
zlib_stream.next_in = base::bit_cast<Bytef*>(source);
zlib_stream.avail_in = source_len;
zlib_stream.next_out = bit_cast<Bytef*>(dest);
zlib_stream.next_out = base::bit_cast<Bytef*>(dest);
zlib_stream.avail_out = dest_left;

code = deflate(&zlib_stream, Z_FINISH);
Expand Down
14 changes: 8 additions & 6 deletions net/filter/gzip_source_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ int GzipSourceStream::FilterData(IOBuffer* output_buffer,
case STATE_SNIFFING_DEFLATE_HEADER: {
DCHECK_EQ(TYPE_DEFLATE, type());

zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_data);
zlib_stream_.get()->next_in = base::bit_cast<Bytef*>(input_data);
zlib_stream_.get()->avail_in = input_data_size;
zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data());
zlib_stream_.get()->next_out =
base::bit_cast<Bytef*>(output_buffer->data());
zlib_stream_.get()->avail_out = output_buffer_size;

int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH);
Expand Down Expand Up @@ -212,9 +213,10 @@ int GzipSourceStream::FilterData(IOBuffer* output_buffer,
DCHECK_LE(0, input_data_size);

state_compressed_entered = true;
zlib_stream_.get()->next_in = bit_cast<Bytef*>(input_data);
zlib_stream_.get()->next_in = base::bit_cast<Bytef*>(input_data);
zlib_stream_.get()->avail_in = input_data_size;
zlib_stream_.get()->next_out = bit_cast<Bytef*>(output_buffer->data());
zlib_stream_.get()->next_out =
base::bit_cast<Bytef*>(output_buffer->data());
zlib_stream_.get()->avail_out = output_buffer_size;

int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH);
Expand Down Expand Up @@ -257,9 +259,9 @@ bool GzipSourceStream::InsertZlibHeader() {
char dummy_output[4];

inflateReset(zlib_stream_.get());
zlib_stream_.get()->next_in = bit_cast<Bytef*>(&dummy_header[0]);
zlib_stream_.get()->next_in = base::bit_cast<Bytef*>(&dummy_header[0]);
zlib_stream_.get()->avail_in = sizeof(dummy_header);
zlib_stream_.get()->next_out = bit_cast<Bytef*>(&dummy_output[0]);
zlib_stream_.get()->next_out = base::bit_cast<Bytef*>(&dummy_output[0]);
zlib_stream_.get()->avail_out = sizeof(dummy_output);

int ret = inflate(zlib_stream_.get(), Z_NO_FLUSH);
Expand Down
14 changes: 8 additions & 6 deletions sandbox/win/src/service_resolver_32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ bool ServiceResolverThunk::IsFunctionAService(void* local_thunk) const {
if (kCallEdx != function_code.call_ptr_edx) {
DWORD ki_system_call;
if (!::ReadProcessMemory(process_,
bit_cast<const void*>(function_code.stub),
base::bit_cast<const void*>(function_code.stub),
&ki_system_call, sizeof(ki_system_call), &read)) {
return false;
}
Expand All @@ -257,7 +257,7 @@ bool ServiceResolverThunk::IsFunctionAService(void* local_thunk) const {
// last check, call_stub should point to a KiXXSystemCall function on ntdll
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
bit_cast<const wchar_t*>(ki_system_call),
base::bit_cast<const wchar_t*>(ki_system_call),
&module_1)) {
return false;
}
Expand Down Expand Up @@ -299,7 +299,8 @@ NTSTATUS ServiceResolverThunk::PerformPatch(void* local_thunk,
intercepted_code.mov_eax = kMovEax;
intercepted_code.service_id = full_local_thunk->original.service_id;
intercepted_code.mov_edx = kMovEdx;
intercepted_code.stub = bit_cast<ULONG>(&full_remote_thunk->internal_thunk);
intercepted_code.stub =
base::bit_cast<ULONG>(&full_remote_thunk->internal_thunk);
intercepted_code.call_ptr_edx = kJmpEdx;
bytes_to_write = kMinServiceSize;

Expand Down Expand Up @@ -357,7 +358,8 @@ bool ServiceResolverThunk::SaveOriginalFunction(void* local_thunk,
ULONG relative = function_code.service_id;

// First, fix our copy of their patch.
relative += bit_cast<ULONG>(target_) - bit_cast<ULONG>(remote_thunk);
relative +=
base::bit_cast<ULONG>(target_) - base::bit_cast<ULONG>(remote_thunk);

function_code.service_id = relative;

Expand All @@ -367,8 +369,8 @@ bool ServiceResolverThunk::SaveOriginalFunction(void* local_thunk,

const ULONG kJmp32Size = 5;

relative_jump_ = bit_cast<ULONG>(&full_thunk->internal_thunk) -
bit_cast<ULONG>(target_) - kJmp32Size;
relative_jump_ = base::bit_cast<ULONG>(&full_thunk->internal_thunk) -
base::bit_cast<ULONG>(target_) - kJmp32Size;
}

// Save the verified code
Expand Down
4 changes: 2 additions & 2 deletions sandbox/win/src/service_resolver_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void CheckJump(void* source, void* target) {
Code* patched = reinterpret_cast<Code*>(source);
EXPECT_EQ(kJump32, patched->jump);

ULONG source_addr = bit_cast<ULONG>(source);
ULONG target_addr = bit_cast<ULONG>(target);
ULONG source_addr = base::bit_cast<ULONG>(source);
ULONG target_addr = base::bit_cast<ULONG>(target);
EXPECT_EQ(target_addr + 19 - source_addr, patched->delta);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion services/data_decoder/gzipper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void Gzipper::Deflate(mojo_base::BigBuffer data, DeflateCallback callback) {
std::vector<uint8_t> compressed_data(compressed_data_size);
if (zlib_internal::CompressHelper(
zlib_internal::ZRAW, compressed_data.data(), &compressed_data_size,
bit_cast<const Bytef*>(data.data()), data.size(),
base::bit_cast<const Bytef*>(data.data()), data.size(),
Z_DEFAULT_COMPRESSION,
/*malloc_fn=*/nullptr, /*free_fn=*/nullptr) != Z_OK) {
std::move(callback).Run(absl::nullopt);
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/timing/time_clamper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ inline double TimeClamper::ToDouble(uint64_t value) {
static const uint64_t kExponentBits = uint64_t{0x3FF0000000000000};
static const uint64_t kMantissaMask = uint64_t{0x000FFFFFFFFFFFFF};
uint64_t random = (value & kMantissaMask) | kExponentBits;
return bit_cast<double>(random) - 1;
return base::bit_cast<double>(random) - 1;
}

// static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void TestInvalidStaticImage(const char* avif_file, ErrorPhase error_phase) {
float HalfFloatToUnorm(uint16_t h) {
const uint32_t f = ((h & 0x8000) << 16) | (((h & 0x7c00) + 0x1c000) << 13) |
((h & 0x03ff) << 13);
return bit_cast<float>(f);
return base::bit_cast<float>(f);
}

void ReadYUV(const char* file_name,
Expand Down
6 changes: 4 additions & 2 deletions third_party/blink/renderer/platform/wtf/hash_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ struct IntHash {
template <typename T>
struct FloatHash {
typedef typename IntTypes<sizeof(T)>::UnsignedType Bits;
static unsigned GetHash(T key) { return HashInt(bit_cast<Bits>(key)); }
static bool Equal(T a, T b) { return bit_cast<Bits>(a) == bit_cast<Bits>(b); }
static unsigned GetHash(T key) { return HashInt(base::bit_cast<Bits>(key)); }
static bool Equal(T a, T b) {
return base::bit_cast<Bits>(a) == base::bit_cast<Bits>(b);
}
static const bool safe_to_compare_to_empty_or_deleted = true;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'base::WrapRefCounted',
'base::WritableSharedMemoryMapping',
'base::as_bytes',
'base::bit_cast',
'base::in_place',
'absl::make_optional',
'base::make_span',
Expand Down
Loading

0 comments on commit cc88ac0

Please sign in to comment.