From: Florian Krohm Date: Sat, 28 Jul 2012 22:20:03 +0000 (+0000) Subject: Testcases for CU42. Part of fixing bugzilla #289839. X-Git-Tag: svn/VALGRIND_3_8_0~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3630d97fcf697fccdb98b011f205971d7db9fd9e;p=thirdparty%2Fvalgrind.git Testcases for CU42. Part of fixing bugzilla #289839. Update opcode table. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@12804 --- diff --git a/docs/internals/s390-opcodes.csv b/docs/internals/s390-opcodes.csv index 3767b701f7..a459721344 100644 --- a/docs/internals/s390-opcodes.csv +++ b/docs/internals/s390-opcodes.csv @@ -667,7 +667,7 @@ mvcos,"move with optional specifications",N/A,"privileged instruction" lptea,"load page-table-entry address",N/A,"privileged instruction" cu24,"convert utf-16 to utf-32",implemented cu21,"convert utf-16 to utf-8",implemented -cu42,"convert utf-32 to utf-16","not implemented","open bugzilla" +cu42,"convert utf-32 to utf-16",implemented cu41,"convert utf-32 to utf-8","not implemented","open bugzilla" cu12,"convert utf-8 to utf-16","not implemented","open bugzilla" cu14,"convert utf-8 to utf-32","not implemented","open bugzilla" diff --git a/memcheck/tests/s390x/Makefile.am b/memcheck/tests/s390x/Makefile.am index b269fea5af..1fb8d7f03c 100644 --- a/memcheck/tests/s390x/Makefile.am +++ b/memcheck/tests/s390x/Makefile.am @@ -2,7 +2,7 @@ include $(top_srcdir)/Makefile.tool-tests.am dist_noinst_SCRIPTS = filter_stderr -INSN_TESTS = cs csg cds cdsg cu21 +INSN_TESTS = cs csg cds cdsg cu21 cu42 check_PROGRAMS = $(INSN_TESTS) diff --git a/memcheck/tests/s390x/cu42.c b/memcheck/tests/s390x/cu42.c new file mode 100644 index 0000000000..185b0dd84a --- /dev/null +++ b/memcheck/tests/s390x/cu42.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include "../../../none/tests/s390x/opcodes.h" + +/* Define various input buffers. */ + +/* U+0000 to U+d7ff: Result is 2 bytes for each uint32_t + U+dc00 to U+ffff: Result is 2 bytes for each uint32_t */ +uint32_t pattern2[] = { + 0x0000, 0xd7ff, /* corner cases */ + 0xdc00, 0xffff, /* corner cases */ + 0xabba, 0xf00d, 0xd00f, 0x1234 /* misc */ +}; + +/* U+00010000 to U+0010ffff: Result is 4 bytes for each uint32_t */ +uint32_t pattern4[] = { + 0x00010000, 0x0010ffff, /* corner cases */ + 0x00010123, 0x00023456, 0x000789ab, 0x00100000 /* misc */ +}; + +static void +do_cu42(uint16_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len) +{ + /* build up the register pairs */ + register uint32_t *source asm("4") = src; + register uint64_t source_len asm("5") = src_len; + register uint16_t *dest asm("2") = dst; + register uint64_t dest_len asm("3") = dst_len; + + asm volatile( + CU42(2,4) + : "+d"(dest), "+d"(source), "+d"(source_len), "+d"(dest_len) + : + : "memory", "cc"); +} + +int main() +{ + /*------------------------------------------------------------*/ + /* Write to a too small buffer */ + /*------------------------------------------------------------*/ + + /* Write 2 bytes into buffer of length 1 */ + do_cu42(malloc(1), 10, pattern2, 4); // complaint (2 bytes) + + /* Write 2 bytes into buffer of length 2 */ + do_cu42(malloc(2), 10, pattern2, 4); // no complaint + + /* Write 4 bytes into buffer of length 1 */ + do_cu42(malloc(1), 10, pattern4, 4); // complaint (4 bytes) + + /* Write 4 bytes into buffer of length 2 */ + do_cu42(malloc(2), 10, pattern4, 4); // complaint (4 bytes) + + /* Write 4 bytes into buffer of length 3 */ + do_cu42(malloc(3), 10, pattern4, 4); // complaint (4 bytes) + + /* Write 4 bytes into buffer of length 4 */ + do_cu42(malloc(4), 10, pattern4, 4); // no complaint + + /*------------------------------------------------------------*/ + /* Read uninitialised data */ + /*------------------------------------------------------------*/ + uint16_t buf[100]; + uint8_t *input; + + /* Input buffer is completely uninitialised */ + input = malloc(10); + do_cu42(buf, sizeof buf, (void *)input, 4); // complaint + + /* Read 4 bytes from input buffer. First byte is uninitialised */ + input = malloc(10); + input[1] = input[2] = input[3] = 0x0; + do_cu42(buf, sizeof buf, (void *)input, 4); // complaint + + /* Read 4 bytes from input buffer. Second byte is uninitialised */ + input = malloc(10); + input[0] = input[2] = input[3] = 0x0; + do_cu42(buf, sizeof buf, (void *)input, 4); // complaint + + /* Read 4 bytes from input buffer. Third byte is uninitialised */ + input = malloc(10); + input[0] = input[1] = input[3] = 0x0; + do_cu42(buf, sizeof buf, (void *)input, 4); // complaint + + /* Read 4 bytes from input buffer. Fourth byte is uninitialised */ + input = malloc(10); + input[0] = input[1] = input[2] = 0x0; + do_cu42(buf, sizeof buf, (void *)input, 4); // complaint + + /* Read 4 bytes from input buffer. All bytes are initialised */ + input = malloc(10); + memset(input, 0, 4); + do_cu42(buf, sizeof buf, (void *)input, 4); // no complaint + + /* Read 8 bytes from input buffer. This iterates once. In the 1st + iteration all input bytes are initialised in the 2nd iteration all + input bytes are uninitialised. */ + input = malloc(10); + memset(input, 0, 4); + do_cu42(buf, sizeof buf, (void *)input, 8); // complaint + + + /* Write to NULL */ + // do_cu42(NULL, 10, pattern1, sizeof pattern1); // complaint + + return 0; +} diff --git a/memcheck/tests/s390x/cu42.stderr.exp b/memcheck/tests/s390x/cu42.stderr.exp new file mode 100644 index 0000000000..246b22a403 --- /dev/null +++ b/memcheck/tests/s390x/cu42.stderr.exp @@ -0,0 +1,52 @@ +Invalid write of size 2 + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:47) + Address 0x........ is 0 bytes inside a block of size 1 alloc'd + at 0x........: malloc (vg_replace_malloc.c:...) + by 0x........: main (cu42.c:47) + +Invalid write of size 4 + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:53) + Address 0x........ is 0 bytes inside a block of size 1 alloc'd + at 0x........: malloc (vg_replace_malloc.c:...) + by 0x........: main (cu42.c:53) + +Invalid write of size 4 + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:56) + Address 0x........ is 0 bytes inside a block of size 2 alloc'd + at 0x........: malloc (vg_replace_malloc.c:...) + by 0x........: main (cu42.c:56) + +Invalid write of size 4 + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:59) + Address 0x........ is 0 bytes inside a block of size 3 alloc'd + at 0x........: malloc (vg_replace_malloc.c:...) + by 0x........: main (cu42.c:59) + +Conditional jump or move depends on uninitialised value(s) + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:72) + +Conditional jump or move depends on uninitialised value(s) + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:77) + +Conditional jump or move depends on uninitialised value(s) + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:82) + +Conditional jump or move depends on uninitialised value(s) + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:87) + +Conditional jump or move depends on uninitialised value(s) + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:92) + +Conditional jump or move depends on uninitialised value(s) + at 0x........: do_cu42 (cu42.c:31) + by 0x........: main (cu42.c:104) + diff --git a/memcheck/tests/s390x/cu42.stdout.exp b/memcheck/tests/s390x/cu42.stdout.exp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/memcheck/tests/s390x/cu42.vgtest b/memcheck/tests/s390x/cu42.vgtest new file mode 100644 index 0000000000..c1d9551019 --- /dev/null +++ b/memcheck/tests/s390x/cu42.vgtest @@ -0,0 +1,2 @@ +prog: cu42 +vgopts: -q diff --git a/none/tests/s390x/Makefile.am b/none/tests/s390x/Makefile.am index d3e7ca0281..e4c73194b0 100644 --- a/none/tests/s390x/Makefile.am +++ b/none/tests/s390x/Makefile.am @@ -7,7 +7,7 @@ INSN_TESTS = clc clcle cvb cvd icm lpr tcxb lam_stam xc mvst add sub mul \ and_EI or_EI xor_EI insert_EI mul_GE add_GE condloadstore \ op_exception fgx stck stckf stcke stfle cksm mvcl clcl troo \ trto trot trtt tr tre cij cgij clij clgij crj cgrj clrj clgrj \ - cs csg cds cdsg cu21 cu21_1 cu24 cu24_1 + cs csg cds cdsg cu21 cu21_1 cu24 cu24_1 cu42 check_PROGRAMS = $(INSN_TESTS) \ allexec \ diff --git a/none/tests/s390x/cu42.c b/none/tests/s390x/cu42.c new file mode 100644 index 0000000000..015700fb2a --- /dev/null +++ b/none/tests/s390x/cu42.c @@ -0,0 +1,227 @@ +#include +#include +#include +#include +#include +#include +#include "opcodes.h" + +/* The abstracted result of an CU42 insn */ +typedef struct { + uint64_t addr1; // target + uint64_t len1; + uint64_t addr2; // source + uint64_t len2; + uint32_t cc; +} cu42_t; + +/* Define various input buffers. */ + +/* U+0000 to U+d7ff: Result is 2 bytes for each uint32_t + U+dc00 to U+ffff: Result is 2 bytes for each uint32_t */ +uint32_t pattern2[] = { + 0x0000, 0xd7ff, /* corner cases */ + 0xdc00, 0xffff, /* corner cases */ + 0xabba, 0xf00d, 0xd00f, 0x1234 /* misc */ +}; + +/* U+00010000 to U+0010ffff: Result is 4 bytes for each uint32_t */ +uint32_t pattern4[] = { + 0x00010000, 0x0010ffff, /* corner cases */ + 0x00010123, 0x00023456, 0x000789ab, 0x00100000 /* misc */ +}; + +/* Invalid UTF-32 character */ +uint32_t invalid[] = { + 0x0000d800, 0x0000dbff, /* corner cases */ + 0x00110000, 0xffffffff, /* corner cases */ + 0x0000daad, 0x0000d901, 0x0000dddd, /* misc */ + 0x00110011, 0x01000000, 0x10000000, 0xdeadbeef /* misc */ +}; + +/* Mixed bytes */ +uint32_t mixed[] = { + 0x00000078 /* 2 bytes */, + 0x0000d000 /* 2 bytes */, + 0x00033333 /* 4 bytes */, + 0x00040404 /* 4 bytes */, + 0x0000abcd /* 2 bytes */, +}; + +/* This is the buffer for the converted bytes. */ +uint16_t buff[1000]; /* Large so we con'don't have to worry about it */ + +void write_and_check(uint32_t *, unsigned, unsigned); + + +static cu42_t +do_cu42(uint16_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len) +{ + int cc = 42; + cu42_t regs; + + /* build up the register pairs */ + register uint32_t *source asm("4") = src; + register uint64_t source_len asm("5") = src_len; + register uint16_t *dest asm("2") = dst; + register uint64_t dest_len asm("3") = dst_len; + + asm volatile( + CU42(2,4) + "ipm %2\n\t" + "srl %2,28\n\t" + : "+d"(dest), "+d"(source), "=d"(cc), + "+d"(source_len), "+d"(dest_len) + : + : "memory", "cc"); + + /* Capture register contents at end of cu42 */ + regs.addr1 = (uint64_t)dest; + regs.len1 = dest_len; + regs.addr2 = (uint64_t)source; + regs.len2 = source_len; + regs.cc = cc; + + return regs; +} + +void +run_test(uint16_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len) +{ + int i; + cu42_t result; + + result = do_cu42(dst, dst_len, src, src_len); + + // Write out the converted values, if any + printf("UTF16: "); + if (dst_len - result.len1 == 0) + printf(" "); + else + assert((dst_len - result.len1) % 2 == 0); + for (i = 0; i < (dst_len - result.len1) / 2; ++i) { + printf(" %04x", dst[i]); + } + printf("\n"); + + printf(" cc = %d\n", result.cc); + if (dst != NULL) + printf(" dst address difference: %"PRId64, result.addr1 - (uint64_t)dst); + printf(" dst len: %"PRId64"\n", result.len1); + + if (src != NULL) + printf(" src address difference: %"PRId64, result.addr2 - (uint64_t)src); + printf(" src len: %"PRId64"\n", result.len2); +} + +int main() +{ + /* Length == 0, no memory should be read or written */ + printf("\n------------- test1 ----------------\n"); + run_test(NULL, 0, NULL, 0); + + /* Test exhaustion of source length (source bytes are valid) */ + printf("\n------------- test2.1 ----------------\n"); + + /* No character will be written to BUFF, i.e. loop in jitted code + is not iterated */ + run_test(buff, sizeof buff, NULL, 0); + run_test(buff, sizeof buff, NULL, 1); + run_test(buff, sizeof buff, NULL, 2); + run_test(buff, sizeof buff, NULL, 3); + run_test(buff, sizeof buff, pattern2, 0); + run_test(buff, sizeof buff, pattern2, 1); + run_test(buff, sizeof buff, pattern2, 2); + run_test(buff, sizeof buff, pattern2, 3); + + printf("\n------------- test2.2 ----------------\n"); + /* At least one character will be written to BUFF, i.e. loop in jitted + code is iterated */ + run_test(buff, sizeof buff, pattern2, 4); /* 1 utf32 -> 1 utf16 */ + run_test(buff, sizeof buff, pattern2, 10); /* 2 utf32 -> 2 utf16 */ + run_test(buff, sizeof buff, pattern4, 5); /* 1 utf32 -> 2 utf16 */ + run_test(buff, sizeof buff, pattern4, 11); /* 2 utf32 -> 4 utf16 */ + run_test(buff, sizeof buff, pattern4, 18); /* 4 utf32 -> 8 utf16 */ + + /* Test exhaustion of destination length (source bytes are valid) */ + printf("\n------------- test3.1 ----------------\n"); + + /* No character will be written to BUFF, i.e. loop in jitted code + is not iterated */ + + /* Want to write at least 1 UTF-16 */ + run_test(NULL, 0, pattern2, sizeof pattern2); + + /* Want to write at least 1 UTF-16 */ + run_test(NULL, 0, pattern2, sizeof pattern2); + run_test(NULL, 1, pattern2, sizeof pattern2); + + /* Want to write at least 2 UTF-16 */ + run_test(NULL, 0, pattern4, sizeof pattern4); + run_test(NULL, 1, pattern4, sizeof pattern4); + run_test(NULL, 2, pattern4, sizeof pattern4); + run_test(NULL, 3, pattern4, sizeof pattern4); + + /* When both operands are exhausted, cc=0 takes precedence. + (test1 tests this for len == 0) */ + printf("\n------------- test4 ----------------\n"); + run_test(buff, 4, pattern2, 8); + + /* Input contains invalid characters */ + printf("\n------------- test5 ----------------\n"); + run_test(buff, sizeof buff, invalid, sizeof invalid); + run_test(buff, 0, invalid, sizeof invalid); // cc = 2 + run_test(buff, 100, invalid, sizeof invalid); + + /* Convert all pattern buffers */ + printf("\n------------- test6 ----------------\n"); + run_test(buff, sizeof buff, pattern2, sizeof pattern2); + run_test(buff, sizeof buff, pattern4, sizeof pattern4); + run_test(buff, sizeof buff, mixed, sizeof mixed); + + /* Make sure we only write the exact number of bytes (and not more) */ + + /* Write 2 bytes */ + printf("\n------------- test7.1 ----------------\n"); + write_and_check(pattern2 + 3, 4, 2); + + /* Write 4 bytes */ + printf("\n------------- test7.2 ----------------\n"); + write_and_check(pattern4 + 5, 4, 4); + + return 0; +} + + +void +write_and_check_aux(uint32_t *input, unsigned num_input_bytes, + unsigned num_expected_output_bytes, + unsigned fill_byte) +{ + int num_errors, i; + + /* Fill output buffer with FILL_BYTE */ + memset(buff, fill_byte, sizeof buff); + + /* Execute cu42 */ + run_test(buff, sizeof buff, input, num_input_bytes); + + /* Make sure the rest of the buffer is unmodified. */ + num_errors = 0; + for (i = num_expected_output_bytes; i < sizeof buff; ++i) + if (((unsigned char *)buff)[i] != fill_byte) ++num_errors; + if (num_errors) + fprintf(stderr, "*** wrote more than %d bytes\n", + num_expected_output_bytes); +} + +void +write_and_check(uint32_t *input, unsigned num_input_bytes, + unsigned num_expected_output_bytes) +{ + write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0x0); + + /* Run again with different fill pattern to make sure we did not write + an extra 0x0 byte */ + write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0xFF); +} diff --git a/none/tests/s390x/cu42.stderr.exp b/none/tests/s390x/cu42.stderr.exp new file mode 100644 index 0000000000..139597f9cb --- /dev/null +++ b/none/tests/s390x/cu42.stderr.exp @@ -0,0 +1,2 @@ + + diff --git a/none/tests/s390x/cu42.stdout.exp b/none/tests/s390x/cu42.stdout.exp new file mode 100644 index 0000000000..e6269918a6 --- /dev/null +++ b/none/tests/s390x/cu42.stdout.exp @@ -0,0 +1,146 @@ + +------------- test1 ---------------- +UTF16: + cc = 0 + dst len: 0 + src len: 0 + +------------- test2.1 ---------------- +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src len: 0 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src len: 1 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src len: 2 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src len: 3 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src address difference: 0 src len: 0 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src address difference: 0 src len: 1 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src address difference: 0 src len: 2 +UTF16: + cc = 0 + dst address difference: 0 dst len: 2000 + src address difference: 0 src len: 3 + +------------- test2.2 ---------------- +UTF16: 0000 + cc = 0 + dst address difference: 2 dst len: 1998 + src address difference: 4 src len: 0 +UTF16: 0000 d7ff + cc = 0 + dst address difference: 4 dst len: 1996 + src address difference: 8 src len: 2 +UTF16: d800 dc00 + cc = 0 + dst address difference: 4 dst len: 1996 + src address difference: 4 src len: 1 +UTF16: d800 dc00 dbff dfff + cc = 0 + dst address difference: 8 dst len: 1992 + src address difference: 8 src len: 3 +UTF16: d800 dc00 dbff dfff d800 dd23 d84d dc56 + cc = 0 + dst address difference: 16 dst len: 1984 + src address difference: 16 src len: 2 + +------------- test3.1 ---------------- +UTF16: + cc = 1 + dst len: 0 + src address difference: 0 src len: 32 +UTF16: + cc = 1 + dst len: 0 + src address difference: 0 src len: 32 +UTF16: + cc = 1 + dst len: 1 + src address difference: 0 src len: 32 +UTF16: + cc = 1 + dst len: 0 + src address difference: 0 src len: 24 +UTF16: + cc = 1 + dst len: 1 + src address difference: 0 src len: 24 +UTF16: + cc = 1 + dst len: 2 + src address difference: 0 src len: 24 +UTF16: + cc = 1 + dst len: 3 + src address difference: 0 src len: 24 + +------------- test4 ---------------- +UTF16: 0000 d7ff + cc = 0 + dst address difference: 4 dst len: 0 + src address difference: 8 src len: 0 + +------------- test5 ---------------- +UTF16: + cc = 2 + dst address difference: 0 dst len: 2000 + src address difference: 0 src len: 44 +UTF16: + cc = 2 + dst address difference: 0 dst len: 0 + src address difference: 0 src len: 44 +UTF16: + cc = 2 + dst address difference: 0 dst len: 100 + src address difference: 0 src len: 44 + +------------- test6 ---------------- +UTF16: 0000 d7ff dc00 ffff abba f00d d00f 1234 + cc = 0 + dst address difference: 16 dst len: 1984 + src address difference: 32 src len: 0 +UTF16: d800 dc00 dbff dfff d800 dd23 d84d dc56 d9a2 ddab dbc0 dc00 + cc = 0 + dst address difference: 24 dst len: 1976 + src address difference: 24 src len: 0 +UTF16: 0078 d000 d88c df33 d8c1 dc04 abcd + cc = 0 + dst address difference: 14 dst len: 1986 + src address difference: 20 src len: 0 + +------------- test7.1 ---------------- +UTF16: ffff + cc = 0 + dst address difference: 2 dst len: 1998 + src address difference: 4 src len: 0 +UTF16: ffff + cc = 0 + dst address difference: 2 dst len: 1998 + src address difference: 4 src len: 0 + +------------- test7.2 ---------------- +UTF16: dbc0 dc00 + cc = 0 + dst address difference: 4 dst len: 1996 + src address difference: 4 src len: 0 +UTF16: dbc0 dc00 + cc = 0 + dst address difference: 4 dst len: 1996 + src address difference: 4 src len: 0 diff --git a/none/tests/s390x/cu42.vgtest b/none/tests/s390x/cu42.vgtest new file mode 100644 index 0000000000..da601bc344 --- /dev/null +++ b/none/tests/s390x/cu42.vgtest @@ -0,0 +1 @@ +prog: cu42