Skip to content

Commit

Permalink
LibSQL: Fix BTree corruption in TreeNode::split
Browse files Browse the repository at this point in the history
After splitting a node, the new node was written to the same pointer as
the current node - probably a copy / paste error. This new code requires
a `.pointer() -> u32` to exist on the object to be serialized,
preventing this issue from happening again.

Fixes SerenityOS#15844.
  • Loading branch information
gmta authored and awesomekling committed Nov 26, 2022
1 parent e5e00a6 commit 70a7bca
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
19 changes: 19 additions & 0 deletions Tests/LibSQL/TestSqlStatementExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,23 @@ TEST_CASE(binary_operator_failure)
expect_failure(move(result), '&');
}

TEST_CASE(describe_large_table_after_persist)
{
ScopeGuard guard([]() { unlink(db_name); });
{
auto database = SQL::Database::construct(db_name);
EXPECT(!database->open().is_error());

auto result = execute(database, "CREATE TABLE Cookies ( name TEXT, value TEXT, same_site INTEGER, creation_time INTEGER, last_access_time INTEGER, expiry_time INTEGER, domain TEXT, path TEXT, secure INTEGER, http_only INTEGER, host_only INTEGER, persistent INTEGER );");
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
}
{
auto database = SQL::Database::construct(db_name);
EXPECT(!database->open().is_error());

auto result = execute(database, "DESCRIBE TABLE Cookies;");
EXPECT_EQ(result.size(), 12u);
}
}

}
2 changes: 1 addition & 1 deletion Userland/Libraries/LibSQL/BTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TreeNode* BTree::new_root()
{
set_pointer(new_record_pointer());
m_root = make<TreeNode>(*this, nullptr, m_root.leak_ptr(), pointer());
serializer().serialize_and_write(*m_root.ptr(), m_root->pointer());
serializer().serialize_and_write(*m_root.ptr());
if (on_new_root)
on_new_root();
return m_root;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibSQL/BTreeIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ bool BTreeIterator::update(Key const& new_value)

// We are friend of BTree and TreeNode. Don't know how I feel about that.
m_current->m_entries[m_index] = new_value;
m_current->tree().serializer().serialize_and_write(*m_current, m_current->pointer());
m_current->tree().serializer().serialize_and_write(*m_current);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibSQL/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ ErrorOr<void> Database::update(Row& tuple)
VERIFY(m_table_cache.get(tuple.table()->key().hash()).has_value());
// TODO Check constraints
m_serializer.reset();
m_serializer.serialize_and_write<Tuple>(tuple, tuple.pointer());
m_serializer.serialize_and_write<Tuple>(tuple);

// TODO update indexes defined on table.
return {};
Expand Down
12 changes: 6 additions & 6 deletions Userland/Libraries/LibSQL/HashIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool HashBucket::insert(Key const& key)
return false;
}
m_entries.append(key);
m_hash_index.serializer().serialize_and_write(*this, pointer());
m_hash_index.serializer().serialize_and_write(*this);
return true;
}

Expand Down Expand Up @@ -226,10 +226,10 @@ HashIndex::HashIndex(Serializer& serializer, NonnullRefPtr<TupleDescriptor> cons
} else {
auto bucket = append_bucket(0u, 1u, new_record_pointer());
bucket->m_inflated = true;
serializer.serialize_and_write(*bucket, bucket->pointer());
serializer.serialize_and_write(*bucket);
bucket = append_bucket(1u, 1u, new_record_pointer());
bucket->m_inflated = true;
serializer.serialize_and_write(*bucket, bucket->pointer());
serializer.serialize_and_write(*bucket);
m_nodes.append(first_node);
write_directory_to_write_ahead_log();
}
Expand Down Expand Up @@ -283,7 +283,7 @@ HashBucket* HashIndex::get_bucket_for_insert(Key const& key)
}
if (moved > 0) {
dbgln_if(SQL_DEBUG, "Moved {} entries from bucket #{} to #{}", moved, base_index, ix);
serializer().serialize_and_write(*sub_bucket, sub_bucket->pointer());
serializer().serialize_and_write(*sub_bucket);
}
total_moved += moved;
}
Expand All @@ -292,7 +292,7 @@ HashBucket* HashIndex::get_bucket_for_insert(Key const& key)
else
dbgln_if(SQL_DEBUG, "Nothing redistributed from bucket #{}", base_index);
bucket->set_local_depth(bucket->local_depth() + 1);
serializer().serialize_and_write(*bucket, bucket->pointer());
serializer().serialize_and_write(*bucket);
write_directory_to_write_ahead_log();

auto bucket_after_redistribution = get_bucket(key_hash % size());
Expand Down Expand Up @@ -327,7 +327,7 @@ void HashIndex::write_directory_to_write_ahead_log()
size_t num_node = 0u;
while (offset < size()) {
HashDirectoryNode node(*this, num_node, offset);
serializer().serialize_and_write(node, node.pointer());
serializer().serialize_and_write(node);
offset += node.number_of_pointers();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibSQL/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ class Serializer {
void serialize(String const&);

template<typename T>
bool serialize_and_write(T const& t, u32 pointer)
bool serialize_and_write(T const& t)
{
VERIFY(m_heap.ptr() != nullptr);
reset();
serialize<T>(t);
m_heap->add_to_wal(pointer, m_buffer);
m_heap->add_to_wal(t.pointer(), m_buffer);
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions Userland/Libraries/LibSQL/TreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ bool TreeNode::update_key_pointer(Key const& key)
if (m_entries[ix].pointer() != key.pointer()) {
m_entries[ix].set_pointer(key.pointer());
dump_if(SQL_DEBUG, "To WAL");
tree().serializer().serialize_and_write<TreeNode>(*this, pointer());
tree().serializer().serialize_and_write<TreeNode>(*this);
}
return true;
}
Expand Down Expand Up @@ -277,7 +277,7 @@ void TreeNode::just_insert(Key const& key, TreeNode* right)
split();
} else {
dump_if(SQL_DEBUG, "To WAL");
tree().serializer().serialize_and_write(*this, pointer());
tree().serializer().serialize_and_write(*this);
}
return;
}
Expand All @@ -289,7 +289,7 @@ void TreeNode::just_insert(Key const& key, TreeNode* right)
split();
} else {
dump_if(SQL_DEBUG, "To WAL");
tree().serializer().serialize_and_write(*this, pointer());
tree().serializer().serialize_and_write(*this);
}
}

Expand Down Expand Up @@ -327,9 +327,9 @@ void TreeNode::split()
auto median = m_entries.take_last();

dump_if(SQL_DEBUG, "Split Left To WAL");
tree().serializer().serialize_and_write(*this, pointer());
tree().serializer().serialize_and_write(*this);
new_node->dump_if(SQL_DEBUG, "Split Right to WAL");
tree().serializer().serialize_and_write(*new_node, pointer());
tree().serializer().serialize_and_write(*new_node);

m_up->just_insert(median, new_node);
}
Expand Down

0 comments on commit 70a7bca

Please sign in to comment.