The author of the excellent book where I found this problem (The Lazy Universe) explains in another part of the book:
A surprisingly tricky example is the case of a sliding block which is
pushed across a table-top by a force, say, pushed by your finger (we
ignore friction). The displacement of the block is anywhere on the
surface whereas the reaction-force acts at right-angles to this
surface preventing the block from burrowing down into the table. So
far, this makes sense. But, hang on, there is also a reaction against
your finger, from the block, and this reaction is in line with the
block’s displacement. The trick is to appreciate that the block’s
displacement due to the finger-push is an actual, not a virtual,
displacement. We can hypothetically freeze the block (switch to a
different reference frame) and get rid of the distraction of its
actual motion. Then we realize that the finger can’t depress the block
as if it were so much sponge-cake, as there is a reaction-force of the
block against the finger. However, the finger is still allowed, infinitesimally, virtually, to move within the back
face of the block, at right-angles to this reaction-force. This is a
general result: for any virtual displacement, being ‘harmonious’ is
the same thing as being in a direction perpendicular to the reaction
I feel this is the answer to my question, but I can't say I understand how this translates to the Atwood machine problem. This is my guess:
The important thing is not to confuse virtual displacements with actual displacements. Sometimes, the actual physical displacements cannot be chosen as virtual displacements. Virtual displacements occur without the passage of time. If we think of the example above, while time is frozen the finger cannot actually move in the direction the block moves when time is flowing. For it do so, it would have to compress the block, but the block is inelastic. So that's not an allowed virtual displacement. But with things frozen in time, the finger is allowed to move within the face of the block (like up and down across the face), and this motion is at right-angles to the reaction (constraint) force from the block on the finger.
As @alephzero stated in another answer, the real constraint in the Atwood machine "is that the string has constant length". This means that, like the block in the previous example, the string can't be compressed. Therefore, if we freeze time, we'll find that we can't take the virtual displacement of the blocks to be the same as the actual displacement, i.e, "up and down", i.e, in the direction of the strings. For them to do so with time frozen they would have to compress the strings, and the strings can't be compressed. So we are once again "getting distracted by the actual motions". This is another case where we can't take the actual displacements as the virtual displacements. The virtual displacements allowed for the blocks are actually at right angles to the string, not up and down, but sideways!
Am I onto something?