root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.66k stars 1.26k forks source link

Crash when macro is named main.cpp #11707

Open ferdymercury opened 1 year ago

ferdymercury commented 1 year ago

Describe the bug

=========================================================== There was a crash (kSigSegmentationViolation). This is the entire stack trace of all threads:

0 0x00007f349a49845a in __GI___wait4 (pid=57745, stat_loc=stat_loc

entry=0x7ffe007656d8, options=options entry=0, usage=usage entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30

1 0x00007f349a49841b in __GI___waitpid (pid=, stat_loc=stat_loc

entry=0x7ffe007656d8, options=options entry=0) at ./posix/waitpid.c:38

2 0x00007f349a3febcb in do_system (line=) at ../sysdeps/posix/system.c:171

3 0x00007f349ac5bc22 in TUnixSystem::Exec (this=0x5620a717b800, shellcmd=0x5620a837aa50 "/opt/root_bld/etc/gdb-backtrace.sh 57715 1>&2") at /opt/root_src/core/unix/src/TUnixSystem.cxx:2104

4 0x00007f349ac5c4d3 in TUnixSystem::StackTrace (this=0x5620a717b800) at /opt/root_src/core/unix/src/TUnixSystem.cxx:2395

5 0x00007f349ac60174 in TUnixSystem::DispatchSignals (this=0x5620a717b800, sig=kSigSegmentationViolation) at /opt/root_src/core/unix/src/TUnixSystem.cxx:3615

6 0x00007f349ac5790e in SigHandler (sig=kSigSegmentationViolation) at /opt/root_src/core/unix/src/TUnixSystem.cxx:395

7 0x00007f349ac60081 in sighandler (sig=11) at /opt/root_src/core/unix/src/TUnixSystem.cxx:3586

8 0x00007f349ac509d0 in textinput::TerminalConfigUnix::HandleSignal (this=0x7f349b144ae0 <textinput::TerminalConfigUnix::Get()::s>, signum=11) at /opt/root_src/core/textinput/src/textinput/TerminalConfigUnix.cpp:99

9 0x00007f349ac506ec in (anonymous namespace)::TerminalConfigUnix__handleSignal (signum=11) at /opt/root_src/core/textinput/src/textinput/TerminalConfigUnix.cpp:36

10

11 __strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:115

12 0x00005620a5400eba in handle_notebook_option (argc=7777040, argv=0x5620a7f7f780) at /opt/root_src/main/src/rmain.cxx:39

13 0x00005620a540109c in main (argc=7777040, argv=0x5620a7f7f780) at /opt/root_src/main/src/rmain.cxx:78

===========================================================

The lines below might hint at the cause of the crash. You may get help by asking at the ROOT forum https://root.cern/forum Only if you are really convinced it is a bug in ROOT then please submit a report at https://root.cern/bugs Please post the ENTIRE stack trace from above as an attachment in addition to anything else that might help us fixing this issue.

11 __strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:115

12 0x00005620a5400eba in handle_notebook_option (argc=7777040, argv=0x5620a7f7f780) at /opt/root_src/main/src/rmain.cxx:39

13 0x00005620a540109c in main (argc=7777040, argv=0x5620a7f7f780) at /opt/root_src/main/src/rmain.cxx:78

===========================================================

Root > .q


- If compiled, there is no crash, but you get a cryptic error:

root -l main.cpp+ root [0] Processing main.cpp+... Info in : creating shared library /tmp//tmp/./main_cpp.so Error in : only one instance of TApplication allowed

- If one uses `void main` instead of `int main`, you get this error:

/tmp/main.cpp:2:1: error: 'main' must return 'int' void main() ^~~~ int

- If one uses an unnamed macro, the problem is not there.

### Expected behavior
An error should be printed if the user tries to execute such kind of named macros, rather than the current crashes or error messages.

### To Reproduce
- Write a file called `main.cpp` with the content `int main() { return 0;}`
- root -l main.cpp

### Setup

| Welcome to ROOT 6.27/01 https://root.cern | | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers | | Built for linuxx8664gcc on Aug 30 2022, 13:52:01 | | From heads/master@v6-25-01-4921-gdfd0f52391 | | With c++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0 | | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |



### Additional context
https://stackoverflow.com/questions/74247557/warning-failed-to-call-main-to-execute-the-macro/74445395#74445395
couet commented 1 year ago

I do not get this crash:

% root -l main.cpp  
root [0] 
Processing main.cpp...
Error in <TApplication::TApplication>: only one instance of TApplication allowed
   ------------------------------------------------------------------
  | Welcome to ROOT 6.27/01                        https://root.cern |
  | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Nov 15 2022, 10:28:34                      |
  | From heads/master@v6-25-02-2747-g7a90392f2a                      |
  | With Apple clang version 14.0.0 (clang-1400.0.29.202)            |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] 

For me, on Mac, it is protected. Seems to me the error is not cryptic at all. It makes it clear that main() already exists.

ferdymercury commented 1 year ago

Seems to me the error is not cryptic at all. It makes it clear that main() already exists.

Newbie users do not know what TApplication is, and how that should be related with main(). See https://stackoverflow.com/questions/74247557/warning-failed-to-call-main-to-execute-the-macro/74445395#74445395

couet commented 1 year ago

Maybe it can be made more clear but the message comes from here. I guess this error can occur for many more reasons than a macro called main.cpp. So it will not be easy to make it clearer there. Maybe @Axel-Naumann or @pcanal can comment.

