PVS-Studio, a solution for developers of modern resource-intensive applications


Published on

The PVS-Studio tool
the Viva64 rule set for 64-bit software analysis;
the VivaMP rule set for parallel software analysis;
the general-purpose analysis rule set.
Licensing and pricing policy for PVS-Studio
About the OOO “Program Verification Systems” company

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PVS-Studio, a solution for developers of modern resource-intensive applications

  1. 1. PVS-Studio, a solution for developers of modern resource-intensive applications<br />OOO “Program Verification Systems” (Co Ltd)<br />www.viva64.com<br />
  2. 2. The PVS-Studio tool<br />the Viva64rule set for 64-bit software analysis;<br />the VivaMPrule set for parallel software analysis;<br />the general-purpose analysis rule set.<br />Licensing and pricing policy forPVS-Studio<br />About the OOO“Program Verification Systems” company<br />Contents<br />
  3. 3. The PVS-Studio Tool<br />Analysis of C/C++ source code<br />
  4. 4. PVS-Studio is a static code analyzer intended for developers of contemporary resource-intensive software<br />PVS-Studio includes three sets of diagnostic rules:<br />Viva64 intended for detecting errors of 64-bit software development and migration;<br />VivaMP intended for detecting parallel issues in source code of software developed with the OpenMP technology;<br />General-purpose analysis for detecting general errors such as misprints, buffer overflows, condition errors, etc.<br />
  5. 5. errors accompanying porting of 32-bit software to 64-bit systems; <br />errors accompanying development of new 64-bit software; <br />errors in parallel applications caused by programmers’ insufficient knowledge of the OpenMP technology; <br />errors caused by incorrect memory handling in parallel code (unprotected memory access, absence of synchronization, incorrect variable access mode and so on);<br />logical errors, incorrect use of algorithms and containers, buffer overflows;<br />misprints brought into the text during copying of code fragments or through inattention;<br />non-optimal constructs that can be easily optimized.<br />PVS-Studio detects the following types of issues in C/C++ code<br />
  6. 6. develop new 64-bit software; <br />port 32-bit code to 64-bit systems; <br />provide software with support of concurrent execution using the OpenMP technology;<br />want to enhance code’s quality and safety;<br />want to detect as many errors as possible already at the stage of developing.<br />The PVS-Studio code analyzer is necessary for those who<br />
  7. 7. Easy-to-download:<br />http://www.viva64.com/en/pvs-studio-download/<br />Easy-to-try:<br />PVS-Studiointegrates into Visual Studio;<br />The distribution package includes samples of software with errors.<br />Easy-to-buy (online or through a bank):<br />http://www.viva64.com/en/order/<br />Why PVS-Studio?<br />
  8. 8. PVS-Studio’s features<br />integration into Visual Studio2005/2008/2010;<br />C and C++ support;<br />C++0x support within the framework of Visual Studio 2010;<br />detailed help system (including the Russian language);<br />usability;<br />convenient error filtering and suppression system;<br />analysis of files in concurrent mode.<br />
  9. 9. PVS-Studio’s appearance<br />
  10. 10. Online-documentation (also in the PDF format) <br />
  11. 11. You may easily study how PVS-Studio works with the help of the demonstration project OmniSample included into the distribution package<br />
  12. 12. Some our customers<br />www.viva64.com/en/customers/<br />
  13. 13. Viva64, a rule set for 64-bit software analysis<br />
  14. 14. The problem of applications migration to 64-bit platforms<br /><ul><li>The process of code migration is inevitable.
  15. 15. C/C++ applications migration is most difficult because of language peculiarities.
  16. 16. During migration, there can occur errors in programs which are impossible to diagnose by the existing methodology of testing.
  17. 17. It is difficult to get assured in the correctness of modern programs after their migration to 64-bit systems (MS-DOS 1.0 contained 4,000 code lines, while Windows Vista contains 50,000,000). That is why it is impossible to refer to the experience of past migrations.</li></li></ul><li>The difficulty of 64-bit migration for various types of code compared to assembler, in percentage terms<br />According to Kang Su Gatlin, Visual C++ Program Manager,Microsoft Corporation, 2004<br />
  18. 18. Here are some samples of errors Viva64 can detect<br />
  19. 19. Problem with overloaded virtual functions<br />Base class:<br />classCWinApp {<br />virtualvoidWinHelp(DWORD_PTR, UINT); <br /> };<br />User’s code:<br />class CMyApp : public CWinApp {<br />virtualvoidWinHelp(DWORD, UINT); <br />};<br />32-bit system:<br />64-bit system:<br />
  20. 20. Address arithmetic with pointers<br />int A = -2;<br />unsigned B = 1;<br />int array[5] = { 1, 2, 3, 4, 5 };<br />int *ptr = array + 3;<br />ptr = ptr + (A + B);<br />printf("%in", *ptr);<br />VariableAtype intis cast to type unsigned;<br />A and B addition is carried out. As a result, we obtain value 0xFFFFFFFF typeunsigned;<br />Expression "ptr + 0xFFFFFFFFu“ is calculated. The result depends on pointer dimension on the given platform. In a 32-bit program, the expression will be equivalent to "ptr - 1" and we will successfully print number 3. Ina 64-bit program, value 0xFFFFFFFFu will be added to the pointer, as a result, the pointer will appear far beyond the array limits.<br />
  21. 21. Infinite loops<br />boolIsPresent(char *array, size_tarraySize,<br />char key)<br />{<br />for (unsigned i = 0; i != arraySize; ++i)<br />if (array[i] == key)<br /> return true;<br /> return false;<br />}<br />This code will lead to an infinite loop occurrence if arraySize exceeds value UINT_MAX.Detection of such errors using unit-tests or dynamic analyzers (BoundsChecker) is extremely complicated by the necessity of running on large data sizes. During processing of a small data size, the error will not be detected. <br />
  22. 22. Errors in shift operations<br />ptrdiff_t SetBitN(ptrdiff_t value, unsigned bitNum) {<br /> ptrdiff_t mask = 1 << bitNum;<br /> return value | mask;<br />}<br />Code of the given bit setting in unit.<br />The first error consists in character variable shift.During 31st bit setting on a 64-bit system, the result of the function operation will be the value 0xffffffff80000000<br />The second error is connected with the fact that this code will never set bits with numbers 32 to 63. Please note that "1" has typeint, and during shift by 32 positions, overflow will occur.Whether we obtain as a result 0 (A) or 1 (B) depends on the compiler implementation.<br />
  23. 23. Errors of magic numbers use<br />#define N_COUNT 100<br />int **pArray = (int**)<br />malloc(N_COUNT * 4);<br />hFileMapping = CreateFileMapping (<br /> (HANDLE) 0xFFFFFFFF,<br /> NULL,<br /> PAGE_READWRITE,<br /> (DWORD) 0,<br /> (DWORD) (szBufIm),<br /> (LPCTSTR) &FileShareNameMap[0]);<br />size_t n, newexp;<br />n = n >> (32 - newexp);<br />The most widespread magic values which are dangerous during applications porting from a 32-bit to a 64-bit platform<br />
  24. 24. Overflow in arithmetic expressions in which 32-bit and 64-bit data types are used together<br />ptrdiff_t UnsafeCalcIndex(int x, int y, int width) {<br />return x + y * width;<br />}<br />...<br />intdomainWidth = 50000;<br />intdomainHeght = 50000;<br /> <br />for (int x = 0; x != domainWidth; ++x)<br /> for (int y = 0; y != domainHeght; ++y)<br /> array[UnsafeCalcIndex(x, y, domainWidth)] = 1;<br />This code cannot fill correctly the array, which consists of 50000*50000 elements. During calculation of the expression "x + y * width"overflow occurs, and, as a result, outbounds the array limits.<br />
  25. 25. Errors of implicit type cast<br />size_t __fread(void * __restrict buf, size_t size, size_t count,<br />FILE * __restrict fp);<br /> <br />size_tfread(void * __restrict buf, size_t size, size_t count,<br />FILE * __restrict fp)<br />{<br />int ret;<br /> FLOCKFILE(fp);<br />ret = __fread(buf, size, count, fp);<br /> FUNLOCKFILE(fp);<br />return (ret);<br />}<br />Function __freadreturns type size_t, however, for storing the quantity of read bytes, type int is used. As a result, in case of large sizes of the data being read, the function can return the wrong quantity of bytes which will be read in fact.<br />
  26. 26. The example shown above was taken from FreeBSDsource code.The error was corrected in December 2008 only!With that, the first (pilot) 64-bit FreeBSDversion was released as far back as June 2003.<br />
  27. 27. Patterns of 64-bit errors in the code were explored in more than 100 various articles in printed and electronic media.<br />Our own experience in computational modeling packages code migration and visualization in C++ was considered.<br />During the research, a base consisting of dozens of various error patterns connected with code migration to 64-bit systems was created. Both well-known (published) errors and earlier unknown errors were included in the base.<br />Rules of diagnostics were formulated on the basis of detected error patterns. <br />Both error patterns and diagnostic rules are published in our articles and are available to everyone.<br />How we made 64- bit code analyzer<br />
  28. 28. Comparison: Viva64,VC++ (/Wp64), C++Test and PC-Lint<br />The table presents comparison carried out at the end of 2008. Nowadays, PVS-Studio has much more capabilities.<br />
  29. 29. E.G.S. S.r.l. Company is engaged in the development of solutions in the sphere of 3D objects modeling on the basis of triangulated grids.<br />Use of Viva64 for verification of CAD-system Leios Studio by EGS S.r.l.<br />
  30. 30. Use of Viva64 for verification of CAD-system Leios Studio by EGSS.r.l. (continued)<br />The total size of Leios Studiosource code is 13 megabytes (440,000 code lines).<br />Code migration with the use of Viva64 allowed to save much time which would be needed in case of manual code review.<br />The main problem cases detected in the process of automatic code analysis with the help of Viva64 tool are:<br />Computational algorithms features during processing of large data volumes; <br />Work with large size files; <br />Processing of 3d-models containing large number of triangles (the larger the number of triangles is, the more precise the models are); <br />Work of licensing subsystem; <br />Details: http://www.viva64.com/en/a/0036/<br />
  31. 31. Project size: <br />1.5Mb, 125files<br />Potentially dangerous constructions detected with the help of Viva64: 89<br />Of which real errors: 6<br />Statistics on detected erroros in Loki library<br />http://loki-lib.sourceforge.net<br />Details: http://www.viva64.com/en/a/0049/<br />
  32. 32. VivaMP, a rule set for parallel (OpenMP) software analysis<br />
  33. 33. Problems in the code of programs using OpenMP<br />Absence of keywords in directives<br />Incorrect operating with locking<br />Dependence of code behavior on the quantity of threads processing it<br />Simultaneous work with common resource<br />Vulnerable access to common memory<br />Careless use of local variables<br />Unnecessary memory protection from simultaneous writing<br />Performance errors <br />
  34. 34. Here are some samples of errors VivaMP can detect<br />
  35. 35. Non processed exceptions in parallel sections<br />#pragmaomp parallel for<br />for (size_t i = 0; i != n; ++i) {<br />float *array =new float[10000];<br /> delete [] array;<br />}<br />The example will lead to incorrect behavior of the program and most likely to its abnormal termination, if memory allotment error occurs.<br />The error is related to the exception throwing from the parallel section. According to OpenMP specification, if you use exceptions inside a parallel section,all these exceptions should be processed inside this section. If you use operator new inside a parallel section, you should consider seizing the exception which, according to C++ language standard will be generated at memory allotment error. <br />
  36. 36. Errors of inattentive use of OpenMP directives and functions<br />
  37. 37. Race condition errors<br />int a = 0;<br />#pragmaomp parallel for num_threads(4)<br />for (inti = 0; i < 100000; i++) {<br />a++;<br />}<br />Race condition happens when several threads of a multithread application attempt to simultaneously gain access to data, with that, at least one thread is making a record. Race conditions can lead to unpredictable results, and they are often hard to detect. Sometimes, race conditions consequences show only after a long period of time in quite another part of the application. Moreover, errors of such kind are extremely difficult to duplicate. <br />It is extremely effective to detect at least some of such errors with the help of static analysis as early as at the code writing stage.<br />
  38. 38. Errors of initialization of static objects in parallel sections<br />pragmaomp parallel<br />{<br /> static intst =<br /> Calc();<br /> ...<br />}<br />The static variable will start the process of initialization in several threads at once, this can lead to an uncertain result.The troubles of such errors consist in their unstable and rare manifestation during testing.<br />
  39. 39. General-purpose analysis rule set<br />
  40. 40. The main advantage of static analysis is that it can detect errors at the very early stages<br />Correlation of the cost on eliminating defects depending on the time they were introduced and found. The data for the table were taken from the book “Code complete” by S. Macconnell.<br />
  41. 41. It is beneficial to detect any type of errors at the coding stage<br />It is not crucial how complicated an error is: whether this is a mere misprint or error of algorithm’s logic. It is very beneficial to detect at least a part of such errors already at the stage of coding. It significantly reduces costs on code testing and maintenance.<br />The PVS-Studio analyzer diagnoses a lot of diverse types of errors. We cannot enumerate all the types of errors it can detect, so please refer to the documentation for the list of provided diagnoses.<br />Documentation (online): http://www.viva64.com/en/d/<br />
  42. 42. Here are samples of errors the general-purpose analyzer can detect<br />
  43. 43. Incorrect condition<br />intiChilds[2];<br /> ...<br />boolhasChilds() const { return(iChilds > 0 || iChilds > 0); }<br />Although this code successfully compiles without any warnings, it is meaningless in this case. The correct code must look as follows:<br />intiChilds[2];<br /> ...<br />boolhasChilds() const { return(iChilds[0] > 0 || iChilds[1] > 0); }<br />
  44. 44. Reference to an already destroyed object<br />structCVariable {<br /> char name[64];<br />};<br />void CRendererContext::RiGeometryV(int n, char *tokens[]){<br /> for (i=0;i<n;i++) {<br />CVariablevar;<br /> if (parseVariable(&var, NULL, tokens[i]))<br />tokens[i] = var.name;<br />}<br />}<br />The pointer to an array located in a variable of the CVariable type is saved in an external array. As a result, the "tokens" array will contain pointers to already non-existent objects as the RiGeometryVfunction terminates.<br />
  45. 45. Incomplete buffer clearing<br />MD5Context *ctx;<br />...<br />memset(ctx, 0, sizeof(ctx));<br />The misprint here causes the structure to be cleared only partially. The error in this code is that it is the size of the pointer which is calculated instead of the MD5Context structure’s size. This is the correct code:<br />MD5Context *ctx;<br />...<br />memset(ctx, 0, sizeof(*ctx));<br />
  46. 46. Error in the if - else - if - else chain<br />if (a == 1)<br /> Foo1();<br />else if (a == 2)<br /> Foo2();<br />else if (a == 1)<br /> Foo3();<br />The 'Foo3()' function will never get control.<br />
  47. 47. Misprint. Double assignment.<br />CSize(POINT pt) { cx = pt.x; cx = pt.y; }<br />The code is taken from a real application where the programmer implemented his own classCSize. The correct code certainly must have looked this way:<br />CSize(POINT pt) { cx = pt.x; cy = pt.y; }<br />Misprint. Unnecessary‘;’.<br />for (i = 0; i < n; i++);<br />{<br /> Foo(i);<br />}<br />
  48. 48. Incorrect use of std::remove<br />void unregisterThread() {<br /> Guard<TaskQueue> g(_taskQueue);<br /> std::remove(_threads.begin(),<br />_threads.end(),<br />ThreadImpl::current());<br />}<br />Thestd::remove function does not remove the items from the container. It only shifts the items and returns the iterator to the beginning of the trash. Assume we have the vector<int> container that contains items 1,2,3,1,2,3,1,2,3. If we execute the code "remove( v.begin(), v.end(), 2 )", the container will contain items 1,3,1,3,?,?,? where ? is some trash. Also, the function will return the iterator to the first trash item, so if we want to remove these trash items, we must write the following code: "v.erase(remove(v.begin(), v.end(), 2), v.end())".<br />
  49. 49. Licensing and pricing policy for PVS-Studio<br />
  50. 50. PVS-Studio:prices<br />Order Page: http://www.viva64.com/en/order/<br />
  51. 51. You are enabled to get new versions (including major-versions) during 1 year;<br />You are enabled to get support by e-mail during 1 year; <br />You get time unbounded right to use the program. A year after you purchased it, you can continue getting new versions of PVS-Studio and contacting the support service. Restrictions will only concern new diagnostic capabilities that will appear in the analyzer after your license has expired.<br />What does the price include besides the right of use?<br />
  52. 52. Information about OOO “Program Verification Systems” (Co Ltd)<br />
  53. 53. The certificate of official registration of computer programs N2007614164, "Viva64". Registered in Computer Program Registeron September 28th, 2007.<br />The certificate of official registration of computer programs N2008610480, "VivaCore, a source code analysis library". Registered in Computer Program Registeron January 25th, 2008.<br />The certificate of official registration of computer programs N2008612845, "Viva64 2.0". Registered in Computer Program Registeron May 29th, 2008.<br />Intellectual property is registered<br />
  54. 54. Our articles are published at largest sites for developers <br />http://www.viva64.com/en/experience/<br />
  55. 55. Common information on working with the PVS-Studio analyzerhttp://www.viva64.com/en/d/0011/<br />A Collection of Examples of 64-bit Errors in Real Programs<br />http://www.viva64.com/en/a/0065/<br />32 OpenMP Traps For C++ Developers<br />http://www.viva64.com/en/a/0054/<br />You can find other articles on 64-bit and parallel programs, as well as on technology of code analysis, at:http://www.viva64.com/en/articles/<br />Our best articles<br />
  56. 56. OOO “Program Verification Systems” (Co Ltd)<br /> 300027, Russia, Tula, Metallurgov70-1-88.<br />Web: www.viva64.com<br />E-mail: support@viva64.com<br />Phone: +7 (4872) 38-59-95<br />Working time: 09:00 – 18:00 (GMT +3:00)<br />Information about company<br />