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

Segfault when running native random.bf #4

Open
Rudxain opened this issue Nov 24, 2022 · 4 comments
Open

Segfault when running native random.bf #4

Rudxain opened this issue Nov 24, 2022 · 4 comments

Comments

@Rudxain
Copy link

Rudxain commented Nov 24, 2022

Repro:

  1. cd into repo root
  2. go run . examples/random.bf
  3. ./random

This is the output on my sys:

��PO[y�KKXi)�������(V����d���'����a=([�WRp
    g�� +��+}~�(6��P�0=�3�xr1̷����n�G.���"�T@Z|��t�sC
                                                    u�ϪG`��+�k`w��U��H|O��u�ۚ� O}���3w5-Y�it�����t\,��B���p���.2Q��x���A�w��SH~�PW�v�3��&�5��d�o���HA���r�Y�&�|87�����N��|���`
                                               q�1�8ٽ��yT2��
�W�D���;q�r{>輒���R�p[�a�I�����X�gh�g_�6(�3�;�  �=�VJ��V�&��+�����.�
����sFs�L�      Ry2ՁR����Ea�H�y�.��mE4��V��Z�ǟ�ތ���J�'1|�-*ͻ�a�L���}�^��78�I��8F��m���`.
                                                                                        ��f��pR�xw_�֙^]�5�#S+x�hG�.X?j�.T�껤r�@����?f�,fߝ�Q���>�D�\��=���|1��#�B"��N=f�{�~�uL�z �]65���=��%���e�ҩ""��aӅ�
                                                                         cH2���=T���:֏$���5���ؤ$}�z��wW5�� P�
                                                                                                             �f�b��P��"���a5?�
                                                                                                                             ?�<_�Q���B6�ѐˍ�g1�l�ȶ��m0p
Segmentation fault (core dumped)

I tried piping it to xxd but got no output. Tried redirecting to a file, still no output (but this time, it only prints "Segmentation fault (core dumped)")

OS+DE: Linux Mint 21 Cinnamon
Device: Dell Inspiron 15R
Terminal: VScode built-in term (haven't tested in GNOME term)

@baris-inandi
Copy link
Owner

Thanks for opening an issue, will investigate

@Rudxain
Copy link
Author

Rudxain commented Nov 25, 2022

If it helps, I ran the BF file here (with "Memory overflow behaviour" set to "abort")

The interpreter shows this

Warning: Memory border overflow in line 5 char 7.

I also tried

go run . --d-dump-ir examples/random.bf

to see the generated C code, but couldn't find anything wrong

@baris-inandi
Copy link
Owner

Totally forgot this was here. I tried testing this using the interpreter, which doesn't do any preprocessing at all.

Here's the output of brainfuck -interpret examples/random.bf

❯ brainfuck -interpret examples/random.bf
òè�POyÁKKXi)ëبéÑñÐ(VÃÎùâ�dÉ�é'ú¡ØÐa=([¯WRp
     g¸ù	+ª¼+}~È(6��Pÿ0=Ý3×xr1Ì·�öÊÞnçG.�ØÙ"àT@Z|ªÅtÑsC
ºPWÉv�3ÐÏ&�5ÝÎd©o Äì¤HA�¸èr�Yë&¿|87ºÑßÇÃNÔÛ|�©Þ`              u¿ÏªG`Úñ+Ík`w®·U�ûH|Oô§uÄÛ�¿ O}��¢3w5-Y�it�ÝÏâ´Öt\,ßàBªûå´p§íì.2QÂÕxÀ�µA�éw�ëSH~]a��9�fýÈÊüÄÜ1õô�c�I�ú
                                                q±1ê8Ù½æ×yT2ðï
ÊW¯D»Óô;q�r{>è¼�óÚ�R©p2î[ùaæIª¡Òü±Xóghâ�g_�6(¶3Ù;Î	õ=äVJÈ×V�&ÆÔ+�»��Ù.�
«¦�ÆsFs²L	Ry2Õ�R��½��EaøHáyª.ºÿmE4ò¸ìVÉéZ¢Ç�¬Þ�þ�óJ¨'1|�-*Í»�aìL�°�}Ð^¯�É78¥IÆÖ8F��máÁÃ`.
                                                                                               ²ÍfÜÃpRÕxw_½Ö�^]ð5Ê#S+x´hG�.X?j�.TÅ껤r·@����?f¿,fß��Q©�Å>�D�\ô=�¹�|1ª�#ÑB"ÖõN=f�{Ý~ãuLåz �]65ËÀþ=ûÖ%ÃÎëe§Ò©""¨ªaÓ�¶
                                                                                                                                                                                                                   cH2¥®Ò=TíÀ©:Ö�$µÔç5ÈøËؤ$}¼z�øwW5Ñý P½
                                     �fÓb©íP�±"×çÿa5?ì
                                                      ?ï<_«Q÷�õB6øÑ�Ë�Ñg1ðl¶È¶¿¼m0p
