ldc icon indicating copy to clipboard operation
ldc copied to clipboard

[WASM] __heap_base is always 0

Open ryuukk opened this issue 2 years ago • 5 comments

Linux ryuukk-pc 5.15.2-2-MANJARO #1 SMP PREEMPT Sat Nov 13 19:25:38 UTC 2021 x86_64 GNU/Linux

LDC - the LLVM D compiler (1.28.0):
  based on DMD v2.098.0 and LLVM 13.0.0
  built with LDC - the LLVM D compiler (1.28.0)
  Default target: x86_64-pc-linux-gnu
  Host CPU: skylake
  http://dlang.org - http://wiki.dlang.org/LDC

clang version 13.0.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

The value of extern(C) byte __heap_base; is always 0, also why is it a byte and not an int?

extern(C) byte __heap_base;

    writeln("address: {}", &__heap_base);
    writeln("value:   {}", __heap_base);
    writeln("ivalue:  {}", *(cast(int*) &__heap_base));

prints:

address: 0x001ea1c4
value:   0
ivalue:  0

And in JS, when loading the wasm file, the value of result.instance.exports.__heap_base is undefined

if i export it: export extern(C) byte __heap_base;, then the value is: 2007448 in JS, so something is wrong somewhere, but iu don't know what

ryuukk avatar Dec 06 '21 19:12 ryuukk

You're defining that symbol (and initializing with 0). Add an extern to reference the linker-generated special symbol. I don't know whether it holds the address as value, or whether its address is the actual heap base - a quick test should suffice to figure that out.

kinke avatar Dec 06 '21 19:12 kinke

You're defining that symbol (and initializing with 0). Add an extern to reference the linker-generated special symbol. I don't know whether it holds the address as value, or whether its address is the actual heap base - a quick test should suffice to figure that out.

that is what i did: extern(C) byte __heap_base; or i'm not sure what do you mean?

ryuukk avatar Dec 06 '21 20:12 ryuukk

extern extern(C) byte __heap_base;

kinke avatar Dec 06 '21 20:12 kinke

i get the same problem, prints 0

maybe that is related: https://stackoverflow.com/questions/58252467/heap-base-seems-to-be-missing-in-clang-9-0-0-is-there-a-replacement

also since we need to compile with: -allow-undefined i'm unable to check wether it reports an issue or not

since we get the adress i guess it is defined, the problem is the 0

ryuukk avatar Dec 06 '21 20:12 ryuukk

Could it be an alignement issue?: https://github.com/tinygo-org/tinygo/issues/2009

according to that issue, the patch will be available for LLVM 13 (ldc uses 12)

    int addr =  cast(int) cast(int*) &__heap_base;
    int alignedFOUR = addr &= -4;

prints: 106976

which is around the value i'd expect

ryuukk avatar Dec 06 '21 20:12 ryuukk