superseriousbusiness / gotosocial

Fast, fun, small ActivityPub server.
https://docs.gotosocial.org
GNU Affero General Public License v3.0
3.62k stars 306 forks source link

[bug] SIGSYS on starup with sqlite driver on OpenBSD 7.3 #1710

Open dusaint opened 1 year ago

dusaint commented 1 year ago

Describe the bug with a clear and concise description of what the bug is.

gotosocial crashes immediately on startup when db-type is set to "sqlite" on OpenBSD 7.3. This is due to a change in the syscall for mmap between OpenBSD 7.2 and 7.3.

This isn't a bug in gotosocial per se, but once the bug is fixed in modernc.org/libc via modernc.org/sqlite, a version bump will be needed in go.mod to pull the fix in. (I don't expect the actual upstream bug in x/sys/unix to be fixed anytime soon.).

The upstream bug is https://gitlab.com/cznic/libc/-/issues/29 (modernc.org/sqlite uses modernc.org/libc) The up-upstream bug is https://github.com/golang/go/issues/59661

The gist of the bug is that OpenBSD removed the "padded" version of several syscalls in 7.3, having created new versions over a year prior. In doing so, the syscall number changed, so when the old one is called, SIGSYS is raised. See https://github.com/openbsd/src/commit/1d60349d0b961891264d426ffe1c0ced24b2374c#diff-e8c6a075c6e1240e27216779540b66f3db43b14a3b3615c2ecb7a111faa54504 and https://github.com/openbsd/src/commit/8c7f5cc47d34f1bd83a08278dad544a34184dabf for the history there.

Here's the pertinent console output of a failing run:

shrike# doas -u _gotosocial /usr/local/bin/gotosocial --config-path /etc/gotosocial.yaml server start
timestamp="16/04/2023 13:14:50.326" func=bundb.sqliteConn level=INFO msg="connected to SQLITE database with address file:/var/gotosocial/db/sqlite.db?_txlock=immediate"
SIGSYS: bad system call
PC=0x27c531e77 m=3 sigcode=0

goroutine 0 [idle]:
syscall.syscall10X(0x4bf160, 0xc5, 0x0, 0x8000, 0x3, 0x1, 0x9, 0x0, 0x0, 0x0, ...)
    runtime/sys_openbsd3.go:79 +0x45 fp=0xc0008bdc70 sp=0xc0008bdc50 pc=0x463745
syscall.syscall10X(0x19aff6b?, 0x4693a6?, 0xc0008bdcf0?, 0xc0008bdd70?, 0x19afefd?, 0xc000586000?, 0x8?, 0xc0008bdd58?, 0x1af1fa9?, 0x0, ...)
    <autogenerated>:1 +0x59 fp=0xc0008bdcf0 sp=0xc0008bdc70 pc=0x469559
syscall.syscall6Internal(0x19aff40?, 0xc000586000?, 0xc0008bdd58?, 0xc0008bddc0?, 0x19b0785?, 0xc000586000?, 0x8?)
    syscall/syscall_openbsd_libc.go:26 +0x45 fp=0xc0008bdd58 sp=0xc0008bdcf0 pc=0x4b9565
syscall.syscall6Internal(0xc5, 0x0, 0x8000, 0x3, 0x1, 0x9, 0x0)
    <autogenerated>:1 +0x45 fp=0xc0008bdda0 sp=0xc0008bdd58 pc=0x4bfb65
modernc.org/libc.Xmmap(0xc000586000?, 0x244102058?, 0xc000000008?, 0x44100ff0?, 0x2?, 0x1?, 0x2441020d0?)
    modernc.org/libc@v1.22.3/libc_openbsd.go:1472 +0x51 fp=0xc0008bde08 sp=0xc0008bdda0 pc=0x19a5471
