1 // Copyright 2021 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/codegen/macro-assembler.h"
6 #include "src/codegen/riscv64/assembler-riscv64-inl.h"
7 #include "src/execution/simulator.h"
8 #include "test/common/assembler-tester.h"
9 #include "test/unittests/test-utils.h"
10 #include "testing/gtest-support.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 #define __ tasm.
16 
17 // Test the x64 assembler by compiling some simple functions into
18 // a buffer and executing them.  These tests do not initialize the
19 // V8 library, create a context, or use any V8 objects.
20 
21 class TurboAssemblerTest : public TestWithIsolate {};
22 
TEST_F(TurboAssemblerTest,TestHardAbort)23 TEST_F(TurboAssemblerTest, TestHardAbort) {
24   auto buffer = AllocateAssemblerBuffer();
25   TurboAssembler tasm(isolate(), AssemblerOptions{}, CodeObjectRequired::kNo,
26                       buffer->CreateView());
27   __ set_root_array_available(false);
28   __ set_abort_hard(true);
29   __ Abort(AbortReason::kNoReason);
30 
31   CodeDesc desc;
32   tasm.GetCode(nullptr, &desc);
33   buffer->MakeExecutable();
34   // We need an isolate here to execute in the simulator.
35   auto f = GeneratedCode<void>::FromBuffer(isolate(), buffer->start());
36   ASSERT_DEATH_IF_SUPPORTED({ f.Call(); }, "abort: no reason");
37 }
38 
TEST_F(TurboAssemblerTest,TestCheck)39 TEST_F(TurboAssemblerTest, TestCheck) {
40   auto buffer = AllocateAssemblerBuffer();
41   TurboAssembler tasm(isolate(), AssemblerOptions{}, CodeObjectRequired::kNo,
42                       buffer->CreateView());
43   __ set_root_array_available(false);
44   __ set_abort_hard(true);
45 
46   // Fail if the first parameter (in {a0}) is 17.
47   __ Check(Condition::ne, AbortReason::kNoReason, a0, Operand(17));
48   __ Ret();
49 
50   CodeDesc desc;
51   tasm.GetCode(nullptr, &desc);
52   buffer->MakeExecutable();
53   // We need an isolate here to execute in the simulator.
54   auto f = GeneratedCode<void, int>::FromBuffer(isolate(), buffer->start());
55 
56   f.Call(0);
57   f.Call(18);
58   ASSERT_DEATH_IF_SUPPORTED({ f.Call(17); }, "abort: no reason");
59 }
60 
61 #undef __
62 
63 }  // namespace internal
64 }  // namespace v8
65