Add branch interception to VIXL simulator (#77)

* Add maybe_unused to runtime call arguments

Currently runtime calls cannot be done if the function to be called
has no parameters because the compiler will give a
"unused-but-set-parameter" warning which is treated as an error. Fix
this by always using the 'arguments' parameter.

Change-Id: I9f4b75ea8b6ae6fe03be33cefa45fa99f5485b7a

* Add branch interception to VIXL simulator

Simulated AARCH64 code, that is not written in using the
macroassembler, can branch (change the simulated PC) to arbitrary
function addresses. This works fine if that function is AARCH64
however if that function is a native (x86_64) C++ function then an
error (likely SIGILL) will be thrown. To handle this case we need to
"intercept" branches to these native (x86_64) C++ functions and
instead either perform a runtime call to the function or provide a
callback to manually handle the particular case.

Add a mechanism to intercept functions as they are branched to
within the VIXL simulator. This means that whenever a function X is
branched to (e.g: bl X) instead, if provided, a callback function Y
is called. If no callback is provided for the interception to
function X then a runtime call will be done on function X.

Branch interception objects consisting of the function to intercept:
X, and an optional callback function Y are stored within the
simulator and checked every unconditional branch to register.

Change-Id: I874a6fa5b8f0581fe930a7a98f762031bdb2f591
diff --git a/examples/aarch64/simulator_interception.cc b/examples/aarch64/simulator_interception.cc
new file mode 100644
index 0000000..1f5d266
--- /dev/null
+++ b/examples/aarch64/simulator_interception.cc
@@ -0,0 +1,159 @@
+// Copyright 2023, VIXL authors
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#include "aarch64/disasm-aarch64.h"
+#include "aarch64/macro-assembler-aarch64.h"
+#include "aarch64/simulator-aarch64.h"
+
+using namespace vixl;
+using namespace vixl::aarch64;
+
+#define __ masm->
+
+enum Result { FAILURE, SUCCESS };
+
+// This will be called via a runtime call.
+extern "C" int example_1() { return SUCCESS; }
+
+// This will never be called, instead it will be intercepted and 'callback'
+// will be called.
+uint32_t example_2() { return FAILURE; }
+
+uint32_t example_3(uint32_t num, float f) {
+  USE(f);
+  return num;
+}
+
+// This will be called instead of example_2.
+uint32_t callback(uint64_t original_target) {
+  USE(original_target);
+  return SUCCESS;
+}
+
+void GenerateInterceptionExamples(MacroAssembler* masm) {
+  // Preserve lr, since the calls will overwrite it.
+  __ Push(xzr, lr);
+
+  // example_1 will be intercepted and called through a runtime call.
+  __ Mov(x16, reinterpret_cast<uint64_t>(example_1));
+  __ Blr(x16);
+  __ Mov(w1, w0);
+
+  // example_2 will be intercepted and callback will be called instead.
+  __ Mov(x16, reinterpret_cast<uint64_t>(example_2));
+  __ Blr(x16);
+  __ Mov(w2, w0);
+
+  // Pass FAILURE as a parameter.
+  __ Mov(x0, FAILURE);
+  __ Fmov(s0, 3.5);
+  // example_3 will be intercepted and lambda callback will be called instead.
+  __ Mov(x16, reinterpret_cast<uint64_t>(example_3));
+  __ Blr(x16);
+  __ Mov(w3, w0);
+
+  // Restore lr and return.
+  __ Pop(lr, xzr);
+  __ Ret();
+}
+
+#ifndef TEST_EXAMPLES
+#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
+
+int main(void) {
+  MacroAssembler masm;
+
+  // Generate the code for the example function.
+  Label call_simulator_interception;
+  masm.Bind(&call_simulator_interception);
+  GenerateInterceptionExamples(&masm);
+  masm.FinalizeCode();
+
+  Instruction* start =
+      masm.GetLabelAddress<Instruction*>(&call_simulator_interception);
+
+  // Disassemble the generated code.
+  PrintDisassembler disassembler(stdout);
+  disassembler.DisassembleBuffer(start, masm.GetSizeOfCodeGenerated());
+
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Register interceptions to the branches, example_1 will be called via a
+  // runtime call and callback will be called instead of example_2.
+  simulator.RegisterBranchInterception(example_1);
+  simulator.RegisterBranchInterception(example_2, callback);
+
+  // Lambda callbacks can be used to arbitrarily modify the simulator.
+  simulator.RegisterBranchInterception(
+      example_3, [&simulator](uint64_t original_target) {
+        USE(original_target);
+        ABI abi;
+
+        uint32_t param1 = simulator.ReadGenericOperand<uint32_t>(
+            abi.GetNextParameterGenericOperand<uint32_t>());
+        float param2 = simulator.ReadGenericOperand<float>(
+            abi.GetNextParameterGenericOperand<float>());
+
+        if (param1 == FAILURE && param2 == 3.5) {
+          simulator.WriteWRegister(0, SUCCESS);
+        } else {
+          simulator.WriteWRegister(0, FAILURE);
+        }
+      });
+
+  simulator.RunFrom(start);
+
+  uint32_t result_1 = simulator.ReadWRegister(1);
+  if (result_1 == SUCCESS) {
+    printf("SUCCESS: example_1 was called via a runtime call.\n");
+  } else {
+    printf("ERROR: example_1 was not called.\n");
+  }
+
+  uint32_t result_2 = simulator.ReadWRegister(2);
+  if (result_2 == SUCCESS) {
+    printf("SUCCESS: callback was called instead of example_2.\n");
+  } else {
+    printf("ERROR: example_2 was called incorrectly.\n");
+  }
+
+  uint32_t result_3 = simulator.ReadWRegister(0);
+  if (result_3 == SUCCESS) {
+    printf("SUCCESS: Lambda callback called instead of example_3.\n");
+  } else {
+    printf("ERROR: example_3 was called instead of the lambda.\n");
+  }
+
+  return 0;
+}
+#else
+// TODO: Support running natively.
+int main(void) { return 0; }
+#endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
+#endif  // TEST_EXAMPLES
diff --git a/src/aarch64/simulator-aarch64.cc b/src/aarch64/simulator-aarch64.cc
index 24092d6..edf5c3a 100644
--- a/src/aarch64/simulator-aarch64.cc
+++ b/src/aarch64/simulator-aarch64.cc
@@ -620,6 +620,8 @@
   // BTI state.
   btype_ = DefaultBType;
   next_btype_ = DefaultBType;
+
+  meta_data_.ResetState();
 }
 
 void Simulator::SetVectorLengthInBits(unsigned vector_length) {
@@ -3726,6 +3728,7 @@
 void Simulator::VisitUnconditionalBranchToRegister(const Instruction* instr) {
   bool authenticate = false;
   bool link = false;
+  bool ret = false;
   uint64_t addr = ReadXRegister(instr->GetRn());
   uint64_t context = 0;
 
@@ -3734,7 +3737,6 @@
       link = true;
       VIXL_FALLTHROUGH();
     case BR:
-    case RET:
       break;
 
     case BLRAAZ:
@@ -3761,6 +3763,9 @@
       authenticate = true;
       addr = ReadXRegister(kLinkRegCode);
       context = ReadXRegister(31, Reg31IsStackPointer);
+      VIXL_FALLTHROUGH();
+    case RET:
+      ret = true;
       break;
     default:
       VIXL_UNREACHABLE();
@@ -3780,6 +3785,22 @@
     }
   }
 