modernc.org/sqlite/lib.unixShmMap(0xc000586000, 0x25f8005b0, 0x0, 0x8000, 0x1, 0x2d4d000f8)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:16174 +0x427 fp=0xc0008bdea0 sp=0xc0008bde08 pc=0x19bff87
modernc.org/sqlite/lib.Xsqlite3OsShmMap(...)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:8380
modernc.org/sqlite/lib.walIndexPageRealloc(0xc0008bdf58?, 0x220000428, 0x0, 0x244100fe0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:24162 +0x165 fp=0xc0008bdef0 sp=0xc0008bdea0 pc=0x19d6f85
modernc.org/sqlite/lib.walIndexPage(0x1000000000000?, 0x244102050?, 0x45626108?, 0xc0008bdf58?)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:24184 +0x45 fp=0xc0008bdf20 sp=0xc0008bdef0 pc=0x19d70c5
modernc.org/sqlite/lib.walIndexReadHdr(0xc000586000, 0x220000428, 0x244100fd0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:25393 +0x208 fp=0xc0008bdf90 sp=0xc0008bdf20 pc=0x19db6c8
modernc.org/sqlite/lib.walTryBeginRead(0xc0008be008?, 0x220000428, 0xc0008be028?, 0x0, 0x1)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:25605 +0xc9 fp=0xc0008bdfe8 sp=0xc0008bdf90 pc=0x19dbfa9
modernc.org/sqlite/lib.Xsqlite3WalBeginReadTransaction(0xc000080006?, 0x220000428, 0x244100fd0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:25803 +0x7b fp=0xc0008be038 sp=0xc0008bdfe8 pc=0x19dc53b
modernc.org/sqlite/lib.pagerBeginReadTransaction(0xc000586000, 0x25f800428)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:21166 +0xaa fp=0xc0008be090 sp=0xc0008be038 pc=0x19cfc6a
modernc.org/sqlite/lib.Xsqlite3PagerSharedLock(0xc000586000, 0x25f800428)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:22577 +0x4f5 fp=0xc0008be128 sp=0xc0008be090 pc=0x19d3c55
modernc.org/sqlite/lib.lockBtree(0xc000586000, 0x220000328)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:29023 +0xa5 fp=0xc0008be1b0 sp=0xc0008be128 pc=0x19e5845
modernc.org/sqlite/lib.Xsqlite3BtreeBeginTrans(0xc000586000?, 0x2acc00128, 0x0, 0x0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:29378 +0x425 fp=0xc0008be200 sp=0xc0008be1b0 pc=0x19e6625
modernc.org/sqlite/lib.Xsqlite3InitOne(0xc000586000, 0x25f800028, 0x0, 0x244100078, 0x0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:84134 +0x2c5 fp=0xc0008be2d0 sp=0xc0008be200 pc=0x1a95505
modernc.org/sqlite/lib.Xsqlite3Init(0xc01a5f420?, 0x25f800028, 0x2100000021?)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:84327 +0x65 fp=0xc0008be320 sp=0xc0008be2d0 pc=0x1a95ce5
modernc.org/sqlite/lib.Xsqlite3ReadSchema(0xc000586000?, 0x25f800028?)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:84354 +0x45 fp=0xc0008be358 sp=0xc0008be320 pc=0x1a95e25
modernc.org/sqlite/lib.Xsqlite3Pragma(0xc000586000, 0x244100070, 0x0?, 0x2441002a0, 0x0?, 0x0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:81060 +0x4df fp=0xc0008be888 sp=0xc0008be358 pc=0x1a8cabf
modernc.org/sqlite/lib.yy_reduce(0xc000586000, 0x244100240, 0xfc, 0x0?, {0x226500199, 0x0, {0x0, 0x0, 0x0, 0x0}}, ...)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:111582 +0x42a7 fp=0xc0008beab8 sp=0xc0008be888 pc=0x1aedfa7
modernc.org/sqlite/lib.Xsqlite3Parser(0xc000586000, 0x244100240, 0x1, {0x226500199, 0x0, {0x0, 0x0, 0x0, 0x0}})
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:112120 +0xbd fp=0xc0008beb28 sp=0xc0008beab8 pc=0x1aef69d
modernc.org/sqlite/lib.Xsqlite3RunParser(0xc000586000, 0x244100070, 0x226500180)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:112843 +0x31c fp=0xc0008bec10 sp=0xc0008beb28 pc=0x1af0f1c
modernc.org/sqlite/lib.sqlite3Prepare(0xc000586000, 0x25f800028, 0x226500180, 0xffffffff, 0x80, 0x0, 0x2d4d000a0, 0x2d4d000b0)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:84626 +0x36e fp=0xc0008beca0 sp=0xc0008bec10 pc=0x1a9682e
modernc.org/sqlite/lib.sqlite3LockAndPrepare(0xc0008bed78?, 0x25f800028, 0x226500180, 0x40a90a?, 0x0?, 0x0?, 0x2d4d000a0, 0x100c0008bed58?)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:84709 +0x12d fp=0xc0008bed10 sp=0xc0008beca0 pc=0x1a96e0d
modernc.org/sqlite/lib.Xsqlite3_prepare_v2(...)
    modernc.org/sqlite@v1.21.1/lib/sqlite_openbsd_amd64.go:84776
modernc.org/sqlite.(*conn).prepareV2(0xc000168500, 0xc0008bede8)
    modernc.org/sqlite@v1.21.1/sqlite.go:1210 +0x1a5 fp=0xc0008bedb0 sp=0xc0008bed10 pc=0x1b697e5
modernc.org/sqlite.(*stmt).exec(0xc000a394e0, {0x29c9d58, 0xc0000a6008}, {0x3c4a408, 0x0, 0x0})
    modernc.org/sqlite@v1.21.1/sqlite.go:498 +0x13c fp=0xc0008bee38 sp=0xc0008bedb0 pc=0x1b65f3c
modernc.org/sqlite.(*conn).exec(0x0?, {0x29c9d58, 0xc0000a6008}, {0xc0004807c0?, 0x2221091e8?}, {0x3c4a408, 0x0, 0x0})
    modernc.org/sqlite@v1.21.1/sqlite.go:1415 +0x12c fp=0xc0008beec8 sp=0xc0008bee38 pc=0x1b6ac4c
modernc.org/sqlite.(*conn).ExecContext(0xc0008bef70?, {0x29c9d58?, 0xc0000a6008?}, {0xc0004807c0?, 0xc000161bc0?}, {0x3c4a408?, 0x100?, 0x26?})
    modernc.org/sqlite@v1.21.1/sqlite_go18.go:33 +0x2d fp=0xc0008bef18 sp=0xc0008beec8 pc=0x1b6b56d
database/sql.ctxDriverExec({0x29c9d58?, 0xc0000a6008?}, {0x2692d7ce8?, 0xc000168500?}, {0x0?, 0x0?}, {0xc0004807c0?, 0x20c8ecd?}, {0x3c4a408, 0x0, ...})
    database/sql/ctxutil.go:31 +0xdd fp=0xc0008befa0 sp=0xc0008bef18 pc=0xaf9a9d
database/sql.(*DB).execDC.func2()
    database/sql/sql.go:1675 +0x175 fp=0xc0008bf068 sp=0xc0008befa0 pc=0xb01915
database/sql.withLock({0x29c3060, 0xc0004ca000}, 0xc0008bf218)
    database/sql/sql.go:3405 +0x8c fp=0xc0008bf0a8 sp=0xc0008bf068 pc=0xb0a68c
database/sql.(*DB).execDC(0x222109501?, {0x29c9d58, 0xc0000a6008}, 0xc0004ca000, 0x3c44878?, {0xc0004807c0, 0x19}, {0x0, 0x0, 0x0})
    database/sql/sql.go:1670 +0x266 fp=0xc0008bf2b8 sp=0xc0008bf0a8 pc=0xb01266
database/sql.(*DB).exec(0x222109508?, {0x29c9d58, 0xc0000a6008}, {0xc0004807c0, 0x19}, {0x0, 0x0, 0x0}, 0x28?)
    database/sql/sql.go:1655 +0xe8 fp=0xc0008bf330 sp=0xc0008bf2b8 pc=0xb00f88
database/sql.(*DB).ExecContext.func1(0x68?)
    database/sql/sql.go:1634 +0x55 fp=0xc0008bf398 sp=0xc0008bf330 pc=0xb00d75
database/sql.(*DB).retry(0xb0?, 0xc0008bf410)
    database/sql/sql.go:1538 +0x47 fp=0xc0008bf3e0 sp=0xc0008bf398 pc=0xb002e7
database/sql.(*DB).ExecContext(0xc0002120a0?, {0x29c9d58?, 0xc0000a6008?}, {0xc0004807c0?, 0x0?}, {0x0?, 0x29c9d58?, 0xc0000a6008?})
    database/sql/sql.go:1633 +0xcc fp=0xc0008bf478 sp=0xc0008bf3e0 pc=0xb00c8c
github.com/uptrace/bun.(*DB).ExecContext(0xc0004ac240, {0x29c9d58, 0xc0000a6008}, {0x24a90f9, 0xa}, {0xc0002120a0, 0x2, 0x2})
    github.com/uptrace/bun@v1.1.12/db.go:248 +0x13c fp=0xc0008bf540 sp=0xc0008bf478 pc=0xb2c33c
github.com/superseriousbusiness/gotosocial/internal/db/bundb.sqlitePragmas({0x29c9d58, 0xc0000a6008}, 0xc000a39420)
    github.com/superseriousbusiness/gotosocial/internal/db/bundb/bundb.go:467 +0x6fe fp=0xc0008bf6e0 sp=0xc0008bf540 pc=0x1b7b01e
github.com/superseriousbusiness/gotosocial/internal/db/bundb.NewBunDBService({0x29c9d58, 0xc0000a6008}, 0xc0002e4000)
    github.com/superseriousbusiness/gotosocial/internal/db/bundb/bundb.go:140 +0x147 fp=0xc0008bf7e8 sp=0xc0008bf6e0 pc=0x1b78ba7
github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/server.glob..func1({0x29c9d58, 0xc0000a6008})
    github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/server/server.go:74 +0x19c fp=0xc0008bfca8 sp=0xc0008bf7e8 pc=0x209323c
main.run({0x29c9d58, 0xc0000a6008}, 0x25641c0)
    github.com/superseriousbusiness/gotosocial/cmd/gotosocial/common.go:81 +0x12c fp=0xc0008bfd00 sp=0xc0008bfca8 pc=0x2095c0c
main.serverCommands.func2(0xc00015c300?, {0x2499158?, 0x2?, 0x2?})
    github.com/superseriousbusiness/gotosocial/cmd/gotosocial/server.go:39 +0x31 fp=0xc0008bfd28 sp=0xc0008bfd00 pc=0x2096451
github.com/spf13/cobra.(*Command).execute(0xc00015c300, {0xc0002137a0, 0x2, 0x2})
    github.com/spf13/cobra@v1.6.1/command.go:916 +0x862 fp=0xc0008bfe60 sp=0xc0008bfd28 pc=0x5b6fc2
github.com/spf13/cobra.(*Command).ExecuteC(0xc000005b00)
    github.com/spf13/cobra@v1.6.1/command.go:1044 +0x3bd fp=0xc0008bff18 sp=0xc0008bfe60 pc=0x5b783d
github.com/spf13/cobra.(*Command).Execute(...)
    github.com/spf13/cobra@v1.6.1/command.go:968
main.main()
    github.com/superseriousbusiness/gotosocial/cmd/gotosocial/main.go:74 +0x1dd fp=0xc0008bff80 sp=0xc0008bff18 pc=0x2095f1d
runtime.main()
    runtime/proc.go:250 +0x1f3 fp=0xc0008bffe0 sp=0xc0008bff80 pc=0x436c33
runtime.goexit()
    runtime/asm_amd64.s:1598 +0x1 fp=0xc0008bffe8 sp=0xc0008bffe0 pc=0x466a81

What's your GoToSocial Version?

v0.8.0

GoToSocial Arch

OpenBSD 7.3 amd64

What happened?

gotosocial crashes immediately

What you expected to happen?

gotosocial should start up normally

How to reproduce it?

rcctl start gotosocial or just launch gotosocial from the command line normally

Anything else we need to know?

No response

tsmethurst commented 1 year ago

Ack, thanks! We'll keep an eye on the sqlite dependency

sparked435 commented 1 year ago

This seems to happen on NetBSD 9.1 as well, switching the journal_mode pragma to 'delete' allows gotosocial to operate normally. Can confirm gotosocial versions 6.0, 7.1, and 8.1 are impacted.

# /usr/pkg/bin/gotosocial --config-path /vol/gotosocial/config.yml server start
timestamp="27/04/2023 15:26:24.486" func=bundb.sqliteConn level=INFO msg="connected to SQLITE database with address file:/vol/gotosocial/social.db?_txlock=immediate"
unexpected fault address 0x7f7fd09c9000
fatal error: fault
[signal SIGBUS: bus error code=0x2 addr=0x7f7fd09c9000 pc=0x466eca]

goroutine 1 [running]:
runtime.throw({0x24a7167?, 0x7f7fd07020a0?})
        runtime/panic.go:1047 +0x5d fp=0xc00091dd88 sp=0xc00091dd58 pc=0x4345dd
runtime.sigpanic()
        runtime/signal_unix.go:834 +0x1ac fp=0xc00091dde8 sp=0xc00091dd88 pc=0x44a3ec
runtime.memmove()
        runtime/memmove_amd64.s:103 +0xaa fp=0xc00091ddf0 sp=0xc00091dde8 pc=0x466eca
modernc.org/libc.Xmemcpy(...)
        modernc.org/libc@v1.22.3/libc.go:824
modernc.org/sqlite/lib.walIndexRecover(0xc0002c4000, 0x7f7fcfd00528)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:24801 +0x707 fp=0xc00091df28 sp=0xc00091ddf0 pc=0x19e0507
modernc.org/sqlite/lib.walIndexReadHdr(0xc0002c4000, 0x7f7fcfd00528, 0x7f7fd0700fc0)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:25550 +0x252 fp=0xc00091df98 sp=0xc00091df28 pc=0x19e34b2
modernc.org/sqlite/lib.walTryBeginRead(0x19dea9b?, 0x7f7fcfd00528, 0xc00091e030?, 0x0, 0x1)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:25759 +0xc9 fp=0xc00091dff0 sp=0xc00091df98 pc=0x19e3d49
modernc.org/sqlite/lib.Xsqlite3WalBeginReadTransaction(0x7f7fd0700fc4?, 0x7f7fcfd00528, 0x7f7fd0700fc0)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:25957 +0x7b fp=0xc00091e040 sp=0xc00091dff0 pc=0x19e42db
modernc.org/sqlite/lib.pagerBeginReadTransaction(0xc0002c4000, 0x7f7fcfb00428)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:21308 +0xaa fp=0xc00091e098 sp=0xc00091e040 pc=0x19d7bea
modernc.org/sqlite/lib.Xsqlite3PagerSharedLock(0xc0002c4000, 0x7f7fcfb00428)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:22733 +0x4f5 fp=0xc00091e130 sp=0xc00091e098 pc=0x19dbbd5
modernc.org/sqlite/lib.lockBtree(0xc0002c4000, 0x7f7fcfd00428)
        modernc.org/sqlite@v1.21.1/lib/sqlite_netbsd_amd64.go:29148 +0xa5 fp=0xc00091e1b8 sp=0xc00091e130 pc=0x19ed305
modernc.org/sqlite/lib.Xsqlite3BtreeBeginTrans(0xc0002c4000?, 0x7f7fd01000a8, 0x0, 0x0)
dusaint commented 1 year ago

I, too, have seen this SIGBUS once I applied the the fix for the SIGSYS above. It looks just a little different on OpenBSD, but same idea. Some experimentation shows that setting journal_mode (as gotosocial does on starup) when the db already has it set to "WAL" triggers this. I'll report it upstream.

dusaint commented 1 year ago

Filed the SIGBUS as https://gitlab.com/cznic/sqlite/-/issues/144