xref: /aosp_15_r20/art/tools/fuzzer/libart_verify_dex_fuzzer.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2023 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 "dex/dex_file_verifier.h"
18 #include "dex/standard_dex_file.h"
19 
LLVMFuzzerInitialize(int * argc,char *** argv)20 extern "C" int LLVMFuzzerInitialize([[maybe_unused]] int* argc, [[maybe_unused]] char*** argv) {
21   // Set logging to error and above to avoid warnings about unexpected checksums.
22   android::base::SetMinimumLogSeverity(android::base::ERROR);
23   return 0;
24 }
25 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)26 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
27   // Do not verify the checksum as we only care about the DEX file contents,
28   // and know that the checksum would probably be erroneous (i.e. random).
29   constexpr bool kVerify = false;
30 
31   auto container = std::make_shared<art::MemoryDexFileContainer>(data, size);
32   art::StandardDexFile dex_file(data,
33                                 /*location=*/"fuzz.dex",
34                                 /*location_checksum=*/0,
35                                 /*oat_dex_file=*/nullptr,
36                                 container);
37 
38   std::string error_msg;
39   art::dex::Verify(&dex_file, dex_file.GetLocation().c_str(), kVerify, &error_msg);
40 
41   return 0;
42 }
43