]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/exp-ptrcheck/tests/bad_percentify.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / exp-ptrcheck / tests / bad_percentify.c
1
2 /* This demonstrates a stack overrun bug that exp-ptrcheck found while
3    running Valgrind itself (self hosting).  As at 12 Sept 08 this bug
4    is still in Valgrind. */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <string.h>
10
11 typedef  unsigned long long int  ULong;
12 typedef  unsigned int            UInt;
13 typedef  signed int              Int;
14 typedef  char             Char;
15
16 /* ---------------------------------------------------------------------
17    percentify()
18    ------------------------------------------------------------------ */
19
20 /* This part excerpted from coregrind/m_libcbase.c */
21
22 // Percentify n/m with d decimal places.  Includes the '%' symbol at the end.
23 // Right justifies in 'buf'.
24 void VG_percentify(ULong n, ULong m, UInt d, Int n_buf, char buf[]) 
25 {
26    Int i, len, space;
27    ULong p1;
28    Char fmt[32];
29
30    if (m == 0) {
31       // Have to generate the format string in order to be flexible about
32       // the width of the field.
33       sprintf(fmt, "%%-%ds", n_buf);
34       // fmt is now "%<n_buf>s" where <d> is 1,2,3...
35       sprintf(buf, fmt, "--%");
36       return;
37    }
38    
39    p1 = (100*n) / m;
40     
41    if (d == 0) {
42       sprintf(buf, "%lld%%", p1);
43    } else {
44       ULong p2;
45       UInt  ex;
46       switch (d) {
47       case 1: ex = 10;    break;
48       case 2: ex = 100;   break;
49       case 3: ex = 1000;  break;
50       default: assert(0);
51       /* was: VG_(tool_panic)("Currently can only handle 3 decimal places"); */
52       }
53       p2 = ((100*n*ex) / m) % ex;
54       // Have to generate the format string in order to be flexible about
55       // the width of the post-decimal-point part.
56       sprintf(fmt, "%%lld.%%0%dlld%%%%", d);
57       // fmt is now "%lld.%0<d>lld%%" where <d> is 1,2,3...
58       sprintf(buf, fmt, p1, p2);
59    }
60
61    len = strlen(buf);
62    space = n_buf - len;
63    if (space < 0) space = 0;     /* Allow for v. small field_width */
64    i = len;
65
66    /* Right justify in field */
67    for (     ; i >= 0;    i--)  buf[i + space] = buf[i];
68    for (i = 0; i < space; i++)  buf[i] = ' ';
69 }
70
71
72 /*------------------------------------------------------------*/
73 /*--- Stats                                                ---*/
74 /*------------------------------------------------------------*/
75
76 /* This part excerpted from coregrind/m_translate.c */
77
78 static UInt n_SP_updates_fast            = 0;
79 static UInt n_SP_updates_generic_known   = 0;
80 static UInt n_SP_updates_generic_unknown = 0;
81
82 void VG_print_translation_stats ( void )
83 {
84    Char buf[6];
85    UInt n_SP_updates = n_SP_updates_fast + n_SP_updates_generic_known
86                                          + n_SP_updates_generic_unknown;
87    VG_percentify(n_SP_updates_fast, n_SP_updates, 1, 6, buf);
88    printf(
89       "translate:            fast SP updates identified: %'u (%s)\n",
90       n_SP_updates_fast, buf );
91
92    VG_percentify(n_SP_updates_generic_known, n_SP_updates, 1, 6, buf);
93    printf(
94       "translate:   generic_known SP updates identified: %'u (%s)\n",
95       n_SP_updates_generic_known, buf );
96
97    VG_percentify(n_SP_updates_generic_unknown, n_SP_updates, 1, 6, buf);
98    printf(
99       "translate: generic_unknown SP updates identified: %'u (%s)\n",
100       n_SP_updates_generic_unknown, buf );
101 }
102
103
104
105 int main ( void )
106 {
107   VG_print_translation_stats();
108   return 0;
109 }