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 #ifndef CONTAINERS_PLAIN_ARRAY_COMMON_FUZZER_H 16 #define CONTAINERS_PLAIN_ARRAY_COMMON_FUZZER_H 17 18 #include "ecmascript/base/builtins_base.h" 19 #include "ecmascript/containers/containers_plainarray.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_plain_array.h" 25 #include "ecmascript/js_handle.h" 26 #include "ecmascript/napi/include/jsnapi.h" 27 28 namespace ContainersPlainArrayFuzzTest { 29 using namespace panda; 30 using namespace panda::ecmascript; 31 using namespace panda::ecmascript::base; 32 using namespace panda::ecmascript::containers; 33 34 class ContainersPlainArrayTest { 35 public: JSObjectCreate(JSThread * thread)36 static JSFunction *JSObjectCreate(JSThread *thread) 37 { 38 JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv(); 39 return globalEnv->GetObjectFunction().GetObject<JSFunction>(); 40 } 41 CreateEcmaRuntimeCallInfo(JSThread * thread,uint32_t numArgs)42 static EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs) 43 { 44 auto factory = thread->GetEcmaVM()->GetFactory(); 45 JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread)); 46 JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass)); 47 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 48 EcmaRuntimeCallInfo *objCallInfo = 49 EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs); 50 return objCallInfo; 51 } 52 InitializePlainArrayConstructor(JSThread * thread)53 static JSTaggedValue InitializePlainArrayConstructor(JSThread *thread) 54 { 55 auto factory = thread->GetEcmaVM()->GetFactory(); 56 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 57 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject(); 58 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate")); 59 JSHandle<JSTaggedValue> value = 60 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue(); 61 62 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 63 objCallInfo->SetFunction(JSTaggedValue::Undefined()); 64 objCallInfo->SetThis(value.GetTaggedValue()); 65 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::PlainArray))); 66 JSTaggedValue result = ContainersPrivate::Load(objCallInfo); 67 return result; 68 } 69 CreateJSAPIPlainArray(JSThread * thread)70 static JSHandle<JSAPIPlainArray> CreateJSAPIPlainArray(JSThread *thread) 71 { 72 JSHandle<JSFunction> newTarget(thread, InitializePlainArrayConstructor(thread)); 73 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4); 74 objCallInfo->SetFunction(newTarget.GetTaggedValue()); 75 objCallInfo->SetNewTarget(newTarget.GetTaggedValue()); 76 objCallInfo->SetThis(JSTaggedValue::Undefined()); 77 JSTaggedValue result = ContainersPlainArray::PlainArrayConstructor(objCallInfo); 78 JSHandle<JSAPIPlainArray> array(thread, result); 79 return array; 80 } 81 ContainersPlainArray_Add_Has_FuzzTest(const uint8_t * data,size_t size)82 static void ContainersPlainArray_Add_Has_FuzzTest(const uint8_t* data, size_t size) 83 { 84 if (data == nullptr || size <= 0) { 85 std::cout << "illegal input!"; 86 return; 87 } 88 RuntimeOption option; 89 option.SetLogLevel(common::LOG_LEVEL::ERROR); 90 EcmaVM *vm = JSNApi::CreateJSVM(option); 91 { 92 JsiFastNativeScope scope(vm); 93 auto thread = vm->GetAssociatedJSThread(); 94 auto factory = vm->GetFactory(); 95 96 uint32_t inputNum = 0; 97 std::string inputStr(data, data + size); 98 const uint32_t MAXBYTELEN = 4; 99 if (size > MAXBYTELEN) { 100 size = MAXBYTELEN; 101 } 102 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 103 std::cout << "memcpy_s failed!"; 104 UNREACHABLE(); 105 } 106 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 107 const uint32_t addTimes = 3; 108 for (uint32_t i = 0; i < addTimes; i++) { 109 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 110 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 111 callInfo->SetFunction(JSTaggedValue::Undefined()); 112 callInfo->SetThis(plainArray.GetTaggedValue()); 113 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum + i))); // set key 114 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); // set value 115 ContainersPlainArray::Add(callInfo); 116 } 117 118 for (uint32_t i = 0; i < addTimes; i++) { 119 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 120 callInfo->SetFunction(JSTaggedValue::Undefined()); 121 callInfo->SetThis(plainArray.GetTaggedValue()); 122 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum + i))); 123 ContainersPlainArray::Has(callInfo); // expected to return true 124 } 125 } 126 JSNApi::DestroyJSVM(vm); 127 } 128 ContainersPlainArray_Get_FuzzTest(const uint8_t * data,size_t size)129 static void ContainersPlainArray_Get_FuzzTest(const uint8_t* data, size_t size) 130 { 131 if (data == nullptr || size <= 0) { 132 std::cout << "illegal input!"; 133 return; 134 } 135 RuntimeOption option; 136 option.SetLogLevel(common::LOG_LEVEL::ERROR); 137 EcmaVM *vm = JSNApi::CreateJSVM(option); 138 { 139 JsiFastNativeScope scope(vm); 140 auto thread = vm->GetAssociatedJSThread(); 141 auto factory = vm->GetFactory(); 142 143 uint32_t inputNum = 0; 144 std::string inputStr(data, data + size); 145 const uint32_t MAXBYTELEN = 4; 146 if (size > MAXBYTELEN) { 147 size = MAXBYTELEN; 148 } 149 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 150 std::cout << "memcpy_s failed!"; 151 UNREACHABLE(); 152 } 153 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 154 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 155 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 156 callInfo->SetFunction(JSTaggedValue::Undefined()); 157 callInfo->SetThis(plainArray.GetTaggedValue()); 158 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 159 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 160 ContainersPlainArray::Add(callInfo); 161 162 EcmaRuntimeCallInfo *cfForGet = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 163 cfForGet->SetFunction(JSTaggedValue::Undefined()); 164 cfForGet->SetThis(plainArray.GetTaggedValue()); 165 cfForGet->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); // set key get value 166 ContainersPlainArray::Get(cfForGet); // expected to return value 167 } 168 JSNApi::DestroyJSVM(vm); 169 } 170 ContainersPlainArray_GetIndexOfKey_FuzzTest(const uint8_t * data,size_t size)171 static void ContainersPlainArray_GetIndexOfKey_FuzzTest(const uint8_t* data, size_t size) 172 { 173 if (data == nullptr || size <= 0) { 174 std::cout << "illegal input!"; 175 return; 176 } 177 RuntimeOption option; 178 option.SetLogLevel(common::LOG_LEVEL::ERROR); 179 EcmaVM *vm = JSNApi::CreateJSVM(option); 180 { 181 JsiFastNativeScope scope(vm); 182 auto thread = vm->GetAssociatedJSThread(); 183 auto factory = vm->GetFactory(); 184 185 uint32_t inputNum = 0; 186 std::string inputStr(data, data + size); 187 const uint32_t MAXBYTELEN = 4; 188 if (size > MAXBYTELEN) { 189 size = MAXBYTELEN; 190 } 191 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 192 std::cout << "memcpy_s failed!"; 193 UNREACHABLE(); 194 } 195 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 196 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 197 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 198 callInfo->SetFunction(JSTaggedValue::Undefined()); 199 callInfo->SetThis(plainArray.GetTaggedValue()); 200 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 201 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 202 ContainersPlainArray::Add(callInfo); 203 204 EcmaRuntimeCallInfo *cfForGetIndexOfKey = 205 CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 206 cfForGetIndexOfKey->SetFunction(JSTaggedValue::Undefined()); 207 cfForGetIndexOfKey->SetThis(plainArray.GetTaggedValue()); 208 cfForGetIndexOfKey->SetCallArg(0, inputEcmaStr.GetTaggedValue()); // value 209 ContainersPlainArray::GetIndexOfKey(cfForGetIndexOfKey); // expected to return the index of key 210 } 211 JSNApi::DestroyJSVM(vm); 212 } 213 ContainersPlainArray_GetIndexOfValue_FuzzTest(const uint8_t * data,size_t size)214 static void ContainersPlainArray_GetIndexOfValue_FuzzTest(const uint8_t* data, size_t size) 215 { 216 if (data == nullptr || size <= 0) { 217 std::cout << "illegal input!"; 218 return; 219 } 220 RuntimeOption option; 221 option.SetLogLevel(common::LOG_LEVEL::ERROR); 222 EcmaVM *vm = JSNApi::CreateJSVM(option); 223 { 224 JsiFastNativeScope scope(vm); 225 auto thread = vm->GetAssociatedJSThread(); 226 auto factory = vm->GetFactory(); 227 228 uint32_t inputNum = 0; 229 std::string inputStr(data, data + size); 230 const uint32_t MAXBYTELEN = 4; 231 if (size > MAXBYTELEN) { 232 size = MAXBYTELEN; 233 } 234 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 235 std::cout << "memcpy_s failed!"; 236 UNREACHABLE(); 237 } 238 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 239 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 240 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 241 callInfo->SetFunction(JSTaggedValue::Undefined()); 242 callInfo->SetThis(plainArray.GetTaggedValue()); 243 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 244 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 245 ContainersPlainArray::Add(callInfo); 246 247 EcmaRuntimeCallInfo *cfForGetIndexOfValue = 248 CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 249 cfForGetIndexOfValue->SetFunction(JSTaggedValue::Undefined()); 250 cfForGetIndexOfValue->SetThis(plainArray.GetTaggedValue()); 251 cfForGetIndexOfValue->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); // key 252 ContainersPlainArray::GetIndexOfValue(cfForGetIndexOfValue); // expected to return the index of value 253 } 254 JSNApi::DestroyJSVM(vm); 255 } 256 ContainersPlainArray_GetKeyAt_FuzzTest(const uint8_t * data,size_t size)257 static void ContainersPlainArray_GetKeyAt_FuzzTest(const uint8_t* data, size_t size) 258 { 259 if (data == nullptr || size <= 0) { 260 std::cout << "illegal input!"; 261 return; 262 } 263 RuntimeOption option; 264 option.SetLogLevel(common::LOG_LEVEL::ERROR); 265 EcmaVM *vm = JSNApi::CreateJSVM(option); 266 { 267 JsiFastNativeScope scope(vm); 268 auto thread = vm->GetAssociatedJSThread(); 269 auto factory = vm->GetFactory(); 270 271 uint32_t inputNum = 0; 272 std::string inputStr(data, data + size); 273 const uint32_t MAXBYTELEN = 4; 274 if (size > MAXBYTELEN) { 275 size = MAXBYTELEN; 276 } 277 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 278 std::cout << "memcpy_s failed!"; 279 UNREACHABLE(); 280 } 281 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 282 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 283 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 284 callInfo->SetFunction(JSTaggedValue::Undefined()); 285 callInfo->SetThis(plainArray.GetTaggedValue()); 286 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 287 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 288 ContainersPlainArray::Add(callInfo); 289 290 EcmaRuntimeCallInfo *cfForGetKeyAt = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 291 cfForGetKeyAt->SetFunction(JSTaggedValue::Undefined()); 292 cfForGetKeyAt->SetThis(plainArray.GetTaggedValue()); 293 cfForGetKeyAt->SetCallArg(0, inputEcmaStr.GetTaggedValue()); // value 294 ContainersPlainArray::GetKeyAt(cfForGetKeyAt); // expected to return the key 295 } 296 JSNApi::DestroyJSVM(vm); 297 } 298 ContainersPlainArray_Remove_FuzzTest(const uint8_t * data,size_t size)299 static void ContainersPlainArray_Remove_FuzzTest(const uint8_t* data, size_t size) 300 { 301 if (data == nullptr || size <= 0) { 302 std::cout << "illegal input!"; 303 return; 304 } 305 RuntimeOption option; 306 option.SetLogLevel(common::LOG_LEVEL::ERROR); 307 EcmaVM *vm = JSNApi::CreateJSVM(option); 308 { 309 JsiFastNativeScope scope(vm); 310 auto thread = vm->GetAssociatedJSThread(); 311 auto factory = vm->GetFactory(); 312 313 uint32_t inputNum = 0; 314 std::string inputStr(data, data + size); 315 const uint32_t MAXBYTELEN = 4; 316 if (size > MAXBYTELEN) { 317 size = MAXBYTELEN; 318 } 319 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 320 std::cout << "memcpy_s failed!"; 321 UNREACHABLE(); 322 } 323 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 324 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 325 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 326 callInfo->SetFunction(JSTaggedValue::Undefined()); 327 callInfo->SetThis(plainArray.GetTaggedValue()); 328 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 329 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 330 ContainersPlainArray::Add(callInfo); 331 332 EcmaRuntimeCallInfo *cfForRemove = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 333 cfForRemove->SetFunction(JSTaggedValue::Undefined()); 334 cfForRemove->SetThis(plainArray.GetTaggedValue()); 335 cfForRemove->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 336 ContainersPlainArray::Remove(cfForRemove); // expected to return the value 337 } 338 JSNApi::DestroyJSVM(vm); 339 } 340 ContainersPlainArray_RemoveAt_FuzzTest(const uint8_t * data,size_t size)341 static void ContainersPlainArray_RemoveAt_FuzzTest(const uint8_t* data, size_t size) 342 { 343 if (data == nullptr || size <= 0) { 344 std::cout << "illegal input!"; 345 return; 346 } 347 RuntimeOption option; 348 option.SetLogLevel(common::LOG_LEVEL::ERROR); 349 EcmaVM *vm = JSNApi::CreateJSVM(option); 350 { 351 JsiFastNativeScope scope(vm); 352 auto thread = vm->GetAssociatedJSThread(); 353 auto factory = vm->GetFactory(); 354 355 uint32_t inputNum = 0; 356 std::string inputStr(data, data + size); 357 const uint32_t MAXBYTELEN = 4; 358 if (size > MAXBYTELEN) { 359 size = MAXBYTELEN; 360 } 361 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 362 std::cout << "memcpy_s failed!"; 363 UNREACHABLE(); 364 } 365 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 366 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 367 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 368 callInfo->SetFunction(JSTaggedValue::Undefined()); 369 callInfo->SetThis(plainArray.GetTaggedValue()); 370 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 371 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 372 ContainersPlainArray::Add(callInfo); 373 374 EcmaRuntimeCallInfo *cfForRemoveAt = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 375 cfForRemoveAt->SetFunction(JSTaggedValue::Undefined()); 376 cfForRemoveAt->SetThis(plainArray.GetTaggedValue()); 377 cfForRemoveAt->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 378 ContainersPlainArray::RemoveAt(cfForRemoveAt); // expected to return the value 379 } 380 JSNApi::DestroyJSVM(vm); 381 } 382 ContainersPlainArray_RemoveRangeFrom_FuzzTest(const uint8_t * data,size_t size)383 static void ContainersPlainArray_RemoveRangeFrom_FuzzTest(const uint8_t* data, size_t size) 384 { 385 if (data == nullptr || size <= 0) { 386 std::cout << "illegal input!"; 387 return; 388 } 389 RuntimeOption option; 390 option.SetLogLevel(common::LOG_LEVEL::ERROR); 391 EcmaVM *vm = JSNApi::CreateJSVM(option); 392 { 393 JsiFastNativeScope scope(vm); 394 auto thread = vm->GetAssociatedJSThread(); 395 auto factory = vm->GetFactory(); 396 397 uint32_t inputNum = 0; 398 std::string inputStr(data, data + size); 399 const uint32_t MAXBYTELEN = 4; 400 if (size > MAXBYTELEN) { 401 size = MAXBYTELEN; 402 } 403 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 404 std::cout << "memcpy_s failed!"; 405 UNREACHABLE(); 406 } 407 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 408 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 409 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 410 callInfo->SetFunction(JSTaggedValue::Undefined()); 411 callInfo->SetThis(plainArray.GetTaggedValue()); 412 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 413 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 414 ContainersPlainArray::Add(callInfo); 415 416 EcmaRuntimeCallInfo *cfForRemoveRangeFrom = 417 CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 418 cfForRemoveRangeFrom->SetFunction(JSTaggedValue::Undefined()); 419 cfForRemoveRangeFrom->SetThis(plainArray.GetTaggedValue()); 420 cfForRemoveRangeFrom->SetCallArg(0, JSTaggedValue(0)); // set index as the head of array 421 cfForRemoveRangeFrom->SetCallArg(1, JSTaggedValue(static_cast<uint32_t>(inputNum))); // number to delete 422 ContainersPlainArray::RemoveRangeFrom(cfForRemoveRangeFrom); // expected to return the safe size 423 } 424 JSNApi::DestroyJSVM(vm); 425 } 426 ContainersPlainArray_SetValueAt_FuzzTest(const uint8_t * data,size_t size)427 static void ContainersPlainArray_SetValueAt_FuzzTest(const uint8_t* data, size_t size) 428 { 429 if (data == nullptr || size <= 0) { 430 std::cout << "illegal input!"; 431 return; 432 } 433 RuntimeOption option; 434 option.SetLogLevel(common::LOG_LEVEL::ERROR); 435 EcmaVM *vm = JSNApi::CreateJSVM(option); 436 { 437 JsiFastNativeScope scope(vm); 438 auto thread = vm->GetAssociatedJSThread(); 439 auto factory = vm->GetFactory(); 440 441 uint32_t inputNum = 0; 442 std::string inputStr(data, data + size); 443 const uint32_t MAXBYTELEN = 4; 444 if (size > MAXBYTELEN) { 445 size = MAXBYTELEN; 446 } 447 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 448 std::cout << "memcpy_s failed!"; 449 UNREACHABLE(); 450 } 451 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 452 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 453 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 454 callInfo->SetFunction(JSTaggedValue::Undefined()); 455 callInfo->SetThis(plainArray.GetTaggedValue()); 456 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 457 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 458 ContainersPlainArray::Add(callInfo); 459 460 EcmaRuntimeCallInfo *cfForSetValueAt = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 461 cfForSetValueAt->SetFunction(JSTaggedValue::Undefined()); 462 cfForSetValueAt->SetThis(plainArray.GetTaggedValue()); 463 cfForSetValueAt->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); // set index to set 464 cfForSetValueAt->SetCallArg(1, JSTaggedValue(static_cast<uint32_t>(inputNum))); // set new value 465 ContainersPlainArray::SetValueAt(cfForSetValueAt); // expected to return undefined 466 } 467 JSNApi::DestroyJSVM(vm); 468 } 469 ContainersPlainArray_GetValueAt_FuzzTest(const uint8_t * data,size_t size)470 static void ContainersPlainArray_GetValueAt_FuzzTest(const uint8_t* data, size_t size) 471 { 472 if (data == nullptr || size <= 0) { 473 std::cout << "illegal input!"; 474 return; 475 } 476 RuntimeOption option; 477 option.SetLogLevel(common::LOG_LEVEL::ERROR); 478 EcmaVM *vm = JSNApi::CreateJSVM(option); 479 { 480 JsiFastNativeScope scope(vm); 481 auto thread = vm->GetAssociatedJSThread(); 482 auto factory = vm->GetFactory(); 483 484 uint32_t inputNum = 0; 485 std::string inputStr(data, data + size); 486 const uint32_t MAXBYTELEN = 4; 487 if (size > MAXBYTELEN) { 488 size = MAXBYTELEN; 489 } 490 if (memcpy_s(&inputNum, MAXBYTELEN, data, size) != 0) { 491 std::cout << "memcpy_s failed!"; 492 UNREACHABLE(); 493 } 494 JSHandle<JSAPIPlainArray> plainArray = CreateJSAPIPlainArray(thread); 495 JSHandle<EcmaString> inputEcmaStr = factory->NewFromStdString(inputStr); 496 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length 497 callInfo->SetFunction(JSTaggedValue::Undefined()); 498 callInfo->SetThis(plainArray.GetTaggedValue()); 499 callInfo->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); 500 callInfo->SetCallArg(1, inputEcmaStr.GetTaggedValue()); 501 ContainersPlainArray::Add(callInfo); 502 503 EcmaRuntimeCallInfo *cfForGetValueAt = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 504 cfForGetValueAt->SetFunction(JSTaggedValue::Undefined()); 505 cfForGetValueAt->SetThis(plainArray.GetTaggedValue()); 506 cfForGetValueAt->SetCallArg(0, JSTaggedValue(static_cast<uint32_t>(inputNum))); // set index to get 507 ContainersPlainArray::GetValueAt(cfForGetValueAt); // expected to return value 508 } 509 JSNApi::DestroyJSVM(vm); 510 } 511 }; 512 } 513 514 #endif