From ad8653d6a2db95816c5f36d9bfabad959e8a4427 Mon Sep 17 00:00:00 2001 From: Artem Senichev Date: Mon, 30 Sep 2019 13:41:12 +0300 Subject: errl: Fix data reading from unaligned pointers Some architectures (like an ARM used in OpenBMC) do not support unaligned memory access. On these systems, reading an integral value from an unaligned pointer leads to undefined behavior. This patch doesn't change existing functionality, but forces the compiler to generate extra instructions to satisfy architectural alignment requirements. Resolves #185 Signed-off-by: Artem Senichev Change-Id: I6e75044b93c26cca76336d17bb3886fab403253a Reviewed-on: http://rchgit01.rchland.ibm.com/gerrit1/84520 Reviewed-by: Matt Derksen Reviewed-by: Christian R Geddes Tested-by: Jenkins Server Tested-by: Jenkins OP Build CI Tested-by: Jenkins OP HW Tested-by: FSP CI Jenkins Reviewed-by: Daniel M Crowell --- src/usr/targeting/common/xmltohb/xmltohb.pl | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/usr/targeting') diff --git a/src/usr/targeting/common/xmltohb/xmltohb.pl b/src/usr/targeting/common/xmltohb/xmltohb.pl index 58a5261da..339c26960 100755 --- a/src/usr/targeting/common/xmltohb/xmltohb.pl +++ b/src/usr/targeting/common/xmltohb/xmltohb.pl @@ -8,6 +8,7 @@ # # Contributors Listed Below - COPYRIGHT 2012,2019 # [+] International Business Machines Corp. +# [+] YADRO # # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -2980,7 +2981,7 @@ sub writeAttrErrlHFile { print $outFile " for (; (l_ptr + sizeof(uint32_t)) <= ((uint8_t*)i_pBuffer + i_buflen); )\n"; print $outFile " {\n"; print $outFile " // first 4 bytes is the attr enum\n"; - print $outFile " uint32_t attrEnum = ntohl(*(uint32_t *)l_ptr);\n"; + print $outFile " uint32_t attrEnum = ntohl(UINT32_FROM_PTR(l_ptr));\n"; print $outFile " l_ptr += sizeof(attrEnum);\n"; print $outFile " char* tmplabel = NULL;\n"; print $outFile "\n"; @@ -3112,21 +3113,21 @@ sub writeAttrErrlHFile { elsif (exists $attribute->{simpleType}->{uint16_t}) { print $outFile " l_traceEntry.resize(10+offset + $total_count * 7);\n"; print $outFile " for (uint32_t i = 0;i<$total_count;i++) {\n"; - print $outFile " sprintf(&(l_traceEntry[offset+i*7]), \"0x%.4X \", ntohs(*(((uint16_t *)l_ptr)+i)));\n"; + print $outFile " sprintf(&(l_traceEntry[offset+i*7]), \"0x%.4X \", ntohs(UINT16_FROM_PTR(reinterpret_cast(l_ptr) + i)));\n"; print $outFile " }\n"; print $outFile " l_ptr += $total_count * sizeof(uint16_t);\n"; } elsif (exists $attribute->{simpleType}->{uint32_t}) { print $outFile " l_traceEntry.resize(10+offset + $total_count * 11);\n"; print $outFile " for (uint32_t i = 0;i<$total_count;i++) {\n"; - print $outFile " sprintf(&(l_traceEntry[offset+i*11]), \"0x%.8X \", ntohl(*(((uint32_t *)l_ptr)+i)));\n"; + print $outFile " sprintf(&(l_traceEntry[offset+i*11]), \"0x%.8X \", ntohl(UINT32_FROM_PTR(reinterpret_cast(l_ptr)+i)));\n"; print $outFile " }\n"; print $outFile " l_ptr += $total_count * sizeof(uint32_t);\n"; } elsif (exists $attribute->{simpleType}->{uint64_t}) { print $outFile " l_traceEntry.resize(10+offset + $total_count * 19);\n"; print $outFile " for (uint32_t i = 0;i<$total_count;i++) {\n"; - print $outFile " sprintf(&(l_traceEntry[offset+i*19]), \"0x%.16llX \", ntohll(*(((uint64_t *)l_ptr)+i)));\n"; + print $outFile " sprintf(&(l_traceEntry[offset+i*19]), \"0x%.16llX \", ntohll(UINT64_FROM_PTR(reinterpret_cast(l_ptr)+i)));\n"; print $outFile " }\n"; print $outFile " l_ptr += $total_count * sizeof(uint64_t);\n"; } @@ -3140,21 +3141,21 @@ sub writeAttrErrlHFile { elsif (exists $attribute->{simpleType}->{int16_t}) { print $outFile " l_traceEntry.resize(10+offset + $total_count * 7);\n"; print $outFile " for (uint32_t i = 0;i<$total_count;i++) {\n"; - print $outFile " sprintf(&(l_traceEntry[offset+i*7]), \"0x%.4X \", ntohs(*(((int16_t *)l_ptr)+i)));\n"; + print $outFile " sprintf(&(l_traceEntry[offset+i*7]), \"0x%.4X \", ntohs(INT16_FROM_PTR(reinterpret_cast(l_ptr)+i)));\n"; print $outFile " }\n"; print $outFile " l_ptr += $total_count * sizeof(int16_t);\n"; } elsif (exists $attribute->{simpleType}->{int32_t}) { print $outFile " l_traceEntry.resize(10+offset + $total_count * 11);\n"; print $outFile " for (uint32_t i = 0;i<$total_count;i++) {\n"; - print $outFile " sprintf(&(l_traceEntry[offset+i*11]), \"0x%.8X \", ntohl(*(((int32_t *)l_ptr)+i)));\n"; + print $outFile " sprintf(&(l_traceEntry[offset+i*11]), \"0x%.8X \", ntohl(INT32_FROM_PTR(reinterpret_cast(l_ptr)+i)));\n"; print $outFile " }\n"; print $outFile " l_ptr += $total_count * sizeof(int32_t);\n"; } elsif (exists $attribute->{simpleType}->{int64_t}) { print $outFile " l_traceEntry.resize(10+offset + $total_count * 19);\n"; print $outFile " for (uint32_t i = 0;i<$total_count;i++) {\n"; - print $outFile " sprintf(&(l_traceEntry[offset+i*19]), \"0x%.16llX \", ntohll(*(((int64_t *)l_ptr)+i)));\n"; + print $outFile " sprintf(&(l_traceEntry[offset+i*19]), \"0x%.16llX \", ntohll(INT64_FROM_PTR(reinterpret_cast(l_ptr)+i)));\n"; print $outFile " }\n"; print $outFile " l_ptr += $total_count * sizeof(int64_t);\n"; } @@ -3950,11 +3951,11 @@ sub writeTargetErrlHFile { print $outFile " // first 4 are always the same\n"; print $outFile " if ((l_ptr32 + 4) <= (uint32_t *)((uint8_t*)i_pBuffer + i_buflen)) {\n"; - print $outFile " i_parser.PrintNumber( l_label, \"HUID = 0x%08X\", ntohl(*l_ptr32) );\n"; + print $outFile " i_parser.PrintNumber( l_label, \"HUID = 0x%08X\", ntohl(UINT32_FROM_PTR(l_ptr32)) );\n"; print $outFile " l_ptr32++;\n"; # find CLASS - print $outFile " switch (ntohl(*l_ptr32)) { // CLASS\n"; + print $outFile " switch (ntohl(UINT32_FROM_PTR(l_ptr32))) { // CLASS\n"; foreach my $enumerationType (@{$attributes->{enumerationType}}) { if( $enumerationType->{id} eq "CLASS" ) { @@ -3973,7 +3974,7 @@ sub writeTargetErrlHFile { print $outFile " l_ptr32++;\n"; # find TYPE - print $outFile " switch (ntohl(*l_ptr32)) { // TYPE\n"; + print $outFile " switch (ntohl(UINT32_FROM_PTR(l_ptr32))) { // TYPE\n"; foreach my $enumerationType (@{$attributes->{enumerationType}}) { if( $enumerationType->{id} eq "TYPE" ) { @@ -3992,7 +3993,7 @@ sub writeTargetErrlHFile { print $outFile " l_ptr32++;\n"; # find MODEL - print $outFile " switch (ntohl(*l_ptr32)) { // MODEL\n"; + print $outFile " switch (ntohl(UINT32_FROM_PTR(l_ptr32))) { // MODEL\n"; foreach my $enumerationType (@{$attributes->{enumerationType}}) { if( $enumerationType->{id} eq "MODEL" ) { @@ -4030,7 +4031,7 @@ sub writeTargetErrlHFile { } } - print $outFile " uint32_t l_pathType = ntohl(*l_ptr32);\n"; + print $outFile " uint32_t l_pathType = ntohl(UINT32_FROM_PTR(l_ptr32));\n"; print $outFile " if ((l_pathType == $attrPhysPath) || // ATTR_PHYS_PATH\n"; print $outFile " (l_pathType == $attrAffinityPath)) // ATTR_AFFINITY_PATH\n"; print $outFile " {\n"; -- cgit v1.2.1