lspitzner / lspitzner/pqueue

Type safety issue?

Open
#124 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
17
Forks
12
PR merge metrics
No merged PRs in 30d

Description

The fake GADT we now use for maps and unordered traversals and folds is a little bit unsafe. I thought that we were well protected by the fact that the queue would have to reach `O(2^WORDSIZE)` elements to cause a problem, but it's actually possible to do this in a reasonable amount of time on systems with 32-bit `Word` but addressable memory considerably exceeding $2^32$ words, if there are any such around. In particular, an adversary could calculate something like `stimes (2^(2^32)) (singleton () ())`. This will take quite a long time, and something in excess of 64 gigabytes of memory, but I think it's possible in principle.

Suppose someone uses `traverseU` on this monstrosity, with a `Backwards` applicative. I suspect they will be able to actually reach the point where underflow occurs, leading to an unsafe coercion and memory fault.

----

The easiest solution is to use `Word64` instead of `Word`. I don't think it's realistic to produce a queue whose size exceeds $2^{2^{64}}$ by any means whatsoever. The downside is that this will hurt performance on 32-bit systems. I don't know how much we care.

The other obvious solution is to make sure we never underflow. The best way to do that is probably to perform a size check on `insert` and on `union`. This has the side benefit of ensuring that `size` always produces a correct value. The downside of course is that insertion becomes ever so slightly slower.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the fake GADT queue representation and the traverseU path with the Backwards applicative. Compare the proposed Word64 approach with overflow checks in insert and union; done means preventing underflow and unsafe coercion while keeping size correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.