|
From: Masha N. (mnaret) <mn...@ci...> - 2013-07-24 14:06:09
|
Hello,
Could anyone please comment the issue described below:
Valgrind shows "invalid write" for memory allocated on the stack while the program runs normally.
I've attached a reproducer for this problem,
During the run two errors show in valgrind.log,
However only the second one is relevant (is the same as the one I have in code)
There are more details in my previous posts on this subject.
Attching once again
Thank you in advance,
Masha.
-----Original Message-----
From: Masha Naret (mnaret)
Sent: Wednesday, July 17, 2013 12:10 PM
To: 'Philippe Waroquiers'; 'lop...@gm...'
Cc: 'val...@li...'; Yanai, Omer (OY...@nd...)
Subject: RE: [Valgrind-users] Valgrind shows "Invalid write os size 4" for memory allocated for the stack
Hello,
Attaching the reproducer - please compile it with gcc -g tmpthread.c -o tmpthread -lpthread And then run: tmpthread argsFile Also attached argsFile and valgrind log.
There are two "Invalid write" errors in the log, but only the second one seems to be the same as in the original program.
Some more explanation:
In the original program I noticed valgrind reported the problem when the stack, while growing, grew from area with permissions 'rwx' to area with permissions 'rw'.
The difference in premissions happened since each time thread stack is created, it's lower and upper part are protected with mprotect(NONE) and when it's out of use the protection is set back to READ | WRITE.
(exactly as in reproducer)
However, when running the program with valgrind, for some reason initially the all the momery is set to 'rwx'.
Which is weird, since normally the memory for the stack shouldn't have the 'x' permission.
When running the program without valgrind, there's no memory with 'rwx', only 'rw'
If I modify the original program to restore also EXEC persmission, valgrind doesn't report any error, however this doesn't seem like a correct thing to do.
The same happens with the reproducer.
If you modify the code of the function restoring permissions to add PROT_EXEC persmissions :
static void ReleaseThreadStack(int stacksize, void** stackbase) {
int ret = 0;
void* ptStack = NULL;
//remove protection
ptStack = *stackbase - STACK_GUARD_SIZE;
ret = mprotect(ptStack, STACK_GUARD_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC );
ptStack = *stackbase + stacksize;
ret = mprotect(ptStack, STACK_GUARD_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
return;
}
The second "invalid write" problem will disappear!
If there’s need of any further logs, please let me know, I’ll send you valgrind debugger outputs.
Thank you very much for your support,
Masha.
-----Original Message-----
From: Philippe Waroquiers [mailto:phi...@sk...]
Sent: Wednesday, June 12, 2013 10:44 PM
To: Masha Naret (mnaret)
Cc: val...@li...
Subject: Re: [Valgrind-users] Valgrind shows "Invalid write os size 4" for memory allocated for the stack
On Mon, 2013-06-10 at 01:23 -0700, mnaret wrote:
> Hello,
> Recently I'm getting lot's of "invalid read/invalid write" valgrind
> errors which point out at memory allocated for the stack. However the
> code doesn't crush and finish running successfully.
> I'm trying to understand where the error comes from - and will be
> grateful fo any help wih this issue.
Do you have a small (compilable) reproducer ?
Philippe
|