1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include "berberis/base/bit_util.h"
20 #include "berberis/guest_abi/function_wrappers.h"
21 #include "berberis/guest_state/guest_state.h"
22 
23 namespace berberis {
24 
25 namespace {
26 
TEST(TrampolineFuncGenerator,IntRes)27 TEST(TrampolineFuncGenerator, IntRes) {
28   struct Callee {
29     static int foo() { return 1; }
30   };
31 
32   TrampolineFunc func = GetTrampolineFunc<int(void)>();
33 
34   ThreadState state{};
35 
36   func(reinterpret_cast<void*>(Callee::foo), &state);
37 
38   EXPECT_EQ(GetXReg<A0>(state.cpu), 1U);
39 }
40 
TEST(TrampolineFuncGenerator,FloatArgs_lp64)41 TEST(TrampolineFuncGenerator, FloatArgs_lp64) {
42   struct Callee {
43     static void foo(void* p, float x, float y) {
44       EXPECT_EQ(p, nullptr);
45       EXPECT_EQ(x, 0.5f);
46       EXPECT_EQ(y, 0.75f);
47     }
48   };
49 
50   TrampolineFunc func = GetTrampolineFunc<void(void*, float, float), GuestAbi::kLp64>();
51 
52   ThreadState state{};
53 
54   SetXReg<A0>(state.cpu, 0U);
55   SetXReg<A1>(state.cpu, bit_cast<uint32_t>(0.5f));
56   SetXReg<A2>(state.cpu, bit_cast<uint32_t>(0.75f));
57 
58   EXPECT_NO_FATAL_FAILURE(func(reinterpret_cast<void*>(Callee::foo), &state));
59 }
60 
TEST(TrampolineFuncGenerator,FloatArgs_lp64d)61 TEST(TrampolineFuncGenerator, FloatArgs_lp64d) {
62   struct Callee {
63     static void foo(void* p, float x, float y) {
64       EXPECT_EQ(p, nullptr);
65       EXPECT_EQ(x, 0.5f);
66       EXPECT_EQ(y, 0.75f);
67     }
68   };
69 
70   TrampolineFunc func = GetTrampolineFunc<void(void*, float, float), GuestAbi::kLp64d>();
71 
72   ThreadState state{};
73 
74   SetXReg<A0>(state.cpu, 0U);
75   SetFReg<FA0>(state.cpu, bit_cast<uint32_t>(0.5f));
76   SetFReg<FA1>(state.cpu, bit_cast<uint32_t>(0.75f));
77 
78   EXPECT_NO_FATAL_FAILURE(func(reinterpret_cast<void*>(Callee::foo), &state));
79 }
80 
81 }  // namespace
82 
83 }  // namespace berberis
84