Skip to content
Open
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
3 changes: 1 addition & 2 deletions fuzzers/read_binary_interp_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// validator's counts out of step).
wabt::ReadBinaryOptions options(features, nullptr, false, true, false);
std::vector<uint8_t> text = data_provider.ConsumeRemainingBytes<uint8_t>();
ReadBinaryInterp("<fuzzer>", text.data(), text.size(), options, &errors,
&module);
ReadBinaryInterp("<fuzzer>", text, options, &errors, &module);
return 0;
}
2 changes: 1 addition & 1 deletion fuzzers/read_binary_ir_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Add only feature related options, but no logging, stop_on_first_error, etc.
wabt::ReadBinaryOptions options(features, nullptr, false, false, false);
std::vector<uint8_t> text = data_provider.ConsumeRemainingBytes<uint8_t>();
ReadBinaryIr("", text.data(), text.size(), options, &errors, &module);
ReadBinaryIr("", text, options, &errors, &module);
return 0;
}
2 changes: 1 addition & 1 deletion fuzzers/wasm2wat_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
wabt::ReadBinaryOptions options;
wabt::Errors errors;
wabt::Module module;
wabt::ReadBinaryIr("dummy filename", data, size, options, &errors, &module);
wabt::ReadBinaryIr("dummy filename", {data, size}, options, &errors, &module);
return 0;
}
10 changes: 5 additions & 5 deletions fuzzers/wasm_objdump_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
objdump_options.log_stream = nullptr;

objdump_options.mode = wabt::ObjdumpMode::Prepass;
wabt::ReadBinaryObjdump(data, size, &objdump_options, &state);
wabt::ReadBinaryObjdump({data, size}, &objdump_options, &state);

objdump_options.mode = wabt::ObjdumpMode::Headers;
wabt::ReadBinaryObjdump(data, size, &objdump_options, &state);
wabt::ReadBinaryObjdump({data, size}, &objdump_options, &state);

objdump_options.mode = wabt::ObjdumpMode::Details;
wabt::ReadBinaryObjdump(data, size, &objdump_options, &state);
wabt::ReadBinaryObjdump({data, size}, &objdump_options, &state);

objdump_options.mode = wabt::ObjdumpMode::Disassemble;
wabt::ReadBinaryObjdump(data, size, &objdump_options, &state);
wabt::ReadBinaryObjdump({data, size}, &objdump_options, &state);

objdump_options.mode = wabt::ObjdumpMode::RawData;
wabt::ReadBinaryObjdump(data, size, &objdump_options, &state);
wabt::ReadBinaryObjdump({data, size}, &objdump_options, &state);

return 0;
}
2 changes: 2 additions & 0 deletions include/wabt/base-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

#include <cstddef>
#include <cstdint>
#include <span>

