Hash :
a8e98a23
Author :
Date :
2021-07-10T15:25:20
Fix gl::Context::unMakeCurrent crashes The crash is because Display::makeCurrent() may fail, in that case, egl_stubs.cpp will not update the current context in global_state and Thread accordingly. And then Context::refCount could be reach 0, and be released, however egl_stubs.cpp still thinks the context is current, and then user-after-free happens. This CL fixes the problem by updating current context of Thread in Display::makeCurrent, and reading the current context of Thread in egl_stubs.cpp and setting it to global_state. Bug: chromium:1171371 Change-Id: Ifc5fffb0e4902c9c72514839d03e5783d50fe283 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3017210 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Peng Huang <penghuang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// entry_points_wgl.cpp: Implements the exported WGL functions.
#include "entry_points_wgl.h"
#include "common/angle_version.h"
#include "common/debug.h"
#include "common/event_tracer.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/EGLSync.h"
#include "libANGLE/Surface.h"
#include "libANGLE/Texture.h"
#include "libANGLE/Thread.h"
#include "libANGLE/entry_points_utils.h"
#include "libANGLE/queryutils.h"
#include "libANGLE/validationEGL.h"
#include "libGL/proc_table_wgl.h"
#include "libGLESv2/global_state.h"
using namespace wgl;
using namespace egl;
namespace
{
bool CompareProc(const ProcEntry &a, const char *b)
{
return strcmp(a.first, b) < 0;
}
void ClipConfigs(const std::vector<const Config *> &filteredConfigs,
EGLConfig *output_configs,
EGLint config_size,
EGLint *num_config)
{
EGLint result_size = static_cast<EGLint>(filteredConfigs.size());
if (output_configs)
{
result_size = std::max(std::min(result_size, config_size), 0);
for (EGLint i = 0; i < result_size; i++)
{
output_configs[i] = const_cast<Config *>(filteredConfigs[i]);
}
}
*num_config = result_size;
}
} // anonymous namespace
#define WGL_EVENT(EP, FMT, ...) EVENT(nullptr, WGL##EP, FMT, __VA_ARGS__)
extern "C" {
// WGL 1.0
int GL_APIENTRY wglChoosePixelFormat(HDC hDc, const PIXELFORMATDESCRIPTOR *pPfd)
{
UNIMPLEMENTED();
return 1;
}
int GL_APIENTRY wglDescribePixelFormat(HDC hdc, int ipfd, UINT cjpfd, PIXELFORMATDESCRIPTOR *ppfd)
{
UNIMPLEMENTED();
if (ppfd)
{
ppfd->dwFlags = ppfd->dwFlags | PFD_DRAW_TO_WINDOW;
ppfd->dwFlags = ppfd->dwFlags | PFD_SUPPORT_OPENGL;
ppfd->dwFlags = ppfd->dwFlags | PFD_GENERIC_ACCELERATED;
ppfd->dwFlags = ppfd->dwFlags | PFD_DOUBLEBUFFER;
ppfd->iPixelType = PFD_TYPE_RGBA;
ppfd->cColorBits = 24;
ppfd->cRedBits = 8;
ppfd->cGreenBits = 8;
ppfd->cBlueBits = 8;
ppfd->cAlphaBits = 8;
ppfd->cDepthBits = 24;
ppfd->cStencilBits = 8;
ppfd->nVersion = 1;
}
return 1;
}
UINT GL_APIENTRY wglGetEnhMetaFilePixelFormat(HENHMETAFILE hemf,
UINT cbBuffer,
PIXELFORMATDESCRIPTOR *ppfd)
{
UNIMPLEMENTED();
return 1u;
}
int GL_APIENTRY wglGetPixelFormat(HDC hdc)
{
UNIMPLEMENTED();
return 1;
}
BOOL GL_APIENTRY wglSetPixelFormat(HDC hdc, int ipfd, const PIXELFORMATDESCRIPTOR *ppfd)
{
UNIMPLEMENTED();
return TRUE;
}
BOOL GL_APIENTRY wglSwapBuffers(HDC hdc)
{
Thread *thread = egl::GetCurrentThread();
egl::Display *display = egl::Display::GetExistingDisplayFromNativeDisplay(hdc);
ANGLE_EGL_TRY_RETURN(thread, display->getWGLSurface()->swap(thread->getContext()),
"wglSwapBuffers", display->getWGLSurface(), FALSE);
return TRUE;
}
BOOL GL_APIENTRY wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
{
UNIMPLEMENTED();
return TRUE;
}
HGLRC GL_APIENTRY wglCreateContext(HDC hDc)
{
Thread *thread = egl::GetCurrentThread();
std::vector<EGLAttrib> displayAttributes;
displayAttributes.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
GLenum platformType = EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE;
displayAttributes.push_back(platformType);
displayAttributes.push_back(EGL_NONE);
const auto &attribMapDisplay = AttributeMap::CreateFromAttribArray(displayAttributes.data());
egl::Display *display = egl::Display::GetDisplayFromNativeDisplay(hDc, attribMapDisplay);
ANGLE_EGL_TRY_RETURN(thread, display->initialize(), "wglCreateContext", display, nullptr);
thread->setAPI(EGL_OPENGL_API);
// Default config
const EGLint configAttributes[] = {EGL_NONE};
// Choose config
EGLint configCount;
EGLConfig config;
AttributeMap attribMapConfig = AttributeMap::CreateFromIntArray(configAttributes);
ClipConfigs(display->chooseConfig(attribMapConfig), &config, 1, &configCount);
Config *configuration = static_cast<Config *>(config);
// Initialize surface
std::vector<EGLint> surfaceAttributes;
surfaceAttributes.push_back(EGL_NONE);
surfaceAttributes.push_back(EGL_NONE);
AttributeMap surfAttributes = AttributeMap::CreateFromIntArray(&surfaceAttributes[0]);
// Create first window surface
egl::Surface *surface = nullptr;
ANGLE_EGL_TRY_RETURN(
thread,
display->createWindowSurface(configuration, WindowFromDC(hDc), surfAttributes, &surface),
"wglCreateContext", display, nullptr);
// Initialize context
EGLint contextAttibutes[] = {EGL_CONTEXT_CLIENT_VERSION, 4, EGL_CONTEXT_MINOR_VERSION, 6,
EGL_NONE};
gl::Context *sharedGLContext = static_cast<gl::Context *>(nullptr);
AttributeMap ctxAttributes = AttributeMap::CreateFromIntArray(contextAttibutes);
gl::Context *context = nullptr;
ANGLE_EGL_TRY_RETURN(thread,
display->createContext(configuration, sharedGLContext, EGL_OPENGL_API,
ctxAttributes, &context),
"wglCreateContext", display, nullptr);
return reinterpret_cast<HGLRC>(context);
}
HGLRC GL_APIENTRY wglCreateLayerContext(HDC hDc, int level)
{
UNIMPLEMENTED();
return nullptr;
}
BOOL GL_APIENTRY wglDeleteContext(HGLRC oldContext)
{
UNIMPLEMENTED();
return FALSE;
}
BOOL GL_APIENTRY wglDescribeLayerPlane(HDC hDc,
int pixelFormat,
int layerPlane,
UINT nBytes,
LAYERPLANEDESCRIPTOR *plpd)
{
UNIMPLEMENTED();
return FALSE;
}
HGLRC GL_APIENTRY wglGetCurrentContext()
{
UNIMPLEMENTED();
return nullptr;
}
HDC GL_APIENTRY wglGetCurrentDC()
{
UNIMPLEMENTED();
return nullptr;
}
int GL_APIENTRY
wglGetLayerPaletteEntries(HDC hdc, int iLayerPlane, int iStart, int cEntries, COLORREF *pcr)
{
UNIMPLEMENTED();
return 0;
}
PROC GL_APIENTRY wglGetProcAddress(LPCSTR lpszProc)
{
ANGLE_SCOPED_GLOBAL_LOCK();
WGL_EVENT(GetProcAddress, "const char *procname = \"%s\"", lpszProc);
egl::Thread *thread = egl::GetCurrentThread();
const ProcEntry *entry =
std::lower_bound(&g_procTable[0], &g_procTable[g_numProcs], lpszProc, CompareProc);
thread->setSuccess();
if (entry == &g_procTable[g_numProcs] || strcmp(entry->first, lpszProc) != 0)
{
return nullptr;
}
return entry->second;
}
BOOL GL_APIENTRY wglMakeCurrent(HDC hDc, HGLRC newContext)
{
Thread *thread = egl::GetCurrentThread();
egl::Display *display = egl::Display::GetExistingDisplayFromNativeDisplay(hDc);
const gl::Context *context =
GetContextIfValid(display, reinterpret_cast<gl::Context *>(newContext));
ScopedSyncCurrentContextFromThread scopedSyncCurrent(thread);
// If display or context are invalid, make thread's current rendering context not current
if (!context)
{
gl::Context *oldContext = thread->getContext();
if (oldContext)
{
ANGLE_EGL_TRY_RETURN(thread, oldContext->unMakeCurrent(display), "wglMakeCurrent",
GetContextIfValid(display, oldContext), EGL_FALSE);
thread->setCurrent(nullptr);
}
return TRUE;
}
egl::Surface *surface = display->getWGLSurface();
Surface *previousDraw = thread->getCurrentDrawSurface();
Surface *previousRead = thread->getCurrentReadSurface();
gl::Context *previousContext = thread->getContext();
if (previousDraw != surface || previousRead != surface || previousContext != context)
{
ANGLE_EGL_TRY_RETURN(thread,
display->makeCurrent(thread, previousContext, surface, surface,
const_cast<gl::Context *>(context)),
"wglMakeCurrent", GetContextIfValid(display, context), EGL_FALSE);
}
return TRUE;
}
BOOL GL_APIENTRY wglRealizeLayerPalette(HDC hdc, int iLayerPlane, BOOL bRealize)
{
UNIMPLEMENTED();
return FALSE;
}
int GL_APIENTRY
wglSetLayerPaletteEntries(HDC hdc, int iLayerPlane, int iStart, int cEntries, const COLORREF *pcr)
{
UNIMPLEMENTED();
return 0;
}
BOOL GL_APIENTRY wglShareLists(HGLRC hrcSrvShare, HGLRC hrcSrvSource)
{
UNIMPLEMENTED();
return FALSE;
}
BOOL GL_APIENTRY wglSwapLayerBuffers(HDC hdc, UINT fuFlags)
{
UNIMPLEMENTED();
return FALSE;
}
BOOL GL_APIENTRY wglUseFontBitmapsA(HDC hDC, DWORD first, DWORD count, DWORD listBase)
{
UNIMPLEMENTED();
return FALSE;
}
BOOL GL_APIENTRY wglUseFontBitmapsW(HDC hDC, DWORD first, DWORD count, DWORD listBase)
{
UNIMPLEMENTED();
return FALSE;
}
BOOL GL_APIENTRY wglUseFontOutlinesA(HDC hDC,
DWORD first,
DWORD count,
DWORD listBase,
FLOAT deviation,
FLOAT extrusion,
int format,
LPGLYPHMETRICSFLOAT lpgmf)
{
UNIMPLEMENTED();
return FALSE;
}
BOOL GL_APIENTRY wglUseFontOutlinesW(HDC hDC,
DWORD first,
DWORD count,
DWORD listBase,
FLOAT deviation,
FLOAT extrusion,
int format,
LPGLYPHMETRICSFLOAT lpgmf)
{
UNIMPLEMENTED();
return FALSE;
}
} // extern "C"