• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/dfx/stackinfo/js_stackinfo.h"
17 #include "ecmascript/tests/test_helper.h"
18 
19 using namespace panda::ecmascript;
20 
21 namespace panda::test {
22 class FrameTest : public testing::Test {
23 public:
SetUpTestCase()24     static void SetUpTestCase()
25     {
26         GTEST_LOG_(INFO) << "SetUpTestCase";
27     }
28 
TearDownTestCase()29     static void TearDownTestCase()
30     {
31         GTEST_LOG_(INFO) << "TearDownCase";
32     }
33 
SetUp()34     void SetUp() override
35     {
36     }
37 
TearDown()38     void TearDown() override
39     {
40     }
41 };
42 
43 /*
44  * @tc.name: GetArkJSHeapCrashInfoTest
45  * @tc.desc: Test API get_ark_js_heap_crash_info
46  * @tc.type: FUNC
47  * @tc.require:
48  */
HWTEST_F_L0(FrameTest,GetArkJSHeapCrashInfoTest)49 HWTEST_F_L0(FrameTest, GetArkJSHeapCrashInfoTest)
50 {
51     if (sizeof(uintptr_t) == sizeof(uint32_t)) {  // 32 bit
52         // The frame structure is different between 32 bit and 64 bit.
53         // Skip 32 bit because there is no ArkJS Heap.
54         return;
55     }
56     uint8_t bytecode[] = {
57         12,  // 12: HandleMul2Imm8V8
58         34,  // 34: HandleDecImm8
59         56,  // 56: HandleStobjbyvalueImm8V8V8
60         78,  // 78: HandleJmpImm16
61         90,  // 90: HandleJstrictequndefinedImm8
62         254, // 254: HandleThrow
63         8,   // 8: HandleThrowIfsupernotcorrectcallPrefImm16
64     };
65     JSTaggedType frame[9];  // 9: size of AsmInterpretedFrame
66     frame[0] = JSTaggedValue::Hole().GetRawData();  // 0: function
67     frame[1] = JSTaggedValue::Hole().GetRawData();  // 1: thisObj
68     frame[2] = JSTaggedValue::Hole().GetRawData();  // 2: acc
69     frame[3] = JSTaggedValue::Hole().GetRawData();  // 3: env
70     frame[4] = static_cast<JSTaggedType>(0);  // 4: callSize
71     frame[5] = static_cast<JSTaggedType>(0);  // 5: fp
72     frame[6] = reinterpret_cast<JSTaggedType>(&bytecode[2]);  // 6: pc
73     frame[7] = static_cast<JSTaggedType>(0);  // 7: base.prev
74     frame[8] = static_cast<JSTaggedType>(FrameType::ASM_INTERPRETER_FRAME);  // 8: base.type
75     uintptr_t x20 = reinterpret_cast<uintptr_t>(&bytecode[5]);  // 5: HandleThrow offset
76     uintptr_t fp = reinterpret_cast<uintptr_t>(&frame[9]);  // 9: bottom of frame
77     char buf[256];  // 256: buf size
78     int result = get_ark_js_heap_crash_info(getpid(), &x20, &fp, 0, &buf[0], 256);  // 256: buf size
79     EXPECT_EQ(result, 1);
80     EXPECT_EQ(std::string(buf), std::string("RegisterBytecode:HandleThrow/HandleThrowIfsupernotcorrectcallPrefImm16") +
81                                 std::string(" FrameBytecode:HandleStobjbyvalueImm8V8V8"));
82 }
83 }  // namespace panda::ecmascript
84