namespace wabt {

using Index = uint32_t; // An index into one of the many index spaces.
using Address = uint64_t; // An address or size in linear memory.
using Offset = size_t; // An offset into a host's file or memory buffer.
using ByteSpan = std::span<const uint8_t>;

constexpr Address kInvalidAddress = ~0;
constexpr Index kInvalidIndex = ~0;
Expand Down
9 changes: 8 additions & 1 deletion include/wabt/binary-reader-ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ struct Module;
struct ReadBinaryOptions;

Result ReadBinaryIr(const char* filename,
const void* data,
ByteSpan data,
const ReadBinaryOptions& options,
Errors*,
Module* out_module);

// TODO(sbc): Remove this old API. Use the ByteSpan overload instead.
Result ReadBinaryIr(const char* filename,
const uint8_t* data,
size_t size,
const ReadBinaryOptions& options,
Errors*,
Expand Down
10 changes: 3 additions & 7 deletions include/wabt/binary-reader-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
uint8_t flags) override;
Result BeginDataSegmentInitExpr(Index index) override;
Result EndDataSegmentInitExpr(Index index) override;
Result OnDataSegmentData(Index index,
const void* data,
Address size) override;
Result OnDataSegmentData(Index index, ByteSpan data) override;
Result EndDataSegment(Index index) override;
Result EndDataSection() override;

Expand Down Expand Up @@ -365,9 +363,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result EndDylinkSection() override;

Result BeginGenericCustomSection(Offset size) override;
Result OnGenericCustomSection(std::string_view name,
const void* data,
Offset size) override;
Result OnGenericCustomSection(std::string_view name, ByteSpan data) override;
Result EndGenericCustomSection() override;

Result BeginTargetFeaturesSection(Offset size) override;
Expand Down Expand Up @@ -425,7 +421,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result BeginCodeMetadataSection(std::string_view name, Offset size) override;
Result OnCodeMetadataFuncCount(Index count) override;
Result OnCodeMetadataCount(Index function_index, Index count) override;
Result OnCodeMetadata(Offset offset, const void* data, Address size) override;
Result OnCodeMetadata(Offset offset, ByteSpan data) override;
Result EndCodeMetadataSection() override;

private:
Expand Down
12 changes: 3 additions & 9 deletions include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
}
Result BeginDataSegmentInitExpr(Index index) override { return Result::Ok; }
Result EndDataSegmentInitExpr(Index index) override { return Result::Ok; }
Result OnDataSegmentData(Index index,
const void* data,
Address size) override {
Result OnDataSegmentData(Index index, ByteSpan data) override {
return Result::Ok;
}
Result EndDataSegment(Index index) override { return Result::Ok; }
Expand Down Expand Up @@ -500,9 +498,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnCodeMetadataCount(Index function_index, Index count) override {
return Result::Ok;
}
Result OnCodeMetadata(Offset offset,
const void* data,
Address size) override {
Result OnCodeMetadata(Offset offset, ByteSpan data) override {
return Result::Ok;
}
Result EndCodeMetadataSection() override { return Result::Ok; }
Expand Down Expand Up @@ -541,9 +537,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {

/* Generic custom section */
Result BeginGenericCustomSection(Offset size) override { return Result::Ok; }
Result OnGenericCustomSection(std::string_view name,
const void* data,
Offset size) override {
Result OnGenericCustomSection(std::string_view name, ByteSpan data) override {
return Result::Ok;
};
Result EndGenericCustomSection() override { return Result::Ok; }
Expand Down
5 changes: 5 additions & 0 deletions include/wabt/binary-reader-objdump.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ struct ObjdumpState {
std::map<Index, Index> function_types;
};

Result ReadBinaryObjdump(ByteSpan data,
ObjdumpOptions* options,
ObjdumpState* state);

// TODO(sbc): Remove this old API. Use the ByteSpan overload instead.
Result ReadBinaryObjdump(const uint8_t* data,
size_t size,
ObjdumpOptions* options,
Expand Down
3 changes: 1 addition & 2 deletions include/wabt/binary-reader-stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ bool operator>=(const OpcodeInfo&, const OpcodeInfo&);

using OpcodeInfoCounts = std::map<OpcodeInfo, size_t>;

Result ReadBinaryOpcnt(const void* data,
size_t size,
Result ReadBinaryOpcnt(ByteSpan data,
const ReadBinaryOptions& options,
OpcodeInfoCounts* opcode_counts);

Expand Down
20 changes: 6 additions & 14 deletions include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ enum class TableInitExprStatus {
class BinaryReaderDelegate {
public:
struct State {
State(const uint8_t* data, Offset size)
: data(data), size(size), offset(0) {}
explicit State(ByteSpan data) : data(data), offset(0) {}

const uint8_t* data;
Offset size;
ByteSpan data;
Offset offset;
};

Expand Down Expand Up @@ -382,9 +380,7 @@ class BinaryReaderDelegate {
uint8_t flags) = 0;
virtual Result BeginDataSegmentInitExpr(Index index) = 0;
virtual Result EndDataSegmentInitExpr(Index index) = 0;
virtual Result OnDataSegmentData(Index index,
const void* data,
Address size) = 0;
virtual Result OnDataSegmentData(Index index, ByteSpan data) = 0;
virtual Result EndDataSegment(Index index) = 0;
virtual Result EndDataSection() = 0;

Expand Down Expand Up @@ -457,8 +453,7 @@ class BinaryReaderDelegate {
/* Generic custom section */
virtual Result BeginGenericCustomSection(Offset size) = 0;
virtual Result OnGenericCustomSection(std::string_view name,
const void* data,
Offset size) = 0;
ByteSpan data) = 0;
virtual Result EndGenericCustomSection() = 0;

/* Linking section */
Expand Down Expand Up @@ -514,16 +509,13 @@ class BinaryReaderDelegate {
Offset size) = 0;
virtual Result OnCodeMetadataFuncCount(Index count) = 0;
virtual Result OnCodeMetadataCount(Index function_index, Index count) = 0;
virtual Result OnCodeMetadata(Offset offset,
const void* data,
Address size) = 0;
virtual Result OnCodeMetadata(Offset offset, ByteSpan data) = 0;
virtual Result EndCodeMetadataSection() = 0;

const State* state = nullptr;
};

Result ReadBinary(const void* data,
size_t size,
Result ReadBinary(ByteSpan data,
BinaryReaderDelegate* reader,
const ReadBinaryOptions& options);

Expand Down
9 changes: 8 additions & 1 deletion include/wabt/interp/binary-reader-interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ struct ReadBinaryOptions;
namespace interp {

Result ReadBinaryInterp(std::string_view filename,
const void* data,
ByteSpan data,
const ReadBinaryOptions& options,
Errors*,
ModuleDesc* out_module);

// TODO(sbc): Remove this old API. Use the ByteSpan overload instead.
Result ReadBinaryInterp(std::string_view filename,
const uint8_t* data,
size_t size,
const ReadBinaryOptions& options,
Errors*,
Expand Down
31 changes: 19 additions & 12 deletions include/wabt/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <cassert>
#include <memory>
#include <span>
#include <vector>

#include "wabt/common.h"
Expand Down Expand Up @@ -55,23 +56,32 @@ class Stream {
void ClearOffset() { offset_ = 0; }
void AddOffset(ssize_t delta);

void WriteData(ByteSpan src,
const char* desc = nullptr,
PrintChars = PrintChars::No);

// TODO(sbc): Remove this old API. Use the ByteSpan overload instead.
// Convenience overload for raw pointers.
void WriteData(const void* src,
size_t size,
const char* desc = nullptr,
PrintChars = PrintChars::No);
PrintChars print_chars = PrintChars::No) {
WriteData({static_cast<const uint8_t*>(src), size}, desc, print_chars);
}

template <typename T>
void WriteData(const std::vector<T> src,
void WriteData(std::span<const T> src,
const char* desc,
PrintChars print_chars = PrintChars::No) {
if (!src.empty()) {
WriteData(src.data(), src.size() * sizeof(T), desc, print_chars);
WriteData(ByteSpan(reinterpret_cast<const uint8_t*>(src.data()),
src.size_bytes()),
desc, print_chars);
}
}

void WriteDataAt(size_t offset,
const void* src,
size_t size,
ByteSpan data,
const char* desc = nullptr,
PrintChars = PrintChars::No);

Expand Down Expand Up @@ -112,8 +122,7 @@ class Stream {
}

// Dump memory as text, similar to the xxd format.
void WriteMemoryDump(const void* start,
size_t size,
void WriteMemoryDump(ByteSpan data,
size_t offset = 0,
PrintChars print_chars = PrintChars::No,
const char* prefix = nullptr,
Expand All @@ -130,9 +139,7 @@ class Stream {
virtual void Flush() {}

protected:
virtual Result WriteDataImpl(size_t offset,
const void* data,
size_t size) = 0;
virtual Result WriteDataImpl(size_t offset, ByteSpan data) = 0;
virtual Result MoveDataImpl(size_t dst_offset,
size_t src_offset,
size_t size) = 0;
Expand Down Expand Up @@ -185,7 +192,7 @@ class MemoryStream : public Stream {
}

protected:
Result WriteDataImpl(size_t offset, const void* data, size_t size) override;
Result WriteDataImpl(size_t offset, ByteSpan data) override;
Result MoveDataImpl(size_t dst_offset,
size_t src_offset,
size_t size) override;
Expand All @@ -212,7 +219,7 @@ class FileStream : public Stream {
void Flush() override;

protected:
Result WriteDataImpl(size_t offset, const void* data, size_t size) override;
Result WriteDataImpl(size_t offset, ByteSpan data) override;
Result MoveDataImpl(size_t dst_offset,
size_t src_offset,
size_t size) override;
Expand Down
Loading
Loading