[Soot-list] SourceFileTag overwritten by class name
Limei Gilham
gilham at kestrel.edu
Thu Nov 14 13:04:54 EST 2013
Hi,
We have a static analysis tool that builds on top of soot. The tool
needs to be able to access the "SourceFile" attribute in class files. I
found that soot parses the attribute into a SourceFileTag. But it then
overwrites it with the class name (please see the code fragments
below). When a Java source file contains multiple top level classes,
the SourceFileTags for the top level classes with names different from
the source file name no longer contain the information necessary to
access the source file.
For example, if a java source file Foo.java contains top level classes
Foo and Bar, their class files will be named
Foo.class and
Bar.class
The "SourceFile" attribute in the class file "Bar.class" contains
"Foo.java". Yet the SourceFileTag for the corresponding SootClass
contains "Bar.class". Is there a reason that soot stores class file
name instead of source file name in the SourceFileTag? Or is this a bug?
Thanks,
Limei Gilham
-----------------------------------------------------------------------------------------
<soot.CoffiClassSource.java>
public Dependencies resolve( SootClass sc ) {
...
soot.coffi.Util.v().resolveFromClassFile(sc, classFile,
fileName, references); // creates SourceFileTag using the "SourceFile"
attribute in the class file
...
addSourceFileTag(sc); // sets/resets the SourceFileTag to the
class name
...
}
<soot.Util.java>
public void resolveFromClassFile(SootClass aClass, InputStream is,
String filePath, List references) {
// Set "SourceFile" attribute tag
for(int i = 0; i < coffiClass.attributes_count; i++){
if(coffiClass.attributes[i] instanceof SourceFile_attribute){
SourceFile_attribute attr =
(SourceFile_attribute)coffiClass.attributes[i];
String sourceFile =
((CONSTANT_Utf8_info)(coffiClass.constant_pool[attr.sourcefile_index])).convert();
if( sourceFile.indexOf(' ') >= 0 ) {
G.v().out.println( "Warning: Class "+className+"
has invalid SourceFile attribute (will be ignored)." );
} else {
bclass.addTag(new SourceFileTag( sourceFile ,
filePath) ); // creates SourceFileTag using the "SourceFile" attribute
in the class file
}
}
...
<soot.CoffiClassSource.java>
protected void addSourceFileTag(soot.SootClass sc){
if (fileName == null && zipFileName == null)
return;
soot.tagkit.SourceFileTag tag = null;
if (sc.hasTag("SourceFileTag")) {
tag = (soot.tagkit.SourceFileTag)sc.getTag("SourceFileTag");
}
else {
tag = new soot.tagkit.SourceFileTag();
sc.addTag(tag);
}
String name = zipFileName == null ? new
File(fileName).getName() : new File(zipFileName).getName(); // computes
the class name
tag.setSourceFile(name); // sets/resets the SourceFileTag to
the computed class name
}
More information about the Soot-list
mailing list