+  if (!ret) {
+    // Check for interceptions to the target address, if one is found, call it.
+    MetaDataDepot::BranchInterceptionAbstract* interception =
+        meta_data_.FindBranchInterception(addr);
+
+    if (interception != nullptr) {
+      // Instead of writing the address of the function to the PC, call the
+      // function's interception directly. We change the address that will be
+      // branched to so that afterwards we continue execution from
+      // the address in the LR. Note: the interception may modify the LR so
+      // store it before calling the interception.
+      addr = ReadRegister<uint64_t>(kLinkRegCode);
+      (*interception)(this);
+    }
+  }
+
   WriteNextBType(GetBTypeFromInstruction(instr));
   WritePc(Instruction::Cast(addr));
 }
diff --git a/src/aarch64/simulator-aarch64.h b/src/aarch64/simulator-aarch64.h
index c5cc894..b3226b7 100644
--- a/src/aarch64/simulator-aarch64.h
+++ b/src/aarch64/simulator-aarch64.h
@@ -64,6 +64,9 @@
 namespace vixl {
 namespace aarch64 {
 
+class Simulator;
+struct RuntimeCallStructHelper;
+
 class SimStack {
  public:
   SimStack() {}
@@ -174,6 +177,12 @@
   return (T)(bits & ~kAddressTagMask);
 }
 
+// A callback function, called when a function has been intercepted if a
+// BranchInterception entry exists in branch_interceptions. The address of
+// the intercepted function is passed to the callback. For usage see
+// BranchInterception.
+using InterceptionCallback = std::function<void(uint64_t)>;
+
 class MetaDataDepot {
  public:
   class MetaDataMTE {
@@ -260,9 +269,76 @@
 
   size_t GetTotalCountMTE() { return metadata_mte_.size(); }
 
+  // A pure virtual struct that allows the templated BranchInterception struct
+  // to be stored. For more information see BranchInterception.
+  struct BranchInterceptionAbstract {
+    virtual ~BranchInterceptionAbstract() {}
+    // Call the callback_ if one exists, otherwise do a RuntimeCall.
+    virtual void operator()(Simulator* simulator) const = 0;
+  };
+
+  // An entry denoting a function to intercept when branched to during
+  // simulator execution. When a function is intercepted the callback will be
+  // called if one exists otherwise the function will be passed to
+  // RuntimeCall.
+  template <typename R, typename... P>
+  struct BranchInterception : public BranchInterceptionAbstract {
+    BranchInterception(R (*function)(P...),
+                       InterceptionCallback callback = nullptr)
+        : function_(function), callback_(callback) {}
+
+    void operator()(Simulator* simulator) const VIXL_OVERRIDE;
+
+   private:
+    // Pointer to the function that will be intercepted.
+    R (*function_)(P...);
+
+    // Function to be called instead of function_
+    InterceptionCallback callback_;
+  };
+
+  // Register a new BranchInterception object. If 'function' is branched to
+  // (e.g: "blr function") in the future; instead, if provided, 'callback' will
+  // be called otherwise a runtime call will be performed on 'function'.
+  //
+  // For example: this can be used to always perform runtime calls on
+  // non-AArch64 functions without using the macroassembler.
+  //
+  // Note: only unconditional branches to registers are currently supported to
+  // be intercepted, e.g: "br"/"blr".
+  //
+  // TODO: support intercepting other branch types.
+  template <typename R, typename... P>
+  void RegisterBranchInterception(R (*function)(P...),
+                                  InterceptionCallback callback = nullptr) {
+    uintptr_t addr = reinterpret_cast<uintptr_t>(function);
+    std::unique_ptr<BranchInterceptionAbstract> intercept =
+        std::make_unique<BranchInterception<R, P...>>(function, callback);
+    branch_interceptions_.insert(std::make_pair(addr, std::move(intercept)));
+  }
+
+  // Search for branch interceptions to the branch_target address; If one is
+  // found return it otherwise return nullptr.
+  BranchInterceptionAbstract* FindBranchInterception(uint64_t branch_target) {
+    // Check for interceptions to the target address, if one is found, call it.
+    auto search = branch_interceptions_.find(branch_target);
+    if (search != branch_interceptions_.end()) {
+      return search->second.get();
+    } else {
+      return nullptr;
+    }
+  }
+
+  void ResetState() { branch_interceptions_.clear(); }
+
  private:
   // Tag recording of each allocated memory in the tag-granule.
   std::unordered_map<uint64_t, class MetaDataMTE> metadata_mte_;
+
+  // Store a map of addresses to be intercepted and their corresponding branch
+  // interception object, see 'BranchInterception'.
+  std::unordered_map<uintptr_t, std::unique_ptr<BranchInterceptionAbstract>>
+      branch_interceptions_;
 };
 
 
@@ -2855,6 +2931,7 @@
   R DoRuntimeCall(R (*function)(P...),
                   std::tuple<P...> arguments,
                   local_index_sequence<I...>) {
+    USE(arguments);
     return function(std::get<I>(arguments)...);
   }
 
@@ -3026,6 +3103,18 @@
   // excluded from the selection.
   uint64_t GenerateRandomTag(uint16_t exclude = 0);
 
+  // Register a new BranchInterception object. If 'function' is branched to
+  // (e.g: "bl function") in the future; instead, if provided, 'callback' will
+  // be called otherwise a runtime call will be performed on 'function'.
+  //
+  // For example: this can be used to always perform runtime calls on
+  // non-AArch64 functions without using the macroassembler.
+  template <typename R, typename... P>
+  void RegisterBranchInterception(R (*function)(P...),
+                                  InterceptionCallback callback = nullptr) {
+    meta_data_.RegisterBranchInterception(*function, callback);
+  }
+
  protected:
   const char* clr_normal;
   const char* clr_flag_name;
@@ -5140,8 +5229,8 @@
   // A configurable size of SVE vector registers.
   unsigned vector_length_;
 
-  // Representation of memory attribute such as MTE tagging and BTI page
-  // protection.
+  // Representation of memory attributes such as MTE tagging and BTI page
+  // protection in addition to branch interceptions.
   MetaDataDepot meta_data_;
 };
 
@@ -5153,6 +5242,17 @@
     : Simulator::emulated_index_sequence<I...> {};
 #endif
 
+template <typename R, typename... P>
+void MetaDataDepot::BranchInterception<R, P...>::operator()(
+    Simulator* simulator) const {
+  if (callback_ == nullptr) {
+    Simulator::RuntimeCallStructHelper<R, P...>::
+        Wrapper(simulator, reinterpret_cast<uint64_t>(function_));
+  } else {
+    callback_(reinterpret_cast<uint64_t>(function_));
+  }
+}
+
 }  // namespace aarch64
 }  // namespace vixl
 
