dart-sdk/runtime/vm/datastream.cc
Tess Strickland f709bf12f0 Reland "[platform] Fix Utils::IsAbsoluteUint and rename to MagnitudeIsUint."
datastream.h is included in places where we cannot import the compiler
namespace, so we can't make compiler::target::word the type of
WriteTargetWord. That also would possibly silently truncate a
host-word sized value that was passed in instead of alerting to a
possible issue if those bits were important.

However, just using IsInt fails if the value being passed in is
a compiler::target::uword that is larger than the max value
that fits in a compiler::target::word. Instead, check for
truncation by checking the bit length of the value, which works
for both signed and unsigned target word values.

Original description:

Also remove previous (incorrect) uses in datastream.cc and
image_snapshot.cc, now that Utils::IsInt<T>(N, value) can now
be run for values of N >= sizeof(T).

Fixes https://github.com/dart-lang/sdk/issues/46572

TEST=language/generic/super_bounded_types_test passes on NNBD simarm,
     added value that triggered failure to vm/cc/MagnitudeIsUint

Cq-Include-Trybots: luci.dart.try:vm-kernel-nnbd-linux-release-simarm-try,vm-kernel-precomp-linux-debug-simarm_x64-try
Change-Id: Idbfdda9f28243d206d482a1d9ac7ae76a5022ad2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/206201
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Tess Strickland <sstrickl@google.com>
2021-07-08 14:22:54 +00:00

59 lines
1.8 KiB
C++

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/datastream.h"
#include "vm/compiler/runtime_api.h"
#include "vm/zone.h"
namespace dart {
void BaseWriteStream::WriteTargetWord(word value) {
ASSERT(Utils::BitLength(value) <= compiler::target::kBitsPerWord);
WriteFixed(static_cast<compiler::target::word>(value));
}
MallocWriteStream::~MallocWriteStream() {
free(buffer_);
}
void MallocWriteStream::Realloc(intptr_t new_size) {
const intptr_t old_offset = current_ - buffer_;
buffer_ = reinterpret_cast<uint8_t*>(realloc(buffer_, new_size));
capacity_ = buffer_ != nullptr ? new_size : 0;
current_ = buffer_ != nullptr ? buffer_ + old_offset : nullptr;
}
void ZoneWriteStream::Realloc(intptr_t new_size) {
const intptr_t old_offset = current_ - buffer_;
buffer_ = zone_->Realloc(buffer_, capacity_, new_size);
capacity_ = buffer_ != nullptr ? new_size : 0;
current_ = buffer_ != nullptr ? buffer_ + old_offset : nullptr;
}
StreamingWriteStream::~StreamingWriteStream() {
Flush();
free(buffer_);
}
void StreamingWriteStream::Realloc(intptr_t new_size) {
Flush();
// Check whether resetting the internal buffer by flushing gave enough space.
if (new_size <= capacity_) {
return;
}
const intptr_t new_capacity = Utils::RoundUp(new_size, 64 * KB);
buffer_ = reinterpret_cast<uint8_t*>(realloc(buffer_, new_capacity));
capacity_ = buffer_ != nullptr ? new_capacity : 0;
current_ = buffer_; // Flushing reset the internal buffer offset to 0.
}
void StreamingWriteStream::Flush() {
intptr_t size = current_ - buffer_;
callback_(callback_data_, buffer_, size);
flushed_size_ += size;
current_ = buffer_;
}
} // namespace dart