Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3e654dc6 authored by Chia-I Wu's avatar Chia-I Wu
Browse files

vulkan: do not use exported functions internally

Our vkGet*ProcAddr uses the exported functions.  They will break when any
of the exported functions are overridden (e.g., through LD_PRELOAD).

Unexport and move all exported functions to vulkan::api namespace.
Re-export them by having vkFoo as a wrapper to vulkan::api::Foo.

Another option is to re-export vulkan::api::Foo by having vkFoo as an
alias using __attribute__((alias)).  That results in smaller binaries.
But we will not be able to catch mismatches between vulkan.h and
vulkan.api.

To avoid future breakage, define VK_NO_PROTOTYPES for all files except
api_gen.cpp.

Bug: 28886971
Change-Id: I08fde7ebb247f8c7e040ccf812b40b02094d3c7f
parent 79994146
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ LOCAL_SANITIZE := integer

LOCAL_CFLAGS := -DLOG_TAG=\"vulkan\" \
	-DVK_USE_PLATFORM_ANDROID_KHR \
	-DVK_NO_PROTOTYPES \
	-std=c99 -fvisibility=hidden -fstrict-aliasing \
	-Weverything -Werror \
	-Wno-padded \
+1009 −304

File changed.

Preview size limit exceeded, changes collapsed.

+44 −21
Original line number Diff line number Diff line
@@ -94,6 +94,8 @@ bool InitDispatchTable(
#include <algorithm>
#include <log/log.h>

// to catch mismatches between vulkan.h and this file
#undef VK_NO_PROTOTYPES
#include "api.h"

namespace vulkan {«
@@ -150,15 +152,20 @@ bool InitDispatchTable(
    return success;
}

»} // namespace api
»} // namespace vulkan

// clang-format off

namespace {«

// forward declarations needed by GetInstanceProcAddr and GetDeviceProcAddr
{{range $f := AllCommands $}}
  {{if (Macro "IsFunctionExported" $f)}}
    __attribute__((visibility("default")))
    VKAPI_ATTR {{Node "Type" $f.Return}} {{$f.Name}}({{Macro "Parameters" $f}}) {
  {{if and (Macro "IsFunctionExported" $f) (not (Macro "api.IsIntercepted" $f))}}
    VKAPI_ATTR {{Node "Type" $f.Return}} {{Macro "BaseName" $f}}({{Macro "Parameters" $f}});
  {{end}}
{{end}}

{{range $f := AllCommands $}}
  {{if and (Macro "IsFunctionExported" $f) (not (Macro "api.IsIntercepted" $f))}}
    VKAPI_ATTR {{Node "Type" $f.Return}} {{Macro "BaseName" $f}}({{Macro "Parameters" $f}}) {
      {{     if eq $f.Name "vkGetInstanceProcAddr"}}
        {{Macro "api.C++.InterceptInstanceProcAddr" $}}
      {{else if eq $f.Name "vkGetDeviceProcAddr"}}
@@ -171,6 +178,26 @@ bool InitDispatchTable(
  {{end}}
{{end}}

»}  // anonymous namespace

// clang-format on

»} // namespace api
»} // namespace vulkan

// clang-format off

{{range $f := AllCommands $}}
  {{if (Macro "IsFunctionExported" $f)}}
    __attribute__((visibility("default")))
    VKAPI_ATTR {{Node "Type" $f.Return}} {{$f.Name}}({{Macro "Parameters" $f}}) {
      {{if not (IsVoid $f.Return.Type)}}return §{{end}}
      vulkan::api::{{Macro "BaseName" $f}}({{Macro "Arguments" $f}});
    }

  {{end}}
{{end}}

// clang-format on
¶{{end}}

@@ -512,8 +539,7 @@ bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
    {{range $f := AllCommands $}}
      {{if (Macro "IsGloballyDispatched" $f)}}
        if (strcmp(pName, "{{$f.Name}}") == 0) return §
          reinterpret_cast<PFN_vkVoidFunction>(§
            vulkan::api::{{Macro "BaseName" $f}});
          reinterpret_cast<PFN_vkVoidFunction>({{Macro "BaseName" $f}});
      {{end}}
    {{end}}

@@ -534,16 +560,16 @@ bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
        {{/* redirect intercepted functions */}}
        {{else if (Macro "api.IsIntercepted" $f)}}
          { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>(§
            vulkan::api::{{Macro "BaseName" $f}}) },
            {{Macro "BaseName" $f}}) },

        {{/* redirect vkGetInstanceProcAddr to itself */}}
        {{else if eq $f.Name "vkGetInstanceProcAddr"}}
          { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>({{$f.Name}}) },
          { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>({{Macro "BaseName" $f}}) },

        {{/* redirect device functions to themselves as a workaround for
             layers that do not intercept in their vkGetInstanceProcAddr */}}
        {{else if (Macro "IsDeviceDispatched" $f)}}
          { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>({{$f.Name}}) },
          { "{{$f.Name}}", reinterpret_cast<PFN_vkVoidFunction>({{Macro "BaseName" $f}}) },

        {{end}}
      {{end}}
@@ -608,11 +634,11 @@ bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
      {{     if (Macro "api.IsIntercepted" $f)}}
        if (strcmp(pName, "{{$f.Name}}") == 0) return §
          reinterpret_cast<PFN_vkVoidFunction>(§
            vulkan::api::{{Macro "BaseName" $f}});
            {{Macro "BaseName" $f}});
      {{else if eq $f.Name "vkGetDeviceProcAddr"}}
        if (strcmp(pName, "{{$f.Name}}") == 0) return §
          reinterpret_cast<PFN_vkVoidFunction>(§
            {{$f.Name}});
            {{Macro "BaseName" $f}});
      {{end}}
    {{end}}
  {{end}}
@@ -627,17 +653,14 @@ bool InitDriverTable(VkDevice dev, PFN_vkGetDeviceProcAddr get_proc,
*/}}
{{define "api.C++.Dispatch"}}
  {{AssertType $ "Function"}}
  {{if (Macro "api.IsIntercepted" $)}}
    {{Error "$.Name should not be generated"}}
  {{end}}

  {{if (Macro "api.IsIntercepted" $)}}// call into api.cpp{{end}}
  {{if not (IsVoid $.Return.Type)}}return §{{end}}

  {{if (Macro "api.IsIntercepted" $)}}
    vulkan::api::§
  {{else}}
  {{$p0 := index $.CallParameters 0}}
    vulkan::api::GetData({{$p0.Name}}).dispatch.§
  {{end}}

  GetData({{$p0.Name}}).dispatch.§
  {{Macro "BaseName" $}}({{Macro "Arguments" $}});
{{end}}