aboutsummaryrefslogtreecommitdiff
path: root/lib/RSScreenFunctionsPass.cpp
blob: 9a63ebd89817ca9eac18f04756d20e725c0415d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright 2014, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Log.h"
#include "RSTransforms.h"
#include "RSFunctionsList.h"

#include <cstdlib>

#include <llvm/IR/Instructions.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/Pass.h>

namespace { // anonymous namespace

// Create a Module pass that screens all the global functions in the
// module and check if any disallowed external function is accessible
// and potentially callable.
class RSScreenFunctionsPass : public llvm::ModulePass {
private:
  static char ID;

  bool isPresent(const std::string &name) {
    auto lower = std::lower_bound(stubList.begin(),
                                  stubList.end(),
                                  name);

    if (lower != stubList.end() && name.compare(*lower) == 0)
      return true;
    return false;
  }

  bool isLegal(llvm::Function &F) {
    // A global function symbol is legal if
    // a. it has a body, i.e. is not empty or
    // b. its name starts with "llvm." or
    // c. it is present in the RS Functions list.

    if (!F.empty())
      return true;

    llvm::StringRef FName = F.getName();
    if (FName.startswith("llvm."))
      return true;

    if (isPresent(FName.str()))
      return true;

    return false;
  }

public:
  RSScreenFunctionsPass()
    : ModulePass (ID) {
      std::sort(stubList.begin(), stubList.end());
  }

  virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
    AU.setPreservesAll();
  }

  bool runOnModule(llvm::Module &M) override {
    bool failed = false;

    auto &FunctionList(M.getFunctionList());
    for(auto &F: FunctionList) {
      if (!isLegal(F)) {
        ALOGE("Call to function %s from RenderScript is disallowed\n",
              F.getName().str().c_str());
        failed = true;
      }
    }

    if (failed) {
      llvm::report_fatal_error("Use of undefined external function");
    }

    return false;
  }

};

}

char RSScreenFunctionsPass::ID = 0;

namespace bcc {

llvm::ModulePass *
createRSScreenFunctionsPass() {
  return new RSScreenFunctionsPass();
}

}