mmottl/postgresql-ocaml

Segmentation Fault in unescape_bytea_9x (?)

Closed this issue · 6 comments

I've been getting seemingly “random” segmentation faults, I tried to run my server in GDB and it gave this:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe6d27700 (LWP 708)]
unescape_bytea_9x (v_from=<value optimized out>) at lib/postgresql_stubs.c:966
966     lib/postgresql_stubs.c: No such file or directory.
        in lib/postgresql_stubs.c

The segmentation faults only happen with the PG backend of https://github.com/smondet/trakeva, exactly the same server works fine the Sqlite backend.

The PG server is on version 9.3.

I forgot to paste the backtrace:

(gdb) bt full
#0  unescape_bytea_9x (v_from=<value optimized out>) at lib/postgresql_stubs.c:966
        v_res = 51368496
        res = <value optimized out>
        n_hex_pairs = <value optimized out>
        end = 0x7fffb7ffeff8 <Address 0x7fffb7ffeff8 out of bounds>
#1  PQunescapeBytea9x_stub (v_from=<value optimized out>) at lib/postgresql_stubs.c:979
        s = <value optimized out>
#2  0x00000000005ebcc6 in camlPostgresql__unescape_bytea_1238 () at lib/postgresql.ml:531
No locals.
#3  0x00000000005e3b3c in camlTrakeva_postgresql__fun_1654 () at src/lib_postgresql/trakeva_postgresql.ml:130
No locals.
#4  0x00000000006d4513 in camlNonstd__loop_1313 ()
No symbol table info available.
#5  0x00000000006d4513 in camlNonstd__loop_1313 ()
No symbol table info available.
#6  0x00000000005e46f2 in camlTrakeva_postgresql__exec_one_1297 () at src/lib_postgresql/trakeva_postgresql.ml:127
No locals.
#7  0x00000000006d52d8 in camlNonstd__count_map_1166 ()
No symbol table info available.
#8  0x00000000005e5518 in camlTrakeva_postgresql__exec_sql_exn_1286 () at src/lib_postgresql/trakeva_postgresql.ml:96
No locals.
#9  0x00000000005e57b3 in camlTrakeva_postgresql__get_exn_1338 () at src/lib_postgresql/trakeva_postgresql.ml:167
No locals.
#10 0x00000000005e4155 in camlTrakeva_postgresql__fun_1604 () at src/lib_postgresql/trakeva_postgresql.ml:51
No locals.
#11 0x00000000005f86e1 in camlLwt_preemptive__task_1061 () at src/preemptive/lwt_preemptive.ml:161
No locals.
#12 0x00000000005f89aa in camlLwt_preemptive__worker_loop_1030 () at src/preemptive/lwt_preemptive.ml:80
No locals.
#13 0x00000000006e3f79 in camlThread__fun_1084 () at thread.ml:37
No locals.
#14 0x000000000075d532 in caml_start_program ()
No symbol table info available.
#15 0x0000000000ecc580 in ?? ()
No symbol table info available.
#16 0x0000000000000000 in ?? ()
No symbol table info available.

Thanks, I'm looking at it right now and have already found the (subtle!) bug. Basically, there are two ways to enter the unescaping routine: given a string that comes from the PostgreSQL C-library, and given a string that comes from OCaml. The former string will never move in memory, the latter can if you allocate in the OCaml heap. I'll upload a patch soon.

@smondet could you please test the latest development snapshot? It's completely untested, but was carefully read and compiles without errors 😉. I guess you will have an easier time than me determining whether the problem is fixed now.

@mmottl I've been using the master branch today, haven't noticed any seg-fault so far !!
Thanks!

@smondet Sounds good, thanks for testing! I'll hold off until the end of the week. If you don't report any segfaults by then, I'll make a new release.

The new release containing the fix has been uploaded to OPAM and should be available there shortly.