Adding External C Modules with external functions to mp as a library

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Adding External C Modules with external functions to mp as a library

Post by dhylands » Wed Mar 11, 2020 8:25 pm

For some values, mp_obj_new_int_from_uint will do a heap allocation, so your heap might not be initialized properly.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Adding External C Modules with external functions to mp as a library

Post by cduran » Wed Mar 11, 2020 10:19 pm

dhylands wrote:
Wed Mar 11, 2020 8:25 pm
For some values, mp_obj_new_int_from_uint will do a heap allocation, so your heap might not be initialized properly.
We are using the lib in a project that already had mp. We haven't changed any of the initialization code, this code has been working since 2016.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Adding External C Modules with external functions to mp as a library

Post by dhylands » Thu Mar 12, 2020 1:01 am

It's also possible that the corruption is occuring elsewhere in your code and its just the call to mp_obj_new_int_from_uint that's tripping up after the corruption.

Try creating an empty function that just calls mp_obj_new_int_from_uint and returns a similar value to your C code.

It could also be stack overflow, of the C code is allocating large objects on the stack. This can then cause heap corruption. The stack is typically located at the high end of RAM and the heap normally ends just before the stack.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Adding External C Modules with external functions to mp as a library

Post by cduran » Thu Mar 12, 2020 2:02 pm

I found the problem. MP's UART code was conflicting with our firmware. Simple fix.

Which brings back an old question I've had, can the CLI be easily removed, along with the entire hardware layer? Basically have mp run scripts and let my module do all the work. In the process making mp even smaller and more portable.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Adding External C Modules with external functions to mp as a library

Post by cduran » Thu Mar 12, 2020 6:01 pm

If it's not one thing it's another lol.

So the library along with my c module is sort of working now, however, when calling mp_obj_get_int() there seems to be an overflow problem when using values >= 0x40000000. Values <= 0x3FFFFFFF don't have a problem. Basically it's having issues with numbers greater than 30 bits.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Adding External C Modules with external functions to mp as a library

Post by jimmo » Mon Mar 16, 2020 4:35 am

cduran wrote:
Thu Mar 12, 2020 6:01 pm
So the library along with my c module is sort of working now, however, when calling mp_obj_get_int() there seems to be an overflow problem when using values >= 0x40000000. Values <= 0x3FFFFFFF don't have a problem. Basically it's having issues with numbers greater than 30 bits.
This is the "some values" that dhylands referred to earlier in "For some values, mp_obj_new_int_from_uint will do a heap allocation..."

"Small ints" are stored inline in the mp_obj_t value. "Long ints" depend on the implementation that's enabled. Most ports use the MPZ implementation.

When you access an int value, you need to know whether it's actually a long int.

Post Reply