emojicode / emojicode

😀😜🔂 World’s only programming language that’s bursting with emojis

Home Page:https://emojicode.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Segmentation fault - unknown cause

joeskeen opened this issue · comments

🚨 Problem

Attempting to code a solution for this puzzle in EmojiCode: https://adventofcode.com/2018/day/1 . Program has segmentation fault on third iteration of 🔁 loop. I have no idea what would be causing this as the first two iterations work fine:

🏁🍇
  💭initialize offset to 0💭
  0➡️🖍️🆕offset

  💭read the first line of input💭
  🆕🔡👂🏼❗➡️🖍️🆕line

  💭until we run out of input💭
  🔁❎line🙌🔤🔤❗🍇
    💭output line for debug purposes💭
    😀🍪🔤'🔤line🔤'🔤🍪❗

    💭parse line into number💭
    🔢line 10❗➡️num

    💭add number to offset💭
    offset⬅️➕🍺num

    💭read next line💭
    🆕🔡👂🏼❗➡️🖍️line
  🍉

  💭output final offset💭
  😀🔡offset 10❗❗
  😀🔤done🔤❗
🍉

First few lines of input:

-16
+12
-18
-1
+5
-8
+9
-15
+12
+6
+11
+7

see https://tio.run/##fVfLbiNFFN37K3rHotVSdVVXV/fnZIgDRhk7SmyBWDFIgJAII1B4LWasQawQ7BHia/ID@A9C3XNOVXs0EpKddlfVvXXvOfeV9fPdR5v3d5fr8enpdHz54nS8/2rVNKfj939utpv95uJ68@m62V1d3a33zX7XONvJB9zj6zf//vXydPzxHo8vf@CZlYRv1xeXzf7DdXO1ub3bN9ebrWlpNtubw14qTOh0fHhzOn73eb76n8dXP72j1OSKysN2v7luPl43t4dtszvs39X38OLx1bcmcjr@8k1@/c2@Wa2coposmWVo0dXutrlcPzt80Nwcbm92d@s76bKjP3@W5X43Fe/ZH@rVi@1kxauq9ebi9m5NpZttxml7eP5sfXum7eFXbPauupmPLPIXl5eSMZSJZZXm6@MfX2Spx9cZtPu/3xIG2Nv1J/Tq7NL/Bxjg2rH7rwvGAudqs724ftsK4vHwRqFAPzICdee3yx0BslXT@fTU9eOq7f2q66f8XbVx1U2rds6/I9Zb287radXN@VfAiQGf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9ZwmGVqapPpS7EvyAA6rebEMIOqMrVjwZ/3D1YmOvY7ogDY2LkWGLsLFWOcuG2moIyZjjGEXUeTaKIv60vd7KZfhZYSJdVBiO5zOGmY1fagTMEeHVkPPUsI4/sxyLykrfBmASYh5NYrQuTbzvoxf8sfXeKSpnTK4H5dZGwk6iX6NoH5cYl82yB8OnREfear27DkWGdW@zJCxtk6GjKJMNdonlQWn@aAMwKU39mwWClHLhwmdAkUF91lxtZE8cQiaa5@c5cws1l2J0DKIDTVGGELW9xxNnTEeYLAyZ2yej3i2we43XK18IO97HgJoDk3K21rU1W7pGC35V9soE7qqonLcL@NGGQ/rCMH/PFRk@opUp8l/yfplotbkPdQBAwTMjBzNwH2tCG39pyvrDiFO/wE

Still not sure what was causing the error, but found another strange error when playing around with it:

🏁🍇
  💭initialize offset to 0💭
  0➡️🖍️🆕offset

  💭read the first line of input💭
  🆕🔡👂🏼❗➡️🖍️🆕line

  💭until we run out of input💭
  🔁❎line🙌🔤🔤❗🍇
    💭parse line into number💭
    🔢line 10❗➡️num

    💭add number to offset💭
    offset⬅️➕🍺num

    💭output line for debug purposes💭
    😀🍪🔤'🔤line🔤'🔤🍪❗

    💭read next line💭
    🆕🔡👂🏼❗➡️🖍️line
  🍉

  💭output final offset💭
  😀🔡offset 10❗❗
  😀🔤done🔤❗
🍉

Output:

'-16'
'+12'
🤯 Program panicked: Unwrapped an optional that contained no value. (.bin.tio.emojic:14:14)

See https://tio.run/##fZLfSsMwFMbv8xS586IUVmVze5zOphrpkpImKF45QUWwDmX@u9jCxCvRexGfpi9g3qCeNFndGHiRNCfnfL9851Ay4od0jyekV9dGT8ZGl5cIY6PvPiijksYZPSGYp2lBJJYcd2wGCjrVfPHzOTH6oWw@F/euBnmxIHGC5QHBKRWFxBllloIpy5X0CCsyerow@vYMnv6uZo8bUKtbIhWTNMNHBAvFMFdykzcdV7MbKzH6@RrCV7sA65tymDwWBXGGKIOOmBoNifCMhvLSJKNOawhKUKuPk8Rr7Dxc163ahdX7OaiqObRXfq2JwTYYdq@nXOCEDNU@zpXIeUGKFRNPp6B9s/637Oaa8oHNgLc/ajNsRo4deIXy/4Cb4dqy8mo5Y@8vpSzO1ntzlqYL/yu46YCJNvOacOfR3lpmXYdRDwXRNgqjPiwUdFHYR8EAzt3mPrBpuN9F4QBOO@gX

Just reproduced the segmentation fault when running it as a console application under WSL. Definitely not a TIO issue.

commented

I’ll have a look at this later. You tested it using 1.0 beta 1 I assume?

Correct.

commented

I have tracked down the issue. I’ll make sure to fix this soon. In the meantime two suggestions to work around the issue:

  1. Change the flow control of the program to make the variable local to the loop. You could check whether the string was empty inside the loop, then in the if body, do you final calculations and return from 🏁 there.
  2. Perform an operation on line that prevents the "optimisation", i.e. all forms of escaping use.

In case you wonder what’s happening: The Emojicode Compiler is optimising memory usage overaggressively here. It detects that your string does not outlive the function and moves the allocation to the stack. However, while allocating in the loop, it allocates the new string where the old string is currently allocated but before releasing the old string, which causes the release of the old string to release the new string instead.

commented

Example of working around the issue that works for me:

🏁🍇
  💭initialize offset to 0💭
  0➡️🖍️🆕offset

  💭read the first line of input💭
  🆕🔡👂🏼❗➡️🖍️🆕line

  🍨line🍆

  💭until we run out of input💭
  🔁❎line🙌🔤🔤❗🍇
    💭parse line into number💭
    🔢line 10❗➡️num

    💭add number to offset💭
    offset⬅️➕🍺num

    💭output line for debug purposes💭
    😀🍪🔤'🔤line🔤'🔤🍪❗

    💭read next line💭
    🆕🔡👂🏼❗➡️🖍️line
  🍉

  💭output final offset💭
  😀🔡offset 10❗❗
  😀🔤done🔤❗
🍉

Awesome! Thanks for looking into this!

commented

The fix for this bug will be included in 1.0 beta 2.