Fix the crash case dumpheapsnapshot3fuzztest

The dumpheapsnapshot3fuzztest test case failed due to an incorrect size parameter passed to memcpy_s

Issue:    https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IAE0KL?from=project-issue
Signed-off-by: l00483465 <laiqishengbao@huawei.com>
This commit is contained in:
l00483465 2024-07-19 10:13:34 +08:00
parent 5933423ff3
commit 954232d4c5
2 changed files with 45 additions and 12 deletions

View File

@ -15,6 +15,8 @@
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "ecmascript/accessor_data.h"
#include "ecmascript/ecma_vm.h"
@ -384,6 +386,41 @@ HWTEST_F_L0(HProfTest, DumpNativeSize)
ASSERT_TRUE(tester.ContrastJSONNativeSizeNum("test10.heapsnapshot", "\"nodes\":[", nativeSizeNum));
}
HWTEST_F_L0(HProfTest, TestSetDumpFormatInRandomNum)
{
std::srand(std::time(nullptr));
for (int i = 0; i < 20; i++) {
size_t size = std::rand() % 128;
if (size <= 0) {
continue;
}
EcmaVM *vm = instance;
size_t maxEnumNum = static_cast<size_t>(DumpFormat::OTHER) + 1;
DumpFormat dumpFormat = static_cast<DumpFormat>(size % maxEnumNum);
DumpSnapShotOption dumpOption;
dumpOption.dumpFormat = dumpFormat;
dumpOption.isVmMode = true;
dumpOption.isPrivate = false;
dumpOption.captureNumericValue = false;
if (size > sizeof(double)) {
size = sizeof(double);
}
uint8_t* data = reinterpret_cast<uint8_t*>(malloc(size));
for (size_t j = 0; j < size - 1; j++) {
data[j] = size;
}
data[size - 1] = 0;
std::string path(data, data + size);
FileStream stream(path);
Progress *progress = nullptr;
DFXJSNApi::DumpHeapSnapshot(vm, &stream, dumpOption, progress);
HeapProfilerInterface *heapProfile = HeapProfilerInterface::GetInstance(instance);
heapProfile->DumpHeapSnapshot(&stream, dumpOption);
ASSERT_TRUE(heapProfile->GetIdCount() > 0);
free(data);
}
}
HWTEST_F_L0(HProfTest, TestIdConsistency)
{
HProfTestHelper tester(instance);

View File

@ -29,26 +29,22 @@ using panda::ecmascript::FileStream;
namespace OHOS {
void DumpHeapSnapshot3FuzzTest(const uint8_t* data, size_t size)
{
RuntimeOption option;
option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
EcmaVM *vm = JSNApi::CreateJSVM(option);
DumpFormat dumpFormat = DumpFormat::JSON;
if (size <= 0) {
return;
}
if (size > MAXBYTELEN) {
size = MAXBYTELEN;
}
if (memcpy_s(&dumpFormat, MAXBYTELEN, data, size) != 0) {
std::cout << "memcpy_s failed!";
UNREACHABLE();
}
RuntimeOption option;
option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
EcmaVM *vm = JSNApi::CreateJSVM(option);
size_t maxEnumNum = static_cast<size_t>(DumpFormat::OTHER) + 1;
DumpFormat dumpFormat = static_cast<DumpFormat>(size % maxEnumNum);
DumpSnapShotOption dumpOption;
dumpOption.dumpFormat = dumpFormat;
dumpOption.isVmMode = true;
dumpOption.isPrivate = false;
dumpOption.captureNumericValue = false;
if (size > MAXBYTELEN) {
size = MAXBYTELEN;
}
std::string path(data, data + size);
FileStream stream(path);
Progress *progress = nullptr;