Skip to content

Commit

Permalink
fix(sharedMemory): x64 size error #2976 (#4295)
Browse files Browse the repository at this point in the history
* fix(sharedMemory): x64 size error #2976

* chore: add Util dependency to Prometheus samples

* fix(HTTPClientSession): not working with UNIX_LOCAL SocketAddress #2578

* fix(WebSocketTest): supress connection reset exception assertion

* fix(PollSet): wait on premature epoll_wait return; reinforce tests for windows

* fix(build): add DataTest dependency to Makefile

* fix(Task): intermittently hanging test and some other improvements

* fix(Net): PollSet loop; suppress test WebSocket handler shutdown IOExeption
  • Loading branch information
aleks-f authored Nov 26, 2023
1 parent 70bb3a4 commit 11de403
Show file tree
Hide file tree
Showing 24 changed files with 244 additions and 104 deletions.
10 changes: 5 additions & 5 deletions Foundation/include/Poco/SharedMemory_WIN32.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class Foundation_API SharedMemoryImpl: public RefCountedObject
SharedMemoryImpl& operator = (const SharedMemoryImpl&);

std::string _name;
HANDLE _memHandle;
HANDLE _fileHandle;
DWORD _size;
DWORD _mode;
char* _address;
HANDLE _memHandle;
HANDLE _fileHandle;
std::size_t _size;
DWORD _mode;
char* _address;
};


Expand Down
14 changes: 6 additions & 8 deletions Foundation/include/Poco/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Foundation_API Task: public Runnable, public RefCountedObject
/// A Task should use this method in favor of Thread::sleep().

bool yield();
/// Yields cpu to other threads
/// Yields cpu to other threads
///
/// If the task is cancelled while it is suspended,
/// yield() will return true. If the tasks resumes
Expand Down Expand Up @@ -145,12 +145,12 @@ class Foundation_API Task: public Runnable, public RefCountedObject
Task(const Task&);
Task& operator = (const Task&);

std::string _name;
TaskManager* _pOwner;
float _progress;
std::string _name;
TaskManager* _pOwner;
std::atomic<float> _progress;
std::atomic<TaskState> _state;
Event _cancelEvent;
mutable FastMutex _mutex;
Event _cancelEvent;
mutable FastMutex _mutex;

friend class TaskManager;
};
Expand All @@ -167,8 +167,6 @@ inline const std::string& Task::name() const

