Skip to content

Make ReadBinaryOptions more strict across readers #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
23 changes: 21 additions & 2 deletions include/wabt/binary-reader-ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,37 @@
#ifndef WABT_BINARY_READER_IR_H_
#define WABT_BINARY_READER_IR_H_

#include "wabt/binary-reader-options.h"
#include "wabt/common.h"
#include "wabt/error.h"
#include "wabt/feature.h"

namespace wabt {

struct Module;
struct ReadBinaryOptions;

class ReadBinaryIrOptions : public ReadBinaryOptions {
public:
ReadBinaryIrOptions() = default;
ReadBinaryIrOptions(const Features& features, Stream* log_stream)
: features(features), log_stream(log_stream) {}
Features features;
Stream* log_stream = nullptr;
bool read_debug_names = false;
bool fail_on_custom_section_error = true;

const Features& GetFeatures() const override { return features; }
Stream* GetLogStream() const override { return log_stream; }
bool ReadDebugNames() const override { return read_debug_names; }
bool FailOnCustomSectionError() const override {
return fail_on_custom_section_error;
}
};

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

Expand Down
2 changes: 1 addition & 1 deletion include/wabt/binary-reader-objdump.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include <map>
#include <string>

#include "wabt/binary-reader-options.h"
#include "wabt/common.h"
#include "wabt/feature.h"
#include "wabt/stream.h"

namespace wabt {

struct Module;
struct ReadBinaryOptions;

enum class ObjdumpMode {
Prepass,
Expand Down
39 changes: 39 additions & 0 deletions include/wabt/binary-reader-options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef WABT_BINARY_READER_OPTIONS_H_
#define WABT_BINARY_READER_OPTIONS_H_

namespace wabt {

class Features;
class Stream;

class ReadBinaryOptions {
public:
virtual ~ReadBinaryOptions() {}

virtual const Features& GetFeatures() const = 0;
virtual Stream* GetLogStream() const = 0;
virtual bool ReadDebugNames() const { return false; }
virtual bool StopOnFirstError() const { return true; }
virtual bool FailOnCustomSectionError() const { return true; }
virtual bool SkipFunctionBodies() const { return false; }
};

} // namespace wabt

#endif /* WABT_BINARY_READER_OPTIONS_H_ */
17 changes: 15 additions & 2 deletions include/wabt/binary-reader-stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,28 @@
#include <map>
#include <vector>

#include "wabt/binary-reader-options.h"
#include "wabt/common.h"
#include "wabt/feature.h"
#include "wabt/opcode.h"

namespace wabt {

struct Module;
struct ReadBinaryOptions;
class Stream;

class ReadBinaryStatsOptions : public ReadBinaryOptions {
public:
ReadBinaryStatsOptions() = default;
ReadBinaryStatsOptions(const Features& features, Stream* log_stream)
: features(features), log_stream(log_stream) {}
Features features;
Stream* log_stream = nullptr;

const Features& GetFeatures() const override { return features; }
Stream* GetLogStream() const override { return log_stream; }
};

class OpcodeInfo {
public:
enum class Kind {
Expand Down Expand Up @@ -88,7 +101,7 @@ using OpcodeInfoCounts = std::map<OpcodeInfo, size_t>;

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

} // namespace wabt
Expand Down
24 changes: 1 addition & 23 deletions include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cstdint>
#include <string_view>

#include "wabt/binary-reader-options.h"
#include "wabt/binary.h"
#include "wabt/common.h"
#include "wabt/error.h"
Expand All @@ -29,29 +30,6 @@

namespace wabt {

class Stream;

struct ReadBinaryOptions {
ReadBinaryOptions() = default;
ReadBinaryOptions(const Features& features,
Stream* log_stream,
bool read_debug_names,
bool stop_on_first_error,
bool fail_on_custom_section_error)
: features(features),
log_stream(log_stream),
read_debug_names(read_debug_names),
stop_on_first_error(stop_on_first_error),
fail_on_custom_section_error(fail_on_custom_section_error) {}

Features features;
Stream* log_stream = nullptr;
bool read_debug_names = false;
bool stop_on_first_error = true;
bool fail_on_custom_section_error = true;
bool skip_function_bodies = false;
};

// TODO: Move somewhere else?
struct TypeMut {
Type type;
Expand Down
17 changes: 15 additions & 2 deletions include/wabt/interp/binary-reader-interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@
#ifndef WABT_BINARY_READER_INTERP_H_
#define WABT_BINARY_READER_INTERP_H_

#include "wabt/binary-reader-options.h"
#include "wabt/common.h"
#include "wabt/error.h"
#include "wabt/feature.h"
#include "wabt/interp/interp.h"

namespace wabt {

struct ReadBinaryOptions;
class ReadBinaryInterpOptions : public ReadBinaryOptions {
public:
ReadBinaryInterpOptions() = default;
ReadBinaryInterpOptions(const Features& features, Stream* log_stream)
: features(features), log_stream(log_stream) {}
Features features;
Stream* log_stream = nullptr;

const Features& GetFeatures() const override { return features; }
Stream* GetLogStream() const override { return log_stream; }
bool ReadDebugNames() const override { return true; }
};

namespace interp {

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

Expand Down
2 changes: 1 addition & 1 deletion src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ Result BinaryReaderIR::OnGenericCustomSection(std::string_view name,
Result ReadBinaryIr(const char* filename,
const void* data,
size_t size,
const ReadBinaryOptions& options,
const ReadBinaryIrOptions& options,
Errors* errors,
Module* out_module) {
BinaryReaderIR reader(out_module, filename, errors);
Expand Down
27 changes: 20 additions & 7 deletions src/binary-reader-objdump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2493,13 +2493,26 @@ Result ReadBinaryObjdump(const uint8_t* data,
size_t size,
ObjdumpOptions* options,
ObjdumpState* state) {
Features features;
features.EnableAll();
const bool kReadDebugNames = true;
const bool kStopOnFirstError = false;
const bool kFailOnCustomSectionError = false;
ReadBinaryOptions read_options(features, options->log_stream, kReadDebugNames,
kStopOnFirstError, kFailOnCustomSectionError);
class ReadBinaryObjdumpOptions : public ReadBinaryOptions {
Features features;
Stream* log_stream;

public:
explicit ReadBinaryObjdumpOptions(Stream* log_stream)
: log_stream(log_stream) {
features.EnableAll();
}
bool skip_function_bodies = false;

const Features& GetFeatures() const override { return features; }
Stream* GetLogStream() const override { return log_stream; }
bool ReadDebugNames() const override { return true; }
bool StopOnFirstError() const override { return false; }
bool FailOnCustomSectionError() const override { return false; }
bool SkipFunctionBodies() const override { return skip_function_bodies; }
};

ReadBinaryObjdumpOptions read_options(options->log_stream);

switch (options->mode) {
case ObjdumpMode::Prepass: {
Expand Down
2 changes: 1 addition & 1 deletion src/binary-reader-stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Result BinaryReaderOpcnt::OnEndExpr() {

Result ReadBinaryOpcnt(const void* data,
size_t size,
const ReadBinaryOptions& options,
const ReadBinaryStatsOptions& options,
OpcodeInfoCounts* counts) {
BinaryReaderOpcnt reader(counts);
return ReadBinary(data, size, &reader, options);
Expand Down
Loading