Hello,

I found simple loop(for, while, do-while) asm code generated from cproc/qbe got 
issue, there's segmentation fault

can someone please give me some pointers? i want to push a fix to this part, 
thanks!

example:

`for-loop-test.c`

```sh
$ cat for-loop-test.c

#include<tcclib.h>
int main(){
    for(int i = 0; i < 5; i++)
        printf("i = %d\n", i);
return 0;
}
```

```sh
$ ~/tinycc/tcc -run for-loop-test.c

i = 0
i = 1
i = 2
i = 3
i = 4
```

generate `for-loop-test.i` for `cproc`

```sh
$ ~/tinycc/tcc -Btinycc -Itinycc -E for-loop-test.c -o for-loop-test.i
$ cat for-loop-test.i

# 1 "for-loop-test.c"
# 1 "<command line>" 1
# 1 "for-loop-test.c" 2

# 1 "tinycc/tcclib.h" 1
# 11 "tinycc/tcclib.h"
# 1 "tinycc/include/stddef.h" 1



typedef unsigned long size_t;
typedef long ssize_t;
typedef int wchar_t;
typedef long ptrdiff_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
# 22 "tinycc/include/stddef.h"
void *alloca(size_t size);
# 11 "tinycc/tcclib.h" 2

# 1 "tinycc/include/stdarg.h" 1



typedef __builtin_va_list va_list;






typedef va_list __gnuc_va_list;
# 12 "tinycc/tcclib.h" 2


void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
int atoi(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
unsigned long int strtoul(const char *nptr, char **endptr, int base);
void exit(int);


typedef struct __FILE FILE;

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const  char *path, const char *mode, FILE *stream);
int fclose(FILE *stream);
size_t  fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t  fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);
int fflush(FILE *stream);
int putchar (int c);

int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const  char  *format, ...);
int asprintf(char **strp, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int vprintf(const char *format, va_list ap);
int vfprintf(FILE  *stream,  const  char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char  *format, va_list ap);
int vasprintf(char  **strp,  const  char *format, va_list ap);
int vdprintf(int fd, const char *format, va_list ap);

void perror(const char *s);


char *strcat(char *dest, const char *src);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
char *strcpy(char *dest, const char *src);
void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
char *strdup(const char *s);
size_t strlen(const char *s);






void *dlopen(const char *filename, int flag);
const char *dlerror(void);
void *dlsym(void *handle, char *symbol);
int dlclose(void *handle);
# 2 "for-loop-test.c" 2
int main(){
    for(int i = 0; i < 5; i++)
        printf("i = %d\n", i);
return 0;
}
```

generate qbe IR code `for-loop-test.qbe`

```sh
$ ~/cproc/cproc-qbe for-loop-test.i > for-loop-test.qbe
$ cat for-loop-test.qbe

data $.Lstring.2 = align 1 { b "i = %d\012\000", }
export
function w $main() {
@start.1
        %.1 =l alloc4 4
@body.2
        storew 0, %.1
@for_cond.3
        %.2 =w loadw %.1
        %.3 =w csltw %.2, 5
        jnz %.3, @for_body.4, @for_join.6
@for_body.4
        %.4 =w loadw %.1
        %.5 =w call $printf(l $.Lstring.2, ..., w %.4)
@for_cont.5
        %.6 =w loadw %.1
        %.7 =w add %.6, 1
        storew %.7, %.1
        jmp @for_cond.3
@for_join.6
        ret 0
}
```
generate x86_64 assembly `for-loop-test.s`

```sh
$ ~/qbe/qbe for-loop-test.qbe > for-loop-test.s
$ cat for-loop-test.s

.data
.balign 1
.Lstring.2:
        .ascii "i = %d\012\000"
/* end data */

.text
.balign 16
.globl main
main:
        endbr64
        pushq %rbp
        movq %rsp, %rbp
        subq $8, %rsp
        pushq %rbx
        movl $0, %ebx
.p2align 4
.Lbb2:
        cmpl $5, %ebx
        jge .Lbb4
        movl %ebx, %esi
        leaq .Lstring.2(%rip), %rdi
        movl $0, %eax
        callq printf
        addl $1, %ebx
        jmp .Lbb2
.Lbb4:
        movl $0, %eax
        popq %rbx
        leave
        ret
.type main, @function
.size main, .-main
/* end function main */

.section .note.GNU-stack,"",@progbits
```

