stormalf / chibicc

A small C compiler from rui314
MIT License
6 stars 1 forks source link

ISS-169 program initdb produced by chibicc during postgres compile failed at running with segmentation fault #62

Open stormalf opened 10 months ago

stormalf commented 10 months ago

program initdb produced by chibicc during postgres compile failed at running with segmentation fault Program received signal SIGSEGV, Segmentation fault. 0x0000000001efd454 in MemoryChunkSetHdrMask () at ../../../../src/include/utils/memutils_memorychunk.h:164 164 chunk->hdrmask = (((uint64) blockoffset) << MEMORYCHUNK_BLOCKOFFSET_BASEBIT) | (gdb) bt

0 0x0000000001efd454 in MemoryChunkSetHdrMask ()

at ../../../../src/include/utils/memutils_memorychunk.h:164

1 0x0000000001f001b9 in AllocSetAlloc () at aset.c:885

2 0x0000000001f243ba in MemoryContextAllocExtended () at mcxt.c:1150

3 0x0000000001e441b9 in DynaHashAlloc () at dynahash.c:292

4 0x0000000001e47998 in hash_create () at dynahash.c:391

5 0x0000000001f817d3 in init_timezone_hashtable () at pgtz.c:209

6 0x0000000001f83317 in pg_tzset () at pgtz.c:246

7 0x0000000001f8396f in pg_timezone_initialize () at pgtz.c:370

8 0x0000000001ec921f in InitializeGUCOptions () at guc.c:1504

9 0x0000000001393be9 in PostmasterMain () at postmaster.c:682

10 0x0000000001014950 in main () at main.c:198

stormalf commented 3 weeks ago

With the last version of chibicc 1.0.22.5, the issue is now different later in hash_initial_lookup : Program received signal SIGSEGV, Segmentation fault. 0x0000000001af1374 in hash_initial_lookup () at dynahash.c:1769 1769 segp = hashp->dir[segment_num]; (gdb) bt

0 0x0000000001af1374 in hash_initial_lookup () at dynahash.c:1769

1 0x0000000001af5d8a in hash_search_with_hash_value () at dynahash.c:1009

2 0x0000000001af6d8d in hash_search () at dynahash.c:960

3 0x0000000001bd8174 in pg_tzset () at pgtz.c:260

4 0x0000000001bd8617 in pg_timezone_initialize () at pgtz.c:370

5 0x0000000001b3a404 in InitializeGUCOptions () at guc.c:1538

6 0x0000000001245ba6 in PostmasterMain () at postmaster.c:573

7 0x0000000000ef5f48 in main () at main.c:197

The segment_num value is incorrect : 658106 hashvalue: 3926571702 segment_num: 658106 hashp->dsize: 256 hashp->dir: 0x2d84d48 Segmentation fault If we compile with gcc and debug with gdb the values are completely different : 1===bucket=97, max_bucket=8191 2===bucket=97, max_bucket=8191 bucket: 97 sshift: 8 hashvalue: 4194828385 segment_num: 0 hashp->dsize: 256 hashp->dir: 0x7ffff70c0e50

stormalf commented 3 weeks ago

the issue was caused by incorrect __builtin_clz. I used the implementation from @cosmopolitan that seems to work better. No more segmentation fault in postgres execution. It will be solved in 1.0.22.5