~ubuntu-branches/ubuntu/maverick/clamav/maverick-security

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Transforms/IPO/ExtractGV.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Marc Deslauriers
  • Date: 2011-02-23 14:27:51 UTC
  • mfrom: (0.35.17 sid)
  • Revision ID: james.westby@ubuntu.com-20110223142751-o9xb8jyvhkh75d0n
Tags: 0.96.5+dfsg-1ubuntu1.10.10.2
* SECURITY UPDATE: denial of service via double free in vba processing
  - libclamav/vba_extract.c: set buf to NULL when it gets freed.
  - http://git.clamav.net/gitweb?p=clamav-devel.git;a=commit;h=d21fb8d975f8c9688894a8cef4d50d977022e09f
  - CVE-2011-1003

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include "llvm/Pass.h"
18
18
#include "llvm/Constants.h"
19
19
#include "llvm/Transforms/IPO.h"
 
20
#include "llvm/ADT/SetVector.h"
20
21
#include <algorithm>
21
22
using namespace llvm;
22
23
 
23
24
namespace {
24
25
  /// @brief A pass to extract specific functions and their dependencies.
25
26
  class GVExtractorPass : public ModulePass {
26
 
    std::vector<GlobalValue*> Named;
 
27
    SetVector<GlobalValue *> Named;
27
28
    bool deleteStuff;
28
 
    bool reLink;
29
29
  public:
30
30
    static char ID; // Pass identification, replacement for typeid
31
31
 
33
33
    /// specified function. Otherwise, it deletes as much of the module as
34
34
    /// possible, except for the function specified.
35
35
    ///
36
 
    explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true,
37
 
                             bool relinkCallees = false)
38
 
      : ModulePass(&ID), Named(GVs), deleteStuff(deleteS),
39
 
        reLink(relinkCallees) {}
 
36
    explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
 
37
      : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
40
38
 
41
39
    bool runOnModule(Module &M) {
42
 
      if (Named.size() == 0) {
43
 
        return false;  // Nothing to extract
44
 
      }
45
 
      
46
 
      
47
 
      if (deleteStuff)
48
 
        return deleteGV();
49
 
      M.setModuleInlineAsm("");
50
 
      return isolateGV(M);
51
 
    }
52
 
 
53
 
    bool deleteGV() {
54
 
      for (std::vector<GlobalValue*>::iterator GI = Named.begin(), 
55
 
             GE = Named.end(); GI != GE; ++GI) {
56
 
        if (Function* NamedFunc = dyn_cast<Function>(*GI)) {
57
 
         // If we're in relinking mode, set linkage of all internal callees to
58
 
         // external. This will allow us extract function, and then - link
59
 
         // everything together
60
 
         if (reLink) {
61
 
           for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end();
62
 
                B != BE; ++B) {
63
 
             for (BasicBlock::iterator I = B->begin(), E = B->end();
64
 
                  I != E; ++I) {
65
 
               if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
66
 
                 Function* Callee = callInst->getCalledFunction();
67
 
                 if (Callee && Callee->hasLocalLinkage())
68
 
                   Callee->setLinkage(GlobalValue::ExternalLinkage);
69
 
               }
70
 
             }
71
 
           }
72
 
         }
73
 
         
74
 
         NamedFunc->setLinkage(GlobalValue::ExternalLinkage);
75
 
         NamedFunc->deleteBody();
76
 
         assert(NamedFunc->isDeclaration() && "This didn't make the function external!");
77
 
       } else {
78
 
          if (!(*GI)->isDeclaration()) {
79
 
            cast<GlobalVariable>(*GI)->setInitializer(0);  //clear the initializer
80
 
            (*GI)->setLinkage(GlobalValue::ExternalLinkage);
81
 
          }
82
 
        }
83
 
      }
84
 
      return true;
85
 
    }
