#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "components/zucchini/disassembler_dex.h"
#include <stddef.h>
#include <stdlib.h>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <optional>
#include <set>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/stringprintf.h"
#include "components/zucchini/buffer_source.h"
#include "components/zucchini/buffer_view.h"
#include "components/zucchini/io_utils.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
namespace zucchini {
namespace {
constexpr offset_t kDexSentinelOffset = …;
constexpr offset_t kDexSentinelIndexAsOffset = …;
static_assert …;
constexpr int kInstrUnitSize = …;
bool Is32BitAligned(offset_t offset) { … }
size_t GetItemBaseSize(uint16_t type_item_code) { … }
class CodeItemParser { … };
class InstructionParser { … };
class InstructionReferenceReader : public ReferenceReader { … };
class ItemReferenceReader : public ReferenceReader { … };
bool ParseItemOffsets(ConstBufferView image,
const dex::MapItem& map_item,
size_t item_width,
std::vector<offset_t>* item_offsets) { … }
bool ParseAnnotationsDirectoryItems(
ConstBufferView image,
const dex::MapItem& annotations_directory_map_item,
std::vector<offset_t>* annotations_directory_item_offsets,
std::vector<offset_t>* field_annotation_offsets,
std::vector<offset_t>* method_annotation_offsets,
std::vector<offset_t>* parameter_annotation_offsets) { … }
class CachedItemListReferenceReader : public ReferenceReader { … };
template <typename INT>
static offset_t ReadTargetIndex(ConstBufferView image,
const dex::MapItem& target_map_item,
size_t target_item_size,
offset_t location) { … }
static offset_t ReadMethodHandleFieldOrMethodId(
ConstBufferView image,
const dex::MapItem& target_map_item,
size_t target_item_size,
dex::MethodHandleType min_type,
dex::MethodHandleType max_type,
offset_t location) { … }
static offset_t ReadTargetOffset32(ConstBufferView image, offset_t location) { … }
class ReferenceWriterAdaptor : public ReferenceWriter { … };
template <typename INT>
static void WriteTargetIndex(const dex::MapItem& target_map_item,
size_t target_item_size,
Reference ref,
MutableBufferView image) { … }
struct ReadDexHeaderResults { … };
bool ReadDexHeader(ConstBufferView image, ReadDexHeaderResults* opt_results) { … }
}
DisassemblerDex::DisassemblerDex() : … { … }
DisassemblerDex::~DisassemblerDex() = default;
bool DisassemblerDex::QuickDetect(ConstBufferView image) { … }
ExecutableType DisassemblerDex::GetExeType() const { … }
std::string DisassemblerDex::GetExeTypeString() const { … }
std::vector<ReferenceGroup> DisassemblerDex::MakeReferenceGroups() const { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadStringIdToStringData(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadTypeIdToDescriptorStringId32(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadProtoIdToShortyStringId32(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadProtoIdToReturnTypeId32(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadProtoIdToParametersTypeList(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadFieldToClassTypeId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadFieldToTypeId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadFieldToNameStringId32(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadMethodIdToClassTypeId16(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadMethodIdToProtoId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadMethodIdToNameStringId32(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadClassDefToClassTypeId32(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadClassDefToSuperClassTypeId32(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadClassDefToInterfacesTypeList(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadClassDefToSourceFileStringId32(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadClassDefToAnnotationDirectory(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadClassDefToClassData(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadClassDefToStaticValuesEncodedArray(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadCallSiteIdToCallSite32(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadMethodHandleToFieldId16(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadMethodHandleToMethodId16(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadTypeListToTypeId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationSetToAnnotation(offset_t lo, offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationSetRefListToAnnotationSet(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToClassAnnotationSet(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToFieldId32(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToFieldAnnotationSet(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToMethodId32(offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToMethodAnnotationSet(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToParameterMethodId32(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader>
DisassemblerDex::MakeReadAnnotationsDirectoryToParameterAnnotationSetRef(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToStringId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToStringId32(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToTypeId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToProtoId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToCallSiteId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToMethodHandle16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToFieldId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToMethodId16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToRelCode8(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToRelCode16(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceReader> DisassemblerDex::MakeReadCodeToRelCode32(
offset_t lo,
offset_t hi) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteStringId16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteStringId32(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteTypeId16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteTypeId32(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteProtoId16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteFieldId16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteFieldId32(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteMethodId16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteMethodId32(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteCallSiteId16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteMethodHandle16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteRelCode8(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteRelCode16(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteRelCode32(
MutableBufferView image) { … }
std::unique_ptr<ReferenceWriter> DisassemblerDex::MakeWriteAbs32(
MutableBufferView image) { … }
bool DisassemblerDex::Parse(ConstBufferView image) { … }
bool DisassemblerDex::ParseHeader() { … }
}