| 1 |
/* { dg-require-effective-target lp64 } |
| 2 |
Misbehaves with -m32 due to optimization turning the pointer arithmetic into: |
| 3 |
_2 = &buf + 4294967246; |
| 4 |
memcpy (_2, _1, 4096); |
| 5 |
*/ |
| 6 |
|
| 7 |
/* { dg-additional-options "-fdiagnostics-text-art-charset=unicode" } */ |
| 8 |
|
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#define LOREM_IPSUM \ |
| 12 |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod" \ |
| 13 |
" tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim" \ |
| 14 |
" veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea" \ |
| 15 |
" commodo consequat. Duis aute irure dolor in reprehenderit in voluptate" \ |
| 16 |
" velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint" \ |
| 17 |
" occaecat cupidatat non proident, sunt in culpa qui officia deserunt" \ |
| 18 |
" mollit anim id est laborum." |
| 19 |
|
| 20 |
/* This memcpy reads from both before and after the bounds of the |
| 21 |
string literal, and writes to both before and after the bounds of "buf". */ |
| 22 |
|
| 23 |
void |
| 24 |
test_bad_memcpy () |
| 25 |
{ |
| 26 |
char buf[100]; |
| 27 |
memcpy (buf - 50, LOREM_IPSUM - 100, 4096); /* { dg-warning "stack-based buffer overflow" } */ |
| 28 |
/* { dg-warning "stack-based buffer underwrite" "" { target *-*-* } .-1 } */ |
| 29 |
/* { dg-warning "buffer under-read" "" { target *-*-* } .-2 } */ |
| 30 |
/* { dg-warning "buffer over-read" "" { target *-*-* } .-3 } */ |
| 31 |
/* { dg-warning "'memcpy' writing 4096 bytes into a region of size 0 overflows the destination" "" { target *-*-* } .-4 } */ |
| 32 |
} |
| 33 |
|
| 34 |
/* { dg-begin-multiline-output "" } |
| 35 |
|
| 36 |
┌────────────────────────────────────────────────────────────────────────────────────────────┐ |
| 37 |
│ read of 4096 bytes │ |
| 38 |
└────────────────────────────────────────────────────────────────────────────────────────────┘ |
| 39 |
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
| 40 |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 41 |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 42 |
┌──────────────────┐┌───┬───────────────────────────────────────────┬─────┐┌─────────────────┐ |
| 43 |
│ ││[0]│ ... │[445]││ │ |
| 44 |
│ │├───┼───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┼─────┤│ │ |
| 45 |
│before valid range││'L'│'o'│'r'│'e'│'m'│' '│...│'o'│'r'│'u'│'m'│'.'│ NUL ││after valid range│ |
| 46 |
│ │├───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─────┤│ │ |
| 47 |
│ ││ string literal (type: 'char[446]') ││ │ |
| 48 |
└──────────────────┘└─────────────────────────────────────────────────────┘└─────────────────┘ |
| 49 |
├────────┬─────────┤├──────────────────────────┬──────────────────────────┤├────────┬────────┤ |
| 50 |
│ │ │ |
| 51 |
╭────────┴──────────────╮ ╭───────┴───────╮ ╭───────────┴───────────╮ |
| 52 |
│under-read of 100 bytes│ │size: 446 bytes│ │over-read of 3550 bytes│ |
| 53 |
╰───────────────────────╯ ╰───────────────╯ ╰───────────────────────╯ |
| 54 |
|
| 55 |
{ dg-end-multiline-output "" } */ |
| 56 |
|
| 57 |
/* { dg-begin-multiline-output "" } |
| 58 |
|
| 59 |
┌──────────────────────────────────────────────────────────────────────┐ |
| 60 |
│ write of 4096 bytes │ |
| 61 |
└──────────────────────────────────────────────────────────────────────┘ |
| 62 |
│ │ │ │ │ |
| 63 |
│ │ │ │ │ |
| 64 |
v v v v v |
| 65 |
┌──────────────────┐┌───────┬───────┬─────────┐┌───────────────────────┐ |
| 66 |
│ ││ [0] │ ... │ [99] ││ │ |
| 67 |
│before valid range│├───────┴───────┴─────────┤│ after valid range │ |
| 68 |
│ ││'buf' (type: 'char[100]')││ │ |
| 69 |
└──────────────────┘└─────────────────────────┘└───────────────────────┘ |
| 70 |
├────────┬─────────┤├────────────┬────────────┤├───────────┬───────────┤ |
| 71 |
│ │ │ |
| 72 |
│ ╭─────────┴─────────╮ ╭───────────┴──────────╮ |
| 73 |
│ │capacity: 100 bytes│ │overflow of 3946 bytes│ |
| 74 |
│ ╰───────────────────╯ ╰──────────────────────╯ |
| 75 |
╭────────┴─────────────╮ |
| 76 |
│underwrite of 50 bytes│ |
| 77 |
╰──────────────────────╯ |
| 78 |
|
| 79 |
{ dg-end-multiline-output "" } */ |
| 80 |
|
| 81 |
/* The read and write diagrams are each emitted twice: once for the "before" |
| 82 |
and once for the "after" diagnostic. */ |
| 83 |
|
| 84 |
/* { dg-begin-multiline-output "" } |
| 85 |
|
| 86 |
┌────────────────────────────────────────────────────────────────────────────────────────────┐ |
| 87 |
│ read of 4096 bytes │ |
| 88 |
└────────────────────────────────────────────────────────────────────────────────────────────┘ |
| 89 |
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
| 90 |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 91 |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 92 |
┌──────────────────┐┌───┬───────────────────────────────────────────┬─────┐┌─────────────────┐ |
| 93 |
│ ││[0]│ ... │[445]││ │ |
| 94 |
│ │├───┼───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┼─────┤│ │ |
| 95 |
│before valid range││'L'│'o'│'r'│'e'│'m'│' '│...│'o'│'r'│'u'│'m'│'.'│ NUL ││after valid range│ |
| 96 |
│ │├───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─────┤│ │ |
| 97 |
│ ││ string literal (type: 'char[446]') ││ │ |
| 98 |
└──────────────────┘└─────────────────────────────────────────────────────┘└─────────────────┘ |
| 99 |
├────────┬─────────┤├──────────────────────────┬──────────────────────────┤├────────┬────────┤ |
| 100 |
│ │ │ |
| 101 |
╭────────┴──────────────╮ ╭───────┴───────╮ ╭───────────┴───────────╮ |
| 102 |
│under-read of 100 bytes│ │size: 446 bytes│ │over-read of 3550 bytes│ |
| 103 |
╰───────────────────────╯ ╰───────────────╯ ╰───────────────────────╯ |
| 104 |
|
| 105 |
{ dg-end-multiline-output "" } */ |
| 106 |
|
| 107 |
/* { dg-begin-multiline-output "" } |
| 108 |
|
| 109 |
┌──────────────────────────────────────────────────────────────────────┐ |
| 110 |
│ write of 4096 bytes │ |
| 111 |
└──────────────────────────────────────────────────────────────────────┘ |
| 112 |
│ │ │ │ │ |
| 113 |
│ │ │ │ │ |
| 114 |
v v v v v |
| 115 |
┌──────────────────┐┌───────┬───────┬─────────┐┌───────────────────────┐ |
| 116 |
│ ││ [0] │ ... │ [99] ││ │ |
| 117 |
│before valid range│├───────┴───────┴─────────┤│ after valid range │ |
| 118 |
│ ││'buf' (type: 'char[100]')││ │ |
| 119 |
└──────────────────┘└─────────────────────────┘└───────────────────────┘ |
| 120 |
├────────┬─────────┤├────────────┬────────────┤├───────────┬───────────┤ |
| 121 |
│ │ │ |
| 122 |
│ ╭─────────┴─────────╮ ╭───────────┴──────────╮ |
| 123 |
│ │capacity: 100 bytes│ │overflow of 3946 bytes│ |
| 124 |
│ ╰───────────────────╯ ╰──────────────────────╯ |
| 125 |
╭────────┴─────────────╮ |
| 126 |
│underwrite of 50 bytes│ |
| 127 |
╰──────────────────────╯ |
| 128 |
|
| 129 |
{ dg-end-multiline-output "" } */ |