1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CONTAINERSVECTORCOMMON_FUZZER_H 17 #define CONTAINERSVECTORCOMMON_FUZZER_H 18 19 #include "ecmascript/containers/containers_vector.h" 20 #include "ecmascript/containers/containers_private.h" 21 #include "ecmascript/ecma_string-inl.h" 22 #include "ecmascript/ecma_vm.h" 23 #include "ecmascript/global_env.h" 24 #include "ecmascript/js_api/js_api_vector.h" 25 #include "ecmascript/js_api/js_api_vector_iterator.h" 26 #include "ecmascript/js_handle.h" 27 #include "ecmascript/napi/include/jsnapi.h" 28 #include "ecmascript/ecma_runtime_call_info.h" 29 #include "ecmascript/js_thread.h" 30 #include "ecmascript/object_factory.h" 31 32 #define MAXBYTELEN sizeof(unsigned int) 33 34 namespace panda::ecmascript { 35 using namespace panda::ecmascript::containers; 36 class ContainersVectorFuzzTestHelper { 37 public: JSObjectCreate(JSThread *thread)38 static JSFunction *JSObjectCreate(JSThread *thread) 39 { 40 EcmaVM *ecmaVM = thread->GetEcmaVM(); 41 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 42 return globalEnv->GetObjectFunction().GetObject<JSFunction>(); 43 } 44 CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)45 static EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs) 46 { 47 auto factory = thread->GetEcmaVM()->GetFactory(); 48 JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread)); 49 JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass)); 50 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 51 EcmaRuntimeCallInfo *objCallInfo = 52 EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs); 53 return objCallInfo; 54 } 55 CreateJSAPIVector(JSThread *thread)56 static JSHandle<JSAPIVector> CreateJSAPIVector(JSThread *thread) 57 { 58 auto factory = thread->GetEcmaVM()->GetFactory(); 59 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 60 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject(); 61 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate")); 62 JSHandle<JSTaggedValue> value = 63 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue(); 64 65 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 66 objCallInfo->SetFunction(JSTaggedValue::Undefined()); 67 objCallInfo->SetThis(value.GetTaggedValue()); 68 // 0 means the argument 69 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::Vector))); 70 JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo); 71 72 JSHandle<JSFunction> newTarget(thread, result); 73 auto objCallInfo2 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 74 objCallInfo2->SetFunction(newTarget.GetTaggedValue()); 75 objCallInfo2->SetNewTarget(newTarget.GetTaggedValue()); 76 objCallInfo2->SetThis(JSTaggedValue::Undefined()); 77 objCallInfo2->SetCallArg(0, JSTaggedValue::Undefined()); 78 79 JSTaggedValue list = containers::ContainersVector::VectorConstructor(objCallInfo2); 80 JSHandle<JSAPIVector> vector(thread, list); 81 return vector; 82 } 83 GetVectorWithData(JSThread *thread, const uint8_t* data, size_t size)84 static JSHandle<JSAPIVector> GetVectorWithData(JSThread *thread, const uint8_t* data, size_t size) 85 { 86 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 87 88 constexpr int32_t ELEMENT_NUMS = 8; 89 90 uint32_t input = 0; 91 if (size > MAXBYTELEN) { 92 size = MAXBYTELEN; 93 } 94 if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) { 95 std::cout << "memcpy_s failed!"; 96 UNREACHABLE(); 97 } 98 99 for (int32_t i = 0; i < ELEMENT_NUMS; i++) { 100 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 101 callInfo->SetFunction(JSTaggedValue::Undefined()); 102 callInfo->SetThis(vector.GetTaggedValue()); 103 callInfo->SetCallArg(0, JSTaggedValue(i + input)); 104 105 ContainersVector::Add(callInfo); 106 } 107 108 return vector; 109 } 110 ContainersVectorAddFuzzTest(const uint8_t* data, size_t size)111 static void ContainersVectorAddFuzzTest(const uint8_t* data, size_t size) 112 { 113 RuntimeOption option; 114 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 115 EcmaVM *vm = JSNApi::CreateJSVM(option); 116 { 117 JsiFastNativeScope scope(vm); 118 auto thread = vm->GetAssociatedJSThread(); 119 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 120 121 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 122 callInfo->SetFunction(JSTaggedValue::Undefined()); 123 callInfo->SetThis(vector.GetTaggedValue()); 124 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 125 std::string str(data, data + size); 126 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 127 callInfo->SetCallArg(0, value); 128 129 ContainersVector::Add(callInfo); 130 } 131 JSNApi::DestroyJSVM(vm); 132 } 133 ContainersVectorGetFirstElementFuzzTest(const uint8_t* data, size_t size)134 static void ContainersVectorGetFirstElementFuzzTest(const uint8_t* data, size_t size) 135 { 136 RuntimeOption option; 137 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 138 EcmaVM *vm = JSNApi::CreateJSVM(option); 139 { 140 JsiFastNativeScope scope(vm); 141 auto thread = vm->GetAssociatedJSThread(); 142 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 143 144 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 145 callInfo->SetFunction(JSTaggedValue::Undefined()); 146 callInfo->SetThis(vector.GetTaggedValue()); 147 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 148 std::string str(data, data + size); 149 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 150 callInfo->SetCallArg(0, value); 151 152 ContainersVector::Add(callInfo); 153 154 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 155 callInfo1->SetFunction(JSTaggedValue::Undefined()); 156 callInfo1->SetThis(vector.GetTaggedValue()); 157 158 ContainersVector::GetFirstElement(callInfo1); 159 } 160 JSNApi::DestroyJSVM(vm); 161 } 162 ContainersVectorGetIndexOfFuzzTest(const uint8_t* data, size_t size)163 static void ContainersVectorGetIndexOfFuzzTest(const uint8_t* data, size_t size) 164 { 165 RuntimeOption option; 166 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 167 EcmaVM *vm = JSNApi::CreateJSVM(option); 168 { 169 JsiFastNativeScope scope(vm); 170 auto thread = vm->GetAssociatedJSThread(); 171 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 172 173 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 174 callInfo->SetFunction(JSTaggedValue::Undefined()); 175 callInfo->SetThis(vector.GetTaggedValue()); 176 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 177 std::string str(data, data + size); 178 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 179 callInfo->SetCallArg(0, value); 180 181 ContainersVector::Add(callInfo); 182 183 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 184 callInfo1->SetFunction(JSTaggedValue::Undefined()); 185 callInfo1->SetThis(vector.GetTaggedValue()); 186 callInfo1->SetCallArg(0, value); 187 188 ContainersVector::GetIndexOf(callInfo1); 189 } 190 JSNApi::DestroyJSVM(vm); 191 } 192 ContainersVectorGetLastElementFuzzTest(const uint8_t* data, size_t size)193 static void ContainersVectorGetLastElementFuzzTest(const uint8_t* data, size_t size) 194 { 195 RuntimeOption option; 196 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 197 EcmaVM *vm = JSNApi::CreateJSVM(option); 198 { 199 JsiFastNativeScope scope(vm); 200 auto thread = vm->GetAssociatedJSThread(); 201 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 202 203 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 204 callInfo->SetFunction(JSTaggedValue::Undefined()); 205 callInfo->SetThis(vector.GetTaggedValue()); 206 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 207 std::string str(data, data + size); 208 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 209 callInfo->SetCallArg(0, value); 210 211 ContainersVector::Add(callInfo); 212 213 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 214 callInfo1->SetFunction(JSTaggedValue::Undefined()); 215 callInfo1->SetThis(vector.GetTaggedValue()); 216 217 ContainersVector::GetLastElement(callInfo1); 218 } 219 JSNApi::DestroyJSVM(vm); 220 } 221 ContainersVectorHasFuzzTest(const uint8_t* data, size_t size)222 static void ContainersVectorHasFuzzTest(const uint8_t* data, size_t size) 223 { 224 RuntimeOption option; 225 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 226 EcmaVM *vm = JSNApi::CreateJSVM(option); 227 { 228 JsiFastNativeScope scope(vm); 229 auto thread = vm->GetAssociatedJSThread(); 230 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 231 232 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 233 callInfo->SetFunction(JSTaggedValue::Undefined()); 234 callInfo->SetThis(vector.GetTaggedValue()); 235 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 236 std::string str(data, data + size); 237 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 238 callInfo->SetCallArg(0, value); 239 240 ContainersVector::Add(callInfo); 241 242 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 243 callInfo1->SetFunction(JSTaggedValue::Undefined()); 244 callInfo1->SetThis(vector.GetTaggedValue()); 245 callInfo1->SetCallArg(0, value); 246 247 ContainersVector::Has(callInfo1); 248 } 249 JSNApi::DestroyJSVM(vm); 250 } 251 ContainersVectorInsertFuzzTest(const uint8_t* data, size_t size)252 static void ContainersVectorInsertFuzzTest(const uint8_t* data, size_t size) 253 { 254 RuntimeOption option; 255 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 256 EcmaVM *vm = JSNApi::CreateJSVM(option); 257 { 258 JsiFastNativeScope scope(vm); 259 auto thread = vm->GetAssociatedJSThread(); 260 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 261 262 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 263 callInfo->SetFunction(JSTaggedValue::Undefined()); 264 callInfo->SetThis(vector.GetTaggedValue()); 265 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 266 std::string str(data, data + size); 267 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 268 callInfo->SetCallArg(0, value); 269 callInfo->SetCallArg(1, JSTaggedValue(0)); 270 271 ContainersVector::Insert(callInfo); 272 } 273 JSNApi::DestroyJSVM(vm); 274 } 275 ContainersVectorRemoveFuzzTest(const uint8_t* data, size_t size)276 static void ContainersVectorRemoveFuzzTest(const uint8_t* data, size_t size) 277 { 278 RuntimeOption option; 279 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 280 EcmaVM *vm = JSNApi::CreateJSVM(option); 281 { 282 JsiFastNativeScope scope(vm); 283 auto thread = vm->GetAssociatedJSThread(); 284 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 285 286 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 287 callInfo->SetFunction(JSTaggedValue::Undefined()); 288 callInfo->SetThis(vector.GetTaggedValue()); 289 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 290 std::string str(data, data + size); 291 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 292 callInfo->SetCallArg(0, value); 293 callInfo->SetCallArg(1, JSTaggedValue(0)); 294 295 ContainersVector::Insert(callInfo); 296 297 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 298 callInfo1->SetFunction(JSTaggedValue::Undefined()); 299 callInfo1->SetThis(vector.GetTaggedValue()); 300 callInfo1->SetCallArg(0, value); 301 ContainersVector::Remove(callInfo1); 302 } 303 JSNApi::DestroyJSVM(vm); 304 } 305 ContainersVectorSetFuzzTest(const uint8_t* data, size_t size)306 static void ContainersVectorSetFuzzTest(const uint8_t* data, size_t size) 307 { 308 RuntimeOption option; 309 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 310 EcmaVM *vm = JSNApi::CreateJSVM(option); 311 { 312 JsiFastNativeScope scope(vm); 313 auto thread = vm->GetAssociatedJSThread(); 314 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 315 316 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 317 callInfo->SetFunction(JSTaggedValue::Undefined()); 318 callInfo->SetThis(vector.GetTaggedValue()); 319 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 320 std::string str(data, data + size); 321 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 322 callInfo->SetCallArg(0, value); 323 callInfo->SetCallArg(1, JSTaggedValue(0)); 324 325 ContainersVector::Insert(callInfo); 326 327 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 328 callInfo1->SetFunction(JSTaggedValue::Undefined()); 329 callInfo1->SetThis(vector.GetTaggedValue()); 330 callInfo1->SetCallArg(0, JSTaggedValue(0)); 331 callInfo1->SetCallArg(1, value); 332 ContainersVector::Set(callInfo1); 333 } 334 JSNApi::DestroyJSVM(vm); 335 } 336 ContainersVectorGetLastIndexOfFuzzTest(const uint8_t* data, size_t size)337 static void ContainersVectorGetLastIndexOfFuzzTest(const uint8_t* data, size_t size) 338 { 339 RuntimeOption option; 340 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 341 EcmaVM *vm = JSNApi::CreateJSVM(option); 342 { 343 JsiFastNativeScope scope(vm); 344 auto thread = vm->GetAssociatedJSThread(); 345 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 346 347 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 348 callInfo->SetFunction(JSTaggedValue::Undefined()); 349 callInfo->SetThis(vector.GetTaggedValue()); 350 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 351 std::string str(data, data + size); 352 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 353 callInfo->SetCallArg(0, value); 354 355 ContainersVector::Add(callInfo); 356 357 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 358 callInfo1->SetFunction(JSTaggedValue::Undefined()); 359 callInfo1->SetThis(vector.GetTaggedValue()); 360 callInfo1->SetCallArg(0, value); 361 ContainersVector::GetLastIndexOf(callInfo1); 362 } 363 JSNApi::DestroyJSVM(vm); 364 } 365 ContainersVectorGetLastIndexFromFuzzTest(const uint8_t* data, size_t size)366 static void ContainersVectorGetLastIndexFromFuzzTest(const uint8_t* data, size_t size) 367 { 368 RuntimeOption option; 369 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 370 EcmaVM *vm = JSNApi::CreateJSVM(option); 371 { 372 JsiFastNativeScope scope(vm); 373 auto thread = vm->GetAssociatedJSThread(); 374 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 375 376 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 377 callInfo->SetFunction(JSTaggedValue::Undefined()); 378 callInfo->SetThis(vector.GetTaggedValue()); 379 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 380 std::string str(data, data + size); 381 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 382 callInfo->SetCallArg(0, value); 383 384 ContainersVector::Add(callInfo); 385 386 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 387 callInfo1->SetFunction(JSTaggedValue::Undefined()); 388 callInfo1->SetThis(vector.GetTaggedValue()); 389 callInfo1->SetCallArg(0, value); 390 callInfo1->SetCallArg(1, JSTaggedValue(0)); 391 ContainersVector::GetLastIndexFrom(callInfo1); 392 } 393 JSNApi::DestroyJSVM(vm); 394 } 395 ContainersVectorGetIndexFromFuzzTest(const uint8_t* data, size_t size)396 static void ContainersVectorGetIndexFromFuzzTest(const uint8_t* data, size_t size) 397 { 398 RuntimeOption option; 399 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 400 EcmaVM *vm = JSNApi::CreateJSVM(option); 401 { 402 JsiFastNativeScope scope(vm); 403 auto thread = vm->GetAssociatedJSThread(); 404 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 405 406 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 407 callInfo->SetFunction(JSTaggedValue::Undefined()); 408 callInfo->SetThis(vector.GetTaggedValue()); 409 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 410 std::string str(data, data + size); 411 JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue(); 412 callInfo->SetCallArg(0, value); 413 414 ContainersVector::Add(callInfo); 415 416 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 417 callInfo1->SetFunction(JSTaggedValue::Undefined()); 418 callInfo1->SetThis(vector.GetTaggedValue()); 419 callInfo1->SetCallArg(0, value); 420 callInfo1->SetCallArg(1, JSTaggedValue(0)); 421 ContainersVector::GetIndexFrom(callInfo1); 422 } 423 JSNApi::DestroyJSVM(vm); 424 } 425 ContainersVectorRemoveByRangeFuzzTest(const uint8_t* data, size_t size)426 static void ContainersVectorRemoveByRangeFuzzTest(const uint8_t* data, size_t size) 427 { 428 if (size <= 0) { 429 return; 430 } 431 RuntimeOption option; 432 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 433 EcmaVM *vm = JSNApi::CreateJSVM(option); 434 { 435 JsiFastNativeScope scope(vm); 436 auto thread = vm->GetAssociatedJSThread(); 437 438 JSHandle<JSAPIVector> vector = CreateJSAPIVector(thread); 439 440 constexpr int32_t ELEMENT_NUMS = 8; 441 442 uint32_t input = 0; 443 if (size > MAXBYTELEN) { 444 size = MAXBYTELEN; 445 } 446 if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) { 447 std::cout << "memcpy_s failed!"; 448 UNREACHABLE(); 449 } 450 451 for (int32_t i = 0; i < ELEMENT_NUMS; i++) { 452 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 453 callInfo->SetFunction(JSTaggedValue::Undefined()); 454 callInfo->SetThis(vector.GetTaggedValue()); 455 callInfo->SetCallArg(0, JSTaggedValue(i + input)); 456 457 ContainersVector::Add(callInfo); 458 } 459 460 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 461 callInfo1->SetFunction(JSTaggedValue::Undefined()); 462 callInfo1->SetThis(vector.GetTaggedValue()); 463 callInfo1->SetCallArg(0, JSTaggedValue(input % ELEMENT_NUMS)); 464 callInfo1->SetCallArg(1, JSTaggedValue((input + 1) % ELEMENT_NUMS)); 465 ContainersVector::RemoveByRange(callInfo1); 466 } 467 JSNApi::DestroyJSVM(vm); 468 } 469 470 class TestClass : public base::BuiltinsBase { 471 public: TestForEachFunc(EcmaRuntimeCallInfo *argv)472 static JSTaggedValue TestForEachFunc(EcmaRuntimeCallInfo *argv) 473 { 474 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 475 JSHandle<JSTaggedValue> key = GetCallArg(argv, 1); 476 JSHandle<JSTaggedValue> vector = GetCallArg(argv, 2); // 2 means the secode arg 477 if (!vector->IsUndefined()) { 478 if (value->IsNumber()) { 479 TaggedArray *elements = TaggedArray::Cast(JSAPIVector::Cast(vector.GetTaggedValue(). 480 GetTaggedObject())->GetElements().GetTaggedObject()); 481 elements->Get(key->GetInt()); 482 } 483 } 484 return JSTaggedValue::Undefined(); 485 } 486 TestReplaceAllElementsFunc(EcmaRuntimeCallInfo *argv)487 static JSTaggedValue TestReplaceAllElementsFunc(EcmaRuntimeCallInfo *argv) 488 { 489 JSThread *thread = argv->GetThread(); 490 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 491 JSHandle<JSTaggedValue> index = GetCallArg(argv, 1); 492 JSHandle<JSTaggedValue> vector = GetCallArg(argv, 2); // 2 means the secode arg 493 if (!vector->IsUndefined()) { 494 if (value->IsNumber()) { 495 JSHandle<JSTaggedValue> newValue(thread, JSTaggedValue(value->GetInt() * 2)); // 2 means mul by 2 496 JSHandle<JSAPIVector>::Cast(vector)->Set(thread, index->GetNumber(), newValue.GetTaggedValue()); 497 return newValue.GetTaggedValue(); 498 } 499 } 500 return JSTaggedValue::Undefined(); 501 } 502 }; 503 ContainersVectorReplaceAllElementsFuzzTest(const uint8_t* data, size_t size)504 static void ContainersVectorReplaceAllElementsFuzzTest(const uint8_t* data, size_t size) 505 { 506 if (size <= 0) { 507 return; 508 } 509 RuntimeOption option; 510 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 511 EcmaVM *vm = JSNApi::CreateJSVM(option); 512 { 513 JsiFastNativeScope scope(vm); 514 auto thread = vm->GetAssociatedJSThread(); 515 516 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 517 518 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 519 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 520 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 521 JSHandle<JSFunction> func = 522 factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestReplaceAllElementsFunc)); 523 callInfo1->SetFunction(JSTaggedValue::Undefined()); 524 callInfo1->SetThis(vector.GetTaggedValue()); 525 callInfo1->SetCallArg(0, func.GetTaggedValue()); 526 ContainersVector::ReplaceAllElements(callInfo1); 527 } 528 JSNApi::DestroyJSVM(vm); 529 } 530 ContainersVectorForEachFuzzTest(const uint8_t* data, size_t size)531 static void ContainersVectorForEachFuzzTest(const uint8_t* data, size_t size) 532 { 533 if (size <= 0) { 534 return; 535 } 536 RuntimeOption option; 537 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 538 EcmaVM *vm = JSNApi::CreateJSVM(option); 539 { 540 JsiFastNativeScope scope(vm); 541 auto thread = vm->GetAssociatedJSThread(); 542 543 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 544 545 JSHandle<JSAPIVector> vec = CreateJSAPIVector(thread); 546 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 547 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 548 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 549 JSHandle<JSFunction> func = 550 factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForEachFunc)); 551 callInfo1->SetFunction(JSTaggedValue::Undefined()); 552 callInfo1->SetThis(vector.GetTaggedValue()); 553 callInfo1->SetCallArg(0, func.GetTaggedValue()); 554 callInfo1->SetCallArg(1, vec.GetTaggedValue()); 555 ContainersVector::ForEach(callInfo1); 556 } 557 JSNApi::DestroyJSVM(vm); 558 } 559 ContainersVectorSortFuzzTest(const uint8_t* data, size_t size)560 static void ContainersVectorSortFuzzTest(const uint8_t* data, size_t size) 561 { 562 if (size <= 0) { 563 return; 564 } 565 RuntimeOption option; 566 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 567 EcmaVM *vm = JSNApi::CreateJSVM(option); 568 { 569 JsiFastNativeScope scope(vm); 570 auto thread = vm->GetAssociatedJSThread(); 571 572 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 573 574 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 575 callInfo1->SetFunction(JSTaggedValue::Undefined()); 576 callInfo1->SetThis(vector.GetTaggedValue()); 577 callInfo1->SetCallArg(0, JSTaggedValue::Undefined()); 578 ContainersVector::Sort(callInfo1); 579 } 580 JSNApi::DestroyJSVM(vm); 581 } 582 ContainersVectorSubVectorFuzzTest(const uint8_t* data, size_t size)583 static void ContainersVectorSubVectorFuzzTest(const uint8_t* data, size_t size) 584 { 585 if (size <= 0) { 586 return; 587 } 588 RuntimeOption option; 589 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 590 EcmaVM *vm = JSNApi::CreateJSVM(option); 591 { 592 JsiFastNativeScope scope(vm); 593 auto thread = vm->GetAssociatedJSThread(); 594 595 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 596 597 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 598 callInfo->SetFunction(JSTaggedValue::Undefined()); 599 callInfo->SetThis(vector.GetTaggedValue()); 600 callInfo->SetCallArg(0, JSTaggedValue(0)); 601 callInfo->SetCallArg(1, JSTaggedValue(2)); // 2 : means the third value 602 ContainersVector::SubVector(callInfo); 603 } 604 JSNApi::DestroyJSVM(vm); 605 } 606 ContainersVectorClearFuzzTest(const uint8_t* data, size_t size)607 static void ContainersVectorClearFuzzTest(const uint8_t* data, size_t size) 608 { 609 if (size <= 0) { 610 return; 611 } 612 RuntimeOption option; 613 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 614 EcmaVM *vm = JSNApi::CreateJSVM(option); 615 { 616 JsiFastNativeScope scope(vm); 617 auto thread = vm->GetAssociatedJSThread(); 618 619 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 620 621 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 622 callInfo->SetFunction(JSTaggedValue::Undefined()); 623 callInfo->SetThis(vector.GetTaggedValue()); 624 ContainersVector::Clear(callInfo); 625 } 626 JSNApi::DestroyJSVM(vm); 627 } 628 ContainersVectorCloneFuzzTest(const uint8_t* data, size_t size)629 static void ContainersVectorCloneFuzzTest(const uint8_t* data, size_t size) 630 { 631 if (size <= 0) { 632 return; 633 } 634 RuntimeOption option; 635 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 636 EcmaVM *vm = JSNApi::CreateJSVM(option); 637 { 638 JsiFastNativeScope scope(vm); 639 auto thread = vm->GetAssociatedJSThread(); 640 641 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 642 643 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 644 callInfo->SetFunction(JSTaggedValue::Undefined()); 645 callInfo->SetThis(vector.GetTaggedValue()); 646 ContainersVector::Clone(callInfo); 647 } 648 JSNApi::DestroyJSVM(vm); 649 } 650 ContainersVectorSetLengthFuzzTest(const uint8_t* data, size_t size)651 static void ContainersVectorSetLengthFuzzTest(const uint8_t* data, size_t size) 652 { 653 if (size <= 0) { 654 return; 655 } 656 RuntimeOption option; 657 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 658 EcmaVM *vm = JSNApi::CreateJSVM(option); 659 { 660 JsiFastNativeScope scope(vm); 661 auto thread = vm->GetAssociatedJSThread(); 662 663 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 664 665 uint32_t length = 0; 666 if (size > MAXBYTELEN) { 667 size = MAXBYTELEN; 668 } 669 if (memcpy_s(&length, MAXBYTELEN, data, size) != 0) { 670 std::cout << "memcpy_s failed!"; 671 UNREACHABLE(); 672 } 673 674 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 675 callInfo->SetFunction(JSTaggedValue::Undefined()); 676 callInfo->SetThis(vector.GetTaggedValue()); 677 callInfo->SetCallArg(0, JSTaggedValue(length)); 678 ContainersVector::SetLength(callInfo); 679 } 680 JSNApi::DestroyJSVM(vm); 681 } 682 ContainersVectorGetCapacityFuzzTest(const uint8_t* data, size_t size)683 static void ContainersVectorGetCapacityFuzzTest(const uint8_t* data, size_t size) 684 { 685 if (size <= 0) { 686 return; 687 } 688 RuntimeOption option; 689 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 690 EcmaVM *vm = JSNApi::CreateJSVM(option); 691 { 692 JsiFastNativeScope scope(vm); 693 auto thread = vm->GetAssociatedJSThread(); 694 695 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 696 697 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 698 callInfo->SetFunction(JSTaggedValue::Undefined()); 699 callInfo->SetThis(vector.GetTaggedValue()); 700 ContainersVector::GetCapacity(callInfo); 701 } 702 JSNApi::DestroyJSVM(vm); 703 } 704 ContainersVectorConvertToArrayFuzzTest(const uint8_t* data, size_t size)705 static void ContainersVectorConvertToArrayFuzzTest(const uint8_t* data, size_t size) 706 { 707 if (size <= 0) { 708 return; 709 } 710 RuntimeOption option; 711 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 712 EcmaVM *vm = JSNApi::CreateJSVM(option); 713 { 714 JsiFastNativeScope scope(vm); 715 auto thread = vm->GetAssociatedJSThread(); 716 717 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 718 719 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 720 callInfo->SetFunction(JSTaggedValue::Undefined()); 721 callInfo->SetThis(vector.GetTaggedValue()); 722 ContainersVector::ConvertToArray(callInfo); 723 } 724 JSNApi::DestroyJSVM(vm); 725 } 726 ContainersVectorIsEmptyFuzzTest(const uint8_t* data, size_t size)727 static void ContainersVectorIsEmptyFuzzTest(const uint8_t* data, size_t size) 728 { 729 if (size <= 0) { 730 return; 731 } 732 RuntimeOption option; 733 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 734 EcmaVM *vm = JSNApi::CreateJSVM(option); 735 { 736 JsiFastNativeScope scope(vm); 737 auto thread = vm->GetAssociatedJSThread(); 738 739 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 740 741 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 742 callInfo->SetFunction(JSTaggedValue::Undefined()); 743 callInfo->SetThis(vector.GetTaggedValue()); 744 ContainersVector::IsEmpty(callInfo); 745 } 746 JSNApi::DestroyJSVM(vm); 747 } 748 ContainersVectorIncreaseCapacityToFuzzTest(const uint8_t* data, size_t size)749 static void ContainersVectorIncreaseCapacityToFuzzTest(const uint8_t* data, size_t size) 750 { 751 if (size <= 0) { 752 return; 753 } 754 RuntimeOption option; 755 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 756 EcmaVM *vm = JSNApi::CreateJSVM(option); 757 { 758 JsiFastNativeScope scope(vm); 759 auto thread = vm->GetAssociatedJSThread(); 760 761 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 762 763 uint32_t capacity = 0; 764 if (size > MAXBYTELEN) { 765 size = MAXBYTELEN; 766 } 767 if (memcpy_s(&capacity, MAXBYTELEN, data, size) != 0) { 768 std::cout << "memcpy_s failed!"; 769 UNREACHABLE(); 770 } 771 772 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 773 callInfo->SetFunction(JSTaggedValue::Undefined()); 774 callInfo->SetThis(vector.GetTaggedValue()); 775 callInfo->SetCallArg(0, JSTaggedValue(capacity)); 776 ContainersVector::IncreaseCapacityTo(callInfo); 777 } 778 JSNApi::DestroyJSVM(vm); 779 } 780 ContainersVectorToStringFuzzTest(const uint8_t* data, size_t size)781 static void ContainersVectorToStringFuzzTest(const uint8_t* data, size_t size) 782 { 783 if (size <= 0) { 784 return; 785 } 786 RuntimeOption option; 787 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 788 EcmaVM *vm = JSNApi::CreateJSVM(option); 789 { 790 JsiFastNativeScope scope(vm); 791 auto thread = vm->GetAssociatedJSThread(); 792 793 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 794 795 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 796 callInfo->SetFunction(JSTaggedValue::Undefined()); 797 callInfo->SetThis(vector.GetTaggedValue()); 798 ContainersVector::ToString(callInfo); 799 } 800 JSNApi::DestroyJSVM(vm); 801 } 802 ContainersVectorTrimToCurrentLengthFuzzTest(const uint8_t* data, size_t size)803 static void ContainersVectorTrimToCurrentLengthFuzzTest(const uint8_t* data, size_t size) 804 { 805 if (size <= 0) { 806 return; 807 } 808 RuntimeOption option; 809 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 810 EcmaVM *vm = JSNApi::CreateJSVM(option); 811 { 812 JsiFastNativeScope scope(vm); 813 auto thread = vm->GetAssociatedJSThread(); 814 815 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 816 817 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 818 callInfo->SetFunction(JSTaggedValue::Undefined()); 819 callInfo->SetThis(vector.GetTaggedValue()); 820 ContainersVector::TrimToCurrentLength(callInfo); 821 } 822 JSNApi::DestroyJSVM(vm); 823 } 824 ContainersVectorCopyToArrayFuzzTest(const uint8_t* data, size_t size)825 static void ContainersVectorCopyToArrayFuzzTest(const uint8_t* data, size_t size) 826 { 827 if (size <= 0) { 828 return; 829 } 830 RuntimeOption option; 831 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 832 EcmaVM *vm = JSNApi::CreateJSVM(option); 833 { 834 JsiFastNativeScope scope(vm); 835 auto thread = vm->GetAssociatedJSThread(); 836 837 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 838 839 constexpr int32_t ELEMENT_NUMS = 8; 840 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 841 JSHandle<JSArray> array = factory->NewJSArray(); 842 JSHandle<TaggedArray> arrayElement = factory->NewTaggedArray(ELEMENT_NUMS, JSTaggedValue::Hole()); 843 array->SetElements(thread, arrayElement); 844 array->SetArrayLength(thread, static_cast<uint32_t>(ELEMENT_NUMS)); 845 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 846 callInfo->SetFunction(JSTaggedValue::Undefined()); 847 callInfo->SetThis(vector.GetTaggedValue()); 848 callInfo->SetCallArg(0, array.GetTaggedValue()); 849 ContainersVector::CopyToArray(callInfo); 850 } 851 JSNApi::DestroyJSVM(vm); 852 } 853 ContainersVectorIteratorFuzzTest(const uint8_t* data, size_t size)854 static void ContainersVectorIteratorFuzzTest(const uint8_t* data, size_t size) 855 { 856 if (size <= 0) { 857 return; 858 } 859 RuntimeOption option; 860 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 861 EcmaVM *vm = JSNApi::CreateJSVM(option); 862 { 863 JsiFastNativeScope scope(vm); 864 auto thread = vm->GetAssociatedJSThread(); 865 JSHandle<JSAPIVector> vector = GetVectorWithData(thread, data, size); 866 auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 867 callInfo1->SetFunction(JSTaggedValue::Undefined()); 868 callInfo1->SetThis(vector.GetTaggedValue()); 869 JSHandle<JSTaggedValue> iterValues(thread, ContainersVector::GetIteratorObj(callInfo1)); 870 871 JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined()); 872 constexpr int32_t ELEMENT_NUMS = 8; 873 for (uint32_t i = 0; i < ELEMENT_NUMS; i++) { 874 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length 875 callInfo->SetFunction(JSTaggedValue::Undefined()); 876 callInfo->SetThis(iterValues.GetTaggedValue()); 877 result.Update(JSAPIVectorIterator::Next(callInfo)); 878 } 879 } 880 JSNApi::DestroyJSVM(vm); 881 } 882 }; 883 } 884 #endif