86
 
 
87
 
    bool isolateGV(Module &M) {
88
 
      // Mark all globals internal
89
 
      // FIXME: what should we do with private linkage?
90
 
      for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
 
40
      // Visit the global inline asm.
 
41
      if (!deleteStuff)
 
42
        M.setModuleInlineAsm("");
 
43
 
 
44
      // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
 
45
      // implementation could figure out which GlobalValues are actually
 
46
      // referenced by the Named set, and which GlobalValues in the rest of
 
47
      // the module are referenced by the NamedSet, and get away with leaving
 
48
      // more internal and private things internal and private. But for now,
 
49
      // be conservative and simple.
 
50
 
 
51
      // Visit the GlobalVariables.
 
52
      for (Module::global_iterator I = M.global_begin(), E = M.global_end();
 
53
           I != E; ++I)
91
54
        if (!I->isDeclaration()) {
92
 
          I->setLinkage(GlobalValue::InternalLinkage);
 
55
          if (I->hasLocalLinkage())
 
56
            I->setVisibility(GlobalValue::HiddenVisibility);
 
57
          I->setLinkage(GlobalValue::ExternalLinkage);
 
58
          if (deleteStuff == Named.count(I))
 
59
            I->setInitializer(0);
93
60
        }
 
61
 
 
62
      // Visit the Functions.
94
63
      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
95
64
        if (!I->isDeclaration()) {
96
 
          I->setLinkage(GlobalValue::InternalLinkage);
97
 
        }
98
 
 
99
 
      // Make sure our result is globally accessible...
100
 
      // by putting them in the used array
101
 
      {
102
 
        std::vector<Constant *> AUGs;
103
 
        const Type *SBP=
104
 
              Type::getInt8PtrTy(M.getContext());
105
 
        for (std::vector<GlobalValue*>::iterator GI = Named.begin(), 
106
 
               GE = Named.end(); GI != GE; ++GI) {
107
 
          (*GI)->setLinkage(GlobalValue::ExternalLinkage);
108
 
          AUGs.push_back(ConstantExpr::getBitCast(*GI, SBP));
109
 
        }
110
 
        ArrayType *AT = ArrayType::get(SBP, AUGs.size());
111
 
        Constant *Init = ConstantArray::get(AT, AUGs);
112
 
        GlobalValue *gv = new GlobalVariable(M, AT, false, 
113
 
                                             GlobalValue::AppendingLinkage, 
114
 
                                             Init, "llvm.used");
115
 
        gv->setSection("llvm.metadata");
116
 
      }
117
 
 
118
 
      // All of the functions may be used by global variables or the named
119
 
      // globals.  Loop through them and create a new, external functions that
120
 
      // can be "used", instead of ones with bodies.
121
 
      std::vector<Function*> NewFunctions;
122
 
 
123
 
      Function *Last = --M.end();  // Figure out where the last real fn is.
124
 
 
125
 
      for (Module::iterator I = M.begin(); ; ++I) {
126
 
        if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
127
 
          Function *New = Function::Create(I->getFunctionType(),
128
 
                                           GlobalValue::ExternalLinkage);
129
 
          New->copyAttributesFrom(I);
130
 
 
131
 
          // If it's not the named function, delete the body of the function
132
 
          I->dropAllReferences();
133
 
 
134
 
          M.getFunctionList().push_back(New);
135
 
          NewFunctions.push_back(New);
136
 
          New->takeName(I);
137
 
        }
138
 
 
139
 
        if (&*I == Last) break;  // Stop after processing the last function
140
 
      }
141
 
 
142
 
      // Now that we have replacements all set up, loop through the module,
143
 
      // deleting the old functions, replacing them with the newly created
144
 
      // functions.
145
 
      if (!NewFunctions.empty()) {
146
 
        unsigned FuncNum = 0;
147
 
        Module::iterator I = M.begin();
148
 
        do {
149
 
          if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
150
 
            // Make everything that uses the old function use the new dummy fn
151
 
            I->replaceAllUsesWith(NewFunctions[FuncNum++]);
152
 
 
153
 
            Function *Old = I;
154
 
            ++I;  // Move the iterator to the new function
155
 
 
156
 
            // Delete the old function!
157
 
            M.getFunctionList().erase(Old);
158
 
 
159
 
          } else {
160
 
            ++I;  // Skip the function we are extracting
161
 
          }
162
 
        } while (&*I != NewFunctions[0]);
163
 
      }
 
65
          if (I->hasLocalLinkage())
 
66
            I->setVisibility(GlobalValue::HiddenVisibility);
 
67
          I->setLinkage(GlobalValue::ExternalLinkage);
 
68
          if (deleteStuff == Named.count(I))
 
69
            I->deleteBody();
 
70
        }
164
71
 
165
72
      return true;
166
73
    }
170
77
}
171
78
 
172
79
ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs, 
173
 
                                         bool deleteFn, bool relinkCallees) {
174
 
  return new GVExtractorPass(GVs, deleteFn, relinkCallees);
 
80
                                         bool deleteFn) {
 
81
  return new GVExtractorPass(GVs, deleteFn);
175
82
}