now compile and run `for-loop-test`

```sh
$ ~/tinycc/tcc for-loop-test.s -o for-loop-test
$ ./for-loop-test
Segmentation fault (core dumped)
```

same asm code can be compiled and run by gcc with no issue:

```sh
$ gcc for-loop-test.s -o for-loop-test-gcc
$ ./for-loop-test-gcc

i = 0
i = 1
i = 2
i = 3
i = 4
```

`objdump` for `for-loop-test`

```sh
$ objdump -d for-loop-test

for-loop-test:     file format elf64-x86-64


Disassembly of section .text:

0000000000600aa0 <main@@Base-0x40>:
  600aa0:       f3 0f 1e fa             endbr64
  600aa4:       31 ed                   xor    %ebp,%ebp
  600aa6:       49 89 d1                mov    %rdx,%r9
  600aa9:       5e                      pop    %rsi
  600aaa:       48 89 e2                mov    %rsp,%rdx
  600aad:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
  600ab1:       50                      push   %rax
  600ab2:       54                      push   %rsp
  600ab3:       45 31 c0                xor    %r8d,%r8d
  600ab6:       31 c9                   xor    %ecx,%ecx
  600ab8:       48 8b 3d 81 03 40 00    mov    0x400381(%rip),%rdi        # 
a00e40 <printf@plt+0x4002d8>
  600abf:       ff 15 83 03 40 00       call   *0x400383(%rip)        # a00e48 
<printf@plt+0x4002e0>
  600ac5:       f4                      hlt
  600ac6:       66 2e 0f 1f 84 00 00    cs nopw 0x0(%rax,%rax,1)
  600acd:       00 00 00
  600ad0:       f3 0f 1e fa             endbr64
  600ad4:       c3                      ret
        ...

0000000000600ae0 <main@@Base>:
  600ae0:       f3 0f 1e fa             endbr64
  600ae4:       55                      push   %rbp
  600ae5:       48 89 e5                mov    %rsp,%rbp
  600ae8:       48 83 ec 08             sub    $0x8,%rsp
  600aec:       53                      push   %rbx
  600aed:       bb 00 00 00 00          mov    $0x0,%ebx
        ...
  600afe:       00 00                   add    %al,(%rax)
  600b00:       83 fb 05                cmp    $0x5,%ebx
  600b03:       0f 8d 18 00 00 00       jge    600b21 <main@@Base+0x41>
  600b09:       89 de                   mov    %ebx,%esi
  600b0b:       48 8d 3d 4a 03 60 00    lea    0x60034a(%rip),%rdi        # 
c00e5c <printf@plt+0x6002f4>
  600b12:       b8 00 00 00 00          mov    $0x0,%eax
  600b17:       e8 4c 00 00 00          call   600b68 <printf@plt>
  600b1c:       83 c3 01                add    $0x1,%ebx
  600b1f:       eb df                   jmp    600b00 <main@@Base+0x20>
  600b21:       b8 00 00 00 00          mov    $0x0,%eax
  600b26:       5b                      pop    %rbx
  600b27:       c9                      leave
  600b28:       c3                      ret

Disassembly of section .init:

0000000000600b2c <.init>:
  600b2c:       f3 0f 1e fa             endbr64
  600b30:       48 83 ec 08             sub    $0x8,%rsp
  600b34:       48 8b 05 15 03 40 00    mov    0x400315(%rip),%rax        # 
a00e50 <printf@plt+0x4002e8>
  600b3b:       48 85 c0                test   %rax,%rax
  600b3e:       74 02                   je     600b42 <main@@Base+0x62>
  600b40:       ff d0                   call   *%rax
  600b42:       48 83 c4 08             add    $0x8,%rsp
  600b46:       c3                      ret

Disassembly of section .fini:

0000000000600b48 <.fini>:
  600b48:       f3 0f 1e fa             endbr64
  600b4c:       48 83 ec 08             sub    $0x8,%rsp
  600b50:       48 83 c4 08             add    $0x8,%rsp
  600b54:       c3                      ret

Disassembly of section .plt:

0000000000600b58 <printf@plt-0x10>:
  600b58:       ff 35 ca 02 40 00       push   0x4002ca(%rip)        # a00e28 
<printf@plt+0x4002c0>
  600b5e:       ff 25 cc 02 40 00       jmp    *0x4002cc(%rip)        # a00e30 
<printf@plt+0x4002c8>
  600b64:       00 00                   add    %al,(%rax)
        ...

0000000000600b68 <printf@plt>:
  600b68:       ff 25 ca 02 40 00       jmp    *0x4002ca(%rip)        # a00e38 
<printf@plt+0x4002d0>
  600b6e:       68 00 00 00 00          push   $0x0
  600b73:       e9 e0 ff ff ff          jmp    600b58 <main@@Base+0x78>
```

