Semantic Clone Detection Using Method IOE ... - ACM Digital Library

10 downloads 266 Views 259KB Size Report
Abstract—This paper presents an algorithm for the detection of semantic clones in Java methods. Semantic clones are defined as functionally-identical code ...
Semantic Clone Detection Using Method IOE-Behavior Rochelle Elva University of Central Florida, Orlando FL, 32816 [email protected]

Gary T. Leavens University of Central Florida, Orlando FL, 32816 [email protected]

seek to identify clones created by activities beyond mere copy and paste. Juergens et al. [2] investigated JabRef, which is a commercially-used Java program. Out of 2,700 LOC they manually found that 32 out of 86 Utility methods were partially semantic clones — a little over 37%. Jiang and Su [4] investigated functional similarity in code within methods. Their evaluation was based on input-output behavior, while ignoring effects. In studying the Linux system, they found that a little more than 42% of the semantically similar code was also syntactically similar; while more than 36% of syntactic clones are not semantic clones. Another application of semantic clone detection is demonstrated by Kawrykow and Robillard [1]. They use semantic clone detection to identify instances of less than optimal usage of APIs, where developers re-implemented library functions instead of making a call to an available function. Using their detection tool iMaus, they studied 10 widely-used Java projects from the SourceForge repository.

Abstract—This paper presents an algorithm for the detection of semantic clones in Java methods. Semantic clones are defined as functionally-identical code fragments. Our detection process operates on the premise that if two code fragments are semantic clones, then their input-output behavior would be identical. We adopt a wholistic approach to the definition of input-output behavior by including not only the parameters and return values of methods; but also their effects, as reflected in the pre- and post-states of the heap. We refer to this as a method’s IOE-behavior (input, output and effects). Keywords-Software Clone Detection, Semantic Clones, Method IOE-behavior, Program Understanding

I. P ROBLEM Duplication in code exists in one of two forms: representational or functional. The former case yields syntactic clones, while the latter is responsible for the formation of semantic clones. Although current literature indicates that as many as 405 valid semantic clones can be found in as few as 1589 kLOC of commercial software written in C [1], the majority of clone detection algorithms and tools are limited to the detection of syntactic clones. For example, when tested on 109 variations in implementations of the same functionality, 4 state of the art tools were only able to identify 1% of the semantic clones [2]. However, since the presence of semantic clones can cause some of the same maintenance issues as their syntactic counterparts [3] they should not be ignored. In the work that has been done on semantic clone detection, we found one tool in the literature [4] for automated detection, and another paper which used manual identification [2]. Both of these used input-output as the technique for identifying semantic clones. It is our thesis that the behavior of a method also includes its effects. Thus the use of inputoutput behavior together with a method’s effects gives a more complete evaluation of the method’s behavior, leading to greater precision and lower incidence of false positives.

III. T HE A PPROACH Our approach works at the method level in Java code. We define functional similarity in terms of input and total output behavior. Input, is determined by two factors, namely: the value of parameters passed to the method, and state of the heap when the method is invoked. Total output behavior is defined in terms of both the method’s return values and its effects. A method’s effects are its possible, persistent changes to the heap. These changes include mutations of static fields. All of this behavior we refer to collectively as a method’s IOE-behavior (input, output and effects). Our semantic clone detection algorithm involves 4 subprocesses: abstraction, filtering, testing and collection. In the abstraction phase static analysis produces a decorated abstract syntax tree (AST) from which is obtained lists of the two method properties — methodtypes and effects. Filtering, which is the second phase in our process, uses the code abstractions to identify preliminary sets of potential method clones, called the candidate clone sets. Two filters are applied to the identified methods. The first filter uses syntactic information encoded in the methodtype. After this filter, only methods with equivalent return and parameter types will be considered candidate clones. The second filter uses the semantic information of a method’s effects to further

II. R EVIEW OF L ITERATURE Recently there has been some interest in the detection of semantic clones, although called by different names: wide-miss clones, high-level concept clones [5], functionally equivalent code [4], behavioral clones [6], representationally similar code fragments and simions [2]. All of these works

c 2012 IEEE 978-1-4673-1795-5/12/$31.00

80

IWSC 2012, Zurich, Switzerland

IV. C ONCLUSION

