FpFtruncate broken on ppc (needs to use FPC_ABI_EABI but does not have it)
Original Reporter info from Mantis: somusque
-
Reporter name: Tobias Giesen
Original Reporter info from Mantis: somusque
- Reporter name: Tobias Giesen
Description:
FpFtruncate passes wrong parameters to the system (in FPC 3.0.4 too).
For example, truncating a file to a size of $0D will cause the file to have the size $D0000000D.
The lower 32 bits are duplicated and used as higher 32 bits too.
The OS seems to expect the 64 bit size in r5 and r6, but the size is passed in r4 and r5. In ossysc.inc, this would require the conditional define FPC_ABI_EABI but it is apparently not defined for ppc Linux.
As a solution, an additional 0 parameter must be inserted like this:
Do_syscall(syscall_nr_ftruncate64,TSysParam(fd),
0, // <-- additional parameter
TSysParam(Hi(flength),TSysParam(Lo(flength)));
This now passes the 64 bit size correctly.
I wonder which other calls might be affected in a similar manner.
Steps to reproduce:
program truncatetest;
uses BaseUnix,Unix,SysUtils,Classes;
var AHandle:THandle;
AStream:TFileStream;
begin
AStream:=TFileStream.Create('/home/tobias/a.txt',fmCreate);
// all these calls set the size to $D 0000 000D instead of just $D
AStream.Size:=13;
FileTruncate(AStream.Handle,13);
FpFtruncate(AStream.Handle,13);
AStream.Free;
end.
Additional information:
// This works correctly:
program truncatetest;
// ONLY FOR PPC 32 BIT
uses BaseUnix,Unix,SysUtils,Classes;
const syscall_nr_ftruncate64 = 194;
type
TSysResult = LongInt;
TSysParam = LongInt;
function Do_SysCall(sysnr,param1,param2,param3,param4:TSysParam):TSysResult; overload; external name 'FPC_SYSCALL4';
Function FpCorrectedFtruncate (fd : cInt; flength : TOff): cInt;
begin
Result:=Do_syscall(syscall_nr_ftruncate64,TSysParam(fd), 0,
tsysparam(hi(flength)),tsysparam(lo(flength)));
end;
var AHandle:THandle;
AStream:TFileStream;
begin
AStream:=TFileStream.Create('/home/tobias/a.txt',fmCreate);
FpCorrectedFtruncate(AStream.Handle,13); // OK
FpCorrectedFtruncate(AStream.Handle,$13300000002); // also OK
AStream.Free;
end.
Mantis conversion info:
- Mantis ID: 32772
- OS: Linux
- OS Build: Kernel 4.4.0-66
- Platform: powerpc 32 bit
- Version: 3.0.2
- Fixed in version: 3.1.1
- Fixed in revision: 37719 (#a3a7285d)