blob: fbf25b849e9b8c217010c403f71a04a39ea2bcca [file] [log] [blame]
Andreas Gampe59484b92018-02-14 14:00:46 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "source_transform.h"
18
19#include <inttypes.h>
20
21#include <memory>
22
23#include <android-base/logging.h>
24
25#include "dex/code_item_accessors-inl.h"
26#include "dex/art_dex_file_loader.h"
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -070027#include "dex/class_accessor-inl.h"
Andreas Gampe59484b92018-02-14 14:00:46 -080028#include "dex/dex_file.h"
29#include "dex/dex_file_loader.h"
30#include "dex/dex_instruction.h"
31
32namespace art {
33namespace Test983SourceTransformVerify {
34
35// The hook we are using.
36void VerifyClassData(jint class_data_len, const unsigned char* class_data) {
37 // Due to b/72402467 the class_data_len might just be an estimate.
38 CHECK_GE(static_cast<size_t>(class_data_len), sizeof(DexFile::Header));
39 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(class_data);
40 uint32_t header_file_size = header->file_size_;
41 CHECK_LE(static_cast<jint>(header_file_size), class_data_len);
42 class_data_len = static_cast<jint>(header_file_size);
43
44 const ArtDexFileLoader dex_file_loader;
45 std::string error;
46 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(class_data,
47 class_data_len,
48 "fake_location.dex",
49 /*location_checksum*/ 0,
50 /*oat_dex_file*/ nullptr,
51 /*verify*/ true,
52 /*verify_checksum*/ true,
53 &error));
54 CHECK(dex.get() != nullptr) << "Failed to verify dex: " << error;
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -070055
56 for (ClassAccessor accessor : dex->GetClasses()) {
57 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
58 for (const DexInstructionPcPair& pair : method.GetInstructions()) {
Andreas Gampe59484b92018-02-14 14:00:46 -080059 const Instruction& inst = pair.Inst();
60 int forbidden_flags = (Instruction::kVerifyError | Instruction::kVerifyRuntimeOnly);
61 if (inst.Opcode() == Instruction::RETURN_VOID_NO_BARRIER ||
62 (inst.GetVerifyExtraFlags() & forbidden_flags) != 0) {
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -070063 LOG(FATAL) << "Unexpected instruction found in " << dex->PrettyMethod(method.GetIndex())
Andreas Gampe59484b92018-02-14 14:00:46 -080064 << " [Dex PC: 0x" << std::hex << pair.DexPc() << std::dec << "] : "
65 << inst.DumpString(dex.get()) << std::endl;
66 }
67 }
68 }
69 }
70}
71
72} // namespace Test983SourceTransformVerify
73} // namespace art