kpeeters / cadabra2

A field-theory motivated approach to computer algebra.
https://cadabra.science/
GNU General Public License v3.0
228 stars 37 forks source link

Cadabra2 evaluate() crashed on segmentation fault #134

Closed mzavadil closed 5 years ago

mzavadil commented 5 years ago

Cadabra2 command evaluate crashed on segmentation fault if a complete command was used for calculation:

evaluate_fail

Example:

# -*- coding: utf-8 -*-
from cadabra2 import *
print("start calculation...")
print(Coordinate(Ex(r"{r,t,f,v}"), Ex(r"")))
print(Indices(Ex(r"{a,b,c,d}"), Ex(r"values={t,r,f,v}, position=fixed")))
print(PartialDerivative(Ex(r"\partial{#}"), Ex(r"")))
print(Metric(Ex(r"g_{a b}"), Ex(r"")))
print(InverseMetric(Ex(r"g^{a b}"), Ex(r"")))
schwarzschild = Ex(r"{ g_{t t}=-(1-2*M/r),  g_{r r}=1/(1-2*M/r), g_{v v}=r**2, g_{f f}=r**2*sin(v)**2 }")
print(schwarzschild)
schwarzschild = complete(schwarzschild, Ex(r"g^{a b}"))
christoffel = Ex(r"\Gamma^{c}_{a b} = 1/2 g^{c d} ( \partial_{a}{g_{b d}} + \partial_{b}{g_{a d}} - \partial_{d}{g_{a b}} )")
print(christoffel)
connection = evaluate(christoffel, schwarzschild, rhsonly=True)
print(connection)
print("done.")

The problem is in dereference a null pointer of tree node of rhs part the expression in core/algorithms/substitute.cc.

Probably this is the cause of this error:

Traceback (most recent call last):
  File "cadabra2", line 248, in <module>
    exec(cmp)
  File "..\tests\crash3.py", line 12, in <module>
    print(schwarzschild)
RuntimeError: Found equals node with only one child node.

Occurred for printing a result of complete command:

...
print(complete(schwarzschild, Ex(r"g^{a b}")))
...

Tested on last windows build from commit: https://github.com/kpeeters/cadabra2/commit/5533cd5bcce492fbc5b9fafc551b08ee5aa19136

My version of Anaconda:

 active environment : base
    active env location : C:\tools\Anaconda3
            shell level : 1
       user config file : C:\Users\bigfo\.condarc
 populated config files : C:\Users\bigfo\.condarc
          conda version : 4.6.4
    conda-build version : 3.17.8
         python version : 3.7.2.final.0
       base environment : C:\Tools\Anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Tools\Anaconda3\pkgs
                          C:\Users\bigfo\.conda\pkgs
                          C:\Users\bigfo\AppData\Local\conda\conda\pkgs
       envs directories : C:\Tools\Anaconda3\envs
                          C:\Users\bigfo\.conda\envs
                          C:\Users\bigfo\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.6.4 requests/2.21.0 CPython/3.7.2 Windows/10 Windows/10.0.17763
          administrator : False
             netrc file : None
           offline mode : False

and the sympy package:

sympy 1.3 py37_0
----------------
file name   : sympy-1.3-py37_0.tar.bz2
name        : sympy
version     : 1.3
build       : py37_0
build number: 0
size        : 9.5 MB
license     : 3-clause BSD
subdir      : win-64
url         : https://repo.anaconda.com/pkgs/main/win-64/sympy-1.3-py37_0.tar.bz2
md5         : 0f82e0de3a83b1e9e311621a603e9d2e
timestamp   : 2018-09-28 17:52:28 UTC
dependencies:
  - fastcache
  - mpmath >=0.19
  - python >=3.7,<3.8.0a0
kpeeters commented 5 years ago

Commit a9331e1fd9c0ead599d523f4c4322a67f2dbd065 messed up something which causes this (some self tests fail too). Haven't tracked it down yet.

mzavadil commented 5 years ago

The problem is may be in this change of core/SympyCdb.cc:

image

Return value of append_child was not used, but the call of it may be something do - just guess, I have not studied it in detail.

kpeeters commented 5 years ago

Yes, that one, and a similar one in core/algorithms/explicit_indices.cc. I went a bit too wild ripping out lines at every compiler warning. Now fixed in master.

kpeeters commented 5 years ago

By the way, in your code example, both complete and evaluate work directly on their argument and change it in place. There is no need to assign the result.

