blob: 9a63ebd89817ca9eac18f04756d20e725c0415d3 [file] [log] [blame]
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -08001/*
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 Brouilleta2dd52f2017-02-16 20:57:26 -080017#include "Log.h"
18#include "RSTransforms.h"
Pirama Arumuga Nainarb85ce442020-07-26 22:34:08 -070019#include "RSFunctionsList.h"
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080020
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
Dan Willemsen2cec3132020-01-23 10:58:52 -080037 bool isPresent(const std::string &name) {
38 auto lower = std::lower_bound(stubList.begin(),
39 stubList.end(),
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080040 name);
41
Dan Willemsen2cec3132020-01-23 10:58:52 -080042 if (lower != stubList.end() && name.compare(*lower) == 0)
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080043 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 Nainarb85ce442020-07-26 22:34:08 -070051 // c. it is present in the RS Functions list.
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080052
53 if (!F.empty())
54 return true;
55
56 llvm::StringRef FName = F.getName();
57 if (FName.startswith("llvm."))
58 return true;
59
Dan Willemsen2cec3132020-01-23 10:58:52 -080060 if (isPresent(FName.str()))
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080061 return true;
62
63 return false;
64 }
65
66public:
Stephen Hines5db508c2015-01-06 01:42:56 -080067 RSScreenFunctionsPass()
68 : ModulePass (ID) {
Dan Willemsen2cec3132020-01-23 10:58:52 -080069 std::sort(stubList.begin(), stubList.end());
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080070 }
71
Stephen Hinesc754d492015-01-08 16:00:50 -080072 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
73 AU.setPreservesAll();
74 }
75
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080076 bool runOnModule(llvm::Module &M) override {
Stephen Hines5db508c2015-01-06 01:42:56 -080077 bool failed = false;
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080078
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 Hines5db508c2015-01-06 01:42:56 -080084 failed = true;
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080085 }
86 }
87
Stephen Hines5db508c2015-01-06 01:42:56 -080088 if (failed) {
89 llvm::report_fatal_error("Use of undefined external function");
90 }
91
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080092 return false;
93 }
94
95};
96
97}
98
99char RSScreenFunctionsPass::ID = 0;
100
101namespace bcc {
102
103llvm::ModulePass *
Stephen Hines5db508c2015-01-06 01:42:56 -0800104createRSScreenFunctionsPass() {
105 return new RSScreenFunctionsPass();
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800106}
107
108}