File: origin2-not-quite.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (60 lines) | stat: -rw-r--r-- 1,648 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

/* This test case was originally written by Nicholas Nethercote. */

// This test demonstrates some cases that the piggybacking algorithm
// doesn't handle but conceivably might, with more modifications.
// The instrumentation based algorithm handles them ok, though.

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

int x = 0;

typedef long long Long;

__attribute__((noinline)) int t1(void);
__attribute__((noinline)) int t2(void);
__attribute__((noinline)) int t3(void);

int main(void)
{
   assert(4 == sizeof(int));
   assert(8 == sizeof(Long));

   x += t1();
   x += t2();
   x += t3();

   return x & 255;
}
   
__attribute__((noinline)) int t1(void)
{
   // 64-bit undefined double.
   double* ptr_to_undef_double = malloc(sizeof(double));
   double  undef_double = *ptr_to_undef_double;
   fprintf(stderr, "\nUndef 1 of 3 (64-bit FP)\n");
   return (undef_double < (double)123.45 ? 12 : 23);
}

__attribute__((noinline)) int t2(void)
{
   // 32-bit undefined float.
   float* ptr_to_undef_float = malloc(sizeof(float));
   float undef_float = *ptr_to_undef_float;
   fprintf(stderr, "\nUndef 2 of 3 (32-bit FP)\n");
   return (undef_float < (float)234.56  ? 13 : 24);
}

__attribute__((noinline)) int t3(void)
{
   // Stack, 32-bit, recently modified.
   // Problem here is that we don't chase backwards through loads and
   // stores.  Ie. the variable is stored after it's been modified, then
   // loaded again, so we don't see the unmodified version.
   int modified_undef_stack_int;
   modified_undef_stack_int++;
   fprintf(stderr, "\nUndef 3 of 3 (int)\n");
   return (modified_undef_stack_int == 0x1234 ? 11 : 22);
}