|
From: <sv...@va...> - 2005-07-07 13:20:33
|
Author: sewardj
Date: 2005-07-07 14:20:31 +0100 (Thu, 07 Jul 2005)
New Revision: 4125
Log:
Self-modifying-code check for amd64.
Added:
trunk/none/tests/amd64/smc1.c
trunk/none/tests/amd64/smc1.stderr.exp
trunk/none/tests/amd64/smc1.stdout.exp
trunk/none/tests/amd64/smc1.vgtest
Modified:
trunk/none/tests/amd64/Makefile.am
Modified: trunk/none/tests/amd64/Makefile.am
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/amd64/Makefile.am 2005-07-07 11:32:37 UTC (rev 4124)
+++ trunk/none/tests/amd64/Makefile.am 2005-07-07 13:20:31 UTC (rev 4125)
@@ -7,10 +7,11 @@
EXTRA_DIST =3D $(noinst_SCRIPTS) \
$(addsuffix .stderr.exp,$(INSN_TESTS)) \
$(addsuffix .stdout.exp,$(INSN_TESTS)) \
- $(addsuffix .vgtest,$(INSN_TESTS))
+ $(addsuffix .vgtest,$(INSN_TESTS)) \
+ smc1.stderr.exp smc1.stdout.exp smc1.vgtest
=20
check_PROGRAMS =3D \
- $(INSN_TESTS)
+ $(INSN_TESTS) smc1
=20
AM_CFLAGS =3D $(WERROR) -Winline -Wall -Wshadow -g -I$(top_srcdir)/inc=
lude
AM_CXXFLAGS =3D $(AM_CFLAGS)
Added: trunk/none/tests/amd64/smc1.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/amd64/smc1.c 2005-07-07 11:32:37 UTC (rev 4124)
+++ trunk/none/tests/amd64/smc1.c 2005-07-07 13:20:31 UTC (rev 4125)
@@ -0,0 +1,114 @@
+
+/* Test Valgrind's ability to spot writes to code which has been
+ translated, and discard the out-of-date translations.
+
+ CORRECT output is
+
+ in p 0
+ in q 1
+ in p 2
+ in q 3
+ in p 4
+ in q 5
+ in p 6
+ in q 7
+ in p 8
+ in q 9
+
+ WRONG output (if you fail to spot code-writes to code[0 .. 4]) is
+
+ in p 0
+ in p 1
+ in p 2
+ in p 3
+ in p 4
+ in p 5
+ in p 6
+ in p 7
+ in p 8
+ in p 9
+*/
+
+#include <stdio.h>
+#include <assert.h>
+#include <malloc.h>
+
+typedef unsigned long long int Addr;
+typedef unsigned char UChar;
+
+void q ( int n )
+{
+ printf("in q %d\n", n);
+}
+
+void p ( int n )
+{
+ printf("in p %d\n", n);
+}
+
+// Unlike on x86, data areas aren't executable; have to put
+// code on the heap therefore
+static UChar* code;
+
+/* Make `code' be movabsq $dest, %rax ; pushq %rax ; ret */
+// This forces the branch onwards to be indirect, so vex can't chase it
+void set_dest ( Addr dest )
+{
+ assert(sizeof(Addr) =3D=3D 8);
+
+ /* movabsq $imm64, %rax */
+ code[0] =3D 0x48;
+ code[1] =3D 0xB8;
+ code[2] =3D (dest & 0xFF);
+ code[3] =3D ((dest >> 8) & 0xFF);
+ code[4] =3D ((dest >> 16) & 0xFF);
+ code[5] =3D ((dest >> 24) & 0xFF);
+ code[6] =3D ((dest >> 32) & 0xFF);
+ code[7] =3D ((dest >> 40) & 0xFF);
+ code[8] =3D ((dest >> 48) & 0xFF);
+ code[9] =3D ((dest >> 56) & 0xFF);
+
+ /* pushq %rax */
+ code[10] =3D 0x50;
+
+ /* ret */
+ code[11] =3D 0xC3;
+}
+
+/* Calling aa gets eventually to the function residing in code[0..].
+ This indirection is necessary to defeat Vex's basic-block chasing
+ optimisation. That will merge up to three basic blocks into the
+ same IR superblock, which causes the test to succeed when it
+ shouldn't if main calls code[] directly. */
+
+// force an indirect branch to code[0], so vex can't chase it
+__attribute__((noinline))
+void dd ( int x, void (*f)(int) ) { f(x); }
+
+__attribute__((noinline))
+void cc ( int x ) { dd(x, (void(*)(int)) &code[0]); }
+
+__attribute__((noinline))
+void bb ( int x ) { cc(x); }
+
+__attribute__((noinline))
+void aa ( int x ) { bb(x); }
+
+__attribute__((noinline))
+void diversion ( void ) { }
+
+int main ( void )
+{
+ int i;
+ code =3D malloc(20);
+ assert(code);
+ for (i =3D 0; i < 10; i +=3D 2) {
+ set_dest ( (Addr)&p );
+ // diversion();
+ aa(i);
+ set_dest ( (Addr)&q );
+ // diversion();
+ aa(i+1);
+ }
+ return 0;
+}
Added: trunk/none/tests/amd64/smc1.stderr.exp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/amd64/smc1.stderr.exp 2005-07-07 11:32:37 UTC (rev 4=
124)
+++ trunk/none/tests/amd64/smc1.stderr.exp 2005-07-07 13:20:31 UTC (rev 4=
125)
@@ -0,0 +1,2 @@
+
+
Added: trunk/none/tests/amd64/smc1.stdout.exp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/amd64/smc1.stdout.exp 2005-07-07 11:32:37 UTC (rev 4=
124)
+++ trunk/none/tests/amd64/smc1.stdout.exp 2005-07-07 13:20:31 UTC (rev 4=
125)
@@ -0,0 +1,10 @@
+in p 0
+in q 1
+in p 2
+in q 3
+in p 4
+in q 5
+in p 6
+in q 7
+in p 8
+in q 9
Added: trunk/none/tests/amd64/smc1.vgtest
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/amd64/smc1.vgtest 2005-07-07 11:32:37 UTC (rev 4124)
+++ trunk/none/tests/amd64/smc1.vgtest 2005-07-07 13:20:31 UTC (rev 4125)
@@ -0,0 +1,2 @@
+prog: smc1
+vgopts: --smc-support=3Dall
|