emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.87k stars 3.32k forks source link

Uncaught TypeError: Cannot read properties of undefined (reading 'length') #20731

Open Flyingbeanss opened 1 year ago

Flyingbeanss commented 1 year ago

Hi! I want to deal with memory access out of bound in my program. After I add set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") and set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address") , the console print as follow:

Uncaught TypeError: Cannot read properties of undefined (reading 'length')
    at lengthBytesUTF8 (occt-webgl-sample.js?t=1700141293680:1746:26)
    at stringToNewUTF8 (occt-webgl-sample.js?t=1700141293680:5971:13)
    at occt-webgl-sample.js?t=1700141293680:10523:32
    at withBuiltinMalloc (occt-webgl-sample.js?t=1700141293680:5962:10)
    at _emscripten_pc_get_file (occt-webgl-sample.js?t=1700141293680:10519:37)
    at occt-webgl-sample.wasm.__sanitizer::EmscriptenSymbolizerTool::SymbolizePC(unsigned long, __sanitizer::SymbolizedStack*) (occt-webgl-sample.wasm:0x16a5673)
    at occt-webgl-sample.wasm.__sanitizer::Symbolizer::SymbolizePC(unsigned long) (occt-webgl-sample.wasm:0x16a5960)
    at occt-webgl-sample.wasm.__lsan::LeakSuppressionContext::GetSuppressionForAddr(unsigned long) (occt-webgl-sample.wasm:0x1696920)
    at occt-webgl-sample.wasm.__lsan::LeakSuppressionContext::Suppress(unsigned int, unsigned long, unsigned long) (occt-webgl-sample.wasm:0x1696a6b)
    at occt-webgl-sample.wasm.__lsan::LeakReport::ApplySuppressions() (occt-webgl-sample.wasm:0x1697c0f)

I just don't understand why. Here is the original output.

occt-webgl-sample.js?t=1700142325331:14608 Uncaught RuntimeError: memory access out of bounds
    at occt-webgl-sample.wasm.math_FunctionSetRoot::math_FunctionSetRoot(math_FunctionSetWithDerivatives&, math_Vector const&, int) (occt-webgl-sample.wasm:0xa28b01)
    at invoke_iiiii (occt-webgl-sample.js?t=1700142325331:14605:36)
    at occt-webgl-sample.wasm.math_FunctionRoot::math_FunctionRoot(math_FunctionWithDerivative&, double, double, double, double, int) (occt-webgl-sample.wasm:0xa261a0)
    at occt-webgl-sample.wasm.Extrema_LocEPCOfLocateExtPC::Perform(gp_Pnt const&, double) (occt-webgl-sample.wasm:0x8a659a)
    at invoke_viid (occt-webgl-sample.js?t=1700142325331:14990:29)
    at occt-webgl-sample.wasm.Extrema_LocateExtPC::Perform(gp_Pnt const&, double) (occt-webgl-sample.wasm:0x8a56ff)
    at invoke_viid (occt-webgl-sample.js?t=1700142325331:14990:29)
    at occt-webgl-sample.wasm.Extrema_LocateExtPC::Extrema_LocateExtPC(gp_Pnt const&, Adaptor3d_Curve const&, double, double, double, double) (occt-webgl-sample.wasm:0x8a62af)
    at invoke_iiiidddd (occt-webgl-sample.js?t=1700142325331:16343:36)
    at occt-webgl-sample.wasm.ShapeAnalysis_Curve::ProjectAct(Adaptor3d_Curve const&, gp_Pnt const&, double, gp_Pnt&, double&) const (occt-webgl-sample.wasm:0x4957fd)

Thanks for help , sincerely!

sbc100 commented 1 year ago

Hmm.. it looks like convertPCtoSourceLocation must be returning a result that doesn't have a .file attribute so undefined is being passed to stringToNewUTF8 here: https://github.com/emscripten-core/emscripten/blob/9429b4893c5e376e23e67d7007d2b948987445a0/src/library.js#L2819

Strange that we have never seen this before.

Can you try adding -g and/or --profiling-funcs to your compile and link flags?

Flyingbeanss commented 1 year ago

@sbc100 Thanks for reply. Actually, I have added set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") and set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g"). After I add set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --profiling-funcs") and set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --profiling-funcs"), it seems to be no change in the console.

sbc100 commented 1 year ago

Can you take look devtools a few levels up the stack in emscripten_pc_get_file? Can you see what result contains? (The thing that convertPCtoSourceLocation returns?)

Flyingbeanss commented 1 year ago

@sbc100 Thanks for your advice. I'm not very familiar with js and browser.

 var result = convertPCtoSourceLocation(pc);
 if (!result) return 0;

convertPCtoSourceLocation(pc); returns null

sbc100 commented 1 year ago

When the crash happens though I'm pretty such it doesn't return null, since it does not return there but goes on to call stringToNewUTF8 below. You need to somehow pause/break when the exceptions happens. Try "Pause on exceptions" in devtools.

Then select the emscripten_pc_get_file and type result in the console to see its contents (or inspect it using the Locals pane).

Flyingbeanss commented 1 year ago

Thank you for being so patient.I make a mistake, it's not null. The convertPCtoSourceLocation(pc); will be called many times.After a certain call, it returns undefined.The content is here:

image