Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add support for snprintf() #216

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

huming2207
Copy link

Hello @vshymanskyy

I've added snprintf() support to format a string and put it into a string buffer, as well as some improvements for supporting %u and %X. However, you may face some strange corruption issue if you call printf() after a snprintf(), but not vice-versa.

Here's a C code demo for WASM:

#include <stdint.h>

#define WASM_EXPORT                 __attribute__((used)) __attribute__((visibility ("default")))
#define WASM_EXPORT_AS(NAME)        WASM_EXPORT __attribute__((export_name(NAME)))
#define WASM_IMPORT(MODULE,NAME)    __attribute__((import_module(MODULE))) __attribute__((import_name(NAME)))

WASM_IMPORT("env","printf")
int printf(const char* fmt, ...);


WASM_IMPORT("env", "snprintf")
int snprintf(char* out_buf, int buf_len, const char *fmt, ...);

WASM_EXPORT_AS("_start")
void run() {
    const char* buff = "print";
    printf("chr: ");
    for (int i=0; i<5; i++) {
        printf("%c,", buff[i]);
    }
    printf("\n");

    printf("int: %d\n", 1234);
    printf("hex: %x\n", 0xABCD);
    
    printf("Message 1: %s %d %u %x %X\n", "test", -123, 123, 0xabcd, 0xABCD);

    char buf[32] = { 0 };
    snprintf(buf, sizeof(buf), "sprintf() Result: %x", 123123);


    printf("Message 2: %s %d %u %x %X\n", "test", -123, 123, 0xabcd, 0xABCD);

    printf("Print snprintf result: %s", buf);
}

The compile command is:

clang --target=wasm32 --no-standard-libraries -fno-builtin-printf -fno-builtin-snprintf -Wl,--no-entry -Wl,--allow-undefined \
    -Wl,--stack-first -Wl,-z,stack-size=5120 \
    -g0 -s -O3 -o test-printf.wasm \

Here's the result on Ubuntu 20.04 with clang-10 from Ubuntu official software repo:

image

As you can see, the printf() works fine until it hits sprintf().

But actually, the snprintf() works fine on ESP32 if there's no printf() after it.

image

I don't know what caused this issue. Maybe I've got a stupid typo somewhere. But anyway, here is the progress for printf family support. You can also have a read for my code if you would like to.

Regards,
Jackson

@huming2207
Copy link
Author

Here's another demo C code for WASM I've done:

int start() {
    printf("Message 1: %s %d %u %x %X\n", "test", -123, 123, 0xabcd, 0xABCD);

    char buf[32] = { 0 };
    snprintf(buf, sizeof(buf), "sprintf() Result: %x", 0xabcdef);

    printf("Print snprintf result: %s", buf);


    return 0;
}

Then I added some logs at my snprintf(), including the fmt, the lengths and their pointers.

I've also tried copying the strings in case the memory gets freed.

Here's a log at my snprintf() and I've got this:

I (1609) printf-jmh: printf: fmt: 0x3f80907c Message 1: %s %d %u %x %X

W (1619) printf-jmh: 0x3f80907c   4d 65 73 73 61 67 65 20  31 3a 20 25 73 20 25 64  |Message 1: %s %d|
W (1629) printf-jmh: 0x3f80908c   20 25 75 20 25 78 20 25  58 0a 00 74 65 73 74 00  | %u %x %X..test.|
W (1639) printf-jmh: 0x3f80909c   73 70 72 69 6e 74 66 28  29 20 52 65 73 75 6c 74  |sprintf() Result|
W (1649) printf-jmh: 0x3f8090ac   3a 20 25 78 00 50 72 69  6e 74 20 73 6e 70 72 69  |: %x.Print snpri|
W (1659) printf-jmh: 0x3f8090bc   6e 74 66 20 72 65 73 75  6c 74 3a 20 25 73 00 00  |ntf result: %s..|
W (1669) printf-jmh: 0x3f8090cc   00 00 00 00                                       |....|
Message 1: test -123 123 abcd ABCD
I (1679) printf-jmh: on start: fmt: sprintf() Result: %x 0x3f80909c
I (1679) printf-jmh: on start: buf len: 32; ; 0x3f80905c
I (1689) printf-jmh: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
I (1699) printf-jmh: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
I (1699) printf-jmh: on end: fmt: sprintf() Result: %x 0x3f80909c, len left: 8
I (1709) printf-jmh: printf: fmt: 0x3f8090b1 Print snprintf result: %s
W (1719) printf-jmh: 0x3f8090b1   50 72 69 6e 74 20 73 6e  70 72 69 6e 74 66 20 72  |Print snprintf r|
W (1729) printf-jmh: 0x3f8090c1   65 73 75 6c 74 3a 20 25  73 00 00 00 00 00 00 00  |esult: %s.......|
W (1739) printf-jmh: 0x3f8090d1   00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
W (1749) printf-jmh: 0x3f8090e1   00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
W (1759) printf-jmh: 0x3f8090f1   00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
W (1769) printf-jmh: 0x3f809101   00 00 00 00                                       |....|
Print snprintf result: sprintf() Result: abcdefI (1779) main: start() executed

So that shows the snprintf() is working...

However, if I remove the logs, garbage contents will show up...It's so weird....

source/m3_api_libc.c Outdated Show resolved Hide resolved
source/m3_api_libc.c Outdated Show resolved Hide resolved
source/m3_api_libc.c Outdated Show resolved Hide resolved
source/m3_api_libc.c Outdated Show resolved Hide resolved
source/m3_api_libc.c Outdated Show resolved Hide resolved
@huming2207 huming2207 mentioned this pull request Mar 21, 2021
@huming2207
Copy link
Author

I've just realised if I compiled the Wasm3 in debug build (with -DCMAKE_BUILD_TYPE=DEBUG) on my host machine, this corruption issue disappeared. I haven't tested on ESP32 for now, will do that soon.

Maybe this can be a compiler toolchain related issue?

My environment details are listed below:

  • Clang for compiling WebAssembly binary: version 10.0.0-4ubuntu1
  • LLD for WASM linker: 10.0.0
  • Host GCC for compiling Wasm3 runtime: 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
  • Host OS: Ubuntu 20.04.2

And my ESP stuff:

  • ESP32's compiler: 8.4.0 (crosstool-NG esp-2020r3)
  • ESP-IDF: latest master

@vshymanskyy Any ideas?

@huming2207
Copy link
Author

huming2207 commented Mar 26, 2021