diff --git a/test/aarch64/test-assembler-aarch64.cc b/test/aarch64/test-assembler-aarch64.cc
index 934c109..3675eb9 100644
--- a/test/aarch64/test-assembler-aarch64.cc
+++ b/test/aarch64/test-assembler-aarch64.cc
@@ -13666,6 +13666,8 @@
 
 void runtime_call_store_at_address(int64_t* address) { *address = 0xf00d; }
 
+int32_t runtime_call_no_args() { return 1; }
+
 enum RuntimeCallTestEnum { Enum0 };
 
 RuntimeCallTestEnum runtime_call_enum(RuntimeCallTestEnum e) { return e; }
@@ -13786,6 +13788,10 @@
   __ Mov(x0, reinterpret_cast<uint64_t>(&value));
   __ CallRuntime(runtime_call_store_at_address);
 
+  __ Mov(w0, 0);
+  __ CallRuntime(runtime_call_no_args);
+  __ Mov(w25, w0);
+
   END();
 
 #if defined(VIXL_HAS_SIMULATED_RUNTIME_CALL_SUPPORT) || \
@@ -13801,9 +13807,79 @@
     ASSERT_EQUAL_64(0, x22);
     ASSERT_EQUAL_32(124, w23);
     ASSERT_EQUAL_64(0, x24);
