Commit Graph

179 Commits

Author SHA1 Message Date
Evan Hahn
7d44dbbc4a Remove unused argument from database method
This change should have no user impact. The `name` argument was never
used so we can remove it.
2023-04-20 10:30:16 -05:00
Max Radermacher
89b6b4136c Improve performance of database callbacks 2023-04-19 15:38:33 -07:00
Max Radermacher
df9a6f09a7
Improve performance of checkpointIfNecessary() 2023-04-19 12:24:26 -07:00
Evan Hahn
510f10ad32
Update SQLCipher to fork that supports BARRIERFSYNC 2023-04-17 12:03:45 -05:00
Max Radermacher
92e170fe12
Swiftify SSKEnvironment 2023-04-14 10:58:21 -07:00
Sasha Weiss
f1063c1071
Migrate OWSDevice to SDSCodableModel 2023-04-13 14:00:46 -07:00
Sasha Weiss
c9fbc9a9ee
Migrate SSKJobRecord and subclasses to SDSCodableModel using factory init 2023-04-13 10:57:48 -07:00
Evan Hahn
8b2ec27f19
Clean up database checkpointing
This change should have no impact on behavior.

SQLite supports [several different checkpoint modes][0]. We always use
the "TRUNCATE" mode, so we can remove the code that handles different
ones.

We can also stop logging checkpoint results that are [always 0 in
"TRUNCATE" mode][1].

We can also use GRDB's WAL checkpoint method instead of calling SQLite's
API directly.

[0]: https://www.sqlite.org/c3ref/c_checkpoint_full.html
[1]: https://sqlite.org/c3ref/wal_checkpoint_v2.html
2023-04-13 12:21:12 -05:00
Max Radermacher
22ef2d8e55
Swiftify AppSetup 2023-04-12 17:54:48 -07:00
Evan Hahn
8a91bf188b
Periodically check internal users's databases for corruption 2023-04-12 15:04:21 -05:00
Evan Hahn
ea01acb00b Database integrity checker shouldn't manage its dispatch queue
*I recommend reviewing this with whitespace changes disabled.*

`GRDBDatabaseStorageAdapter.logIntegrityChecks()` needn't return a
promise. It only does that so it can run on a specific dispatch queue.

Instead, the dispatch queue should be the responsibility of the caller.

This change should have no user impact and makes a future improvement
easier.
2023-04-11 14:59:46 -05:00
Evan Hahn
2895b84ef1
Database recovery: REINDEX as a first step
*I recommend reviewing this with whitespace changes disabled.*
2023-04-05 15:21:16 -05:00
Harry
5ad17c4f2d
Support change number in registration coordinator
* Factor out RegistrationCoordinatorLoader

* Add PNI change number state to regcoordinatorloader

* Don't exit change number flow once we start a pni operation

* Add ChangePhoneNumberPniManager to reg coordinator dependencies

* add params required for pni to change number registration mode

* change number from regcoordinator

* rename ChangePhoneNumber -> LegacyChangePhoneNumber

* remove pni change number support from LegacyChangePhoneNumber

* remove codable conformance from pni change number manager's pending state

* Update ts account manager state when finalizing change number

* Add message processor deps to reg coordinator

* addAsyncCompletion on dbv2 write transactions

* Suspend message processing while doing a reg coordinator change number

* hook up change number

* Add handler-free message processing suspension method

* odds and ends

* minor cleanup

* slight refactor to finalize change phone number in the export function

* Put wait for message processing + suspend onto messageProcessor

* kick of prekey refresh after pni change number finalize transaction ends

* nit
2023-03-17 17:55:06 -07:00
Evan Hahn
e871e20e07 Enable checkpoint_fullfsync database option
From [the SQLite docs][0]:

> Query or change the fullfsync flag for checkpoint operations. If this
> flag is set, then the F_FULLFSYNC syncing method is used during
> checkpoint operations on systems that support F_FULLFSYNC. The default
> value of the checkpoint_fullfsync flag is off.

In other words, checkpoints should be more reliable (but possibly
slower).

[0]: https://www.sqlite.org/pragma.html#pragma_checkpoint_fullfsync
2023-03-09 14:43:51 -06:00
Sasha Weiss
e5f1af6c2e
Support lookup-by-username 2023-02-08 10:16:57 -08:00
Harry
003cc19e62
Update SignalCoreKit. Use explicit DispatchQueue instead of relying on the implicit type on promise 'on' params
* Use explicit DispatchQueue instead of relying on the implicit type on promise 'on' params

* Update SignalCoreKit
2023-02-07 16:14:39 -08:00
Evan Hahn
8ba6f6cc24
Add additional result code to grdbErrorForLogging 2023-02-01 10:27:48 -06:00
Evan Hahn
8d707a9c74
Add spam reporting tokens 2023-01-25 14:14:57 -06:00
Harry
b3136fb42c
Call Disposition
* Add CallEvent sync message proto

* Add CallRecord class and db table

* Renames and comments for clarity

* create and update CallRecord from local device call events

