Commit af4905d3 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Remove non-const arguments from decoder

R=ahaas@chromium.org

Bug: v8:9429, v8:9396
Change-Id: I79e5d707f6c3970c96eb2186604d8b26fda787e5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1687897Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62556}
parent 7a08b033
......@@ -45,7 +45,7 @@ BytecodeIterator::BytecodeIterator(const byte* start, const byte* end,
DecodeResult VerifyWasmCode(AccountingAllocator* allocator,
const WasmFeatures& enabled,
const WasmModule* module, WasmFeatures* detected,
FunctionBody& body) { // NOLINT(runtime/references)
const FunctionBody& body) {
Zone zone(allocator, ZONE_NAME);
WasmFullDecoder<Decoder::kValidate, EmptyInterface> decoder(
&zone, module, enabled, detected, body);
......
......@@ -34,10 +34,11 @@ struct FunctionBody {
: sig(sig), offset(offset), start(start), end(end) {}
};
V8_EXPORT_PRIVATE DecodeResult
VerifyWasmCode(AccountingAllocator* allocator, const WasmFeatures& enabled,
const WasmModule* module, WasmFeatures* detected,
FunctionBody& body); // NOLINT(runtime/references)
V8_EXPORT_PRIVATE DecodeResult VerifyWasmCode(AccountingAllocator* allocator,
const WasmFeatures& enabled,
const WasmModule* module,
WasmFeatures* detected,
const FunctionBody& body);
enum PrintLocals { kPrintLocals, kOmitLocals };
V8_EXPORT_PRIVATE
......
......@@ -1921,7 +1921,7 @@ bool AsyncStreamingProcessor::ProcessSection(SectionCode section_code,
if (section_code == SectionCode::kUnknownSectionCode) {
Decoder decoder(bytes, offset);
section_code = ModuleDecoder::IdentifyUnknownSection(
decoder, bytes.begin() + bytes.length());
&decoder, bytes.begin() + bytes.length());
if (section_code == SectionCode::kUnknownSectionCode) {
// Skip unknown sections that we do not know how to handle.
return true;
......
......@@ -131,35 +131,35 @@ ValueType TypeOf(const WasmModule* module, const WasmInitExpr& expr) {
// Reads a length-prefixed string, checking that it is within bounds. Returns
// the offset of the string, and the length as an out parameter.
WireBytesRef consume_string(Decoder& decoder, // NOLINT(runtime/references)
bool validate_utf8, const char* name) {
uint32_t length = decoder.consume_u32v("string length");
uint32_t offset = decoder.pc_offset();
const byte* string_start = decoder.pc();
WireBytesRef consume_string(Decoder* decoder, bool validate_utf8,
const char* name) {
uint32_t length = decoder->consume_u32v("string length");
uint32_t offset = decoder->pc_offset();
const byte* string_start = decoder->pc();
// Consume bytes before validation to guarantee that the string is not oob.
if (length > 0) {
decoder.consume_bytes(length, name);
if (decoder.ok() && validate_utf8 &&
decoder->consume_bytes(length, name);
if (decoder->ok() && validate_utf8 &&
!unibrow::Utf8::ValidateEncoding(string_start, length)) {
decoder.errorf(string_start, "%s: no valid UTF-8 string", name);
decoder->errorf(string_start, "%s: no valid UTF-8 string", name);
}
}
return {offset, decoder.failed() ? 0 : length};
return {offset, decoder->failed() ? 0 : length};
}
// An iterator over the sections in a wasm binary module.
// Automatically skips all unknown sections.
class WasmSectionIterator {
public:
explicit WasmSectionIterator(Decoder& decoder) // NOLINT(runtime/references)
explicit WasmSectionIterator(Decoder* decoder)
: decoder_(decoder),
section_code_(kUnknownSectionCode),
section_start_(decoder.pc()),
section_end_(decoder.pc()) {
section_start_(decoder->pc()),
section_end_(decoder->pc()) {
next();
}
inline bool more() const { return decoder_.ok() && decoder_.more(); }
inline bool more() const { return decoder_->ok() && decoder_->more(); }
inline SectionCode section_code() const { return section_code_; }
......@@ -184,23 +184,23 @@ class WasmSectionIterator {
// Advances to the next section, checking that decoding the current section
// stopped at {section_end_}.
void advance(bool move_to_section_end = false) {
if (move_to_section_end && decoder_.pc() < section_end_) {
decoder_.consume_bytes(
static_cast<uint32_t>(section_end_ - decoder_.pc()));
if (move_to_section_end && decoder_->pc() < section_end_) {
decoder_->consume_bytes(
static_cast<uint32_t>(section_end_ - decoder_->pc()));
}
if (decoder_.pc() != section_end_) {
const char* msg = decoder_.pc() < section_end_ ? "shorter" : "longer";
decoder_.errorf(decoder_.pc(),
if (decoder_->pc() != section_end_) {
const char* msg = decoder_->pc() < section_end_ ? "shorter" : "longer";
decoder_->errorf(decoder_->pc(),
"section was %s than expected size "
"(%u bytes expected, %zu decoded)",
msg, section_length(),
static_cast<size_t>(decoder_.pc() - section_start_));
static_cast<size_t>(decoder_->pc() - section_start_));
}
next();
}
private:
Decoder& decoder_;
Decoder* decoder_;
SectionCode section_code_;
const byte* section_start_;
const byte* payload_start_;
......@@ -209,17 +209,17 @@ class WasmSectionIterator {
// Reads the section code/name at the current position and sets up
// the embedder fields.
void next() {
if (!decoder_.more()) {
if (!decoder_->more()) {
section_code_ = kUnknownSectionCode;
return;
}
section_start_ = decoder_.pc();
uint8_t section_code = decoder_.consume_u8("section code");
section_start_ = decoder_->pc();
uint8_t section_code = decoder_->consume_u8("section code");
// Read and check the section size.
uint32_t section_length = decoder_.consume_u32v("section length");
uint32_t section_length = decoder_->consume_u32v("section length");
payload_start_ = decoder_.pc();
if (decoder_.checkAvailable(section_length)) {
payload_start_ = decoder_->pc();
if (decoder_->checkAvailable(section_length)) {
// Get the limit of the section within the module.
section_end_ = payload_start_ + section_length;
} else {
......@@ -234,19 +234,19 @@ class WasmSectionIterator {
ModuleDecoder::IdentifyUnknownSection(decoder_, section_end_);
// As a side effect, the above function will forward the decoder to after
// the identifier string.
payload_start_ = decoder_.pc();
payload_start_ = decoder_->pc();
} else if (!IsValidSectionCode(section_code)) {
decoder_.errorf(decoder_.pc(), "unknown section code #0x%02x",
decoder_->errorf(decoder_->pc(), "unknown section code #0x%02x",
section_code);
section_code = kUnknownSectionCode;
}
section_code_ = decoder_.failed() ? kUnknownSectionCode
section_code_ = decoder_->failed() ? kUnknownSectionCode
: static_cast<SectionCode>(section_code);
if (section_code_ == kUnknownSectionCode && section_end_ > decoder_.pc()) {
if (section_code_ == kUnknownSectionCode && section_end_ > decoder_->pc()) {
// skip to the end of the unknown section.
uint32_t remaining = static_cast<uint32_t>(section_end_ - decoder_.pc());
decoder_.consume_bytes(remaining, "section payload");
uint32_t remaining = static_cast<uint32_t>(section_end_ - decoder_->pc());
decoder_->consume_bytes(remaining, "section payload");
}
}
};
......@@ -520,8 +520,8 @@ class ModuleDecoderImpl : public Decoder {
});
WasmImport* import = &module_->import_table.back();
const byte* pos = pc_;
import->module_name = consume_string(*this, true, "module name");
import->field_name = consume_string(*this, true, "field name");
import->module_name = consume_string(this, true, "module name");
import->field_name = consume_string(this, true, "field name");
import->kind =
static_cast<ImportExportKindCode>(consume_u8("import kind"));
switch (import->kind) {
......@@ -694,7 +694,7 @@ class ModuleDecoderImpl : public Decoder {
});
WasmExport* exp = &module_->export_table.back();
exp->name = consume_string(*this, true, "field name");
exp->name = consume_string(this, true, "field name");
const byte* pos = pc();
exp->kind = static_cast<ImportExportKindCode>(consume_u8("export kind"));
......@@ -957,7 +957,7 @@ class ModuleDecoderImpl : public Decoder {
// Decode module name, ignore the rest.
// Function and local names will be decoded when needed.
if (name_type == NameSectionKindCode::kModule) {
WireBytesRef name = consume_string(inner, false, "module name");
WireBytesRef name = consume_string(&inner, false, "module name");
if (inner.ok() && validate_utf8(&inner, name)) module_->name = name;
} else {
inner.consume_bytes(name_payload_len, "name subsection payload");
......@@ -970,7 +970,7 @@ class ModuleDecoderImpl : public Decoder {
void DecodeSourceMappingURLSection() {
Decoder inner(start_, pc_, end_, buffer_offset_);
WireBytesRef url = wasm::consume_string(inner, true, "module name");
WireBytesRef url = wasm::consume_string(&inner, true, "module name");
if (inner.ok() &&
!has_seen_unordered_section(kSourceMappingURLSectionCode)) {
const byte* url_start =
......@@ -1128,7 +1128,7 @@ class ModuleDecoderImpl : public Decoder {
offset += 8;
Decoder decoder(start_ + offset, end_, offset);
WasmSectionIterator section_iter(decoder);
WasmSectionIterator section_iter(&decoder);
while (ok() && section_iter.more()) {
// Shift the offset by the section header length
......@@ -1373,34 +1373,33 @@ class ModuleDecoderImpl : public Decoder {
uint32_t consume_func_index(WasmModule* module, WasmFunction** func,
const char* name) {
return consume_index(name, module->functions, func);
return consume_index(name, &module->functions, func);
}
uint32_t consume_global_index(WasmModule* module, WasmGlobal** global) {
return consume_index("global index", module->globals, global);
return consume_index("global index", &module->globals, global);
}
uint32_t consume_table_index(WasmModule* module, WasmTable** table) {
return consume_index("table index", module->tables, table);
return consume_index("table index", &module->tables, table);
}
uint32_t consume_exception_index(WasmModule* module, WasmException** except) {
return consume_index("exception index", module->exceptions, except);
return consume_index("exception index", &module->exceptions, except);
}
template <typename T>
uint32_t consume_index(const char* name,
std::vector<T>& vector, // NOLINT(runtime/references)
T** ptr) {
uint32_t consume_index(const char* name, std::vector<T>* vector, T** ptr) {
const byte* pos = pc_;
uint32_t index = consume_u32v(name);
if (index >= vector.size()) {
if (index >= vector->size()) {
errorf(pos, "%s %u out of bounds (%d entr%s)", name, index,
static_cast<int>(vector.size()), vector.size() == 1 ? "y" : "ies");
static_cast<int>(vector->size()),
vector->size() == 1 ? "y" : "ies");
*ptr = nullptr;
return 0;
}
*ptr = &vector[index];
*ptr = &(*vector)[index];
return index;
}
......@@ -1831,17 +1830,17 @@ ModuleResult ModuleDecoder::FinishDecoding(bool verify_functions) {
return impl_->FinishDecoding(verify_functions);
}
SectionCode ModuleDecoder::IdentifyUnknownSection(Decoder& decoder,
SectionCode ModuleDecoder::IdentifyUnknownSection(Decoder* decoder,
const byte* end) {
WireBytesRef string = consume_string(decoder, true, "section name");
if (decoder.failed() || decoder.pc() > end) {
if (decoder->failed() || decoder->pc() > end) {
return kUnknownSectionCode;
}
const byte* section_name_start =
decoder.start() + decoder.GetBufferRelativeOffset(string.offset());
decoder->start() + decoder->GetBufferRelativeOffset(string.offset());
TRACE(" +%d section name : \"%.*s\"\n",
static_cast<int>(section_name_start - decoder.start()),
static_cast<int>(section_name_start - decoder->start()),
string.length() < 20 ? string.length() : 20, section_name_start);
if (string.length() == num_chars(kNameString) &&
......@@ -1987,20 +1986,20 @@ std::vector<CustomSectionOffset> DecodeCustomSections(const byte* start,
namespace {
bool FindNameSection(Decoder& decoder) { // NOLINT(runtime/references)
bool FindNameSection(Decoder* decoder) {
static constexpr int kModuleHeaderSize = 8;
decoder.consume_bytes(kModuleHeaderSize, "module header");
decoder->consume_bytes(kModuleHeaderSize, "module header");
WasmSectionIterator section_iter(decoder);
while (decoder.ok() && section_iter.more() &&
while (decoder->ok() && section_iter.more() &&
section_iter.section_code() != kNameSectionCode) {
section_iter.advance(true);
}
if (!section_iter.more()) return false;
// Reset the decoder to not read beyond the name section end.
decoder.Reset(section_iter.payload(), decoder.pc_offset());
decoder->Reset(section_iter.payload(), decoder->pc_offset());
return true;
}
......@@ -2012,7 +2011,7 @@ void DecodeFunctionNames(const byte* module_start, const byte* module_end,
DCHECK(names->empty());
Decoder decoder(module_start, module_end);
if (!FindNameSection(decoder)) return;
if (!FindNameSection(&decoder)) return;
while (decoder.ok() && decoder.more()) {
uint8_t name_type = decoder.consume_u8("name type");
......@@ -2029,7 +2028,7 @@ void DecodeFunctionNames(const byte* module_start, const byte* module_end,
for (; decoder.ok() && functions_count > 0; --functions_count) {
uint32_t function_index = decoder.consume_u32v("function index");
WireBytesRef name = consume_string(decoder, false, "function name");
WireBytesRef name = consume_string(&decoder, false, "function name");
// Be lenient with errors in the name section: Ignore non-UTF8 names. You
// can even assign to the same function multiple times (last valid one
......@@ -2047,7 +2046,7 @@ void DecodeLocalNames(const byte* module_start, const byte* module_end,
DCHECK(result->names.empty());
Decoder decoder(module_start, module_end);
if (!FindNameSection(decoder)) return;
if (!FindNameSection(&decoder)) return;
while (decoder.ok() && decoder.more()) {
uint8_t name_type = decoder.consume_u8("name type");
......@@ -2072,7 +2071,7 @@ void DecodeLocalNames(const byte* module_start, const byte* module_end,
uint32_t num_names = decoder.consume_u32v("namings count");
for (uint32_t k = 0; k < num_names; ++k) {
uint32_t local_index = decoder.consume_u32v("local index");
WireBytesRef name = consume_string(decoder, true, "local name");
WireBytesRef name = consume_string(&decoder, true, "local name");
if (!decoder.ok()) break;
if (local_index > kMaxInt) continue;
func_names.max_local_index =
......
......@@ -139,14 +139,12 @@ class ModuleDecoder {
// Translates the unknown section that decoder is pointing to to an extended
// SectionCode if the unknown section is known to decoder.
// The decoder is expected to point after the section lenght and just before
// The decoder is expected to point after the section length and just before
// the identifier string of the unknown section.
// If a SectionCode other than kUnknownSectionCode is returned, the decoder
// will point right after the identifier string. Otherwise, the position is
// undefined.
static SectionCode IdentifyUnknownSection(
Decoder& decoder, // NOLINT(runtime/references)
const byte* end);
static SectionCode IdentifyUnknownSection(Decoder* decoder, const byte* end);
private:
const WasmFeatures enabled_features_;
......
......@@ -18,29 +18,29 @@ namespace wasm {
namespace {
PRINTF_FORMAT(3, 0)
void VPrintFToString(std::string& str, // NOLINT(runtime/references)
size_t str_offset, const char* format, va_list args) {
DCHECK_LE(str_offset, str.size());
void VPrintFToString(std::string* str, size_t str_offset, const char* format,
va_list args) {
DCHECK_LE(str_offset, str->size());
size_t len = str_offset + strlen(format);
// Allocate increasingly large buffers until the message fits.
for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
DCHECK_GE(kMaxInt, len);
str.resize(len);
str->resize(len);
va_list args_copy;
va_copy(args_copy, args);
int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
int written = VSNPrintF(Vector<char>(&str->front() + str_offset,
static_cast<int>(len - str_offset)),
format, args_copy);
va_end(args_copy);
if (written < 0) continue; // not enough space.
str.resize(str_offset + written);
str->resize(str_offset + written);
return;
}
}
PRINTF_FORMAT(3, 4)
void PrintFToString(std::string& str, // NOLINT(runtime/references)
size_t str_offset, const char* format, ...) {
void PrintFToString(std::string* str, size_t str_offset, const char* format,
...) {
va_list args;
va_start(args, format);
VPrintFToString(str, str_offset, format, args);
......@@ -52,7 +52,7 @@ void PrintFToString(std::string& str, // NOLINT(runtime/references)
// static
std::string WasmError::FormatError(const char* format, va_list args) {
std::string result;
VPrintFToString(result, 0, format, args);
VPrintFToString(&result, 0, format, args);
return result;
}
......@@ -63,10 +63,10 @@ void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
size_t context_len = 0;
if (context_) {
PrintFToString(error_msg_, 0, "%s: ", context_);
PrintFToString(&error_msg_, 0, "%s: ", context_);
context_len = error_msg_.size();
}
VPrintFToString(error_msg_, context_len, format, args);
VPrintFToString(&error_msg_, context_len, format, args);
error_type_ = type;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment