View Issue Details

IDProjectCategoryView StatusLast Update
0035291LazarusPackagespublic2019-09-17 19:57
ReporterDenis KozlovAssigned ToMartin Friebe 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionsuspended 
Product Version2.0Product Build 
Target VersionFixed in Version 
Summary0035291: Pascal Script: Dynamic array result types cause Access Violation
DescriptionFunctions which use dynamic array types as results produce Access Violation in `fpc_dynarray_setlength` at assembler line `0040B511 833800 cmpl $0x0,(%eax)`.

Example function signature:

```
type TTestArrayType = Array of String;
function TestReturn: TTestArrayType;
```

It is worth noting that using dynamic array types as parameters, or changing the calling convention to `cdecl` works fine.

Tested on Windows 7 and 10:

* Lazarus 1.4.4 + FPC 2.6.4 = WORKS
* Lazarus 1.8.4 + FPC 3.0.4 = FAILS
* Lazarus 2.0.0 + FPC 3.0.4 = FAILS

Call stack:

```
#0 fpc_dynarray_setlength at :0
0000001 TESTRETURN(<error reading variable: Cannot access memory at address 0x0>) at project.pas:51
0000002 REALCALL_REGISTER(0x4017e0, 0, 0, 0, 0xfffffffc, 0, 4, 0x0) at x86.inc:113
0000003 TPSEXEC__INNERFUSECALL(0x0, 0x4017e0, CDREGISTER, 0x1826270, 0x183de80, <error reading variable>) at x86.inc:607
0000004 DELPHIFUNCTIONPROC(0x18a18f0, 0x1826ad0, 0x1815600, 0x1815660, CDREGISTER) at uPSRuntime.pas:11972
0000005 DELPHIFUNCTIONPROC_REGISTER(0x18a18f0, 0x1826ad0, 0x1815600, 0x1815660) at uPSRuntime.pas:11985
0000006 TPSEXEC__RUNSCRIPT(<error reading variable>) at uPSRuntime.pas:7873
0000007 TPSSCRIPT__EXECUTE(<error reading variable>) at uPSComponent.pas:678
0000008 TTESTAPPLICATION__DORUN(<error reading variable>) at project.pas:87
0000009 CUSTAPP$_$TCUSTOMAPPLICATION_$__$$_RUN at :0
0000010 main at project.pas:119
```
Steps To ReproduceSample project attached.

Open project in Lazarus, click Run, and enjoy an Access Violation.
TagsNo tags attached.
Fixed in Revision
LazTarget-
Widgetset
Attached Files

Activities

Denis Kozlov

2019-03-27 22:05

reporter  

PSArrayReturnTest.zip (591,874 bytes)

Denis Kozlov

2019-03-27 22:06

reporter   ~0115087

Is this due to some change in FPC calling convention related to how dynamic arrays are passed to functions, i.e. registers vs stack?

May be related to issue 0029230:
https://bugs.freepascal.org/view.php?id=29230

Martin Friebe

2019-03-28 16:14

manager   ~0115092

Does the issue also happen with the original? https://github.com/remobjects/pascalscript

We merely maintain a copy (with some very few mods, such as removing some none fpc parts).

    Bugs must be reported against the upstream project, and fixed there.


You can test with Lazarus trunk, which has a more up-to-date copy.

This issue will be closed, unless the it exists in Lazarus trunk, but not in upstream.

Denis Kozlov

2019-03-31 10:52

reporter   ~0115140

Same problem with latest Lazarus trunk and latest official Pascal Script.

Lazaus revision 60800 dated March 30, 2019.

Pascal Script commit bcf0f1b1b718f52006d518866ed82e04760b79c7 dated Feb 3, 2019.

Call stack:

#0 fpc_dynarray_setlength at :0
0000001 TESTRETURN(0x0) at project.pas:51
0000002 REALCALL_REGISTER(0x4017e0, 0, 0, 0, 0xfffffffc, 0, 4, 0x0) at .\pascalscript\Source\x86.inc:115
0000003 TPSEXEC__INNERFUSECALL(0x0, 0x4017e0, CDREGISTER, 0x16434f8, 0x165afc0, <error reading variable>) at .\pascalscript\Source\x86.inc:602
0000004 DELPHIFUNCTIONPROC(0x16d6570, 0x16461d8, 0x1632898, 0x16328f8, CDREGISTER) at .\pascalscript\Source\uPSRuntime.pas:11971
0000005 DELPHIFUNCTIONPROC_REGISTER(0x16d6570, 0x16461d8, 0x1632898, 0x16328f8) at .\pascalscript\Source\uPSRuntime.pas:11984
0000006 TPSEXEC__RUNSCRIPT(<error reading variable>) at .\pascalscript\Source\uPSRuntime.pas:7879
0000007 TPSSCRIPT__EXECUTE(<error reading variable>) at .\pascalscript\Source\uPSComponent.pas:679
0000008 TTESTAPPLICATION__DORUN(<error reading variable>) at project.pas:87
0000009 CUSTAPP$_$TCUSTOMAPPLICATION_$__$$_RUN at :0
0000010 main at project.pas:119

Denis Kozlov

2019-03-31 11:13

reporter   ~0115141

Reported upstream:
https://github.com/remobjects/pascalscript/issues/198

Martin Friebe

2019-09-17 19:57

manager   ~0118099

Suspended.
Please reopen, if fixed upstream.


Alternatively (not tested) try using "RegisterFunctionName"
Examples in EMScriptClasses in Lazurus/components/EditorMacroScript.
Though examples do not include dyn-array.

Issue History

Date Modified Username Field Change
2019-03-27 22:05 Denis Kozlov New Issue
2019-03-27 22:05 Denis Kozlov File Added: PSArrayReturnTest.zip
2019-03-27 22:06 Denis Kozlov Note Added: 0115087
2019-03-28 16:14 Martin Friebe LazTarget => -
2019-03-28 16:14 Martin Friebe Note Added: 0115092
2019-03-28 16:14 Martin Friebe Assigned To => Martin Friebe
2019-03-28 16:14 Martin Friebe Status new => feedback
2019-03-31 10:52 Denis Kozlov Note Added: 0115140
2019-03-31 10:52 Denis Kozlov Status feedback => assigned
2019-03-31 11:13 Denis Kozlov Note Added: 0115141
2019-09-17 19:57 Martin Friebe Status assigned => resolved
2019-09-17 19:57 Martin Friebe Resolution open => suspended
2019-09-17 19:57 Martin Friebe Note Added: 0118099