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

mrbc 3.2.0 - custom module method not resolved #5993

Open
vivi168 opened this issue May 1, 2023 · 14 comments
Open

mrbc 3.2.0 - custom module method not resolved #5993

vivi168 opened this issue May 1, 2023 · 14 comments

Comments

@vivi168
Copy link

vivi168 commented May 1, 2023

Hello.

I recently tried to update from 3.1.0 to 3.2.0

But I'm encountering a weird problem with mrbc.

For example, in the C program, I define the following module and method:

rng = mrb_define_module(mrb, "Rng");
mrb_define_module_function(mrb, rng, "randint", mrb_randint, MRB_ARGS_REQ(2));

And I have the following mruby script that I want to call from the C program:

sum = 0

100.times do
  a = Rng::randint(1,6)

  sum += a
end

puts "avg= #{sum / 100.0}"

sum

When I compile it with mrbc 3.2.0 I get the following output:

xxd -a test.mrb
00000000: 5249 5445 3033 3030 0000 00d8 4d41 545a  RITE0300....MATZ
00000010: 3030 3030 4952 4550 0000 00a2 3033 3030  0000IREP....0300
00000020: 0000 0055 0002 0007 0001 0000 0000 0020  ...U........... 
00000030: 0601 0302 6457 0300 3002 0000 5103 0001  ....dW..0...Q...
00000040: 0401 0205 0141 0452 032d 0201 0138 0169  .....A.R.-...8.i
00000050: 0002 0000 0561 7667 3d20 0005 0000 0000  .....avg= ......
00000060: 0000 5940 0002 0005 7469 6d65 7300 0004  ..Y@....times...
00000070: 7075 7473 0000 0000 4100 0300 0700 0000  puts....A.......
00000080: 0000 0000 2134 0000 001d 0300 0704 0c05  ....!4..........
00000090: 2f03 0102 0102 0321 0301 0001 0402 3c03  /......!......<.
000000a0: 2203 0100 3803 0000 0002 0003 526e 6700  "...8.......Rng.
000000b0: 0003 7375 6d00 4c56 4152 0000 001a 0000  ..sum.LVAR......
000000c0: 0002 0003 7375 6d00 0161 0000 ffff 0001  ....sum..a......
000000d0: 454e 4400 0000 0008                      END.....

If I execute the C program, I get the following error from mruby

undefined method 'sum' (NoMethodError)

We can see in the mrb disassembly above 000000a0 [...] Rng...sum

With mrbc 3.1.0, I don't get the same issue, randint symbol is left as is, as we can see in the hexdump below:

xxd -a test.mrb
00000000: 5249 5445 3033 3030 0000 00dc 4d41 545a  RITE0300....MATZ
00000010: 3030 3030 4952 4550 0000 00a6 3033 3030  0000IREP....0300
00000020: 0000 0055 0002 0007 0001 0000 0000 0020  ...U........... 
00000030: 0601 0302 6457 0300 3002 0000 5103 0001  ....dW..0...Q...
00000040: 0401 0205 0141 0452 032d 0201 0138 0169  .....A.R.-...8.i
00000050: 0002 0000 0561 7667 3d20 0005 0000 0000  .....avg= ......
00000060: 0000 5940 0002 0005 7469 6d65 7300 0004  ..Y@....times...
00000070: 7075 7473 0000 0000 4500 0300 0700 0000  puts....E.......
00000080: 0000 0000 2134 0000 001d 0300 0704 0c05  ....!4..........
00000090: 2f03 0102 0102 0321 0301 0001 0402 3c03  /......!......<.
000000a0: 2203 0100 3803 0000 0002 0003 526e 6700  "...8.......Rng.
000000b0: 0007 7261 6e64 696e 7400 4c56 4152 0000  ..randint.LVAR..
000000c0: 001a 0000 0002 0003 7375 6d00 0161 0000  ........sum..a..
000000d0: ffff 0001 454e 4400 0000 0008            ....END.....

If I modify the script slightly:

p 'hello from mrb'
p 12 * 3

sum = 0

100.times do
  a = Rng::randint(1,6)

  sum += a
  p a
end

sum / 100.0

Now I get Rng...100.0 with mrbc 3.2.0. So it seems to take the last symbol of the script instead of randint.

