The state has received more than $160 million in funding from federal immigration authorities since 2012, mostly in exchange for keeping and transporting ICE detainees in jails run by four Massachusetts sheriff’s departments, a Globe review has found.

The sum, brought into the state’s coffers through controversial contracts with US Immigration and Customs Enforcement, has raised the eyebrows of some advocates and immigration attorneys who oppose the agreements and think there are better alternatives.

The sheriff’s offices, meanwhile, have defended the arrangements, with at least two departments saying their relationship with ICE has made Massachusetts residents safer.