A gamma-ray source and detectors on a pad against the borehole wall measure electron density, which is converted to bulk density in grams per cubic centimetre. Porosity is the gap between this reading and the density of the solid minerals, after an assumption for the fluid density. A wrong matrix, quartz instead of calcite, shifts the porosity.
The pad has to touch the wall. A washed-out hole, or heavy barite mud sitting between the pad and the rock, biases the reading. Gas in the pores makes the density look light, which is half of the density-neutron gas crossover.