• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <inttypes.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include "../../../none/tests/s390x/opcodes.h"
7 
8 /* Define various input buffers. */
9 
10 /* U+0000 to U+d7ff:  Result is 2 bytes for each uint32_t
11    U+dc00 to U+ffff:  Result is 2 bytes for each uint32_t */
12 uint32_t pattern2[] = {
13    0x0000, 0xd7ff,    /* corner cases */
14    0xdc00, 0xffff,    /* corner cases */
15    0xabba, 0xf00d, 0xd00f, 0x1234 /* misc */
16 };
17 
18 /* U+00010000 to U+0010ffff:  Result is 4 bytes for each uint32_t */
19 uint32_t pattern4[] = {
20    0x00010000, 0x0010ffff,    /* corner cases */
21    0x00010123, 0x00023456, 0x000789ab, 0x00100000  /* misc */
22 };
23 
24 static void
do_cu42(uint16_t * dst,uint64_t dst_len,uint32_t * src,uint64_t src_len)25 do_cu42(uint16_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len)
26 {
27    /* build up the register pairs */
28    register uint32_t *source     asm("4") = src;
29    register uint64_t  source_len asm("5") = src_len;
30    register uint16_t *dest       asm("2") = dst;
31    register uint64_t  dest_len   asm("3") = dst_len;
32 
33    asm volatile(
34                 CU42(2,4)
35                 : "+d"(dest), "+d"(source), "+d"(source_len), "+d"(dest_len)
36                 :
37                 : "memory", "cc");
38 }
39 
main()40 int main()
41 {
42    /*------------------------------------------------------------*/
43    /* Write to a too small buffer                                */
44    /*------------------------------------------------------------*/
45 
46    /* Write 2 bytes into buffer of length 1 */
47    do_cu42(malloc(1), 10, pattern2, 4);             // complaint (2 bytes)
48 
49    /* Write 2 bytes into buffer of length 2 */
50    do_cu42(malloc(2), 10, pattern2, 4);             // no complaint
51 
52    /* Write 4 bytes into buffer of length 1 */
53    do_cu42(malloc(1), 10, pattern4, 4);             // complaint (4 bytes)
54 
55    /* Write 4 bytes into buffer of length 2 */
56    do_cu42(malloc(2), 10, pattern4, 4);             // complaint (4 bytes)
57 
58    /* Write 4 bytes into buffer of length 3 */
59    do_cu42(malloc(3), 10, pattern4, 4);             // complaint (4 bytes)
60 
61    /* Write 4 bytes into buffer of length 4 */
62    do_cu42(malloc(4), 10, pattern4, 4);             // no complaint
63 
64    /*------------------------------------------------------------*/
65    /* Read uninitialised data                                    */
66    /*------------------------------------------------------------*/
67    uint16_t buf[100];
68    uint8_t *input;
69 
70    /* Input buffer is completely uninitialised */
71    input = malloc(10);
72    do_cu42(buf, sizeof buf, (void *)input, 4);         // complaint
73 
74    /* Read 4 bytes from input buffer. First byte is uninitialised */
75    input = malloc(10);
76    input[1] = input[2] = input[3] = 0x0;
77    do_cu42(buf, sizeof buf, (void *)input, 4);          // complaint
78 
79    /* Read 4 bytes from input buffer. Second byte is uninitialised */
80    input = malloc(10);
81    input[0] = input[2] = input[3] = 0x0;
82    do_cu42(buf, sizeof buf, (void *)input, 4);          // complaint
83 
84    /* Read 4 bytes from input buffer. Third byte is uninitialised */
85    input = malloc(10);
86    input[0] = input[1] = input[3] = 0x0;
87    do_cu42(buf, sizeof buf, (void *)input, 4);          // complaint
88 
89    /* Read 4 bytes from input buffer. Fourth byte is uninitialised */
90    input = malloc(10);
91    input[0] = input[1] = input[2] = 0x0;
92    do_cu42(buf, sizeof buf, (void *)input, 4);          // complaint
93 
94    /* Read 4 bytes from input buffer. All bytes are initialised */
95    input = malloc(10);
96    memset(input, 0, 4);
97    do_cu42(buf, sizeof buf, (void *)input, 4);          // no complaint
98 
99    /* Read 8 bytes from input buffer. This iterates once. In the 1st
100       iteration all input bytes are initialised in the 2nd iteration all
101       input bytes are uninitialised. */
102    input = malloc(10);
103    memset(input, 0, 4);
104    do_cu42(buf, sizeof buf, (void *)input, 8);          // complaint
105 
106 
107    /* Write to NULL */
108    //   do_cu42(NULL, 10, pattern1, sizeof pattern1);    // complaint
109 
110    return 0;
111 }
112