A» ]añ"$�q¡ØÌÎø5¾Å®�nS'öPºG�<�ÏVBþL+år,�÷^\Y38,�l³±|\¬SVj$öpanic: runtime error: index out of range [30001] with length 30000

goroutine 1 [running]:
github.com/baris-inandi/brainfuck/lang/exec/interpreter.(*BfContext).EvalExprContextually(0x1400012c3c6, {0x14000066120, 0x54})
	/Users/bi/me/code/brainfuck-go/lang/exec/interpreter/evalExpr.go:68 +0x568
github.com/baris-inandi/brainfuck/lang/exec/interpreter.(*BfContext).EvalExprContextually(0x1400012c3c6, {0x14000166070, 0x67})
	/Users/bi/me/code/brainfuck-go/lang/exec/interpreter/evalExpr.go:69 +0x524
github.com/baris-inandi/brainfuck/lang/exec/interpreter.(*BfContext).EvalExprContextually(0x1400012c3c6, {0x1400017c0d0, 0x74})
	/Users/bi/me/code/brainfuck-go/lang/exec/interpreter/evalExpr.go:69 +0x524
github.com/baris-inandi/brainfuck/lang/exec/interpreter.EvalExpr(...)
	/Users/bi/me/code/brainfuck-go/lang/exec/interpreter/evalExpr.go:37
github.com/baris-inandi/brainfuck/lang/exec/interpreter.Interpret(...)
	/Users/bi/me/code/brainfuck-go/lang/exec/interpreter/interpret.go:13
github.com/baris-inandi/brainfuck/lang/exec.Interpret(0x14000133b78?, {0x16d10b865?, 0x9?})
	/Users/bi/me/code/brainfuck-go/lang/exec/exec.go:33 +0x15c
github.com/baris-inandi/brainfuck/cli.CmdHandler(0x14000022c00)
	/Users/bi/me/code/brainfuck-go/cli/cmdhandler.go:33 +0x2f0
github.com/urfave/cli/v2.(*App).RunContext(0x10305b7a0, {0x102f18dd8?, 0x140000160c8}, {0x140000101b0, 0x3, 0x3})
	/Users/bi/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:322 +0x6dc
github.com/urfave/cli/v2.(*App).Run(...)
	/Users/bi/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:224
main.main()
	/Users/bi/me/code/brainfuck-go/main.go:28 +0x50

Go panics with the message: panic: runtime error: index out of range [30001] with length 30000 which implies it's simply because the brainfuck tape (represented by a Go byte slice in interpreter mode) runs out of memory and tries to access the 3001st element.

I run the following tests in all optimization options (-F, -B, and -O):
The native binary results in a segfault.
When running it with the -jvm flag, it panics with the following message:

30001 out of bounds for length 30000
        at Random.main(baris-inandi__brainfuck_go_1799082334.java:6)

So it's most definitely an index out of bounds situation.

The solution

I'm inclined to think doing a simple N%30000 when accessing the bf tape is the easiest way to handle this (maybe adding a -no-wrap-tape cli option would be nice). However, it might have some negative effect on the performance. If it does, we can make the default behavior not wrap the array and add a cli option that enables the feature (like -use-wrap-tape).

@Rudxain
Copy link
Author

Rudxain commented Mar 10, 2023

I agree with the latter patch. The wrapping behavior is "non-standard" so it should be chosen explicitly.

If I have time, I'll try debugging the code once again, to find a fix rather than a patch. Perhaps ChatGPT could help. I've used CGPT on some other code, and it was kinda decent at finding bugs (except for something "simple", like properly handling -0 in a JS fn, lol)

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

No branches or pull requests

2 participants