GlXGetProcAddress returns non-null for any procName starting with "gl"

I was very surprised when I discovered that the following code and many of its variants create a non-zero memory address. The options I tried to include:

  • Call glXGetProcAddressARBinstead glXGetProcAddress.
  • The presence of an active GL context created using GLFW.
  • Using a cross-platform alternative delivered with GLFW: glfwGetProcAddress.

    #include <GL/glx.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        void *ptr;
    
        ptr = glXGetProcAddress((const GLubyte *)"glottis");
    
        printf("ptr: %x\n", ptr);
    
        return 0;
    }
    

Programs are compiled with -lGL(and -lglfwif necessary), and there are no warnings and errors.

The only way to get the conclusion 0is a pointer NULLis to set the address of a function whose name does not start with gl, for example manny.

, glottis manny , , NULL.

glXGetProcAddress.

NULL , .

GLU - , .

+4
1

, , , , . , NULL NULL ( ) , . . , ( , GL ), undefined.

glXGetProcAddress. , , , . , , .

. GLX 1.4 . 3.3.12 " " glXGetProcAddress ( ):

NULL , .

NULL NULL , . glGetString(GL_EXTENSIONS) glXQueryExtensionsString, , .[...]

glXGetProcAddress :

  • GL GLX, (, ).
  • () GL GLX 1.0 , , glGetString(GL_VERSION) glXQueryVersion.

, Mesa3D , gl.

/src/mapi/glapi/glapi_getproc.c, , _glapi_get_proc_address() :

/**
 * Return pointer to the named function.  If the function name isn't found
 * in the name of static functions, try generating a new API entrypoint on
 * the fly with assembly language.
 */
_glapi_proc
_glapi_get_proc_address(const char *funcName)
{
   _glapi_proc func;
   struct _glapi_function * entry;

   init_glapi_relocs_once();

#ifdef MANGLE
   /* skip the prefix on the name */
   if (funcName[1] != 'g' || funcName[2] != 'l')
      return NULL;
#else
   if (funcName[0] != 'g' || funcName[1] != 'l')
      return NULL;
#endif

   /* search extension functions first */
   func = get_extension_proc_address(funcName);
   if (func)
      return func;

   /* search static functions */
   func = get_static_proc_address(funcName);
   if (func)
      return func;

   /* generate entrypoint, dispatch offset must be filled in by the driver */
   entry = add_function_name(funcName);
   if (entry == NULL)
      return NULL;

   return entry->dispatch_stub;
}

, gl, , . , hw backend, gl, - , .

+7

Source: https://habr.com/ru/post/1626774/


All Articles