xxd -a test.mrb
00000000: 5249 5445 3033 3030 0000 00f0 4d41 545a  RITE0300....MATZ
00000010: 3030 3030 4952 4550 0000 00ba 3033 3030  0000IREP....0300
00000020: 0000 0060 0002 0006 0001 0000 0000 0025  ...`...........%
00000030: 5103 002d 0200 0103 0324 2d02 0001 0601  Q..-.....$-.....
00000040: 0302 6457 0300 3002 0100 0102 0102 0301  ..dW..0.........
00000050: 4102 3802 6900 0200 000e 6865 6c6c 6f20  A.8.i.....hello 
00000060: 6672 6f6d 206d 7262 0005 0000 0000 0000  from mrb........
00000070: 5940 0002 0001 7000 0005 7469 6d65 7300  Y@....p...times.
00000080: 0000 004e 0003 0007 0000 0000 0000 0028  ...N...........(
00000090: 3400 0000 1d03 0007 040c 052f 0301 0201  4........../....
000000a0: 0203 2103 0100 0104 023c 0322 0301 0001  ..!......<."....
000000b0: 0402 2d03 0201 3803 0000 0003 0003 526e  ..-...8.......Rn
000000c0: 6700 0005 3130 302e 3000 0001 7000 4c56  g...100.0...p.LV
000000d0: 4152 0000 001a 0000 0002 0003 7375 6d00  AR..........sum.
000000e0: 0161 0000 ffff 0001 454e 4400 0000 0008  .a......END.....

I don't really understand what's going on. I couldn't find any information in the release note or in the issues. Has anything changed about how mrbc operates ?

I've compiled mruby myself, on macos 13.1
I get the same error with mruby from homebrew.

@matz
Copy link
Member

matz commented May 1, 2023

I need more information. Could you provide the whole process to create and execute your program, please?
FYI, invoke mrbc with --verbose will print the disassembled code.

@vivi168
Copy link
Author

vivi168 commented May 1, 2023

Yes, sorry for the late reply.

In the makefile I have a line that take a rb file as input.

test.mrb: test.rb
        $(MRBC) -Btest_rb test.rb

Then in the C program,

// main.c

#include "test.c"

// ...

mrb_rng_init(mrb); // function that call mrb_define_module and mrb_define_module_function
v = mrb_load_irep(mrb, test_rb);

// ...

After that I just run the C exectuable.

mrbc -v on 3.2.0
mruby 3.2.0 (2023-02-24)
00001 NODE_SCOPE:
00004   local variables:
00004     sum
00001   NODE_BEGIN:
00001     NODE_FCALL:
00001       method='p' (27)
00001       args:
00001         NODE_STR "hello from mrb" len 14
00002     NODE_FCALL:
00002       method='p' (27)
00002       args:
00002         NODE_CALL(.):
00002           NODE_INT 12 base 10
00002           method='*' (4)
00002           args:
00002             NODE_INT 3 base 10
00004     NODE_ASGN:
00004       lhs:
00004         NODE_LVAR sum
00004       rhs:
00004         NODE_INT 0 base 10
00006     NODE_CALL(.):
00006       NODE_INT 100 base 10
00006       method='times' (500)
00011       args:
00011       block:
00006         NODE_BLOCK:
00011           body:
00007             NODE_BEGIN:
00007               NODE_ASGN:
00007                 lhs:
00007                   NODE_LVAR a
00007                 rhs:
00007                   NODE_CALL(.):
00007                     NODE_CONST Rng
00007                     method='"100.0"' (1440)
00007                     args:
00007                       NODE_INT 1 base 10
00007                       NODE_INT 6 base 10
00009               NODE_OP_ASGN:
00009                 lhs:
00009                   NODE_LVAR sum
00009                 op='+' (5)
00009                 NODE_LVAR a
00010               NODE_FCALL:
00010                 method='p' (27)
00010                 args:
00010                   NODE_LVAR a
00014     NODE_CALL(.):
00014       NODE_LVAR sum
00014       method='/' (7)
00014       args:
00014         NODE_FLOAT 100.0
irep 0x6000023ac000 nregs=6 nlocals=2 pools=2 syms=2 reps=1 ilen=37
local variable names:
  R1:sum
file: test.rb
    1 000 STRING	R3	L(0)	; hello from mrb
    1 003 SSEND		R2	:p	n=1
    2 007 LOADI		R3	36	
    2 010 SSEND		R2	:p	n=1
    4 014 LOADI_0	R1	(0)		; R1:sum
    6 016 LOADI		R2	100	
    6 019 BLOCK		R3	I(0:0x6000023ac050)
    6 022 SENDB		R2	:times	n=0
   14 026 MOVE		R2	R1		; R1:sum
   14 029 LOADL		R3	L(1)	; 100.000000
   14 032 DIV		R2	R3
   14 034 RETURN	R2		
   14 036 STOP

irep 0x6000023ac050 nregs=7 nlocals=3 pools=0 syms=3 reps=0 ilen=40
local variable names:
  R2:a
file: test.rb
    6 000 ENTER		0:0:0:0:0:0:0 (0x0)
    7 004 GETCONST	R3	Rng	
    7 007 LOADI_1	R4	(1)	
    7 009 LOADI_6	R5	(6)	
    7 011 SEND		R3	:"100.0"	n=2
    7 015 MOVE		R2	R3		; R2:a
    9 018 GETUPVAR	R3	1	0	
    9 022 MOVE		R4	R2		; R2:a
    9 025 ADD		R3	R4
    9 027 SETUPVAR	R3	1	0	
   10 031 MOVE		R4	R2		; R2:a
   10 034 SSEND		R3	:p	n=1
   10 038 RETURN	R3
mrbc -v on 3.1.0
mruby 3.1.0 (2022-05-12)
00001 NODE_SCOPE:
00004   local variables:
00004     sum
00001   NODE_BEGIN:
00001     NODE_FCALL:
00001       method='p' (285212672)
00001       args:
00001         NODE_STR "hello from mrb" len 14
00002     NODE_FCALL:
00002       method='p' (285212672)
00002       args:
00002         NODE_CALL(.):
00002           NODE_INT 12 base 10
00002           method='*' (79)
00002           args:
00002             NODE_INT 3 base 10
00004     NODE_ASGN:
00004       lhs:
00004         NODE_LVAR sum
00004       rhs:
00004         NODE_INT 0 base 10
00006     NODE_CALL(.):
00006       NODE_INT 100 base 10
00006       method='times' (355000724)
00011       args:
00011       block:
00006         NODE_BLOCK:
00011           body:
00007             NODE_BEGIN:
00007               NODE_ASGN:
00007                 lhs:
00007                   NODE_LVAR a
00007                 rhs:
00007                   NODE_CALL(.):
00007                     NODE_CONST Rng
00007                     method='randint' (176)
00007                     args:
00007                       NODE_INT 1 base 10
00007                       NODE_INT 6 base 10
00009               NODE_OP_ASGN:
00009                 lhs:
00009                   NODE_LVAR sum
00009                 op='+' (78)
00009                 NODE_LVAR a
00010               NODE_FCALL:
00010                 method='p' (285212672)
00010                 args:
00010                   NODE_LVAR a
00014     NODE_CALL(.):
00014       NODE_LVAR sum
00014       method='/' (148)
00014       args:
00014         NODE_FLOAT 100.0
irep 0x6000014a4050 nregs=6 nlocals=2 pools=2 syms=2 reps=1 ilen=37
local variable names:
  R1:sum
file: test.rb
    1 000 STRING	R3	L(0)	; hello from mrb
    1 003 SSEND		R2	:p	n=1
    2 007 LOADI		R3	36	
    2 010 SSEND		R2	:p	n=1
    4 014 LOADI_0	R1			; R1:sum
    6 016 LOADI		R2	100	
    6 019 BLOCK		R3	I(0:0x6000014a40a0)
    6 022 SENDB		R2	:times	n=0
   14 026 MOVE		R2	R1		; R1:sum
   14 029 LOADL		R3	L(1)	; 100.000000
   14 032 DIV		R2	R3
   14 034 RETURN	R2		
   14 036 STOP

irep 0x6000014a40a0 nregs=7 nlocals=3 pools=0 syms=3 reps=0 ilen=40
local variable names:
  R2:a
file: test.rb
    6 000 ENTER		0:0:0:0:0:0:0 (0x0)
    7 004 GETCONST	R3	Rng	
    7 007 LOADI_1	R4		
    7 009 LOADI_6	R5		
    7 011 SEND		R3	:randint	n=2
    7 015 MOVE		R2	R3		; R2:a
    9 018 GETUPVAR	R3	1	0	
    9 022 MOVE		R4	R2		; R2:a
    9 025 ADD		R3	R4
    9 027 SETUPVAR	R3	1	0	
   10 031 MOVE		R4	R2		; R2:a
   10 034 SSEND		R3	:p	n=1
   10 038 RETURN	R3	

@matz
Copy link
Member

matz commented May 1, 2023

I could not reproduce the issue with mruby 3.2.0 on both Linux and MacOS.
Can you provide the whole source code without omitting any line, including Makefile?

@vivi168
Copy link
Author

vivi168 commented May 2, 2023

Sure, here under you can find the whole code. Thank you for your help!

mrubytest.zip

@matz
Copy link
Member

matz commented May 3, 2023

It still works for me. I needed to tweak a little for Linux environment, though.

  • swap $(LIBS) and $(LDFLAGS)
  • add -lm to LIBS
  • replace %d to %lld in test.c to stop a warning

I'm confusing. We used very similar environments and we got very different results. Do you use Intel Mac or M1 (or M2)?

According to your report, mrbc produced broken binary. And it suggests we have an unknown bug in the compiler (since AST was already broken).

@matz
Copy link
Member

matz commented May 3, 2023

The core of this issue is the following compiled binary:

00000000: 5249 5445 3033 3030 0000 00f0 4d41 545a  RITE0300....MATZ
00000010: 3030 3030 4952 4550 0000 00ba 3033 3030  0000IREP....0300
00000020: 0000 0060 0002 0006 0001 0000 0000 0025  ...`...........%
00000030: 5103 002d 0200 0103 0324 2d02 0001 0601  Q..-.....$-.....
00000040: 0302 6457 0300 3002 0100 0102 0102 0301  ..dW..0.........
00000050: 4102 3802 6900 0200 000e 6865 6c6c 6f20  A.8.i.....hello 
00000060: 6672 6f6d 206d 7262 0005 0000 0000 0000  from mrb........
00000070: 5940 0002 0001 7000 0005 7469 6d65 7300  Y@....p...times.
00000080: 0000 004e 0003 0007 0000 0000 0000 0028  ...N...........(
00000090: 3400 0000 1d03 0007 040c 052f 0301 0201  4........../....
000000a0: 0203 2103 0100 0104 023c 0322 0301 0001  ..!......<."....
000000b0: 0402 2d03 0201 3803 0000 0003 0003 526e  ..-...8.......Rn
000000c0: 6700 0005 3130 302e 3000 0001 7000 4c56  g...100.0...p.LV
000000d0: 4152 0000 001a 0000 0002 0003 7375 6d00  AR..........sum.
000000e0: 0161 0000 ffff 0001 454e 4400 0000 0008  .a......END.....

If I can reproduce this file on my machine, I am sure I can fix the issue. If you can regenerate the above file, please explain how to regenerate the file in the step by step description.

@vivi168
Copy link
Author

vivi168 commented May 3, 2023

I'm using an intel mac.

I can reproduce this file everytime I use mrbc from 3.2.0 (either with mrbc I get when I compile mruby from source on the master branch, or with mrbc from homebrew install)

for example

➜  mrubytest mrbc --version # from the homebrew installation
mruby 3.2.0 (2023-02-24)
➜  mrubytest cat test.rb
p 'Test'
p 12 * 3

sum = 0

100.times do
  a = Rng::randint(1,6)

  sum += a
  p a
end


sum / 100.0


➜  mrubytest mrbc test.rb
➜  mrubytest xxd -a test.mrb
00000000: 5249 5445 3033 3030 0000 00e6 4d41 545a  RITE0300....MATZ
00000010: 3030 3030 4952 4550 0000 00b0 3033 3030  0000IREP....0300
00000020: 0000 0056 0002 0006 0001 0000 0000 0025  ...V...........%
00000030: 5103 002d 0200 0103 0324 2d02 0001 0601  Q..-.....$-.....
00000040: 0302 6457 0300 3002 0100 0102 0102 0301  ..dW..0.........
00000050: 4102 3802 6900 0200 0004 5465 7374 0005  A.8.i.....Test..
00000060: 0000 0000 0000 5940 0002 0001 7000 0005  ......Y@....p...
00000070: 7469 6d65 7300 0000 004e 0003 0007 0000  times....N......
00000080: 0000 0000 0028 3400 0000 1d03 0007 040c  .....(4.........
00000090: 052f 0301 0201 0203 2103 0100 0104 023c  ./......!......<
000000a0: 0322 0301 0001 0402 2d03 0201 3803 0000  ."......-...8...
000000b0: 0003 0003 526e 6700 0005 3130 302e 3000  ....Rng...100.0.
000000c0: 0001 7000 4c56 4152 0000 001a 0000 0002  ..p.LVAR........
000000d0: 0003 7375 6d00 0161 0000 ffff 0001 454e  ..sum..a......EN
000000e0: 4400 0000 0008                           D.....

And 3.1.0

➜  mrubytest $MRUBY_PATH/build/host/bin/mrbc --version # compiled from 3.1.0 tag
mruby 3.1.0 (2022-05-12)         
➜  mrubytest cat test.rb
p 'Test'
p 12 * 3

sum = 0

100.times do
  a = Rng::randint(1,6)

  sum += a
  p a
end


sum / 100.0


➜  mrubytest $MRUBY_PATH/build/host/bin/mrbc test.rb  
➜  mrubytest xxd -a test.mrb
00000000: 5249 5445 3033 3030 0000 00eb 4d41 545a  RITE0300....MATZ
00000010: 3030 3030 4952 4550 0000 00b2 3033 3030  0000IREP....0300
00000020: 0000 0056 0002 0006 0001 0000 0000 0025  ...V...........%
00000030: 5103 002d 0200 0103 0324 2d02 0001 0601  Q..-.....$-.....
00000040: 0302 6457 0300 3002 0100 0102 0102 0301  ..dW..0.........
00000050: 4102 3802 6900 0200 0004 5465 7374 0005  A.8.i.....Test..
00000060: 0000 0000 0000 5940 0002 0001 7000 0005  ......Y@....p...
00000070: 7469 6d65 7300 0000 0050 0003 0007 0000  times....P......
00000080: 0000 0000 0028 3400 0000 1d03 0007 040c  .....(4.........
00000090: 052f 0301 0201 0203 2103 0100 0104 023c  ./......!......<
000000a0: 0322 0301 0001 0402 2d03 0201 3803 0000  ."......-...8...
000000b0: 0003 0003 526e 6700 0007 7261 6e64 696e  ....Rng...randin
000000c0: 7400 0001 7000 4c56 4152 0000 001d 0000  t...p.LVAR......
000000d0: 0003 0003 7375 6d00 0126 0001 6100 0000  ....sum..&..a...
000000e0: 0100 0245 4e44 0000 0000 08              ...END.....

@matz
Copy link
Member

matz commented May 3, 2023

OK, I will try to get Intel Mac.

@matz
Copy link
Member

matz commented Nov 18, 2023

By the way, do you use clang version 13? We see similar issues with clang 13 lately.

@vivi168
Copy link
Author

vivi168 commented Nov 20, 2023

I don't remember if it was clang 13. I think I've updated my system since then. Right now I have clang 14.0.0
To be sure, I recompiled mruby 3.2.0 and still get the same issue with mrbc

@matz
Copy link
Member

matz commented Nov 20, 2023

Could you tell us more information please? Versions of OS, clang, type of CPU or whatever that may affect?

@vivi168
Copy link
Author

vivi168 commented Nov 24, 2023

Here are some more information

Please tell me if you need more details.

13-inch, 2017 MacBookPro14,1
2.3 GHz Dual-Core Intel Core i5-7360U
MacOs Ventura 13.6 (22G120)

clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin22.6.0
Thread model: posix

I compiled mruby from this commit: 87260e7 (tag: 3.2.0)

@matz
Copy link
Member

matz commented Nov 25, 2023

Could you please try the latest master if possible?

@vivi168
Copy link
Author

vivi168 commented Nov 27, 2023

I think I finally understood 🫣

I noticed there are two mrbc binaries.

find . -type f -name mrbc
./build/host/bin/mrbc
./build/host/mrbc/bin/mrbc

On master and 3.2.0, only build/host/mrbc/bin/mrbc produces a working mrb file.

On ruby 3.1.0 tag branch, both mrbc work.

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