Skip to contentSkip to conversation

tools/tests: Add EIP check to test_x86_emulator.c

PATCH Superseded Razvan Cojocaru 4 participants 10 messages Aug 7, 2014

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/tests: Add EIP check to test_x86_emulator.c

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

---
 tools/tests/x86_emulator/test_x86_emulator.c |   30 +++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +20 -10
index 0a00d5a..dd4e986 100644
@@ -615,7 +615,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -637,7 +638,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "pcmpeqb %%mm3, %%mm3\n\t"
               "pcmpeqb %%mm5, %%mm3\n\t"
@@ -665,7 +667,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -687,7 +690,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
               "pcmpeqb %%xmm4, %%xmm2\n\t"
@@ -716,7 +720,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -744,7 +749,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
 #if 0 /* Don't use AVX2 instructions for now */
         asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
@@ -784,7 +790,8 @@ int main(int argc, char **argv)
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -809,7 +816,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "cmpeqps %1, %%xmm7\n\t"
               "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
@@ -837,7 +845,8 @@ int main(int argc, char **argv)
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -862,7 +871,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
               "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 5 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

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.

~Andrew

Quoted 108 lines
---
 tools/tests/x86_emulator/test_x86_emulator.c |   30 +++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +20 -10
index 0a00d5a..dd4e986 100644
@@ -615,7 +615,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -637,7 +638,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "pcmpeqb %%mm3, %%mm3\n\t"
               "pcmpeqb %%mm5, %%mm3\n\t"
@@ -665,7 +667,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -687,7 +690,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
               "pcmpeqb %%xmm4, %%xmm2\n\t"
@@ -716,7 +720,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -744,7 +749,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
 #if 0 /* Don't use AVX2 instructions for now */
         asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
@@ -784,7 +790,8 @@ int main(int argc, char **argv)
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -809,7 +816,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "cmpeqps %1, %%xmm7\n\t"
               "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
@@ -837,7 +845,8 @@ int main(int argc, char **argv)
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         printf("okay\n");
     }
@@ -862,7 +871,8 @@ int main(int argc, char **argv)
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
             goto fail;
         asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
               "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
#2 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 07.08.14 at 10:18, <[email protected]> wrote:
Quoted 4 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

Quoted 6 lines
Quoted 1 line

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.

And apart from that this should be done for all tests, not just the MMX/SSE/AVX ones.

Jan

#3 lore raw
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 07.08.14 at 10:18, <[email protected]> wrote:
Quoted 5 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

Quoted 7 lines
Quoted 4 lines

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.

And apart from that this should be done for all tests, not just the MMX/SSE/AVX ones.

The other tests already do this, but there it's simpler to check. For example:

218     printf("%-40s", "Testing addl %%ecx,%%eax...");
219     instr[0] = 0x01; instr[1] = 0xc8;
220     regs.eflags = 0x200;
221     regs.eip    = (unsigned long)&instr[0];
222     regs.ecx    = 0x12345678;
223     regs.eax    = 0x7FFFFFFF;
224     rc = x86_emulate(&ctxt, &emulops);
225     if ( (rc != X86EMUL_OKAY) ||
226          (regs.ecx != 0x12345678) ||
227          (regs.eax != 0x92345677) ||
228          (regs.eflags != 0xa94) ||
229          (regs.eip != (unsigned long)&instr[2]) )

230 goto fail;
231 printf("okay\n");

Thanks,
Razvan Cojocaru

#4 lore raw
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 07.08.14 at 10:35, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 07.08.14 at 10:18, <[email protected]> wrote:
Quoted 5 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

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.

Quoted 4 lines
Quoted 2 lines

And apart from that this should be done for all tests, not just the MMX/SSE/AVX ones.

The other tests already do this, but there it's simpler to check.

Ah, my bad then (both in not having checked the code before replying, and in not having added these checks back when I added those tests) - I'm sorry.

Jan

#5 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 07.08.14 at 10:35, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 07.08.14 at 10:18, <[email protected]> wrote:
Quoted 5 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

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.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

And apart from that this should be done for all tests, not just the MMX/SSE/AVX ones.

The other tests already do this, but there it's simpler to check.

Ah, my bad then (both in not having checked the code before replying, and in not having added these checks back when I added those tests) - I'm sorry.

