Getting FieldInfo should be aligned when FPC_REQUIRES_PROPER_ALIGNMENT
System Information
- Operating system: Linux
- Processor architecture: LoongArch64
- Compiler version: 3.3.1 (branch main)
Steps to reproduce
When I used Lazarus with the latest main ppcloongarch64, and taped Tools->Options, it wronged.
When I put ./lazarus to gdb, and ran it. It showed,
Thread 1 "lazarus-ide" received signal SIGSEGV, Segmentation fault.
0x00000001201aa880 in FindInFieldTable ($parentfp=0x7, Instance=0x7fffdf914690) at ../objpas/classes/reader.inc:1818
1818 PersistenClass := ClassTable^.Entries[FieldInfo^.ClassTypeIndex-1]^;
Then I printed FieldInfo^, it was strange. But when I printed the surrounding memory values, I could see they were organized. I thought it was because of the address of FieldInfo being wrong.
0x121c4b550: 0x00000000000007b8 0x646c697542110001
0x121c4b560: 0x626d6f4365646f4d 0x00000000786f426f
0x121c4b570: 0x00000000000007c0 0x646c697542150002
0x121c4b580: 0x616e614d65646f4d 0x6e6f747475426567
0x121c4b590: 0x00000000000007c8 0x646c697542140003
0x121c4b5a0: 0x656c655365646f4d 0x006c656e61507463
0x121c4b5b0: 0x00000000000007d0 0x6f747475420b0004
0x121c4b5c0: 0x00006c656e61506e || 0x00000000000007d8
0x121c4b5d0: 0x67657461430d0003 0x6c656e615079726f
0x121c4b5e0: 0x00000000000007e0 0x67657461430c0005
...
Lastly, I saw TFieldInfo defined with pack ifndef FPC_REQUIRES_PROPER_ALIGNMENT. I thought the method to get FieldInfo should be different.
Possible fixes
diff --git a/rtl/objpas/classes/reader.inc b/rtl/objpas/classes/reader.inc
index 568c3694f8..76158fbfa0 100644
--- a/rtl/objpas/classes/reader.inc
+++ b/rtl/objpas/classes/reader.inc
@@ -1821,7 +1821,11 @@ function TReader.FindComponentClass(const AName, anUnitName, AClassName: rawbyte
and PersistenClass.InheritsFrom(TComponent) then
exit(TComponentClass(PersistenClass));
end;
+{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
+{$else FPC_REQUIRES_PROPER_ALIGNMENT}
+ FieldInfo := PFieldInfo(align(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name), sizeof(pointer)));
+{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
end;
end;
// Try again with the parent class type