* Handle incoming call event sync messages

* Add outgoing call sync event piping

* Send call event sync messages

* Handle call record sync messages before the app is launched

* Mark TSCall interactions as read and update their status when getting call event sync messages

* Prevent CallKit race conditions by checking state before creating TSCall interactions

* Update chat call event text for unanswered and declined calls

* Remove incorrect debug assert which fires every time a call is missed in the background

* PR comments, mostly nits

* convert old debug assert into a log + task comment

* nit

* Update copy for in chat call logs

* Smaller PR comments

* Add foreign key to CallRecord's interactionUniqueId

* Update write transaction ordering comment

* run genstrings

* fix strings
2023-01-10 11:48:46 -08:00
Evan Hahn
dc9637b127 Handle SQLite corruption during database initialization
GRDB executes some simple statements when initializing a `DatabasePool`
object (see [1][], [2][], [3][], [4][]). If any of those fail with a
corruption error, we should flag the database as corrupted. This will
let the user attempt recovery.

[1]: 23f4254ae3/GRDB/Core/Database.swift (L365)
[2]: 23f4254ae3/GRDB/Core/DatabasePool.swift (L75)
[3]: 23f4254ae3/GRDB/Core/DatabasePool.swift (L82)
[4]: 23f4254ae3/GRDB/Core/DatabasePool.swift (L91-L94)
2022-12-19 14:35:33 -06:00
Evan Hahn
fb8f390733 Remove unnecessary swiftlint:disables
In a48049f44f, we disabled some Swiftlint
rules due to a Swiftlint bug. This reverts that commit because it
[appears the bug has been fixed][0].

[0]: 7bd8362dae/CHANGELOG.md (0503-bundle-of-towels)
2022-12-19 14:35:21 -06:00
Max Radermacher
921301cdb0
Add CDSv2 2022-12-14 16:30:35 -08:00
Max Radermacher
a48049f44f Disable false positive inert_defer warnings
The `#if` blocks seem to be confusing swiftlint. Since this is
debug-only code, just disable those warnings.

