#Weird clang "bug"

33 messages · Page 1 of 1 (latest)

latent goblet
#

Call it a bug or not, the following behavior is quite surprising to me and I'm not sure to actually understand what's happening:

void exit(int code)
{
    extern void js_exit(int code);
    js_exit(code);
}


int accept(void)
{
    exit(-1);
}

when compiling this file, I get the following error:

clang --target=wasm32 \
                -std=c99 -nostdlib -Wextra -Wall -Werror -Wconversion -funsigned-char -pedantic-errors \
                -finput-charset=utf-8 -fexec-charset=utf-8 \
                -Os -flto \
                -Wl,--no-entry \
                -Wl,--allow-undefined-file=/home/src/js_symbols.syms \
                -Wl,--import-memory \
                -Wl,--export=__heap_base \
                -Wl,--export=accept \
                        /home/src/main.c \
                        /home/src/std/impl.c \
                -o /home/dist/app.wasm
/home/src/main.c:9:1: error: function declared 'noreturn' should not return [-Werror,-Winvalid-noreturn]
    9 | }
      | ^
1 error generated.

I suspect this is because of the special semantic of the exit function. if I rename the symbol to exot , it works fine (I get another compile error about accept not returning an int, which is fine)

where is the problem exactly? what is clang trying to do?

midnight canopyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

latent goblet
#

well, it is part of the symbols left undefned and injected at runtime. it is listed in the file linked here --allow-undefined-file=/home/src/js_symbols.syms

#

I have the same issue with _Exit

latent goblet
gritty vapor
#

try

__attribute__((noreturn)) extern void js_exit(int code);
#

or _Noreturn from C11

latent goblet
#

what does that do? I never tried __attribute__?

gritty vapor
latent goblet
#

(yes it works now)

#

in the assumption I could inline some WASM directly to do this:

void _Exit(int code)
{
    __inline_wasm("unreachable");
}

I guess that "hack" wouldn't work, right?

#

(unreachable is a WASM instruction that makes the program stop)

gritty vapor
#

probably

#

though where is the code going to be shown

latent goblet
#

so essentially what happens is that, as the spec requires, _Exitshould never return. but in my case, it does return a data object of type void

#

which is not correct

#

is that so ?

#
void _Exit(int code)
{
    extern void js_exit(int code);
    js_exit(code);
    for(;;);
}

this works for example

midnight canopyBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

latent goblet
latent goblet
#

niiiiice

#

I love that lang

gritty vapor
#

might as well put the function in the for loop as well

latent goblet
#

indeed

gritty vapor
#

so it just repeatedly called until it quits

latent goblet
#

savage

#

but I like that

#

especially if on the JS side, I alert("Something went terribly wrong. close this tab asap")

#

because it blocks the thread trolol

latent goblet
#
function js_exit(errorCode) {
  const message = `WASM module crashed hard. Error code: ${errorCode}`;
  alert(message, "\nClose this tab asap");
  throw message;
}

what a nice interop