Skip to contentSkip to conversation

Make sure fetch_instruction exist before calling it

PATCH Applied [email protected] 5 participants 6 messages Nov 14, 2025 – Nov 21, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make sure fetch_instruction exist before calling it

From: Nguyen Dinh Phi <[email protected]>

Currently, this function is only available in MSHV. If a different accelerator is used, and the code jumps to this section, a segfault will occur. (I ran into this with HVF)

---
 target/i386/emulate/x86_decode.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/i386/emulate/x86_decode.c +5 -1
index 97bd6f1a3b..d037ed1142 100644
@@ -77,7 +77,11 @@ static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode,
         memcpy(&val, decode->stream->bytes + decode->len, size);
     } else {
         target_ulong va = linear_rip(env_cpu(env), env->eip) + decode->len;
-        emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        if (emul_ops->fetch_instruction) {
+            emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        } else {
+            emul_ops->read_mem(env_cpu(env), &val, va, size);
+        }
     }
     decode->len += size;
 
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Quoted 7 lines · On 11/14/25 09:29, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

Currently, this function is only available in MSHV. If a different accelerator is used, and the code jumps to this section, a segfault will occur. (I ran into this with HVF)

Applied, thanks.

Paolo

Quoted 21 lines
---
  target/i386/emulate/x86_decode.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
target/i386/emulate/x86_decode.c +5 -1
index 97bd6f1a3b..d037ed1142 100644
@@ -77,7 +77,11 @@ static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode,
          memcpy(&val, decode->stream->bytes + decode->len, size);
      } else {
          target_ulong va = linear_rip(env_cpu(env), env->eip) + decode->len;
-        emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        if (emul_ops->fetch_instruction) {
+            emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        } else {
+            emul_ops->read_mem(env_cpu(env), &val, va, size);
+        }
      }
      decode->len += size;
 
#2 lore raw
In reply to [email protected]
Quoted 6 lines · On 14/11/25 09:29, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

Currently, this function is only available in MSHV. If a different accelerator is used, and the code jumps to this section, a segfault will occur. (I ran into this with HVF)

Fixes 1e25327b244 ("x86/emulate: Allow instruction decoding from stream")
Quoted 22 lines
---
  target/i386/emulate/x86_decode.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
target/i386/emulate/x86_decode.c +5 -1
index 97bd6f1a3b..d037ed1142 100644
@@ -77,7 +77,11 @@ static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode,
          memcpy(&val, decode->stream->bytes + decode->len, size);
      } else {
          target_ulong va = linear_rip(env_cpu(env), env->eip) + decode->len;
-        emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        if (emul_ops->fetch_instruction) {
+            emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        } else {
+            emul_ops->read_mem(env_cpu(env), &val, va, size);
+        }
      }
      decode->len += size;
 
#3 lore raw
In reply to [email protected]
Quoted 18 lines · On 11/14/25 09:29, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

Currently, this function is only available in MSHV. If a different accelerator is used, and the code jumps to this section, a segfault will occur. (I ran into this with HVF)

---
  target/i386/emulate/x86_decode.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
target/i386/emulate/x86_decode.c +0 -0
index 97bd6f1a3b..d037ed1142 100644
@@ -77,7 +77,11 @@ static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode,
          memcpy(&val, decode->stream->bytes + decode->len, size);
      } else {

target/i386/emulate/x86_decode.c static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode, · line 87

Quoted 10 lines
target/i386/emulate/x86_decode.c +5 -1
          target_ulong va = linear_rip(env_cpu(env), env->eip) + decode->len;
-        emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        if (emul_ops->fetch_instruction) {
+            emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        } else {
+            emul_ops->read_mem(env_cpu(env), &val, va, size);
+        }
      }
      decode->len += size;
 

We should instead have fetch_instruction == read_mem for the cases that do not require a specialized version.

r~

#4 lore raw
In reply to [email protected]
Quoted 5 lines · On 11/14/25 11:29, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

Currently, this function is only available in MSHV. If a different accelerator is used, and the code jumps to this section, a segfault will occur. (I ran into this with HVF)

Is it a qemu-stable material? It looks like it is. Please let me know if it isn't.

Thanks,

/mjt

Quoted 22 lines
---
  target/i386/emulate/x86_decode.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
target/i386/emulate/x86_decode.c +5 -1
index 97bd6f1a3b..d037ed1142 100644
@@ -77,7 +77,11 @@ static inline uint64_t decode_bytes(CPUX86State *env, struct x86_decode *decode,
          memcpy(&val, decode->stream->bytes + decode->len, size);
      } else {
          target_ulong va = linear_rip(env_cpu(env), env->eip) + decode->len;
-        emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        if (emul_ops->fetch_instruction) {
+            emul_ops->fetch_instruction(env_cpu(env), &val, va, size);
+        } else {
+            emul_ops->read_mem(env_cpu(env), &val, va, size);
+        }
      }
      decode->len += size;
 
#5 lore raw
Quoted 9 lines · Michael Tokarev
Quoted 5 lines · On 11/14/25 11:29, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

Currently, this function is only available in MSHV. If a different accelerator is used, and the code jumps to this section, a segfault will occur. (I ran into this with HVF)

Is it a qemu-stable material?  It looks like it is. Please let me know if it isn't.

Ah, n/m. This one fixes a commit which were added after 10.1 (so not in any stable release), --

1e25327b24 target/i386/emulate: Allow instruction decoding from stream

Sorry for the noise.

/mjt