mzavadil commented 5 years ago

Thanks for a hint and quick fix.

If you want to eliminate every warnings, then here are some of the MS Build:

Build succeeded.

"C:\Users\bigfo\Workspace\dev\cadabra2\build\install.vcxproj" (default target) (1) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\ALL_BUILD.vcxproj" (default target) (3) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\tests\beginners_test.vcxproj" (default target) (4) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\core\cadabra2cadabra.vcxproj" (default target) (5) ->
(ClCompile target) ->
  c:\users\bigfo\workspace\dev\cadabra2\libs\whereami\whereami.c(49): warning C4005: 'WIN32_LEAN_AND_MEAN': macro redefinition [C:\Users\bigfo\Workspace\dev\cadabra2\build
\core\cadabra2cadabra.vcxproj]

"C:\Users\bigfo\Workspace\dev\cadabra2\build\install.vcxproj" (default target) (1) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\ALL_BUILD.vcxproj" (default target) (3) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\client_server\cadabra-server.vcxproj" (default target) (7) ->
  c:\users\bigfo\workspace\dev\cadabra2\libs\whereami\whereami.c(49): warning C4005: 'WIN32_LEAN_AND_MEAN': macro redefinition [C:\Users\bigfo\Workspace\dev\cadabra2\build
\client_server\cadabra-server.vcxproj]

"C:\Users\bigfo\Workspace\dev\cadabra2\build\install.vcxproj" (default target) (1) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\ALL_BUILD.vcxproj" (default target) (3) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\core\cadabra2.vcxproj" (default target) (8) ->
  c:\users\bigfo\workspace\dev\vcpkg\installed\x64-windows\include\pcrecpp.h(701): warning C4251: 'pcrecpp::RE::pattern_': class 'std::basic_string<char,std::char_traits<c
har>,std::allocator<char>>' needs to have dll-interface to be used by clients of class 'pcrecpp::RE' (compiling source file C:\Users\bigfo\Workspace\dev\cadabra2\core\Stor
age.cc) [C:\Users\bigfo\Workspace\dev\cadabra2\build\core\cadabra2.vcxproj]

"C:\Users\bigfo\Workspace\dev\cadabra2\build\install.vcxproj" (default target) (1) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\ALL_BUILD.vcxproj" (default target) (3) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\core\cadabra2.vcxproj" (default target) (8) ->
(Link target) ->
  LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification [C:\Users\bigfo\Workspace\dev\cadabra2\build\core\cadabra2.vcxproj]

"C:\Users\bigfo\Workspace\dev\cadabra2\build\install.vcxproj" (default target) (1) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\ALL_BUILD.vcxproj" (default target) (3) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\frontend\gtkmm\cadabra2-gtk.vcxproj" (default target) (9) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\client_server\cadabra_client.vcxproj" (default target) (10) ->
(ClCompile target) ->
  c:\users\bigfo\workspace\dev\cadabra2\libs\whereami\whereami.c(49): warning C4005: 'WIN32_LEAN_AND_MEAN': macro redefinition [C:\Users\bigfo\Workspace\dev\cadabra2\build
\client_server\cadabra_client.vcxproj]

"C:\Users\bigfo\Workspace\dev\cadabra2\build\install.vcxproj" (default target) (1) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\ALL_BUILD.vcxproj" (default target) (3) ->
"C:\Users\bigfo\Workspace\dev\cadabra2\build\frontend\gtkmm\cadabra2-gtk.vcxproj" (default target) (9) ->
  c:\users\bigfo\workspace\dev\cadabra2\frontend\gtkmm\notebookwindow.cc(1795): warning C4805: '==': unsafe mix of type 'bool' and type 'int' in operation [C:\Users\bigfo\
Workspace\dev\cadabra2\build\frontend\gtkmm\cadabra2-gtk.vcxproj]
  c:\users\bigfo\workspace\dev\cadabra2\frontend\common\lodepng.cc(3411): warning C4334: '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift int
ended?) [C:\Users\bigfo\Workspace\dev\cadabra2\build\frontend\gtkmm\cadabra2-gtk.vcxproj]

    7 Warning(s)
    0 Error(s)

Time Elapsed 00:03:44.17
kpeeters commented 5 years ago

Sorry, that's a bit too much for my restricted spare time. If you know how to fix it then a patch will be gratefully accepted, but otherwise I think this will just have to stay as it is.