Improve threading and i/o routines

- On Windows connect() can now be interrupted by a signal; connect() w/
  O_NONBLOCK will now raise EINPROGRESS; and connect() with SO_SNDTIMEO
  will raise ETIMEDOUT after the interval has elapsed.

- We now get the AcceptEx(), ConnectEx(), and TransmitFile() functions
  from the WIN32 API the officially blessed way, using WSAIoctl().

- Do nothing on Windows when fsync() is called on a directory handle.
  This was raising EACCES earlier becaues GENERIC_WRITE is required on
  the handle. It's possible to FlushFileBuffers() a directory handle if
  it's opened with write access but MSDN doesn't document what it does.
  If you have any idea, please let us know!

- Prefer manual reset event objects for read() and write() on Windows.

- Do some code cleanup on our dlmalloc customizations.

- Fix errno type error in Windows blocking routines.

- Make the futex polyfill simpler and faster.
This commit is contained in:
Justine Tunney 2023-10-12 18:53:17 -07:00
parent f7343319cc
commit 49b0eaa69f
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
43 changed files with 528 additions and 425 deletions

View file

@ -51,77 +51,72 @@ ssh 22/tcp # SSH Remote Login Protocol";
ASSERT_NE(-1, close(fd));
}
/* TEST(LookupServicesByPort, GetNameWhenPortCorrect) { */
/* char name[8]; /\* service names are of length 3 *\/ */
/* char eitherproto[8]; /\* protocol names are of length 3 *\/ */
/* char proto1[] = "tcp"; */
/* char proto2[] = "udp"; */
/* char* localproto; */
/* strcpy(eitherproto, ""); */
/* strcpy(name, ""); */
TEST(LookupServicesByPort, GetNameWhenPortCorrect) {
char name[8]; /* service names are of length 3 */
char eitherproto[8]; /* protocol names are of length 3 */
char proto1[] = "tcp";
char proto2[] = "udp";
char* localproto;
strcpy(eitherproto, "");
strcpy(name, "");
/* localproto = eitherproto; */
/* ASSERT_EQ(-1, /\* non existent port *\/ */
/* LookupServicesByPort(965, localproto, sizeof(eitherproto), name,
*/
/* sizeof(name), "services")); */
/* ASSERT_EQ('\0', localproto[0]); */
localproto = eitherproto;
ASSERT_EQ(-1, /* non existent port */
LookupServicesByPort(965, localproto, sizeof(eitherproto), name,
sizeof(name), "services"));
ASSERT_EQ('\0', localproto[0]);
/* localproto = eitherproto; */
/* ASSERT_EQ(-1, /\* port in network byte order *\/ */
/* LookupServicesByPort(htons(22), localproto, sizeof(eitherproto),
*/
/* name, sizeof(name), "services")); */
/* ASSERT_EQ('\0', localproto[0]); */
localproto = eitherproto;
ASSERT_EQ(-1, /* port in network byte order */
LookupServicesByPort(htons(22), localproto, sizeof(eitherproto),
name, sizeof(name), "services"));
ASSERT_EQ('\0', localproto[0]);
/* localproto = proto2; */
/* ASSERT_EQ(-1, /\* port ok but wrong protocol *\/ */
/* LookupServicesByPort(22, localproto, sizeof(proto2), name, */
/* sizeof(name), "services")); */
/* ASSERT_STREQ(proto2, "udp"); */
localproto = proto2;
ASSERT_EQ(-1, /* port ok but wrong protocol */
LookupServicesByPort(22, localproto, sizeof(proto2), name,
sizeof(name), "services"));
ASSERT_STREQ(proto2, "udp");
/* localproto = proto1; */
/* ASSERT_EQ( */
/* -1, /\* protocol is non-NULL/length must be nonzero *\/ */
/* LookupServicesByPort(22, localproto, 0, name, sizeof(name),
* "services")); */
/* ASSERT_STREQ(proto1, "tcp"); */
localproto = proto1;
ASSERT_EQ(
-1, /* protocol is non-NULL/length must be nonzero */
LookupServicesByPort(22, localproto, 0, name, sizeof(name), "services"));
ASSERT_STREQ(proto1, "tcp");
/* localproto = proto1; */
/* ASSERT_EQ(-1, /\* sizeof(name) insufficient, memccpy failure *\/ */
/* LookupServicesByPort(22, localproto, sizeof(proto1), name, 1, */
/* "services")); */
/* ASSERT_STREQ(proto1, "tcp"); */
/* ASSERT_STREQ(name, ""); /\* cleaned up after memccpy failed *\/ */
localproto = proto1;
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
LookupServicesByPort(22, localproto, sizeof(proto1), name, 1,
"services"));
ASSERT_STREQ(proto1, "tcp");
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
/* localproto = eitherproto; */
/* ASSERT_EQ( */
/* -1, /\* sizeof(proto) insufficient, memccpy failure *\/ */
/* LookupServicesByPort(22, localproto, 1, name, sizeof(name),
* "services")); */
/* ASSERT_STREQ(eitherproto, ""); /\* cleaned up after memccpy failed *\/ */
localproto = eitherproto;
ASSERT_EQ(
-1, /* sizeof(proto) insufficient, memccpy failure */
LookupServicesByPort(22, localproto, 1, name, sizeof(name), "services"));
ASSERT_STREQ(eitherproto, ""); /* cleaned up after memccpy failed */
/* localproto = proto1; */
/* ASSERT_EQ(0, LookupServicesByPort(22, localproto, sizeof(proto1), name, */
/* sizeof(name), "services")); */
/* ASSERT_STREQ(name, "ssh"); */
/* ASSERT_STREQ(proto1, "tcp"); */
localproto = proto1;
ASSERT_EQ(0, LookupServicesByPort(22, localproto, sizeof(proto1), name,
sizeof(name), "services"));
ASSERT_STREQ(name, "ssh");
ASSERT_STREQ(proto1, "tcp");
/* localproto = proto2; */
/* ASSERT_EQ(0, LookupServicesByPort(19, localproto, sizeof(proto2), name, */
/* sizeof(name), "services")); */
/* ASSERT_STREQ(name, "chargen"); */
/* ASSERT_STREQ(proto2, "udp"); */
localproto = proto2;
ASSERT_EQ(0, LookupServicesByPort(19, localproto, sizeof(proto2), name,
sizeof(name), "services"));
ASSERT_STREQ(name, "chargen");
ASSERT_STREQ(proto2, "udp");
/* localproto = eitherproto; */
/* ASSERT_EQ(0, /\* pick first matching protocol *\/ */
/* LookupServicesByPort(19, localproto, sizeof(eitherproto), name,
*/
/* sizeof(name), "services")); */
/* ASSERT_STREQ(name, "chargen"); */
/* ASSERT_NE('\0', localproto[0]); /\* buffer filled during the call *\/ */
/* ASSERT_STREQ(eitherproto, "tcp"); */
/* } */
localproto = eitherproto;
ASSERT_EQ(0, /* pick first matching protocol */
LookupServicesByPort(19, localproto, sizeof(eitherproto), name,
sizeof(name), "services"));
ASSERT_STREQ(name, "chargen");
ASSERT_NE('\0', localproto[0]); /* buffer filled during the call */
ASSERT_STREQ(eitherproto, "tcp");
}
TEST(LookupServicesByName, GetPortWhenNameOrAlias) {
char name[8]; /* service names are of length 3 */
@ -132,42 +127,36 @@ TEST(LookupServicesByName, GetPortWhenNameOrAlias) {
strcpy(eitherproto, "");
strcpy(name, "");
/* localproto = eitherproto; */
/* ASSERT_EQ(-1, /\* non-existent name *\/ */
/* LookupServicesByName("http", localproto, sizeof(eitherproto),
* name, */
/* sizeof(name), "services")); */
/* ASSERT_EQ('\0', localproto[0]); */
localproto = eitherproto;
ASSERT_EQ(-1, /* non-existent name */
LookupServicesByName("http", localproto, sizeof(eitherproto), name,
sizeof(name), "services"));
ASSERT_EQ('\0', localproto[0]);
/* localproto = proto2; */
/* ASSERT_EQ(-1, /\* name exists but wrong protocol *\/ */
/* LookupServicesByName("ssh", localproto, sizeof(proto2), name, */
/* sizeof(name), "services")); */
/* ASSERT_STREQ(proto2, "udp"); */
localproto = proto2;
ASSERT_EQ(-1, /* name exists but wrong protocol */
LookupServicesByName("ssh", localproto, sizeof(proto2), name,
sizeof(name), "services"));
ASSERT_STREQ(proto2, "udp");
/* localproto = proto2; */
/* ASSERT_EQ(-1, /\* protocol is non-NULL/length must be nonzero *\/ */
/* LookupServicesByName("ssh", localproto, sizeof(proto2), name, */
/* sizeof(name), "services")); */
/* ASSERT_STREQ(proto2, "udp"); */
localproto = proto2;
ASSERT_EQ(-1, /* protocol is non-NULL/length must be nonzero */
LookupServicesByName("ssh", localproto, sizeof(proto2), name,
sizeof(name), "services"));
ASSERT_STREQ(proto2, "udp");
/* localproto = proto1; */
/* ASSERT_EQ(-1, /\* sizeof(name) insufficient, memccpy failure *\/ */
/* LookupServicesByName("ssh", localproto, sizeof(proto1), name, 1,
*/
/* "services")); */
/* ASSERT_STREQ(proto1, "tcp"); */
/* ASSERT_STREQ(name, ""); /\* cleaned up after memccpy failed *\/ */
localproto = proto1;
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
LookupServicesByName("ssh", localproto, sizeof(proto1), name, 1,
"services"));
ASSERT_STREQ(proto1, "tcp");
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
/* localproto = eitherproto; */
/* ASSERT_EQ(-1, /\* sizeof(proto) insufficient, memccpy failure *\/ */
/* LookupServicesByName("ssh", localproto, 1, name, sizeof(name), */
/* "services")); */
/* ASSERT_STREQ(eitherproto, ""); /\* cleaned up after memccpy failed *\/ */
ftrace_install();
strace_enabled(+1);
ftrace_enabled(+1);
localproto = eitherproto;
ASSERT_EQ(-1, /* sizeof(proto) insufficient, memccpy failure */
LookupServicesByName("ssh", localproto, 1, name, sizeof(name),
"services"));
ASSERT_STREQ(eitherproto, ""); /* cleaned up after memccpy failed */
localproto = proto1;
ASSERT_EQ(22, LookupServicesByName("ssh", localproto, sizeof(proto1), name,