emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.63k stars 3.28k forks source link

how to make the change of system lib source to work? #22462

Open yejun81 opened 2 weeks ago

yejun81 commented 2 weeks ago

Version of emscripten/emsdk: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.64 (a1fe3902bf73a3802eae0357d273d0e37ea79898) clang version 19.0.0git (https:/github.com/llvm/llvm-project 4d8e42ea6a89c73f90941fd1b6e899912e31dd34) Target: wasm32-unknown-emscripten Thread model: posix

i make a little change the emscripten_fetch.c file of the fetch lib, https://github.com/emscripten-core/emscripten/blob/5a1578a50cf0a4d7120813984c9a71cccc97e61a/system/lib/fetch/emscripten_fetch.c#L88 to something like this:

  printf("change calloc to malloc\n");
  emscripten_fetch_t* fetch = (emscripten_fetch_t*)malloc(sizeof(emscripten_fetch_t));
  if (!fetch) {
    return NULL;
  }
  memset(fetch, 0, sizeof(emscripten_fetch_t));

after change the code ,i remove the libfetch*.a in cache/sysroot/lib folder, and after compile , in runtime, i see the 'change calloc to malloc' output in the browser console, but when i see the wasm decode in web browser, there is no malloc call, the calloc call is still there, why?

sbc100 commented 2 weeks ago

If you see the printf in the console then I don't see how the following line wouldn't be a call to malloc.

You can check contents libfetch.a if you like by extracting that object files and using wasm-objdump -x on them.

yejun81 commented 2 weeks ago

yes, i am also very confused by the result, after change calloc to malloc, i even did printf in the dlcalloc function, the browser console showed that the real function be called was still dlcalloc. so, i thought it may due to compiler optimize?

sbc100 commented 2 weeks ago

I don't think the compiler would ever change a calloc to a malloc though.. that seem backwards. Something strange much be going on. What does the it looks like if you inspect (via objdump or similar) the emscripten_fetch.o object file? Do you see a call to calloc or malloc?

By the way why are trying to make this change from calloc to malloc?

yejun81 commented 2 weeks ago

seriously, i seeked for the compiled emscripten_fetch.o file, but i can't find it.it would be auto removed after compile? i can't use memory large than 4GB, so i want to record the actually memory used to do some control, so i made some change to the dlmalloc, but it seemed the calloc doesn't memset the memory because the change i did.so i changed the calloc call to malloc to test.but it still call calloc...

yejun81 commented 2 weeks ago

I don't think the compiler would ever change a calloc to a malloc though.. that seem backwards. Something strange much be going on. What does the it looks like if you inspect (via objdump or similar) the emscripten_fetch.o object file? Do you see a call to calloc or malloc?

By the way why are trying to make this change from calloc to malloc?

i have confirmed that when there is a memset after malloc, the compiler will change these to a calloc call. anyway, thks for your reply and advices. and i want to know where the compiled intermediate files like emscripten_fetch.o?

sbc100 commented 1 week ago

To get emscripten_fetch.o you can run ar x cache/sysroot/lib/wasm32-emscripten/libfetch.a.