public inbox for intel-gfx@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
To: Chris Wilson <chris@chris-wilson.co.uk>, intel-gfx@lists.freedesktop.org
Subject: Re: [PATCH igt 02/10] igt/gem_mmap_gtt: Simulate gdb inspecting a GTT mmap using ptrace()
Date: Mon, 31 Jul 2017 10:41:06 +0100	[thread overview]
Message-ID: <6390da63-de05-b2fa-22d5-9b4974d50a39@linux.intel.com> (raw)
In-Reply-To: <20170728120808.25824-2-chris@chris-wilson.co.uk>


On 28/07/2017 13:08, Chris Wilson wrote:
> gdb uses ptrace() to peek and poke bytes of the target's address space.
> The kernel must implement an vm_ops->access() handler or else gdb will
> be unable to inspect the pointer and report it as out-of-bounds. Worse
> than useless as it causes immediate suspicion of the valid GTT pointer.
> 
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> ---
>   tests/gem_mmap_gtt.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 79 insertions(+)
> 
> diff --git a/tests/gem_mmap_gtt.c b/tests/gem_mmap_gtt.c
> index 4ff5e7f1..61c08406 100644
> --- a/tests/gem_mmap_gtt.c
> +++ b/tests/gem_mmap_gtt.c
> @@ -36,6 +36,8 @@
>   #include <errno.h>
>   #include <sys/stat.h>
>   #include <sys/ioctl.h>
> +#include <sys/ptrace.h>
> +#include <sys/wait.h>
>   #include "drm.h"
>   
>   #include "igt.h"
> @@ -310,6 +312,81 @@ test_write_gtt(int fd)
>   	munmap(src, OBJECT_SIZE);
>   }
>   
> +static void *memchr_inv(const void *s, int c, size_t n)
> +{
> +	const uint8_t *us = s;
> +	const uint8_t uc = c;
> +
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wcast-qual"
> +	while (n--) {
> +		if (*us != uc)
> +			return (void *) us;
> +		us++;
> +	}
> +#pragma GCC diagnostic pop

Stripping away constness wouldn't work with a simpler:

uint8_t *us = (uint8_t *)s?

> +
> +	return NULL;
> +}
> +
> +static void
> +test_ptrace(int fd)
> +{
> +	long AA, CC;
> +	long *gtt, *copy;
> +	uint32_t bo;
> +	pid_t pid;
> +
> +	memset(&AA, 0xaa, sizeof(AA));
> +	memset(&CC, 0x55, sizeof(CC));
> +
> +	copy = malloc(OBJECT_SIZE);
> +	memset(copy, AA, OBJECT_SIZE);
> +
> +	bo = gem_create(fd, OBJECT_SIZE);
> +	gtt = mmap_bo(fd, bo);
> +	memset(gtt, CC, OBJECT_SIZE);
> +	gem_close(fd, bo);
> +
> +	igt_assert(!memchr_inv(gtt, CC, OBJECT_SIZE));
> +	igt_assert(!memchr_inv(copy, AA, OBJECT_SIZE));
> +
> +	switch ((pid = fork())) {
> +		case -1:
> +			igt_assert(pid != -1);
> +			break;
> +
> +		case 0:
> +			ptrace(PTRACE_TRACEME, 0, NULL, NULL);
> +			raise(SIGSTOP);
> +			raise(SIGKILL);
> +			exit(0);
> +			break;
> +
> +		default:
> +			/* Wait for the child to ready themselves */
> +			wait(NULL);
> +
> +			ptrace(PTRACE_ATTACH, pid, NULL, NULL);
> +			for (int i = 0; i < OBJECT_SIZE/sizeof(long); i++) {
> +				copy[i] = ptrace(PTRACE_PEEKDATA, pid, gtt+i, NULL);
> +				ptrace(PTRACE_POKEDATA, pid, gtt + i, AA);

Inconsistent whitespace in the above three lines. First and second need 
a tidy.

> +			}
> +			ptrace(PTRACE_DETACH, pid, NULL, NULL);
> +
> +			/* Wakeup the child for it to exit */
> +			kill(SIGCONT, pid);
> +			break;
> +	}
> +
> +	/* The contents of the two buffers should now be swapped */
> +	igt_assert(!memchr_inv(gtt, AA, OBJECT_SIZE));
> +	igt_assert(!memchr_inv(copy, CC, OBJECT_SIZE));
> +
> +	munmap(gtt, OBJECT_SIZE);
> +	free(copy);
> +}
> +
>   static void
>   test_coherency(int fd)
>   {
> @@ -809,6 +886,8 @@ igt_main
>   		test_write(fd);
>   	igt_subtest("basic-write-gtt")
>   		test_write_gtt(fd);
> +	igt_subtest("ptrace")
> +		test_ptrace(fd);
>   	igt_subtest("coherency")
>   		test_coherency(fd);
>   	igt_subtest("clflush")
> 

Apart from the little details, and ptrace API which I haven't looked in 
detail (if it works it works), looks simple enough.

Regards,

Tvrtko
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

  reply	other threads:[~2017-07-31  9:41 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-28 12:07 [PATCH igt 01/10] tests/prime_rw: Add basic tests for reading/writing to a dmabuf Chris Wilson
2017-07-28 12:08 ` [PATCH igt 02/10] igt/gem_mmap_gtt: Simulate gdb inspecting a GTT mmap using ptrace() Chris Wilson
2017-07-31  9:41   ` Tvrtko Ursulin [this message]
2017-07-31 10:17     ` Chris Wilson
2017-07-31 10:39       ` Tvrtko Ursulin
2017-07-28 12:08 ` [PATCH igt 03/10] igt/gem_ctx_switch: Exercise all engines at once Chris Wilson
2017-07-31  9:57   ` Tvrtko Ursulin
2017-07-28 12:08 ` [PATCH igt 04/10] igt/gem_exec_schedule: Exercise reordering with many priority levels Chris Wilson
2017-07-31 14:35   ` Tvrtko Ursulin
2017-07-31 15:21     ` Chris Wilson
2017-07-31 15:27   ` Michał Winiarski
2017-07-28 12:08 ` [PATCH igt 05/10] igt/drv_hangman: Skip if resets are disallowed Chris Wilson
2017-07-28 13:53   ` Michał Winiarski
2017-07-28 13:59     ` Chris Wilson
2017-07-28 14:01     ` Chris Wilson
2017-07-28 12:08 ` [PATCH igt 06/10] igt/gem_ringfill: Prime execbuf before measuring ring size Chris Wilson
2017-07-31  9:09   ` Michał Winiarski
2017-07-28 12:08 ` [PATCH igt 07/10] igt/gem_exec_suspend: Try to suspend with a pending GPU hang Chris Wilson
2017-07-31  8:58   ` Michał Winiarski
2017-07-31  9:02     ` Chris Wilson
2017-07-28 12:08 ` [PATCH igt 08/10] igt/gem_exec_fence: Exercise merging fences Chris Wilson
2017-07-31  8:26   ` Michał Winiarski
2017-07-31  9:19     ` Chris Wilson
2017-07-28 12:08 ` [PATCH igt 09/10] igt/gem_userptr_blits: Errors from gup are permanent Chris Wilson
2017-07-28 12:33   ` Michał Winiarski
2017-07-28 12:08 ` [PATCH igt 10/10] igt/gem_eio: i915.reset is no longer a boolean Chris Wilson
2017-07-28 13:36   ` Michał Winiarski
2017-07-28 13:50     ` Chris Wilson
2017-07-28 15:56       ` Michał Winiarski
2017-07-31  9:26 ` [PATCH igt 01/10] tests/prime_rw: Add basic tests for reading/writing to a dmabuf Tvrtko Ursulin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6390da63-de05-b2fa-22d5-9b4974d50a39@linux.intel.com \
    --to=tvrtko.ursulin@linux.intel.com \
    --cc=chris@chris-wilson.co.uk \
    --cc=intel-gfx@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox