Well, it seems that there is something wrong with g++ compiler.
I add **return 0** in the end of function `sha_256()`, and the compiled result 
is 
```
...
    36b3:       e8 48 fc ff ff          callq  3300 <calc_sha256>
    36b8:       85 c0                   test   %eax,%eax
    36ba:       75 74                   jne    3730 <_Z7sha_256v+0xf0>
...
    36c0:       85 ff                   test   %edi,%edi
    36c2:       75 60                   jne    3724 <_Z7sha_256v+0xe4>
...
    3719:       31 c0                   xor    %eax,%eax
    371b:       48 83 c4 10             add    $0x10,%rsp
    371f:       5b                      pop    %rbx
    3720:       5d                      pop    %rbp
    3721:       41 5c                   pop    %r12
    3723:       c3                      retq   
```

Seems right and the function will return successfully.
On the contrary, if I **DO NOT** add `return 0` ,the compiled result will be
```
...
    36b3:       e8 48 fc ff ff          callq  3300 <calc_sha256>
    36b8:       85 c0                   test   %eax,%eax
    36ba:       75 24                   jne    36e0 <_Z7sha_256v+0xa0>
    36bc:       8b 7c 24 0c             mov    0xc(%rsp),%edi
    36c0:       85 ff                   test   %edi,%edi
    36c2:       74 31                   je     36f5 <_Z7sha_256v+0xb5>
...
    36f5:       48 8d 35 66 29 00 00    lea    0x2966(%rip),%rsi        # 6062 
<_IO_stdin_used+0x62>
    36fc:       bf 01 00 00 00          mov    $0x1,%edi
    3701:       4c 8d 63 20             lea    0x20(%rbx),%r12
    3705:       31 c0                   xor    %eax,%eax
    3707:       e8 a4 f0 ff ff          callq  27b0 <__printf_chk@plt>
    370c:       48 8d 2d 65 29 00 00    lea    0x2965(%rip),%rbp        # 6078 
<_IO_stdin_used+0x78>
    3713:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
    3718:       0f b6 13                movzbl (%rbx),%edx
    371b:       48 89 ee                mov    %rbp,%rsi
    371e:       bf 01 00 00 00          mov    $0x1,%edi
    3723:       31 c0                   xor    %eax,%eax
    3725:       48 83 c3 01             add    $0x1,%rbx
    3729:       e8 82 f0 ff ff          callq  27b0 <__printf_chk@plt>
    372e:       4c 39 e3                cmp    %r12,%rbx
    3731:       75 e5                   jne    3718 <_Z7sha_256v+0xd8>
    3733:       bf 0a 00 00 00          mov    $0xa,%edi
    3738:       e8 63 ed ff ff          callq  24a0 <putchar@plt>
    373d:       48 8d 3d 39 29 00 00    lea    0x2939(%rip),%rdi        # 607d 
<_IO_stdin_used+0x7d>
    3744:       e8 c7 ed ff ff          callq  2510 <puts@plt>
    3749:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

0000000000003750 <_Z11aes_gcm_128v>:
    3750:       f3 0f 1e fa             endbr64 
```
Don't care about address 0x36f5 - 0x3744. They are contents from original file

```
    printf("[+] SHA256 result is ");

    int i;
    for(i = 0; i < 32; i ++) {
        printf("%02x", output_hash[i]);
    }
    printf("\n");
    printf("[+] calc_sha256 success ...\n");
```
And we can see address 0x3749, we want the function returns but actually the 
machine code doesn't do such thing. So the program will continue executing from 
0x3740, which is the start address of function `aes_gcm_128()`. So we can see a 
printf result and segmentation fault, which is caused by an unalloced stack 
storage for 
```
uint8_t aes_gcm_plaintext[16]
```

Maybe it's something wrong with the g++ compiler?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/apache/incubator-teaclave-sgx-sdk/issues/285#issuecomment-726494292

Reply via email to