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