Re: [REGRESSION 5.8] x86/entry: DR0 break-on-write not working
From: Peter Zijlstra
Date: Wed Aug 19 2020 - 17:36:19 EST
On Wed, Aug 19, 2020 at 12:28:16PM -0700, Kyle Huey wrote:
> > I'm guess that is not the expected outcome, is that the same failure you
> > saw?
>
> Yes. Is status also 0x4d00 for you?
Indeed.
> The program is expected to complete with no assertions firing.
When I comment out the break-on-exec test, the break-on-write test
succeeds.
When I add a few printk()'s to our #DB handler (6) the program will
magically work again.
I'm not much for ptrace(), but are we sure the test program is well
behaved?
The below also always works..
---
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <stdarg.h>
#include <assert.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <sys/user.h>
/**
* Print the printf-like arguments to stdout as atomic-ly as we can
* manage. Async-signal-safe. Does not flush stdio buffers (doing so
* isn't signal safe).
*/
__attribute__((format(printf, 1, 2))) inline static int atomic_printf(
const char* fmt, ...) {
va_list args;
char buf[1024];
int len;
va_start(args, fmt);
len = vsnprintf(buf, sizeof(buf) - 1, fmt, args);
va_end(args);
return write(STDOUT_FILENO, buf, len);
}
/**
* Write |str| on its own line to stdout as atomic-ly as we can
* manage. Async-signal-safe. Does not flush stdio buffers (doing so
* isn't signal safe).
*/
inline static int atomic_puts(const char* str) {
return atomic_printf("%s\n", str);
}
inline static int check_cond(int cond) {
if (!cond) {
atomic_printf("FAILED: errno=%d (%s)\n", errno, strerror(errno));
}
return cond;
}
#define test_assert(cond) assert("FAILED: !" && check_cond(cond))
#define NEW_VALUE 0xabcdef
static void breakpoint(void) {}
static char watch_var;
int main(void) {
pid_t child;
int status;
int pipe_fds[2];
test_assert(0 == pipe(pipe_fds));
if (0 == (child = fork())) {
char ch;
read(pipe_fds[0], &ch, 1);
breakpoint();
watch_var = 1;
return 77;
}
test_assert(0 == ptrace(PTRACE_ATTACH, child, NULL, NULL));
test_assert(child == waitpid(child, &status, 0));
test_assert(status == ((SIGSTOP << 8) | 0x7f));
test_assert(1 == write(pipe_fds[1], "x", 1));
test_assert(0 == ptrace(PTRACE_POKEUSER, child,
(void*)offsetof(struct user, u_debugreg[0]),
(void*)breakpoint));
test_assert(0 == ptrace(PTRACE_POKEUSER, child,
(void*)offsetof(struct user, u_debugreg[1]),
&watch_var));
test_assert(0 == ptrace(PTRACE_POKEUSER, child,
(void*)offsetof(struct user, u_debugreg[7]),
(void*)0x100005));
test_assert(0 == ptrace(PTRACE_CONT, child, NULL, NULL));
test_assert(child == waitpid(child, &status, 0));
test_assert(status == ((SIGTRAP << 8) | 0x7f));
test_assert(0x1 == ptrace(PTRACE_PEEKUSER, child,
(void*)offsetof(struct user, u_debugreg[6])));
test_assert(0 == ptrace(PTRACE_CONT, child, NULL, NULL));
test_assert(child == waitpid(child, &status, 0));
test_assert(status == ((SIGTRAP << 8) | 0x7f));
test_assert(0x2 == ptrace(PTRACE_PEEKUSER, child,
(void*)offsetof(struct user, u_debugreg[6])));
test_assert(0 == ptrace(PTRACE_DETACH, child, NULL, NULL));
test_assert(child == waitpid(child, &status, 0));
test_assert(WIFEXITED(status));
test_assert(WEXITSTATUS(status) == 77);
atomic_puts("EXIT-SUCCESS");
return 0;
}