Hundreds of thousands of foreign workers in the world's top oil exporter are trying to rectify their immigration status or leave the country before July 3, when the government will resume a crackdown on illegal labor.

Workers have queued outside government offices and some consulates for days on end over recent weeks in daytime temperatures topping 40 degrees centigrade, often sleeping outside the offices to keep their place in line.

Local media have reported people attempting to resolve their status have hit a string of bureaucratic obstacles as government offices and expatriates consulates are overwhelmed.

Jeddah police spokesman Nawaf al-Booq said protesters had entered the consulate, causing a stampede in which some people suffered mild injuries. He said protesters had peacefully dispersed and a fire outside the consulate had been put out.

"Workers were throwing stones and water bottles at police, who shot into the air," said a witness who did not want to be named.

Films posted online and verified by witnesses showed a fire against the wall of the consulate. Other pictures showed men with scarves over their faces setting alight plastic barriers next to the wall.

Later on Sunday hundreds of Indonesians were still waiting near the building peacefully, some of them apparently staying in a nearby empty lot where 20-30 tents had been set up, a Reuters witness said. Food was being distributed and people were selling bottles of water and other goods. There were few police present.

Saudi Arabia is cracking down on expatriates working illegally as part of wide ranging labor reforms aimed at pushing more local people into private sector jobs.

The latest central bank figures, from 2011, showed that around nine tenths of all jobs for private companies were held by some of the roughly nine million foreigners living in the country. Saudis enjoy greater job protection than foreigners and often expect more pay.

The labor reforms have imposed a series of localization quotas backed up with fines and hiring bans, and have levied an annual 2,400 riyals ($640) fee that firms must pay for every foreigner they employ above the number of their Saudi staff.

Under Saudi law, foreigners must be "sponsored" by their employer and can only work in the profession listed on their residency papers. In practice, many foreigners find other jobs without changing their residency status.