• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "examples.h"
28 
29 #include "aarch64/disasm-aarch64.h"
30 #include "aarch64/macro-assembler-aarch64.h"
31 #include "aarch64/simulator-aarch64.h"
32 
33 using namespace vixl;
34 using namespace vixl::aarch64;
35 
36 #define __ masm->
37 
38 enum Result { FAILURE, SUCCESS };
39 
40 // This will be called via a runtime call.
example_1()41 extern "C" int example_1() { return SUCCESS; }
42 
43 // This will never be called, instead it will be intercepted and 'callback'
44 // will be called.
example_2()45 uint32_t example_2() { return FAILURE; }
46 
example_3(uint32_t num,float f)47 uint32_t example_3(uint32_t num, float f) {
48   USE(f);
49   return num;
50 }
51 
52 // This will be called instead of example_2.
callback(uint64_t original_target)53 uint32_t callback(uint64_t original_target) {
54   USE(original_target);
55   return SUCCESS;
56 }
57 
GenerateInterceptionExamples(MacroAssembler * masm)58 void GenerateInterceptionExamples(MacroAssembler* masm) {
59   // Preserve lr, since the calls will overwrite it.
60   __ Push(xzr, lr);
61 
62   // example_1 will be intercepted and called through a runtime call.
63   __ Mov(x16, reinterpret_cast<uint64_t>(example_1));
64   __ Blr(x16);
65   __ Mov(w1, w0);
66 
67   // example_2 will be intercepted and callback will be called instead.
68   __ Mov(x16, reinterpret_cast<uint64_t>(example_2));
69   __ Blr(x16);
70   __ Mov(w2, w0);
71 
72   // Pass FAILURE as a parameter.
73   __ Mov(x0, FAILURE);
74   __ Fmov(s0, 3.5);
75   // example_3 will be intercepted and lambda callback will be called instead.
76   __ Mov(x16, reinterpret_cast<uint64_t>(example_3));
77   __ Blr(x16);
78   __ Mov(w3, w0);
79 
80   // Restore lr and return.
81   __ Pop(lr, xzr);
82   __ Ret();
83 }
84 
85 #ifndef TEST_EXAMPLES
86 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
87 
main(void)88 int main(void) {
89   MacroAssembler masm;
90 
91   // Generate the code for the example function.
92   Label call_simulator_interception;
93   masm.Bind(&call_simulator_interception);
94   GenerateInterceptionExamples(&masm);
95   masm.FinalizeCode();
96 
97   Instruction* start =
98       masm.GetLabelAddress<Instruction*>(&call_simulator_interception);
99 
100   // Disassemble the generated code.
101   PrintDisassembler disassembler(stdout);
102   disassembler.DisassembleBuffer(start, masm.GetSizeOfCodeGenerated());
103 
104   Decoder decoder;
105   Simulator simulator(&decoder);
106 
107   // Register interceptions to the branches, example_1 will be called via a
108   // runtime call and callback will be called instead of example_2.
109   simulator.RegisterBranchInterception(example_1);
110   simulator.RegisterBranchInterception(example_2, callback);
111 
112   // Lambda callbacks can be used to arbitrarily modify the simulator.
113   simulator.RegisterBranchInterception(
114       example_3, [&simulator](uint64_t original_target) {
115         USE(original_target);
116         ABI abi;
117 
118         uint32_t param1 = simulator.ReadGenericOperand<uint32_t>(
119             abi.GetNextParameterGenericOperand<uint32_t>());
120         float param2 = simulator.ReadGenericOperand<float>(
121             abi.GetNextParameterGenericOperand<float>());
122 
123         if (param1 == FAILURE && param2 == 3.5) {
124           simulator.WriteWRegister(0, SUCCESS);
125         } else {
126           simulator.WriteWRegister(0, FAILURE);
127         }
128       });
129 
130   simulator.RunFrom(start);
131 
132   uint32_t result_1 = simulator.ReadWRegister(1);
133   if (result_1 == SUCCESS) {
134     printf("SUCCESS: example_1 was called via a runtime call.\n");
135   } else {
136     printf("ERROR: example_1 was not called.\n");
137   }
138 
139   uint32_t result_2 = simulator.ReadWRegister(2);
140   if (result_2 == SUCCESS) {
141     printf("SUCCESS: callback was called instead of example_2.\n");
142   } else {
143     printf("ERROR: example_2 was called incorrectly.\n");
144   }
145 
146   uint32_t result_3 = simulator.ReadWRegister(0);
147   if (result_3 == SUCCESS) {
148     printf("SUCCESS: Lambda callback called instead of example_3.\n");
149   } else {
150     printf("ERROR: example_3 was called instead of the lambda.\n");
151   }
152 
153   return 0;
154 }
155 #else
156 // TODO: Support running natively.
main(void)157 int main(void) { return 0; }
158 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
159 #endif  // TEST_EXAMPLES
160