-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements Dr Memory analysis #837
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6da9c76
Implements Dr Memory analysis
arnaudsylvestre fef6ea0
Correct a test due to Dr Memory integration
arnaudsylvestre acff735
Add new line in package-info.java
arnaudsylvestre 9b2549e
Correct error handling
arnaudsylvestre dd521d6
Change remediationFunctionBaseEffort to remediationFunctionGapMultiplier
arnaudsylvestre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/drmemory/CxxDrMemoryRuleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plugins.cxx.drmemory; | ||
|
||
import org.sonar.api.config.Settings; | ||
import org.sonar.api.platform.ServerFileSystem; | ||
import org.sonar.api.server.rule.RulesDefinitionXmlLoader; | ||
import org.sonar.plugins.cxx.utils.CxxAbstractRuleRepository; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public final class CxxDrMemoryRuleRepository extends CxxAbstractRuleRepository { | ||
|
||
public static final String KEY = "drmemory"; | ||
public static final String CUSTOM_RULES_KEY = "sonar.cxx.drmemory.customRules"; | ||
private static final String NAME = "Dr Memory"; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public CxxDrMemoryRuleRepository(ServerFileSystem fileSystem, RulesDefinitionXmlLoader xmlRuleLoader, Settings settings) { | ||
super(fileSystem, xmlRuleLoader, settings, KEY, NAME, CUSTOM_RULES_KEY); | ||
} | ||
|
||
@Override | ||
protected String fileName() { | ||
return "/drmemory.xml"; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/drmemory/CxxDrMemorySensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plugins.cxx.drmemory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.sonar.api.batch.SensorContext; | ||
import org.sonar.api.batch.fs.FileSystem; | ||
import org.sonar.api.batch.fs.InputFile; | ||
import org.sonar.api.component.ResourcePerspectives; | ||
import org.sonar.api.config.Settings; | ||
import org.sonar.api.profiles.RulesProfile; | ||
import org.sonar.api.resources.Project; | ||
import org.sonar.plugins.cxx.drmemory.DrMemoryParser.DrMemoryError; | ||
import org.sonar.plugins.cxx.drmemory.DrMemoryParser.DrMemoryError.Location; | ||
import org.sonar.plugins.cxx.utils.CxxMetrics; | ||
import org.sonar.plugins.cxx.utils.CxxReportSensor; | ||
import org.sonar.plugins.cxx.utils.CxxUtils; | ||
import org.sonar.plugins.cxx.utils.EmptyReportException; | ||
|
||
/** | ||
* Dr. Memory is a memory monitoring tool capable of identifying memory-related | ||
* programming errors such as accesses of uninitialized memory, accesses to | ||
* unaddressable memory (including outside of allocated heap units and heap | ||
* underflow and overflow), accesses to freed memory, double frees, memory | ||
* leaks, and (on Windows) handle leaks, GDI API usage errors, and accesses to | ||
* un-reserved thread local storage slots. See also: http://drmemory.org | ||
* | ||
* @author asylvestre | ||
*/ | ||
public class CxxDrMemorySensor extends CxxReportSensor { | ||
|
||
public static final String REPORT_PATH_KEY = "sonar.cxx.drmemory.reportPath"; | ||
private final RulesProfile profile; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public CxxDrMemorySensor(ResourcePerspectives perspectives, | ||
Settings settings, FileSystem fs, RulesProfile profile) { | ||
super(perspectives, settings, fs, CxxMetrics.DRMEMORY); | ||
this.profile = profile; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean shouldExecuteOnProject(Project project) { | ||
return super.shouldExecuteOnProject(project) | ||
&& !profile.getActiveRulesByRepository( | ||
CxxDrMemoryRuleRepository.KEY).isEmpty(); | ||
} | ||
|
||
@Override | ||
protected String reportPathKey() { | ||
return REPORT_PATH_KEY; | ||
} | ||
|
||
@Override | ||
protected void processReport(final Project project, | ||
final SensorContext context, File report) | ||
throws javax.xml.stream.XMLStreamException { | ||
CxxUtils.LOG.debug("Parsing 'Dr Memory' format"); | ||
|
||
try { | ||
for (DrMemoryError error : DrMemoryParser.parse(report)) { | ||
if (error.stackTrace.isEmpty()) { | ||
saveUniqueViolation(project, context, CxxDrMemoryRuleRepository.KEY, | ||
null, null, | ||
error.type.getId(), error.message); | ||
} | ||
for (Location errorLocation : error.stackTrace) { | ||
if (isFileInAnalysis(errorLocation)) { | ||
saveUniqueViolation(project, context, CxxDrMemoryRuleRepository.KEY, | ||
errorLocation.file, errorLocation.line.toString(), | ||
error.type.getId(), error.message); | ||
break; | ||
} | ||
|
||
} | ||
} | ||
} catch (IOException e1) { | ||
throw new EmptyReportException(); | ||
} | ||
} | ||
|
||
private boolean isFileInAnalysis(Location errorLocation) { | ||
String root = fs.baseDir().getAbsolutePath(); | ||
String normalPath = CxxUtils.normalizePathFull(errorLocation.file, root); | ||
InputFile inputFile = fs.inputFile(fs.predicates().is(new File(normalPath))); | ||
return inputFile != null; | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/drmemory/DrMemoryParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plugins.cxx.drmemory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.sonar.plugins.cxx.drmemory.DrMemoryParser.DrMemoryError.Location; | ||
|
||
public class DrMemoryParser { | ||
|
||
public static enum DrMemoryErrorType { | ||
UNADRESSABLE_ACCESS("UnadressableAccess", "UNADDRESSABLE ACCESS"), | ||
UNINITIALIZE_READ("UninitializedRead", "UNINITIALIZED READ"), | ||
INVALID_HEAP_ARGUMENT("InvalidHeapArgument", "INVALID HEAP ARGUMENT"), | ||
GDI_USAGE_ERROR("GdiUsageError", "GDI Usage Error"), | ||
HANDLE_LEAK("HandleLeak", "HANDLE LEAK"), | ||
WARNING("DrMemoryWarning", "WARNING"), | ||
POSSIBLE_LEAK("PossibleMemoryLeak", "POSSIBLE LEAK"), | ||
LEAK("MemoryLeak", "LEAK"), | ||
UNRECOGNIZED("Dr Memory unrecognized error", ""); | ||
|
||
private String id; | ||
private String title; | ||
|
||
DrMemoryErrorType(String id, String title) { | ||
this.id = id; | ||
this.title = title; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
} | ||
|
||
|
||
|
||
public static class DrMemoryError { | ||
|
||
public static class Location { | ||
public String file = ""; | ||
public Integer line; | ||
} | ||
|
||
public DrMemoryErrorType type = DrMemoryErrorType.UNRECOGNIZED; | ||
public List<Location> stackTrace = new ArrayList<Location>(); | ||
public String message = ""; | ||
} | ||
|
||
|
||
private DrMemoryParser() { | ||
} | ||
|
||
public static final Pattern rx_message_finder = Pattern.compile( "^Error #\\d+:(.*)"); | ||
public static final Pattern rx_file_finder = Pattern.compile( "^.*\\[(.*):(\\d+)\\]$"); | ||
|
||
public static final int __TOP_COUNT = 4; | ||
|
||
public static List<DrMemoryError> parse( File file ) throws IOException { | ||
|
||
List<DrMemoryError> result = new ArrayList<DrMemoryError>(); | ||
|
||
List<String> elements = getElements(file); | ||
|
||
for (String element : elements) { | ||
Matcher m = rx_message_finder.matcher( element ); | ||
|
||
if( m.find() ) { | ||
DrMemoryError error = new DrMemoryError(); | ||
error.type = extractErrorType(m.group(1)); | ||
String elementSplitted[] = element.split("\\r?\\n"); | ||
error.message = elementSplitted[0]; | ||
for (String elementPart : elementSplitted) { | ||
Matcher locationMatcher = rx_file_finder.matcher( elementPart ); | ||
if (locationMatcher.find()) { | ||
Location location = new Location(); | ||
location.file = locationMatcher.group( 1 ); | ||
location.line = Integer.parseInt(locationMatcher.group( 2 )); | ||
error.stackTrace.add(location); | ||
} | ||
} | ||
result.add(error); | ||
|
||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
private static DrMemoryErrorType extractErrorType(String title) { | ||
String cleanedTitle = clean(title); | ||
for (DrMemoryErrorType drMemoryErrorType : DrMemoryErrorType.values()) { | ||
if (cleanedTitle.startsWith(drMemoryErrorType.getTitle())) | ||
return drMemoryErrorType; | ||
} | ||
return DrMemoryErrorType.UNRECOGNIZED; | ||
} | ||
|
||
|
||
private static String clean(String title) { | ||
return title.trim(); | ||
} | ||
|
||
|
||
public static List<String> getElements( File file ) throws IOException { | ||
FileReader fr = new FileReader( file ); | ||
BufferedReader br = new BufferedReader( fr ); | ||
List<String> list = new ArrayList<String>(); | ||
StringBuilder sb = new StringBuilder(); | ||
String line; | ||
int cnt = 0; | ||
while( ( line = br.readLine() ) != null ) { | ||
if( cnt > ( __TOP_COUNT ) ) { | ||
|
||
if( line.matches( "^\\s*$" ) ) { | ||
list.add( sb.toString() ); | ||
sb.setLength( 0 ); | ||
|
||
} else { | ||
sb.append( line + "\n" ); | ||
} | ||
} | ||
|
||
cnt++; | ||
} | ||
|
||
if( sb.length() > 0 ) { | ||
list.add( sb.toString() ); | ||
} | ||
|
||
br.close(); | ||
|
||
return list; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/drmemory/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
/** | ||
* Package with sensor to evaluate Dr Memrory specific results files. | ||
*/ | ||
@ParametersAreNonnullByDefault | ||
package org.sonar.plugins.cxx.drmemory; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no newline at end of file |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling is different. All IO failures are handled as EmptyReportException. Other sensor are doing this only if really empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of Exception do you want to be raised ? Can I create my own Exception type ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arnaudsylvestre please have a look to https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java. EmptyReportException is a special case.