(GRDB itself will catch *some* re-entrant queries but not all of them,
so we might want to keep this logic around even though it’s disabled. It
detects read/read and write/write, but it doesn’t detect read/write or
write/read.)
2022-11-29 14:46:40 -08:00
Max Radermacher
6a1ec67662 Remove readThrows method
It’s not necessary -- the same functionality can be implemented with the
normal `read` method.
2022-11-29 14:46:40 -08:00
Jordan Rose
a87651e7ed Handle incoming group call rings
This adds a table of "cancelled group rings", to handle out-of-order
delivery between a cancellation and a ring. (This can happen when the
cancellation comes from a linked device rather than the original
ringer, though it's very unlikely.) Thirty-minute-old cancellations
are cleared lazily.

This also adds a new IncomingCallControls to replace the usual
CallControls in a GroupCallViewController.

And finally, there's a lot of code that was 1:1-call-specific that now
handles group calls as well, either by being more general or by
checking which kind of call we have.
2022-11-07 09:48:53 -08:00
Evan Hahn
a941c82c14
Prefer isEmpty with strings
This change may improve performance slightly but should have no other
user impact.

`myString.isEmpty` is faster than `myString.count == 0` or equivalent,
because computing `count` may require iterating over the string.

I tried to fix all occurrences of this.

Tested this by sending a message in a group and doing a full
re-registration, just in case I broke something there.
2022-11-01 17:53:46 -05:00
Evan Hahn
370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00
Evan Hahn
349681ff85
Attempt to recover corrupted databases
If a user's database is corrupted, we now try to fix it. I recommend
reviewing `DatabaseRecovery` to see how this works, and
`DatabaseRecoveryViewController` for the bulk of the UI.
2022-10-06 16:42:19 -05:00
Sasha Weiss
7fb1da0cec
Migrate ExperienceUpgrade models to SDSCodableModel 2022-10-04 08:52:03 -07:00
Harry
74228f424e
Add StoryContextAssociatedData to compute story tab badge count without hidden stories
* Add StoryContextAssociatedData and db migrations

* Remove hideStory from ThreadAssociatedData

* Remove lastViewedStoryTimestamp and lastReceivedStoryTimestamp from TSThread

* drop deprecated columns in db migration

* add indexes

* dump schema.sql

* Update unviewed stories SQL query

* fix thread fetching for incoming story messages

* reload story tab badge when StoryContextAssociatedData changes

* Add test for TSGroupModel backwards-compatible deserialization

* move db migration

* Only use StoryContextAssociatedData for story badge count

* update StoryContextAssociatedData lastReceivedTime when a story message is deleted

* catch group threads for outgoing story messages too

* clean up sql query

* Only update lastReceievedStoryTimestamp for remote deletions

* add latestUnexpiredTimestamp to StoryContextAssociatedData
2022-10-03 21:16:01 -07:00
Evan Hahn
11c2cf0813
Add basic test for database migration
_This change should have no user impact._

This adds a basic test for database migration, and makes the necessary
changes to make that possible.
2022-09-22 17:24:40 +00:00
Evan Hahn
8a13595143 Prompt for diagnostics if db corruption happens
Previously, users who experienced database corruption would be asked to
submit debug logs. (Special builds had additional options which were not
generally available, so some of this code was already written.)

Now, if they choose to submit debug logs, they will also be asked to
submit a "database diagnostic" before the logs are submitted. This runs
and logs the results of `PRAGMA cipher_provider`, `PRAGMA
cipher_integrity_check`, and whether `PRAGMA quick_check` succeeds. If
they choose to skip the diagnostic, we still submit debug logs.

Other launch failures, such as "out of disk space", do not run these
diagnostics, as they're irrelevant.
2022-07-27 11:57:16 -05:00
george-signal
80565fb43f
Revert "Remove GRDB's memory management. (#4521)" (#4546)
This reverts commit a202f6204e.

It also uses the new facility in GRDB to disable
automatic memory management.

For context see:
https://github.com/groue/GRDB.swift/pull/1253
2022-07-11 14:37:29 -07:00
george-signal
a202f6204e
Remove GRDB's memory management. (#4521)
This disgusting hack removes GRDB's notification
center observers, which prevents it from calling
`DatabasePool.releaseMemory()`.

`releaseMemory` is harmful because it places all
future reads behind a dispatch barrier. The effect
is that even very short reads block on any db
reads that precede the barrier. Some of those can
be very slow, such as orphan message handling.

The practical effect of this barrier is that the
main thread gets stuck on what should be a very
quick operation, such as loading your own avatar
data. iOS can kill the app with 8badf00d.

This change is a temporary workaround. A proper
solution has been added to a PR that I sent to
GRDB, which you can see here:

https://github.com/groue/GRDB.swift/pull/1253

Once that is accepted upstream we should update
GRDB and revert this commit.

This change should fix IOS-2474 and doubtless many
other issues.
2022-07-06 13:27:13 -07:00
Evan Hahn
24ca5abf9c Remove unneeded breaks
This fixes our violations of [the SwiftLint `unneeded_break_in_switch`
rule][0].

[0]: https://realm.github.io/SwiftLint/unneeded_break_in_switch.html
2022-06-03 17:52:28 +00:00
Max Radermacher
80e0208bbb
Time out syncTruncatingCheckpoint after 3 seconds 2022-05-17 12:31:03 -07:00
Jordan Rose
7238e957ec Allow running a SQLite integrity check when the app fails to launch
The results are *not* automatically logged in case they contain
sensitive data. (They shouldn't, but we don't control this output
format.)
2022-04-21 09:47:05 -07:00
Evan Hahn
636b417390 Upgrade GRDB to version 5.23.0
This upgrades [GRDB][0] from v4.3.0 to v5.23.0, the latest version.

I closely followed the [migration guide][1] and stole from a prior
attempt at this upgrade. I also made sure to test the media gallery,
because that allegedly had problems before.

[0]: https://github.com/groue/GRDB.swift
[1]: 6d3f309cad/Documentation/GRDB5MigrationGuide.md
2022-04-20 09:08:50 -05:00
Jordan Rose
a61088b76f Add "Export Database" to the internal settings, for debugging 2022-04-18 17:36:42 -07:00
Jordan Rose
49608044b7 SSK: Remove obsolete OWSMessageDecryptJob
This hasn't been used since before GRDB.
2022-04-11 14:46:43 -07:00
Evan Hahn
8b072620a6 Add "Donation Receipts" view 2022-04-11 16:21:12 -05:00
Dimitris Apostolou
62724cf0be Fix typos 2022-03-18 11:31:06 -07:00
Nora Trapp
059dd9a542 Convert TSMention to SDSCodableModel 2022-03-16 18:07:38 -07:00
Nora Trapp
80d1fce505 Convert TSGroupMember to SDSCodableModel 2022-03-16 18:06:59 -07:00
Nora Trapp
91777d5b9f Convert OWSReaction to SDSCodableModel 2022-03-16 18:06:24 -07:00
Nora Trapp
d7f12d3f39 Convert StoryMessageRecord to use new SDSCodableModel 2022-03-16 17:54:38 -07:00
Nora Trapp
5673b73c3a Add HomeTabBarController 2022-03-16 17:52:41 -07:00
Jordan Rose
19f9f6b7cd SSK: Limit the per-database-connection cache in app extensions
The default cache size for a SQLite connection is 2000KiB, but our
database pool has several reader connections plus a writer connection.
They can't share a cache because that changes SQLite's locking model,
so the next best thing is to limit their caches individually when
we're in a memory-constrained environment. To avoid the caches getting
*too* small, this also removes one of the available readers outside
the main app.
2022-02-01 16:39:25 -08:00
Michelle Linington
5f6ee31e68 Most of badge fetching and persistence
Saving progress before running database codegen
2021-11-09 14:25:42 -08:00
Matthew Chen
153095a500 Reduce statics. 2021-11-02 10:36:25 -03:00