ferdymercury commented 1 year ago

For me, on Mac, it is protected.

It seems to me that it is undefined behavior. Sometimes, I get a normal error message, sometimes I get the crash. See here below consecutive runs:


/tmp$ root -l main.cpp 
root [0] 
Processing main.cpp...
Error in <TApplication::TApplication>: only one instance of TApplication allowed
   ------------------------------------------------------------------
  | Welcome to ROOT 6.27/01                        https://root.cern |
  | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Aug 30 2022, 13:52:01                 |
  | From heads/master@v6-25-01-4921-gdfd0f52391                      |
  | With c++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0                     |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] .q
/tmp$ root -l main.cpp 
root [0] 
Processing main.cpp...
Error in <TApplication::TApplication>: only one instance of TApplication allowed
   ------------------------------------------------------------------
  | Welcome to ROOT 6.27/01                        https://root.cern |
  | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Aug 30 2022, 13:52:01                 |
  | From heads/master@v6-25-01-4921-gdfd0f52391                      |
  | With c++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0                     |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] .q
/tmp$ root -l main.cpp 
root [0] 
Processing main.cpp...

 *** Break *** segmentation violation

===========================================================
There was a crash (kSigSegmentationViolation).
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f7b9f49845a in __GI___wait4 (pid=95642, stat_loc=stat_loc
entry=0x7ffd4f13a298, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1  0x00007f7b9f49841b in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffd4f13a298, options=options
entry=0) at ./posix/waitpid.c:38
#2  0x00007f7b9f3febcb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:171
#3  0x00007f7b9fc5bc22 in TUnixSystem::Exec (this=0x55d680e3b800, shellcmd=0x55d681cbf580 "/opt/root_bld/etc/gdb-backtrace.sh 95620 1>&2") at /opt/root_src/core/unix/src/TUnixSystem.cxx:2104
#4  0x00007f7b9fc5c4d3 in TUnixSystem::StackTrace (this=0x55d680e3b800) at /opt/root_src/core/unix/src/TUnixSystem.cxx:2395
#5  0x00007f7b9fc60174 in TUnixSystem::DispatchSignals (this=0x55d680e3b800, sig=kSigSegmentationViolation) at /opt/root_src/core/unix/src/TUnixSystem.cxx:3615
#6  0x00007f7b9fc5790e in SigHandler (sig=kSigSegmentationViolation) at /opt/root_src/core/unix/src/TUnixSystem.cxx:395
#7  0x00007f7b9fc60081 in sighandler (sig=11) at /opt/root_src/core/unix/src/TUnixSystem.cxx:3586
#8  0x00007f7b9fc509d0 in textinput::TerminalConfigUnix::HandleSignal (this=0x7f7ba0144ae0 <textinput::TerminalConfigUnix::Get()::s>, signum=11) at /opt/root_src/core/textinput/src/textinput/TerminalConfigUnix.cpp:99
#9  0x00007f7b9fc506ec in (anonymous namespace)::TerminalConfigUnix__handleSignal (signum=11) at /opt/root_src/core/textinput/src/textinput/TerminalConfigUnix.cpp:36
#10 <signal handler called>
#11 __strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:115
#12 0x000055d67fa00eba in handle_notebook_option (argc=1326708416, argv=0x55d681c181b0) at /opt/root_src/main/src/rmain.cxx:39
#13 0x000055d67fa0109c in main (argc=1326708416, argv=0x55d681c181b0) at /opt/root_src/main/src/rmain.cxx:78
===========================================================

The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#11 __strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:115
#12 0x000055d67fa00eba in handle_notebook_option (argc=1326708416, argv=0x55d681c181b0) at /opt/root_src/main/src/rmain.cxx:39
#13 0x000055d67fa0109c in main (argc=1326708416, argv=0x55d681c181b0) at /opt/root_src/main/src/rmain.cxx:78
===========================================================

Root > 
ferdymercury commented 1 year ago

I found the issue with the crash. It seems that if I print argc just before the crash line in rmain.cxx:39, it has a value of 1563131968, sometimes of 1404392704, so random:

fprintf(stderr, "%d\n", argc);
   for (i = 1; i < argc; i++) {
      if (!strcmp(argv[i], "--notebook")) { notebook = i; break; }
   }

maybe because of this place in TRint:151: --*argc;

ferdymercury commented 1 year ago

If in rmain.cxx, i use:

int main(int argc, char **argv)
{
   fprintf(stderr, "%d %s\n", argc, "a");
   handle_notebook_option(argc, argv);
   TRint *theApp = new TRint("Rint", &argc, argv, /*options*/ nullptr, /*numOptions*/ 0, /*noLogo*/ kFALSE,
                             /*exitOnUnknownArgs*/ kTRUE);
   fprintf(stderr, "%d %s\n", argc, "b");
   // and enter the event loop...
   theApp->Run();
   delete theApp;

   return 0;
}

Then I get:

4 a
1 b

Processing /tmp/main.cpp...
283144112 a

 *** Break *** segmentation violation

So the second time this function is called, argc is random memory I guess.

ferdymercury commented 1 year ago

Obviously, if in rmain.cxx, I add the following line, then the crash disappears:

int main(int argc, char **argv)
{
   if(argc>4 || argc<0) return 0;
   handle_notebook_option(argc, argv);