modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.94k stars 2.58k forks source link

[BUG]: Printing `let` variable string doesn't work quite right in REPL #638

Open cversek opened 1 year ago

cversek commented 1 year ago

Bug description

I just downloaded the 0.2.1 SDK and found a little quirk in the REPL. I couldn't find an existing issue which quite matched the odd behavior...

When a let type variable is entered in one block of the REPL and printed in another, print shows a weird output. If the statements are in the same block it works (sort of).

Steps to reproduce

The anomalous bind and print:

Welcome to Mojo! 🔥
Expressions are delimited by a blank line.
Type `:mojo help` for further assistance.
1> let my_var = "Hello mojo?"   
2. 
(StringLiteral) my_var = {
  (string) value =
}
2> print(my_var)
3. 
%.*s

A working bind and print:

3> let my_var2 = "Hello mojo!"
4. print(my_var2)
5. 
Hello mojo!
(StringLiteral) my_var2 = {
  (string) value =
}

I am rocking Ubuntu 23.04 with this python interpreter:

5> %%python
6. import sys
7. print("Python version from python:", sys.version)
8. 
Python version from python: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:23:32) [GCC 11.3.0]
(PythonObject) sys = {
  (PyObjectPtr) py_object = {
    (pointer<scalar<si8>>) value = 0x00007fa59e30a390
  }
}

Just for fun I tried to print that last variable again with weird results:

8> print(my_var2)
9. 
rint(\"Pyth

Looks like a buffer overflow?

System information

- What OS did you do install Mojo on ?
Ubuntu 23.04

- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 0.2.1 (64d14e85)

- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.1.4 (6b54d308)
Aliraqimustafa commented 1 year ago

up

cversek commented 1 year ago

Here is a repro using my other system, Ubuntu 22.04.3 LTS. This time I inserted another print statement with the alphabet so the buffer misalignment can be measured (a clue?):

Welcome to Mojo! 🔥
Expressions are delimited by a blank line.
Type `:mojo help` for further assistance.
1> let my_var = "Hello mojo?"
2. 
(StringLiteral) my_var = {
  (string) value =
}
2> print(my_var)
3. 
%.*s
3> let my_var2 = "Hello mojo!"
4. print(my_var2)
5. 
Hello mojo!
(StringLiteral) my_var2 = {
  (string) value =
}
5> print("abcdefghijklmnopqrstuvwxyz")
6. 
abcdefghijklmnopqrstuvwxyz
6> print(my_var2)
7. 
qrstuvwxyz
7> %%python
8. import sys
9. print("Python version from python:", sys.version)
10. 
Python version from python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:55:59) [GCC 12.3.0]
(PythonObject) sys = {
  (PyObjectPtr) py_object = {
    (pointer<scalar<si8>>) value = 0x00007f8d3457e390
  }
}
10> 
cversek commented 1 year ago

Bug is independent of variable mutability type:

1> my_var = "Hello mojo?"
2. 
(StringLiteral) my_var = {
  (string) value =
}
2> print(my_var)
3. 
%.*s
3> 
4> var my_var2 = "Hello mojo?"
5. 
(StringLiteral) my_var2 = {
  (string) value =
}
5> print(my_var2)
6. 
%.*s
6> 
hududed commented 1 year ago

not sure if related, but print("Hello, World!") + enter twice in Mojo REPL just hangs at the next line:

1> print("Hello, world!")
2. 

Am running dev container on macos 11.7.8 (20G1351)

cversek commented 1 year ago

@hududed That bug might have a similar origin in your environment to the one in mine, but that statement seems to work correctly in mine.

cversek commented 1 year ago

@ematejska Still broken in v0.3 of the SDK... come on guys you need to prioritize a working REPL! Slightly different symptoms, but that's to be expected with a memory access bug:

Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

  1> let my_var = "Hello mojo?" 
  2.  
(StringLiteral) my_var = {
  (string) value = "Hello mojo?"
}
  2> print(my_var) 
  3.  
%.*s
  3> let my_var2 = "Hello mojo!" 
  4. print(my_var2) 
  5.  
Hello mojo!
(StringLiteral) my_var2 = {
  (string) value = "Hello mojo!"
}
  5> print("abcdefghijklmnopqrstuvwxyz") 
  6.  
abcdefghijklmnopqrstuvwxyz
  6> print(my_var2) 
  7.  
abcdefghijk
  7>  

System information

- What OS did you do install Mojo on ?
Ubuntu 23.04

- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 0.3.0 (f64f9601)

- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.1.4 (6b54d308)
cversek commented 11 months ago

@ematejska Still broken in v0.4.0 of the SDK, identical symptoms to v0.3.0 :

Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

  1> let my_var = "Hello mojo?" 
  2.  
(StringLiteral) my_var = "Hello mojo?"
  2> print(my_var) 
  3.  
%.*s
  3> let my_var2 = "Hello mojo!" 
  4. print(my_var2) 
  5.  
Hello mojo!
(StringLiteral) my_var2 = "Hello mojo!"
  5> print("abcdefghijklmnopqrstuvwxyz")  
  6.  
abcdefghijklmnopqrstuvwxyz
  6> print(my_var2) 
  7.  
abcdefghijk
  7>  

System information

- What OS did you do install Mojo on ?
Ubuntu 23.04

- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 0.4.0 (9e33b013)

- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.2.0 (355ea9c0)
lattner commented 11 months ago

This is a known issue with top level code.

cversek commented 11 months ago

@lattner It's great to hear that this issue is getting some attention now. I would have loved to contribute a possible fix with a PR, but that's hard to do without source code 😕... probably wouldn't understand all the mysterious layers anyway, but I do like a good puzzle. I imagine a working REPL would be a big priority, right up there with all the awesome features your team is currently implementing; so, can we expect a potential fix to test in v0.5?

cversek commented 5 months ago

@lattner @ematejska Well, it's been a while, on this occasion of the Mojo standard library being open sourced ( 🥂), I decided to test out this bug again.

Unfortunately, it is still broken in v24.2.0 of the SDK, similar symptoms as before:

Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

  1> var my_var = "Hello mojo?"  
  2.  
(StringLiteral) my_var = "Hello mojo?"
  2> print(my_var) 
  3.  
a
  3> var my_var2 = "Hello mojo!" 
  4. print(my_var2) 
  5.  
Hello mojo!
(StringLiteral) my_var2 = "Hello mojo!"
  5> print("abcdefghijklmnopqrstuvwxyz") 
  6.  
abcdefghijklmnopqrstuvwxyz
  6> print(my_var2)

  7> ^D

System information

- What OS did you do install Mojo on ?
Ubuntu Ubuntu 23.10

- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 24.2.0 (c2427bc5)

- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.5.2 (6b3a04fd)