Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: VSCode gives error when formatting #1218

Open
simonjwright opened this issue Oct 23, 2024 · 21 comments
Open

[Bug]: VSCode gives error when formatting #1218

simonjwright opened this issue Oct 23, 2024 · 21 comments
Assignees
Labels

Comments

@simonjwright
Copy link
Contributor

simonjwright commented Oct 23, 2024

Environment

  • OS and Version: macOS Sequoia 15.0.1
  • IDE Version: 1.94.2
  • Ada & SPARK Extension Version: ? latest, I think I got it to update

Bug Summary and Reproducer

Bug Summary:

Error - 21:13:44] Request textDocument/formatting failed.
  Message: GNATformat failed to format source
  Code: -32603 ```

Steps to reproduce: 

1. Make a project using the source in [formatting_control.zip](https://github.com/user-attachments/files/17497887/formatting_control.zip), cf #1217
1. Select _Format Document_ from the right button menu.

Expected behavior:
Code formatted (I was hoping to compare the result with #1217).

### Configuration and Logs

[ALS.MAIN] ALS version: 25.0.20241014 ()
[ALS.MAIN] Initializing server ...
[ALS.MAIN] GPR PATH: 
[ALS.MAIN] PATH: /Library/Frameworks/Python.framework/Versions/3.10/bin:/Users/simon/Library/Python/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/opt/gcc-14.2.0-1-aarch64/bin:/Users/simon/.alire/bin:/Users/simon/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/MacGPG2/bin:/Library/TeX/texbin:/Users/simon/bin
[ALS.MAIN] Ada version used for predefined completion: ADA_2020
[ALS.MAIN] In Message_Handler Text_Document_Did_Open URI:
[ALS.MAIN] file:///Users/simon/Developer/bugs/gnat/demo.adb
[ALS.MAIN] Looking for a project... Root:
[ALS.MAIN] file:///Users/simon/Developer/bugs/gnat
[ALS.MAIN] Loading:
[ALS.MAIN] /Users/simon/Developer/bugs/gnat/demo.gpr
[ALS.MAIN] GPR2 Log Messages:
[ALS.MAIN] Prepend Context Id: file:///Users/simon/Developer/bugs/gnat/demo.gpr
[ALS.MAIN] Diag: 
_ALS.MAIN_ (PROJECT_TYPE => SINGLE_PROJECT_FOUND,
_ALS.MAIN_  STATUS => VALID_PROJECT,
_ALS.MAIN_  PROJECT_FILE => {GNATCOLL.VFS.VIRTUAL_FILE object},
_ALS.MAIN_  MISSING_ADA_RUNTIME => FALSE,
_ALS.MAIN_  GPR2_MESSAGES => {GPR2.LOG.OBJECT object})
[ALS.MAIN] Out Message_Handler Text_Document_Did_Open
[ALS.MAIN] GPR2 Log Messages:
[ALS.MAIN] Prepend Context Id: file:///Users/simon/Developer/bugs/gnat/demo.gpr
[ALS.MAIN] Diag: 
_ALS.MAIN_ (PROJECT_TYPE => CONFIGURED_PROJECT,
_ALS.MAIN_  STATUS => VALID_PROJECT,
_ALS.MAIN_  PROJECT_FILE => {GNATCOLL.VFS.VIRTUAL_FILE object},
_ALS.MAIN_  MISSING_ADA_RUNTIME => FALSE,
_ALS.MAIN_  GPR2_MESSAGES => {GPR2.LOG.OBJECT object})
[ALS.MAIN] in GNATformat Format
[ALS.MAIN] raised CONSTRAINT_ERROR : erroneous memory access
_ALS.MAIN_ 0x0000000105847D68 0x0000000105847DA0 0x00000001058EA6AC 0x00000001058EB5F4 0x0000000104B292C5 0x0000000104B36C5C 0x0000000104A8C0A0 0x0000000104A3C29C 0x00000001049DB52C 0x0000000104A03844 0x0000000104A02DE8 0x0000000104A03058 0x0000000104A03C0C 0x0000000104A03240 0x0000000104A03854 0x0000000104A03884 0x0000000104A02DE8 0x0000000104A04F8C 0x00000001049996E8 0x00000001035BD344 0x00000001035BD620 0x00000001034AFBC8 0x00000001034962DC 0x000000010354930C 0x000000010353D33C 0x000000010252074C 0x000000010252D25C 0x000000010350A53C 0x0000000105831980 0x000000019EE8F2E0
[ALS.MAIN] 0x0000000105847D68 0x0000000105847DA0 0x00000001058EA6AC 0x00000001058EB5F4 0x0000000104B292C5 0x0000000104B36C5C 0x0000000104A8C0A0 0x0000000104A3C29C 0x00000001049DB52C 0x0000000104A03844 0x0000000104A02DE8 0x0000000104A03058 0x0000000104A03C0C 0x0000000104A03240 0x0000000104A03854 0x0000000104A03884 0x0000000104A02DE8 0x0000000104A04F8C 0x00000001049996E8 0x00000001035BD344 0x00000001035BD620 0x00000001034AFBC8 0x00000001034962DC 0x000000010354930C 0x000000010353D33C 0x000000010252074C 0x000000010252D25C 0x000000010350A53C 0x0000000105831980 0x000000019EE8F2E0

### Other VS Code Extensions

_No response_

### Additional context

re: exception traces & macOS:

Compiling with `-g` and `-bargs -E`, `Ada.Exceptions.Exception_Information` gives a much more useful trace. The test program does a recursive call, counting down, and raises an exception on reaching a limit:

$ ./raiser
caught raised CONSTRAINT_ERROR : a message
Load address: 0x10261c000
Call stack traceback locations:
0x102620a7c 0x102620adc 0x10261eb54 0x10261eb7c 0x10261eb7c 0x10261eb7c 0x10261eb7c 0x10261eb7c 0x10261ebc0 0x10261eacc

$ atos -o raiser -l 0x10261c000 0x102620a7c 0x102620adc 0x10261eb54 0x10261eb7c 0x10261eb7c 0x10261eb7c 0x10261eb7c 0x10261eb7c 0x10261ebc0 0x10261eacc
ada__exceptions__complete_and_propagate_occurrence (in raiser) (a-except.adb:1128)
__gnat_raise_exception (in raiser) (a-except.adb:1165)
raiser__inner.0 (in raiser) (raiser.adb:7)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
_ada_raiser (in raiser) (raiser.adb:12)
main (in raiser) (b~raiser.adb:227)

@joaopsazevedo joaopsazevedo self-assigned this Oct 24, 2024
@joaopsazevedo
Copy link
Contributor

Hello @simonjwright. Thanks for reporting this.

I was not able to reproduce on Linux. I suspect that this will be specific to the aarch64 ALS build. I'll try to reproduce on a Mac.

I have a couple questions though.

Compiling with -g and -bargs -E, Ada.Exceptions.Exception_Information gives a much more useful trace. The test program does a recursive call, counting down, and raises an exception on reaching a limit

What exactly was compiled here? ALS?

$ ./raiser

Can you elaborate on what raiser is?

@simonjwright
Copy link
Contributor Author

Sorry to be unclear.

I don’t know how the stack dump in my bug report was produced, but - and I assume here it’s from a caught exception? - if you compile with -g (I assume you do, since the download includes ada_language_server.dSYM) and bind with -Es, you could use instead Ada.Exceptions.Exception_Message, which produces

  • on macOS, a dump like this
    caught raised CONSTRAINT_ERROR : a message
    Load address: 0x104604000
    Call stack traceback locations:
    0x104608a7c 0x104608adc 0x104606460 0x104606488 0x104606488 0x104606488 0x104606488 0x104606488 0x1046064cc 0x104606d78
    
    where you give the Load Address to atos’s -l switch.
  • on other OSs (well, at least Linux?) you should get a nicely symbolicated stack dump.

This is raiser's GPR

project Raiser is

   for Main use ("raiser.adb");

   package Builder is
      for Default_Switches ("ada") use ("-g");
   end Builder;

   package Binder is
      for default_switches ("ada") use ("-Es");
   end Binder;

end Raiser;

This is the source

with Ada.Exceptions;
with Ada.Text_IO;
procedure Raiser is
   procedure Inner(N: Natural) is
   begin
      if N <= 5 then
         raise Constraint_Error with "a message";
      end if;
      Inner (N - 1);
   end Inner;
begin
   Inner(10);
exception
   when E : others =>
      Ada.Text_IO.Put_Line
        ("caught " & Ada.Exceptions.Exception_Information (E));
end Raiser;

and this is the execution and stack dump processing

$ ./raiser
caught raised CONSTRAINT_ERROR : a message
Load address: 0x104604000
Call stack traceback locations:
0x104608a7c 0x104608adc 0x104606460 0x104606488 0x104606488 0x104606488 0x104606488 0x104606488 0x1046064cc 0x104606d78

ramoth:tmp simon$ atos -o raiser -l 0x104604000 0x104608a7c 0x104608adc 0x104606460 0x104606488 0x104606488 0x104606488 0x104606488 0x104606488 0x1046064cc 0x104606d78
ada__exceptions__complete_and_propagate_occurrence (in raiser) (a-except.adb:1128)
__gnat_raise_exception (in raiser) (a-except.adb:1165)
raiser__inner.0 (in raiser) (raiser.adb:7)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
raiser__inner.0 (in raiser) (raiser.adb:9)
_ada_raiser (in raiser) (raiser.adb:12)
main (in raiser) (b__raiser.adb:230)

@rongcuid
Copy link

Same issue:

Log directory is /Users/rongcuid/.als/ada_ls_log.2024-11-19T133335.log
[Error - 1:33:58 PM] Request textDocument/formatting failed.
  Message: GNATformat failed to format source
  Code: -32603 
$ cat /Users/rongcuid/.als/ada_ls_log.2024-11-19T133335.log
[ALS.MAIN] ALS version: 26.0.202411173 ()
[ALS.MAIN] Initializing server ...
[ALS.MAIN] GPR PATH: 
[ALS.MAIN] PATH: /Users/rongcuid/.elan/bin:/usr/bin:/bin:/usr/sbin:/sbin
[ALS.MAIN] Ada version used for predefined completion: ADA_2012
[ALS.MAIN] In Message_Handler Text_Document_Did_Open URI:
[ALS.MAIN] file:///Users/rongcuid/tmp/adalisp/src/adalisp.adb
[ALS.PROJECT] Looking for a project in root: file:///Users/rongcuid/tmp/adalisp
[ALS.PROJECT] The workspace is an Alire crate
[ALS.PROJECT] Determining project from 'alr show' output
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] Setting environment from 'alr printenv'
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] No 'alr' in the PATH.
[ALS.PROJECT] Loading:
[ALS.PROJECT] /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project: /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project with GPR2
[ALS.PROJECT] GPR2 messages after load:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Updating project sources
[ALS.PROJECT] GPR2 messages after updating sources:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Prepend Context Id: file:///Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Project status after loading: 
_ALS.PROJECT_ (PROJECT_TYPE => SINGLE_PROJECT_FOUND,
_ALS.PROJECT_  STATUS => VALID_PROJECT_WITH_WARNING,
_ALS.PROJECT_  PROJECT_FILE => {GNATCOLL.VFS.VIRTUAL_FILE object},
_ALS.PROJECT_  MISSING_ADA_RUNTIME => TRUE,
_ALS.PROJECT_  GPR2_MESSAGES => {GPR2.LOG.OBJECT object})
[ALS.MAIN] Out Message_Handler Text_Document_Did_Open
[ALS.PROJECT] Reload_Project was called
[ALS.PROJECT] ada.projectFile is not set. We will try to find the project automatically.
[ALS.PROJECT] Looking for a project in root: file:///Users/rongcuid/tmp/adalisp
[ALS.PROJECT] The workspace is an Alire crate
[ALS.PROJECT] Determining project from 'alr show' output
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] Setting environment from 'alr printenv'
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] No 'alr' in the PATH.
[ALS.PROJECT] Loading:
[ALS.PROJECT] /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project: /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project with GPR2
[ALS.PROJECT] GPR2 messages after load:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Updating project sources
[ALS.PROJECT] GPR2 messages after updating sources:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Prepend Context Id: file:///Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Project status after loading: 
_ALS.PROJECT_ (PROJECT_TYPE => SINGLE_PROJECT_FOUND,
_ALS.PROJECT_  STATUS => VALID_PROJECT_WITH_WARNING,
_ALS.PROJECT_  PROJECT_FILE => {GNATCOLL.VFS.VIRTUAL_FILE object},
_ALS.PROJECT_  MISSING_ADA_RUNTIME => TRUE,
_ALS.PROJECT_  GPR2_MESSAGES => {GPR2.LOG.OBJECT object})
[ALS.MAIN] in GNATformat Format
[ALS.MAIN] raised CONSTRAINT_ERROR : erroneous memory access
_ALS.MAIN_ 0x000000010636E448 0x000000010636E480 0x0000000106410D8C 0x0000000106411CD4 0x000000010564BB05 0x000000010565949C 0x00000001055AE740 0x000000010555F49C 0x00000001054F414C 0x000000010552615C 0x00000001055256F4 0x0000000105525964 0x0000000105526524 0x00000001055264D4 0x0000000105525B4C 0x000000010552616C 0x000000010552619C 0x00000001055256F4 0x0000000105527CCC 0x00000001054B0104 0x0000000104054764 0x0000000104054A40 0x0000000103F431A8 0x0000000103F2971C 0x0000000103FDE60C 0x0000000103FD265C 0x0000000102FB3C0C 0x0000000102FC071C 0x0000000103F9F7BC 0x0000000106358060 0x0000000199BF5F90
[ALS.MAIN] 0x000000010636E448 0x000000010636E480 0x0000000106410D8C 0x0000000106411CD4 0x000000010564BB05 0x000000010565949C 0x00000001055AE740 0x000000010555F49C 0x00000001054F414C 0x000000010552615C 0x00000001055256F4 0x0000000105525964 0x0000000105526524 0x00000001055264D4 0x0000000105525B4C 0x000000010552616C 0x000000010552619C 0x00000001055256F4 0x0000000105527CCC 0x00000001054B0104 0x0000000104054764 0x0000000104054A40 0x0000000103F431A8 0x0000000103F2971C 0x0000000103FDE60C 0x0000000103FD265C 0x0000000102FB3C0C 0x0000000102FC071C 0x0000000103F9F7BC 0x0000000106358060 0x0000000199BF5F90

@rongcuid
Copy link

I don't know if this is at all related, but note that Mac OS's ARM64 calling convention is different from ARM64's official AAPCS64. I've worked with C-FFI for ARM64 on other languages, so I just want to point this out:

@AnthonyLeonardoGracio
Copy link
Collaborator

Same issue:

Log directory is /Users/rongcuid/.als/ada_ls_log.2024-11-19T133335.log
[Error - 1:33:58 PM] Request textDocument/formatting failed.
  Message: GNATformat failed to format source
  Code: -32603 
$ cat /Users/rongcuid/.als/ada_ls_log.2024-11-19T133335.log
[ALS.MAIN] ALS version: 26.0.202411173 ()
[ALS.MAIN] Initializing server ...
[ALS.MAIN] GPR PATH: 
[ALS.MAIN] PATH: /Users/rongcuid/.elan/bin:/usr/bin:/bin:/usr/sbin:/sbin
[ALS.MAIN] Ada version used for predefined completion: ADA_2012
[ALS.MAIN] In Message_Handler Text_Document_Did_Open URI:
[ALS.MAIN] file:///Users/rongcuid/tmp/adalisp/src/adalisp.adb
[ALS.PROJECT] Looking for a project in root: file:///Users/rongcuid/tmp/adalisp
[ALS.PROJECT] The workspace is an Alire crate
[ALS.PROJECT] Determining project from 'alr show' output
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] Setting environment from 'alr printenv'
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] No 'alr' in the PATH.
[ALS.PROJECT] Loading:
[ALS.PROJECT] /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project: /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project with GPR2
[ALS.PROJECT] GPR2 messages after load:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Updating project sources
[ALS.PROJECT] GPR2 messages after updating sources:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Prepend Context Id: file:///Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Project status after loading: 
_ALS.PROJECT_ (PROJECT_TYPE => SINGLE_PROJECT_FOUND,
_ALS.PROJECT_  STATUS => VALID_PROJECT_WITH_WARNING,
_ALS.PROJECT_  PROJECT_FILE => {GNATCOLL.VFS.VIRTUAL_FILE object},
_ALS.PROJECT_  MISSING_ADA_RUNTIME => TRUE,
_ALS.PROJECT_  GPR2_MESSAGES => {GPR2.LOG.OBJECT object})
[ALS.MAIN] Out Message_Handler Text_Document_Did_Open
[ALS.PROJECT] Reload_Project was called
[ALS.PROJECT] ada.projectFile is not set. We will try to find the project automatically.
[ALS.PROJECT] Looking for a project in root: file:///Users/rongcuid/tmp/adalisp
[ALS.PROJECT] The workspace is an Alire crate
[ALS.PROJECT] Determining project from 'alr show' output
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] Setting environment from 'alr printenv'
[ALS.PROJECT] Encountered errors with Alire:
_ALS.PROJECT_ No alr in the PATH
[ALS.PROJECT] No 'alr' in the PATH.
[ALS.PROJECT] Loading:
[ALS.PROJECT] /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project: /Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Loading project with GPR2
[ALS.PROJECT] GPR2 messages after load:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Updating project sources
[ALS.PROJECT] GPR2 messages after updating sources:
   [ALS.PROJECT] embedded_kb/kb: warning: can't find a toolchain for the following configuration: language 'Ada', target 'aarch64-darwin', default runtime
[ALS.PROJECT] Prepend Context Id: file:///Users/rongcuid/tmp/adalisp/adalisp.gpr
[ALS.PROJECT] Project status after loading: 
_ALS.PROJECT_ (PROJECT_TYPE => SINGLE_PROJECT_FOUND,
_ALS.PROJECT_  STATUS => VALID_PROJECT_WITH_WARNING,
_ALS.PROJECT_  PROJECT_FILE => {GNATCOLL.VFS.VIRTUAL_FILE object},
_ALS.PROJECT_  MISSING_ADA_RUNTIME => TRUE,
_ALS.PROJECT_  GPR2_MESSAGES => {GPR2.LOG.OBJECT object})
[ALS.MAIN] in GNATformat Format
[ALS.MAIN] raised CONSTRAINT_ERROR : erroneous memory access
_ALS.MAIN_ 0x000000010636E448 0x000000010636E480 0x0000000106410D8C 0x0000000106411CD4 0x000000010564BB05 0x000000010565949C 0x00000001055AE740 0x000000010555F49C 0x00000001054F414C 0x000000010552615C 0x00000001055256F4 0x0000000105525964 0x0000000105526524 0x00000001055264D4 0x0000000105525B4C 0x000000010552616C 0x000000010552619C 0x00000001055256F4 0x0000000105527CCC 0x00000001054B0104 0x0000000104054764 0x0000000104054A40 0x0000000103F431A8 0x0000000103F2971C 0x0000000103FDE60C 0x0000000103FD265C 0x0000000102FB3C0C 0x0000000102FC071C 0x0000000103F9F7BC 0x0000000106358060 0x0000000199BF5F90
[ALS.MAIN] 0x000000010636E448 0x000000010636E480 0x0000000106410D8C 0x0000000106411CD4 0x000000010564BB05 0x000000010565949C 0x00000001055AE740 0x000000010555F49C 0x00000001054F414C 0x000000010552615C 0x00000001055256F4 0x0000000105525964 0x0000000105526524 0x00000001055264D4 0x0000000105525B4C 0x000000010552616C 0x000000010552619C 0x00000001055256F4 0x0000000105527CCC 0x00000001054B0104 0x0000000104054764 0x0000000104054A40 0x0000000103F431A8 0x0000000103F2971C 0x0000000103FDE60C 0x0000000103FD265C 0x0000000102FB3C0C 0x0000000102FC071C 0x0000000103F9F7BC 0x0000000106358060 0x0000000199BF5F90

I see a lot of _ALS.PROJECT_ No alr in the PATH logs here: it seems that you are trying to load a project based on Alire, without having alr (the Alire executable) in your PATH. Could you try again after making sure alr is in your PATH?

Note that in parallel we will try to make GNATformat working on Ada files that do not belong to the loaded project, which would solve these issues in the long-term.

Regards,

@AnthonyLeonardoGracio
Copy link
Collaborator

@rongcuid Note that I can reproduce on my side on Mac OS aarch64. We'll investigate.

@rongcuid
Copy link

I see a lot of _ALS.PROJECT_ No alr in the PATH logs here: it seems that you are trying to load a project based on Alire, without having alr (the Alire executable) in your PATH. Could you try again after making sure alr is in your PATH?

I don't know why this is the case. alr is in my path in my terminal, but I think vscode doesn't pick it up.

@AnthonyLeonardoGracio
Copy link
Collaborator

AnthonyLeonardoGracio commented Nov 20, 2024

I see a lot of _ALS.PROJECT_ No alr in the PATH logs here: it seems that you are trying to load a project based on Alire, without having alr (the Alire executable) in your PATH. Could you try again after making sure alr is in your PATH?

I don't know why this is the case. alr is in my path in my terminal, but I think vscode doesn't pick it up.

Do you launch VS Code from your terminal, or outside of it? If you launch it outside of your terminal, you might want to have a look at this

@rongcuid
Copy link

I see a lot of _ALS.PROJECT_ No alr in the PATH logs here: it seems that you are trying to load a project based on Alire, without having alr (the Alire executable) in your PATH. Could you try again after making sure alr is in your PATH?

I don't know why this is the case. alr is in my path in my terminal, but I think vscode doesn't pick it up.

Do you launch VS Code from your terminal, or outside of it? If you launch it outside of your terminal, you might want to have a look at this

Ok. I didn't know vscode can do that now. It does remove the error about alr

@AnthonyLeonardoGracio
Copy link
Collaborator

I see a lot of _ALS.PROJECT_ No alr in the PATH logs here: it seems that you are trying to load a project based on Alire, without having alr (the Alire executable) in your PATH. Could you try again after making sure alr is in your PATH?

I don't know why this is the case. alr is in my path in my terminal, but I think vscode doesn't pick it up.

Do you launch VS Code from your terminal, or outside of it? If you launch it outside of your terminal, you might want to have a look at this

Ok. I didn't know vscode can do that now. It does remove the error about alr

Great! We'll investigate the erroneous memory access on our side, keeping you posted.

@AnthonyLeonardoGracio
Copy link
Collaborator

@simonjwright I confirm that it's a aarch64 Mac OS issue: I can't reproduce with the extension built for x86-64 Mac OS.

I think you have worked on the aarch64 port for Mac OS right? Unfortunately at AdaCore we don't officially support this platform, so it will be hard for us to fix that issue, which looks like a pure compiler issue (the same code runs fine on Mac OS x86-64, Linux platforms and Windows).

Would you be able to investigate on your side? That would greatly help us and the Ada/Mac OS community. If not we'll see internally how we can proceed.

Regards,

@simonjwright
Copy link
Contributor Author

@AnthonyLeonardoGracio, I will certainly have a go. I’ll need to rebuild the compiler first (so I can decode those tracebacks, see GCC PR 117358). Would the GCC 14.2 compiler be an appropriate base?

@simonjwright
Copy link
Contributor Author

I've built ALS (25.0.0) with my fixed compiler, and it's working, but how do I get VSCode to use it?

GNATFormat 25.0.0 works fine with my test code, see above.

VSCode is OK so long as I configure not to use gnatformat (i.e., use gnatpp).

I've managed to start ALS with --config /Users/simon/tmp/gf.json, which contains

{
    "ada.useGnatformat": true
}

but it seems to have no effect: when I formatted the file, ada.text_io was updated to Ada.Text_IO, and I don't think that's something that GNATformat does?

I tried replacing 'true' with 'maybe', and ALS refused to start up, so it's getting read. I tried "ada.useGnatformatX": true, with no observable effect.

@simonjwright
Copy link
Contributor Author

how do I get VSCode to use it?

Found the location:

~/.vscode/extensions/adacore.ada-26.0.202411173-darwin-arm64/arm64/darwin

I’ll put my rebuilt version in there.

@AnthonyLeonardoGracio
Copy link
Collaborator

how do I get VSCode to use it?

Found the location:

~/.vscode/extensions/adacore.ada-26.0.202411173-darwin-arm64/arm64/darwin

I’ll put my rebuilt version in there.

Great @simonjwright ! Is the fix already available via your personal Alire index? Because a temporary solution for us would be to use your index to build the Ada Language Server for Mac OS aarch64, until the fix gets propagated in Alire's community index: would that be ok for you?

@simonjwright
Copy link
Contributor Author

@AnthonyLeonardoGracio , the point of that compiler is so that I can find out where the exceptions are being raised (on macOS, the standard GNAT.Traceback.Symbolic.Symbolic_Traceback doesn’t include the program load address, and since on macOS all executables are position-independent, the traceback can’t be decoded. See GCC PR 117538).

So it wouldn’t help anyone.

However! I think I’ve found the issue; it’s in Libadalang.

Using the patched compiler, I found that the exception is in Libadalang.Analysis.Check_Safety_Net line 36610 (!),

      --  Then check that the R rebindings reference, if not-null, is not stale
      elsif R /= null and then R.Version /= SN.Rebindings_Version then
         raise Stale_Reference_Error with "related unit was reparsed";
      end if;

because R.Version was 1 and SN.Rebindings_Version was 0 (0 is the default initial value).

In Libadalang.Implementation.Release_Rebinding, we have

      --  Bumping the version number, to invalidate existing references to
      --  Self.
      Self.Version := Self.Version + 1;
      Self.Children.Destroy;
      Available.Append (Self);
      Self := null;

(returning the object to the Available pool).

In the procedure just above it, Acquire_Rebinding, we have

      --  Use an existing and available Env_Rebindings_Type record for Node's
      --  Context, otherwise allocate a new rebinding.
      Result := (if Available.Is_Empty
                 then new Env_Rebindings_Type'(Version => 0, others => <>)
                 else Available.Pop);
      Result.Parent := Parent;
      Result.Old_Env := Old_Env;
      Result.New_Env := New_Env;
      Result.Children := Env_Rebindings_Vectors.Empty_Vector;
      return Result;

so if we pop a rebinding off the Available pool, we leave the Version as set by Release_Rebinding, i.e. 1.

So, I patched in Result.Version := 0; and rebuilt; now VSCode will reformat my sample code without complaint!

This change I could put in my personal Alire index.

So, this would explain why the error happens with the gnatformat option in ALS, because it’s built over Libadalang - well, so is gnatpp, but the usage pattern is probably different?

Why does the problem show up on the aarch64 build and not on the x86_64 build? I have no idea. If I’m right and this is a bug, it’d mean that the x86_64 build never returned a rebinding to pool, which seems strange.

@simonjwright
Copy link
Contributor Author

I thought to check whether there was a problem with other pointers to the now-stale rebinding; I arranged for Release_Rebinding to Free the access, and for Acquire_Rebinding to always new (which of course it would have done anyway, since Available would be empty (????)). The hope was that access via a stale pointer would show itself.

I can see this might affect performance (how much?), but it seems to work OK for both VSCode and ada-ts-mode; no errors reported so far.

@pmderodat
Copy link
Member

Hello @simonjwright,

If I understand correctly what you meant, this is not going in the right direction: the goal of the safety net mechanism is to detect (and error out) when a Libadalang user calls a Libadalang subprogram passing an entity to it when that entity no longer exists (root cause: because it somehow refers to a node that does not exist due to reparsing).

To achieve that at a reasonable cost, rebindings records are allocated on demand, and then never deallocated. The goal of never deallocating them is to completely avoid dangling pointers (or worse: user after free+alloc). Yet to avoid leaking memory, we reuse allocated rebindings when they were disposed (i.e. when they were marked as stale), so that we do not allocate a new record if there still one that is available.

The second stage for this mechanism is rejecting uses of reference to a rebinding when they are stale. To achieve this, rebindings records have a version number set to 0 initially, and then that version number is bumbed each time the rebinding is marked stale: any attempt to use this disposed (or repurposed) rebinding while the old purpose was meant can be turned into an exception.

Unfortunately, both of your suggestions defeat these goals:

  • If we set the version number to 0 on re-using existing records in Acquire_Rebinding, we’ll fail to detect use of stale references since they will all be 0 for “active” rebindings: the version number will help detecting only uses of released rebindings (not repurposed ones), as the version number will be always 0 for active rebindings, and 1 for disposed rebindings.
  • If we free stale rebindings, we’ll start getting Storage_Error exceptions (if we are lucky) when someone tries to use a released rebinding (or worse if that memory was reallocated for something else: no crash but clearly erroneous behavior).

The fact that you are getting this error on one arch but not the other is indeed concerning, but we need to get to the bottom of this (really understand what is going on) to fix the actual bug. It would be much easier if we could reproduce this ourselves.

@simonjwright
Copy link
Contributor Author

If I replace the aarch64 ALS installed under ~/.vscode with the x86_64 ALS downloaded from here, I no longer have a problem. Nor do I have a problem using an Alire 25.0.0 ALS built for x86_64.

So clearly there’s an arch-related problem (sorry to have doubted this :-)

Immediate lines of investigation:

  • could the aarch64 compiler (which is built from a patched source tree) have an error?
  • would an x86_64 compiler built from the patched source tree show the issue?
  • would a GCC 15 aarch64 compiler behave differently?

Since we don’t have an Ada-aware debugger for aarch64 Darwin, progress is likely to be slow, but at least users have a way ahead as I noted at the top.


As a matter of interest, if a stale rebinding is reused, at what point does SN.Rebindings_Version get updated so Check_Safety_Net doesn’t find the error?

@pmderodat
Copy link
Member

As a matter of interest, if a stale rebinding is reused, at what point does SN.Rebindings_Version get updated so Check_Safety_Net doesn’t find the error?

I’m not sure I understand precisely your question, let me know if you wanted to know something else:

When a Libadalang function returns a node (which includes a reference to rebindings), we create a safety net for it that keeps track of the rebindings version: that’s the safety net Rebindings_Version component, set to a copy of Env_Rebindings_Type.Version, i.e. the current version for the rebinding object at that point.

Once this is done, the safety net in the returned node is read-only: when rebindings are invalidated, it is Env_Rebindings_Type.Version that is bumped so that Rebindings_Version is no longer equal to it. At that point the node that was returned no longer exists, it must not be used anymore at all (the point of Check_Safety_Net is to reject such illegal uses): one needs to start over to get the new node reference (if that makes sense after the update that caused the rebindings invalidation).

@simonjwright
Copy link
Contributor Author

OK, things are much clearer now.

Using the unpatched source (i.e. no messing with the standard safety net stuff), we have on macOS

  • gcc 14.2, x86_64: no problems
  • gcc 14.2, aarch64: problems
  • gcc 15.0 x86_64: compilation failures - not progressed at this time
  • gcc 15.0 aarch64: likewise; but, with minor adjustments (see below) we get a successful build, with no problems!!!

Since lal_tools built OK after removing -gnatX from lal_tools_common.gpr, I’d like to ask your (OK, all) developers to restrict the use of that switch to development code where necessary, and to remove it if at all possible in releasable code.

In at least one case I saw a claim that this switch would allow using the () aggregate syntax without generating a warning; that’s not so.

I’d like to add that the only way to get the successful build was to compile with alr build -- -gnatwn because of uses of the () aggregate syntax and GPRs with -gnatwae. In GCC PR 104751 we’re told that

The introduction of [] in Ada was a controversial one and this warning is meant to help disambiguate and simplify the Ada 2022 language, so this warning is intentional.

so making people using released code override the warning seems counter-effective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants