Skip to content

InvokeCall.inc: marshal sets whose PS and Delphi sizes differ - #322

Open
Vizit0r wants to merge 1 commit into
remobjects:masterfrom
Vizit0r:fix/set-size-mismatch
Open

Vizit0r wants to merge 1 commit into
remobjects:masterfrom
Vizit0r:fix/set-size-mismatch

Conversation

@Vizit0r

@Vizit0r Vizit0r commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Problem

PS sizes a set as ceil(members / 8) bytes. Delphi rounds 3 bytes up to 4, and on Win64 also 5..7 bytes up to 8:

members PS Delphi Win32 Delphi Win64
20 3 4 4
44 6 6 8

On the InvokeCall.inc path (the default since Delphi 10.1) the two sizes are assumed to be equal, so a set of 17..24 members (everywhere) or 33..56 members (Win64) breaks when it crosses the script/Delphi boundary:

  • result, 3 bytes: written with Longint(res.Dta^) := ..., one byte past the script variable;
  • result, 5..7 bytes: the RTTI lookup filters on TypeSize = aByteSize, finds nothing, skips the Invoke and InnerfuseCall still returns True. The native function is never called and the script gets an unwritten result, with no error at all;
  • var parameter: the callee writes its own size through the pointer, past the end of the script variable;
  • by-value parameter: the callee reads past the end.

Change (Source/InvokeCall.inc)

  • FindNativeSetType looks the Delphi type up by name with the size as a preference, not a filter: a type with the same name and size wins, otherwise the same name at Delphi's size. It returns PTypeInfo and the size, not a TRttiType, so nothing outlives the TRttiContext.
  • 3-byte sets: by value as a zero-extended Cardinal; as a result, the Cardinal is invoked and only 3 bytes are kept; as a var parameter, a 4-byte buffer.
  • Wider sets: by value, a zero-padded copy of the Delphi size (kept alive in TOpenArrayData.DataSet); as a result, min(PS, Delphi) bytes are copied; as a var parameter, a buffer of the Delphi size whose script share is copied back after the call.
  • Sets whose sizes agree resolve and travel exactly as before. The FPC branches keep their current behaviour except for the 3-byte cases.

Repro / testing

Delphi 13, current master, Win32 and Win64. Two native set types (20 and 44 members, declared in a unit interface so RTTI can see them), registered functions returning them, taking them by value and as var. In the script each set sits in a record followed by guard bytes, so writes past the end become visible:

                                                     master            this PR
                                                     Win32   Win64     Win32   Win64
result, 20 members                                   ok      ok        ok      ok
result, 44 members                                   ok      FAIL (*)  ok      ok
result into a record field, 20: guard byte intact    FAIL    FAIL      ok      ok
result into a record field, 44: guard bytes intact   ok      ok        ok      ok
by value, 20 / 44 members                            ok      ok        ok      ok
var, 20 members: guard byte intact                   FAIL    FAIL      ok      ok
var, 44 members: guard bytes intact                  ok      FAIL      ok      ok

(*) the native function was not called at all

The by-value over-read is not visible from the script (the extra bytes only hold bits of non-existent members); it is fixed the same way for safety.

We have been running the same approach in production since August (with a different type lookup); this PR rewrites it against the current upstream code.

PS sizes a set as ceil(members / 8) bytes. Delphi rounds 3 bytes up to 4,
and on Win64 5..7 bytes up to 8. On the InvokeCall path the two sizes
were assumed equal, which breaks sets of 17..24 members everywhere and
of 33..56 members on Win64:

- result, 3 bytes: written as a Longint, one byte past the variable;
- result, 5..7 bytes: the RTTI lookup filtered on size, found nothing,
  skipped the call and still returned True, so the script got an
  unwritten result without any error;
- var parameter: the callee writes its own size through the pointer,
  past the end of the script variable;
- by-value parameter: the callee reads past the end.

The Delphi type is now looked up by name with the size as a preference,
not a filter, and each direction copies only the bytes both sides have:
3-byte sets go through a zero-extended Cardinal, wider ones through a
zero-padded buffer of the Delphi size, and var sets copy the script's
share back after the call. Types whose sizes agree resolve and travel
exactly as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant