@@ -873,7 +873,14 @@ Instance::Ptr Instance::Instantiate(Store& store,
873
873
if (Failed (inst->CallInitFunc (store, func_ref, &value, out_trap))) {
874
874
return {};
875
875
}
876
- u32 offset = value.Get <u32>();
876
+
877
+ u64 offset;
878
+ if (table->type ().limits .is_64 ) {
879
+ offset = value.Get <u64>();
880
+ } else {
881
+ offset = value.Get <u32>();
882
+ }
883
+
877
884
if (pass == Check) {
878
885
result = table->IsValidRange (offset, segment.size ()) ? Result::Ok
879
886
: Result::Error;
@@ -888,8 +895,8 @@ Instance::Ptr Instance::Instantiate(Store& store,
888
895
*out_trap = Trap::New (
889
896
store, StringPrintf (
890
897
" out of bounds table access: elem segment is "
891
- " out of bounds: [%u , %" PRIu64 " ) >= max value %u" ,
892
- offset, u64{ offset} + segment.size (), table->size ()));
898
+ " out of bounds: [%" PRIu64 " , %" PRIu64 " ) >= max value %u" ,
899
+ offset, offset + segment.size (), table->size ()));
893
900
return {};
894
901
}
895
902
} else if (desc.mode == SegmentMode::Declared) {
@@ -1119,6 +1126,26 @@ u64 Thread::PopPtr(const Memory::Ptr& memory) {
1119
1126
return memory->type ().limits .is_64 ? Pop<u64>() : Pop<u32>();
1120
1127
}
1121
1128
1129
+ u64 Thread::PopPtr (const Table::Ptr & table) {
1130
+ return table->type ().limits .is_64 ? Pop<u64>() : Pop<u32>();
1131
+ }
1132
+
1133
+ void Thread::PushPtr (const Memory::Ptr & memory, u64 value) {
1134
+ if (memory->type ().limits .is_64 ) {
1135
+ Push<u64>(value);
1136
+ } else {
1137
+ Push<u32>(value);
1138
+ }
1139
+ }
1140
+
1141
+ void Thread::PushPtr (const Table::Ptr & table, u64 value) {
1142
+ if (table->type ().limits .is_64 ) {
1143
+ Push<u64>(value);
1144
+ } else {
1145
+ Push<u32>(value);
1146
+ }
1147
+ }
1148
+
1122
1149
template <typename T>
1123
1150
void WABT_VECTORCALL Thread::Push (T value) {
1124
1151
Push (Value::Make (value));
@@ -1190,7 +1217,7 @@ RunResult Thread::StepInternal(Trap::Ptr* out_trap) {
1190
1217
case O::ReturnCallIndirect: {
1191
1218
Table::Ptr table{store_, inst_->tables ()[instr.imm_u32x2 .fst ]};
1192
1219
auto && func_type = mod_->desc ().func_types [instr.imm_u32x2 .snd ];
1193
- auto entry = Pop<u32>( );
1220
+ u64 entry = PopPtr (table );
1194
1221
TRAP_IF (entry >= table->elements ().size (), " undefined table index" );
1195
1222
auto new_func_ref = table->elements ()[entry];
1196
1223
TRAP_IF (new_func_ref == Ref::Null, " uninitialized table element" );
@@ -1273,29 +1300,17 @@ RunResult Thread::StepInternal(Trap::Ptr* out_trap) {
1273
1300
1274
1301
case O::MemorySize: {
1275
1302
Memory::Ptr memory{store_, inst_->memories ()[instr.imm_u32 ]};
1276
- if (memory->type ().limits .is_64 ) {
1277
- Push<u64>(memory->PageSize ());
1278
- } else {
1279
- Push<u32>(static_cast <u32>(memory->PageSize ()));
1280
- }
1303
+ PushPtr (memory, memory->PageSize ());
1281
1304
break ;
1282
1305
}
1283
1306
1284
1307
case O::MemoryGrow: {
1285
1308
Memory::Ptr memory{store_, inst_->memories ()[instr.imm_u32 ]};
1286
1309
u64 old_size = memory->PageSize ();
1287
- if (memory->type ().limits .is_64 ) {
1288
- if (Failed (memory->Grow (Pop<u64>()))) {
1289
- Push<s64>(-1 );
1290
- } else {
1291
- Push<u64>(old_size);
1292
- }
1310
+ if (Failed (memory->Grow (PopPtr (memory)))) {
1311
+ PushPtr (memory, -1 );
1293
1312
} else {
1294
- if (Failed (memory->Grow (Pop<u32>()))) {
1295
- Push<s32>(-1 );
1296
- } else {
1297
- Push<u32>(old_size);
1298
- }
1313
+ PushPtr (memory, old_size);
1299
1314
}
1300
1315
break ;
1301
1316
}
@@ -2071,7 +2086,7 @@ RunResult Thread::DoMemoryInit(Instr instr, Trap::Ptr* out_trap) {
2071
2086
auto && data = inst_->datas ()[instr.imm_u32x2 .snd ];
2072
2087
auto size = Pop<u32>();
2073
2088
auto src = Pop<u32>();
2074
- auto dst = PopPtr (memory);
2089
+ u64 dst = PopPtr (memory);
2075
2090
TRAP_IF (Failed (memory->Init (dst, data, src, size)),
2076
2091
" out of bounds memory access: memory.init out of bounds" );
2077
2092
return RunResult::Ok;
@@ -2085,9 +2100,9 @@ RunResult Thread::DoDataDrop(Instr instr) {
2085
2100
RunResult Thread::DoMemoryCopy (Instr instr, Trap::Ptr * out_trap) {
2086
2101
Memory::Ptr mem_dst{store_, inst_->memories ()[instr.imm_u32x2 .fst ]};
2087
2102
Memory::Ptr mem_src{store_, inst_->memories ()[instr.imm_u32x2 .snd ]};
2088
- auto size = PopPtr (mem_src);
2089
- auto src = PopPtr (mem_src);
2090
- auto dst = PopPtr (mem_dst);
2103
+ u64 size = PopPtr (mem_src);
2104
+ u64 src = PopPtr (mem_src);
2105
+ u64 dst = PopPtr (mem_dst);
2091
2106
// TODO: change to "out of bounds"
2092
2107
TRAP_IF (Failed (Memory::Copy (*mem_dst, dst, *mem_src, src, size)),
2093
2108
" out of bounds memory access: memory.copy out of bound" );
@@ -2096,9 +2111,9 @@ RunResult Thread::DoMemoryCopy(Instr instr, Trap::Ptr* out_trap) {
2096
2111
2097
2112
RunResult Thread::DoMemoryFill (Instr instr, Trap::Ptr * out_trap) {
2098
2113
Memory::Ptr memory{store_, inst_->memories ()[instr.imm_u32 ]};
2099
- auto size = PopPtr (memory);
2114
+ u64 size = PopPtr (memory);
2100
2115
auto value = Pop<u32>();
2101
- auto dst = PopPtr (memory);
2116
+ u64 dst = PopPtr (memory);
2102
2117
TRAP_IF (Failed (memory->Fill (dst, value, size)),
2103
2118
" out of bounds memory access: memory.fill out of bounds" );
2104
2119
return RunResult::Ok;
@@ -2109,7 +2124,7 @@ RunResult Thread::DoTableInit(Instr instr, Trap::Ptr* out_trap) {
2109
2124
auto && elem = inst_->elems ()[instr.imm_u32x2 .snd ];
2110
2125
auto size = Pop<u32>();
2111
2126
auto src = Pop<u32>();
2112
- auto dst = Pop<u32>( );
2127
+ u64 dst = PopPtr (table );
2113
2128
TRAP_IF (Failed (table->Init (store_, dst, elem, src, size)),
2114
2129
" out of bounds table access: table.init out of bounds" );
2115
2130
return RunResult::Ok;
@@ -2123,61 +2138,61 @@ RunResult Thread::DoElemDrop(Instr instr) {
2123
2138
RunResult Thread::DoTableCopy (Instr instr, Trap::Ptr * out_trap) {
2124
2139
Table::Ptr table_dst{store_, inst_->tables ()[instr.imm_u32x2 .fst ]};
2125
2140
Table::Ptr table_src{store_, inst_->tables ()[instr.imm_u32x2 .snd ]};
2126
- auto size = Pop<u32>( );
2127
- auto src = Pop<u32>( );
2128
- auto dst = Pop<u32>( );
2141
+ u64 size = PopPtr (table_src );
2142
+ u64 src = PopPtr (table_src );
2143
+ u64 dst = PopPtr (table_dst );
2129
2144
TRAP_IF (Failed (Table::Copy (store_, *table_dst, dst, *table_src, src, size)),
2130
2145
" out of bounds table access: table.copy out of bounds" );
2131
2146
return RunResult::Ok;
2132
2147
}
2133
2148
2134
2149
RunResult Thread::DoTableGet (Instr instr, Trap::Ptr * out_trap) {
2135
2150
Table::Ptr table{store_, inst_->tables ()[instr.imm_u32 ]};
2136
- auto index = Pop<u32>( );
2151
+ u64 index = PopPtr (table );
2137
2152
Ref ref;
2138
2153
TRAP_IF (Failed (table->Get (index , &ref)),
2139
- StringPrintf (
2140
- " out of bounds table access: table.get at %u >= max value %u" ,
2141
- index , table->size ()));
2154
+ StringPrintf (" out of bounds table access: table.get at % " PRIu64
2155
+ " >= max value %u" ,
2156
+ index , table->size ()));
2142
2157
Push (ref);
2143
2158
return RunResult::Ok;
2144
2159
}
2145
2160
2146
2161
RunResult Thread::DoTableSet (Instr instr, Trap::Ptr * out_trap) {
2147
2162
Table::Ptr table{store_, inst_->tables ()[instr.imm_u32 ]};
2148
2163
auto ref = Pop<Ref>();
2149
- auto index = Pop<u32>( );
2164
+ u64 index = PopPtr (table );
2150
2165
TRAP_IF (Failed (table->Set (store_, index , ref)),
2151
- StringPrintf (
2152
- " out of bounds table access: table.set at %u >= max value %u" ,
2153
- index , table->size ()));
2166
+ StringPrintf (" out of bounds table access: table.set at % " PRIu64
2167
+ " >= max value %u" ,
2168
+ index , table->size ()));
2154
2169
return RunResult::Ok;
2155
2170
}
2156
2171
2157
2172
RunResult Thread::DoTableGrow (Instr instr, Trap::Ptr * out_trap) {
2158
2173
Table::Ptr table{store_, inst_->tables ()[instr.imm_u32 ]};
2159
2174
u32 old_size = table->size ();
2160
- auto delta = Pop<u32>( );
2175
+ auto delta = PopPtr (table );
2161
2176
auto ref = Pop<Ref>();
2162
2177
if (Failed (table->Grow (store_, delta, ref))) {
2163
- Push<s32>( -1 );
2178
+ PushPtr (table, -1 );
2164
2179
} else {
2165
- Push<u32>( old_size);
2180
+ PushPtr (table, old_size);
2166
2181
}
2167
2182
return RunResult::Ok;
2168
2183
}
2169
2184
2170
2185
RunResult Thread::DoTableSize (Instr instr) {
2171
2186
Table::Ptr table{store_, inst_->tables ()[instr.imm_u32 ]};
2172
- Push<u32>( table->size ());
2187
+ PushPtr (table, table->size ());
2173
2188
return RunResult::Ok;
2174
2189
}
2175
2190
2176
2191
RunResult Thread::DoTableFill (Instr instr, Trap::Ptr * out_trap) {
2177
2192
Table::Ptr table{store_, inst_->tables ()[instr.imm_u32 ]};
2178
- auto size = Pop<u32>( );
2193
+ u64 size = PopPtr (table );
2179
2194
auto value = Pop<Ref>();
2180
- auto dst = Pop<u32>( );
2195
+ u64 dst = PopPtr (table );
2181
2196
TRAP_IF (Failed (table->Fill (store_, dst, value, size)),
2182
2197
" out of bounds table access: table.fill out of bounds" );
2183
2198
return RunResult::Ok;
0 commit comments