Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 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 | |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | #include "RSTransforms.h" |
Pirama Arumuga Nainar | b85ce44 | 2020-07-26 22:34:08 -0700 | [diff] [blame] | 19 | #include "RSFunctionsList.h" |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 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 | |
Dan Willemsen | 2cec313 | 2020-01-23 10:58:52 -0800 | [diff] [blame] | 37 | bool isPresent(const std::string &name) { |
| 38 | auto lower = std::lower_bound(stubList.begin(), |
| 39 | stubList.end(), |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 40 | name); |
| 41 | |
Dan Willemsen | 2cec313 | 2020-01-23 10:58:52 -0800 | [diff] [blame] | 42 | if (lower != stubList.end() && name.compare(*lower) == 0) |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 43 | return true; |
| 44 | return false; |
| 45 | } |
| 46 | |
| 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 |
Pirama Arumuga Nainar | b85ce44 | 2020-07-26 22:34:08 -0700 | [diff] [blame] | 51 | // c. it is present in the RS Functions list. |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 52 | |
| 53 | if (!F.empty()) |
| 54 | return true; |
| 55 | |
| 56 | llvm::StringRef FName = F.getName(); |
| 57 | if (FName.startswith("llvm.")) |
| 58 | return true; |
| 59 | |
Dan Willemsen | 2cec313 | 2020-01-23 10:58:52 -0800 | [diff] [blame] | 60 | if (isPresent(FName.str())) |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 61 | return true; |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | public: |
Stephen Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 67 | RSScreenFunctionsPass() |
| 68 | : ModulePass (ID) { |
Dan Willemsen | 2cec313 | 2020-01-23 10:58:52 -0800 | [diff] [blame] | 69 | std::sort(stubList.begin(), stubList.end()); |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Stephen Hines | c754d49 | 2015-01-08 16:00:50 -0800 | [diff] [blame] | 72 | virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { |
| 73 | AU.setPreservesAll(); |
| 74 | } |
| 75 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 76 | bool runOnModule(llvm::Module &M) override { |
Stephen Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 77 | bool failed = false; |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 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()); |
Stephen Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 84 | failed = true; |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Stephen Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 88 | if (failed) { |
| 89 | llvm::report_fatal_error("Use of undefined external function"); |
| 90 | } |
| 91 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
| 95 | }; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | char RSScreenFunctionsPass::ID = 0; |
| 100 | |
| 101 | namespace bcc { |
| 102 | |
| 103 | llvm::ModulePass * |
Stephen Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 104 | createRSScreenFunctionsPass() { |
| 105 | return new RSScreenFunctionsPass(); |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } |