Skip to content

Commit 3698161

Browse files
committed
Move filename out of Location
Reduces memory consumption, and cannot refer to freed memory.
1 parent 5551e79 commit 3698161

25 files changed

Lines changed: 90 additions & 114 deletions

include/wabt/binary-reader-ir.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ namespace wabt {
2525
struct Module;
2626
struct ReadBinaryOptions;
2727

28-
Result ReadBinaryIr(const char* filename,
29-
const void* data,
28+
Result ReadBinaryIr(const void* data,
3029
size_t size,
3130
const ReadBinaryOptions& options,
3231
Errors*,

include/wabt/common.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,14 @@ struct Location {
209209
};
210210

211211
Location() : line(0), first_column(0), last_column(0) {}
212-
Location(std::string_view filename,
213-
int line,
212+
Location(int line,
214213
int first_column,
215214
int last_column)
216-
: filename(filename),
217-
line(line),
215+
: line(line),
218216
first_column(first_column),
219217
last_column(last_column) {}
220-
explicit Location(size_t offset) : offset(offset) {}
218+
explicit Location(size_t offset) : offset(offset), print_filename(false) {}
221219

222-
std::string_view filename;
223220
union {
224221
// For text files.
225222
struct {
@@ -230,6 +227,7 @@ struct Location {
230227
// For binary files.
231228
struct {
232229
size_t offset;
230+
bool print_filename;
233231
};
234232
};
235233
};

include/wabt/error-formatter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum class PrintHeader {
3535

3636
std::string FormatErrorsToString(const Errors&,
3737
Location::Type,
38+
const std::string_view filename,
3839
LexerSourceLineFinder* = nullptr,
3940
const Color& color = Color(nullptr, false),
4041
const std::string& header = {},
@@ -43,6 +44,7 @@ std::string FormatErrorsToString(const Errors&,
4344

4445
void FormatErrorsToFile(const Errors&,
4546
Location::Type,
47+
const std::string_view filename,
4648
LexerSourceLineFinder* = nullptr,
4749
FILE* = stderr,
4850
const std::string& header = {},

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ struct ReadBinaryOptions;
2727

2828
namespace interp {
2929

30-
Result ReadBinaryInterp(std::string_view filename,
31-
const void* data,
30+
Result ReadBinaryInterp(const void* data,
3231
size_t size,
3332
const ReadBinaryOptions& options,
3433
Errors*,

include/wabt/wast-lexer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ class WastLexer {
3838
WABT_DISALLOW_COPY_AND_ASSIGN(WastLexer);
3939

4040
WastLexer(std::unique_ptr<LexerSource> source,
41-
std::string_view filename,
4241
Errors*);
4342

4443
// Convenience functions.
45-
static std::unique_ptr<WastLexer> CreateBufferLexer(std::string_view filename,
46-
const void* data,
44+
static std::unique_ptr<WastLexer> CreateBufferLexer(const void* data,
4745
size_t size,
4846
Errors*);
4947

@@ -100,7 +98,6 @@ class WastLexer {
10098
Token GetReservedToken();
10199

102100
std::unique_ptr<LexerSource> source_;
103-
std::string filename_;
104101
int line_;
105102
const char* buffer_;
106103
const char* buffer_end_;

src/binary-reader-ir.cc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class BinaryReaderIR : public BinaryReaderNop {
9797
static constexpr size_t kMaxFunctionResults = 1000; // matches V8
9898

9999
public:
100-
BinaryReaderIR(Module* out_module, const char* filename, Errors* errors);
100+
BinaryReaderIR(Module* out_module, Errors* errors);
101101

102102
bool OnError(const Error&) override;
103103

@@ -402,21 +402,18 @@ class BinaryReaderIR : public BinaryReaderNop {
402402

403403
Func* current_func_ = nullptr;
404404
std::vector<LabelNode> label_stack_;
405-
const char* filename_;
406405

407406
CodeMetadataExprQueue code_metadata_queue_;
408407
std::string_view current_metadata_name_;
409408
};
410409

411-
BinaryReaderIR::BinaryReaderIR(Module* out_module,
412-
const char* filename,
413-
Errors* errors)
414-
: errors_(errors), module_(out_module), filename_(filename) {}
410+
BinaryReaderIR::BinaryReaderIR(Module* out_module, Errors* errors)
411+
: errors_(errors), module_(out_module) {}
415412

416413
Location BinaryReaderIR::GetLocation() const {
417414
Location loc;
418-
loc.filename = filename_;
419415
loc.offset = state->offset;
416+
loc.print_filename = true;
420417
return loc;
421418
}
422419

@@ -1895,13 +1892,12 @@ Result BinaryReaderIR::OnGenericCustomSection(std::string_view name,
18951892

18961893
} // end anonymous namespace
18971894

1898-
Result ReadBinaryIr(const char* filename,
1899-
const void* data,
1895+
Result ReadBinaryIr(const void* data,
19001896
size_t size,
19011897
const ReadBinaryOptions& options,
19021898
Errors* errors,
19031899
Module* out_module) {
1904-
BinaryReaderIR reader(out_module, filename, errors);
1900+
BinaryReaderIR reader(out_module, errors);
19051901
return ReadBinary(data, size, &reader, options);
19061902
}
19071903

src/emscripten-helpers.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ wabt::WastLexer* wabt_new_wast_buffer_lexer(const char* filename,
9999
size_t size,
100100
wabt::Errors* errors) {
101101
std::unique_ptr<wabt::WastLexer> lexer =
102-
wabt::WastLexer::CreateBufferLexer(filename, data, size, errors);
102+
wabt::WastLexer::CreateBufferLexer(data, size, errors);
103103
return lexer.release();
104104
}
105105

@@ -138,8 +138,7 @@ WabtReadBinaryResult* wabt_read_binary(const void* data,
138138
wabt::Module* module = new wabt::Module();
139139
// TODO(binji): Pass through from wabt_read_binary parameter.
140140
const char* filename = "<binary>";
141-
result->result =
142-
wabt::ReadBinaryIr(filename, data, size, options, errors, module);
141+
result->result = wabt::ReadBinaryIr(data, size, options, errors, module);
143142
result->module.reset(module);
144143
return result;
145144
}

src/error-formatter.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace {
2222

2323
std::string FormatError(const Error& error,
2424
Location::Type location_type,
25+
const std::string_view filename,
2526
const Color& color,
2627
LexerSourceLineFinder* line_finder,
2728
int source_line_max_length,
@@ -32,8 +33,9 @@ std::string FormatError(const Error& error,
3233
result += color.MaybeBoldCode();
3334

3435
const Location& loc = error.loc;
35-
if (!loc.filename.empty()) {
36-
result += loc.filename;
36+
if ((location_type != Location::Type::Binary || loc.print_filename) &&
37+
!filename.empty()) {
38+
result += filename;
3739
result += ":";
3840
}
3941

@@ -84,6 +86,7 @@ std::string FormatError(const Error& error,
8486

8587
std::string FormatErrorsToString(const Errors& errors,
8688
Location::Type location_type,
89+
const std::string_view filename,
8790
LexerSourceLineFinder* line_finder,
8891
const Color& color,
8992
const std::string& header,
@@ -107,23 +110,24 @@ std::string FormatErrorsToString(const Errors& errors,
107110

108111
int indent = header.empty() ? 0 : 2;
109112

110-
result += FormatError(error, location_type, color, line_finder,
113+
result += FormatError(error, location_type, filename, color, line_finder,
111114
source_line_max_length, indent);
112115
}
113116
return result;
114117
}
115118

116119
void FormatErrorsToFile(const Errors& errors,
117120
Location::Type location_type,
121+
const std::string_view filename,
118122
LexerSourceLineFinder* line_finder,
119123
FILE* file,
120124
const std::string& header,
121125
PrintHeader print_header,
122126
int source_line_max_length) {
123127
Color color(file);
124128
std::string s =
125-
FormatErrorsToString(errors, location_type, line_finder, color, header,
126-
print_header, source_line_max_length);
129+
FormatErrorsToString(errors, location_type, filename, line_finder, color,
130+
header, print_header, source_line_max_length);
127131
fwrite(s.data(), 1, s.size(), file);
128132
}
129133

src/interp/binary-reader-interp.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class BinaryReaderInterp : public BinaryReaderNop {
7777
static constexpr Index kMaxPreallocatedBufferSize = 16384;
7878

7979
BinaryReaderInterp(ModuleDesc* module,
80-
std::string_view filename,
8180
Errors* errors,
8281
const Features& features);
8382

@@ -360,14 +359,12 @@ class BinaryReaderInterp : public BinaryReaderNop {
360359
std::vector<MemoryType> memory_types_; // Includes imported and defined.
361360
std::vector<GlobalType> global_types_; // Includes imported and defined.
362361
std::vector<TagType> tag_types_; // Includes imported and defined.
363-
364-
std::string_view filename_;
365362
};
366363

367364
Location BinaryReaderInterp::GetLocation() const {
368365
Location loc;
369-
loc.filename = filename_;
370366
loc.offset = state->offset;
367+
loc.print_filename = true;
371368
return loc;
372369
}
373370

@@ -391,14 +388,12 @@ void FixupMap::Resolve(Istream& istream, Index index) {
391388
}
392389

393390
BinaryReaderInterp::BinaryReaderInterp(ModuleDesc* module,
394-
std::string_view filename,
395391
Errors* errors,
396392
const Features& features)
397393
: errors_(errors),
398394
module_(*module),
399395
istream_(module->istream),
400-
validator_(errors, ValidateOptions(features)),
401-
filename_(filename) {}
396+
validator_(errors, ValidateOptions(features)) {}
402397

403398
Label* BinaryReaderInterp::GetLabel(Index depth) {
404399
assert(depth < label_stack_.size());
@@ -1814,13 +1809,12 @@ Result BinaryReaderInterp::OnDelegateExpr(Index depth) {
18141809

18151810
} // namespace
18161811

1817-
Result ReadBinaryInterp(std::string_view filename,
1818-
const void* data,
1812+
Result ReadBinaryInterp(const void* data,
18191813
size_t size,
18201814
const ReadBinaryOptions& options,
18211815
Errors* errors,
18221816
ModuleDesc* out_module) {
1823-
BinaryReaderInterp reader(out_module, filename, errors, options.features);
1817+
BinaryReaderInterp reader(out_module, errors, options.features);
18241818
return ReadBinary(data, size, &reader, options);
18251819
}
18261820

src/interp/interp-wasm-c-api.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,9 @@ own wasm_module_t* wasm_module_new(wasm_store_t* store,
639639
const wasm_byte_vec_t* binary) {
640640
Errors errors;
641641
ModuleDesc module_desc;
642-
if (Failed(ReadBinaryInterp("<internal>", binary->data, binary->size,
643-
GetOptions(), &errors, &module_desc))) {
644-
FormatErrorsToFile(errors, Location::Type::Binary);
642+
if (Failed(ReadBinaryInterp(binary->data, binary->size, GetOptions(), &errors,
643+
&module_desc))) {
644+
FormatErrorsToFile(errors, Location::Type::Binary, "<internal>");
645645
return nullptr;
646646
}
647647

0 commit comments

Comments
 (0)