Skip to contentSkip to conversation

slirp: Use lduw_be_p in slirp_input

PATCH In review Richard Henderson 7 participants 12 messages Dec 26, 2018 – Jan 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slirp: Use lduw_be_p in slirp_input

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

This fixes sparc64 host SIGBUS during pxe boot.

---
 slirp/slirp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/slirp.c +1 -1
index 322edf51eb..a116f43878 100644
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = lduw_be_p(pkt + 12);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);
Signature · 1 line
2.17.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Richard Henderson

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

This fixes sparc64 host SIGBUS during pxe boot.

---
 slirp/slirp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

thanks
-- PMM

Reviewed-by on v1 1/1
#2 lore raw
In reply to Richard Henderson
Quoted 8 lines · Richard Henderson

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

This fixes sparc64 host SIGBUS during pxe boot.

---

Quoted 22 lines
 slirp/slirp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/slirp.c +1 -1
index 322edf51eb..a116f43878 100644
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int
 pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = lduw_be_p(pkt + 12);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

--
2.17.2

#3 lore raw
In reply to Richard Henderson

Hello,

Richard Henderson, le mer. 26 déc. 2018 14:42:54 +1100, a ecrit:

Quoted 5 lines

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

This fixes sparc64 host SIGBUS during pxe boot.

I'm not at ease with applying this, when Marc-André is trying to make slirp an external library... I'd rather apply the change below, could somebody review it?

Samuel

slirp: Avoid unaligned 16bit memory access

pkt parameter may be unaligned, so we must access it byte-wise.

This fixes sparc64 host SIGBUS during pxe boot.

slirp/slirp.c +1 -1
index ab2fc4eb8b..0e41d5aedf 100644
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);
author Reviewed-by
#4 lore raw
Quoted 7 lines · Samuel Thibault

Hello,

Richard Henderson, le mer. 26 déc. 2018 14:42:54 +1100, a ecrit:

Quoted 4 lines

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

Quoted 5 lines
Quoted 1 line

This fixes sparc64 host SIGBUS during pxe boot.

I'm not at ease with applying this, when Marc-André is trying to make slirp an external library... I'd rather apply the change below, could somebody review it?

Fair.

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 854

Quoted 2 lines
slirp/slirp.c +1 -1
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];

This works for me too, though I note unnecessary parenthesis around the cast.

r~

#5 lore raw
In reply to Samuel Thibault
Quoted 29 lines · Samuel Thibault

Hello,

Richard Henderson, le mer. 26 déc. 2018 14:42:54 +1100, a ecrit:

Quoted 5 lines

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

This fixes sparc64 host SIGBUS during pxe boot.

I'm not at ease with applying this, when Marc-André is trying to make slirp an external library... I'd rather apply the change below, could somebody review it?

Samuel

slirp: Avoid unaligned 16bit memory access

pkt parameter may be unaligned, so we must access it byte-wise.

This fixes sparc64 host SIGBUS during pxe boot.

slirp/slirp.c +0 -0
index ab2fc4eb8b..0e41d5aedf 100644

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 857

Quoted 8 lines
slirp/slirp.c +1 -1
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

What about using memcpy?

-- >8 --
@@ -846,12 +846,13 @@ static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)

 {
     struct mbuf *m;

- int proto;
+ uint16_t proto;

if (pkt_len < ETH_HLEN)
return;

- proto = ntohs(*(uint16_t *)(pkt + 12));
+ memcpy(&proto, pkt + 12, sizeof(proto)); /* Avoid unaligned 16bit
access */

+    proto = ntohs(proto);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

---

#6 lore raw

Philippe Mathieu-Daudé, le jeu. 17 janv. 2019 14:16:16 +0100, a ecrit:

Quoted 33 lines · Samuel Thibault
Quoted 32 lines

Hello,

Richard Henderson, le mer. 26 déc. 2018 14:42:54 +1100, a ecrit:

Quoted 5 lines

The pointer may be unaligned, so we must use our routines for that. At the same time, we might as well use the big-endian version instead of ntohs.

This fixes sparc64 host SIGBUS during pxe boot.

I'm not at ease with applying this, when Marc-André is trying to make slirp an external library... I'd rather apply the change below, could somebody review it?

Samuel

slirp: Avoid unaligned 16bit memory access

pkt parameter may be unaligned, so we must access it byte-wise.

This fixes sparc64 host SIGBUS during pxe boot.

slirp/slirp.c +0 -0
index ab2fc4eb8b..0e41d5aedf 100644
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
Quoted 7 lines

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 857

Quoted 5 lines
slirp/slirp.c +1 -1
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

What about using memcpy?

Well, it looks to me even more confusing than doing the shifts :)

