RSScreenFunctionsPass.cpp revision 5db508c73e6177eb306bac4725616b7c001587c4
1/* 2 * Copyright 2014, 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 "bcc/Renderscript/RSTransforms.h" 18#include "bcc/Support/Log.h" 19#include "RSStubsWhiteList.h" 20 21#include <cstdlib> 22 23#include <llvm/IR/Instructions.h> 24#include <llvm/IR/Module.h> 25#include <llvm/IR/Function.h> 26#include <llvm/Pass.h> 27 28namespace { // anonymous namespace 29 30// Create a Module pass that screens all the global functions in the 31// module and check if any disallowed external function is accessible 32// and potentially callable. 33class RSScreenFunctionsPass : public llvm::ModulePass { 34private: 35 static char ID; 36 37 std::vector<std::string> &whiteList = stubList; 38 39 bool isPresent(std::vector<std::string> &list, std::string name) { 40 auto lower = std::lower_bound(list.begin(), 41 list.end(), 42 name); 43 44 if (lower != list.end() && name.compare(*lower) == 0) 45 return true; 46 return false; 47 } 48 49 bool isLegal(llvm::Function &F) { 50 // A global function symbol is legal if 51 // a. it has a body, i.e. is not empty or 52 // b. its name starts with "llvm." or 53 // c. it is present in the whitelist 54 55 if (!F.empty()) 56 return true; 57 58 llvm::StringRef FName = F.getName(); 59 if (FName.startswith("llvm.")) 60 return true; 61 62 if (isPresent(whiteList, FName.str())) 63 return true; 64 65 return false; 66 } 67 68public: 69 RSScreenFunctionsPass() 70 : ModulePass (ID) { 71 std::sort(whiteList.begin(), whiteList.end()); 72 } 73 74 bool runOnModule(llvm::Module &M) override { 75 bool failed = false; 76 77 auto &FunctionList(M.getFunctionList()); 78 for(auto &F: FunctionList) { 79 if (!isLegal(F)) { 80 ALOGE("Call to function %s from RenderScript is disallowed\n", 81 F.getName().str().c_str()); 82 failed = true; 83 } 84 } 85 86 if (failed) { 87 llvm::report_fatal_error("Use of undefined external function"); 88 } 89 90 return false; 91 } 92 93}; 94 95} 96 97char RSScreenFunctionsPass::ID = 0; 98 99namespace bcc { 100 101llvm::ModulePass * 102createRSScreenFunctionsPass() { 103 return new RSScreenFunctionsPass(); 104} 105 106} 107