18e920a95Sopenharmony_ci/*
28e920a95Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38e920a95Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48e920a95Sopenharmony_ci * you may not use this file except in compliance with the License.
58e920a95Sopenharmony_ci * You may obtain a copy of the License at
68e920a95Sopenharmony_ci *
78e920a95Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88e920a95Sopenharmony_ci *
98e920a95Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108e920a95Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118e920a95Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128e920a95Sopenharmony_ci * See the License for the specific language governing permissions and
138e920a95Sopenharmony_ci * limitations under the License.
148e920a95Sopenharmony_ci */
158e920a95Sopenharmony_ci
168e920a95Sopenharmony_ci#include <cstdio>
178e920a95Sopenharmony_ci#include <cstring>
188e920a95Sopenharmony_ci#include <fcntl.h>
198e920a95Sopenharmony_ci#include <gtest/gtest.h>
208e920a95Sopenharmony_ci#include <gtest/hwext/gtest-multithread.h>
218e920a95Sopenharmony_ci#include <random>
228e920a95Sopenharmony_ci#include <securec.h>
238e920a95Sopenharmony_ci#include <sys/mman.h>
248e920a95Sopenharmony_ci#include <sys/ioctl.h>
258e920a95Sopenharmony_ci#include <unistd.h>
268e920a95Sopenharmony_ci
278e920a95Sopenharmony_ci#include "errcode.h"
288e920a95Sopenharmony_ci#include "jit_code_signer_factory.h"
298e920a95Sopenharmony_ci#include "jit_buffer_integrity.h"
308e920a95Sopenharmony_ci#include "code_sign_attr_utils.h"
318e920a95Sopenharmony_ci#include "pac_sign_ctx.h"
328e920a95Sopenharmony_ci
338e920a95Sopenharmony_cinamespace OHOS {
348e920a95Sopenharmony_cinamespace Security {
358e920a95Sopenharmony_cinamespace CodeSign {
368e920a95Sopenharmony_ciusing namespace std;
378e920a95Sopenharmony_ciusing namespace testing::ext;
388e920a95Sopenharmony_ciusing namespace testing::mt;
398e920a95Sopenharmony_ci
408e920a95Sopenharmony_ci#define CAST_VOID_PTR(buffer) (reinterpret_cast<void *>(buffer))
418e920a95Sopenharmony_ci
428e920a95Sopenharmony_cistatic Instr g_testInstructionSet[] = {
438e920a95Sopenharmony_ci    0x11111111,
448e920a95Sopenharmony_ci    0x22222222,
458e920a95Sopenharmony_ci    0x33333333, // patched -> 0x66666666
468e920a95Sopenharmony_ci    0x44444444, // patched -> 0x77777777
478e920a95Sopenharmony_ci    0x55555555
488e920a95Sopenharmony_ci};
498e920a95Sopenharmony_ci
508e920a95Sopenharmony_cistatic Instr g_afterPatchInstructionSet[] = {
518e920a95Sopenharmony_ci    0x11111111,
528e920a95Sopenharmony_ci    0x22222222,
538e920a95Sopenharmony_ci    0x66666666,
548e920a95Sopenharmony_ci    0x77777777,
558e920a95Sopenharmony_ci    0x55555555
568e920a95Sopenharmony_ci};
578e920a95Sopenharmony_ci
588e920a95Sopenharmony_cistatic Instr g_testPatchInstructionSet[] = {
598e920a95Sopenharmony_ci    0x66666666,
608e920a95Sopenharmony_ci    0x77777777
618e920a95Sopenharmony_ci};
628e920a95Sopenharmony_ci
638e920a95Sopenharmony_cistatic constexpr uint32_t MULTI_THREAD_NUM = 10;
648e920a95Sopenharmony_cistatic constexpr int INSTRUCTIONS_SET_SIZE =
658e920a95Sopenharmony_ci    sizeof(g_testInstructionSet) / sizeof(g_testInstructionSet[0]);
668e920a95Sopenharmony_cistatic constexpr int INSTRUCTIONS_SET_SIZE_BYTES = sizeof(g_testInstructionSet);
678e920a95Sopenharmony_cistatic constexpr int TEST_PATCH_INDEX = 2;
688e920a95Sopenharmony_ci
698e920a95Sopenharmony_cistatic constexpr int PATCH_INSTRUCTIONS_SET_SIZE =
708e920a95Sopenharmony_ci    sizeof(g_testPatchInstructionSet) / sizeof(g_testPatchInstructionSet[0]);
718e920a95Sopenharmony_ci
728e920a95Sopenharmony_cistatic void *g_testInstructionBuf = CAST_VOID_PTR(g_testInstructionSet);
738e920a95Sopenharmony_cistatic void *g_afterPatchInstructionBuf = CAST_VOID_PTR(g_afterPatchInstructionSet);
748e920a95Sopenharmony_cistatic void *g_testPatchInstructionBuf = CAST_VOID_PTR(g_testPatchInstructionSet);
758e920a95Sopenharmony_cistatic void *g_jitMemory = nullptr;
768e920a95Sopenharmony_ci
778e920a95Sopenharmony_civoid *g_mapJitBase = CAST_VOID_PTR(0x800000000);
788e920a95Sopenharmony_civoid *g_mapJitBase2 = CAST_VOID_PTR(0x800001000);
798e920a95Sopenharmony_ciconstexpr size_t PAGE_SIZE = 4096;
808e920a95Sopenharmony_ciconstexpr int BUFFER_SIZE = 4096;
818e920a95Sopenharmony_ci
828e920a95Sopenharmony_ci#define JITFORT_PRCTL_OPTION 0x6a6974
838e920a95Sopenharmony_ci#define JITFORT_CREATE_COPGTABLE    5
848e920a95Sopenharmony_ci#define MAP_JIT 0x1000
858e920a95Sopenharmony_ci
868e920a95Sopenharmony_ciconst JitBufferIntegrityLevel MIN_LEVEL = JitBufferIntegrityLevel::Level0;
878e920a95Sopenharmony_ciconst JitBufferIntegrityLevel MAX_LEVEL = JitBufferIntegrityLevel::Level1;
888e920a95Sopenharmony_ci
898e920a95Sopenharmony_cistd::mutex g_jitMemory_mutex;
908e920a95Sopenharmony_ci
918e920a95Sopenharmony_cistatic inline void AllocJitMemory()
928e920a95Sopenharmony_ci{
938e920a95Sopenharmony_ci    g_jitMemory = mmap(g_mapJitBase, PAGE_SIZE + PAGE_SIZE,
948e920a95Sopenharmony_ci        PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
958e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
968e920a95Sopenharmony_ci    int cookie = std::random_device{}();
978e920a95Sopenharmony_ci    g_jitMemory = mmap(g_mapJitBase2, PAGE_SIZE,
988e920a95Sopenharmony_ci        PROT_READ | PROT_WRITE | PROT_EXEC,
998e920a95Sopenharmony_ci        MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT, cookie, 0);
1008e920a95Sopenharmony_ci#endif
1018e920a95Sopenharmony_ci    EXPECT_NE(g_jitMemory, MAP_FAILED);
1028e920a95Sopenharmony_ci}
1038e920a95Sopenharmony_ci
1048e920a95Sopenharmony_cistatic inline void JitFortPrepare()
1058e920a95Sopenharmony_ci{
1068e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
1078e920a95Sopenharmony_ci    EXPECT_EQ(InitXpm(1, PROCESS_OWNERID_UNINIT, NULL), CS_SUCCESS);
1088e920a95Sopenharmony_ci    PrctlWrapper(JITFORT_PRCTL_OPTION, JITFORT_CREATE_COPGTABLE);
1098e920a95Sopenharmony_ci#endif
1108e920a95Sopenharmony_ci}
1118e920a95Sopenharmony_ci
1128e920a95Sopenharmony_cistatic inline void FreeJitMemory()
1138e920a95Sopenharmony_ci{
1148e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
1158e920a95Sopenharmony_ci    munmap(g_mapJitBase, PAGE_SIZE);
1168e920a95Sopenharmony_ci    munmap(g_mapJitBase2, PAGE_SIZE);
1178e920a95Sopenharmony_ci#endif
1188e920a95Sopenharmony_ci}
1198e920a95Sopenharmony_ci
1208e920a95Sopenharmony_ciclass JitCodeSignTest : public testing::Test {
1218e920a95Sopenharmony_cipublic:
1228e920a95Sopenharmony_ci    JitCodeSignTest() {};
1238e920a95Sopenharmony_ci    virtual ~JitCodeSignTest() {};
1248e920a95Sopenharmony_ci
1258e920a95Sopenharmony_ci    static void SetUpTestCase()
1268e920a95Sopenharmony_ci    {
1278e920a95Sopenharmony_ci        EXPECT_EQ(IsSupportJitCodeSigner(), true);
1288e920a95Sopenharmony_ci        JitFortPrepare();
1298e920a95Sopenharmony_ci        AllocJitMemory();
1308e920a95Sopenharmony_ci    };
1318e920a95Sopenharmony_ci
1328e920a95Sopenharmony_ci    static void TearDownTestCase()
1338e920a95Sopenharmony_ci    {
1348e920a95Sopenharmony_ci        FreeJitMemory();
1358e920a95Sopenharmony_ci    };
1368e920a95Sopenharmony_ci
1378e920a95Sopenharmony_ci    void SetUp() {};
1388e920a95Sopenharmony_ci    void TearDown() {};
1398e920a95Sopenharmony_ci};
1408e920a95Sopenharmony_ci
1418e920a95Sopenharmony_ci/**
1428e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0001
1438e920a95Sopenharmony_ci * @tc.desc: sign instructions and verify succuss
1448e920a95Sopenharmony_ci * @tc.type: Func
1458e920a95Sopenharmony_ci * @tc.require: I9O6PK
1468e920a95Sopenharmony_ci */
1478e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0001, TestSize.Level0)
1488e920a95Sopenharmony_ci{
1498e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
1508e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
1518e920a95Sopenharmony_ci        level <= MAX_LEVEL; level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
1528e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
1538e920a95Sopenharmony_ci        int i = 0;
1548e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
1558e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
1568e920a95Sopenharmony_ci            i++;
1578e920a95Sopenharmony_ci        }
1588e920a95Sopenharmony_ci
1598e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionSet,
1608e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
1618e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(g_jitMemory, g_testInstructionSet, INSTRUCTIONS_SET_SIZE_BYTES), 0);
1628e920a95Sopenharmony_ci
1638e920a95Sopenharmony_ci        delete signer;
1648e920a95Sopenharmony_ci        signer = nullptr;
1658e920a95Sopenharmony_ci    }
1668e920a95Sopenharmony_ci}
1678e920a95Sopenharmony_ci
1688e920a95Sopenharmony_ci
1698e920a95Sopenharmony_ci/**
1708e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0002
1718e920a95Sopenharmony_ci * @tc.desc: sign data and verify succuss
1728e920a95Sopenharmony_ci * @tc.type: Func
1738e920a95Sopenharmony_ci * @tc.require: I9O6PK
1748e920a95Sopenharmony_ci */
1758e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0002, TestSize.Level0)
1768e920a95Sopenharmony_ci{
1778e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
1788e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
1798e920a95Sopenharmony_ci        level <= MAX_LEVEL; level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
1808e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
1818e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
1828e920a95Sopenharmony_ci
1838e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionBuf,
1848e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
1858e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(g_jitMemory, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES), 0);
1868e920a95Sopenharmony_ci
1878e920a95Sopenharmony_ci        delete signer;
1888e920a95Sopenharmony_ci        signer = nullptr;
1898e920a95Sopenharmony_ci    }
1908e920a95Sopenharmony_ci}
1918e920a95Sopenharmony_ci
1928e920a95Sopenharmony_ci/**
1938e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0003
1948e920a95Sopenharmony_ci * @tc.desc: sign and patch instructions succuss
1958e920a95Sopenharmony_ci * @tc.type: Func
1968e920a95Sopenharmony_ci * @tc.require: I9O6PK
1978e920a95Sopenharmony_ci */
1988e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0003, TestSize.Level0)
1998e920a95Sopenharmony_ci{
2008e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
2018e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
2028e920a95Sopenharmony_ci        level <= MAX_LEVEL;
2038e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
2048e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
2058e920a95Sopenharmony_ci        int i = 0, offset = 0;
2068e920a95Sopenharmony_ci        while (i < TEST_PATCH_INDEX) {
2078e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
2088e920a95Sopenharmony_ci            i++;
2098e920a95Sopenharmony_ci        }
2108e920a95Sopenharmony_ci        for (int j = 0; j < PATCH_INSTRUCTIONS_SET_SIZE; j++) {
2118e920a95Sopenharmony_ci            WillFixUp(signer, 1);
2128e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
2138e920a95Sopenharmony_ci            i++;
2148e920a95Sopenharmony_ci        }
2158e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
2168e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
2178e920a95Sopenharmony_ci            i++;
2188e920a95Sopenharmony_ci        }
2198e920a95Sopenharmony_ci        offset = TEST_PATCH_INDEX * INSTRUCTION_SIZE;
2208e920a95Sopenharmony_ci        for (int j = 0; j < PATCH_INSTRUCTIONS_SET_SIZE; j++) {
2218e920a95Sopenharmony_ci            PatchInstruction(signer, offset, g_testPatchInstructionSet[j]);
2228e920a95Sopenharmony_ci            offset += INSTRUCTION_SIZE;
2238e920a95Sopenharmony_ci        }
2248e920a95Sopenharmony_ci
2258e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_afterPatchInstructionBuf,
2268e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
2278e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(g_jitMemory, g_afterPatchInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES), 0);
2288e920a95Sopenharmony_ci
2298e920a95Sopenharmony_ci        delete signer;
2308e920a95Sopenharmony_ci        signer = nullptr;
2318e920a95Sopenharmony_ci    }
2328e920a95Sopenharmony_ci}
2338e920a95Sopenharmony_ci
2348e920a95Sopenharmony_ci/**
2358e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0004
2368e920a95Sopenharmony_ci * @tc.desc: sign and patch data succuss
2378e920a95Sopenharmony_ci * @tc.type: Func
2388e920a95Sopenharmony_ci * @tc.require: I9O6PK
2398e920a95Sopenharmony_ci */
2408e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0004, TestSize.Level0)
2418e920a95Sopenharmony_ci{
2428e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
2438e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
2448e920a95Sopenharmony_ci        level <= MAX_LEVEL;
2458e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
2468e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
2478e920a95Sopenharmony_ci        int i = 0, offset = 0;
2488e920a95Sopenharmony_ci        while (i < TEST_PATCH_INDEX) {
2498e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
2508e920a95Sopenharmony_ci            offset += INSTRUCTION_SIZE;
2518e920a95Sopenharmony_ci            i++;
2528e920a95Sopenharmony_ci        }
2538e920a95Sopenharmony_ci
2548e920a95Sopenharmony_ci        int patchSize = sizeof(g_testPatchInstructionSet);
2558e920a95Sopenharmony_ci        WillFixUp(signer, PATCH_INSTRUCTIONS_SET_SIZE);
2568e920a95Sopenharmony_ci        AppendData(signer, CAST_VOID_PTR(&g_testInstructionSet[i]), patchSize);
2578e920a95Sopenharmony_ci        i += PATCH_INSTRUCTIONS_SET_SIZE;
2588e920a95Sopenharmony_ci        offset += patchSize;
2598e920a95Sopenharmony_ci
2608e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
2618e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
2628e920a95Sopenharmony_ci            i++;
2638e920a95Sopenharmony_ci        }
2648e920a95Sopenharmony_ci
2658e920a95Sopenharmony_ci        offset = TEST_PATCH_INDEX * INSTRUCTION_SIZE;
2668e920a95Sopenharmony_ci        PatchData(signer, offset, g_testPatchInstructionBuf, INSTRUCTION_SIZE);
2678e920a95Sopenharmony_ci
2688e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, g_afterPatchInstructionBuf);
2698e920a95Sopenharmony_ci        PatchData(signer, CAST_VOID_PTR(reinterpret_cast<uintptr_t>(
2708e920a95Sopenharmony_ci            g_afterPatchInstructionBuf) + offset + INSTRUCTION_SIZE),
2718e920a95Sopenharmony_ci            CAST_VOID_PTR(&g_testPatchInstructionSet[1]),
2728e920a95Sopenharmony_ci            INSTRUCTION_SIZE);
2738e920a95Sopenharmony_ci
2748e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_afterPatchInstructionBuf,
2758e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
2768e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(g_jitMemory, g_afterPatchInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES), 0);
2778e920a95Sopenharmony_ci
2788e920a95Sopenharmony_ci        delete signer;
2798e920a95Sopenharmony_ci        signer = nullptr;
2808e920a95Sopenharmony_ci    }
2818e920a95Sopenharmony_ci}
2828e920a95Sopenharmony_ci
2838e920a95Sopenharmony_ci/**
2848e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0005
2858e920a95Sopenharmony_ci * @tc.desc: sign and copy wrong data failed
2868e920a95Sopenharmony_ci * @tc.type: Func
2878e920a95Sopenharmony_ci * @tc.require: I9O6PK
2888e920a95Sopenharmony_ci */
2898e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0005, TestSize.Level0)
2908e920a95Sopenharmony_ci{
2918e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
2928e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
2938e920a95Sopenharmony_ci        level <= MAX_LEVEL;
2948e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
2958e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
2968e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
2978e920a95Sopenharmony_ci        int sizeInByte = sizeof(g_testInstructionSet);
2988e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_afterPatchInstructionBuf,
2998e920a95Sopenharmony_ci            sizeInByte), CS_ERR_VALIDATE_CODE);
3008e920a95Sopenharmony_ci
3018e920a95Sopenharmony_ci        delete signer;
3028e920a95Sopenharmony_ci        signer = nullptr;
3038e920a95Sopenharmony_ci    }
3048e920a95Sopenharmony_ci}
3058e920a95Sopenharmony_ci
3068e920a95Sopenharmony_ci/**
3078e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0006
3088e920a95Sopenharmony_ci * @tc.desc: sign and copy with wrong size failed
3098e920a95Sopenharmony_ci * @tc.type: Func
3108e920a95Sopenharmony_ci * @tc.require: I9O6PK
3118e920a95Sopenharmony_ci */
3128e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0006, TestSize.Level0)
3138e920a95Sopenharmony_ci{
3148e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
3158e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
3168e920a95Sopenharmony_ci        level <= MAX_LEVEL;
3178e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
3188e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
3198e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, g_testInstructionBuf);
3208e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
3218e920a95Sopenharmony_ci
3228e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionBuf,
3238e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES - 1), CS_ERR_JIT_SIGN_SIZE);
3248e920a95Sopenharmony_ci
3258e920a95Sopenharmony_ci        delete signer;
3268e920a95Sopenharmony_ci        signer = nullptr;
3278e920a95Sopenharmony_ci    }
3288e920a95Sopenharmony_ci}
3298e920a95Sopenharmony_ci
3308e920a95Sopenharmony_ci/**
3318e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0007
3328e920a95Sopenharmony_ci * @tc.desc: sign and copy with buffer failed
3338e920a95Sopenharmony_ci * @tc.type: Func
3348e920a95Sopenharmony_ci * @tc.require: I9O6PK
3358e920a95Sopenharmony_ci */
3368e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0007, TestSize.Level0)
3378e920a95Sopenharmony_ci{
3388e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
3398e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
3408e920a95Sopenharmony_ci        level <= MAX_LEVEL;
3418e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
3428e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
3438e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, g_testInstructionBuf);
3448e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES - 1);
3458e920a95Sopenharmony_ci
3468e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_afterPatchInstructionBuf,
3478e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_ERR_JIT_SIGN_SIZE);
3488e920a95Sopenharmony_ci
3498e920a95Sopenharmony_ci        delete signer;
3508e920a95Sopenharmony_ci        signer = nullptr;
3518e920a95Sopenharmony_ci    }
3528e920a95Sopenharmony_ci}
3538e920a95Sopenharmony_ci
3548e920a95Sopenharmony_ci/**
3558e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0008
3568e920a95Sopenharmony_ci * @tc.desc: sign data without 4 byte-alignment and copy success
3578e920a95Sopenharmony_ci * @tc.type: Func
3588e920a95Sopenharmony_ci * @tc.require: I9O6PK
3598e920a95Sopenharmony_ci */
3608e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0008, TestSize.Level0)
3618e920a95Sopenharmony_ci{
3628e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
3638e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
3648e920a95Sopenharmony_ci        level <= MAX_LEVEL;
3658e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
3668e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
3678e920a95Sopenharmony_ci        Byte *ptr = reinterpret_cast<Byte *>(g_testInstructionBuf) + 1;
3688e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, 1);
3698e920a95Sopenharmony_ci        AppendData(signer, CAST_VOID_PTR(ptr), INSTRUCTIONS_SET_SIZE_BYTES - 1);
3708e920a95Sopenharmony_ci
3718e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionBuf,
3728e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
3738e920a95Sopenharmony_ci
3748e920a95Sopenharmony_ci        delete signer;
3758e920a95Sopenharmony_ci        signer = nullptr;
3768e920a95Sopenharmony_ci    }
3778e920a95Sopenharmony_ci}
3788e920a95Sopenharmony_ci
3798e920a95Sopenharmony_ci/**
3808e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0009
3818e920a95Sopenharmony_ci * @tc.desc: sign data and patch without 4 byte-alignment failed
3828e920a95Sopenharmony_ci * @tc.type: Func
3838e920a95Sopenharmony_ci * @tc.require: I9O6PK
3848e920a95Sopenharmony_ci */
3858e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0009, TestSize.Level0)
3868e920a95Sopenharmony_ci{
3878e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
3888e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
3898e920a95Sopenharmony_ci        level <= MAX_LEVEL;
3908e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
3918e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
3928e920a95Sopenharmony_ci        int i = 0, offset = 0;
3938e920a95Sopenharmony_ci        while (i < TEST_PATCH_INDEX) {
3948e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
3958e920a95Sopenharmony_ci            offset += INSTRUCTION_SIZE;
3968e920a95Sopenharmony_ci            i++;
3978e920a95Sopenharmony_ci        }
3988e920a95Sopenharmony_ci
3998e920a95Sopenharmony_ci        int patchSize = sizeof(g_testPatchInstructionSet);
4008e920a95Sopenharmony_ci        WillFixUp(signer, PATCH_INSTRUCTIONS_SET_SIZE);
4018e920a95Sopenharmony_ci        AppendData(signer, CAST_VOID_PTR(&g_testInstructionSet[i]), patchSize);
4028e920a95Sopenharmony_ci        i += PATCH_INSTRUCTIONS_SET_SIZE;
4038e920a95Sopenharmony_ci        offset += patchSize;
4048e920a95Sopenharmony_ci
4058e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
4068e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
4078e920a95Sopenharmony_ci            i++;
4088e920a95Sopenharmony_ci        }
4098e920a95Sopenharmony_ci
4108e920a95Sopenharmony_ci        offset = TEST_PATCH_INDEX * INSTRUCTION_SIZE;
4118e920a95Sopenharmony_ci        EXPECT_EQ(PatchData(signer, offset, g_testPatchInstructionBuf,
4128e920a95Sopenharmony_ci            patchSize - 1), CS_ERR_JIT_SIGN_SIZE);
4138e920a95Sopenharmony_ci
4148e920a95Sopenharmony_ci        delete signer;
4158e920a95Sopenharmony_ci        signer = nullptr;
4168e920a95Sopenharmony_ci    }
4178e920a95Sopenharmony_ci}
4188e920a95Sopenharmony_ci
4198e920a95Sopenharmony_ci/**
4208e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0010
4218e920a95Sopenharmony_ci * @tc.desc: patch with buffer address successfully
4228e920a95Sopenharmony_ci * @tc.type: Func
4238e920a95Sopenharmony_ci * @tc.require: I9O6PK
4248e920a95Sopenharmony_ci */
4258e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0010, TestSize.Level0)
4268e920a95Sopenharmony_ci{
4278e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
4288e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
4298e920a95Sopenharmony_ci        level <= MAX_LEVEL;
4308e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
4318e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
4328e920a95Sopenharmony_ci        int i = 0, offset = 0;
4338e920a95Sopenharmony_ci        while (i < TEST_PATCH_INDEX) {
4348e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
4358e920a95Sopenharmony_ci            i++;
4368e920a95Sopenharmony_ci        }
4378e920a95Sopenharmony_ci        for (int j = 0; j < PATCH_INSTRUCTIONS_SET_SIZE; j++) {
4388e920a95Sopenharmony_ci            WillFixUp(signer, 1);
4398e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
4408e920a95Sopenharmony_ci            i++;
4418e920a95Sopenharmony_ci        }
4428e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
4438e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
4448e920a95Sopenharmony_ci            i++;
4458e920a95Sopenharmony_ci        }
4468e920a95Sopenharmony_ci        offset = TEST_PATCH_INDEX * INSTRUCTION_SIZE;
4478e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, g_afterPatchInstructionBuf);
4488e920a95Sopenharmony_ci        for (int j = 0; j < PATCH_INSTRUCTIONS_SET_SIZE; j++) {
4498e920a95Sopenharmony_ci            PatchInstruction(signer, CAST_VOID_PTR(
4508e920a95Sopenharmony_ci                &g_afterPatchInstructionSet[TEST_PATCH_INDEX + j]), g_testPatchInstructionSet[j]);
4518e920a95Sopenharmony_ci            offset += INSTRUCTION_SIZE;
4528e920a95Sopenharmony_ci        }
4538e920a95Sopenharmony_ci
4548e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_afterPatchInstructionBuf,
4558e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
4568e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(g_jitMemory, g_afterPatchInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES), 0);
4578e920a95Sopenharmony_ci
4588e920a95Sopenharmony_ci        delete signer;
4598e920a95Sopenharmony_ci        signer = nullptr;
4608e920a95Sopenharmony_ci    }
4618e920a95Sopenharmony_ci}
4628e920a95Sopenharmony_ci
4638e920a95Sopenharmony_ci/**
4648e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0011
4658e920a95Sopenharmony_ci * @tc.desc: patch faied with invalid buffer
4668e920a95Sopenharmony_ci * @tc.type: Func
4678e920a95Sopenharmony_ci * @tc.require: I9O6PK
4688e920a95Sopenharmony_ci */
4698e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0011, TestSize.Level0)
4708e920a95Sopenharmony_ci{
4718e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
4728e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
4738e920a95Sopenharmony_ci        level <= MAX_LEVEL;
4748e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
4758e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
4768e920a95Sopenharmony_ci
4778e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, g_afterPatchInstructionBuf);
4788e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, nullptr, INSTRUCTION_SIZE), CS_ERR_PATCH_INVALID);
4798e920a95Sopenharmony_ci
4808e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, nullptr);
4818e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, reinterpret_cast<Byte *>(g_afterPatchInstructionBuf),
4828e920a95Sopenharmony_ci            INSTRUCTION_SIZE), CS_ERR_PATCH_INVALID);
4838e920a95Sopenharmony_ci
4848e920a95Sopenharmony_ci        delete signer;
4858e920a95Sopenharmony_ci        signer = nullptr;
4868e920a95Sopenharmony_ci    }
4878e920a95Sopenharmony_ci}
4888e920a95Sopenharmony_ci
4898e920a95Sopenharmony_ci/**
4908e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_00012
4918e920a95Sopenharmony_ci * @tc.desc: reset jit memory success
4928e920a95Sopenharmony_ci * @tc.type: Func
4938e920a95Sopenharmony_ci * @tc.require: I9O6PK
4948e920a95Sopenharmony_ci */
4958e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_00012, TestSize.Level0)
4968e920a95Sopenharmony_ci{
4978e920a95Sopenharmony_ci    Byte tmpBuffer[INSTRUCTIONS_SET_SIZE_BYTES] = {0};
4988e920a95Sopenharmony_ci    ResetJitCode(g_jitMemory, INSTRUCTIONS_SET_SIZE_BYTES);
4998e920a95Sopenharmony_ci    EXPECT_EQ(memcmp(g_jitMemory, tmpBuffer, INSTRUCTIONS_SET_SIZE_BYTES), 0);
5008e920a95Sopenharmony_ci}
5018e920a95Sopenharmony_ci
5028e920a95Sopenharmony_ci/**
5038e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_00013
5048e920a95Sopenharmony_ci * @tc.desc: copy failed with wrong size
5058e920a95Sopenharmony_ci * @tc.type: Func
5068e920a95Sopenharmony_ci * @tc.require: I9O6PK
5078e920a95Sopenharmony_ci */
5088e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_00013, TestSize.Level0)
5098e920a95Sopenharmony_ci{
5108e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
5118e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
5128e920a95Sopenharmony_ci        level <= MAX_LEVEL;
5138e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
5148e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
5158e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
5168e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionBuf,
5178e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES - 1), CS_ERR_JIT_SIGN_SIZE);
5188e920a95Sopenharmony_ci
5198e920a95Sopenharmony_ci        signer->Reset();
5208e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionBuf,
5218e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_ERR_JIT_SIGN_SIZE);
5228e920a95Sopenharmony_ci
5238e920a95Sopenharmony_ci        delete signer;
5248e920a95Sopenharmony_ci        signer = nullptr;
5258e920a95Sopenharmony_ci    }
5268e920a95Sopenharmony_ci}
5278e920a95Sopenharmony_ci
5288e920a95Sopenharmony_ci/**
5298e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_00014
5308e920a95Sopenharmony_ci * @tc.desc: copy data with different size
5318e920a95Sopenharmony_ci * @tc.type: Func
5328e920a95Sopenharmony_ci * @tc.require: I9O6PK
5338e920a95Sopenharmony_ci */
5348e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_00014, TestSize.Level0)
5358e920a95Sopenharmony_ci{
5368e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
5378e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
5388e920a95Sopenharmony_ci        level <= MAX_LEVEL;
5398e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
5408e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
5418e920a95Sopenharmony_ci        Byte *data = reinterpret_cast<Byte *>(g_testInstructionSet);
5428e920a95Sopenharmony_ci        uint32_t dataSize[] = {1, 2, 1, 4, 2, 8, 2, 1, 3};
5438e920a95Sopenharmony_ci        int pos = 0;
5448e920a95Sopenharmony_ci        for (auto size : dataSize) {
5458e920a95Sopenharmony_ci            AppendData(signer, CAST_VOID_PTR(data + pos), size);
5468e920a95Sopenharmony_ci            pos += size;
5478e920a95Sopenharmony_ci        }
5488e920a95Sopenharmony_ci
5498e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, g_testInstructionBuf,
5508e920a95Sopenharmony_ci            INSTRUCTIONS_SET_SIZE_BYTES), CS_SUCCESS);
5518e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(g_jitMemory, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES), 0);
5528e920a95Sopenharmony_ci
5538e920a95Sopenharmony_ci        delete signer;
5548e920a95Sopenharmony_ci        signer = nullptr;
5558e920a95Sopenharmony_ci    }
5568e920a95Sopenharmony_ci}
5578e920a95Sopenharmony_ci
5588e920a95Sopenharmony_ci/**
5598e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_00015
5608e920a95Sopenharmony_ci * @tc.desc: validate and copy code to same buffer in parallel
5618e920a95Sopenharmony_ci * @tc.type: Func
5628e920a95Sopenharmony_ci * @tc.require: I9O6PK
5638e920a95Sopenharmony_ci */
5648e920a95Sopenharmony_ciHWMTEST_F(JitCodeSignTest, JitCodeSignTest_00015, TestSize.Level1, MULTI_THREAD_NUM)
5658e920a95Sopenharmony_ci{
5668e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
5678e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
5688e920a95Sopenharmony_ci        level <= MAX_LEVEL;
5698e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
5708e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
5718e920a95Sopenharmony_ci        int i = 0;
5728e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
5738e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
5748e920a95Sopenharmony_ci            i++;
5758e920a95Sopenharmony_ci        }
5768e920a95Sopenharmony_ci        size_t size = INSTRUCTIONS_SET_SIZE_BYTES;
5778e920a95Sopenharmony_ci        {
5788e920a95Sopenharmony_ci            std::lock_guard<std::mutex> lock(g_jitMemory_mutex);
5798e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
5808e920a95Sopenharmony_ci            PrctlWrapper(JITFORT_PRCTL_OPTION, JITFORT_SWITCH_IN, 0);
5818e920a95Sopenharmony_ci#endif
5828e920a95Sopenharmony_ci            EXPECT_EQ(signer->ValidateCodeCopy(reinterpret_cast<Instr *>(g_jitMemory),
5838e920a95Sopenharmony_ci                reinterpret_cast<Byte *>(g_testInstructionSet), size), CS_SUCCESS);
5848e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
5858e920a95Sopenharmony_ci            PrctlWrapper(JITFORT_PRCTL_OPTION, JITFORT_SWITCH_OUT, 0);
5868e920a95Sopenharmony_ci#endif
5878e920a95Sopenharmony_ci            EXPECT_EQ(memcmp(g_jitMemory, g_testInstructionSet, size), 0);
5888e920a95Sopenharmony_ci        }
5898e920a95Sopenharmony_ci
5908e920a95Sopenharmony_ci        delete signer;
5918e920a95Sopenharmony_ci        signer = nullptr;
5928e920a95Sopenharmony_ci    }
5938e920a95Sopenharmony_ci}
5948e920a95Sopenharmony_ci
5958e920a95Sopenharmony_ci/**
5968e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0016
5978e920a95Sopenharmony_ci * @tc.desc: validate and copy code to different buffer in parallel
5988e920a95Sopenharmony_ci * @tc.type: Func
5998e920a95Sopenharmony_ci * @tc.require: I9O6PK
6008e920a95Sopenharmony_ci */
6018e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_00016, TestSize.Level0)
6028e920a95Sopenharmony_ci{
6038e920a95Sopenharmony_ci    void *tmpMemory = nullptr;
6048e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
6058e920a95Sopenharmony_ci    int cookie = std::random_device{}();
6068e920a95Sopenharmony_ci    tmpMemory = mmap(nullptr, PAGE_SIZE,
6078e920a95Sopenharmony_ci        PROT_READ | PROT_WRITE | PROT_EXEC,
6088e920a95Sopenharmony_ci        MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT, cookie, 0);
6098e920a95Sopenharmony_ci#else
6108e920a95Sopenharmony_ci    tmpMemory = mmap(nullptr, PAGE_SIZE,
6118e920a95Sopenharmony_ci        PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
6128e920a95Sopenharmony_ci#endif
6138e920a95Sopenharmony_ci    EXPECT_NE(tmpMemory, MAP_FAILED);
6148e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
6158e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
6168e920a95Sopenharmony_ci        level <= MAX_LEVEL;
6178e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
6188e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
6198e920a95Sopenharmony_ci        int i = 0;
6208e920a95Sopenharmony_ci        while (i < INSTRUCTIONS_SET_SIZE) {
6218e920a95Sopenharmony_ci            AppendInstruction(signer, g_testInstructionSet[i]);
6228e920a95Sopenharmony_ci            i++;
6238e920a95Sopenharmony_ci        }
6248e920a95Sopenharmony_ci        size_t size = INSTRUCTIONS_SET_SIZE_BYTES;
6258e920a95Sopenharmony_ci        {
6268e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
6278e920a95Sopenharmony_ci            PrctlWrapper(JITFORT_PRCTL_OPTION, JITFORT_SWITCH_IN, 0);
6288e920a95Sopenharmony_ci#endif
6298e920a95Sopenharmony_ci            EXPECT_EQ(signer->ValidateCodeCopy(reinterpret_cast<Instr *>(tmpMemory),
6308e920a95Sopenharmony_ci                reinterpret_cast<Byte *>(g_testInstructionSet), size), CS_SUCCESS);
6318e920a95Sopenharmony_ci#ifndef JIT_FORT_DISABLE
6328e920a95Sopenharmony_ci            PrctlWrapper(JITFORT_PRCTL_OPTION, JITFORT_SWITCH_OUT, 0);
6338e920a95Sopenharmony_ci#endif
6348e920a95Sopenharmony_ci            EXPECT_EQ(memcmp(tmpMemory, g_testInstructionSet, size), 0);
6358e920a95Sopenharmony_ci        }
6368e920a95Sopenharmony_ci
6378e920a95Sopenharmony_ci        delete signer;
6388e920a95Sopenharmony_ci        signer = nullptr;
6398e920a95Sopenharmony_ci    }
6408e920a95Sopenharmony_ci    munmap(tmpMemory, PAGE_SIZE);
6418e920a95Sopenharmony_ci}
6428e920a95Sopenharmony_ci
6438e920a95Sopenharmony_ci/**
6448e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0017
6458e920a95Sopenharmony_ci * @tc.desc: validate and copy code to same buffer in parallel
6468e920a95Sopenharmony_ci * @tc.type: Func
6478e920a95Sopenharmony_ci * @tc.require: I9O6PK
6488e920a95Sopenharmony_ci */
6498e920a95Sopenharmony_ciHWMTEST_F(JitCodeSignTest, JitCodeSignTest_0017, TestSize.Level1, MULTI_THREAD_NUM)
6508e920a95Sopenharmony_ci{
6518e920a95Sopenharmony_ci    int instructionNum = BUFFER_SIZE / sizeof(uint32_t);
6528e920a95Sopenharmony_ci    uint32_t *tmpBuffer = reinterpret_cast<uint32_t *>(malloc(BUFFER_SIZE));
6538e920a95Sopenharmony_ci    for (int i = 0; i < instructionNum; i++) {
6548e920a95Sopenharmony_ci        tmpBuffer[i] = i;
6558e920a95Sopenharmony_ci    }
6568e920a95Sopenharmony_ci
6578e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
6588e920a95Sopenharmony_ci        level <= MAX_LEVEL;
6598e920a95Sopenharmony_ci        level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
6608e920a95Sopenharmony_ci        JitCodeSignerBase *signer = CreateJitCodeSigner(level);
6618e920a95Sopenharmony_ci        int i = 0;
6628e920a95Sopenharmony_ci        while (i < instructionNum) {
6638e920a95Sopenharmony_ci            AppendInstruction(signer, tmpBuffer[i]);
6648e920a95Sopenharmony_ci            i++;
6658e920a95Sopenharmony_ci        }
6668e920a95Sopenharmony_ci        int cookie = std::random_device{}();
6678e920a95Sopenharmony_ci        void *tmpJitMemory = mmap(nullptr, PAGE_SIZE,
6688e920a95Sopenharmony_ci            PROT_READ | PROT_WRITE | PROT_EXEC,
6698e920a95Sopenharmony_ci            MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT, cookie, 0);
6708e920a95Sopenharmony_ci
6718e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, tmpJitMemory, tmpBuffer, BUFFER_SIZE), CS_SUCCESS);
6728e920a95Sopenharmony_ci        EXPECT_EQ(memcmp(tmpJitMemory, tmpBuffer, BUFFER_SIZE), 0);
6738e920a95Sopenharmony_ci
6748e920a95Sopenharmony_ci        delete signer;
6758e920a95Sopenharmony_ci        signer = nullptr;
6768e920a95Sopenharmony_ci    }
6778e920a95Sopenharmony_ci    free(tmpBuffer);
6788e920a95Sopenharmony_ci    tmpBuffer = nullptr;
6798e920a95Sopenharmony_ci}
6808e920a95Sopenharmony_ci
6818e920a95Sopenharmony_ci/**
6828e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0018
6838e920a95Sopenharmony_ci * @tc.desc: no signer
6848e920a95Sopenharmony_ci * @tc.type: Func
6858e920a95Sopenharmony_ci * @tc.require: I9O6PK
6868e920a95Sopenharmony_ci */
6878e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0018, TestSize.Level0)
6888e920a95Sopenharmony_ci{
6898e920a95Sopenharmony_ci    EXPECT_EQ(RegisterTmpBuffer(nullptr, nullptr), CS_ERR_NO_SIGNER);
6908e920a95Sopenharmony_ci    EXPECT_EQ(AppendInstruction(nullptr, 0), CS_ERR_NO_SIGNER);
6918e920a95Sopenharmony_ci    EXPECT_EQ(AppendData(nullptr, nullptr, 0), CS_ERR_NO_SIGNER);
6928e920a95Sopenharmony_ci    EXPECT_EQ(WillFixUp(nullptr, 1), CS_ERR_NO_SIGNER);
6938e920a95Sopenharmony_ci    EXPECT_EQ(PatchInstruction(nullptr, 0, 0), CS_ERR_NO_SIGNER);
6948e920a95Sopenharmony_ci    EXPECT_EQ(PatchInstruction(nullptr, nullptr, 1), CS_ERR_NO_SIGNER);
6958e920a95Sopenharmony_ci    EXPECT_EQ(PatchData(nullptr, 0, nullptr, 0), CS_ERR_NO_SIGNER);
6968e920a95Sopenharmony_ci    EXPECT_EQ(PatchData(nullptr, nullptr, nullptr, 0), CS_ERR_NO_SIGNER);
6978e920a95Sopenharmony_ci    EXPECT_EQ(CopyToJitCode(nullptr, nullptr, nullptr, 0), CS_ERR_NO_SIGNER);
6988e920a95Sopenharmony_ci}
6998e920a95Sopenharmony_ci
7008e920a95Sopenharmony_ci/**
7018e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0019
7028e920a95Sopenharmony_ci * @tc.desc: create failed
7038e920a95Sopenharmony_ci * @tc.type: Func
7048e920a95Sopenharmony_ci * @tc.require: I9O6PK
7058e920a95Sopenharmony_ci */
7068e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0019, TestSize.Level0)
7078e920a95Sopenharmony_ci{
7088e920a95Sopenharmony_ci    EXPECT_EQ(CreateJitCodeSigner(
7098e920a95Sopenharmony_ci        static_cast<JitBufferIntegrityLevel>(static_cast<int>(MAX_LEVEL) + 1)),
7108e920a95Sopenharmony_ci        nullptr);
7118e920a95Sopenharmony_ci}
7128e920a95Sopenharmony_ci
7138e920a95Sopenharmony_ci/**
7148e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0020
7158e920a95Sopenharmony_ci * @tc.desc: patch instruction failed for wrong offset or address
7168e920a95Sopenharmony_ci * @tc.type: Func
7178e920a95Sopenharmony_ci * @tc.require: I9O6PK
7188e920a95Sopenharmony_ci */
7198e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0020, TestSize.Level0)
7208e920a95Sopenharmony_ci{
7218e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
7228e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
7238e920a95Sopenharmony_ci        level <= MAX_LEVEL; level = static_cast<JitBufferIntegrityLevel>(static_cast<int>(level) + 1)) {
7248e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
7258e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
7268e920a95Sopenharmony_ci
7278e920a95Sopenharmony_ci        // offset is greater than signed size
7288e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, INSTRUCTIONS_SET_SIZE_BYTES + 4, 1), CS_ERR_PATCH_INVALID);
7298e920a95Sopenharmony_ci        // offset < 0
7308e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, -INSTRUCTION_SIZE, 1), CS_ERR_PATCH_INVALID);
7318e920a95Sopenharmony_ci
7328e920a95Sopenharmony_ci        // offset is greater than signed size
7338e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, CAST_VOID_PTR(reinterpret_cast<uintptr_t>(
7348e920a95Sopenharmony_ci            g_testInstructionBuf) + INSTRUCTIONS_SET_SIZE_BYTES), 1), CS_ERR_PATCH_INVALID);
7358e920a95Sopenharmony_ci        // offset < 0
7368e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, CAST_VOID_PTR(reinterpret_cast<uintptr_t>(
7378e920a95Sopenharmony_ci            g_testInstructionBuf) - INSTRUCTION_SIZE), 1), CS_ERR_PATCH_INVALID);
7388e920a95Sopenharmony_ci
7398e920a95Sopenharmony_ci        delete signer;
7408e920a95Sopenharmony_ci        signer = nullptr;
7418e920a95Sopenharmony_ci    }
7428e920a95Sopenharmony_ci}
7438e920a95Sopenharmony_ci
7448e920a95Sopenharmony_ci/**
7458e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0021
7468e920a95Sopenharmony_ci * @tc.desc: append or patch data with nullptr failed
7478e920a95Sopenharmony_ci * @tc.type: Func
7488e920a95Sopenharmony_ci * @tc.require: I9O6PK
7498e920a95Sopenharmony_ci */
7508e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0021, TestSize.Level0)
7518e920a95Sopenharmony_ci{
7528e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
7538e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
7548e920a95Sopenharmony_ci        level <= MAX_LEVEL; level = static_cast<JitBufferIntegrityLevel>(
7558e920a95Sopenharmony_ci        static_cast<int>(level) + 1)) {
7568e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
7578e920a95Sopenharmony_ci        AppendData(signer, nullptr, INSTRUCTIONS_SET_SIZE_BYTES);
7588e920a95Sopenharmony_ci
7598e920a95Sopenharmony_ci        AppendData(signer, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
7608e920a95Sopenharmony_ci        EXPECT_EQ(PatchInstruction(signer, nullptr, 0), CS_ERR_PATCH_INVALID);
7618e920a95Sopenharmony_ci        EXPECT_EQ(PatchData(signer, 0, nullptr, 0), CS_ERR_INVALID_DATA);
7628e920a95Sopenharmony_ci
7638e920a95Sopenharmony_ci        RegisterTmpBuffer(signer, g_testInstructionBuf);
7648e920a95Sopenharmony_ci        EXPECT_EQ(PatchData(signer, reinterpret_cast<Byte *>(g_testInstructionBuf),
7658e920a95Sopenharmony_ci            nullptr, 0), CS_ERR_INVALID_DATA);
7668e920a95Sopenharmony_ci
7678e920a95Sopenharmony_ci        delete signer;
7688e920a95Sopenharmony_ci        signer = nullptr;
7698e920a95Sopenharmony_ci    }
7708e920a95Sopenharmony_ci}
7718e920a95Sopenharmony_ci
7728e920a95Sopenharmony_ci/**
7738e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0022
7748e920a95Sopenharmony_ci * @tc.desc: jit memory == nullptr
7758e920a95Sopenharmony_ci * @tc.type: Func
7768e920a95Sopenharmony_ci * @tc.require: I9O6PK
7778e920a95Sopenharmony_ci */
7788e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0022, TestSize.Level0)
7798e920a95Sopenharmony_ci{
7808e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
7818e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
7828e920a95Sopenharmony_ci        level <= MAX_LEVEL; level = static_cast<JitBufferIntegrityLevel>(
7838e920a95Sopenharmony_ci        static_cast<int>(level) + 1)) {
7848e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
7858e920a95Sopenharmony_ci        EXPECT_EQ(ResetJitCode(nullptr, 0), CS_ERR_JIT_MEMORY);
7868e920a95Sopenharmony_ci        EXPECT_EQ(CopyToJitCode(signer, nullptr, g_testInstructionBuf, 0), CS_ERR_JIT_MEMORY);
7878e920a95Sopenharmony_ci
7888e920a95Sopenharmony_ci        delete signer;
7898e920a95Sopenharmony_ci        signer = nullptr;
7908e920a95Sopenharmony_ci    }
7918e920a95Sopenharmony_ci}
7928e920a95Sopenharmony_ci
7938e920a95Sopenharmony_ci/**
7948e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0023
7958e920a95Sopenharmony_ci * @tc.desc: sign instructions and verify succuss
7968e920a95Sopenharmony_ci * @tc.type: Func
7978e920a95Sopenharmony_ci * @tc.require: I9O6PK
7988e920a95Sopenharmony_ci */
7998e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0023, TestSize.Level0)
8008e920a95Sopenharmony_ci{
8018e920a95Sopenharmony_ci    JitCodeSignerBase *signer = nullptr;
8028e920a95Sopenharmony_ci    for (JitBufferIntegrityLevel level = MIN_LEVEL;
8038e920a95Sopenharmony_ci        level <= MAX_LEVEL; level = static_cast<JitBufferIntegrityLevel>(
8048e920a95Sopenharmony_ci        static_cast<int>(level) + 1)) {
8058e920a95Sopenharmony_ci        signer = CreateJitCodeSigner(level);
8068e920a95Sopenharmony_ci        for (int i = 0; i < INSTRUCTIONS_SET_SIZE_BYTES; i++) {
8078e920a95Sopenharmony_ci            uint32_t tmpBuffer[INSTRUCTIONS_SET_SIZE];
8088e920a95Sopenharmony_ci            (void) memcpy_s(tmpBuffer, INSTRUCTIONS_SET_SIZE_BYTES, g_testInstructionBuf, INSTRUCTIONS_SET_SIZE_BYTES);
8098e920a95Sopenharmony_ci
8108e920a95Sopenharmony_ci            for (int j = 0; j < INSTRUCTIONS_SET_SIZE; j++) {
8118e920a95Sopenharmony_ci                AppendInstruction(signer, tmpBuffer[j]);
8128e920a95Sopenharmony_ci            }
8138e920a95Sopenharmony_ci            *(reinterpret_cast<Byte *>(tmpBuffer) + i) = 0;
8148e920a95Sopenharmony_ci
8158e920a95Sopenharmony_ci            EXPECT_EQ(CopyToJitCode(signer, g_jitMemory, tmpBuffer,
8168e920a95Sopenharmony_ci                INSTRUCTIONS_SET_SIZE_BYTES), CS_ERR_VALIDATE_CODE);
8178e920a95Sopenharmony_ci        }
8188e920a95Sopenharmony_ci
8198e920a95Sopenharmony_ci        delete signer;
8208e920a95Sopenharmony_ci        signer = nullptr;
8218e920a95Sopenharmony_ci    }
8228e920a95Sopenharmony_ci}
8238e920a95Sopenharmony_ci
8248e920a95Sopenharmony_ci/**
8258e920a95Sopenharmony_ci * @tc.name: JitCodeSignTest_0024
8268e920a95Sopenharmony_ci * @tc.desc: pac sign with auth
8278e920a95Sopenharmony_ci * @tc.type: Func
8288e920a95Sopenharmony_ci * @tc.require: IAKH9D
8298e920a95Sopenharmony_ci */
8308e920a95Sopenharmony_ciHWTEST_F(JitCodeSignTest, JitCodeSignTest_0024, TestSize.Level0)
8318e920a95Sopenharmony_ci{
8328e920a95Sopenharmony_ci    PACSignCtx signCtx(CTXPurpose::SIGN);
8338e920a95Sopenharmony_ci    signCtx.InitSalt();
8348e920a95Sopenharmony_ci    signCtx.Init(0);
8358e920a95Sopenharmony_ci    uint32_t signature[INSTRUCTIONS_SET_SIZE];
8368e920a95Sopenharmony_ci    int i;
8378e920a95Sopenharmony_ci    for (i = 0; i < INSTRUCTIONS_SET_SIZE; i++) {
8388e920a95Sopenharmony_ci        signature[i] = signCtx.Update(g_testInstructionSet[i]);
8398e920a95Sopenharmony_ci    }
8408e920a95Sopenharmony_ci    PACSignCtx verifyCtx(CTXPurpose::VERIFY, signCtx.GetSalt());
8418e920a95Sopenharmony_ci    verifyCtx.Init(0);
8428e920a95Sopenharmony_ci    for (i = 0; i < INSTRUCTIONS_SET_SIZE; i++) {
8438e920a95Sopenharmony_ci        EXPECT_EQ(signature[i], verifyCtx.Update(g_testInstructionSet[i]));
8448e920a95Sopenharmony_ci    }
8458e920a95Sopenharmony_ci}
8468e920a95Sopenharmony_ci}
8478e920a95Sopenharmony_ci}
8488e920a95Sopenharmony_ci}
849