Fix: (1) Windows stack overflow due to buffer too large in peer deserialize, (2) clean up some other stuff seen during debugging and reduce the sizes of some buffers due to Windows small stack size, (3) remove a redundant try/catch.

This commit is contained in:
Adam Ierymenko 2015-11-13 12:14:28 -08:00
parent 90f9415107
commit 0d9f33dc4f
12 changed files with 103 additions and 97 deletions

View File

@ -58,7 +58,12 @@ class AntiRecursion
public: public:
AntiRecursion() AntiRecursion()
{ {
memset(_history,0,sizeof(_history)); for(int i=0;i<ZT_ANTIRECURSION_HISTORY_SIZE;++i) {
_history[i].tail[0] = 0;
_history[i].tail[1] = 0;
_history[i].tail[2] = 0;
_history[i].tail[3] = 0;
}
_ptr = 0; _ptr = 0;
} }

View File

@ -628,7 +628,7 @@ void Cluster::doPeriodicTasks()
} }
alive.append((uint64_t)now); alive.append((uint64_t)now);
alive.append((uint64_t)0); // TODO: compute and send load average alive.append((uint64_t)0); // TODO: compute and send load average
alive.append((uint64_t)RR->topology->countActive()); alive.append((uint64_t)RR->topology->countActive(now));
alive.append((uint64_t)0); // unused/reserved flags alive.append((uint64_t)0); // unused/reserved flags
alive.append((uint8_t)_zeroTierPhysicalEndpoints.size()); alive.append((uint8_t)_zeroTierPhysicalEndpoints.size());
for(std::vector<InetAddress>::const_iterator pe(_zeroTierPhysicalEndpoints.begin());pe!=_zeroTierPhysicalEndpoints.end();++pe) for(std::vector<InetAddress>::const_iterator pe(_zeroTierPhysicalEndpoints.begin());pe!=_zeroTierPhysicalEndpoints.end();++pe)
@ -769,7 +769,7 @@ void Cluster::status(ZT_ClusterStatus &status) const
s->y = _y; s->y = _y;
s->z = _z; s->z = _z;
s->load = 0; // TODO s->load = 0; // TODO
s->peers = RR->topology->countActive(); s->peers = RR->topology->countActive(now);
for(std::vector<InetAddress>::const_iterator ep(_zeroTierPhysicalEndpoints.begin());ep!=_zeroTierPhysicalEndpoints.end();++ep) { for(std::vector<InetAddress>::const_iterator ep(_zeroTierPhysicalEndpoints.begin());ep!=_zeroTierPhysicalEndpoints.end();++ep) {
if (s->numZeroTierPhysicalEndpoints >= ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES) // sanity check if (s->numZeroTierPhysicalEndpoints >= ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES) // sanity check
break; break;

View File

@ -158,7 +158,7 @@ bool Identity::fromString(const char *str)
return false; return false;
char *saveptr = (char *)0; char *saveptr = (char *)0;
char tmp[4096]; char tmp[1024];
if (!Utils::scopy(tmp,sizeof(tmp),str)) if (!Utils::scopy(tmp,sizeof(tmp),str))
return false; return false;

View File

@ -93,21 +93,22 @@ Node::Node(
_prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream)); _prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream));
} }
std::string idtmp(dataStoreGet("identity.secret")); {
if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) { std::string idtmp(dataStoreGet("identity.secret"));
TRACE("identity.secret not found, generating..."); if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
RR->identity.generate(); TRACE("identity.secret not found, generating...");
idtmp = RR->identity.toString(true); RR->identity.generate();
if (!dataStorePut("identity.secret",idtmp,true)) idtmp = RR->identity.toString(true);
throw std::runtime_error("unable to write identity.secret"); if (!dataStorePut("identity.secret",idtmp,true))
} throw std::runtime_error("unable to write identity.secret");
RR->publicIdentityStr = RR->identity.toString(false); }
RR->secretIdentityStr = RR->identity.toString(true); RR->publicIdentityStr = RR->identity.toString(false);
RR->secretIdentityStr = RR->identity.toString(true);
idtmp = dataStoreGet("identity.public"); idtmp = dataStoreGet("identity.public");
if (idtmp != RR->publicIdentityStr) { if (idtmp != RR->publicIdentityStr) {
if (!dataStorePut("identity.public",RR->publicIdentityStr,false)) if (!dataStorePut("identity.public",RR->publicIdentityStr,false))
throw std::runtime_error("unable to write identity.public"); throw std::runtime_error("unable to write identity.public");
}
} }
try { try {
@ -662,7 +663,7 @@ void Node::backgroundThreadMain()
std::string Node::dataStoreGet(const char *name) std::string Node::dataStoreGet(const char *name)
{ {
char buf[16384]; char buf[1024];
std::string r; std::string r;
unsigned long olen = 0; unsigned long olen = 0;
do { do {

View File

@ -50,6 +50,7 @@ Topology::Topology(const RuntimeEnvironment *renv) :
const uint8_t *all = reinterpret_cast<const uint8_t *>(alls.data()); const uint8_t *all = reinterpret_cast<const uint8_t *>(alls.data());
RR->node->dataStoreDelete("peers.save"); RR->node->dataStoreDelete("peers.save");
Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE> *deserializeBuf = new Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>();
unsigned int ptr = 0; unsigned int ptr = 0;
while ((ptr + 4) < alls.size()) { while ((ptr + 4) < alls.size()) {
try { try {
@ -60,7 +61,8 @@ Topology::Topology(const RuntimeEnvironment *renv) :
(((unsigned int)all[ptr + 3]) & 0xff) (((unsigned int)all[ptr + 3]) & 0xff)
); );
unsigned int pos = 0; unsigned int pos = 0;
SharedPtr<Peer> p(Peer::deserializeNew(RR->identity,Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>(all + ptr,reclen + 4),pos)); deserializeBuf->copyFrom(all + ptr,reclen + 4);
SharedPtr<Peer> p(Peer::deserializeNew(RR->identity,*deserializeBuf,pos));
ptr += pos; ptr += pos;
if (!p) if (!p)
break; // stop if invalid records break; // stop if invalid records
@ -70,16 +72,19 @@ Topology::Topology(const RuntimeEnvironment *renv) :
break; // stop if invalid records break; // stop if invalid records
} }
} }
delete deserializeBuf;
clean(RR->node->now()); clean(RR->node->now());
std::string dsWorld(RR->node->dataStoreGet("world")); std::string dsWorld(RR->node->dataStoreGet("world"));
World cachedWorld; World cachedWorld;
try { if (dsWorld.length() > 0) {
Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp(dsWorld.data(),(unsigned int)dsWorld.length()); try {
cachedWorld.deserialize(dswtmp,0); Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp(dsWorld.data(),(unsigned int)dsWorld.length());
} catch ( ... ) { cachedWorld.deserialize(dswtmp,0);
cachedWorld = World(); // clear if cached world is invalid } catch ( ... ) {
cachedWorld = World(); // clear if cached world is invalid
}
} }
World defaultWorld; World defaultWorld;
{ {
@ -315,20 +320,6 @@ void Topology::clean(uint64_t now)
} }
} }
unsigned long Topology::countActive() const
{
const uint64_t now = RR->node->now();
unsigned long cnt = 0;
Mutex::Lock _l(_lock);
Hashtable< Address,SharedPtr<Peer> >::Iterator i(const_cast<Topology *>(this)->_peers);
Address *a = (Address *)0;
SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
while (i.next(a,p)) {
cnt += (unsigned long)((*p)->hasActiveDirectPath(now));
}
return cnt;
}
Identity Topology::_getIdentity(const Address &zta) Identity Topology::_getIdentity(const Address &zta)
{ {
char p[128]; char p[128];

View File

@ -200,9 +200,21 @@ public:
void clean(uint64_t now); void clean(uint64_t now);
/** /**
* @param now Current time
* @return Number of peers with active direct paths * @return Number of peers with active direct paths
*/ */
unsigned long countActive() const; inline unsigned long countActive(uint64_t now) const
{
unsigned long cnt = 0;
Mutex::Lock _l(_lock);
Hashtable< Address,SharedPtr<Peer> >::Iterator i(const_cast<Topology *>(this)->_peers);
Address *a = (Address *)0;
SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
while (i.next(a,p)) {
cnt += (unsigned long)((*p)->hasActiveDirectPath(now));
}
return cnt;
}
/** /**
* Apply a function or function object to all peers * Apply a function or function object to all peers
@ -253,7 +265,7 @@ private:
Identity _getIdentity(const Address &zta); Identity _getIdentity(const Address &zta);
void _setWorld(const World &newWorld); void _setWorld(const World &newWorld);
const RuntimeEnvironment *RR; const RuntimeEnvironment *const RR;
World _world; World _world;
Hashtable< Address,SharedPtr<Peer> > _peers; Hashtable< Address,SharedPtr<Peer> > _peers;

60
one.cpp
View File

@ -958,13 +958,15 @@ int main(int argc,char **argv)
#endif // __UNIX_LIKE__ #endif // __UNIX_LIKE__
#ifdef __WINDOWS__ #ifdef __WINDOWS__
WSADATA wsaData; {
WSAStartup(MAKEWORD(2,2),&wsaData); WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
}
#ifdef ZT_WIN_RUN_IN_CONSOLE #ifdef ZT_WIN_RUN_IN_CONSOLE
bool winRunFromCommandLine = true; bool winRunFromCommandLine = true;
#else #else
bool winRunFromCommandLine = false; bool winRunFromCommandLine = true;
#endif #endif
#endif // __WINDOWS__ #endif // __WINDOWS__
@ -1153,37 +1155,29 @@ int main(int argc,char **argv)
unsigned int returnValue = 0; unsigned int returnValue = 0;
try { for(;;) {
for(;;) { zt1Service = OneService::newInstance(homeDir.c_str(),port);
zt1Service = OneService::newInstance(homeDir.c_str(),port); switch(zt1Service->run()) {
switch(zt1Service->run()) { case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done case OneService::ONE_NORMAL_TERMINATION:
case OneService::ONE_NORMAL_TERMINATION: break;
break; case OneService::ONE_UNRECOVERABLE_ERROR:
case OneService::ONE_UNRECOVERABLE_ERROR: fprintf(stderr,"%s: fatal error: %s"ZT_EOL_S,argv[0],zt1Service->fatalErrorMessage().c_str());
fprintf(stderr,"%s: fatal error: %s"ZT_EOL_S,argv[0],zt1Service->fatalErrorMessage().c_str()); returnValue = 1;
returnValue = 1; break;
break; case OneService::ONE_IDENTITY_COLLISION: {
case OneService::ONE_IDENTITY_COLLISION: { delete zt1Service;
delete zt1Service; zt1Service = (OneService *)0;
zt1Service = (OneService *)0; std::string oldid;
std::string oldid; OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid); if (oldid.length()) {
if (oldid.length()) { OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid); OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str()); OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str()); }
} } continue; // restart!
} continue; // restart!
}
break; // terminate loop -- normally we don't keep restarting
} }
} catch (std::exception &exc) { break; // terminate loop -- normally we don't keep restarting
fprintf(stderr,"%s: fatal error: %s"ZT_EOL_S,argv[0],exc.what());
returnValue = 1;
} catch ( ... ) {
fprintf(stderr,"%s: fatal error: unknown exception"ZT_EOL_S,argv[0]);
returnValue = 1;
} }
delete zt1Service; delete zt1Service;

View File

@ -206,7 +206,7 @@ skip_add_inetaddr:
bool OSUtils::readFile(const char *path,std::string &buf) bool OSUtils::readFile(const char *path,std::string &buf)
{ {
char tmp[4096]; char tmp[1024];
FILE *f = fopen(path,"rb"); FILE *f = fopen(path,"rb");
if (f) { if (f) {
for(;;) { for(;;) {

View File

@ -213,7 +213,7 @@ std::string WindowsEthernetTap::addNewPersistentTapDevice(const char *pathToInf,
Mutex::Lock _l(_systemDeviceManagementLock); Mutex::Lock _l(_systemDeviceManagementLock);
GUID classGuid; GUID classGuid;
char className[4096]; char className[1024];
if (!WINENV.SetupDiGetINFClassA(pathToInf,&classGuid,className,sizeof(className),(PDWORD)0)) { if (!WINENV.SetupDiGetINFClassA(pathToInf,&classGuid,className,sizeof(className),(PDWORD)0)) {
return std::string("SetupDiGetINFClassA() failed -- unable to read zttap driver INF file"); return std::string("SetupDiGetINFClassA() failed -- unable to read zttap driver INF file");
} }
@ -269,9 +269,9 @@ std::string WindowsEthernetTap::addNewPersistentTapDevice(const char *pathToInf,
std::string WindowsEthernetTap::destroyAllLegacyPersistentTapDevices() std::string WindowsEthernetTap::destroyAllLegacyPersistentTapDevices()
{ {
char subkeyName[4096]; char subkeyName[1024];
char subkeyClass[4096]; char subkeyClass[1024];
char data[4096]; char data[1024];
std::set<std::string> instanceIdPathsToRemove; std::set<std::string> instanceIdPathsToRemove;
{ {
@ -321,9 +321,9 @@ std::string WindowsEthernetTap::destroyAllLegacyPersistentTapDevices()
std::string WindowsEthernetTap::destroyAllPersistentTapDevices() std::string WindowsEthernetTap::destroyAllPersistentTapDevices()
{ {
char subkeyName[4096]; char subkeyName[1024];
char subkeyClass[4096]; char subkeyClass[1024];
char data[4096]; char data[1024];
std::set<std::string> instanceIdPathsToRemove; std::set<std::string> instanceIdPathsToRemove;
{ {
@ -479,9 +479,9 @@ WindowsEthernetTap::WindowsEthernetTap(
_initialized(false), _initialized(false),
_enabled(true) _enabled(true)
{ {
char subkeyName[4096]; char subkeyName[1024];
char subkeyClass[4096]; char subkeyClass[1024];
char data[4096]; char data[1024];
char tag[24]; char tag[24];
std::string mySubkeyName; std::string mySubkeyName;

View File

@ -860,8 +860,9 @@ struct TestPhyHandlers
inline void phyOnUnixClose(PhySocket *sock,void **uptr) {} inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {} inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {} inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {}
inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
#endif // __UNIX_LIKE__ #endif // __UNIX_LIKE__
inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
}; };
static int testPhy() static int testPhy()
{ {

View File

@ -24,6 +24,7 @@
<ClCompile Include="..\..\ext\lz4\lz4.c" /> <ClCompile Include="..\..\ext\lz4\lz4.c" />
<ClCompile Include="..\..\node\C25519.cpp" /> <ClCompile Include="..\..\node\C25519.cpp" />
<ClCompile Include="..\..\node\CertificateOfMembership.cpp" /> <ClCompile Include="..\..\node\CertificateOfMembership.cpp" />
<ClCompile Include="..\..\node\Cluster.cpp" />
<ClCompile Include="..\..\node\DeferredPackets.cpp" /> <ClCompile Include="..\..\node\DeferredPackets.cpp" />
<ClCompile Include="..\..\node\Dictionary.cpp" /> <ClCompile Include="..\..\node\Dictionary.cpp" />
<ClCompile Include="..\..\node\Identity.cpp" /> <ClCompile Include="..\..\node\Identity.cpp" />
@ -44,18 +45,14 @@
<ClCompile Include="..\..\node\Switch.cpp" /> <ClCompile Include="..\..\node\Switch.cpp" />
<ClCompile Include="..\..\node\Topology.cpp" /> <ClCompile Include="..\..\node\Topology.cpp" />
<ClCompile Include="..\..\node\Utils.cpp" /> <ClCompile Include="..\..\node\Utils.cpp" />
<ClCompile Include="..\..\one.cpp" /> <ClCompile Include="..\..\one.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\osdep\BackgroundResolver.cpp" /> <ClCompile Include="..\..\osdep\BackgroundResolver.cpp" />
<ClCompile Include="..\..\osdep\Http.cpp" /> <ClCompile Include="..\..\osdep\Http.cpp" />
<ClCompile Include="..\..\osdep\OSUtils.cpp" /> <ClCompile Include="..\..\osdep\OSUtils.cpp" />
<ClCompile Include="..\..\osdep\UPNPClient.cpp" /> <ClCompile Include="..\..\osdep\UPNPClient.cpp" />
<ClCompile Include="..\..\osdep\WindowsEthernetTap.cpp" /> <ClCompile Include="..\..\osdep\WindowsEthernetTap.cpp" />
<ClCompile Include="..\..\selftest.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\service\ControlPlane.cpp" /> <ClCompile Include="..\..\service\ControlPlane.cpp" />
<ClCompile Include="..\..\service\OneService.cpp" /> <ClCompile Include="..\..\service\OneService.cpp" />
<ClCompile Include="ServiceBase.cpp" /> <ClCompile Include="ServiceBase.cpp" />
@ -123,6 +120,7 @@
<ClInclude Include="..\..\node\Switch.hpp" /> <ClInclude Include="..\..\node\Switch.hpp" />
<ClInclude Include="..\..\node\Topology.hpp" /> <ClInclude Include="..\..\node\Topology.hpp" />
<ClInclude Include="..\..\node\Utils.hpp" /> <ClInclude Include="..\..\node\Utils.hpp" />
<ClInclude Include="..\..\node\World.hpp" />
<ClInclude Include="..\..\osdep\BackgroundResolver.hpp" /> <ClInclude Include="..\..\osdep\BackgroundResolver.hpp" />
<ClInclude Include="..\..\osdep\Http.hpp" /> <ClInclude Include="..\..\osdep\Http.hpp" />
<ClInclude Include="..\..\osdep\OSUtils.hpp" /> <ClInclude Include="..\..\osdep\OSUtils.hpp" />
@ -232,6 +230,7 @@
<AdditionalIncludeDirectories> <AdditionalIncludeDirectories>
</AdditionalIncludeDirectories> </AdditionalIncludeDirectories>
<PreprocessorDefinitions>NOMINMAX;ZT_TRACE;ZT_USE_MINIUPNPC;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NOMINMAX;ZT_TRACE;ZT_USE_MINIUPNPC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>false</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>

View File

@ -96,9 +96,6 @@
<ClCompile Include="..\..\osdep\OSUtils.cpp"> <ClCompile Include="..\..\osdep\OSUtils.cpp">
<Filter>Source Files\osdep</Filter> <Filter>Source Files\osdep</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\selftest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\node\C25519.cpp"> <ClCompile Include="..\..\node\C25519.cpp">
<Filter>Source Files\node</Filter> <Filter>Source Files\node</Filter>
</ClCompile> </ClCompile>
@ -192,6 +189,9 @@
<ClCompile Include="..\..\node\DeferredPackets.cpp"> <ClCompile Include="..\..\node\DeferredPackets.cpp">
<Filter>Source Files\node</Filter> <Filter>Source Files\node</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\node\Cluster.cpp">
<Filter>Source Files\node</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="resource.h"> <ClInclude Include="resource.h">
@ -419,6 +419,9 @@
<ClInclude Include="..\..\node\DeferredPackets.hpp"> <ClInclude Include="..\..\node\DeferredPackets.hpp">
<Filter>Header Files\node</Filter> <Filter>Header Files\node</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\node\World.hpp">
<Filter>Header Files\node</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="ZeroTierOne.rc"> <ResourceCompile Include="ZeroTierOne.rc">