+    ASSERT_EQUAL_32(1, w25);
   }
 #endif  // #if defined(VIXL_HAS_SIMULATED_RUNTIME_CALL_SUPPORT) || ...
 }
+
+#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
+void void_func() {}
+uint32_t uint32_func() { return 2; }
+void void_param_func(uint32_t x) { USE(x); }
+uint32_t uint32_param_func(uint32_t x) { return ++x; }
+
+void void_placeholder() {}
+uint32_t uint32_placeholder() { return 4; }
+void void_param_placeholder(uint32_t x) { USE(x); }
+uint32_t uint32_param_placeholder(uint32_t x) { return ++x; }
+
+#define DO_TEST_BRANCH_INTERCEPTION(func)        \
+  __ Mov(x16, reinterpret_cast<uint64_t>(func)); \
+  __ Blr(x16);
+
+TEST(branch_interception) {
+  SETUP();
+  START();
+
+  // Test default branch interception, i.e: do a runtime call to the function.
+  DO_TEST_BRANCH_INTERCEPTION(void_func);
+  DO_TEST_BRANCH_INTERCEPTION(uint32_func);
+  __ Mov(w20, w0);
+  DO_TEST_BRANCH_INTERCEPTION(void_param_func);
+  __ Mov(w0, 2);
+  DO_TEST_BRANCH_INTERCEPTION(uint32_param_func);
+  __ Mov(w21, w0);
+
+  // Test interceptions with callbacks.
+  DO_TEST_BRANCH_INTERCEPTION(void_placeholder);
+  __ Mov(w22, w0);
+  DO_TEST_BRANCH_INTERCEPTION(uint32_placeholder);
+  __ Mov(w23, w0);
+  __ Mov(w0, 4);
+  DO_TEST_BRANCH_INTERCEPTION(uint32_placeholder);
+  __ Mov(w24, w0);
+  DO_TEST_BRANCH_INTERCEPTION(uint32_placeholder);
+  __ Mov(w25, w0);
+
+  END();
+
+  simulator.RegisterBranchInterception(void_func);
+  simulator.RegisterBranchInterception(uint32_func);
+  simulator.RegisterBranchInterception(void_param_func);
+  simulator.RegisterBranchInterception(uint32_param_func);
+
+  auto callback = [&simulator](uint64_t original_target) {
+    USE(original_target);
+    simulator.WriteWRegister(0, 1);
+  };
+
+  simulator.RegisterBranchInterception(void_placeholder, callback);
+  simulator.RegisterBranchInterception(uint32_placeholder, callback);
+  simulator.RegisterBranchInterception(void_param_placeholder, callback);
+  simulator.RegisterBranchInterception(uint32_param_placeholder, callback);
+
+  if (CAN_RUN()) {
+    RUN();
+
+    ASSERT_EQUAL_32(2, w20);
+    ASSERT_EQUAL_32(3, w21);
+    ASSERT_EQUAL_32(1, w22);
+    ASSERT_EQUAL_32(1, w23);
+    ASSERT_EQUAL_32(1, w24);
+    ASSERT_EQUAL_32(1, w25);
+  }
+}
+#endif  // #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
 #endif  // #ifdef VIXL_HAS_MACROASSEMBLER_RUNTIME_CALL_SUPPORT
 
 
diff --git a/tools/code_coverage.log b/tools/code_coverage.log
index 1362b07..5a16d69 100644
--- a/tools/code_coverage.log
+++ b/tools/code_coverage.log
@@ -19,3 +19,4 @@
 1677171445 82.78% 97.56% 94.81%
 1681814646 82.90% 97.57% 94.87%
 1686666000 82.90% 97.57% 94.87%
+1693487542 82.91% 97.57% 94.87%