• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "fuzz_tester/FuzzerWrapper.h"
18 
19 #include <dlfcn.h>
20 
21 #include <iostream>
22 #include <string>
23 
24 #include "component_loader/DllLoader.h"
25 #include "fuzz_tester/FuzzerBase.h"
26 #include "utils/InterfaceSpecUtil.h"
27 
28 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
29 
30 using namespace std;
31 
32 namespace android {
33 namespace vts {
34 
FuzzerWrapper()35 FuzzerWrapper::FuzzerWrapper()
36     : function_name_prefix_chars_(NULL), fuzzer_base_(NULL) {}
37 
LoadInterfaceSpecificationLibrary(const char * spec_dll_path)38 bool FuzzerWrapper::LoadInterfaceSpecificationLibrary(
39     const char* spec_dll_path) {
40   if (!spec_dll_path) {
41     cerr << __func__ << ":" << __LINE__ << " arg is NULL" << endl;
42     return false;
43   }
44   if (spec_dll_path_.size() > 0 &&
45       !strcmp(spec_dll_path, spec_dll_path_.c_str())) {
46     return true;
47   }
48   spec_dll_path_ = spec_dll_path;
49   if (!dll_loader_.Load(spec_dll_path_.c_str(), false)) return false;
50   cout << "DLL loaded " << spec_dll_path_ << endl;
51   return true;
52 }
53 
GetFuzzer(const vts::ComponentSpecificationMessage & message)54 FuzzerBase* FuzzerWrapper::GetFuzzer(
55     const vts::ComponentSpecificationMessage& message) {
56   cout << __func__ << ":" << __LINE__ << " entry" << endl;
57   if (spec_dll_path_.size() == 0) {
58     cerr << __func__ << ": spec_dll_path_ not set" << endl;
59     return NULL;
60   }
61 
62   string function_name_prefix = GetFunctionNamePrefix(message);
63   const char* function_name_prefix_chars = function_name_prefix.c_str();
64   cout << __func__ << ": function name '" << function_name_prefix_chars
65        << "'" << endl;
66   if (function_name_prefix_chars_ && function_name_prefix_chars &&
67       !strcmp(function_name_prefix_chars, function_name_prefix_chars_)) {
68     return fuzzer_base_;
69   }
70 
71   loader_function func =
72       dll_loader_.GetLoaderFunction(function_name_prefix_chars);
73   if (!func) {
74     cerr << __func__ << ": function not found." << endl;
75     return NULL;
76   }
77   cout << __func__ << ": function found; trying to call." << endl;
78   fuzzer_base_ = func();
79   function_name_prefix_chars_ =
80       (char*)malloc(strlen(function_name_prefix_chars) + 1);
81   strcpy(function_name_prefix_chars_, function_name_prefix_chars);
82   return fuzzer_base_;
83 }
84 
85 }  // namespace vts
86 }  // namespace android
87