Let me quote this:
WPA World Standarized Rules
Definitions:
8.5 Driven off the Table
A ball is considered driven off the table if it comes to rest other than on the playing surface but is not pocketed. A ball is also considered driven off the table if it would have been driven off the table except for striking an object such as a light fixture, piece of chalk or a player which causes it to return to the table.
A ball that contacts the top of the rail is not considered to have been driven off the table if it returns to the playing surface or enters a pocket.
and also
8.1 Parts of the Table
... The table is comprised of rails, cushions, a playing surface and pockets....
In other words:
If the ball leaves the playing surface (table bed) but returns and sets on the playing surface (or in the pocket) and in the course of that action does not contact anything that is not a part of the table (cushions, rails, etc.) it is not a foul. However; in the instance that the ball rolling on the top pf the rail contacts anything (like the piece of chalk lying there) it is instantly a foul shot, even if the ball sets back on table bed afterwards.
Hope it helps.
Regards