Hi everyone,

I am doing some work with Android and linuxptp so using Android toolchain 
(clang) to compile all the binaries.

When cross-compiling using clang for AARCH64 there are some warnings regarding 
unaligned access to members of packed structures. This unaligned access could 
cause app crash on ARM platforms.

Most of the issues is located within pmc_common.c file such as this one (and my 
workaround, using local variables) :

@@ -215,17 +225,24 @@ static void do_set_action(struct pmc *pmc, int action, 
int index, char *str)
                                                    "timeTraceable           %d 
"
                                                    "frequencyTraceable      %d 
"
                                                    "timeSource              
%hhx ",
-                                                   
&gsn.clockQuality.clockClass,
-                                                   
&gsn.clockQuality.clockAccuracy,
-                                                   
&gsn.clockQuality.offsetScaledLogVariance,
-                                                   &gsn.utc_offset,
+                                                  &clockClass,
+                                                  &clockAccuracy,
+                                                  &offsetScaledLogVariance,
+                                                  &utc_offset,
                                                    &leap_61,
                                                    &leap_59,
                                                    &utc_off_valid,
                                                    &ptp_timescale,
                                                    &time_traceable,
                                                    &freq_traceable,
-                                                   &gsn.time_source);
+                                                  &time_source);
+
+                             gsn.clockQuality.clockClass = clockClass ;
+                             gsn.clockQuality.clockAccuracy = clockAccuracy;
+                             gsn.clockQuality.offsetScaledLogVariance = 
offsetScaledLogVariance;
+                gsn.utc_offset = utc_offset;
+                gsn.time_source = time_source;
+

Another unaligned access in util.c :
------------------------------------ util.c ------------------------------------
index a59b559..dfab1b8 100644
@@ -346,11 +346,13 @@ int str2pid(const char *s, struct PortIdentity *result)
{
               struct PortIdentity pid;
               unsigned char *ptr = pid.clockIdentity.id;
+        unsigned int portNumber;
               int c;
               c = sscanf(s, " 
%02hhx%02hhx%02hhx.%02hhx%02hhx.%02hhx%02hhx%02hhx-%hu",
                                  &ptr[0], &ptr[1], &ptr[2], &ptr[3],
                                  &ptr[4], &ptr[5], &ptr[6], &ptr[7],
-                                 &pid.portNumber);
+                                &portNumber);
+                   pid.portNumber = portNumber;


Then a small issue is in rtnl.c where void pointer is used in arith. operation 
(see NLMSG_DATA in kernel  
https://elixir.bootlin.com/linux/v4.8/source/include/uapi/linux/netlink.h#L85), 
solution might be :

-#define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
+#define GENLMSG_DATA(glh) ((void *)((char *)NLMSG_DATA(glh) + GENL_HDRLEN))

Then second small issue might be there, missing include of string.h (however 
might be related to my integration to Android and different build system, so 
not really sure) :
--------------------------------- interface.c ---------------------------------
index 6c2630c..441f670 100644
@@ -4,6 +4,7 @@
  * @note Copyright (C) 2020 Richard Cochran 
richardcoch...@gmail.com<mailto:richardcoch...@gmail.com>
  * @note SPDX-License-Identifier: GPL-2.0+
  */
+#include <string.h>
#include <stdlib.h>
#include "interface.h"

And one small change to get Android happy (as Android already defines same 
macros 
https://cs.android.com/android/platform/superproject/+/master:bionic/libc/include/sys/endian.h;l=76;bpv=1?q=HTONS&ss=android%2Fplatform%2Fsuperproject
 ). This is not an issue, just improvement :
----------------------------------- tlv.c ------------------------------------
index 1c13460..4a8f0a7 100644
@@ -26,10 +26,18 @@
#include "tlv.h"
#include "msg.h"
-#define HTONS(x) (x) = htons(x)
-#define HTONL(x) (x) = htonl(x)
-#define NTOHS(x) (x) = ntohs(x)
-#define NTOHL(x) (x) = ntohl(x)
+#ifndef HTONS
+  #define HTONS(x) (x) = htons(x)
+#endif
+#ifndef HTONL
+  #define HTONL(x) (x) = htonl(x)
+#endif
+#ifndef NTOHS
+  #define NTOHS(x) (x) = ntohs(x)
+#endif
+#ifndef NTOHL
+  #define NTOHL(x) (x) = ntohl(x)
+#endif

Thanks for reading this super long email.

Kind Regards,


Ondrej Lutera
SW Engineer
NXP Semiconductors
Sochorova 3232/36
616 00 Brno Zabovresky
Czech Republic
Email: ondrej.lut...@nxp.com<mailto:ondrej.lut...@nxp.com>

[cid:image002.png@01D8D97A.90DE87D0]

_______________________________________________
Linuxptp-users mailing list
Linuxptp-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-users

Reply via email to