C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Detecting Misalignments

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Pramod
Guest





PostPosted: Tue Jul 06, 2004 11:33 am    Post subject: Detecting Misalignments Reply with quote



Hi,

I don't know if this is the right place to ask this question.

{It probably isn't but as I cannot think of a better place ... -mod}

Is there any tool or any way to detect all the places where a
misaligned read can occur in the C/C++ code? Will Valgrind do that?

Thanks

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Antoun Kanawati
Guest





PostPosted: Tue Jul 06, 2004 9:12 pm    Post subject: Re: Detecting Misalignments Reply with quote



Pramod wrote:

Quote:
Hi,

I don't know if this is the right place to ask this question.

{It probably isn't but as I cannot think of a better place ... -mod}

Is there any tool or any way to detect all the places where a
misaligned read can occur in the C/C++ code? Will Valgrind do that?

Valgrind, purify, and similar tools, will detect problems WHEN they
occur [more precisely AFTER they occur]. If the problem "can occur" [as
opposed to "shall occur"], you need some static analysis tool, and I
don't know of any that looks for potential misalignments.

(did lint do that for C programs?)

Anyway, if you pick the right hardware, you can detect alignment errors
with GDB. The x86 performance drops on misaligned access, but it does
not crash. Other CPU families are very picky about alignment, and will
raise exceptions on misaligned access.
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pramod
Guest





PostPosted: Mon Jul 19, 2004 5:27 pm    Post subject: Re: Detecting Misalignments Reply with quote



Yes, lint does that C programs. There's a work around for misalignments. On
x86 platforms, misalignments don't occur at all since hardware takes care of
it and goes ahead with the read. I am working on Solaris, which does not
allow misligned reads but you can set trap handlers.

But I would like to correct the code where such misaligned reads occur. As a
general thing to know, what are all cases when a misaligned read can occur.
I can think of only one: trying to read wrong number of bytes in the middle
of a word. If anyone can tell me where all misaligned reads can occur,
that'll help a lot.

Thanks
-Pramod

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Falk Tannhäuser
Guest





PostPosted: Tue Jul 20, 2004 7:57 am    Post subject: Re: Detecting Misalignments Reply with quote

Pramod wrote:
Quote:
Yes, lint does that C programs. There's a work around for misalignments. On
x86 platforms, misalignments don't occur at all since hardware takes care of
it and goes ahead with the read. I am working on Solaris, which does not
allow misligned reads but you can set trap handlers.

But I would like to correct the code where such misaligned reads occur. As a
general thing to know, what are all cases when a misaligned read can occur.
I can think of only one: trying to read wrong number of bytes in the middle
of a word. If anyone can tell me where all misaligned reads can occur,
that'll help a lot.

Do you want statical (compile time) code analysis? (I imagine that runtime
analysis would consist in compiling your program with full debug info,
running and having it crash with Solaris and doing post-mortem debugging
of the 'core' file...)

Misaligned accesses generally occur as a result of casting a pointer
(or reference) to an object having lower alignment requirements to a
pointer (or reference) of a type having higher alignment requirements
- for example, when casting a 'char*' to 'int*' - followed by
dereferencing said pointer or accessing said reference.
Such casts can be performed in different manners. Suppose for example
you have
char c[sizeof(int)];
Then you can obtain a potentially misaligned pointer to 'int' at least
in the following ways:
/* 1) */ int* pi = (int*)c; // Old C-Style cast
/* 2) */ int* pi = reinterpret_cast<int*>(c);
/* 3) */ void* pv = c; /* ... */ int* pi = static_cast<int*>(pv);
/* 4) */ something more weird, like 'memcpy', or writing a
'char*' to a file, then reading it back as 'int*', or...

(1) should not occur in recent, well-written C++ code. gcc helps
you to spot all occurrences of C-style casts when you specify the
option '-Wold-style-cast' on the command line. Depending on the
size of your code base and the quantity of maintenance work to do,
you may consider converting all these casts to 'reinterpret_cast'

(2) A simple grep lets you localize all of these, so you can
check them.

(3) is more difficult to spot, especially when both casts appear
far apart from each other in the program. You would have to
check the origin of all the 'void*' pointers in your program
that are converted to 'int*' or something else having strict
alignment requirements. Similar for (4) - watch for all 'unusual'
ways of initializing pointers!

Falk


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.