gethostbyname API fails when App Verifier is on

2019-05-28 00:56发布

问题:

I ran into a problem trying to test an application under Application Verifier with Page Heap on. It turns out that gethostbyname API always fails even for legit host names like "localhost". The problem reproduces on every Win-7 or Server 2008 R2 I tried even for a very simple test applications using gethostbyname.

Repro steps: in appverifier check "page heap" and "UseLFGGuard..." checkboxes, run any app using gethostbyname(..).

Example of application code (prints "127.0.0.1" when appverifier is off, "getaddrinfo failed" when appverifier is on):

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

void
Exercise()
{
    int i = 0;

    struct hostent *remoteHost;
    struct in_addr addr;

    remoteHost = gethostbyname("localhost");
    if (remoteHost == NULL)
    {
        printf("gethostbyname(localhost) failed\n");
    }
    else
    {
        if (remoteHost->h_addrtype == AF_INET)
        {
            i=0;
            while (remoteHost->h_addr_list[i] != 0)
            {
                addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
                printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
            }
        }
        else
        {
            printf("unexpected address type\n");
        }
    }
}

int
main()
{
    WSADATA wsaData;
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0)
    {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    for(int i=0; i<1000; i++)
    {
        Exercise();
        Sleep(1000);
    }

    return 0;
}

The most unusual thing is that I could not find anything in the internet. Is this a known problem? Any workarounds?