Absolutely no problem at all, we're all friends here.

Thanks,
Razvan Cojocaru

#6 lore raw
In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper
Quoted 5 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

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.

Thanks,
Razvan Cojocaru

#7 lore raw
Quoted 37 lines · Razvan Cojocaru
Quoted 10 lines · Andrew Cooper
Quoted 5 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

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");

Quoted 6 lines

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

#8 lore raw
In reply to Razvan Cojocaru
Quoted 14 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

---
  tools/tests/x86_emulator/test_x86_emulator.c |   30 +++++++++++++++++---------
  1 file changed, 20 insertions(+), 10 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +0 -0
index 0a00d5a..dd4e986 100644

tools/tests/x86_emulator/test_x86_emulator.c int main(int argc, char **argv) · line 619

Quoted 6 lines
tools/tests/x86_emulator/test_x86_emulator.c +2 -1
@@ -615,7 +615,8 @@ int main(int argc, char **argv)
          regs.eip    = (unsigned long)&instr[0];
          regs.ecx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )

Is really any modification okay? I think the test should check for the correct EIP, not just for one of 2^64 - 1 incorrect values.

Quoted 94 lines
tools/tests/x86_emulator/test_x86_emulator.c +18 -9
              goto fail;
          printf("okay\n");
      }
@@ -637,7 +638,8 @@ int main(int argc, char **argv)
          regs.ecx    = 0;
          regs.edx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          asm ( "pcmpeqb %%mm3, %%mm3\n\t"
                "pcmpeqb %%mm5, %%mm3\n\t"
@@ -665,7 +667,8 @@ int main(int argc, char **argv)
          regs.eip    = (unsigned long)&instr[0];
          regs.ecx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          printf("okay\n");
      }
@@ -687,7 +690,8 @@ int main(int argc, char **argv)
          regs.ecx    = 0;
          regs.edx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
                "pcmpeqb %%xmm4, %%xmm2\n\t"
@@ -716,7 +720,8 @@ int main(int argc, char **argv)
          regs.eip    = (unsigned long)&instr[0];
          regs.ecx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          printf("okay\n");
      }
@@ -744,7 +749,8 @@ int main(int argc, char **argv)
          regs.ecx    = 0;
          regs.edx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
  #if 0 /* Don't use AVX2 instructions for now */
          asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
@@ -784,7 +790,8 @@ int main(int argc, char **argv)
          regs.ecx    = (unsigned long)(res + 2);
          regs.edx    = 0;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          printf("okay\n");
      }
@@ -809,7 +816,8 @@ int main(int argc, char **argv)
          regs.ecx    = 0;
          regs.edx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          asm ( "cmpeqps %1, %%xmm7\n\t"
                "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
@@ -837,7 +845,8 @@ int main(int argc, char **argv)
          regs.ecx    = (unsigned long)(res + 2);
          regs.edx    = 0;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          printf("okay\n");
      }
@@ -862,7 +871,8 @@ int main(int argc, char **argv)
          regs.ecx    = 0;
          regs.edx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip == (unsigned long)&instr[0]) )
              goto fail;
          asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
                "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
#9 lore raw
Quoted 11 lines · Jürgen Groß
Quoted 10 lines · Razvan Cojocaru

The test now also checks that EIP was modified after emulating instructions after (and including) the "movq %mm3,(%ecx)..." code block.

---
  tools/tests/x86_emulator/test_x86_emulator.c |   30
+++++++++++++++++---------
  1 file changed, 20 insertions(+), 10 deletions(-)
Quoted 15 lines

tools/tests/x86_emulator/test_x86_emulator.c int main(int argc, char **argv) · line 619

Quoted 12 lines
tools/tests/x86_emulator/test_x86_emulator.c +2 -1
index 0a00d5a..dd4e986 100644
@@ -615,7 +615,8 @@ int main(int argc, char **argv)
          regs.eip    = (unsigned long)&instr[0];
          regs.ecx    = (unsigned long)res;
          rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip == (unsigned long)&instr[0]) )

Is really any modification okay? I think the test should check for the correct EIP, not just for one of 2^64 - 1 incorrect values.

While you are, of course, perfectly right that not any modification is okay, this does at least catch the bigger issue of EIP not being updated at all. I'll look into it.

Thanks,
Razvan Cojocaru