diff options
author | Aldy Hernandez <aldyh@redhat.com> | 2011-10-10 13:42:41 +0000 |
---|---|---|
committer | Aldy Hernandez <aldyh@gcc.gnu.org> | 2011-10-10 13:42:41 +0000 |
commit | ff8e7c4d682099d61e81a20b465280bcc7cdaab5 (patch) | |
tree | bba6939b464863b1cd47394507ef6db3d08fb471 /gcc/testsuite/gcc.dg/simulate-thread | |
parent | 53fbb7241f0d2d4e3a4eb8b3225671b121091b06 (diff) | |
download | gcc-ff8e7c4d682099d61e81a20b465280bcc7cdaab5.zip gcc-ff8e7c4d682099d61e81a20b465280bcc7cdaab5.tar.gz gcc-ff8e7c4d682099d61e81a20b465280bcc7cdaab5.tar.bz2 |
gcc-simulate-thread.exp: New.
* lib/gcc-simulate-thread.exp: New.
* gcc.dg/simulate-thread/guality.h: New.
* gcc.dg/simulate-thread/simulate-thread.h: New.
* gcc.dg/simulate-thread/simulate-thread.exp: New.
* gcc.dg/simulate-thread/simulate-thread.gdb: New.
* gcc.dg/simulate-thread/README: New.
* g++.dg/simulate-thread/guality.h: New.
* g++.dg/simulate-thread/simulate-thread.h: New.
* g++.dg/simulate-thread/simulate-thread.exp: New.
* g++.dg/simulate-thread/simulate-thread.gdb: New.
* c-c++-common/cxxbitfields-2.c: Remove.
* c-c++-common/cxxbitfields.c: Remove.
* c-c++-common/cxxbitfields-4.c: Remove.
* c-c++-common/cxxbitfields-5.c: Remove.
* c-c++-common/simulate-thread/bitfields-1.c: New.
* c-c++-common/simulate-thread/bitfields-2.c: New.
* c-c++-common/simulate-thread/bitfields-3.c: New.
* c-c++-common/simulate-thread/bitfields-4.c: New.
From-SVN: r179751
Diffstat (limited to 'gcc/testsuite/gcc.dg/simulate-thread')
4 files changed, 180 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/simulate-thread/README b/gcc/testsuite/gcc.dg/simulate-thread/README new file mode 100644 index 0000000..5588e04 --- /dev/null +++ b/gcc/testsuite/gcc.dg/simulate-thread/README @@ -0,0 +1,118 @@ +OVERVIEW +-------- + +This is a harness to test the atomicity of certain operations, and to +make sure the compiler does not introduce data races in a +multi-threaded environment. + +The basic premise is that we set up testcases such that the thing we +want test, say an atomic instruction which stores a double word is in +a function of its own. We then run this testcase within GDB, +controlled by a gdb script (simulate-thread.gdb). The gdb script will +break on the function to be tested, and then single step through every +machine instruction in the function. We set this up so GDB can make a +couple of inferior function calls before and after each of these +single step instructions for a couple of purposes: + + 1. One of the calls simulates another thread running in the + process which changes or access memory. + + 2. The other calls are used to verify that we always get the + expected behavior. + +For example, in the case of an atomic store, anyone looking at the +memory associated with an atomic variable should never see any in +between states. If you have an atomic long long int, and it starts +with the value 0, and you write the value MAX_LONG_LONG, any other +thread looking at that variable should never see anything other than 0 +or MAX_LONG_LONG. If you implement the atomic write as a sequence of +2 stores, it is possible for another thread to read the location after +the first store, but before the second one is complete. That thread +would then see an in-between state (one word would still be 0). + +We simulate this in the testcase by having GDB step through the +program, instruction by instruction, and after each step, making an +inferior function call which looks at the value of the atomic variable +and verifies that it sees either 0 or MAX_LONG_LONG. If it sees any +other value, it fails the testcase. + +This way, we are *sure* there is no in between state because we +effectively acted like an OS and switched to another thread after +every single instruction of the routine is executed and looked at the +results each time. + +We use the same idea to test for data races to see if an illegal load +has been hoisted, or that two parallel bitfield writes don't overlap +in a data race. + +Below is a skeleton of how a test should look like. For more details, +look at the tests themselves. + +ANATOMY OF A TEST +----------------- + +/* { dg-do link } */ +/* { dg-options "-some-flags" } */ +/* { dg-final { simulate-thread } } */ + +/* NOTE: Any failure must be indicated by displaying "FAIL:". */ + +#include "simulate-thread.h" + +/* Called before each instruction, simulating another thread executing. */ +void simulate_thread_other_threads() +{ +} + +/* Called after each instruction. Returns 1 if any inconsistency is + found, 0 otherwise. */ +int simulate_thread_step_verify() +{ + if (some_problem) + { + printf("FAIL: reason\n"); + return 1; + } + return 0; +} + +/* Called at the end of the program (simulate_thread_fini == 1). Verifies + the state of the program and returns 1 if any inconsistency is + found, 0 otherwise. */ +int simulate_thread_final_verify() +{ + if (some_problem) + { + printf("FAIL: reason\n"); + return 1; + } + return 0; +} + +/* The gdb script will break on simulate_thread_main(), so make sure + GCC does not inline it, thus making the break point fail. */ +__attribute__((noinline)) +void simulate_thread_main() +{ + /* Do stuff. */ +} + +int main() +{ + + /* Perform any setup code that will run outside of the testing + harness. Put code here that you do NOT want to be interrupted on + an instruction basis. E.g., setup code, and system library + calls. */ + + /* Do un-instrumented stuff. */ + /* ... */ + + /* Start the instrumented show. */ + simulate_thread_main(); + + /* Must be called at the end of the test. */ + simulate_thread_done(); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.exp b/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.exp new file mode 100644 index 0000000..6d47388 --- /dev/null +++ b/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.exp @@ -0,0 +1,38 @@ +# Copyright (C) 2011 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING3. If not see +# <http://www.gnu.org/licenses/>. + +# Your run of the mill dg test, but verify that we have a working GDB first. + +load_lib gcc-dg.exp +load_lib gcc-simulate-thread.exp +load_lib torture-options.exp + +dg-init +torture-init +set-torture-options [list \ + { -O0 -g } \ + { -O1 -g } \ + { -O2 -g } \ + { -O3 -g } \ + { -Os -g } ] + +if [gdb-exists] { + gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] "" + gcc-dg-runtest [lsort [glob $srcdir/c-c++-common/simulate-thread/*.c]] "" +} + +torture-finish +dg-finish diff --git a/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.gdb b/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.gdb new file mode 100644 index 0000000..004909f --- /dev/null +++ b/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.gdb @@ -0,0 +1,17 @@ +set height 0 +break simulate_thread_main +disp/i $pc +run + +set $ret = 0 +while (simulate_thread_fini != 1) && (! $ret) + call simulate_thread_other_threads() + stepi + set $ret |= simulate_thread_step_verify() +end + +if (! $ret) + set $ret |= simulate_thread_final_verify() +end +continue +quit $ret diff --git a/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.h b/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.h new file mode 100644 index 0000000..8dabfa2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/simulate-thread/simulate-thread.h @@ -0,0 +1,7 @@ +int simulate_thread_fini = 0; + +void __attribute__((noinline)) +simulate_thread_done () +{ + simulate_thread_fini = 1; +} |