Get a basic modification of dex file working

This allows the modification of a single classes methods through
transformation. One must ensure that the provided dex file only
contains one function and does not add or remove any methods or fields
and does not change the inheritance hierarchy in any way. The provided
dex file must verify and there must be no frames of the old code
present on any thread. These constraints are not checked or verified.
Breaking them might cause undefined behavior in all parts of the
runtime. Code that has been inlined in any way might not be replaced.
This feature is extremely experimental.

Bug: 31455788
Test: ./test/run-test --host 902-hello-transformation

Change-Id: I35133d24f6cdafdd2af9dc9863e15ba8493fc50e
diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc
index 70b7f87..409fbba 100644
--- a/runtime/dex_file.cc
+++ b/runtime/dex_file.cc
@@ -564,6 +564,34 @@
   return nullptr;
 }
 
+uint32_t DexFile::FindCodeItemOffset(const DexFile::ClassDef& class_def,
+                                     uint32_t method_idx) const {
+  const uint8_t* class_data = GetClassData(class_def);
+  CHECK(class_data != nullptr);
+  ClassDataItemIterator it(*this, class_data);
+  // Skip fields
+  while (it.HasNextStaticField()) {
+    it.Next();
+  }
+  while (it.HasNextInstanceField()) {
+    it.Next();
+  }
+  while (it.HasNextDirectMethod()) {
+    if (it.GetMemberIndex() == method_idx) {
+      return it.GetMethodCodeItemOffset();
+    }
+    it.Next();
+  }
+  while (it.HasNextVirtualMethod()) {
+    if (it.GetMemberIndex() == method_idx) {
+      return it.GetMethodCodeItemOffset();
+    }
+    it.Next();
+  }
+  LOG(FATAL) << "Unable to find method " << method_idx;
+  UNREACHABLE();
+}
+
 const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
                                              const DexFile::StringId& name,
                                              const DexFile::TypeId& type) const {