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 "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/wasm/module-decoder.h" | 7 #include "src/wasm/module-decoder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 #include "src/wasm/wasm-opcodes.h" | 9 #include "src/wasm/wasm-opcodes.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace wasm { | 13 namespace wasm { |
14 | 14 |
15 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) | 15 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) |
| 16 #define EMPTY_BODY 0 |
| 17 #define NOP_BODY 2, 0, kExprNop |
16 #define VOID_VOID_SIG 0, kLocalVoid | 18 #define VOID_VOID_SIG 0, kLocalVoid |
17 #define INT_INT_SIG 1, kLocalI32, kLocalI32 | 19 #define INT_INT_SIG 1, kLocalI32, kLocalI32 |
18 | 20 |
19 #define EXPECT_VERIFIES(data) \ | 21 #define EXPECT_VERIFIES(data) \ |
20 do { \ | 22 do { \ |
21 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ | 23 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ |
22 EXPECT_TRUE(result.ok()); \ | 24 EXPECT_TRUE(result.ok()); \ |
23 if (result.val) delete result.val; \ | 25 if (result.val) delete result.val; \ |
24 } while (false) | 26 } while (false) |
25 | 27 |
(...skipping 1026 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 static const byte data[] = { | 1054 static const byte data[] = { |
1053 kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1, | 1055 kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1, |
1054 IMPORT_SIG_INDEX(0), // sig index | 1056 IMPORT_SIG_INDEX(0), // sig index |
1055 NAME_LENGTH(1), 'm', // module name | 1057 NAME_LENGTH(1), 'm', // module name |
1056 NAME_LENGTH(1), 'f', // function name | 1058 NAME_LENGTH(1), 'f', // function name |
1057 }; | 1059 }; |
1058 | 1060 |
1059 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); | 1061 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); |
1060 } | 1062 } |
1061 | 1063 |
1062 TEST_F(WasmModuleVerifyTest, ExportTable_empty) { | 1064 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) { |
| 1065 static const byte data[] = {kDeclSignatures, 1, VOID_VOID_SIG, |
| 1066 kDeclFunctions, 1, EMPTY_FUNCTION(0), |
| 1067 kDeclExportTable, 0}; |
| 1068 EXPECT_VERIFIES(data); |
| 1069 } |
| 1070 |
| 1071 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) { |
1063 static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0, | 1072 static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0, |
1064 kDeclExportTable, 0}; | 1073 kDeclExportTable, 0}; |
1065 EXPECT_VERIFIES(data); | 1074 // TODO(titzer): current behavior treats empty functions section as missing. |
| 1075 EXPECT_FAILURE(data); |
1066 } | 1076 } |
1067 | 1077 |
1068 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { | 1078 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { |
1069 static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0}; | 1079 static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0}; |
1070 EXPECT_FAILURE(data); | 1080 EXPECT_FAILURE(data); |
1071 } | 1081 } |
1072 | 1082 |
1073 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { | 1083 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { |
1074 static const byte data[] = {kDeclExportTable, 0}; | 1084 static const byte data[] = {kDeclExportTable, 0}; |
1075 EXPECT_FAILURE(data); | 1085 EXPECT_FAILURE(data); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1155 NO_NAME // -- | 1165 NO_NAME // -- |
1156 }; | 1166 }; |
1157 | 1167 |
1158 for (int length = 13; length < sizeof(data); length++) { | 1168 for (int length = 13; length < sizeof(data); length++) { |
1159 ModuleResult result = DecodeModule(data, data + length); | 1169 ModuleResult result = DecodeModule(data, data + length); |
1160 EXPECT_FALSE(result.ok()); | 1170 EXPECT_FALSE(result.ok()); |
1161 if (result.val) delete result.val; | 1171 if (result.val) delete result.val; |
1162 } | 1172 } |
1163 } | 1173 } |
1164 | 1174 |
| 1175 #define SIGNATURES_SECTION(count, ...) \ |
| 1176 kDeclSignatures, U32V_1(count), __VA_ARGS__ |
| 1177 #define FUNCTION_SIGNATURES_SECTION(count, ...) \ |
| 1178 kDeclFunctionSignatures, U32V_1(count), __VA_ARGS__ |
| 1179 #define FUNCTION_BODIES_SECTION(count, ...) \ |
| 1180 kDeclFunctionBodies, U32V_1(count), __VA_ARGS__ |
| 1181 #define NAMES_SECTION(count, ...) kDeclNames, U32V_1(count), __VA_ARGS__ |
| 1182 |
| 1183 #define FOO_STRING 3, 'f', 'o', 'o' |
| 1184 #define NO_LOCAL_NAMES 0 |
| 1185 |
| 1186 #define EMPTY_SIGNATURES_SECTION kDeclSignatures, 0 |
| 1187 #define EMPTY_FUNCTION_SIGNATURES_SECTION kDeclFunctionSignatures, 0 |
| 1188 #define EMPTY_FUNCTION_BODIES_SECTION kDeclFunctionBodies, 0 |
| 1189 #define EMPTY_NAMES_SECTION kDeclNames, 0 |
| 1190 |
| 1191 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) { |
| 1192 static const byte data[] = {kDeclSignatures, 0, kDeclFunctionSignatures, 0}; |
| 1193 EXPECT_VERIFIES(data); |
| 1194 } |
| 1195 |
| 1196 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) { |
| 1197 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
| 1198 FUNCTION_SIGNATURES_SECTION(1, 0)}; |
| 1199 EXPECT_VERIFIES(data); |
| 1200 } |
| 1201 |
| 1202 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) { |
| 1203 static const byte data[] = {EMPTY_SIGNATURES_SECTION, |
| 1204 EMPTY_FUNCTION_SIGNATURES_SECTION, |
| 1205 EMPTY_FUNCTION_BODIES_SECTION}; |
| 1206 EXPECT_VERIFIES(data); |
| 1207 } |
| 1208 |
| 1209 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) { |
| 1210 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
| 1211 FUNCTION_SIGNATURES_SECTION(1, 0), |
| 1212 FUNCTION_BODIES_SECTION(1, EMPTY_BODY)}; |
| 1213 EXPECT_VERIFIES(data); |
| 1214 } |
| 1215 |
| 1216 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) { |
| 1217 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
| 1218 FUNCTION_SIGNATURES_SECTION(1, 0), |
| 1219 FUNCTION_BODIES_SECTION(1, NOP_BODY)}; |
| 1220 EXPECT_VERIFIES(data); |
| 1221 } |
| 1222 |
| 1223 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) { |
| 1224 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
| 1225 FUNCTION_SIGNATURES_SECTION(2, 0, 0), |
| 1226 FUNCTION_BODIES_SECTION(1, EMPTY_BODY)}; |
| 1227 EXPECT_FAILURE(data); |
| 1228 } |
| 1229 |
| 1230 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) { |
| 1231 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
| 1232 FUNCTION_SIGNATURES_SECTION(1, 0), |
| 1233 FUNCTION_BODIES_SECTION(2, NOP_BODY, NOP_BODY)}; |
| 1234 EXPECT_FAILURE(data); |
| 1235 } |
| 1236 |
| 1237 TEST_F(WasmModuleVerifyTest, Names_empty) { |
| 1238 static const byte data[] = { |
| 1239 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION, |
| 1240 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION}; |
| 1241 EXPECT_VERIFIES(data); |
| 1242 } |
| 1243 |
| 1244 TEST_F(WasmModuleVerifyTest, Names_one_empty) { |
| 1245 static const byte data[] = { |
| 1246 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- |
| 1247 FUNCTION_SIGNATURES_SECTION(1, 0), // -- |
| 1248 FUNCTION_BODIES_SECTION(1, EMPTY_BODY), // -- |
| 1249 NAMES_SECTION(1, FOO_STRING, NO_LOCAL_NAMES) // -- |
| 1250 }; |
| 1251 EXPECT_VERIFIES(data); |
| 1252 } |
| 1253 |
| 1254 TEST_F(WasmModuleVerifyTest, Names_two_empty) { |
| 1255 static const byte data[] = { |
| 1256 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- |
| 1257 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- |
| 1258 FUNCTION_BODIES_SECTION(2, EMPTY_BODY, EMPTY_BODY), // -- |
| 1259 NAMES_SECTION(2, FOO_STRING, NO_LOCAL_NAMES, // -- |
| 1260 FOO_STRING, NO_LOCAL_NAMES) // -- |
| 1261 }; |
| 1262 EXPECT_VERIFIES(data); |
| 1263 } |
| 1264 |
1165 } // namespace wasm | 1265 } // namespace wasm |
1166 } // namespace internal | 1266 } // namespace internal |
1167 } // namespace v8 | 1267 } // namespace v8 |
OLD | NEW |