Here's a Release build with -DDEBUG=1 and all logging enabled explicitly:

  runtime  |  new stack: 0x55f82996dea0
    parse  |  load module: 725 bytes
    parse  |  found magic + version
    parse  |  ** Type [3]
    parse  |      type  0: (i32, i32) -> i32
    parse  |      type  1: (i32, i32, i32, i32) -> i32
    parse  |      type  2: () -> 
    parse  |  ** Import [2]
    parse  |      kind: 0 'env.printf' 
    parse  |      kind: 0 'env.snprintf' 
    parse  |  ** Function [1]
    parse  |   skipped section type: 4
    parse  |  ** Memory [1]
    parse  |  ** Global [1]
    parse  |      global: [0] i32 mutable: 1
  compile  |     0 | 0x41  .. i32.const
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
    parse  |  ** Export [2]
    parse  |      index:   0; kind: 2; export: 'memory'; 
    parse  |      index:   2; kind: 0; export: '_start'; 
    parse  |  ** Code [1]
    parse  |      code size: 467 
    parse  |         1 locals; type: 'i32'
    parse  |  ** Data [1]
  compile  |     0 | 0x41  .. i32.const
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
    parse  |      segment [0]  memory: 0;  expr-size: 4;  size: 136
  runtime  |  resized old: 0x55f82997f790; mem: 0x55f82997f790; length: 65536; pages: 1
  runtime  |  initializing global: 0
  runtime  |  new page: 0x55f82998f7c8; seq: 1; bytes: 4096; lines: 509
     emit  |  acquire page: 1
  compile  |     0 | 0x41  .. i32.const
     emit  |  0x55f82998f7c8: i32.const
    stack  |                                                                         i0 
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
     emit  |  0x55f82998f7e0: return
  compile  |  unwound stack top: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x7ffd1e2a9fa0; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f82998f7c8 |            i32.const  
     code  |  0x55f82998f7d0 | 0x1400
     code  |  0x55f82998f7d8 | (nil)
     code  |  0x55f82998f7e0 |               return  
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 505
     emit  |  acquire page: 1
  compile  |     0 | 0x41  .. i32.const
     emit  |  0x55f82998f7c8: i32.const
    stack  |                                                                         i0 
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
     emit  |  0x55f82998f7e0: return
  compile  |  unwound stack top: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x7ffd1e2a9f80; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f82998f7c8 |            i32.const  
     code  |  0x55f82998f7d0 | 0x1400
     code  |  0x55f82998f7d8 | (nil)
     code  |  0x55f82998f7e0 |               return  
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 505
  runtime  |  loading data segment: 0; size: 136; offset: 5120
     emit  |  acquire page: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x55f82996b380; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f82998f7c8 | 0x55f827e4d300
     code  |  0x55f82998f7d0 | 0x55f827e49e20
     code  |  0x55f82998f7d8 | 0x55f82997e6d0
     code  |  0x55f82998f7e0 | (nil)
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 505
     emit  |  acquire page: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x55f82996b380; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f82998f7c8 | 0x55f827e4d300
     code  |  0x55f82998f7d0 | 0x55f827e49e20
     code  |  0x55f82998f7d8 | 0x55f82997e6d0
     code  |  0x55f82998f7e0 | (nil)
     code  |  0x55f82998f7e8 | 0x55f827e4d300
     code  |  0x55f82998f7f0 | 0x55f827e49610
     code  |  0x55f82998f7f8 | 0x55f82997e748
     code  |  0x55f82998f800 | (nil)
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 501
  compile  |  compiling: '_start'; wasm-size: 467; numArgs: 0; return: nil
     emit  |  acquire page: 1
  compile  |  pushing locals. count: 1; type: i32
    stack  |                                                                         i0 
  compile  |  estimated constant slots: 60
  compile  |  start stack index: 1
     emit  |  0x55f82998f808: Entry
  compile  |     0 | 0x23  .. global.get
     emit  |  0x55f82998f818: GetGlobal_s32
    stack  |                                                                         i61 
  compile  |     1 | 0x41  .. i32.const
    stack  |                                                                         i61  ic1 
  compile  |       | .......... (const i32 = 240)
  compile  |     2 | 0x6b  .. i32.sub
     emit  |  0x55f82998f830: i32.sub
    stack  |                                                          (r0)           ir0 
  compile  |     3 | 0x22  .. local.tee
     emit  |  0x55f82998f848: SetSlot
  compile  |     4 | 0x24  .. global.set
     emit  |  0x55f82998f858: SetGlobal
  compile  |     5 | 0x41  .. i32.const
    stack  |                                                                         ic2 
  compile  |       | .......... (const i32 = 5120)
  compile  |     6 | 0x41  .. i32.const
    stack  |                                                                         ic2  ic3 
  compile  |       | .......... (const i32 = 0)
  compile  |     7 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998f868: CopySlot_32
     emit  |  0x55f82998f880: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998f898: call
  compile  |     8 | 0x1a  .. drop
    stack  |                                                                        
  compile  |     9 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    10 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic4 
  compile  |       | .......... (const i32 = 112)
  compile  |    11 | 0x36  .. i32.store
  compile  |       | .......... (offset = 192)
     emit  |  0x55f82998f8b0: i32.store
  compile  |    12 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    13 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    14 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic6 
  compile  |       | .......... (const i32 = 192)
  compile  |    15 | 0x6a  .. i32.add
     emit  |  0x55f82998f8d0: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    16 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998f8e8: SetSlot
     emit  |  0x55f82998f8f8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998f910: call
  compile  |    17 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    18 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    19 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic7 
  compile  |       | .......... (const i32 = 114)
  compile  |    20 | 0x36  .. i32.store
  compile  |       | .......... (offset = 176)
     emit  |  0x55f82998f928: i32.store
  compile  |    21 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    22 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    23 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic8 
  compile  |       | .......... (const i32 = 176)
  compile  |    24 | 0x6a  .. i32.add
     emit  |  0x55f82998f948: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    25 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998f960: SetSlot
     emit  |  0x55f82998f970: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998f988: call
  compile  |    26 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    27 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    28 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic9 
  compile  |       | .......... (const i32 = 105)
  compile  |    29 | 0x36  .. i32.store
  compile  |       | .......... (offset = 160)
     emit  |  0x55f82998f9a0: i32.store
  compile  |    30 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    31 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    32 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic10 
  compile  |       | .......... (const i32 = 160)
  compile  |    33 | 0x6a  .. i32.add
     emit  |  0x55f82998f9c0: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    34 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998f9d8: SetSlot
     emit  |  0x55f82998f9e8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fa00: call
  compile  |    35 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    36 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    37 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic11 
  compile  |       | .......... (const i32 = 110)
  compile  |    38 | 0x36  .. i32.store
  compile  |       | .......... (offset = 144)
     emit  |  0x55f82998fa18: i32.store
  compile  |    39 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    40 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    41 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic12 
  compile  |       | .......... (const i32 = 144)
  compile  |    42 | 0x6a  .. i32.add
     emit  |  0x55f82998fa38: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    43 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fa50: SetSlot
     emit  |  0x55f82998fa60: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fa78: call
  compile  |    44 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    45 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    46 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic13 
  compile  |       | .......... (const i32 = 116)
  compile  |    47 | 0x36  .. i32.store
  compile  |       | .......... (offset = 128)
     emit  |  0x55f82998fa90: i32.store
  compile  |    48 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    49 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    50 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic14 
  compile  |       | .......... (const i32 = 128)
  compile  |    51 | 0x6a  .. i32.add
     emit  |  0x55f82998fab0: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    52 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fac8: SetSlot
     emit  |  0x55f82998fad8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998faf0: call
  compile  |    53 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    54 | 0x41  .. i32.const
    stack  |                                                                         ic15 
  compile  |       | .......... (const i32 = 5130)
  compile  |    55 | 0x41  .. i32.const
    stack  |                                                                         ic15  ic3 
  compile  |       | .......... (const i32 = 0)
  compile  |    56 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fb08: CopySlot_32
     emit  |  0x55f82998fb20: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fb38: call
  compile  |    57 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    58 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    59 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic16 
  compile  |       | .......... (const i32 = 1234)
  compile  |    60 | 0x36  .. i32.store
  compile  |       | .......... (offset = 112)
     emit  |  0x55f82998fb50: i32.store
  compile  |    61 | 0x41  .. i32.const
    stack  |                                                                         ic17 
  compile  |       | .......... (const i32 = 5132)
  compile  |    62 | 0x20  .. local.get
    stack  |                                                                         ic17  iL0 
  compile  |    63 | 0x41  .. i32.const
    stack  |                                                                         ic17  iL0  ic4 
  compile  |       | .......... (const i32 = 112)
  compile  |    64 | 0x6a  .. i32.add
     emit  |  0x55f82998fb70: i32.add
    stack  |                                                          (r0)           ic17  ir0 
  compile  |    65 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fb88: SetSlot
     emit  |  0x55f82998fb98: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fbb0: call
  compile  |    66 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    67 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    68 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic18 
  compile  |       | .......... (const i32 = 43981)
  compile  |    69 | 0x36  .. i32.store
  compile  |       | .......... (offset = 96)
     emit  |  0x55f82998fbc8: i32.store
  compile  |    70 | 0x41  .. i32.const
    stack  |                                                                         ic19 
  compile  |       | .......... (const i32 = 5141)
  compile  |    71 | 0x20  .. local.get
    stack  |                                                                         ic19  iL0 
  compile  |    72 | 0x41  .. i32.const
    stack  |                                                                         ic19  iL0  ic20 
  compile  |       | .......... (const i32 = 96)
  compile  |    73 | 0x6a  .. i32.add
     emit  |  0x55f82998fbe8: i32.add
    stack  |                                                          (r0)           ic19  ir0 
  compile  |    74 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fc00: SetSlot
     emit  |  0x55f82998fc10: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fc28: call
  compile  |    75 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    76 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    77 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic22 
  compile  |       | .......... (const i64 = 188896956689357)
  compile  |    78 | 0x37  .. i64.store
  compile  |       | .......... (offset = 76)
     emit  |  0x55f82998fc40: i64.store
  compile  |    79 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    80 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic24 
  compile  |       | .......... (const i64 = 532575944581)
  compile  |    81 | 0x37  .. i64.store
  compile  |       | .......... (offset = 68)
     emit  |  0x55f82998fc60: i64.store
  compile  |    82 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    83 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic21 
  compile  |       | .......... (const i32 = 5177)
  compile  |    84 | 0x36  .. i32.store
  compile  |       | .......... (offset = 64)
     emit  |  0x55f82998fc80: i32.store
  compile  |    85 | 0x41  .. i32.const
    stack  |                                                                         ic26 
  compile  |       | .......... (const i32 = 5150)
  compile  |    86 | 0x20  .. local.get
    stack  |                                                                         ic26  iL0 
  compile  |    87 | 0x41  .. i32.const
    stack  |                                                                         ic26  iL0  ic27 
  compile  |       | .......... (const i32 = 64)
  compile  |    88 | 0x6a  .. i32.add
     emit  |  0x55f82998fca0: i32.add
    stack  |                                                          (r0)           ic26  ir0 
  compile  |    89 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fcb8: SetSlot
     emit  |  0x55f82998fcc8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fce0: call
  compile  |    90 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    91 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    92 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic28 
  compile  |       | .......... (const i32 = 232)
  compile  |    93 | 0x6a  .. i32.add
     emit  |  0x55f82998fcf8: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |    94 | 0x42  .. i64.const
    stack  |                                                          (r0)           ir0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |    95 | 0x37  .. i64.store
  compile  |       | .......... (offset = 0)
     emit  |  0x55f82998fd10: i64.store
  compile  |    96 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    97 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic29 
  compile  |       | .......... (const i32 = 224)
  compile  |    98 | 0x6a  .. i32.add
     emit  |  0x55f82998fd28: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |    99 | 0x42  .. i64.const
    stack  |                                                          (r0)           ir0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |   100 | 0x37  .. i64.store
  compile  |       | .......... (offset = 0)
     emit  |  0x55f82998fd40: i64.store
  compile  |   101 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   102 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic32 
  compile  |       | .......... (const i32 = 123123)
  compile  |   103 | 0x36  .. i32.store
  compile  |       | .......... (offset = 48)
     emit  |  0x55f82998fd58: i32.store
  compile  |   104 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   105 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |   106 | 0x37  .. i64.store
  compile  |       | .......... (offset = 216)
     emit  |  0x55f82998fd78: i64.store
  compile  |   107 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   108 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |   109 | 0x37  .. i64.store
  compile  |       | .......... (offset = 208)
     emit  |  0x55f82998fd98: i64.store
  compile  |   110 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   111 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic33 
  compile  |       | .......... (const i32 = 208)
  compile  |   112 | 0x6a  .. i32.add
     emit  |  0x55f82998fdb8: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |   113 | 0x41  .. i32.const
    stack  |                                                          (r0)           ir0  ic34 
  compile  |       | .......... (const i32 = 32)
  compile  |   114 | 0x41  .. i32.const
    stack  |                                                          (r0)           ir0  ic34  ic35 
  compile  |       | .......... (const i32 = 5182)
  compile  |   115 | 0x20  .. local.get
    stack  |                                                          (r0)           ir0  ic34  ic35  iL0 
  compile  |   116 | 0x41  .. i32.const
    stack  |                                                          (r0)           ir0  ic34  ic35  iL0  ic36 
  compile  |       | .......... (const i32 = 48)
  compile  |   117 | 0x6a  .. i32.add
     emit  |  0x55f82998fdd0: SetSlot
     emit  |  0x55f82998fde0: i32.add
    stack  |                                                          (r0)           i61  ic34  ic35  ir0 
  compile  |   118 | 0x10  .. call
  compile  |       | .......... (func= 'snprintf'; args= 4)
     emit  |  0x55f82998fdf8: SetSlot
     emit  |  0x55f82998fe08: CopySlot_32
     emit  |  0x55f82998fe20: CopySlot_32
     emit  |  0x55f82998fe38: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998fe50: call
  compile  |   119 | 0x1a  .. drop
    stack  |                                                                        
  compile  |   120 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   121 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic22 
  compile  |       | .......... (const i64 = 188896956689357)
  compile  |   122 | 0x37  .. i64.store
  compile  |       | .......... (offset = 28)
     emit  |  0x55f82998fe68: i64.store
  compile  |   123 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   124 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic24 
  compile  |       | .......... (const i64 = 532575944581)
  compile  |   125 | 0x37  .. i64.store
  compile  |       | .......... (offset = 20)
     emit  |  0x55f82998fe88: i64.store
  compile  |   126 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   127 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic21 
  compile  |       | .......... (const i32 = 5177)
  compile  |   128 | 0x36  .. i32.store
  compile  |       | .......... (offset = 16)
     emit  |  0x55f82998fea8: i32.store
  compile  |   129 | 0x41  .. i32.const
    stack  |                                                                         ic37 
  compile  |       | .......... (const i32 = 5203)
  compile  |   130 | 0x20  .. local.get
    stack  |                                                                         ic37  iL0 
  compile  |   131 | 0x41  .. i32.const
    stack  |                                                                         ic37  iL0  ic38 
  compile  |       | .......... (const i32 = 16)
  compile  |   132 | 0x6a  .. i32.add
     emit  |  0x55f82998fec8: i32.add
    stack  |                                                          (r0)           ic37  ir0 
  compile  |   133 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998fee0: SetSlot
     emit  |  0x55f82998fef0: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998ff08: call
  compile  |   134 | 0x1a  .. drop
    stack  |                                                                        
  compile  |   135 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   136 | 0x20  .. local.get
    stack  |                                                                         iL0  iL0 
  compile  |   137 | 0x41  .. i32.const
    stack  |                                                                         iL0  iL0  ic33 
  compile  |       | .......... (const i32 = 208)
  compile  |   138 | 0x6a  .. i32.add
     emit  |  0x55f82998ff20: i32.add
    stack  |                                                          (r0)           iL0  ir0 
  compile  |   139 | 0x36  .. i32.store
  compile  |       | .......... (offset = 0)
     emit  |  0x55f82998ff38: i32.store
  compile  |   140 | 0x41  .. i32.const
    stack  |                                                                         ic39 
  compile  |       | .......... (const i32 = 5230)
  compile  |   141 | 0x20  .. local.get
    stack  |                                                                         ic39  iL0 
  compile  |   142 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f82998ff50: CopySlot_32
     emit  |  0x55f82998ff68: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f82998ff80: call
  compile  |   143 | 0x1a  .. drop
    stack  |                                                                        
  compile  |   144 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   145 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic1 
  compile  |       | .......... (const i32 = 240)
  compile  |   146 | 0x6a  .. i32.add
     emit  |  0x55f82998ff98: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |   147 | 0x24  .. global.set
     emit  |  0x55f82998ffb0: SetGlobal
  compile  |   148 | 0x0b   end
     emit  |  0x55f82998ffc0: return
  compile  |  unique constant slots: 39; unused slots: 21
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x55f82996b380; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f82998f7c8 | 0x55f827e4d300
     code  |  0x55f82998f7d0 | 0x55f827e49e20
     code  |  0x55f82998f7d8 | 0x55f82997e6d0
     code  |  0x55f82998f7e0 | (nil)
     code  |  0x55f82998f7e8 | 0x55f827e4d300
     code  |  0x55f82998f7f0 | 0x55f827e49610
     code  |  0x55f82998f7f8 | 0x55f82997e748
     code  |  0x55f82998f800 | (nil)
     code  |  0x55f82998f808 |                Entry  
     code  |  0x55f82998f810 | 0x55f82997e7c0
     code  |  0x55f82998f818 |        GetGlobal_s32  
     code  |  0x55f82998f820 | 0x55f82997e850
     code  |  0x55f82998f828 | 0x3d
     code  |  0x55f82998f830 |              i32.sub  
     code  |  0x55f82998f838 | 0x1
     code  |  0x55f82998f840 | 0x3d
     code  |  0x55f82998f848 |              SetSlot  
     code  |  0x55f82998f850 | (nil)
     code  |  0x55f82998f858 |            SetGlobal  
     code  |  0x55f82998f860 | 0x55f82997e850
     code  |  0x55f82998f868 |          CopySlot_32  
     code  |  0x55f82998f870 | 0x40
     code  |  0x55f82998f878 | 0x3
     code  |  0x55f82998f880 |          CopySlot_32  
     code  |  0x55f82998f888 | 0x3e
     code  |  0x55f82998f890 | 0x2
     code  |  0x55f82998f898 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998f8b0 |            i32.store  
     code  |  0x55f82998f8b8 | 0x4
     code  |  0x55f82998f8c0 | (nil)
     code  |  0x55f82998f8c8 | 0xc0
     code  |  0x55f82998f8d0 |              i32.add  
     code  |  0x55f82998f8d8 | 0x6
     code  |  0x55f82998f8e0 | (nil)
     code  |  0x55f82998f8e8 |              SetSlot  
     code  |  0x55f82998f8f0 | 0x40
     code  |  0x55f82998f8f8 |          CopySlot_32  
     code  |  0x55f82998f900 | 0x3e
     code  |  0x55f82998f908 | 0x5
     code  |  0x55f82998f910 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998f928 |            i32.store  
     code  |  0x55f82998f930 | 0x7
     code  |  0x55f82998f938 | (nil)
     code  |  0x55f82998f940 | 0xb0
     code  |  0x55f82998f948 |              i32.add  
     code  |  0x55f82998f950 | 0x8
     code  |  0x55f82998f958 | (nil)
     code  |  0x55f82998f960 |              SetSlot  
     code  |  0x55f82998f968 | 0x40
     code  |  0x55f82998f970 |          CopySlot_32  
     code  |  0x55f82998f978 | 0x3e
     code  |  0x55f82998f980 | 0x5
     code  |  0x55f82998f988 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998f9a0 |            i32.store  
     code  |  0x55f82998f9a8 | 0x9
     code  |  0x55f82998f9b0 | (nil)
     code  |  0x55f82998f9b8 | 0xa0
     code  |  0x55f82998f9c0 |              i32.add  
     code  |  0x55f82998f9c8 | 0xa
     code  |  0x55f82998f9d0 | (nil)
     code  |  0x55f82998f9d8 |              SetSlot  
     code  |  0x55f82998f9e0 | 0x40
     code  |  0x55f82998f9e8 |          CopySlot_32  
     code  |  0x55f82998f9f0 | 0x3e
     code  |  0x55f82998f9f8 | 0x5
     code  |  0x55f82998fa00 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fa18 |            i32.store  
     code  |  0x55f82998fa20 | 0xb
     code  |  0x55f82998fa28 | (nil)
     code  |  0x55f82998fa30 | 0x90
     code  |  0x55f82998fa38 |              i32.add  
     code  |  0x55f82998fa40 | 0xc
     code  |  0x55f82998fa48 | (nil)
     code  |  0x55f82998fa50 |              SetSlot  
     code  |  0x55f82998fa58 | 0x40
     code  |  0x55f82998fa60 |          CopySlot_32  
     code  |  0x55f82998fa68 | 0x3e
     code  |  0x55f82998fa70 | 0x5
     code  |  0x55f82998fa78 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fa90 |            i32.store  
     code  |  0x55f82998fa98 | 0xd
     code  |  0x55f82998faa0 | (nil)
     code  |  0x55f82998faa8 | 0x80
     code  |  0x55f82998fab0 |              i32.add  
     code  |  0x55f82998fab8 | 0xe
     code  |  0x55f82998fac0 | (nil)
     code  |  0x55f82998fac8 |              SetSlot  
     code  |  0x55f82998fad0 | 0x40
     code  |  0x55f82998fad8 |          CopySlot_32  
     code  |  0x55f82998fae0 | 0x3e
     code  |  0x55f82998fae8 | 0x5
     code  |  0x55f82998faf0 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fb08 |          CopySlot_32  
     code  |  0x55f82998fb10 | 0x40
     code  |  0x55f82998fb18 | 0x3
     code  |  0x55f82998fb20 |          CopySlot_32  
     code  |  0x55f82998fb28 | 0x3e
     code  |  0x55f82998fb30 | 0xf
     code  |  0x55f82998fb38 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fb50 |            i32.store  
     code  |  0x55f82998fb58 | 0x10
     code  |  0x55f82998fb60 | (nil)
     code  |  0x55f82998fb68 | 0x70
     code  |  0x55f82998fb70 |              i32.add  
     code  |  0x55f82998fb78 | 0x4
     code  |  0x55f82998fb80 | (nil)
     code  |  0x55f82998fb88 |              SetSlot  
     code  |  0x55f82998fb90 | 0x40
     code  |  0x55f82998fb98 |          CopySlot_32  
     code  |  0x55f82998fba0 | 0x3e
     code  |  0x55f82998fba8 | 0x11
     code  |  0x55f82998fbb0 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fbc8 |            i32.store  
     code  |  0x55f82998fbd0 | 0x12
     code  |  0x55f82998fbd8 | (nil)
     code  |  0x55f82998fbe0 | 0x60
     code  |  0x55f82998fbe8 |              i32.add  
     code  |  0x55f82998fbf0 | 0x14
     code  |  0x55f82998fbf8 | (nil)
     code  |  0x55f82998fc00 |              SetSlot  
     code  |  0x55f82998fc08 | 0x40
     code  |  0x55f82998fc10 |          CopySlot_32  
     code  |  0x55f82998fc18 | 0x3e
     code  |  0x55f82998fc20 | 0x13
     code  |  0x55f82998fc28 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fc40 |            i64.store  
     code  |  0x55f82998fc48 | 0x16
     code  |  0x55f82998fc50 | (nil)
     code  |  0x55f82998fc58 | 0x4c
     code  |  0x55f82998fc60 |            i64.store  
     code  |  0x55f82998fc68 | 0x18
     code  |  0x55f82998fc70 | (nil)
     code  |  0x55f82998fc78 | 0x44
     code  |  0x55f82998fc80 |            i32.store  
     code  |  0x55f82998fc88 | 0x15
     code  |  0x55f82998fc90 | (nil)
     code  |  0x55f82998fc98 | 0x40
     code  |  0x55f82998fca0 |              i32.add  
     code  |  0x55f82998fca8 | 0x1b
     code  |  0x55f82998fcb0 | (nil)
     code  |  0x55f82998fcb8 |              SetSlot  
     code  |  0x55f82998fcc0 | 0x40
     code  |  0x55f82998fcc8 |          CopySlot_32  
     code  |  0x55f82998fcd0 | 0x3e
     code  |  0x55f82998fcd8 | 0x1a
     code  |  0x55f82998fce0 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998fcf8 |              i32.add  
     code  |  0x55f82998fd00 | 0x1c
     code  |  0x55f82998fd08 | (nil)
     code  |  0x55f82998fd10 |            i64.store  
     code  |  0x55f82998fd18 | 0x1e
     code  |  0x55f82998fd20 | (nil)
     code  |  0x55f82998fd28 |              i32.add  
     code  |  0x55f82998fd30 | 0x1d
     code  |  0x55f82998fd38 | (nil)
     code  |  0x55f82998fd40 |            i64.store  
     code  |  0x55f82998fd48 | 0x1e
     code  |  0x55f82998fd50 | (nil)
     code  |  0x55f82998fd58 |            i32.store  
     code  |  0x55f82998fd60 | 0x20
     code  |  0x55f82998fd68 | (nil)
     code  |  0x55f82998fd70 | 0x30
     code  |  0x55f82998fd78 |            i64.store  
     code  |  0x55f82998fd80 | 0x1e
     code  |  0x55f82998fd88 | (nil)
     code  |  0x55f82998fd90 | 0xd8
     code  |  0x55f82998fd98 |            i64.store  
     code  |  0x55f82998fda0 | 0x1e
     code  |  0x55f82998fda8 | (nil)
     code  |  0x55f82998fdb0 | 0xd0
     code  |  0x55f82998fdb8 |              i32.add  
     code  |  0x55f82998fdc0 | 0x21
     code  |  0x55f82998fdc8 | (nil)
     code  |  0x55f82998fdd0 |              SetSlot  
     code  |  0x55f82998fdd8 | 0x3d
     code  |  0x55f82998fde0 |              i32.add  
     code  |  0x55f82998fde8 | 0x24
     code  |  0x55f82998fdf0 | (nil)
     code  |  0x55f82998fdf8 |              SetSlot  
     code  |  0x55f82998fe00 | 0x44
     code  |  0x55f82998fe08 |          CopySlot_32  
     code  |  0x55f82998fe10 | 0x42
     code  |  0x55f82998fe18 | 0x23
     code  |  0x55f82998fe20 |          CopySlot_32  
     code  |  0x55f82998fe28 | 0x40
     code  |  0x55f82998fe30 | 0x22
     code  |  0x55f82998fe38 |          CopySlot_32  
     code  |  0x55f82998fe40 | 0x3e
     code  |  0x55f82998fe48 | 0x3d
     code  |  0x55f82998fe50 |                 call  0x55f82998f7e8; stack-offset: 62
     code  |  0x55f82998fe68 |            i64.store  
     code  |  0x55f82998fe70 | 0x16
     code  |  0x55f82998fe78 | (nil)
     code  |  0x55f82998fe80 | 0x1c
     code  |  0x55f82998fe88 |            i64.store  
     code  |  0x55f82998fe90 | 0x18
     code  |  0x55f82998fe98 | (nil)
     code  |  0x55f82998fea0 | 0x14
     code  |  0x55f82998fea8 |            i32.store  
     code  |  0x55f82998feb0 | 0x15
     code  |  0x55f82998feb8 | (nil)
     code  |  0x55f82998fec0 | 0x10
     code  |  0x55f82998fec8 |              i32.add  
     code  |  0x55f82998fed0 | 0x26
     code  |  0x55f82998fed8 | (nil)
     code  |  0x55f82998fee0 |              SetSlot  
     code  |  0x55f82998fee8 | 0x40
     code  |  0x55f82998fef0 |          CopySlot_32  
     code  |  0x55f82998fef8 | 0x3e
     code  |  0x55f82998ff00 | 0x25
     code  |  0x55f82998ff08 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998ff20 |              i32.add  
     code  |  0x55f82998ff28 | 0x21
     code  |  0x55f82998ff30 | (nil)
     code  |  0x55f82998ff38 |            i32.store  
     code  |  0x55f82998ff40 | (nil)
     code  |  0x55f82998ff48 | (nil)
     code  |  0x55f82998ff50 |          CopySlot_32  
     code  |  0x55f82998ff58 | 0x40
     code  |  0x55f82998ff60 | (nil)
     code  |  0x55f82998ff68 |          CopySlot_32  
     code  |  0x55f82998ff70 | 0x3e
     code  |  0x55f82998ff78 | 0x27
     code  |  0x55f82998ff80 |                 call  0x55f82998f7c8; stack-offset: 62
     code  |  0x55f82998ff98 |              i32.add  
     code  |  0x55f82998ffa0 | 0x1
     code  |  0x55f82998ffa8 | (nil)
     code  |  0x55f82998ffb0 |            SetGlobal  
     code  |  0x55f82998ffb8 | 0x55f82997e850
     code  |  0x55f82998ffc0 |               return  
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 253
chr: p,r,i,n,t,
int: 1234
hex: abcd
Message 1: test -123 123 abcd ABCD
Message 2: D1e0f3 -123 123 abcd ABCD
Native stack used: 16
Print snprintf result: sABCDpABCDrABCDiABCDnABCDtABCDfABCD(ABCD)ABCD ABCDRABCDeABCDsABCDuABCDlABCDtABCD:ABCD ABCD1e0f3   module  |  freeing module: .unnamed (funcs: 3; segments: 1)
  runtime  |  freeing 1 pages from environment
     code  |  free page: 1; 0x55f82998f7b0; util: 0.0%