`objdump` for `for-loop-test-gcc`

```sh
$ objdump -d for-loop-test-gcc

for-loop-test-gcc:     file format elf64-x86-64


Disassembly of section .init:

0000000000001000 <_init>:
    1000:       f3 0f 1e fa             endbr64
    1004:       48 83 ec 08             sub    $0x8,%rsp
    1008:       48 8b 05 d9 2f 00 00    mov    0x2fd9(%rip),%rax        # 3fe8 
<__gmon_start__@Base>
    100f:       48 85 c0                test   %rax,%rax
    1012:       74 02                   je     1016 <_init+0x16>
    1014:       ff d0                   call   *%rax
    1016:       48 83 c4 08             add    $0x8,%rsp
    101a:       c3                      ret

Disassembly of section .plt:

0000000000001020 <printf@plt-0x10>:
    1020:       ff 35 9a 2f 00 00       push   0x2f9a(%rip)        # 3fc0 
<_GLOBAL_OFFSET_TABLE_+0x8>
    1026:       ff 25 9c 2f 00 00       jmp    *0x2f9c(%rip)        # 3fc8 
<_GLOBAL_OFFSET_TABLE_+0x10>
    102c:       0f 1f 40 00             nopl   0x0(%rax)

0000000000001030 <printf@plt>:
    1030:       ff 25 9a 2f 00 00       jmp    *0x2f9a(%rip)        # 3fd0 
<printf@GLIBC_2.2.5>
    1036:       68 00 00 00 00          push   $0x0
    103b:       e9 e0 ff ff ff          jmp    1020 <_init+0x20>

Disassembly of section .plt.got:

0000000000001040 <__cxa_finalize@plt>:
    1040:       ff 25 b2 2f 00 00       jmp    *0x2fb2(%rip)        # 3ff8 
<__cxa_finalize@GLIBC_2.2.5>
    1046:       66 90                   xchg   %ax,%ax

Disassembly of section .text:

0000000000001050 <_start>:
    1050:       f3 0f 1e fa             endbr64
    1054:       31 ed                   xor    %ebp,%ebp
    1056:       49 89 d1                mov    %rdx,%r9
    1059:       5e                      pop    %rsi
    105a:       48 89 e2                mov    %rsp,%rdx
    105d:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1061:       50                      push   %rax
    1062:       54                      push   %rsp
    1063:       45 31 c0                xor    %r8d,%r8d
    1066:       31 c9                   xor    %ecx,%ecx
    1068:       48 8d 3d d1 00 00 00    lea    0xd1(%rip),%rdi        # 1140 
<main>
    106f:       ff 15 63 2f 00 00       call   *0x2f63(%rip)        # 3fd8 
<__libc_start_main@GLIBC_2.34>
    1075:       f4                      hlt
    1076:       66 2e 0f 1f 84 00 00    cs nopw 0x0(%rax,%rax,1)
    107d:       00 00 00

0000000000001080 <deregister_tm_clones>:
    1080:       48 8d 3d 91 2f 00 00    lea    0x2f91(%rip),%rdi        # 4018 
<__TMC_END__>
    1087:       48 8d 05 8a 2f 00 00    lea    0x2f8a(%rip),%rax        # 4018 
<__TMC_END__>
    108e:       48 39 f8                cmp    %rdi,%rax
    1091:       74 15                   je     10a8 <deregister_tm_clones+0x28>
    1093:       48 8b 05 46 2f 00 00    mov    0x2f46(%rip),%rax        # 3fe0 
<_ITM_deregisterTMCloneTable@Base>
    109a:       48 85 c0                test   %rax,%rax
    109d:       74 09                   je     10a8 <deregister_tm_clones+0x28>
    109f:       ff e0                   jmp    *%rax
    10a1:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
    10a8:       c3                      ret
    10a9:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

00000000000010b0 <register_tm_clones>:
    10b0:       48 8d 3d 61 2f 00 00    lea    0x2f61(%rip),%rdi        # 4018 
<__TMC_END__>
    10b7:       48 8d 35 5a 2f 00 00    lea    0x2f5a(%rip),%rsi        # 4018 
<__TMC_END__>
    10be:       48 29 fe                sub    %rdi,%rsi
    10c1:       48 89 f0                mov    %rsi,%rax
    10c4:       48 c1 ee 3f             shr    $0x3f,%rsi
    10c8:       48 c1 f8 03             sar    $0x3,%rax
    10cc:       48 01 c6                add    %rax,%rsi
    10cf:       48 d1 fe                sar    $1,%rsi
    10d2:       74 14                   je     10e8 <register_tm_clones+0x38>
    10d4:       48 8b 05 15 2f 00 00    mov    0x2f15(%rip),%rax        # 3ff0 
<_ITM_registerTMCloneTable@Base>
    10db:       48 85 c0                test   %rax,%rax
    10de:       74 08                   je     10e8 <register_tm_clones+0x38>
    10e0:       ff e0                   jmp    *%rax
    10e2:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
    10e8:       c3                      ret
    10e9:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

00000000000010f0 <__do_global_dtors_aux>:
    10f0:       f3 0f 1e fa             endbr64
    10f4:       80 3d 1d 2f 00 00 00    cmpb   $0x0,0x2f1d(%rip)        # 4018 
<__TMC_END__>
    10fb:       75 2b                   jne    1128 <__do_global_dtors_aux+0x38>
    10fd:       55                      push   %rbp
    10fe:       48 83 3d f2 2e 00 00    cmpq   $0x0,0x2ef2(%rip)        # 3ff8 
<__cxa_finalize@GLIBC_2.2.5>
    1105:       00
    1106:       48 89 e5                mov    %rsp,%rbp
    1109:       74 0c                   je     1117 <__do_global_dtors_aux+0x27>
    110b:       48 8b 3d f6 2e 00 00    mov    0x2ef6(%rip),%rdi        # 4008 
<__dso_handle>
    1112:       e8 29 ff ff ff          call   1040 <__cxa_finalize@plt>
    1117:       e8 64 ff ff ff          call   1080 <deregister_tm_clones>
    111c:       c6 05 f5 2e 00 00 01    movb   $0x1,0x2ef5(%rip)        # 4018 
<__TMC_END__>
    1123:       5d                      pop    %rbp
    1124:       c3                      ret
    1125:       0f 1f 00                nopl   (%rax)
    1128:       c3                      ret
    1129:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

0000000000001130 <frame_dummy>:
    1130:       f3 0f 1e fa             endbr64
    1134:       e9 77 ff ff ff          jmp    10b0 <register_tm_clones>
    1139:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

0000000000001140 <main>:
    1140:       f3 0f 1e fa             endbr64
    1144:       55                      push   %rbp
    1145:       48 89 e5                mov    %rsp,%rbp
    1148:       48 83 ec 08             sub    $0x8,%rsp
    114c:       53                      push   %rbx
    114d:       bb 00 00 00 00          mov    $0x0,%ebx
    1152:       66 66 2e 0f 1f 84 00    data16 cs nopw 0x0(%rax,%rax,1)
    1159:       00 00 00 00
    115d:       0f 1f 00                nopl   (%rax)
    1160:       83 fb 05                cmp    $0x5,%ebx
    1163:       7d 18                   jge    117d <main+0x3d>
    1165:       89 de                   mov    %ebx,%esi
    1167:       48 8d 3d a2 2e 00 00    lea    0x2ea2(%rip),%rdi        # 4010 
<__dso_handle+0x8>
    116e:       b8 00 00 00 00          mov    $0x0,%eax
    1173:       e8 b8 fe ff ff          call   1030 <printf@plt>
    1178:       83 c3 01                add    $0x1,%ebx
    117b:       eb e3                   jmp    1160 <main+0x20>
    117d:       b8 00 00 00 00          mov    $0x0,%eax
    1182:       5b                      pop    %rbx
    1183:       c9                      leave
    1184:       c3                      ret

Disassembly of section .fini:

0000000000001188 <_fini>:
    1188:       f3 0f 1e fa             endbr64
    118c:       48 83 ec 08             sub    $0x8,%rsp
    1190:       48 83 c4 08             add    $0x8,%rsp
    1194:       c3                      ret
```
Regards
Jerry
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to