inline float Task::progress() const
{
FastMutex::ScopedLock lock(_mutex);

return _progress;
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/include/Poco/TaskManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Foundation_API TaskManager
TaskList _taskList;
Timestamp _lastProgressNotification;
NotificationCenter _nc;
mutable MutexT _mutex;
mutable MutexT _mutex;

friend class Task;
};
Expand Down
28 changes: 23 additions & 5 deletions Foundation/src/SharedMemory_WIN32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SharedMemoryImpl::SharedMemoryImpl(const std::string& name, std::size_t size, Sh
_name(name),
_memHandle(INVALID_HANDLE_VALUE),
_fileHandle(INVALID_HANDLE_VALUE),
_size(static_cast<DWORD>(size)),
_size(size),
_mode(PAGE_READONLY),
_address(0)
{
Expand All @@ -37,22 +37,40 @@ SharedMemoryImpl::SharedMemoryImpl(const std::string& name, std::size_t size, Sh

std::wstring utf16name;
UnicodeConverter::toUTF16(_name, utf16name);
_memHandle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, _mode, 0, _size, utf16name.c_str());
#ifdef _WIN64
const DWORD dwMaxSizeLow = static_cast<DWORD>(_size & 0xFFFFFFFFULL);
const DWORD dwMaxSizeHigh = static_cast<DWORD>((_size & (0xFFFFFFFFULL << 32)) >> 32);
#else
if (_size > std::numeric_limits<DWORD>::max())
{
throw Poco::InvalidArgumentException(Poco::format("Requested shared memory size (%z) too large (max %lu)",
_size, std::numeric_limits<DWORD>::max()));
}
const DWORD dwMaxSizeLow = static_cast<DWORD>(_size);
const DWORD dwMaxSizeHigh = 0UL;
#endif
_memHandle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, _mode, dwMaxSizeHigh, dwMaxSizeLow, utf16name.c_str());

if (!_memHandle)
{
DWORD dwRetVal = GetLastError();
int retVal = static_cast<int>(dwRetVal);
#if defined (_WIN32_WCE)
throw SystemException(format("Cannot create shared memory object %s [Error %d: %s]", _name, static_cast<int>(dwRetVal), Error::getMessage(dwRetVal)));
throw SystemException(Poco::format("Cannot create shared memory object %s [Error %d: %s]",
_name, retVal, Error::getMessage(dwRetVal)));
#else
if (_mode != PAGE_READONLY || dwRetVal != 5)
throw SystemException(format("Cannot create shared memory object %s [Error %d: %s]", _name, static_cast<int>(dwRetVal), Error::getMessage(dwRetVal)));
{
throw SystemException(Poco::format("Cannot create shared memory object %s [Error %d: %s]",
_name, retVal, Error::getMessage(dwRetVal)), retVal);
}

_memHandle = OpenFileMappingW(PAGE_READONLY, FALSE, utf16name.c_str());
if (!_memHandle)
{
dwRetVal = GetLastError();
throw SystemException(format("Cannot open shared memory object %s [Error %d: %s]", _name, static_cast<int>(dwRetVal), Error::getMessage(dwRetVal)));
throw SystemException(Poco::format("Cannot open shared memory object %s [Error %d: %s]",
_name, retVal, Error::getMessage(dwRetVal)), retVal);
}
#endif
}
Expand Down
11 changes: 4 additions & 7 deletions Foundation/src/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ void Task::run()
catch (std::exception& exc)
{
if (pOwner)
pOwner->taskFailed(this, SystemException(exc.what()));
pOwner->taskFailed(this, SystemException("Task::run()", exc.what()));
}
catch (...)
{
if (pOwner)
pOwner->taskFailed(this, SystemException("unknown exception"));
pOwner->taskFailed(this, SystemException("Task::run(): unknown exception"));
}
_state = TASK_FINISHED;
if (pOwner) pOwner->taskFinished(this);
Expand All @@ -98,11 +98,9 @@ bool Task::yield()

void Task::setProgress(float progress)
{
FastMutex::ScopedLock lock(_mutex);

if (_progress != progress)
if (_progress.exchange(progress) != progress)
{
_progress = progress;
FastMutex::ScopedLock lock(_mutex);
if (_pOwner)
_pOwner->taskProgress(this, _progress);
}
Expand All @@ -112,7 +110,6 @@ void Task::setProgress(float progress)
void Task::setOwner(TaskManager* pOwner)
{
FastMutex::ScopedLock lock(_mutex);

_pOwner = pOwner;
}

Expand Down
25 changes: 25 additions & 0 deletions Foundation/testsuite/src/SharedMemoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ Poco::Path SharedMemoryTest::findDataFile(const std::string& afile)
}


void SharedMemoryTest::testCreateLarge()
{
#if POCO_OS == POCO_OS_WINDOWS_NT
try
{
#ifdef _WIN64
const size_t size = 0x03FFFFFFFFULL;
#else
const size_t size = 0xDFFFFFFFUL;
#endif
SharedMemory mem("hiLarge", size, SharedMemory::AM_WRITE);
assertTrue((mem.end() - mem.begin()) == size);
mem.begin()[0] = 'A';
mem.end()[-1] = 'Z';
}
catch (Poco::SystemException& ex)
{
// no memory, quite posible to happen
assertEqual(ERROR_NOT_ENOUGH_MEMORY, ex.code());
}
#endif
}


void SharedMemoryTest::setUp()
{
}
Expand All @@ -89,6 +113,7 @@ CppUnit::Test* SharedMemoryTest::suite()
#if !defined(POCO_NO_SHAREDMEMORY)
CppUnit_addTest(pSuite, SharedMemoryTest, testCreate);
CppUnit_addTest(pSuite, SharedMemoryTest, testCreateFromFile);
CppUnit_addTest(pSuite, SharedMemoryTest, testCreateLarge);
#endif
return pSuite;
}
1 change: 1 addition & 0 deletions Foundation/testsuite/src/SharedMemoryTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SharedMemoryTest: public CppUnit::TestCase

