@@ -167,46 +167,23 @@ namespace smack {
167167 this ->trans ->setSymbolicHeapHExpr (sh);
168168 }
169169
170- bool MemSafeChecker::isValidPtrInMain (std::string ptrName) {
171- int l, r = ptrName.length ();
172- for (l = 1 ; ptrName[l - 1 ] != ' _' ; l++);
173- if (r - l < 5 ) return false ;
174- std::string funcName = ptrName.substr (l, 4 );
175- std::string reaptedTime = ptrName.substr (l + 4 , r - l - 4 );
176- // std::cout << funcName << ' ' << reaptedTime << std::endl;
177- if (funcName != " main" ) return false ;
178- for (auto ch : reaptedTime)
179- if (ch < ' 0' || ch > ' 9' )
180- return false ;
181- // std::cout << ptrName << " is valid in main\n";
182- return true ;
183- }
184-
185- std::queue<std::string> MemSafeChecker::getValidPtrInMain (ExecutionStatePtr state, CFGPtr cfg) {
186- std::queue<std::string> Q;
187- std::map<std::string, bool > isGlobal;
188- for (auto gv : cfg->getConstDecls ()) {
189- // std::cout << gv->getName() << std::endl;
190- isGlobal[gv->getName ()] = true ;
170+ std::set<std::string> MemSafeChecker::getRootVarsForMemtrackAnalysis (ExecutionStatePtr state, CFGPtr cfg) {
171+ std::set<std::string> rootVars;
172+ for (auto rootVar : state->obtainMemtrackRootSet ()) {
173+ std::string blkName = state->getVarEquiv ()->getBlkName (rootVar);
174+ rootVars.insert (blkName);
191175 }
192- for (auto spl : state->getSH ()->getSpatialExpr ()) {
193- if (spl->getId () != SpatialLiteral::Kind::SPT) continue ;
194- // std::cout << spl << std::endl;
195- std::string blkName = spl->getBlkName ();
196- std::string origName = state->getVarFactory ()->getOrigVarName (blkName);
197- if (this ->isValidPtrInMain (origName) || isGlobal[origName]) {
198- Q.push (blkName);
199- // std::cout << blkName << std::endl;
200- }
176+ for (auto globalVar : cfg->getConstDecls ()) {
177+ std::string varName = globalVar->getName ();
178+ rootVars.insert (state->getVarEquiv ()->getBlkName (varName + " _bb0" ));
201179 }
202- return Q ;
180+ return rootVars ;
203181 }
204182
205- std::vector<std::string> MemSafeChecker::getSuccessors (ExecutionStatePtr state, std::string u) {
183+ std::vector<std::string> MemSafeChecker::getSuccessorsForMemtrackAnalysis (ExecutionStatePtr state, std::string u) {
206184 std::vector<std::string> successors;
207185 int u_offset = state->getVarEquiv ()->getOffset (u);
208186 std::string u_blk = state->getVarEquiv ()->getBlkName (u);
209- // std::cout << u << " " << u_blk << " " << u_offset << std::endl;
210187 bool inRegion = false ;
211188 for (auto spl : state->getSH ()->getSpatialExpr ()) {
212189 if (spl->getId () == SpatialLiteral::Kind::SPT &&
@@ -233,24 +210,20 @@ namespace smack {
233210 std::queue<std::string> workList;
234211 std::map<std::string, bool > hasVisited;
235212 std::map<std::string, bool > tracked;
236- workList = this ->getValidPtrInMain (state, cfg);
213+ for (auto rootVar : this ->getRootVarsForMemtrackAnalysis (state, cfg))
214+ workList.push (rootVar);
237215 while (!workList.empty ()) {
238216 std::string u = workList.front ();
239- // std::cout << "\n=============================\n";
240- // std::cout << "src: " << u << std::endl;
241217 workList.pop ();
242218 if (hasVisited[u]) continue ;
243219 hasVisited[u] = true ;
244220 if (state->getVarEquiv ()->getOffset (u) == 0 ) {
245- // std::cout << u << " is tracked" << std::endl;
246221 tracked[u] = true ;
247222 }
248- for (auto v : this ->getSuccessors (state, u)) {
249- // std::cout << state->getVarEquiv()->getAllocName(v) << ' ';
223+ for (auto v : this ->getSuccessorsForMemtrackAnalysis (state, u)) {
250224 if (hasVisited[v]) continue ;
251225 workList.push (v);
252226 }
253- // std::cout << "\n=============================\n";
254227 }
255228 for (auto spl : state->getSH ()->getSpatialExpr ())
256229 if (spl->getId () == SpatialLiteral::Kind::SPT &&
@@ -294,14 +267,18 @@ namespace smack {
294267 }
295268 }
296269 }
270+ std::string prp = SmackOptions::prp.getValue ();
271+ bool trackAll = checkMemTrack (state, mainGraph);
297272 int errType;
298- std::set<std::string> memtrackRoots = state->obtainMemtrackRootSet ();
299- if (!checkMemTrack (state, mainGraph)) {
273+ if (!trackAll && prp.find (" memsafety" ) != std::string::npos) {
300274 DEBUG_WITH_COLOR (std::cout << " LEAK: Memtrack!!!" << std::endl;, color::red);
301275 errType = MEMTRACK;
302- } else {
276+ } else if (trackAll && prp. find ( " memcleanup " ) != std::string::npos) {
303277 DEBUG_WITH_COLOR (std::cout << " LEAK: Memcleanup!!!" << std::endl;, color::red);
304278 errType = MEMCLEAN;
279+ } else {
280+ DEBUG_WITH_COLOR (std::cout << " LEAK: CHECKUNKNOWN!!!" << std::endl;, color::yellow);
281+ errType = UNKNOWN;
305282 }
306283 return {false , errType};
307284 // for(std::string unusedName : state->getVarFactory()->getUnusedNames()){
0 commit comments