refine the candidate method clones returned by the first filter, by rejecting as candidates methods that have possibly different effects. The net result of the two filters, is that the only methods left in any equivalence class, have the same methodtype and effects. The reduction in the number and sizes of equivalence classes is important, as it decreases the number of computations at the next level of the detection process, which is computationally the most costly. The testing phase embodies the hypothesis that running the methods on a sufficiently large sample of their input domain, gives a good approximation to behavioral equivalence. This phase rejects as candidates, methods that have different total output behavior based on the same inputs. Each candidate clone set is tested using a test-file with automatically generated tests. The tests evaluate the actual dynamic behavior of the clone candidates, by using method calls to run each member of an equivalence class on the same input and then comparing the corresponding outputs. Since there is a potentially infinite number of possible combinations of object states and parameter values, and only a finite set of those can practically be evaluated, randomly generated values are used to explore the space of the inputs. The reliability of the results is enhanced by creating a userdefined number of test cases. In the Collection phase, the Test Driver created for running the test-files, is executed. As each test-file is run, a subset of the methods tested is returned as an equivalence class of semantic clones - that is, each element of the class, produced identical results for every test case and can thus be defined as having the same observable IOE-behavior. For example, given the two void methods of the SomeNumber class shown in Figure 1, both methods have the same IOE-behavior and are thus semantic clones. They have the identical methodtype and the same effect—they both act on a SomeNumber receiver object and set its value field to the greatest power of 2, less than or equal to the value of the parameter. Yet, their functional equivalence is not at all apparent from just looking at the text of the methods. Our algorithm detects such semantic clones that other purely syntactic detection methods would totally miss. We have also developed a tool JSCTracker for the automation of this detection process. More details about our tool and our algorithm can be found in [7].

We present a computationally-simple method for detecting semantic clones among methods. Our focus is on using the IOE-behavior of methods to detect functional equivalence, which would very often be unidentified by existing structurally-based clone detection techniques. R EFERENCES [1] D. Kawrykow and M. P. Robillard, “Improving api usage through automatic detection of redundant code,” in Proceedings of the 2009 IEEE/ACM International Conference on Automated Software Engineering, ser. ASE ’09. Washington, DC, USA: IEEE Computer Society, 2009, pp. 111–122. [Online]. Available: http://dx.doi.org/10.1109/ASE.2009.62 [2] E. Juergens, F. Deissenboeck, and B. Hummel, “Code similarities beyond copy & paste,” in Proceedings of the 2010 14th European Conference on Software Maintenance and Reengineering, ser. CSMR ’10. Washington, DC, USA: IEEE Computer Society, 2010, pp. 78–87. [Online]. Available: http://dx.doi.org/10.1109/CSMR.2010.33 [3] V. R. Richard Fanta, “Removing clones from the code,” Journal of Software Maintenance: Research and Practice, vol. 11, no. 4, pp. 223–243, July/August 1999, 17 pages. [4] L. Jiang and Z. Su, “Automatic mining of functionally equivalent code fragments via random testing,” in Proceedings of the eighteenth international symposium on Software testing and analysis, ser. ISSTA ’09. New York, NY, USA: ACM, 2009, pp. 81–92. [Online]. Available: http://doi.acm.org/10.1145/1572272.1572283 [5] A. Marcus and J. I. Maletic, “Identification of high-level concept clones in source code,” in Proceedings of the 16th IEEE international conference on Automated software engineering, ser. ASE ’01. Washington, DC, USA: IEEE Computer Society, 2001, pp. 107–. [Online]. Available: http://portal.acm.org/citation.cfm?id=872023.872542 [6] E. Juergens, F. Deissenboeck, and B. Hummel, “Clone detection beyond copy & paste,” in Proc. of the 3rd International Workshop on Software Clones, 2009. [7] R. Elva and G. Leavens, “Jsctracker: A semantic clone detection tool for Java code,” University of Central Florida, Department of EECS,University of Central Florida,4000 Central Florida Blvd,Orlando, Florida, 32816, USA, Research paper CS-TR-12-04, March 2012, http://www.eecs.ucf.edu/ leavens/tech-reports/UCF/CS-TR-1204/TR.pdf;. [8] H. S. Warren, Hacker’s Delight. Boston, MA, USA: AddisonWesley Longman Publishing Co., Inc., 2002.

Figure 1.

Semantic Clones From Modified Code in Hacker’s Delight [8]

2

81