summaryrefslogtreecommitdiff
path: root/src/binary-reader.cc
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2017-08-31 17:40:40 -0700
committerGitHub <noreply@github.com>2017-08-31 17:40:40 -0700
commitcd9f0a60b5c869673a843f1c0ff47068661b5440 (patch)
tree7d15efd724484f9b84cd7ee2ef36daea47f54c85 /src/binary-reader.cc
parent03a3c76144192dcc53826ba1cefb35c6502e407e (diff)
downloadwabt-cd9f0a60b5c869673a843f1c0ff47068661b5440.tar.gz
wabt-cd9f0a60b5c869673a843f1c0ff47068661b5440.tar.bz2
wabt-cd9f0a60b5c869673a843f1c0ff47068661b5440.zip
Add type check to data segment offset (#525)
This is a conservative check that we can do in the binary reader itself. More extensive checking is still done in the interpreter (i.e. vefiying the type of the global).
Diffstat (limited to 'src/binary-reader.cc')
-rw-r--r--src/binary-reader.cc20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc
index d17751c1..c484ae4d 100644
--- a/src/binary-reader.cc
+++ b/src/binary-reader.cc
@@ -176,7 +176,8 @@ class BinaryReader {
Index NumTotalMemories();
Index NumTotalGlobals();
- Result ReadInitExpr(Index index) WABT_WARN_UNUSED;
+ Result ReadI32InitExpr(Index index) WABT_WARN_UNUSED;
+ Result ReadInitExpr(Index index, bool require_i32 = false) WABT_WARN_UNUSED;
Result ReadTable(Type* out_elem_type,
Limits* out_elem_limits) WABT_WARN_UNUSED;
Result ReadMemory(Limits* out_page_limits) WABT_WARN_UNUSED;
@@ -492,9 +493,14 @@ Index BinaryReader::NumTotalGlobals() {
return num_global_imports_ + num_globals_;
}
-Result BinaryReader::ReadInitExpr(Index index) {
+Result BinaryReader::ReadI32InitExpr(Index index) {
+ return ReadInitExpr(index, true);
+}
+
+Result BinaryReader::ReadInitExpr(Index index, bool require_i32) {
Opcode opcode;
CHECK_RESULT(ReadOpcode(&opcode, "opcode"));
+
switch (opcode) {
case Opcode::I32Const: {
uint32_t value = 0;
@@ -538,6 +544,12 @@ Result BinaryReader::ReadInitExpr(Index index) {
return ReportUnexpectedOpcode(opcode, "in initializer expression");
}
+ if (require_i32 && opcode != Opcode::I32Const &&
+ opcode != Opcode::GetGlobal) {
+ PrintError("expected i32 init_expr");
+ return Result::Error;
+ }
+
CHECK_RESULT(ReadOpcode(&opcode, "opcode"));
ERROR_UNLESS(opcode == Opcode::End,
"expected END opcode after initializer expression");
@@ -1581,7 +1593,7 @@ Result BinaryReader::ReadElemSection(Offset section_size) {
CHECK_RESULT(ReadIndex(&table_index, "elem segment table index"));
CALLBACK(BeginElemSegment, i, table_index);
CALLBACK(BeginElemSegmentInitExpr, i);
- CHECK_RESULT(ReadInitExpr(i));
+ CHECK_RESULT(ReadI32InitExpr(i));
CALLBACK(EndElemSegmentInitExpr, i);
Index num_function_indexes;
@@ -1647,7 +1659,7 @@ Result BinaryReader::ReadDataSection(Offset section_size) {
CHECK_RESULT(ReadIndex(&memory_index, "data segment memory index"));
CALLBACK(BeginDataSegment, i, memory_index);
CALLBACK(BeginDataSegmentInitExpr, i);
- CHECK_RESULT(ReadInitExpr(i));
+ CHECK_RESULT(ReadI32InitExpr(i));
CALLBACK(EndDataSegmentInitExpr, i);
Address data_size;