The Prophet was protected from falling into Kufr and Shirk before and after he received Revelation (salallāh ‘alaihi wasallam). From the proofs for that is the hadeeth of Anas ibn Mālik (radiyallāhu ‘anhu):
أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم أَتَاهُ جِبْرِيلُ صلى الله عليه وسلم وَهُوَ يَلْعَبُ مَعَ الْغِلْمَانِ فَأَخَذَهُ فَصَرَعَهُ فَشَقَّ عَنْ قَلْبِهِ فَاسْتَخْرَجَ الْقَلْبَ فَاسْتَخْرَجَ مِنْهُ عَلَقَةً فَقَالَ هَذَا حَظُّ الشَّيْطَانِ مِنْكَ . ثُمَّ غَسَلَهُ فِي طَسْتٍ مِنْ ذَهَبٍ بِمَاءِ زَمْزَمَ ثُمَّ لأَمَهُ ثُمَّ أَعَادَهُ فِي مَكَانِهِ وَجَاءَ الْغِلْمَانُ يَسْعَوْنَ إِلَى أُمِّهِ – يَعْنِي ظِئْرَهُ – فَقَالُوا إِنَّ مُحَمَّدًا قَدْ قُتِلَ . فَاسْتَقْبَلُوهُ وَهُوَ مُنْتَقَعُ اللَّوْنِ . قَالَ أَنَسٌ وَقَدْ كُنْتُ أَرَى أَثَرَ ذَلِكَ الْمِخْيَطِ فِي صَدْرِهِ
“Jibreel came to the Messenger of Allah (salallāh ‘alaihi wasallam) while he was playing with two boys. He took hold of him and lay him on the ground and split open his chest and took out his heart, and then extracted a black clot out of it and said: “That was the share of Satan in you. Then he washed it with the water of Zamzam in a gold vessel and then it was mended and restored to its place. The boys sent running to the Prophet’s foster-mother (his wet-nurse who breastfed him) and called out: “Muhammad has been killed”. So, they all rushed to him alive but his colour had changed due to the experience.” Anas said, “I saw for myself the marks of the needle stitching on his chest.” (Muslim, no. 162) Anas (radiyallāhu ‘anhu) saw those marks over 50 years after the event occurred.
So this hadeeth is a proof that Jibreel removed the share of Shaytān from the Messenger (salallāh ‘alaihi wasallam), purified his heart such that Shaytān was never able to mislead the Messenger (salallāh ‘alaihi wasallam) – he had no way of approaching him. And this is evidence that he was purified and protected from Shirk (all forms of polytheism) since he was a young child.
Al-Jarjānī  and Al-Āmidī  have cited consensus (ijmā’) upon the fact that all of the Prophets of Allah, peace and blessings be upon them, were protected from shirk and kufr.
 See Sharhul-Mawāqif, p. 134.
 See Al-Ihkām fī Usūlil-Ahkām, 1/128.