xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Razvan Cojocaru <rcojocaru@bitdefender.com>, xen-devel@lists.xen.org
Cc: ian.jackson@eu.citrix.com, ian.campbell@citrix.com,
	JBeulich@suse.com, stefano.stabellini@eu.citrix.com
Subject: Re: [PATCH] tools/tests: Add EIP check to test_x86_emulator.c
Date: Thu, 7 Aug 2014 10:35:32 +0100	[thread overview]
Message-ID: <53E34864.5090206@citrix.com> (raw)
In-Reply-To: <53E3442E.40600@bitdefender.com>

On 07/08/14 10:17, Razvan Cojocaru wrote:
> On 08/07/2014 11:18 AM, Andrew Cooper wrote:
>> On 07/08/2014 09:16, Razvan Cojocaru wrote:
>>> The test now also checks that EIP was modified after emulating
>>> instructions after (and including) the "movq %mm3,(%ecx)..."
>>> code block.
>>>
>>> Signed-off-by: Razvan Cojocaru <rcojocaru@bitdefender.com>
>> These checks do check that the instruction pointer has changed, which
>> catches your problem, but doesn't check that it has changed correctly. 
>> You need to work out exactly how long the instruction is a verify that
>> eip points to first byte of the next instruction.
> Code like this:
>
> 602     printf("%-40s", "Testing movq %mm3,(%ecx)...");
> 603     if ( stack_exec && cpu_has_mmx )
> 604     {
> 605         extern const unsigned char movq_to_mem[];
> 606
> 607         asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
> 608                        ".pushsection .test, \"a\", @progbits\n"
> 609                        "movq_to_mem: movq %%mm3, (%0)\n"
> 610                        ".popsection" :: "c" (NULL) );
> 611
> 612         memcpy(instr, movq_to_mem, 15);
> 613         memset(res, 0x33, 64);
> 614         memset(res + 8, 0xff, 8);
> 615         regs.eip    = (unsigned long)&instr[0];
> 616         regs.ecx    = (unsigned long)res;
> 617         rc = x86_emulate(&ctxt, &emulops);
> 618         if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
> 619              (regs.eip == (unsigned long)&instr[0]) )
> 620             goto fail;
> 621         printf("okay\n");
> 622     }
> 623     else
> 624         printf("skipped\n");
>
> probably shows how useful a generic Xen way of determining instruction
> length would be. Where for previous cases it was clear what the length
> of the instruction was (since it was being built by assigning each byte
> of the instruction buffer explicitly), it's not trivial to determine the
> length of a buffer containing a "compiled" instruction and check that
> EIP has advanced by the correct ammount.

I suggest something like:

extern const uint8_t movq_to_mem_start[], movq_to_mem_end[];

...

".pushsection .test, \"a\", @progbits\n"
"movq_to_mem_start: movq %%mm3, (%0)\n"
"movq_to_mem_end:"
".popsection"

At this point, the difference between end and start is however many
bytes the assembler decided to use for the movq instruction, and is
perhaps safer than trying to work it out by hand (especially for the VEX
instructions where there are several valid encodings)

~Andrew

  reply	other threads:[~2014-08-07  9:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-07  8:16 [PATCH] tools/tests: Add EIP check to test_x86_emulator.c Razvan Cojocaru
2014-08-07  8:18 ` Andrew Cooper
2014-08-07  8:32   ` Jan Beulich
2014-08-07  8:35     ` Razvan Cojocaru
2014-08-07  8:45       ` Jan Beulich
2014-08-07  8:53         ` Razvan Cojocaru
2014-08-07  9:17   ` Razvan Cojocaru
2014-08-07  9:35     ` Andrew Cooper [this message]
2014-08-07  8:23 ` Jürgen Groß
2014-08-07  8:27   ` Razvan Cojocaru

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=53E34864.5090206@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=rcojocaru@bitdefender.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=xen-devel@lists.xen.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;
as well as URLs for NNTP newsgroup(s).