Skip to content

Commit 6be9e86

Browse files
authored
Prevent fuzzer allocation errors (#2713)
1 parent dbd22f8 commit 6be9e86

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/interp/binary-reader-interp.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ struct FixupMap {
7373

7474
class BinaryReaderInterp : public BinaryReaderNop {
7575
public:
76+
// Prevent too much memory allocation errors by fuzzers.
77+
static constexpr Index kMaxPreallocatedBufferSize = 16384;
78+
7679
BinaryReaderInterp(ModuleDesc* module,
7780
std::string_view filename,
7881
Errors* errors,
@@ -525,7 +528,7 @@ Result BinaryReaderInterp::EndModule() {
525528
}
526529

527530
Result BinaryReaderInterp::OnTypeCount(Index count) {
528-
module_.func_types.reserve(count);
531+
module_.func_types.reserve(std::min(count, kMaxPreallocatedBufferSize));
529532
return Result::Ok;
530533
}
531534

@@ -615,7 +618,7 @@ Result BinaryReaderInterp::OnImportTag(Index import_index,
615618
}
616619

617620
Result BinaryReaderInterp::OnFunctionCount(Index count) {
618-
module_.funcs.reserve(count);
621+
module_.funcs.reserve(std::min(count, kMaxPreallocatedBufferSize));
619622
return Result::Ok;
620623
}
621624

@@ -629,7 +632,7 @@ Result BinaryReaderInterp::OnFunction(Index index, Index sig_index) {
629632
}
630633

631634
Result BinaryReaderInterp::OnTableCount(Index count) {
632-
module_.tables.reserve(count);
635+
module_.tables.reserve(std::min(count, kMaxPreallocatedBufferSize));
633636
return Result::Ok;
634637
}
635638

@@ -662,7 +665,7 @@ Result BinaryReaderInterp::EndTableInitExpr(Index index) {
662665
}
663666

664667
Result BinaryReaderInterp::OnMemoryCount(Index count) {
665-
module_.memories.reserve(count);
668+
module_.memories.reserve(std::min(count, kMaxPreallocatedBufferSize));
666669
return Result::Ok;
667670
}
668671

@@ -677,7 +680,7 @@ Result BinaryReaderInterp::OnMemory(Index index,
677680
}
678681

679682
Result BinaryReaderInterp::OnGlobalCount(Index count) {
680-
module_.globals.reserve(count);
683+
module_.globals.reserve(std::min(count, kMaxPreallocatedBufferSize));
681684
return Result::Ok;
682685
}
683686

@@ -719,7 +722,7 @@ Result BinaryReaderInterp::EndGlobalInitExpr(Index index) {
719722
}
720723

721724
Result BinaryReaderInterp::OnTagCount(Index count) {
722-
module_.tags.reserve(count);
725+
module_.tags.reserve(std::min(count, kMaxPreallocatedBufferSize));
723726
return Result::Ok;
724727
}
725728

@@ -760,7 +763,7 @@ Result BinaryReaderInterp::OnStartFunction(Index func_index) {
760763
}
761764

762765
Result BinaryReaderInterp::OnElemSegmentCount(Index count) {
763-
module_.elems.reserve(count);
766+
module_.elems.reserve(std::min(count, kMaxPreallocatedBufferSize));
764767
return Result::Ok;
765768
}
766769

@@ -820,7 +823,7 @@ Result BinaryReaderInterp::EndElemExpr(Index elem_index, Index expr_index) {
820823

821824
Result BinaryReaderInterp::OnDataCount(Index count) {
822825
validator_.OnDataCount(count);
823-
module_.datas.reserve(count);
826+
module_.datas.reserve(std::min(count, kMaxPreallocatedBufferSize));
824827
return Result::Ok;
825828
}
826829

0 commit comments

Comments
 (0)