<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/75698>75698</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Segmentation fault happened when using -Os.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bfsyrgz
</td>
</tr>
</table>
<pre>
For the following two c files, I performed the equivalent transformation of loop from **file1.c** to **file2.c**. I ran these two files respectively using different optimization levels including **-O0**, **-O1**, **-O2**, **-O3**, **-Os**, **-Ofast**. The exact output results(i.e. checksum values) are:
```
-O0 -O1 -O2 -O3 -Os -Ofast
file1.c 544B63A3 544B63A3 2D7F89A7 2D7F89A7 5EF6A4E1 2D7F89A7
file2.c 544B63A3 544B63A3 2D7F89A7 2D7F89A7 Segmentation 2D7F89A7
```
using command line:
```
clang <filename.c> <optimization level> -lm -I $CSMITH_HOME/include && ./a.out
```
Besides, I ran the command line ```gcc <filename.c> <optimization level> -fsanitize=undefined -I /usr/include/csmith/ && ./a.out```, and the terminal showed that my programs have no undefined behavior.
Please help me to explain why **file2.c** produced **Segmentation fault** when using the optimization level **-Os**, thank you.
version: clang+llvm 14.0.0
os: ubuntu 22.04
**file1.c**
```
#include "csmith.h"
static uint32_t g_a65[2];
struct S1 {
int32_t f0;
int16_t f1;
const volatile int32_t f2;
};
static uint16_t g_2[2][2] = {{1UL, 1UL}, {1UL, 1UL}};
static volatile int32_t g_3 = 0x6FF23B1AL;
static int32_t g_4 = 1L;
static uint8_t g_11[3][1] = {{1UL}, {1UL}, {1UL}};
static int32_t g_21 = 0L;
static int32_t *volatile g_20 = &g_21;
static uint32_t g_25 = 0UL;
static const struct S1 g_27 = {-3L, 0L, 2L};
static uint32_t *g_31 = &g_25;
static uint32_t **g_30 = &g_31;
static uint32_t ***const volatile g_29 = &g_30;
static int32_t *g_35 = (void *)0;
static int32_t **volatile g_34 = &g_35;
static uint64_t g_45 = 0x9A97A8D1D9D65FE9LL;
static int32_t g_47 = (-1L);
static void func_1(void);
static int32_t *func_5(int32_t *p_6, const int32_t p_9);
static void func_1(void) {
uint32_t l_43 = 0UL;
uint8_t *l_10 = &g_11[1][0];
int32_t l_38 = (-8L);
uint64_t *l_44 = &g_45;
int8_t l_57 = 7L;
uint32_t *l_95 = &g_25;
(*g_34) = func_5(&g_4, g_4);
g_47 &= ((((*l_10) = (safe_lshift_func_uint64_t_u_u(((*l_44) ^= ((l_38 <= ((((safe_sub_func_int64_t_s_s((safe_lshift_func_int64_t_s_u(g_4, 37)), l_43)) || l_38), g_11[1][0]) ^ 0x18D7C584EDB553A8LL)) < 4294967295UL)), 63))) || 0UL), (*g_20));
(*g_35) = 0x2FD17BA7L;
int ii_5 = 0;
// strength reduction
for (l_38 = 1; (l_38 >= 0); l_38 -= 1) {
g_a65[ii_5] = g_11[2][0] * l_38 + (**g_30);
ii_5++;
(*g_35) = (safe_add_func_uint32_t_u_u(((safe_mod_func_int64_t_s_s(((safe_lshift_func_int8_t_s_s(((((((safe_sub_func_uint16_t_u_u(((((*l_10) = g_2[l_38][l_38]), ((249UL && g_3) < 0xECLL)), g_47), g_27.f1)), g_47) >= 0xAB8BF514L), l_43), l_57) && g_2[l_38][l_38]), g_2[0][1])) || g_47), 0x3564F69674B172ABLL)) == g_25), 0x914D865EL));
for (g_45 = 0; (g_45 <= 1); g_45 += 1) {
g_2[l_38][g_45] = (((l_43 = g_27.f1), &g_31) == &l_95);
}
}
}
static int32_t *func_5(int32_t *p_6, const int32_t p_9) {
uint32_t *l_24 = &g_25;
if ((safe_lshift_func_uint8_t_u_s((0x9BA5F27AL != ((*l_24) |= (p_9 && g_4))), g_4))) {
int32_t *l_32[1] = {&g_21};
(*g_29) = &l_24;
return l_32[0];
}
return &g_21;
}
int main(void) {
int i, j;
int print_hash_value = 0;
platform_main_begin();
crc32_gentab();
func_1();
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
transparent_crc(g_2[i][j], "g_2[i][j]", print_hash_value);
}
}
transparent_crc(g_3, "g_3", print_hash_value);
transparent_crc(g_4, "g_4", print_hash_value);
for (i = 0; i < 3; i++) {
for (j = 0; j < 1; j++) {
transparent_crc(g_11[i][j], "g_11[i][j]", print_hash_value);
}
}
transparent_crc(g_21, "g_21", print_hash_value);
transparent_crc(g_25, "g_25", print_hash_value);
transparent_crc(g_27.f0, "g_27.f0", print_hash_value);
transparent_crc(g_27.f1, "g_27.f1", print_hash_value);
transparent_crc(g_27.f2, "g_27.f2", print_hash_value);
transparent_crc(g_45, "g_45", print_hash_value);
transparent_crc(g_47, "g_47", print_hash_value);
for (i = 0; i < 2; i++) {
transparent_crc(g_a65[i], "g_a65[i]", print_hash_value);
}
platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
return 0;
}
```
**file2.c**
```
#include "csmith.h"
static uint32_t g_a65[2];
struct S1 {
int32_t f0;
int16_t f1;
const volatile int32_t f2;
};
static uint16_t g_2[2][2] = {{1UL, 1UL}, {1UL, 1UL}};
static volatile int32_t g_3 = 0x6FF23B1AL;
static int32_t g_4 = 1L;
static uint8_t g_11[3][1] = {{1UL}, {1UL}, {1UL}};
static int32_t g_21 = 0L;
static int32_t *volatile g_20 = &g_21;
static uint32_t g_25 = 0UL;
static const struct S1 g_27 = {-3L, 0L, 2L};
static uint32_t *g_31 = &g_25;
static uint32_t **g_30 = &g_31;
static uint32_t ***const volatile g_29 = &g_30;
static int32_t *g_35 = (void *)0;
static int32_t **volatile g_34 = &g_35;
static uint64_t g_45 = 0x9A97A8D1D9D65FE9LL;
static int32_t g_47 = (-1L);
static void func_1(void);
static int32_t *func_5(int32_t *p_6, const int32_t p_9);
static void func_1(void) {
uint32_t l_43 = 0UL;
uint8_t *l_10 = &g_11[1][0];
int32_t l_38 = (-8L);
uint64_t *l_44 = &g_45;
int8_t l_57 = 7L;
uint32_t *l_95 = &g_25;
(*g_34) = func_5(&g_4, g_4);
g_47 &= ((((*l_10) = (safe_lshift_func_uint64_t_u_u(((*l_44) ^= ((l_38 <= ((((safe_sub_func_int64_t_s_s((safe_lshift_func_int64_t_s_u(g_4, 37)), l_43)) || l_38), g_11[1][0]) ^ 0x18D7C584EDB553A8LL)) < 4294967295UL)), 63))) || 0UL), (*g_20));
(*g_35) = 0x2FD17BA7L;
l_38 = 1;
uint32_t s_5 = g_11[2][0] * l_38 + (**g_30);
int ii_5 = 0;
// strength reduction
for (; (l_38 >= 0); l_38 -= 1) {
g_a65[ii_5] = s_5;
s_5 = s_5 - g_11[2][0] * 1;
ii_5++;
(*g_35) = (safe_add_func_uint32_t_u_u(((safe_mod_func_int64_t_s_s(((safe_lshift_func_int8_t_s_s(((((((safe_sub_func_uint16_t_u_u(((((*l_10) = g_2[l_38][l_38]), ((249UL && g_3) < 0xECLL)), g_47), g_27.f1)), g_47) >= 0xAB8BF514L), l_43), l_57) && g_2[l_38][l_38]), g_2[0][1])) || g_47), 0x3564F69674B172ABLL)) == g_25), 0x914D865EL));
for (g_45 = 0; (g_45 <= 1); g_45 += 1) {
g_2[l_38][g_45] = (((l_43 = g_27.f1), &g_31) == &l_95);
}
}
}
static int32_t *func_5(int32_t *p_6, const int32_t p_9) {
uint32_t *l_24 = &g_25;
if ((safe_lshift_func_uint8_t_u_s((0x9BA5F27AL != ((*l_24) |= (p_9 && g_4))), g_4))) {
int32_t *l_32[1] = {&g_21};
(*g_29) = &l_24;
return l_32[0];
}
return &g_21;
}
int main(void) {
int i, j;
int print_hash_value = 0;
platform_main_begin();
crc32_gentab();
func_1();
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
transparent_crc(g_2[i][j], "g_2[i][j]", print_hash_value);
}
}
transparent_crc(g_3, "g_3", print_hash_value);
transparent_crc(g_4, "g_4", print_hash_value);
for (i = 0; i < 3; i++) {
for (j = 0; j < 1; j++) {
transparent_crc(g_11[i][j], "g_11[i][j]", print_hash_value);
}
}
transparent_crc(g_21, "g_21", print_hash_value);
transparent_crc(g_25, "g_25", print_hash_value);
transparent_crc(g_27.f0, "g_27.f0", print_hash_value);
transparent_crc(g_27.f1, "g_27.f1", print_hash_value);
transparent_crc(g_27.f2, "g_27.f2", print_hash_value);
transparent_crc(g_45, "g_45", print_hash_value);
transparent_crc(g_47, "g_47", print_hash_value);
for (i = 0; i < 2; i++) {
transparent_crc(g_a65[i], "g_a65[i]", print_hash_value);
}
platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
return 0;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWltv47YS_jXMC2FDpO4PfpDtGF3ARQ6w3WdBlimJqS4-IuU4--sPeNFdTrPpAYoCNoxIGg5nPs58HDEDR4zRtCRkA-wtsPdPUcOzqt6cEvZepz-fTtX5fXOoasgzApMqz6s3WqaQv1UwhgnNCQN4B7_BC6mTqi7IWSqS_zb0GuWk5JDXUcnEUMRpVcIqgXlVXWBSVwUEOAA4EFbQOlYPkFcDMW7Fa_gN1lEpjDMivUvfsCbsQmJOryR_hw0T0M40SUgtXFcXTgv6UznOyZXkDNIyzpuz0FOGVy-GuhHLaEVoLsJzkTkXsbkoiRhv1_CHCM0tijmsGn5puIDf5JwB7NE1WcM4I_GfrCngNcobEVkfRjUBZgCMPTAC4Bj6Kx_h8LN6MSbPCK5e8ERmTp7Z5FmAhcq6zooasS1r65hBN336jPfuwfMDd_ZsPx-cwHpGS4q9H_yXfu460J_vJC1IyVWqh-OLkVNEiauiiMozzGl5N8ZxHgmqmDsBs4wKso6B-SwEc3KJgVVewNU3CLC1-_77tz9-C397-f0Z4IOiHYEAOwA7cA3wIVpXDV90uiWMntuNpVk_Qgu7CWkc_wq6hEUl5fQnAea-Kc8koSU5K8CHhtU9UIAPMSsozwA-zEF3cPEOCkwCHyd1Qcsohyyr3mQZiDgs3uGlrtI6KhjMoiuBZQV7vyeSRVda1WsdBfn3PzmJGIEZyS-wIKIckNslj2gJ37L3pdIgPJybmJz14IgKSdTkevvBt4yUuk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