OpenJDK’s Java.utils.Collection.sort() Is Broken: The Good, the Bad and the Worst Case

  • Stijn de Gouw
  • Jurriaan Rot
  • Frank S. de Boer
  • Richard Bubel
  • Reiner Hähnle
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 9206)


We investigate the correctness of TimSort, which is the main sorting algorithm provided by the Java standard library. The goal is functional verification with mechanical proofs. During our verification attempt we discovered a bug which causes the implementation to crash. We characterize the conditions under which the bug occurs, and from this we derive a bug-free version that does not compromise the performance. We formally specify the new version and mechanically verify the absence of this bug with KeY, a state-of-the-art verification tool for Java.





We thank Peter Wong for suggesting to verify TimSort.


  1. 1.
    Ahrendt, W., Mostowski, W., Paganelli, G.: Real-time Java API specifications for high coverage test generation. In: Proceedings of the 10th International Workshop on Java Technologies for Real-time and Embedded Systems, JTRES 2012, pp. 145–154. ACM, New York (2012)Google Scholar
  2. 2.
    Akbarpour, B., Abdel-Hamid, A.T., Tahar, S., Harrison, J.: Verifying a synthesized implementation of IEEE-754 floating-point exponential function using HOL. Comput. J. 53(4), 465–488 (2010)CrossRefGoogle Scholar
  3. 3.
    Beckert, B., Hähnle, R.: Reasoning and verification. IEEE Intell. Syst. 29(1), 20–29 (2014)CrossRefGoogle Scholar
  4. 4.
    Beckert, B., Hähnle, R., Schmitt, P.H. (eds.): Verification of Object-Oriented Software: The KeY Approach. LNCS, vol. 4334. Springer, Heidelberg (2007) Google Scholar
  5. 5.
    Pelevina, M., Bubel, R., Hähnle, R.: Fully abstract operation contracts. In: Margaria, T., Steffen, B. (eds.) ISoLA 2014, Part II. LNCS, vol. 8803, pp. 120–134. Springer, Heidelberg (2014) Google Scholar
  6. 6.
    de Gouw, S., de Boer, F.S., Rot, J.: Proof pearl: the key to correct and stable sorting. J. Autom. Reasoning 53(2), 129–139 (2014)CrossRefGoogle Scholar
  7. 7.
    de Gouw, S., et al: Web appendix of this paper. (2015)
  8. 8.
    Filliâtre, J.-C., Magaud, N.: Certification of sorting algorithms in the system Coq. In: Theorem Proving in Higher Order Logics: Emerging Trends. Nice (1999)Google Scholar
  9. 9.
    Foley, M., Hoare, C.A.R.: Proof of a recursive program: quicksort. Comput. J. 14(4), 391–395 (1971)MathSciNetCrossRefGoogle Scholar
  10. 10.
    Hähnle, R., Schaefer, I., Bubel, R.: Reuse in software verification by abstract method calls. In: Bonacina, M.P. (ed.) CADE 2013. LNCS, vol. 7898, pp. 300–314. Springer, Heidelberg (2013) CrossRefGoogle Scholar
  11. 11.
    Leavens, G.T., Poll, E., Clifton, C., Cheon, Y., Ruby, C., Cok, D., Müller, P., Kiniry, J., Chalin, P., Zimmerman, D.M., Dietl, W.:. JML Reference Manual, Draft revision 2344 (2013)Google Scholar
  12. 12.
    McIlroy, P.M.: Optimistic sorting and information theoretic complexity. In: Ramachandran, V. (ed.) Proceedings of the Fourth Annual ACM/SIGACT-SIAM Symposium on Discrete Algorithms, pp. 467–474. ACM/SIAM, Austin (1993)Google Scholar
  13. 13.
    Mostowski, W.: Formalisation and verification of Java Card security properties in dynamic logic. In: Cerioli, M. (ed.) FASE 2005. LNCS, vol. 3442, pp. 357–371. Springer, Heidelberg (2005) CrossRefGoogle Scholar
  14. 14.
    Mostowski, W.: Fully verified Java card API reference implementation. In: Beckert, B. (ed.) Proceedings of the 4th International Verification Workshop in Connection with CADE-21, CEUR Workshop Proceedings, Vol. 259,, Bremen (2007)Google Scholar
  15. 15.
    Peters, T.: Timsort description. Accessed Feb 2015
  16. 16.
    Sternagel, C.: Proof pearl - a mechanized proof of ghc’s mergesort. J. Autom. Reasoning 51(4), 357–370 (2013)MathSciNetCrossRefGoogle Scholar

Copyright information

© Springer International Publishing Switzerland 2015

Authors and Affiliations

  • Stijn de Gouw
    • 1
    • 2
  • Jurriaan Rot
    • 1
    • 3
  • Frank S. de Boer
    • 1
    • 3
  • Richard Bubel
    • 4
  • Reiner Hähnle
    • 4
  1. 1.CWIAmsterdamThe Netherlands
  2. 2.SDLAmsterdamThe Netherlands
  3. 3.Leiden UniversityLeidenThe Netherlands
  4. 4.Technische Universität DarmstadtDarmstadtGermany

Personalised recommendations