Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-04

Valgrind strscan

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hi.

In the morning I was hunting valgrind "Invalid read of size 4" in my code and finally I found it. Example:

program str;
uses
  SysUtils;
var
  p: PChar;
begin
  GetMem(p, 1);
  p^ := #0;
  StrScan(p, 'x');
  FreeMem(p);
end.

==14379== Invalid read of size 4
==14379== at 0x80708F0: SYSUTILS_STRSCAN$PCHAR$CHAR$$PCHAR (strings.inc:439) ==14379== by 0x804828A: main (str.pas:9)
==14379== Address 0x416A494 is 4 bytes inside a block of size 5 alloc'd ==14379== at 0x401D38B: malloc (vg_replace_malloc.c:149) ==14379== by 0x8063398: CMEM_CGETMEM$LONGWORD$$POINTER (cmem.pp:50) ==14379== by 0x805AFA6: SYSTEM_GETMEM$POINTER$LONGWORD (heap.inc:235) ==14379== by 0x8048276: main (str.pas:7)

Problem is in assembler code for i386. rtl/i386/strings.inc. This routine reads always 4 bytes alligned by 4 regardless string length.

function strscan(p : pchar;c : char) : pchar;assembler; ...
// load new 4 bytes
movl (%edi),%edx <-here valgrind report Invalid read ...

Problem was discovered aleady here http://bugs.freepascal.org/view.php?id=6314

My assembler knowledge is unfortunately not so good yet to effectively repair it. Besides routine is very sophisticated. :)
But I think that solving this cosmetic error will save many developers hours during valgrind code checking. And preparing testcases fo fpc developers.

Petr

Ing. Petr Kristan . EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice tel: +420 466335223 Czech Republic (Eastern Europe) fax: +420 466510709

Reply by email

Jonas Maebe
to Unknown author: Hi. In the morning I was hunting valgrind "Invalid read of size 4" in my code and finally…
Show quoted text 30 lines
==14379== Invalid read of size 4 ==14379== at 0x80708F0: SYSUTILS_STRSCAN$PCHAR$CHAR$$PCHAR (strings.inc:439) ==14379== by 0x804828A: main (str.pas:9) ==14379== Address 0x416A494 is 4 bytes inside a block of size 5 alloc'd ==14379== at 0x401D38B: malloc (vg_replace_malloc.c:149) ==14379== by 0x8063398: CMEM_CGETMEM$LONGWORD$$POINTER (cmem.pp:50) ==14379== by 0x805AFA6: SYSTEM_GETMEM$POINTER$LONGWORD (heap.inc: 235) ==14379== by 0x8048276: main (str.pas:7) Problem is in assembler code for i386. rtl/i386/strings.inc. This routine reads always 4 bytes alligned by 4 regardless string length. function strscan(p : pchar;c : char) : pchar;assembler; ... // load new 4 bytes movl (%edi),%edx <-here valgrind report Invalid read ... Problem was discovered aleady here http://bugs.freepascal.org/view.php?id=6314 My assembler knowledge is unfortunately not so good yet to effectively repair it. Besides routine is very sophisticated. :) But I think that solving this cosmetic error will save many developers hours during valgrind code checking. And preparing testcases fo fpc developers.

It is a false positive by Valgrind. That code intentionally does not care about reading uninitialised memory from time to time, and treats that situation correctly. There is similar code in glibc, but Valgrind comes with standard suppression files for glibc which suppress these false positives. I'm not aware of any suppression files for FPC.

Jonas

Reply by email

