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

Onconflict leaks heap memory #17

Open
xgroleau opened this issue Jul 21, 2021 · 2 comments
Open

Onconflict leaks heap memory #17

xgroleau opened this issue Jul 21, 2021 · 2 comments

Comments

@xgroleau
Copy link
Contributor

xgroleau commented Jul 21, 2021

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
Copy link
Contributor

beltrame commented Nov 8, 2021

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

@xgroleau
Copy link
Contributor Author

xgroleau commented Nov 9, 2021

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

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