xref: /aosp_15_r20/art/test/983-source-transform-verify/source_transform_art.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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   ArtDexFileLoader dex_file_loader(class_data, class_data_len, "fake_location.dex");
45   std::string error;
46   std::unique_ptr<const DexFile> dex(dex_file_loader.Open(/*location_checksum*/ 0,
47                                                           /*oat_dex_file*/ nullptr,
48                                                           /*verify*/ true,
49                                                           /*verify_checksum*/ true,
50                                                           &error));
51   CHECK(dex.get() != nullptr) << "Failed to verify dex: " << error;
52 
53   for (ClassAccessor accessor : dex->GetClasses()) {
54     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
55       for (const DexInstructionPcPair& pair : method.GetInstructions()) {
56         const Instruction& inst = pair.Inst();
57         int forbidden_flags = Instruction::kVerifyError;
58         if ((inst.GetVerifyExtraFlags() & forbidden_flags) != 0) {
59           LOG(FATAL) << "Unexpected instruction found in " << dex->PrettyMethod(method.GetIndex())
60                      << " [Dex PC: 0x" << std::hex << pair.DexPc() << std::dec << "] : "
61                      << inst.DumpString(dex.get()) << std::endl;
62         }
63       }
64     }
65   }
66 }
67 
68 }  // namespace Test983SourceTransformVerify
69 }  // namespace art
70