Unknown author
to Jonas Maebe
Jonas Maebe wrote 40 lines
On 10 Apr 2008, at 14:50, wrote: >==14379== Invalid read of size 4 >==14379== at 0x80708F0: SYSUTILS_STRSCAN$PCHAR$CHAR$$PCHAR >(strings.inc:439) >==14379== by 0x804828A: main (str.pas:9) >==14379== Address 0x416A494 is 4 bytes inside a block of size 5 >alloc'd >==14379== at 0x401D38B: malloc (vg_replace_malloc.c:149) >==14379== by 0x8063398: CMEM_CGETMEM$LONGWORD$$POINTER (cmem.pp:50) >==14379== by 0x805AFA6: SYSTEM_GETMEM$POINTER$LONGWORD (heap.inc: >235) >==14379== by 0x8048276: main (str.pas:7) > >Problem is in assembler code for i386. rtl/i386/strings.inc. >This routine reads always 4 bytes alligned by 4 regardless string >length. > >function strscan(p : pchar;c : char) : pchar;assembler; >... >// load new 4 bytes > movl (%edi),%edx <-here valgrind report Invalid read >... > >Problem was discovered aleady here >http://bugs.freepascal.org/view.php?id=6314 > >My assembler knowledge is unfortunately not so good yet to >effectively repair it. >Besides routine is very sophisticated. :) >But I think that solving this cosmetic error will save many developers >hours during valgrind code checking. And preparing testcases fo fpc >developers. It is a false positive by Valgrind. That code intentionally does not care about reading uninitialised memory from time to time, and treats that situation correctly. There is similar code in glibc, but Valgrind comes with standard suppression files for glibc which suppress these false positives. I'm not aware of any suppression files for FPC.

I think that this is not false positive. We allocate 1 byte and read 4, report is right. But obviously effective solution do not exists.

What about to establish valgind suppressions file in fpc tree? Here is first record.
{
Assembler routine always reads 4 bytes alligned by 4 regardles str length Memcheck:Addr4
fun:SYSUTILS_STRSCAN$PCHAR$CHAR$$PCHAR
}

Petr

Ing. Petr Kristan . EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice tel: +420 466335223 Czech Republic (Eastern Europe) fax: +420 466510709

Reply by email

Jonas Maebe
to Unknown author
Show quoted text 13 lines
On Thu, Apr 10, 2008 at 03:09:34PM +0200, Jonas Maebe wrote: > > It is a false positive by Valgrind. That code intentionally does not > care about reading uninitialised memory from time to time, and treats > that situation correctly. There is similar code in glibc, but > Valgrind > comes with standard suppression files for glibc which suppress these > false positives. I'm not aware of any suppression files for FPC. I think that this is not false positive. We allocate 1 byte and read 4, report is right. But obviously effective solution do not exists.

It is a false positive because users read Valgrind reports as lists of error locations in their program (which is indeed how the MemCheck backend of Valgrind is supposed to be used).

Show quoted text 8 lines
What about to establish valgind suppressions file in fpc tree? Here is first record. { Assembler routine always reads 4 bytes alligned by 4 regardles str length Memcheck:Addr4 fun:SYSUTILS_STRSCAN$PCHAR$CHAR$$PCHAR }

If you submit such a file, we can add it to svn.

Jonas

Reply by email

Unknown author
to Jonas Maebe
Jonas Maebe wrote 30 lines
On 10 Apr 2008, at 15:32, wrote: >On Thu, Apr 10, 2008 at 03:09:34PM +0200, Jonas Maebe wrote: >> > >>It is a false positive by Valgrind. That code intentionally does not >>care about reading uninitialised memory from time to time, and treats >>that situation correctly. There is similar code in glibc, but >>Valgrind >>comes with standard suppression files for glibc which suppress these >>false positives. I'm not aware of any suppression files for FPC. > >I think that this is not false positive. We allocate 1 byte and read >4, >report is right. But obviously effective solution do not exists. It is a false positive because users read Valgrind reports as lists of error locations in their program (which is indeed how the MemCheck backend of Valgrind is supposed to be used). >What about to establish valgind suppressions file in fpc tree? >Here is first record. >{ > Assembler routine always reads 4 bytes alligned by 4 regardles str >length > Memcheck:Addr4 > fun:SYSUTILS_STRSCAN$PCHAR$CHAR$$PCHAR >} If you submit such a file, we can add it to svn.

No problem. Here it is.

Petr

Ing. Petr Kristan . EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice tel: +420 466335223 Czech Republic (Eastern Europe) fax: +420 466510709

Reply by email

Jonas Maebe
to Unknown author
Show quoted text 7 lines
On Thu, Apr 10, 2008 at 03:52:42PM +0200, Jonas Maebe wrote: > > If you submit such a file, we can add it to svn. No problem. Here it is.

Thanks, committed (to the fpcbuild repository rather than to the fpc repository, because it is an installation-related file rather than a compilation-related file).

Jonas

Reply by email