@huming2207
Copy link
Author

huming2207 commented Mar 26, 2021

And here's a Debug build's log, where you can see the snprintf() follows by printf() prints the correct result:

  runtime  |  new stack: 0x55f3cc910ea0
    parse  |  load module: 725 bytes
    parse  |  found magic + version
    parse  |  ** Type [3]
    parse  |      type  0: (i32, i32) -> i32
    parse  |      type  1: (i32, i32, i32, i32) -> i32
    parse  |      type  2: () -> 
    parse  |  ** Import [2]
    parse  |      kind: 0 'env.printf' 
    parse  |      kind: 0 'env.snprintf' 
    parse  |  ** Function [1]
    parse  |   skipped section type: 4
    parse  |  ** Memory [1]
    parse  |  ** Global [1]
    parse  |      global: [0] i32 mutable: 1
  compile  |     0 | 0x41  .. i32.const
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
    parse  |  ** Export [2]
    parse  |      index:   0; kind: 2; export: 'memory'; 
    parse  |      index:   2; kind: 0; export: '_start'; 
    parse  |  ** Code [1]
    parse  |      code size: 467 
    parse  |         1 locals; type: 'i32'
    parse  |  ** Data [1]
  compile  |     0 | 0x41  .. i32.const
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
    parse  |      segment [0]  memory: 0;  expr-size: 4;  size: 136
  runtime  |  resized old: 0x55f3cc922790; mem: 0x55f3cc922790; length: 65536; pages: 1
  runtime  |  initializing global: 0
  runtime  |  new page: 0x55f3cc9327c8; seq: 1; bytes: 4096; lines: 509
     emit  |  acquire page: 1
  compile  |     0 | 0x41  .. i32.const
     emit  |  0x55f3cc9327c8: i32.const
    stack  |                                                                         i0 
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
     emit  |  0x55f3cc9327e0: return
  compile  |  unwound stack top: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x7ffef6cd5ac0; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f3cc9327c8 |            i32.const  
     code  |  0x55f3cc9327d0 | 0x1400
     code  |  0x55f3cc9327d8 | (nil)
     code  |  0x55f3cc9327e0 |               return  
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 505
     emit  |  acquire page: 1
  compile  |     0 | 0x41  .. i32.const
     emit  |  0x55f3cc9327c8: i32.const
    stack  |                                                                         i0 
  compile  |       | .......... (const i32 = 5120)
  compile  |     1 | 0x0b   end
     emit  |  0x55f3cc9327e0: return
  compile  |  unwound stack top: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x7ffef6cd5ac0; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f3cc9327c8 |            i32.const  
     code  |  0x55f3cc9327d0 | 0x1400
     code  |  0x55f3cc9327d8 | (nil)
     code  |  0x55f3cc9327e0 |               return  
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 505
  runtime  |  loading data segment: 0; size: 136; offset: 5120
     emit  |  acquire page: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x55f3cc90e380; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f3cc9327c8 | 0x55f3cc684216
     code  |  0x55f3cc9327d0 | 0x55f3cc680200
     code  |  0x55f3cc9327d8 | 0x55f3cc9216d0
     code  |  0x55f3cc9327e0 | (nil)
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 505
     emit  |  acquire page: 1
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x55f3cc90e380; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f3cc9327c8 | 0x55f3cc684216
     code  |  0x55f3cc9327d0 | 0x55f3cc680200
     code  |  0x55f3cc9327d8 | 0x55f3cc9216d0
     code  |  0x55f3cc9327e0 | (nil)
     code  |  0x55f3cc9327e8 | 0x55f3cc684216
     code  |  0x55f3cc9327f0 | 0x55f3cc67fd2d
     code  |  0x55f3cc9327f8 | 0x55f3cc921748
     code  |  0x55f3cc932800 | (nil)
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 501
  compile  |  compiling: '_start'; wasm-size: 467; numArgs: 0; return: nil
     emit  |  acquire page: 1
  compile  |  pushing locals. count: 1; type: i32
    stack  |                                                                         i0 
  compile  |  estimated constant slots: 60
  compile  |  start stack index: 1
     emit  |  0x55f3cc932808: Entry
  compile  |     0 | 0x23  .. global.get
     emit  |  0x55f3cc932818: GetGlobal_s32
    stack  |                                                                         i61 
  compile  |     1 | 0x41  .. i32.const
    stack  |                                                                         i61  ic1 
  compile  |       | .......... (const i32 = 240)
  compile  |     2 | 0x6b  .. i32.sub
     emit  |  0x55f3cc932830: i32.sub
    stack  |                                                          (r0)           ir0 
  compile  |     3 | 0x22  .. local.tee
     emit  |  0x55f3cc932848: SetSlot
  compile  |     4 | 0x24  .. global.set
     emit  |  0x55f3cc932858: SetGlobal
  compile  |     5 | 0x41  .. i32.const
    stack  |                                                                         ic2 
  compile  |       | .......... (const i32 = 5120)
  compile  |     6 | 0x41  .. i32.const
    stack  |                                                                         ic2  ic3 
  compile  |       | .......... (const i32 = 0)
  compile  |     7 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932868: CopySlot_32
     emit  |  0x55f3cc932880: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932898: call
  compile  |     8 | 0x1a  .. drop
    stack  |                                                                        
  compile  |     9 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    10 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic4 
  compile  |       | .......... (const i32 = 112)
  compile  |    11 | 0x36  .. i32.store
  compile  |       | .......... (offset = 192)
     emit  |  0x55f3cc9328b0: i32.store
  compile  |    12 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    13 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    14 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic6 
  compile  |       | .......... (const i32 = 192)
  compile  |    15 | 0x6a  .. i32.add
     emit  |  0x55f3cc9328d0: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    16 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc9328e8: SetSlot
     emit  |  0x55f3cc9328f8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932910: call
  compile  |    17 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    18 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    19 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic7 
  compile  |       | .......... (const i32 = 114)
  compile  |    20 | 0x36  .. i32.store
  compile  |       | .......... (offset = 176)
     emit  |  0x55f3cc932928: i32.store
  compile  |    21 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    22 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    23 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic8 
  compile  |       | .......... (const i32 = 176)
  compile  |    24 | 0x6a  .. i32.add
     emit  |  0x55f3cc932948: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    25 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932960: SetSlot
     emit  |  0x55f3cc932970: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932988: call
  compile  |    26 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    27 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    28 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic9 
  compile  |       | .......... (const i32 = 105)
  compile  |    29 | 0x36  .. i32.store
  compile  |       | .......... (offset = 160)
     emit  |  0x55f3cc9329a0: i32.store
  compile  |    30 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    31 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    32 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic10 
  compile  |       | .......... (const i32 = 160)
  compile  |    33 | 0x6a  .. i32.add
     emit  |  0x55f3cc9329c0: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    34 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc9329d8: SetSlot
     emit  |  0x55f3cc9329e8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932a00: call
  compile  |    35 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    36 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    37 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic11 
  compile  |       | .......... (const i32 = 110)
  compile  |    38 | 0x36  .. i32.store
  compile  |       | .......... (offset = 144)
     emit  |  0x55f3cc932a18: i32.store
  compile  |    39 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    40 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    41 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic12 
  compile  |       | .......... (const i32 = 144)
  compile  |    42 | 0x6a  .. i32.add
     emit  |  0x55f3cc932a38: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    43 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932a50: SetSlot
     emit  |  0x55f3cc932a60: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932a78: call
  compile  |    44 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    45 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    46 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic13 
  compile  |       | .......... (const i32 = 116)
  compile  |    47 | 0x36  .. i32.store
  compile  |       | .......... (offset = 128)
     emit  |  0x55f3cc932a90: i32.store
  compile  |    48 | 0x41  .. i32.const
    stack  |                                                                         ic5 
  compile  |       | .......... (const i32 = 5126)
  compile  |    49 | 0x20  .. local.get
    stack  |                                                                         ic5  iL0 
  compile  |    50 | 0x41  .. i32.const
    stack  |                                                                         ic5  iL0  ic14 
  compile  |       | .......... (const i32 = 128)
  compile  |    51 | 0x6a  .. i32.add
     emit  |  0x55f3cc932ab0: i32.add
    stack  |                                                          (r0)           ic5  ir0 
  compile  |    52 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932ac8: SetSlot
     emit  |  0x55f3cc932ad8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932af0: call
  compile  |    53 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    54 | 0x41  .. i32.const
    stack  |                                                                         ic15 
  compile  |       | .......... (const i32 = 5130)
  compile  |    55 | 0x41  .. i32.const
    stack  |                                                                         ic15  ic3 
  compile  |       | .......... (const i32 = 0)
  compile  |    56 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932b08: CopySlot_32
     emit  |  0x55f3cc932b20: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932b38: call
  compile  |    57 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    58 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    59 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic16 
  compile  |       | .......... (const i32 = 1234)
  compile  |    60 | 0x36  .. i32.store
  compile  |       | .......... (offset = 112)
     emit  |  0x55f3cc932b50: i32.store
  compile  |    61 | 0x41  .. i32.const
    stack  |                                                                         ic17 
  compile  |       | .......... (const i32 = 5132)
  compile  |    62 | 0x20  .. local.get
    stack  |                                                                         ic17  iL0 
  compile  |    63 | 0x41  .. i32.const
    stack  |                                                                         ic17  iL0  ic4 
  compile  |       | .......... (const i32 = 112)
  compile  |    64 | 0x6a  .. i32.add
     emit  |  0x55f3cc932b70: i32.add
    stack  |                                                          (r0)           ic17  ir0 
  compile  |    65 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932b88: SetSlot
     emit  |  0x55f3cc932b98: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932bb0: call
  compile  |    66 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    67 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    68 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic18 
  compile  |       | .......... (const i32 = 43981)
  compile  |    69 | 0x36  .. i32.store
  compile  |       | .......... (offset = 96)
     emit  |  0x55f3cc932bc8: i32.store
  compile  |    70 | 0x41  .. i32.const
    stack  |                                                                         ic19 
  compile  |       | .......... (const i32 = 5141)
  compile  |    71 | 0x20  .. local.get
    stack  |                                                                         ic19  iL0 
  compile  |    72 | 0x41  .. i32.const
    stack  |                                                                         ic19  iL0  ic20 
  compile  |       | .......... (const i32 = 96)
  compile  |    73 | 0x6a  .. i32.add
     emit  |  0x55f3cc932be8: i32.add
    stack  |                                                          (r0)           ic19  ir0 
  compile  |    74 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932c00: SetSlot
     emit  |  0x55f3cc932c10: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932c28: call
  compile  |    75 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    76 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    77 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic22 
  compile  |       | .......... (const i64 = 188896956689357)
  compile  |    78 | 0x37  .. i64.store
  compile  |       | .......... (offset = 76)
     emit  |  0x55f3cc932c40: i64.store
  compile  |    79 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    80 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic24 
  compile  |       | .......... (const i64 = 532575944581)
  compile  |    81 | 0x37  .. i64.store
  compile  |       | .......... (offset = 68)
     emit  |  0x55f3cc932c60: i64.store
  compile  |    82 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    83 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic21 
  compile  |       | .......... (const i32 = 5177)
  compile  |    84 | 0x36  .. i32.store
  compile  |       | .......... (offset = 64)
     emit  |  0x55f3cc932c80: i32.store
  compile  |    85 | 0x41  .. i32.const
    stack  |                                                                         ic26 
  compile  |       | .......... (const i32 = 5150)
  compile  |    86 | 0x20  .. local.get
    stack  |                                                                         ic26  iL0 
  compile  |    87 | 0x41  .. i32.const
    stack  |                                                                         ic26  iL0  ic27 
  compile  |       | .......... (const i32 = 64)
  compile  |    88 | 0x6a  .. i32.add
     emit  |  0x55f3cc932ca0: i32.add
    stack  |                                                          (r0)           ic26  ir0 
  compile  |    89 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932cb8: SetSlot
     emit  |  0x55f3cc932cc8: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932ce0: call
  compile  |    90 | 0x1a  .. drop
    stack  |                                                                        
  compile  |    91 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    92 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic28 
  compile  |       | .......... (const i32 = 232)
  compile  |    93 | 0x6a  .. i32.add
     emit  |  0x55f3cc932cf8: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |    94 | 0x42  .. i64.const
    stack  |                                                          (r0)           ir0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |    95 | 0x37  .. i64.store
  compile  |       | .......... (offset = 0)
     emit  |  0x55f3cc932d10: i64.store
  compile  |    96 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |    97 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic29 
  compile  |       | .......... (const i32 = 224)
  compile  |    98 | 0x6a  .. i32.add
     emit  |  0x55f3cc932d28: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |    99 | 0x42  .. i64.const
    stack  |                                                          (r0)           ir0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |   100 | 0x37  .. i64.store
  compile  |       | .......... (offset = 0)
     emit  |  0x55f3cc932d40: i64.store
  compile  |   101 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   102 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic32 
  compile  |       | .......... (const i32 = 123123)
  compile  |   103 | 0x36  .. i32.store
  compile  |       | .......... (offset = 48)
     emit  |  0x55f3cc932d58: i32.store
  compile  |   104 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   105 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |   106 | 0x37  .. i64.store
  compile  |       | .......... (offset = 216)
     emit  |  0x55f3cc932d78: i64.store
  compile  |   107 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   108 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic30 
  compile  |       | .......... (const i64 = 0)
  compile  |   109 | 0x37  .. i64.store
  compile  |       | .......... (offset = 208)
     emit  |  0x55f3cc932d98: i64.store
  compile  |   110 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   111 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic33 
  compile  |       | .......... (const i32 = 208)
  compile  |   112 | 0x6a  .. i32.add
     emit  |  0x55f3cc932db8: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |   113 | 0x41  .. i32.const
    stack  |                                                          (r0)           ir0  ic34 
  compile  |       | .......... (const i32 = 32)
  compile  |   114 | 0x41  .. i32.const
    stack  |                                                          (r0)           ir0  ic34  ic35 
  compile  |       | .......... (const i32 = 5182)
  compile  |   115 | 0x20  .. local.get
    stack  |                                                          (r0)           ir0  ic34  ic35  iL0 
  compile  |   116 | 0x41  .. i32.const
    stack  |                                                          (r0)           ir0  ic34  ic35  iL0  ic36 
  compile  |       | .......... (const i32 = 48)
  compile  |   117 | 0x6a  .. i32.add
     emit  |  0x55f3cc932dd0: SetSlot
     emit  |  0x55f3cc932de0: i32.add
    stack  |                                                          (r0)           i61  ic34  ic35  ir0 
  compile  |   118 | 0x10  .. call
  compile  |       | .......... (func= 'snprintf'; args= 4)
     emit  |  0x55f3cc932df8: SetSlot
     emit  |  0x55f3cc932e08: CopySlot_32
     emit  |  0x55f3cc932e20: CopySlot_32
     emit  |  0x55f3cc932e38: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932e50: call
  compile  |   119 | 0x1a  .. drop
    stack  |                                                                        
  compile  |   120 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   121 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic22 
  compile  |       | .......... (const i64 = 188896956689357)
  compile  |   122 | 0x37  .. i64.store
  compile  |       | .......... (offset = 28)
     emit  |  0x55f3cc932e68: i64.store
  compile  |   123 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   124 | 0x42  .. i64.const
    stack  |                                                                         iL0  Ic24 
  compile  |       | .......... (const i64 = 532575944581)
  compile  |   125 | 0x37  .. i64.store
  compile  |       | .......... (offset = 20)
     emit  |  0x55f3cc932e88: i64.store
  compile  |   126 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   127 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic21 
  compile  |       | .......... (const i32 = 5177)
  compile  |   128 | 0x36  .. i32.store
  compile  |       | .......... (offset = 16)
     emit  |  0x55f3cc932ea8: i32.store
  compile  |   129 | 0x41  .. i32.const
    stack  |                                                                         ic37 
  compile  |       | .......... (const i32 = 5203)
  compile  |   130 | 0x20  .. local.get
    stack  |                                                                         ic37  iL0 
  compile  |   131 | 0x41  .. i32.const
    stack  |                                                                         ic37  iL0  ic38 
  compile  |       | .......... (const i32 = 16)
  compile  |   132 | 0x6a  .. i32.add
     emit  |  0x55f3cc932ec8: i32.add
    stack  |                                                          (r0)           ic37  ir0 
  compile  |   133 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932ee0: SetSlot
     emit  |  0x55f3cc932ef0: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932f08: call
  compile  |   134 | 0x1a  .. drop
    stack  |                                                                        
  compile  |   135 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   136 | 0x20  .. local.get
    stack  |                                                                         iL0  iL0 
  compile  |   137 | 0x41  .. i32.const
    stack  |                                                                         iL0  iL0  ic33 
  compile  |       | .......... (const i32 = 208)
  compile  |   138 | 0x6a  .. i32.add
     emit  |  0x55f3cc932f20: i32.add
    stack  |                                                          (r0)           iL0  ir0 
  compile  |   139 | 0x36  .. i32.store
  compile  |       | .......... (offset = 0)
     emit  |  0x55f3cc932f38: i32.store
  compile  |   140 | 0x41  .. i32.const
    stack  |                                                                         ic39 
  compile  |       | .......... (const i32 = 5230)
  compile  |   141 | 0x20  .. local.get
    stack  |                                                                         ic39  iL0 
  compile  |   142 | 0x10  .. call
  compile  |       | .......... (func= 'printf'; args= 2)
     emit  |  0x55f3cc932f50: CopySlot_32
     emit  |  0x55f3cc932f68: CopySlot_32
    stack  |                                                                         i62 
     emit  |  0x55f3cc932f80: call
  compile  |   143 | 0x1a  .. drop
    stack  |                                                                        
  compile  |   144 | 0x20  .. local.get
    stack  |                                                                         iL0 
  compile  |   145 | 0x41  .. i32.const
    stack  |                                                                         iL0  ic1 
  compile  |       | .......... (const i32 = 240)
  compile  |   146 | 0x6a  .. i32.add
     emit  |  0x55f3cc932f98: i32.add
    stack  |                                                          (r0)           ir0 
  compile  |   147 | 0x24  .. global.set
     emit  |  0x55f3cc932fb0: SetGlobal
  compile  |   148 | 0x0b   end
     emit  |  0x55f3cc932fc0: return
  compile  |  unique constant slots: 39; unused slots: 21
     emit  |  release page: 1 to queue: 'open'
  runtime  |  runtime: 0x55f3cc90e380; open-pages: 1; full-pages: 0; active: 0; total: 1
     code  |  code page seq: 1
     code  |  ---------------------------------------------------------------------------------------
     code  |  0x55f3cc9327c8 | 0x55f3cc684216
     code  |  0x55f3cc9327d0 | 0x55f3cc680200
     code  |  0x55f3cc9327d8 | 0x55f3cc9216d0
     code  |  0x55f3cc9327e0 | (nil)
     code  |  0x55f3cc9327e8 | 0x55f3cc684216
     code  |  0x55f3cc9327f0 | 0x55f3cc67fd2d
     code  |  0x55f3cc9327f8 | 0x55f3cc921748
     code  |  0x55f3cc932800 | (nil)
     code  |  0x55f3cc932808 |                Entry  
     code  |  0x55f3cc932810 | 0x55f3cc9217c0
     code  |  0x55f3cc932818 |        GetGlobal_s32  
     code  |  0x55f3cc932820 | 0x55f3cc921850
     code  |  0x55f3cc932828 | 0x3d
     code  |  0x55f3cc932830 |              i32.sub  
     code  |  0x55f3cc932838 | 0x1
     code  |  0x55f3cc932840 | 0x3d
     code  |  0x55f3cc932848 |              SetSlot  
     code  |  0x55f3cc932850 | (nil)
     code  |  0x55f3cc932858 |            SetGlobal  
     code  |  0x55f3cc932860 | 0x55f3cc921850
     code  |  0x55f3cc932868 |          CopySlot_32  
     code  |  0x55f3cc932870 | 0x40
     code  |  0x55f3cc932878 | 0x3
     code  |  0x55f3cc932880 |          CopySlot_32  
     code  |  0x55f3cc932888 | 0x3e
     code  |  0x55f3cc932890 | 0x2
     code  |  0x55f3cc932898 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc9328b0 |            i32.store  
     code  |  0x55f3cc9328b8 | 0x4
     code  |  0x55f3cc9328c0 | (nil)
     code  |  0x55f3cc9328c8 | 0xc0
     code  |  0x55f3cc9328d0 |              i32.add  
     code  |  0x55f3cc9328d8 | 0x6
     code  |  0x55f3cc9328e0 | (nil)
     code  |  0x55f3cc9328e8 |              SetSlot  
     code  |  0x55f3cc9328f0 | 0x40
     code  |  0x55f3cc9328f8 |          CopySlot_32  
     code  |  0x55f3cc932900 | 0x3e
     code  |  0x55f3cc932908 | 0x5
     code  |  0x55f3cc932910 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932928 |            i32.store  
     code  |  0x55f3cc932930 | 0x7
     code  |  0x55f3cc932938 | (nil)
     code  |  0x55f3cc932940 | 0xb0
     code  |  0x55f3cc932948 |              i32.add  
     code  |  0x55f3cc932950 | 0x8
     code  |  0x55f3cc932958 | (nil)
     code  |  0x55f3cc932960 |              SetSlot  
     code  |  0x55f3cc932968 | 0x40
     code  |  0x55f3cc932970 |          CopySlot_32  
     code  |  0x55f3cc932978 | 0x3e
     code  |  0x55f3cc932980 | 0x5
     code  |  0x55f3cc932988 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc9329a0 |            i32.store  
     code  |  0x55f3cc9329a8 | 0x9
     code  |  0x55f3cc9329b0 | (nil)
     code  |  0x55f3cc9329b8 | 0xa0
     code  |  0x55f3cc9329c0 |              i32.add  
     code  |  0x55f3cc9329c8 | 0xa
     code  |  0x55f3cc9329d0 | (nil)
     code  |  0x55f3cc9329d8 |              SetSlot  
     code  |  0x55f3cc9329e0 | 0x40
     code  |  0x55f3cc9329e8 |          CopySlot_32  
     code  |  0x55f3cc9329f0 | 0x3e
     code  |  0x55f3cc9329f8 | 0x5
     code  |  0x55f3cc932a00 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932a18 |            i32.store  
     code  |  0x55f3cc932a20 | 0xb
     code  |  0x55f3cc932a28 | (nil)
     code  |  0x55f3cc932a30 | 0x90
     code  |  0x55f3cc932a38 |              i32.add  
     code  |  0x55f3cc932a40 | 0xc
     code  |  0x55f3cc932a48 | (nil)
     code  |  0x55f3cc932a50 |              SetSlot  
     code  |  0x55f3cc932a58 | 0x40
     code  |  0x55f3cc932a60 |          CopySlot_32  
     code  |  0x55f3cc932a68 | 0x3e
     code  |  0x55f3cc932a70 | 0x5
     code  |  0x55f3cc932a78 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932a90 |            i32.store  
     code  |  0x55f3cc932a98 | 0xd
     code  |  0x55f3cc932aa0 | (nil)
     code  |  0x55f3cc932aa8 | 0x80
     code  |  0x55f3cc932ab0 |              i32.add  
     code  |  0x55f3cc932ab8 | 0xe
     code  |  0x55f3cc932ac0 | (nil)
     code  |  0x55f3cc932ac8 |              SetSlot  
     code  |  0x55f3cc932ad0 | 0x40
     code  |  0x55f3cc932ad8 |          CopySlot_32  
     code  |  0x55f3cc932ae0 | 0x3e
     code  |  0x55f3cc932ae8 | 0x5
     code  |  0x55f3cc932af0 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932b08 |          CopySlot_32  
     code  |  0x55f3cc932b10 | 0x40
     code  |  0x55f3cc932b18 | 0x3
     code  |  0x55f3cc932b20 |          CopySlot_32  
     code  |  0x55f3cc932b28 | 0x3e
     code  |  0x55f3cc932b30 | 0xf
     code  |  0x55f3cc932b38 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932b50 |            i32.store  
     code  |  0x55f3cc932b58 | 0x10
     code  |  0x55f3cc932b60 | (nil)
     code  |  0x55f3cc932b68 | 0x70
     code  |  0x55f3cc932b70 |              i32.add  
     code  |  0x55f3cc932b78 | 0x4
     code  |  0x55f3cc932b80 | (nil)
     code  |  0x55f3cc932b88 |              SetSlot  
     code  |  0x55f3cc932b90 | 0x40
     code  |  0x55f3cc932b98 |          CopySlot_32  
     code  |  0x55f3cc932ba0 | 0x3e
     code  |  0x55f3cc932ba8 | 0x11
     code  |  0x55f3cc932bb0 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932bc8 |            i32.store  
     code  |  0x55f3cc932bd0 | 0x12
     code  |  0x55f3cc932bd8 | (nil)
     code  |  0x55f3cc932be0 | 0x60
     code  |  0x55f3cc932be8 |              i32.add  
     code  |  0x55f3cc932bf0 | 0x14
     code  |  0x55f3cc932bf8 | (nil)
     code  |  0x55f3cc932c00 |              SetSlot  
     code  |  0x55f3cc932c08 | 0x40
     code  |  0x55f3cc932c10 |          CopySlot_32  
     code  |  0x55f3cc932c18 | 0x3e
     code  |  0x55f3cc932c20 | 0x13
     code  |  0x55f3cc932c28 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932c40 |            i64.store  
     code  |  0x55f3cc932c48 | 0x16
     code  |  0x55f3cc932c50 | (nil)
     code  |  0x55f3cc932c58 | 0x4c
     code  |  0x55f3cc932c60 |            i64.store  
     code  |  0x55f3cc932c68 | 0x18
     code  |  0x55f3cc932c70 | (nil)
     code  |  0x55f3cc932c78 | 0x44
     code  |  0x55f3cc932c80 |            i32.store  
     code  |  0x55f3cc932c88 | 0x15
     code  |  0x55f3cc932c90 | (nil)
     code  |  0x55f3cc932c98 | 0x40
     code  |  0x55f3cc932ca0 |              i32.add  
     code  |  0x55f3cc932ca8 | 0x1b
     code  |  0x55f3cc932cb0 | (nil)
     code  |  0x55f3cc932cb8 |              SetSlot  
     code  |  0x55f3cc932cc0 | 0x40
     code  |  0x55f3cc932cc8 |          CopySlot_32  
     code  |  0x55f3cc932cd0 | 0x3e
     code  |  0x55f3cc932cd8 | 0x1a
     code  |  0x55f3cc932ce0 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932cf8 |              i32.add  
     code  |  0x55f3cc932d00 | 0x1c
     code  |  0x55f3cc932d08 | (nil)
     code  |  0x55f3cc932d10 |            i64.store  
     code  |  0x55f3cc932d18 | 0x1e
     code  |  0x55f3cc932d20 | (nil)
     code  |  0x55f3cc932d28 |              i32.add  
     code  |  0x55f3cc932d30 | 0x1d
     code  |  0x55f3cc932d38 | (nil)
     code  |  0x55f3cc932d40 |            i64.store  
     code  |  0x55f3cc932d48 | 0x1e
     code  |  0x55f3cc932d50 | (nil)
     code  |  0x55f3cc932d58 |            i32.store  
     code  |  0x55f3cc932d60 | 0x20
     code  |  0x55f3cc932d68 | (nil)
     code  |  0x55f3cc932d70 | 0x30
     code  |  0x55f3cc932d78 |            i64.store  
     code  |  0x55f3cc932d80 | 0x1e
     code  |  0x55f3cc932d88 | (nil)
     code  |  0x55f3cc932d90 | 0xd8
     code  |  0x55f3cc932d98 |            i64.store  
     code  |  0x55f3cc932da0 | 0x1e
     code  |  0x55f3cc932da8 | (nil)
     code  |  0x55f3cc932db0 | 0xd0
     code  |  0x55f3cc932db8 |              i32.add  
     code  |  0x55f3cc932dc0 | 0x21
     code  |  0x55f3cc932dc8 | (nil)
     code  |  0x55f3cc932dd0 |              SetSlot  
     code  |  0x55f3cc932dd8 | 0x3d
     code  |  0x55f3cc932de0 |              i32.add  
     code  |  0x55f3cc932de8 | 0x24
     code  |  0x55f3cc932df0 | (nil)
     code  |  0x55f3cc932df8 |              SetSlot  
     code  |  0x55f3cc932e00 | 0x44
     code  |  0x55f3cc932e08 |          CopySlot_32  
     code  |  0x55f3cc932e10 | 0x42
     code  |  0x55f3cc932e18 | 0x23
     code  |  0x55f3cc932e20 |          CopySlot_32  
     code  |  0x55f3cc932e28 | 0x40
     code  |  0x55f3cc932e30 | 0x22
     code  |  0x55f3cc932e38 |          CopySlot_32  
     code  |  0x55f3cc932e40 | 0x3e
     code  |  0x55f3cc932e48 | 0x3d
     code  |  0x55f3cc932e50 |                 call  0x55f3cc9327e8; stack-offset: 62
     code  |  0x55f3cc932e68 |            i64.store  
     code  |  0x55f3cc932e70 | 0x16
     code  |  0x55f3cc932e78 | (nil)
     code  |  0x55f3cc932e80 | 0x1c
     code  |  0x55f3cc932e88 |            i64.store  
     code  |  0x55f3cc932e90 | 0x18
     code  |  0x55f3cc932e98 | (nil)
     code  |  0x55f3cc932ea0 | 0x14
     code  |  0x55f3cc932ea8 |            i32.store  
     code  |  0x55f3cc932eb0 | 0x15
     code  |  0x55f3cc932eb8 | (nil)
     code  |  0x55f3cc932ec0 | 0x10
     code  |  0x55f3cc932ec8 |              i32.add  
     code  |  0x55f3cc932ed0 | 0x26
     code  |  0x55f3cc932ed8 | (nil)
     code  |  0x55f3cc932ee0 |              SetSlot  
     code  |  0x55f3cc932ee8 | 0x40
     code  |  0x55f3cc932ef0 |          CopySlot_32  
     code  |  0x55f3cc932ef8 | 0x3e
     code  |  0x55f3cc932f00 | 0x25
     code  |  0x55f3cc932f08 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932f20 |              i32.add  
     code  |  0x55f3cc932f28 | 0x21
     code  |  0x55f3cc932f30 | (nil)
     code  |  0x55f3cc932f38 |            i32.store  
     code  |  0x55f3cc932f40 | (nil)
     code  |  0x55f3cc932f48 | (nil)
     code  |  0x55f3cc932f50 |          CopySlot_32  
     code  |  0x55f3cc932f58 | 0x40
     code  |  0x55f3cc932f60 | (nil)
     code  |  0x55f3cc932f68 |          CopySlot_32  
     code  |  0x55f3cc932f70 | 0x3e
     code  |  0x55f3cc932f78 | 0x27
     code  |  0x55f3cc932f80 |                 call  0x55f3cc9327c8; stack-offset: 62
     code  |  0x55f3cc932f98 |              i32.add  
     code  |  0x55f3cc932fa0 | 0x1
     code  |  0x55f3cc932fa8 | (nil)
     code  |  0x55f3cc932fb0 |            SetGlobal  
     code  |  0x55f3cc932fb8 | 0x55f3cc921850
     code  |  0x55f3cc932fc0 |               return  
     code  |  ---------------------------------------------------------------------------------------
     code  |  free-lines: 253
