| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/wasm/module-decoder.h" | 5 #include "src/wasm/module-decoder.h" |
| 6 #include "src/wasm/function-body-decoder-impl.h" | 6 #include "src/wasm/function-body-decoder-impl.h" |
| 7 | 7 |
| 8 #include "src/base/functional.h" | 8 #include "src/base/functional.h" |
| 9 #include "src/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
| 10 #include "src/counters.h" | 10 #include "src/counters.h" |
| (...skipping 1119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 ValueType* buffer = | 1130 ValueType* buffer = |
| 1131 module_zone_->NewArray<ValueType>(param_count + return_count); | 1131 module_zone_->NewArray<ValueType>(param_count + return_count); |
| 1132 uint32_t b = 0; | 1132 uint32_t b = 0; |
| 1133 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; | 1133 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; |
| 1134 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; | 1134 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; |
| 1135 | 1135 |
| 1136 return new (module_zone_) FunctionSig(return_count, param_count, buffer); | 1136 return new (module_zone_) FunctionSig(return_count, param_count, buffer); |
| 1137 } | 1137 } |
| 1138 }; | 1138 }; |
| 1139 | 1139 |
| 1140 } // namespace | 1140 ModuleResult DecodeWasmModuleInternal(Isolate* isolate, |
| 1141 | 1141 const byte* module_start, |
| 1142 ModuleResult DecodeWasmModule(Isolate* isolate, const byte* module_start, | 1142 const byte* module_end, |
| 1143 const byte* module_end, bool verify_functions, | 1143 bool verify_functions, |
| 1144 ModuleOrigin origin) { | 1144 ModuleOrigin origin, bool is_sync) { |
| 1145 HistogramTimerScope wasm_decode_module_time_scope( | |
| 1146 IsWasm(origin) ? isolate->counters()->wasm_decode_wasm_module_time() | |
| 1147 : isolate->counters()->wasm_decode_asm_module_time()); | |
| 1148 size_t size = module_end - module_start; | 1145 size_t size = module_end - module_start; |
| 1149 if (module_start > module_end) return ModuleResult::Error("start > end"); | 1146 if (module_start > module_end) return ModuleResult::Error("start > end"); |
| 1150 if (size >= kV8MaxWasmModuleSize) | 1147 if (size >= kV8MaxWasmModuleSize) |
| 1151 return ModuleResult::Error("size > maximum module size: %zu", size); | 1148 return ModuleResult::Error("size > maximum module size: %zu", size); |
| 1152 // TODO(bradnelson): Improve histogram handling of size_t. | 1149 // TODO(bradnelson): Improve histogram handling of size_t. |
| 1153 (IsWasm(origin) ? isolate->counters()->wasm_wasm_module_size_bytes() | 1150 if (is_sync) |
| 1154 : isolate->counters()->wasm_asm_module_size_bytes()) | 1151 // TODO(karlschimpf): Make this work when asynchronous. |
| 1155 ->AddSample(static_cast<int>(size)); | 1152 // https://bugs.chromium.org/p/v8/issues/detail?id=6361 |
| 1153 (IsWasm(origin) ? isolate->counters()->wasm_wasm_module_size_bytes() |
| 1154 : isolate->counters()->wasm_asm_module_size_bytes()) |
| 1155 ->AddSample(static_cast<int>(size)); |
| 1156 // Signatures are stored in zone memory, which have the same lifetime | 1156 // Signatures are stored in zone memory, which have the same lifetime |
| 1157 // as the {module}. | 1157 // as the {module}. |
| 1158 Zone* zone = new Zone(isolate->allocator(), ZONE_NAME); | 1158 Zone* zone = new Zone(isolate->allocator(), ZONE_NAME); |
| 1159 ModuleDecoder decoder(zone, module_start, module_end, origin); | 1159 ModuleDecoder decoder(zone, module_start, module_end, origin); |
| 1160 ModuleResult result = decoder.DecodeModule(verify_functions); | 1160 ModuleResult result = decoder.DecodeModule(verify_functions); |
| 1161 // TODO(bradnelson): Improve histogram handling of size_t. | 1161 // TODO(bradnelson): Improve histogram handling of size_t. |
| 1162 // TODO(titzer): this isn't accurate, since it doesn't count the data | 1162 // TODO(titzer): this isn't accurate, since it doesn't count the data |
| 1163 // allocated on the C++ heap. | 1163 // allocated on the C++ heap. |
| 1164 // https://bugs.chromium.org/p/chromium/issues/detail?id=657320 | 1164 // https://bugs.chromium.org/p/chromium/issues/detail?id=657320 |
| 1165 (IsWasm(origin) | 1165 if (is_sync) |
| 1166 ? isolate->counters()->wasm_decode_wasm_module_peak_memory_bytes() | 1166 // TODO(karlschimpf): Make this work when asynchronous. |
| 1167 : isolate->counters()->wasm_decode_asm_module_peak_memory_bytes()) | 1167 // https://bugs.chromium.org/p/v8/issues/detail?id=6361 |
| 1168 ->AddSample(static_cast<int>(zone->allocation_size())); | 1168 (IsWasm(origin) |
| 1169 ? isolate->counters()->wasm_decode_wasm_module_peak_memory_bytes() |
| 1170 : isolate->counters()->wasm_decode_asm_module_peak_memory_bytes()) |
| 1171 ->AddSample(static_cast<int>(zone->allocation_size())); |
| 1169 return result; | 1172 return result; |
| 1170 } | 1173 } |
| 1171 | 1174 |
| 1175 } // namespace |
| 1176 |
| 1177 ModuleResult DecodeWasmModule(Isolate* isolate, const byte* module_start, |
| 1178 const byte* module_end, bool verify_functions, |
| 1179 ModuleOrigin origin, bool is_sync) { |
| 1180 if (is_sync) { |
| 1181 // TODO(karlschimpf): Make this work when asynchronous. |
| 1182 // https://bugs.chromium.org/p/v8/issues/detail?id=6361 |
| 1183 HistogramTimerScope wasm_decode_module_time_scope( |
| 1184 IsWasm(origin) ? isolate->counters()->wasm_decode_wasm_module_time() |
| 1185 : isolate->counters()->wasm_decode_asm_module_time()); |
| 1186 return DecodeWasmModuleInternal(isolate, module_start, module_end, |
| 1187 verify_functions, origin, true); |
| 1188 } |
| 1189 return DecodeWasmModuleInternal(isolate, module_start, module_end, |
| 1190 verify_functions, origin, false); |
| 1191 } |
| 1192 |
| 1172 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, | 1193 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, |
| 1173 const byte* end) { | 1194 const byte* end) { |
| 1174 ModuleDecoder decoder(zone, start, end, kWasmOrigin); | 1195 ModuleDecoder decoder(zone, start, end, kWasmOrigin); |
| 1175 return decoder.DecodeFunctionSignature(start); | 1196 return decoder.DecodeFunctionSignature(start); |
| 1176 } | 1197 } |
| 1177 | 1198 |
| 1178 WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, const byte* end) { | 1199 WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, const byte* end) { |
| 1179 AccountingAllocator allocator; | 1200 AccountingAllocator allocator; |
| 1180 Zone zone(&allocator, ZONE_NAME); | 1201 Zone zone(&allocator, ZONE_NAME); |
| 1181 ModuleDecoder decoder(&zone, start, end, kWasmOrigin); | 1202 ModuleDecoder decoder(&zone, start, end, kWasmOrigin); |
| 1182 return decoder.DecodeInitExpr(start); | 1203 return decoder.DecodeInitExpr(start); |
| 1183 } | 1204 } |
| 1184 | 1205 |
| 1185 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, | 1206 namespace { |
| 1186 ModuleBytesEnv* module_env, | 1207 |
| 1187 const byte* function_start, | 1208 FunctionResult DecodeWasmFunctionInternal(Isolate* isolate, Zone* zone, |
| 1188 const byte* function_end) { | 1209 ModuleBytesEnv* module_env, |
| 1189 bool is_wasm = module_env->module_env.is_wasm(); | 1210 const byte* function_start, |
| 1190 HistogramTimerScope wasm_decode_function_time_scope( | 1211 const byte* function_end, |
| 1191 is_wasm ? isolate->counters()->wasm_decode_wasm_function_time() | 1212 bool is_sync) { |
| 1192 : isolate->counters()->wasm_decode_asm_function_time()); | |
| 1193 size_t size = function_end - function_start; | 1213 size_t size = function_end - function_start; |
| 1194 if (function_start > function_end) | 1214 if (function_start > function_end) |
| 1195 return FunctionResult::Error("start > end"); | 1215 return FunctionResult::Error("start > end"); |
| 1196 if (size > kV8MaxWasmFunctionSize) | 1216 if (size > kV8MaxWasmFunctionSize) |
| 1197 return FunctionResult::Error("size > maximum function size: %zu", size); | 1217 return FunctionResult::Error("size > maximum function size: %zu", size); |
| 1198 (is_wasm ? isolate->counters()->wasm_wasm_function_size_bytes() | 1218 if (is_sync) { |
| 1199 : isolate->counters()->wasm_asm_function_size_bytes()) | 1219 // TODO(karlschimpf): Make this work when asynchronous. |
| 1200 ->AddSample(static_cast<int>(size)); | 1220 // https://bugs.chromium.org/p/v8/issues/detail?id=6361 |
| 1221 bool is_wasm = module_env->module_env.is_wasm(); |
| 1222 (is_wasm ? isolate->counters()->wasm_wasm_function_size_bytes() |
| 1223 : isolate->counters()->wasm_asm_function_size_bytes()) |
| 1224 ->AddSample(static_cast<int>(size)); |
| 1225 } |
| 1201 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); | 1226 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); |
| 1202 return decoder.DecodeSingleFunction( | 1227 return decoder.DecodeSingleFunction( |
| 1203 module_env, std::unique_ptr<WasmFunction>(new WasmFunction())); | 1228 module_env, std::unique_ptr<WasmFunction>(new WasmFunction())); |
| 1204 } | 1229 } |
| 1205 | 1230 |
| 1231 } // namespace |
| 1232 |
| 1233 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, |
| 1234 ModuleBytesEnv* module_env, |
| 1235 const byte* function_start, |
| 1236 const byte* function_end, bool is_sync) { |
| 1237 if (is_sync) { |
| 1238 // TODO(karlschimpf): Make this work when asynchronous. |
| 1239 // https://bugs.chromium.org/p/v8/issues/detail?id=6361 |
| 1240 size_t size = function_end - function_start; |
| 1241 bool is_wasm = module_env->module_env.is_wasm(); |
| 1242 (is_wasm ? isolate->counters()->wasm_wasm_function_size_bytes() |
| 1243 : isolate->counters()->wasm_asm_function_size_bytes()) |
| 1244 ->AddSample(static_cast<int>(size)); |
| 1245 HistogramTimerScope wasm_decode_function_time_scope( |
| 1246 is_wasm ? isolate->counters()->wasm_decode_wasm_function_time() |
| 1247 : isolate->counters()->wasm_decode_asm_function_time()); |
| 1248 return DecodeWasmFunctionInternal(isolate, zone, module_env, function_start, |
| 1249 function_end, true); |
| 1250 } |
| 1251 return DecodeWasmFunctionInternal(isolate, zone, module_env, function_start, |
| 1252 function_end, false); |
| 1253 } |
| 1254 |
| 1206 AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* tables_start, | 1255 AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* tables_start, |
| 1207 const byte* tables_end) { | 1256 const byte* tables_end) { |
| 1208 AsmJsOffsets table; | 1257 AsmJsOffsets table; |
| 1209 | 1258 |
| 1210 Decoder decoder(tables_start, tables_end); | 1259 Decoder decoder(tables_start, tables_end); |
| 1211 uint32_t functions_count = decoder.consume_u32v("functions count"); | 1260 uint32_t functions_count = decoder.consume_u32v("functions count"); |
| 1212 // Reserve space for the entries, taking care of invalid input. | 1261 // Reserve space for the entries, taking care of invalid input. |
| 1213 if (functions_count < static_cast<unsigned>(tables_end - tables_start)) { | 1262 if (functions_count < static_cast<unsigned>(tables_end - tables_start)) { |
| 1214 table.reserve(functions_count); | 1263 table.reserve(functions_count); |
| 1215 } | 1264 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1279 result.push_back({section_start, name_offset, name_length, payload_offset, | 1328 result.push_back({section_start, name_offset, name_length, payload_offset, |
| 1280 payload_length, section_length}); | 1329 payload_length, section_length}); |
| 1281 } | 1330 } |
| 1282 | 1331 |
| 1283 return result; | 1332 return result; |
| 1284 } | 1333 } |
| 1285 | 1334 |
| 1286 } // namespace wasm | 1335 } // namespace wasm |
| 1287 } // namespace internal | 1336 } // namespace internal |
| 1288 } // namespace v8 | 1337 } // namespace v8 |
| OLD | NEW |