Recent

Author Topic: Redeclared constant as variable  (Read 2055 times)

440bx

  • Hero Member
  • *****
  • Posts: 1131
Re: Redeclared constant as variable
« Reply #45 on: July 27, 2019, 01:24:54 pm »
Yes, the strange thing is that I usually compile with the "-a" key to see the assembler code. It turned out that this is the reason.
Yes, the constant is not placed in a Read only section when -a is specified.

Quote
compiler FPC v3.0.4 - target Windows 32bit (both cases)


case 1.

compiled with -a

SigsegvAssembly.lpr:9             P := @X;
00401429 b83ca04000               mov    $0x40a03c,%eax
0040142E a300c04000               mov    %eax,0x40c000
SigsegvAssembly.lpr:10            P^ := 2; // Error SIGSEGV
00401433 c70002000000             movl   $0x2,(%eax)


0x400000, Image,         80 kB, WCX, 83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe
0x400000, Image: Commit,  4 kB, R,   83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe
0x401000, Image: Commit, 36 kB, RX,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe

; address $0x40a03c is in a RW section - P^ := 2; is perfectly fine

0x40a000, Image: Commit,  4 kB, RW,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe
0x40b000, Image: Commit,  4 kB, R,   83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe
0x40c000, Image: Commit,  8 kB, RW,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe

etc



; ------------------------------------

case 2.

compiled without -a

SigsegvAssembly.lpr:9             P := @X;
00401429 b800b04000               mov    $0x40b000,%eax
0040142E a300c04000               mov    %eax,0x40c000
SigsegvAssembly.lpr:10            P^ := 2; // Error SIGSEGV
00401433 c70002000000             movl   $0x2,(%eax)


0x400000, Image: Commit,  4 kB, R,   83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe,
0x401000, Image: Commit, 36 kB, RX,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe,
0x40a000, Image: Commit,  4 kB, RW,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe,

; address $0x40b000 is in a Read only section - P^ := 2; causes SIGSEGV

0x40b000, Image: Commit,  4 kB, R,   83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe,
0x40c000, Image: Commit,  8 kB, RW,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe,
0x40e000, Image: Commit,  4 kB, WC,  83_SigsegvAssembly\lib\i386-win32\SigsegvAssembly.exe,

etc



using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 8966
Re: Redeclared constant as variable
« Reply #46 on: July 27, 2019, 01:55:33 pm »
That's 3.0.4? Sure?
If I test with 3.2 or trunk it is always in the read only section, also on arm if {$J-} is in effect.
:
Code: ASM  [Select]
  1. .section .rodata.n_TC_$P$STATIC_$$_B
  2.         .balign 4
  3. .globl  TC_$P$STATIC_$$_B
  4.         .hidden TC_$P$STATIC_$$_B
  5.         .type   TC_$P$STATIC_$$_B,#object
  6. TC_$P$STATIC_$$_B:
  7.         .long   100

Given:
Code: Pascal  [Select]
  1. {$J-}const b:integer = 100;
  2. begin
  3.   writeln(b);
  4. end.
Compiled with
Code: Bash  [Select]
  1. fpc -a -CX -XXs -O4 -CfVFPv4 static.pas
I don't see a point in bringing this discussion any further. See my summary.
And https://wiki.freepascal.org/User_Changes_3.0#Literal_storage_memory_has_been_made_read-only so even 3.0.4 should adhere to that given {$J-}

« Last Edit: July 27, 2019, 02:03:48 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1131
Re: Redeclared constant as variable
« Reply #47 on: July 27, 2019, 02:09:42 pm »
That's 3.0.4? Sure?
It's the only compiler I have installed, therefore, for sure it is FPC v3.0.4

As far as the assembly code you posted, there is no guarantee that what the assembly code shows is what actually ends up in memory.  A bug someplace can cause what is produced to be different than what is spec-ed.

What I posted is what ends up in memory, executed by the CPU and, it is clear, in one case, the constant is in a read only section and in another case, it isn't.  That's a fact.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 8966
Re: Redeclared constant as variable
« Reply #48 on: July 27, 2019, 02:24:59 pm »
Yes {$J-} and {$J+} (sigh)
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1131
Re: Redeclared constant as variable
« Reply #49 on: July 27, 2019, 02:32:34 pm »
Yes {$J-} and {$J+} (sigh)

Here are a couple of suggestions you should consider following:

1. Follow your own advice:

Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.  (Among other things, you'd learn about Boolean operator precedence.)

2. Read the code:
Code: Pascal  [Select]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3. {$WRITEABLECONST OFF}
  4. const
  5.   X: Integer = 1;
  6. var
  7.   P: PInteger;
  8. begin
  9.   P := @X;
  10.   P^ := 2; // Error SIGSEGV
  11.   Writeln(X);
  12. end.
{$WRITEABLECONST OFF} means the statement
Code: Pascal  [Select]
  1.   P^ := 2; // Error SIGSEGV
should always be placed in a read only section causing it to always fail.

got it ?

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 8966
Re: Redeclared constant as variable
« Reply #50 on: July 27, 2019, 02:54:34 pm »
Yes, and it is always placed in a read-only section if the platform supports it, as Sven pointed out. (PascalDragon). I still can't see a bug. I see consistent behavior, got it?
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1131
Re: Redeclared constant as variable
« Reply #51 on: July 27, 2019, 03:06:26 pm »
Yes, and it is always placed in a read-only section if the platform supports it, as Sven pointed out. (PascalDragon). I still can't see a bug. I see consistent behavior, got it?
Yes, I got it.  I got that you haven't figured out yet, that on the Windows platform, read-only sections are supported.

You should really consider following your own advice: "Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner."


using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 8966
Re: Redeclared constant as variable
« Reply #52 on: July 27, 2019, 03:10:33 pm »
You are barking up the wrong tree: I always explained correct what happens, even identified the cause of your and others misunderstanding. But some people want to discuss "the bug".
As I and PascalDragon explained, it is NOT a bug but intended behavior. Read it back.
Most people that want to use threading should learn to patch their jeans first: use a needle.

PascalDragon

  • Hero Member
  • *****
  • Posts: 636
  • Compiler Developer
Re: Redeclared constant as variable
« Reply #53 on: July 28, 2019, 11:32:26 am »
"the platform supports it" in this case also means that the compiler's assembler writer needs to support it. In this specific case the GNU AS writer did not support it until I had fixed that in revision 35363 which was not merged back to fixes.