chr: p,r,i,n,t,
int: 1234
hex: abcd
Message 1: test -123 123 abcd ABCD
Message 2: test -123 123 abcd ABCD
Native stack used: 7552
Print snprintf result: sprintf() Result: 1e0f3   module  |  freeing module: .unnamed (funcs: 3; segments: 1)
  runtime  |  freeing 1 pages from environment
     code  |  free page: 1; 0x55f3cc9327b0; util: 0.0%

Again, here's what the code be like in WASM binary:

```c
#include <stdint.h>

#define WASM_EXPORT                 __attribute__((used)) __attribute__((visibility ("default")))
#define WASM_EXPORT_AS(NAME)        WASM_EXPORT __attribute__((export_name(NAME)))
#define WASM_IMPORT(MODULE,NAME)    __attribute__((import_module(MODULE))) __attribute__((import_name(NAME)))

WASM_IMPORT("env","printf")
int printf(const char* fmt, ...);


WASM_IMPORT("env", "snprintf")
int snprintf(char* out_buf, int buf_len, const char *fmt, ...);

WASM_EXPORT_AS("_start")
void run() {
    const char* buff = "print";
    printf("chr: ");
    for (int i=0; i<5; i++) {
        printf("%c,", buff[i]);
    }
    printf("\n");

    printf("int: %d\n", 1234);
    printf("hex: %x\n", 0xABCD);
    
    printf("Message 1: %s %d %u %x %X\n", "test", -123, 123, 0xabcd, 0xABCD);

    char buf[32] = { 0 };
    snprintf(buf, sizeof(buf), "sprintf() Result: %x", 123123);


    printf("Message 2: %s %d %u %x %X\n", "test", -123, 123, 0xabcd, 0xABCD);

    printf("Print snprintf result: %s", buf);
}


</pre>
</details>

@huming2207
Copy link
Author

Hello again @vshymanskyy , one of your recent commits (52f80e9) reminds me that some of the GCC optimisation flags may cause some troubles. So I had a try and eventually, I realised by removing the -fno-stack-protector, this problem will be solved!

@vshymanskyy
Copy link
Member

@huming2207 should I merge this?

@huming2207
Copy link
Author

@huming2207 should I merge this?

So far I think this PR works fine with GCC stack protector on. I think something is wrong with the runtime's stack management. I have no idea for now about this. Could you please also investigate this issue if possible? Thanks!

@vshymanskyy
Copy link
Member

@huming2207 please rebase to the latest version and re-check (with different GCC stack protector settings, if you can).
We have fixed some bugs in the interpreter.

@huming2207
Copy link
Author

@huming2207 please rebase to the latest version and re-check (with different GCC stack protector settings, if you can).
We have fixed some bugs in the interpreter.

Here's a quick test on my desktop with Ubuntu 20.04.1 x64. Looks like I still need to get the -fno-stack-protector removed:

image

Otherwise it will output something like this:

image

@huming2207
Copy link
Author

Here's another test on my M1 Mac with Clang 11.1, macOS 11.2.3, compiled with cmake . -DCMAKE_BUILD_TYPE=Release -DBUILD_NATIVE=OFF (because Clang on Apple Silicon does not support -march=native) and the -fno-stack-protector was kept:

image

Interestingly, even if I remove the -fno-stack-protector it still prints garbage:

image

I've also tried a few more combinations like adding -fstack-protector-all or -fstack-protector-strong or removing the -fno-stack-check etc., no luck at all.

@huming2207
Copy link
Author

@vshymanskyy also I remembered earlier when I was playing with this on ESP32, the third parameter (the format string) can also be corrupted, so does printf() you implemented earlier. So I guess it may not be a va_list internal issue but more likely to be a stack handling issue. But I can't tell for now...I'm still too naive for the WASM world...

@huming2207
Copy link
Author

@vshymanskyy I think I still can't solve this issue, even with your latest commit (till 307265a). Since I'm also busy working on my exams and some work-related stuff, I don't have enough time to take another deeper look. I will close it for now until I have some times later.

@huming2207 huming2207 closed this Apr 15, 2021
@vshymanskyy
Copy link
Member

Please take your time. I think we have better tools to investigate this issue!

@vshymanskyy vshymanskyy reopened this Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants