buzz-lang / BittyBuzz

BittyBuzz is an implementation of Buzz for microcrontrollers.
MIT License
8 stars 7 forks source link

Onconflict leaks heap memory #17

Open xgroleau opened 3 years ago

xgroleau commented 3 years ago

It seems when a stigmergy message causes an onconflict to be called, memory is leaked from the system, which eventually can cause an out of memory error. The error is probably caused in the following lines. I tried to investigate what was leaking memory exactly, but I'm not familiar enough with the garbage collection of the vm yet.

Here is the C code to reproduce

#define FILE_TEST5 "resources/5_Onconflict.bbo"
TEST(vm_onconflict) {
    // Init VM
    vm = &vmObj;

    uint16_t robot = 0;
    bbzvm_construct(robot);
    bbzvm_set_error_receiver(&set_last_error);
    fbcode = fopen(FILE_TEST5, "rb");
    REQUIRE(fbcode != NULL);
    REQUIRE(fseek(fbcode, 0, SEEK_END) == 0);
    fsize = ftell(fbcode);
    REQUIRE(fsize > 0);
    REQUIRE(fseek(fbcode, 0, SEEK_SET) >= 0);

    bbzvm_set_bcode(&testBcode, fsize);

    REQUIRE(vm->state == BBZVM_STATE_READY);
    REQUIRE(bbzvm_register_functions() >= 0); // If this fails, it means that the heap doesn't have enough memory allocated to execute this test.

    // Stepping through script
    while (vm->state == BBZVM_STATE_READY) {
#ifdef DEBUG
        uint8_t instr = *vm->bcode_fetch_fun(vm->pc,1);
        if (instr > BBZVM_INSTR_CALLS) {
            printf("[%d: %s %d]\n", vm->pc, instr_desc[instr], *(int16_t*)vm->bcode_fetch_fun(vm->pc+1,2));
        }
        else {
            printf("[%d: %s]\n", vm->pc, instr_desc[instr]);
        }
#endif
        bbzvm_step();
        ASSERT(vm->state != BBZVM_STATE_ERROR);
    }
    ASSERT_EQUAL(vm->state, BBZVM_STATE_DONE);
    ASSERT_EQUAL(vm->error, BBZVM_ERROR_NONE);

    vm->state = BBZVM_STATE_READY;

    // Sending stigmergy message, will leak memory and eventually get an memory error
    uint8_t i = 0;
    while (1) {
        i++;
        bbzmsg_payload_t payload;
        uint8_t buff[] = {1, i, 0, __BBZSTRID_data, 0, 57, 2, 0, 1};
        bbzringbuf_construct(&payload, buff, 1, 16);
        payload.elsize = 1;
        payload.capacity = 10;
        payload.datastart = 0;
        payload.dataend = 9;

        bbzinmsg_queue_append(&payload);
        bbzvm_process_inmsgs();
        if(vm->error == BBZVM_ERROR_MEM){
            break;
        }

        bbzvm_gc();
    }

    // Runs out of memory
    ASSERT_EQUAL(vm->state, BBZVM_STATE_ERROR);
    ASSERT_EQUAL(vm->error, BBZVM_ERROR_MEM);

    bbzvm_destruct();
    fclose(fbcode);
}

Here is the buzz code from ressources/5_Onconflict.bzz

stig = stigmergy.create(0);
stig.onconflict(function(key, ld, rd) {
        return ld
})
beltrame commented 2 years ago

Just to confirm, this issue is present only in BittyBuzz, not Buzz?

xgroleau commented 2 years ago

I did not test on Buzz, we only use BittyBuzz since we are on an embedded controller. It's been some time since I've checked this issues, I can try to reproduce it on Buzz