Quoted 21 lines

-- >8 --

(quoted diff) +2 -2
@@ -846,12 +846,13 @@ static void arp_input(Slirp *slirp, const uint8_t
 *pkt, int pkt_len)
 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
 {
     struct mbuf *m;
-    int proto;
+    uint16_t proto;
 
     if (pkt_len < ETH_HLEN)
         return;
 
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    memcpy(&proto, pkt + 12, sizeof(proto)); /* Avoid unaligned 16bit

access */

+    proto = ntohs(proto);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

---

#7 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Samuel Thibault
slirp/slirp.c +0 -0
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
Quoted 7 lines

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 857

Quoted 5 lines
slirp/slirp.c +1 -1
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

What about using memcpy?

We should use whatever the new libslirp wants to consistently use as its mechanism for loading unaligned data. I don't suppose this is the only place where it ever needs to do this.

Personally I would vote for having libslirp have versions of the ld*_p functions, because they solve the problem in a clear and correct way. But that's up to Marc-André really.

(As you can see from clang build logs:
https://travis-ci.org/qemu/qemu/jobs/480813811
slirp/ has a lot of as-yet-unfixed "takes address of packed member" bugs, which suggest it's a bit slapdash with alignment. Running it under the clang sanitizer to look for runtime alignment errors might also be interesting.)

thanks
-- PMM

#8 lore raw

Hi

Quoted 17 lines · Peter Maydell
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Samuel Thibault
slirp/slirp.c +0 -0
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
Quoted 7 lines

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 857

Quoted 5 lines
slirp/slirp.c +1 -1
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

What about using memcpy?

Quoted 7 lines

We should use whatever the new libslirp wants to consistently use as its mechanism for loading unaligned data. I don't suppose this is the only place where it ever needs to do this.

Personally I would vote for having libslirp have versions of the ld*_p functions, because they solve the problem in a clear and correct way. But that's up to Marc-André really.

I think I would go with a copy of qemu bswap.h, unless there is an equivalent in glib (I don't think so) or gnulib? Or other standard compiler solution.

It's somehow surprising me that there is no goto solution :).

Quoted 9 lines

(As you can see from clang build logs:
https://travis-ci.org/qemu/qemu/jobs/480813811
slirp/ has a lot of as-yet-unfixed "takes address of packed member" bugs, which suggest it's a bit slapdash with alignment. Running it under the clang sanitizer to look for runtime alignment errors might also be interesting.)

thanks
-- PMM

Signature · 1 line
Marc-André Lureau
#9 lore raw

Hi

Quoted 25 lines · Marc-André Lureau

Hi

Quoted 21 lines · Peter Maydell
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Samuel Thibault
slirp/slirp.c +0 -0
@@ -851,7 +851,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     if (pkt_len < ETH_HLEN)
         return;
 
Quoted 7 lines

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 857

Quoted 5 lines
slirp/slirp.c +1 -1
-    proto = ntohs(*(uint16_t *)(pkt + 12));
+    proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);

What about using memcpy?

We should use whatever the new libslirp wants to consistently use as its mechanism for loading unaligned data. I don't suppose this is the only place where it ever needs to do this.

Quoted 8 lines
Quoted 3 lines

Personally I would vote for having libslirp have versions of the ld*_p functions, because they solve the problem in a clear and correct way. But that's up to Marc-André really.

I think I would go with a copy of qemu bswap.h, unless there is an equivalent in glib (I don't think so) or gnulib? Or other standard compiler solution.

The GStreamer solution is also quite readable.
https://gitlab.freedesktop.org/gstreamer/gstreamer/blob/master/gst/gstutils.h#L165

Signature · 1 line
Marc-André Lureau
#10 lore raw
Quoted 2 lines · Marc-André Lureau

The GStreamer solution is also quite readable.
https://gitlab.freedesktop.org/gstreamer/gstreamer/blob/master/gst/gstutils.h#L165

It has the disadvantage that you can never legitimately set GST_HAVE_UNALIGNED_ACCESS (because it is always undefined-behaviour by the C standard) and the slow-path versions are slow-and-clunky. Using malloc like the QEMU approach has the advantage of telling the compiler what you're doing so it can emit the optimal code on systems where it works.

thanks
-- PMM

#11 lore raw
In reply to Richard Henderson
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
time make docker-test-mingw@fedora SHOW_ENV=1 J=14 === TEST SCRIPT END ===

CC crypto/block.o
CC crypto/block-qcow.o
/tmp/qemu-test/src/block/sheepdog.c: In function 'find_vdi_name':
/tmp/qemu-test/src/block/sheepdog.c:1239:5: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]

     strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]