void testCreate();
void testCreateFromFile();
void testCreateLarge();

void setUp();
void tearDown();
Expand Down
66 changes: 34 additions & 32 deletions Foundation/testsuite/src/TaskTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,42 @@ namespace

void runTask()
{
try
{
_event.wait();
if (sleep(10))
return;
setProgress(0.5);
_event.wait();
if (isCancelled())
return;
setProgress(1.0);
_event.wait();
}
catch(const Poco::Exception& e)
{
std::cerr << "TestTask::run(): " << e.displayText() << '\n';
}
catch(const std::exception& e)
{
std::cerr << "TestTask::run(): " << e.what() << '\n';
}
catch(...)
{
std::cerr << "TestTask::run(): unknown exception." << '\n';
}
try
{
_event.wait();
if (sleep(100))
return;
setProgress(0.5);
_event.wait();
if (isCancelled())
return;
setProgress(1.0);
_event.wait();
}
catch(const Poco::Exception& e)
{
std::cerr << "TestTask::run(): " << e.displayText() << '\n';
}
catch(const std::exception& e)
{
std::cerr << "TestTask::run(): " << e.what() << '\n';
}
catch(...)
{
std::cerr << "TestTask::run(): unknown exception." << '\n';
}
}

void cont()
{
try
{
_event.set();
}
catch(const Poco::SystemException& e)
{
std::cerr << "TestTask::cont(): " << e.displayText() << '\n';
}
try
{
_event.set();
}
catch(const Poco::SystemException& e)
{
std::cerr << "TestTask::cont(): " << e.displayText() << '\n';
}
}

private:
Expand Down Expand Up @@ -131,6 +131,8 @@ void TaskTest::testCancel2()
assertTrue (pTT->state() == Task::TASK_IDLE);
Thread thr;
thr.start(*pTT);
while (pTT->state() != Task::TASK_RUNNING)
Thread::sleep(50);
assertTrue (pTT->progress() == 0);
pTT->cancel();
assertTrue (pTT->state() == Task::TASK_CANCELLING);
Expand Down
15 changes: 9 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ NetSSL_OpenSSL-clean:
Data-libexec: Foundation-libexec
$(MAKE) -C $(POCO_BASE)/Data

Data-tests: Data-libexec cppunit
DataTest-libexec: Data-libexec
$(MAKE) -C $(POCO_BASE)/Data/testsuite/DataTest

Data-tests: Data-libexec DataTest-libexec cppunit
$(MAKE) -C $(POCO_BASE)/Data/testsuite

Data-samples: Data-libexec Data-libexec Data/SQLite-libexec Net-libexec
Expand All @@ -260,7 +263,7 @@ Data-clean:
Data/SQLite-libexec: Foundation-libexec Data-libexec
$(MAKE) -C $(POCO_BASE)/Data/SQLite

Data/SQLite-tests: Data/SQLite-libexec cppunit
Data/SQLite-tests: Data/SQLite-libexec DataTest-libexec cppunit
$(MAKE) -C $(POCO_BASE)/Data/SQLite/testsuite

Data/SQLite-clean:
Expand All @@ -270,7 +273,7 @@ Data/SQLite-clean:
Data/ODBC-libexec: Foundation-libexec Data-libexec
$(MAKE) -C $(POCO_BASE)/Data/ODBC

