Re: Fastcode Memory Read Rules



"Dennis" <marianndkc@xxxxxxxxxxxxxxx> wrote in message news:45f315d0@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi

The two rules are for different string types

"It is allowed to read past the end of AnsiStrings and WideStrings including
the dword containing the zero-terminator."

Maybe this can be cleared up ?
How far beyond the end of the ansistring can allways safely be read and is garuanteed to not violate the general rule ?

does this mean you can read past the end aslong as the dword you are reading is alligned and atleast contains the trailing #0 ?
I can't image that the allocator would waste more then maybe 8 bytes?
Having written a checking allocator and trying to allign on byte instead of 8 byte boundaries i quickly ran into problems with fastcode :)


"It is not allowed to read past the end of ShortStrings and PChar strings."

Sounds good because these pchars might not be allocated using the local memorymanager but might come from some outside system and alligned on the end of a page. so this might be a slightly overly strict way of enforcing the general rule.

They are both more strict than the global rule

"It is allowed to read anywhere from any memory page containing at least one
byte of a ShortString, PChar, AnsiString or WideString string, including a
#0 terminator."

Strictly speaking this rule is ok aslong as hardware memory protection stays the same, but be aware that the size of a page can change in the future.

If they are removed then we will have the global rule for all targets, which
is much easier to handle in the B&V. If the global rule is 100% safe, which
we believe it is, then we do not need the two stricter rules for the RTL
replacement target (now renamed as IA32 size penalty target).

The global rule might be safe in the sense that it won't cause accessviolations, and fine aslong as the rules for writing are alot more strict.

but code writters do need to take care to be able to proccess pchars that do not start on an alligned bounary and where the first byte after then #0 might fall in a page with no read rights.


Memory pages are 1024 byte and they are read/write protected on a page

4096bytes ?

basis. This means that if you can read one byte from the page then you can
read the entire page without getting an AV. This means that you can read any
X byte aligned X byte sized block from the page without reading across a
page boundary. If you read a string in 16 byte aligned chunks then you are
safe as long as at least one byte is on the page where the string is
located.

16 byte ? hmz i only allign on 8 bytes in my own paging allocator, so this could run into problems with fastcode ?

The stricter rules ensure that only memory allocated for the string is read.

AnsiStrings/WideStrings are 4 byte aligned and 0-3 bytes after the end of
the string is allocated for it.

ShortStrings/PChars are not guaranteed to be aligned.

Feel free to ask again. Perhaps somebody else will help me explain the
rules?

Best regards
Dennis Kjaer Christensen



.



Relevant Pages

  • [SUMMARY] AnsiString (#185)
    ... It would seem that writing Transfire's desired `ANSIString` class is ... Robert relies on other libraries to provide the actual ANSI codes; ... def register_lib lib_path, &blk ... string objects in a couple of ways: ...
    (comp.lang.ruby)
  • Re: Ascii und Ansi tauschen - Codeproblem
    ... eine Syntax "CharToOem AnsiString, AnsiString" die als Parameter 2x die selbe Variable bemüht für logisch, progammtechnisch zunmindest, unsinnig finde]. ... Was immer 'CharToOem AnsiString' bis hierhin tut erscheint logisch: der Funktionalität CharToOem wird ein Quelltext übergeben. ... Public Function ASCIItoANSI(ByVal AsciiString As String) As String ...
    (microsoft.public.de.word.vba)
  • Re: Did Borland doing well in Q4? Listen to the Earning CC
    ... Changing an type does not necessarily affect the application code. ... Because a String is *very* different from any ordinal type. ... to "ANSIString" etc. ... Not long after Tiburon is released you might hope to get a Unicode ...
    (borland.public.delphi.non-technical)
  • Re: SysFreeString - Is the memory mine to use as I please ?
    ... are mostly just an extra layer of overhead over the standard ... heap that provide services that most modern programs have no use for. ... OLE/COM, so that any time someone returns a string to you, you know how to ... The allocator used with Sys*String is specially tuned for rapid ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Fastcode AnsiStringReplace
    ... function AnsiStringReplace(const S, OldPattern, NewPattern: ... Flags: TReplaceFlags): AnsiString; ... TReplaceFlags): string; ...
    (borland.public.delphi.language.basm)