Search code examples
emscripten

Met Uncaught (in promise) RuntimeError: index out of bounds in Emscripten


I Use Emscripten's emcmake to compile my C project.

When I opened the generated webpage, I got the error:

the picture from browser Firefox

text of above picture

Uncaught (in promise) RuntimeError: index out of bounds createExportWrapper http://localhost:63342/new_exp/emsdk-build/executable.js:1165 callMain http://localhost:63342/new_exp/emsdk-build/executable.js:9815 doRun http://localhost:63342/new_exp/emsdk-build/executable.js:9869 run http://localhost:63342/new_exp/emsdk-build/executable.js:9884 runCaller http://localhost:63342/new_exp/emsdk-build/executable.js:9800 removeRunDependency http://localhost:63342/new_exp/emsdk-build/executable.js:1086 receiveInstance http://localhost:63342/new_exp/emsdk-build/executable.js:1253 receiveInstantiationResult http://localhost:63342/new_exp/emsdk-build/executable.js:1271 promise callbackcreateWasm/instantiateAsync/< http://localhost:63342/new_exp/emsdk-build/executable.js:1312 promise callbackinstantiateAsync http://localhost:63342/new_exp/emsdk-build/executable.js:1304 createWasm http://localhost:63342/new_exp/emsdk-build/executable.js:1341 http://localhost:63342/new_exp/emsdk-build/executable.js:9333executable.wasm:384544:1

I run this C project on Linux(not using Emscripten), check it with sanitizers and all runs well.

I find the problem might be caused by:

uint64_t i; 
char     text[1024];    
FILE* fp = stdout;

fprintf(fp, "CAPACITY %" PRIu64 ", ELE_SIZE %" PRIu64 ", BUCKET_SIZE %" PRIu64 "\n", map->buckets_capacity, map->ele_size, map->bucket_size);
fprintf(fp, "USER_DATA 0x%p\n", map->user_data);    
fprintf(fp, "%6s\t%16s\tDATA\n", "psl", "HASH");    
for (i = 0; i < map->buckets_capacity; i++) {       
    _bucket_t *bucket;      

    /* error happened on the line below */      
    bucket = (_bucket_t *) ((char *) map->buckets_ + i * map->bucket_size); /*original: hashmap_bucket_at_(map, map->buckets_, i);*/    
    
    if (bucket->psl) print_data(hashmap_bucket_data_(bucket), text, 1024);      
    else text[0] = '\0';    
    fprintf(fp, "%6u\t%16lu\t%s\n", bucket->psl, bucket->hash, text);   
}

The memory is allocated by:

map->buckets_  = malloc(map->bucket_size * map->buckets_capacity);

where bucket_size is 144, and bucket_capacity is 16.

I have used "-s ALLOW_MEMORY_GROWTH=1" and "-fsanitize=address" when use Emscripten when this error occurs.I don't know why this happened.


Solution

  • fprintf(fp, "%6u\t%16lu\t%s\n", bucket->psl, bucket->hash, text);   
    

    Where bucket->hash has a type uint64_t .

    After I change %lu to %"PRIu64", no problems appear anymore.