kaist-cp/cs431

[Question] Hazard Pointers: is it safe to protect a pointer to invalid memory?

BraSDon opened this issue · 5 comments

In the HP-protected Treiber's Stack example on slide 6 of the SMR slides, we call protect() on cur. However cur might be pointing to invalid memory. Is it still safe to execute protect(cur)?
If yes, why does it say "unsafe protect()" on slide 7.
If no, wouldn't the example be wrong?

Let me illustrate with an execution trace, where we start with a single node in the stack.

T1
(1) L11-15: execute pop() up to and including line 15
(3) L16: protect(cur)--> protect on a pointer to invalid memory

T2:
(2) L11-22: execute pop() in its entirety, therefore potentially free'ing the node

What the slide is saying that without protection, the access may be unsafe.

Now even if you did protection, the pointer may have been freed between the time you obtained it and called protect(cur). We perform validation to ensure that this does not happen.

Hence we can call protect on an already freed pointer safely? I.e. the protect method doesn't dereference the pointer
Is this: protect(null) also safe then?

protect(null) itself is safe (hence in the homework Shield::protect() is a safe function) what isn't safe is dereference of a null pointer or protected pointer without validation. This is precisely why we validate the pointer and also check if its null.

Okay, so protecting a pointer to invalid memory is safe. Then why does it say "unsafe protect()" on slide 7?

Ah, so the situation in slide 7 is a case where the dereference is unsafe even if validation succeeds. That's why its saying "unsafe protect()".