mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2025-01-06 12:09:26 +08:00
99ba4b64d3
I was looking at PR gdb/19675 and the related test gdb.base/step-over-syscall.exp. This test includes a call to kfail when we are testing a displaced step over a clone syscall. While looking at the test I removed the call to kfail and ran the test, and was surprised that the test passed. I ran the test a few times and it does sometimes fail, but mostly it passed fine. PR gdb/19675 describes how, when we displaced step over a clone, the new thread is created with a $pc in the displaced step buffer. GDB then fails to "fix" this $pc (for the new thread), and the thread will be set running with its current $pc value. This means that the new thread will just start executing from whatever happens to be after the displaced stepping buffer. In the original PR gdb/19675 bug report Yao Qi was seeing the new thread cause a segfault, the problem is, what actually happens is totally undefined. On my machine, I'm seeing the new thread reenter main, it then starts trying to run the test again (in the new thread). This just happens to be safe enough (in this simple test) that most of the time the inferior doesn't crash. In this commit I try to make the test slightly more likely to fail by doing a couple of things. First, I added a static variable to main, this is set true when the first thread enters main, if a second thread ever enters main then I force an abort. Second, when the test is finishing I want to ensure that the new threads have had a chance to do "something bad" if they are going to. So I added a global counter, as each thread starts successfully it decrements the counter. The main thread does not proceed to the final marker function (where GDB has placed a breakpoint) until all threads have started successfully. This means that if the newly created thread doesn't successfully enter clone_fn then the counter will never reach zero and the test will timeout. With these two changes my hope is that the test should fail more reliably, and so, I have also changed the test to call setup_kfail before the specific steps that we expect to misbehave instead of just calling kfail and skipping parts of the test completely. The benefit of this is that if/when we fix GDB this test will start to KPASS and we'll know to update this test to remove the setup_kfail call.
94 lines
2.6 KiB
C
94 lines
2.6 KiB
C
/* This testcase is part of GDB, the GNU debugger.
|
|
|
|
Copyright 2016-2021 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 this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#define _GNU_SOURCE
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sched.h>
|
|
#include <pthread.h>
|
|
|
|
static void
|
|
marker ()
|
|
{}
|
|
|
|
#define STACK_SIZE 0x1000
|
|
|
|
/* These are used to signal that the threads have started correctly. The
|
|
GLOBAL_THREAD_COUNT is set to the number of threads in main, then
|
|
decremented (under a lock) in each new thread. */
|
|
pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
int global_thread_count = 0;
|
|
|
|
static int
|
|
clone_fn (void *unused)
|
|
{
|
|
/* Signal that this thread has started correctly. */
|
|
if (pthread_mutex_lock (&global_lock) != 0)
|
|
abort ();
|
|
global_thread_count--;
|
|
if (pthread_mutex_unlock (&global_lock) != 0)
|
|
abort ();
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
main (void)
|
|
{
|
|
int i, pid;
|
|
unsigned char *stack[6];
|
|
|
|
/* Due to bug gdb/19675 the cloned thread _might_ try to reenter main
|
|
(this depends on where the displaced instruction is placed for
|
|
execution). However, if we do reenter main then lets ensure we fail
|
|
hard rather then just silently executing the code below. */
|
|
static int started = 0;
|
|
if (!started)
|
|
started = 1;
|
|
else
|
|
abort ();
|
|
|
|
for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
|
|
stack[i] = malloc (STACK_SIZE);
|
|
|
|
global_thread_count = (sizeof (stack) / sizeof (stack[0]));
|
|
|
|
for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
|
|
{
|
|
pid = clone (clone_fn, stack[i] + STACK_SIZE, CLONE_FILES | CLONE_VM,
|
|
NULL);
|
|
}
|
|
|
|
for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
|
|
free (stack[i]);
|
|
|
|
/* Set an alarm so we don't end up stuck waiting for threads that might
|
|
never start correctly. */
|
|
alarm (120);
|
|
|
|
/* Now wait for all the threads to start up. */
|
|
while (global_thread_count != 0)
|
|
{
|
|
/* Force memory barrier so GLOBAL_THREAD_COUNT will be refetched. */
|
|
asm volatile ("" ::: "memory");
|
|
sleep (1);
|
|
}
|
|
|
|
/* Call marker, this is what GDB is waiting for. */
|
|
marker ();
|
|
}
|