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 
28 namespace { // 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.
33 class RSScreenFunctionsPass : public llvm::ModulePass {
34 private:
35   static char ID;
36 
37   std::vector<std::string> &whiteList = stubList;
38 
isPresent(std::vector<std::string> & list,std::string name)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 
isLegal(llvm::Function & F)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 
68 public:
RSScreenFunctionsPass()69   RSScreenFunctionsPass()
70     : ModulePass (ID) {
71       std::sort(whiteList.begin(), whiteList.end());
72   }
73 
getAnalysisUsage(llvm::AnalysisUsage & AU) const74   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
75     AU.setPreservesAll();
76   }
77 
runOnModule(llvm::Module & M)78   bool runOnModule(llvm::Module &M) override {
79     bool failed = false;
80 
81     auto &FunctionList(M.getFunctionList());
82     for(auto &F: FunctionList) {
83       if (!isLegal(F)) {
84         ALOGE("Call to function %s from RenderScript is disallowed\n",
85               F.getName().str().c_str());
86         failed = true;
87       }
88     }
89 
90     if (failed) {
91       llvm::report_fatal_error("Use of undefined external function");
92     }
93 
94     return false;
95   }
96 
97 };
98 
99 }
100 
101 char RSScreenFunctionsPass::ID = 0;
102 
103 namespace bcc {
104 
105 llvm::ModulePass *
createRSScreenFunctionsPass()106 createRSScreenFunctionsPass() {
107   return new RSScreenFunctionsPass();
108 }
109 
110 }
111