[or-cvs] r18743: {tor} Add and use set/get_uint64 on onion tags. [bug 604; backport (in tor/trunk: . src/common src/or)

nickm at seul.org nickm at seul.org
Mon Mar 2 19:15:05 UTC 2009


Author: nickm
Date: 2009-03-02 14:15:05 -0500 (Mon, 02 Mar 2009)
New Revision: 18743

Modified:
   tor/trunk/ChangeLog
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
   tor/trunk/src/or/cpuworker.c
Log:
Add and use set/get_uint64 on onion tags. [bug 604; backportable]

It seems that 64-bit Sparc Solaris demands 64-bit-aligned access to
uint64_t, but does not 64-bit-align the stack-allocated char array we
use for cpuworker tags.  So this patch adds a set/get_uint64 pair, and
uses them to access the conn_id field in the tag.

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2009-03-02 16:32:00 UTC (rev 18742)
+++ tor/trunk/ChangeLog	2009-03-02 19:15:05 UTC (rev 18743)
@@ -36,6 +36,9 @@
       timed out.  Bugfix on 0.1.2.1-alpha; our CLEAR debugging code
       had been suppressing the bug since 0.1.2.10-alpha.  Partial fix
       for bug 929.
+    - Do not assume that a stack-allocated character array will be
+      64-bit aligned on platforms that demand that uint64_t access is
+      aligned.  Possible fix for bug 604.
 
   o Minor features:
     - On Linux, use the prctl call to re-enable core dumps when the user

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2009-03-02 16:32:00 UTC (rev 18742)
+++ tor/trunk/src/common/compat.c	2009-03-02 19:15:05 UTC (rev 18743)
@@ -449,8 +449,21 @@
   return v;
 }
 /**
+ * Read a 32-bit value beginning at <b>cp</b>.  Equivalent to
+ * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
+ * unaligned memory access.
+ */
+uint64_t
+get_uint64(const char *cp)
+{
+  uint64_t v;
+  memcpy(&v,cp,8);
+  return v;
+}
+
+/**
  * Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
- * *(uint16_t)(cp) = v, but will not cause segfaults on platforms that forbid
+ * *(uint16_t*)(cp) = v, but will not cause segfaults on platforms that forbid
  * unaligned memory access. */
 void
 set_uint16(char *cp, uint16_t v)
@@ -459,13 +472,22 @@
 }
 /**
  * Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
- * *(uint32_t)(cp) = v, but will not cause segfaults on platforms that forbid
+ * *(uint32_t*)(cp) = v, but will not cause segfaults on platforms that forbid
  * unaligned memory access. */
 void
 set_uint32(char *cp, uint32_t v)
 {
   memcpy(cp,&v,4);
 }
+/**
+ * Set a 64-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
+ * *(uint64_t*)(cp) = v, but will not cause segfaults on platforms that forbid
+ * unaligned memory access. */
+void
+set_uint64(char *cp, uint64_t v)
+{
+  memcpy(cp,&v,8);
+}
 
 /**
  * Rename the file <b>from</b> to the file <b>to</b>.  On unix, this is

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2009-03-02 16:32:00 UTC (rev 18742)
+++ tor/trunk/src/common/compat.h	2009-03-02 19:15:05 UTC (rev 18743)
@@ -450,8 +450,10 @@
 
 uint16_t get_uint16(const char *cp) ATTR_PURE ATTR_NONNULL((1));
 uint32_t get_uint32(const char *cp) ATTR_PURE ATTR_NONNULL((1));
+uint64_t get_uint64(const char *cp) ATTR_PURE ATTR_NONNULL((1));
 void set_uint16(char *cp, uint16_t v) ATTR_NONNULL((1));
 void set_uint32(char *cp, uint32_t v) ATTR_NONNULL((1));
+void set_uint64(char *cp, uint64_t v) ATTR_NONNULL((1));
 
 /* These uint8 variants are defined to make the code more uniform. */
 #define get_uint8(cp) (*(const uint8_t*)(cp))

Modified: tor/trunk/src/or/cpuworker.c
===================================================================
--- tor/trunk/src/or/cpuworker.c	2009-03-02 16:32:00 UTC (rev 18742)
+++ tor/trunk/src/or/cpuworker.c	2009-03-02 19:15:05 UTC (rev 18743)
@@ -63,8 +63,8 @@
 tag_pack(char *tag, uint64_t conn_id, circid_t circ_id)
 {
   /*XXXX RETHINK THIS WHOLE MESS !!!! !NM NM NM NM*/
-  *(uint64_t *)tag     = conn_id;
-  *(uint16_t *)(tag+8) = circ_id;
+  set_uint64(tag, conn_id);
+  set_uint16(tag+8, circ_id);
 }
 
 /** Unpack <b>tag</b> into addr, port, and circ_id.
@@ -72,8 +72,8 @@
 static void
 tag_unpack(const char *tag, uint64_t *conn_id, circid_t *circ_id)
 {
-  *conn_id = *(const uint64_t *)tag;
-  *circ_id = *(const uint16_t *)(tag+8);
+  *conn_id = get_uint64(tag);
+  *circ_id = get_uint16(tag+8);
 }
 
 /** Called when the onion key has changed and we need to spawn new



More information about the tor-commits mailing list