Skip to content

Commit aa8c028

Browse files
committed
Make ReadBinaryOptions more strict across readers
1 parent 508984b commit aa8c028

25 files changed

+202
-138
lines changed

include/wabt/binary-reader-ir.h

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,37 @@
1717
#ifndef WABT_BINARY_READER_IR_H_
1818
#define WABT_BINARY_READER_IR_H_
1919

20+
#include "wabt/binary-reader-options.h"
2021
#include "wabt/common.h"
2122
#include "wabt/error.h"
23+
#include "wabt/feature.h"
2224

2325
namespace wabt {
2426

2527
struct Module;
26-
struct ReadBinaryOptions;
28+
29+
class ReadBinaryIrOptions : public ReadBinaryOptions {
30+
public:
31+
ReadBinaryIrOptions() = default;
32+
ReadBinaryIrOptions(const Features& features, Stream* log_stream)
33+
: features(features), log_stream(log_stream) {}
34+
Features features;
35+
Stream* log_stream = nullptr;
36+
bool read_debug_names = false;
37+
bool fail_on_custom_section_error = true;
38+
39+
const Features& GetFeatures() const override { return features; }
40+
Stream* GetLogStream() const override { return log_stream; }
41+
bool ReadDebugNames() const override { return read_debug_names; }
42+
bool FailOnCustomSectionError() const override {
43+
return fail_on_custom_section_error;
44+
}
45+
};
2746

2847
Result ReadBinaryIr(const char* filename,
2948
const void* data,
3049
size_t size,
31-
const ReadBinaryOptions& options,
50+
const ReadBinaryIrOptions& options,
3251
Errors*,
3352
Module* out_module);
3453

include/wabt/binary-reader-objdump.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
#include <map>
2121
#include <string>
2222

23+
#include "wabt/binary-reader-options.h"
2324
#include "wabt/common.h"
2425
#include "wabt/feature.h"
2526
#include "wabt/stream.h"
2627

2728
namespace wabt {
2829

2930
struct Module;
30-
struct ReadBinaryOptions;
3131

3232
enum class ObjdumpMode {
3333
Prepass,

include/wabt/binary-reader-options.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WABT_BINARY_READER_OPTIONS_H_
18+
#define WABT_BINARY_READER_OPTIONS_H_
19+
20+
namespace wabt {
21+
22+
class Features;
23+
class Stream;
24+
25+
class ReadBinaryOptions {
26+
public:
27+
virtual ~ReadBinaryOptions() {}
28+
29+
virtual const Features& GetFeatures() const = 0;
30+
virtual Stream* GetLogStream() const = 0;
31+
virtual bool ReadDebugNames() const { return false; }
32+
virtual bool StopOnFirstError() const { return true; }
33+
virtual bool FailOnCustomSectionError() const { return true; }
34+
virtual bool SkipFunctionBodies() const { return false; }
35+
};
36+
37+
} // namespace wabt
38+
39+
#endif /* WABT_BINARY_READER_OPTIONS_H_ */

include/wabt/binary-reader-stats.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,28 @@
2020
#include <map>
2121
#include <vector>
2222

23+
#include "wabt/binary-reader-options.h"
2324
#include "wabt/common.h"
25+
#include "wabt/feature.h"
2426
#include "wabt/opcode.h"
2527

2628
namespace wabt {
2729

2830
struct Module;
29-
struct ReadBinaryOptions;
3031
class Stream;
3132

33+
class ReadBinaryStatsOptions : public ReadBinaryOptions {
34+
public:
35+
ReadBinaryStatsOptions() = default;
36+
ReadBinaryStatsOptions(const Features& features, Stream* log_stream)
37+
: features(features), log_stream(log_stream) {}
38+
Features features;
39+
Stream* log_stream = nullptr;
40+
41+
const Features& GetFeatures() const override { return features; }
42+
Stream* GetLogStream() const override { return log_stream; }
43+
};
44+
3245
class OpcodeInfo {
3346
public:
3447
enum class Kind {
@@ -88,7 +101,7 @@ using OpcodeInfoCounts = std::map<OpcodeInfo, size_t>;
88101

89102
Result ReadBinaryOpcnt(const void* data,
90103
size_t size,
91-
const ReadBinaryOptions& options,
104+
const ReadBinaryStatsOptions& options,
92105
OpcodeInfoCounts* opcode_counts);
93106

94107
} // namespace wabt

include/wabt/binary-reader.h

+1-23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cstdint>
2222
#include <string_view>
2323

24+
#include "wabt/binary-reader-options.h"
2425
#include "wabt/binary.h"
2526
#include "wabt/common.h"
2627
#include "wabt/error.h"
@@ -29,29 +30,6 @@
2930

3031
namespace wabt {
3132

32-
class Stream;
33-
34-
struct ReadBinaryOptions {
35-
ReadBinaryOptions() = default;
36-
ReadBinaryOptions(const Features& features,
37-
Stream* log_stream,
38-
bool read_debug_names,
39-
bool stop_on_first_error,
40-
bool fail_on_custom_section_error)
41-
: features(features),
42-
log_stream(log_stream),
43-
read_debug_names(read_debug_names),
44-
stop_on_first_error(stop_on_first_error),
45-
fail_on_custom_section_error(fail_on_custom_section_error) {}
46-
47-
Features features;
48-
Stream* log_stream = nullptr;
49-
bool read_debug_names = false;
50-
bool stop_on_first_error = true;
51-
bool fail_on_custom_section_error = true;
52-
bool skip_function_bodies = false;
53-
};
54-
5533
// TODO: Move somewhere else?
5634
struct TypeMut {
5735
Type type;

include/wabt/interp/binary-reader-interp.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,33 @@
1717
#ifndef WABT_BINARY_READER_INTERP_H_
1818
#define WABT_BINARY_READER_INTERP_H_
1919

20+
#include "wabt/binary-reader-options.h"
2021
#include "wabt/common.h"
2122
#include "wabt/error.h"
23+
#include "wabt/feature.h"
2224
#include "wabt/interp/interp.h"
2325

2426
namespace wabt {
2527

26-
struct ReadBinaryOptions;
28+
class ReadBinaryInterpOptions : public ReadBinaryOptions {
29+
public:
30+
ReadBinaryInterpOptions() = default;
31+
ReadBinaryInterpOptions(const Features& features, Stream* log_stream)
32+
: features(features), log_stream(log_stream) {}
33+
Features features;
34+
Stream* log_stream = nullptr;
35+
36+
const Features& GetFeatures() const override { return features; }
37+
Stream* GetLogStream() const override { return log_stream; }
38+
bool ReadDebugNames() const override { return true; }
39+
};
2740

2841
namespace interp {
2942

3043
Result ReadBinaryInterp(std::string_view filename,
3144
const void* data,
3245
size_t size,
33-
const ReadBinaryOptions& options,
46+
const ReadBinaryInterpOptions& options,
3447
Errors*,
3548
ModuleDesc* out_module);
3649

src/binary-reader-ir.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ Result BinaryReaderIR::OnGenericCustomSection(std::string_view name,
18501850
Result ReadBinaryIr(const char* filename,
18511851
const void* data,
18521852
size_t size,
1853-
const ReadBinaryOptions& options,
1853+
const ReadBinaryIrOptions& options,
18541854
Errors* errors,
18551855
Module* out_module) {
18561856
BinaryReaderIR reader(out_module, filename, errors);

src/binary-reader-objdump.cc

+20-7
Original file line numberDiff line numberDiff line change
@@ -2493,13 +2493,26 @@ Result ReadBinaryObjdump(const uint8_t* data,
24932493
size_t size,
24942494
ObjdumpOptions* options,
24952495
ObjdumpState* state) {
2496-
Features features;
2497-
features.EnableAll();
2498-
const bool kReadDebugNames = true;
2499-
const bool kStopOnFirstError = false;
2500-
const bool kFailOnCustomSectionError = false;
2501-
ReadBinaryOptions read_options(features, options->log_stream, kReadDebugNames,
2502-
kStopOnFirstError, kFailOnCustomSectionError);
2496+
class ReadBinaryObjdumpOptions : public ReadBinaryOptions {
2497+
Features features;
2498+
Stream* log_stream;
2499+
2500+
public:
2501+
explicit ReadBinaryObjdumpOptions(Stream* log_stream)
2502+
: log_stream(log_stream) {
2503+
features.EnableAll();
2504+
}
2505+
bool skip_function_bodies = false;
2506+
2507+
const Features& GetFeatures() const override { return features; }
2508+
Stream* GetLogStream() const override { return log_stream; }
2509+
bool ReadDebugNames() const override { return true; }
2510+
bool StopOnFirstError() const override { return false; }
2511+
bool FailOnCustomSectionError() const override { return false; }
2512+
bool SkipFunctionBodies() const override { return skip_function_bodies; }
2513+
};
2514+
2515+
ReadBinaryObjdumpOptions read_options(options->log_stream);
25032516

25042517
switch (options->mode) {
25052518
case ObjdumpMode::Prepass: {

src/binary-reader-stats.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Result BinaryReaderOpcnt::OnEndExpr() {
305305

306306
Result ReadBinaryOpcnt(const void* data,
307307
size_t size,
308-
const ReadBinaryOptions& options,
308+
const ReadBinaryStatsOptions& options,
309309
OpcodeInfoCounts* counts) {
310310
BinaryReaderOpcnt reader(counts);
311311
return ReadBinary(data, size, &reader, options);

0 commit comments

Comments
 (0)