dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
517 stars 97 forks source link

BigInt RTS functions return pointers to stack instead of heap #2604

Closed osa1 closed 3 years ago

osa1 commented 3 years ago

Relevant code: https://github.com/dfinity/motoko/blob/34a740618f099e6ca81eb6f7ea8170b2084cabf2/rts/motoko-rts/src/bigint.rs#L150-L168

persist_bigint takes stack-allocated mp_int struct. It's supposed to move the struct to the heap (as documented) but it returns a pointer to the stack instead.

osa1 commented 3 years ago

I also don't see where we initialize the BigInt object header here. BigInt::from_payload does not do it: https://github.com/dfinity/motoko/blob/34a740618f099e6ca81eb6f7ea8170b2084cabf2/rts/motoko-rts/src/types.rs#L325-L327

osa1 commented 3 years ago

False alarm. The allocation is done in mp_init, which calls mp_alloc, which allocates on the heap with the BigInt tag.

I guess I didn't expect mp_init to allocate, I'd expect mp to allocate lazily (e.g. in mp_set_u32 when needed).