• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Log.h"
18 #include "RSTransforms.h"
19 #include "RSFunctionsList.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 
isPresent(const std::string & name)37   bool isPresent(const std::string &name) {
38     auto lower = std::lower_bound(stubList.begin(),
39                                   stubList.end(),
40                                   name);
41 
42     if (lower != stubList.end() && name.compare(*lower) == 0)
43       return true;
44     return false;
45   }
46 
isLegal(llvm::Function & F)47   bool isLegal(llvm::Function &F) {
48     // A global function symbol is legal if
49     // a. it has a body, i.e. is not empty or
50     // b. its name starts with "llvm." or
51     // c. it is present in the RS Functions list.
52 
53     if (!F.empty())
54       return true;
55 
56     llvm::StringRef FName = F.getName();
57     if (FName.startswith("llvm."))
58       return true;
59 
60     if (isPresent(FName.str()))
61       return true;
62 
63     return false;
64   }
65 
66 public:
RSScreenFunctionsPass()67   RSScreenFunctionsPass()
68     : ModulePass (ID) {
69       std::sort(stubList.begin(), stubList.end());
70   }
71 
getAnalysisUsage(llvm::AnalysisUsage & AU) const72   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
73     AU.setPreservesAll();
74   }
75 
runOnModule(llvm::Module & M)76   bool runOnModule(llvm::Module &M) override {
77     bool failed = false;
78 
79     auto &FunctionList(M.getFunctionList());
80     for(auto &F: FunctionList) {
81       if (!isLegal(F)) {
82         ALOGE("Call to function %s from RenderScript is disallowed\n",
83               F.getName().str().c_str());
84         failed = true;
85       }
86     }
87 
88     if (failed) {
89       llvm::report_fatal_error("Use of undefined external function");
90     }
91 
92     return false;
93   }
94 
95 };
96 
97 }
98 
99 char RSScreenFunctionsPass::ID = 0;
100 
101 namespace bcc {
102 
103 llvm::ModulePass *
createRSScreenFunctionsPass()104 createRSScreenFunctionsPass() {
105   return new RSScreenFunctionsPass();
106 }
107 
108 }
109