Hello people,
I've just discovered that passing an invalid pointer to MemSize() results in a bogus value returned and no error generally reported. Is this intentional?
Example:
var
a: integer;
begin
writeln('MemSize(@a)=', MemSize(@a));
end.
Mailing lists / fpc-devel / 2010-02
9 messages · 6 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello people,
I've just discovered that passing an invalid pointer to MemSize() results in a bogus value returned and no error generally reported. Is this intentional?
Example:
var
a: integer;
begin
writeln('MemSize(@a)=', MemSize(@a));
end.Nikolai Zhubr wroteI've just discovered that passing an invalid pointer to MemSize() results in a bogus value returned and no error generally reported. Is this intentional?
It's just as intentional as dereferencing an invalid pointer can result in a segmentation fault, that freemem'ing an invalid pointer can corrupt the heap, etc. It's how manual memory management works: the programmer is responsible for not making any errors with pointers.
Jonas
In our previous episode, Jonas Maebe said:
... and that means assuming that you can't make out failsafe if a pointer is valid or not. One needs to manage that oneself.
I would immediately think that the memory size could either be stored at a negative offset in the memory block or in a table. Looking at the standard implementation in FPC it uses a negative offset:
from heap.inc:
function SysMemSize(p: pointer): ptruint;
begin
result := pmemchunk_fixed(pointer(p)-sizeof(tmemchunk_fixed_hdr))^.size;
if (result and fixedsizeflag) = 0 then
begin
result := result and sizemask;
dec(result, sizeof(tmemchunk_var_hdr));
end
else
begin
result := result and fixedsizemask;
dec(result, sizeof(tmemchunk_fixed_hdr));
end;
end;So it's impossible with this implementation to check if the pointer is valid or not.
I think it's possible for you to write your own memory manager which stores data about memory blocks in a table instead and is then able to check if the pointer is valid or not. Some operations would be slower in this case, but also safer.
Hello Felipe,
Wednesday, February 17, 2010, 12:56:15 PM, you wrote:
FMdC> I think it's possible for you to write your own memory manager which FMdC> stores data about memory blocks in a table instead and is then able to FMdC> check if the pointer is valid or not. Some operations would be slower FMdC> in this case, but also safer.
heaptrc has some settings for that. I think they are between defines so a recompile is needed but it performs that checks with a serious speed penalty and a very big penalty in memory needed.
I would immediately think that the memory size could either be stored at a negative…
In our previous episode, Felipe Monteiro de Carvalho said:
I think it's possible for you to write your own memory manager which stores data about memory blocks in a table instead and is then able to check if the pointer is valid or not. Some operations would be slower in this case, but also safer.
Such stuff is routinely done in debugging memory managers.
Afaik fastmm does this on Delphi. Together with having barriers before and after the allocation that are checked regularly to see if they are overwritten.
One could also fill deallocated blocks with random garbage. (trashing memmanager), afaik there is a simple one on BDN for Delphi that does this.
It lowers speed, but one can use it to find bugs/validate(*) an app, if possible at lower speed, and then switch back to the normal one. The FPC functionality to trash local vars (-gt) is also useful in this regard.
(*) of course such validation is never 100%In our previous episode, Jonas Maebe said: ... and that means assuming that you can't…
17.02.2010 13:35, Marco van de Voort:
Ok, thanks. I just hoped MemSize could be magically different from others :)
Nikolai
_______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel
In our previous episode, Felipe Monteiro de Carvalho said: Such stuff is routinely done…
Afaik fastmm does this on Delphi. Together with having barriers before and after the allocation that are checked regularly to see if they are overwritten.
I'm sure somebody "has to" have asked this before (maybe even me :-$ ), but has someone ever tried to port fastmm to FPC?
Best regards,
Flávio
In our previous episode, Fl?vio Etrusco said:
No idea. I don't even know how much sense it makes.
Some stuff (debug format(traces), dll and packages stuff) is probably not (or not yet) portable. The license is incompatible etc etc.
Better improve on the current memory managers (2.4/2.5 default memmgr and heaptrc)