Data/ODBC-tests: Data/ODBC-libexec cppunit
Data/ODBC-tests: Data/ODBC-libexec DataTest-libexec cppunit
$(MAKE) -C $(POCO_BASE)/Data/ODBC/testsuite

Data/ODBC-clean:
Expand All @@ -280,7 +283,7 @@ Data/ODBC-clean:
Data/MySQL-libexec: Foundation-libexec Data-libexec
$(MAKE) -C $(POCO_BASE)/Data/MySQL

Data/MySQL-tests: Data/MySQL-libexec cppunit
Data/MySQL-tests: Data/MySQL-libexec DataTest-libexec cppunit
$(MAKE) -C $(POCO_BASE)/Data/MySQL/testsuite

Data/MySQL-clean:
Expand All @@ -290,7 +293,7 @@ Data/MySQL-clean:
Data/PostgreSQL-libexec: Foundation-libexec Data-libexec
$(MAKE) -C $(POCO_BASE)/Data/PostgreSQL

Data/PostgreSQL-tests: Data/PostgreSQL-libexec cppunit
Data/PostgreSQL-tests: Data/PostgreSQL-libexec DataTest-libexec cppunit
$(MAKE) -C $(POCO_BASE)/Data/PostgreSQL/testsuite

Data/PostgreSQL-clean:
Expand Down Expand Up @@ -407,7 +410,7 @@ Prometheus-libexec: Foundation-libexec Net-libexec
Prometheus-tests: Prometheus-libexec cppunit
$(MAKE) -C $(POCO_BASE)/Prometheus/testsuite

Prometheus-samples: Prometheus-libexec
Prometheus-samples: Prometheus-libexec Util-libexec
$(MAKE) -C $(POCO_BASE)/Prometheus/samples

Prometheus-clean:
Expand Down
2 changes: 1 addition & 1 deletion Net/include/Poco/Net/HTTPClientSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class Net_API HTTPClientSession: public HTTPSession
HTTPBasicCredentials _proxyBasicCreds;
HTTPDigestCredentials _proxyDigestCreds;
HTTPNTLMCredentials _proxyNTLMCreds;
bool _ntlmProxyAuthenticated;
bool _ntlmProxyAuthenticated = false;

static ProxyConfig _globalProxyConfig;

Expand Down
3 changes: 3 additions & 0 deletions Net/include/Poco/Net/SocketAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ class Net_API SocketAddress
/// Maximum length in bytes of a socket address.
};

static bool isUnixLocal(const std::string& hostAndPort);
/// Returns true iff `hostAndPort` is an absolute file path.

protected:
void init(const IPAddress& hostAddress, Poco::UInt16 portNumber);
void init(const std::string& hostAddress, Poco::UInt16 portNumber);
Expand Down
7 changes: 6 additions & 1 deletion Net/src/HTTPClientSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,12 @@ void HTTPClientSession::reconnect()
{
SocketAddress addr;
if (_proxyConfig.host.empty() || bypassProxy())
addr = SocketAddress(_host, _port);
{
if (SocketAddress::isUnixLocal(_host))
addr = SocketAddress(_host);
else
addr = SocketAddress(_host, _port);
}
else
addr = SocketAddress(_proxyConfig.host, _proxyConfig.port);

Expand Down
3 changes: 2 additions & 1 deletion Net/src/HTTPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ void HTTPSession::connect(const SocketAddress& address)
_socket.connect(address, _connectionTimeout);
_socket.setReceiveTimeout(_receiveTimeout);
_socket.setSendTimeout(_sendTimeout);
_socket.setNoDelay(true);
if (address.family() != SocketAddress::UNIX_LOCAL)
_socket.setNoDelay(true);
// There may be leftover data from a previous (failed) request in the buffer,
// so we clear it.
_pCurrent = _pEnd = _pBuffer;
Expand Down
Loading

0 comments on commit 11de403

Please sign in to comment.