1 /*
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"
27 #include "dex/class_accessor-inl.h"
28 #include "dex/dex_file.h"
29 #include "dex/dex_file_loader.h"
30 #include "dex/dex_instruction.h"
31 
32 namespace art {
33 namespace Test983SourceTransformVerify {
34 
35 // The hook we are using.
VerifyClassData(jint class_data_len,const unsigned char * class_data)36 void 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;
55 
56   for (ClassAccessor accessor : dex->GetClasses()) {
57     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
58       for (const DexInstructionPcPair& pair : method.GetInstructions()) {
59         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) {
63           LOG(FATAL) << "Unexpected instruction found in " << dex->PrettyMethod(method.GetIndex())
64                      << " [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
74