Skip to content

Commit

Permalink
Reserve more mrefs
Browse files Browse the repository at this point in the history
Summary: Refactor the interdex plugin. Reserve more mrefs.

Reviewed By: NTillmann

Differential Revision: D48276313

fbshipit-source-id: 96a97dc023c33c8534e4d46a6f9e993c202a7617
  • Loading branch information
agampe authored and facebook-github-bot committed Aug 13, 2023
1 parent d5a8b66 commit eea0ac0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
65 changes: 37 additions & 28 deletions opt/instrument/Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ constexpr const char* METHOD_REPLACEMENT = "methods_replacement";

class InstrumentInterDexPlugin : public interdex::InterDexPassPlugin {
public:
explicit InstrumentInterDexPlugin(size_t max_analysis_methods)
: m_max_analysis_methods(max_analysis_methods) {}
InstrumentInterDexPlugin(size_t frefs, size_t trefs, size_t mrefs)
: m_frefs(frefs), m_trefs(trefs), m_mrefs(mrefs) {}

ReserveRefsInfo reserve_refs() override {
// We may introduce a new field. We introduce a type reference to the
// analysis class in each dex. We will introduce more method refs from
// analysis methods.
return ReserveRefsInfo(/* frefs */ 1,
/* trefs */ 1,
/* mrefs */ m_max_analysis_methods);
return ReserveRefsInfo(m_frefs, m_trefs, m_mrefs);
}

private:
const size_t m_max_analysis_methods;
const size_t m_frefs;
const size_t m_trefs;
const size_t m_mrefs;
};

// For example, say that "Lcom/facebook/debug/" is in the set. We match either
Expand Down Expand Up @@ -433,6 +430,9 @@ void count_source_block_chain_length(DexStoresVector& stores, PassManager& pm) {

} // namespace

InstrumentPass::InstrumentPass() : Pass("InstrumentPass") {}
InstrumentPass::~InstrumentPass() {}

// Find a sequence of opcode that creates a static array. Patch the array size.
void InstrumentPass::patch_array_size(DexClass* analysis_cls,
const std::string_view array_name,
Expand Down Expand Up @@ -557,25 +557,7 @@ void InstrumentPass::bind_config() {
bind("inline_onNonLoopBlockHit", false, m_options.inline_onNonLoopBlockHit);
bind("apply_CSE_CopyProp", false, m_options.apply_CSE_CopyProp);

size_t max_analysis_methods;
if (m_options.instrumentation_strategy == SIMPLE_METHOD_TRACING) {
max_analysis_methods = m_options.num_shards;
} else if (m_options.instrumentation_strategy == BASIC_BLOCK_TRACING) {
// Our current DynamicAnalysis has 7 onMethodExits and 1 onMethodBegin.
max_analysis_methods = 8;
} else {
max_analysis_methods = 1;
}

after_configuration([this, max_analysis_methods] {
// Make a small room for additional method refs during InterDex.
interdex::InterDexRegistry* registry =
static_cast<interdex::InterDexRegistry*>(
PluginRegistry::get().pass_registry(interdex::INTERDEX_PASS_NAME));
registry->register_plugin(
"INSTRUMENT_PASS_PLUGIN", [max_analysis_methods]() {
return new InstrumentInterDexPlugin(max_analysis_methods);
});
after_configuration([this] {
// Currently we only support instance call to static call.
for (auto& pair : m_options.methods_replacement) {
always_assert(!is_static(pair.first));
Expand Down Expand Up @@ -670,6 +652,33 @@ void InstrumentPass::eval_pass(DexStoresVector& stores,

set_no_opt_flag_on_analysis_methods(true, m_options.analysis_class_name,
m_options.analysis_method_names);

// Make a small room for additional method refs during InterDex. We may
// introduce a new field. We introduce a type reference to the analysis class
// in each dex. We will introduce more method refs from analysis methods.

interdex::InterDexRegistry* registry =
static_cast<interdex::InterDexRegistry*>(
PluginRegistry::get().pass_registry(interdex::INTERDEX_PASS_NAME));

size_t max_analysis_methods;
if (m_options.instrumentation_strategy == SIMPLE_METHOD_TRACING) {
max_analysis_methods = m_options.num_shards;
} else if (m_options.instrumentation_strategy == BASIC_BLOCK_TRACING) {
// TODO: Derive this from the source.
// Our current DynamicAnalysis has 2 * 7 onMethodExits and 1 onMethodBegin.
max_analysis_methods = 15;
} else {
max_analysis_methods = 1;
}

m_plugin = std::unique_ptr<interdex::InterDexPassPlugin>(
new InstrumentInterDexPlugin(1, 1, max_analysis_methods));

registry->register_plugin("INSTRUMENT_PASS_PLUGIN", [this]() {
return new InstrumentInterDexPlugin(
*(InstrumentInterDexPlugin*)m_plugin.get());
});
}

// Check for inclusion in allow/block lists of methods/classes. It supports:
Expand Down
9 changes: 8 additions & 1 deletion opt/instrument/Instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@

#pragma once

#include <memory>
#include <unordered_map>
#include <unordered_set>

#include "Pass.h"

class DexMethod;

namespace interdex {
class InterDexPassPlugin;
} // namespace interdex

namespace instrument {

enum class ProfileTypeFlags {
Expand All @@ -29,7 +34,8 @@ enum class ProfileTypeFlags {

class InstrumentPass : public Pass {
public:
InstrumentPass() : Pass("InstrumentPass") {}
InstrumentPass();
~InstrumentPass();

redex_properties::PropertyInteractions get_property_interactions()
const override {
Expand Down Expand Up @@ -101,6 +107,7 @@ class InstrumentPass : public Pass {

private:
Options m_options;
std::unique_ptr<interdex::InterDexPassPlugin> m_plugin;
};

} // namespace instrument

0 comments on commit eea0ac0

Please sign in to comment.