Skip to content

Commit ecedbfa

Browse files
authored
Avoid exporting malloc/free in all pthreads builds. NFC (#18299)
The JS code for pthread_create does actually depend on malloc or free except in the case of OFFSCREENCANVASES_TO_PTHREAD.
1 parent ae2f8d8 commit ecedbfa

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

emcc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2618,7 +2618,6 @@ def check_memory_setting(setting):
26182618
if settings.RELOCATABLE or \
26192619
settings.BUILD_AS_WORKER or \
26202620
settings.USE_WEBGPU or \
2621-
settings.USE_PTHREADS or \
26222621
settings.OFFSCREENCANVAS_SUPPORT or \
26232622
settings.LEGACY_GL_EMULATION or \
26242623
not settings.DISABLE_EXCEPTION_CATCHING or \

src/library_pthread.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,11 @@ var LibraryPThread = {
663663
// allocations from __pthread_create_js we could also remove this.
664664
__pthread_create_js__noleakcheck: true,
665665
__pthread_create_js__sig: 'iiiii',
666-
__pthread_create_js__deps: ['$spawnThread', 'pthread_self', '$pthreadCreateProxied'],
666+
__pthread_create_js__deps: ['$spawnThread', 'pthread_self', '$pthreadCreateProxied',
667+
#if OFFSCREENCANVAS_SUPPORT
668+
'malloc',
669+
#endif
670+
],
667671
__pthread_create_js: function(pthread_ptr, attr, startRoutine, arg) {
668672
if (typeof SharedArrayBuffer == 'undefined') {
669673
err('Current environment does not support SharedArrayBuffer, pthreads are not available!');
@@ -773,7 +777,7 @@ var LibraryPThread = {
773777
return {{{ cDefine('EINVAL') }}}; // Hitting this might indicate an implementation bug or some other internal error
774778
}
775779
}
776-
#endif
780+
#endif // OFFSCREENCANVAS_SUPPORT
777781

778782
// Synchronously proxy the thread creation to main thread if possible. If we
779783
// need to transfer ownership of objects, then proxy asynchronously via

src/library_webgl.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ var LibraryGL = {
149149
#if GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS
150150
// If GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS is enabled, GL.initExtensions() will call to initialize these.
151151
$GL__deps: [
152+
#if USE_PTHREADS
153+
'malloc', // Needed by registerContext
154+
#endif
152155
#if MIN_WEBGL_VERSION == 1
153156
'_webgl_enable_ANGLE_instanced_arrays',
154157
'_webgl_enable_OES_vertex_array_object',

test/test_browser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4098,9 +4098,10 @@ def test_pthread_call_async(self):
40984098
# Test that it is possible to synchronously call a JavaScript function on the main thread and get a return value back.
40994099
@requires_threads
41004100
def test_pthread_call_sync_on_main_thread(self):
4101+
self.set_setting('EXPORTED_FUNCTIONS', '_main,_malloc')
41014102
self.btest_exit(test_file('pthread/call_sync_on_main_thread.c'), args=['-O3', '-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD', '-DPROXY_TO_PTHREAD=1', '--js-library', test_file('pthread/call_sync_on_main_thread.js')])
41024103
self.btest_exit(test_file('pthread/call_sync_on_main_thread.c'), args=['-O3', '-sUSE_PTHREADS', '-DPROXY_TO_PTHREAD=0', '--js-library', test_file('pthread/call_sync_on_main_thread.js')])
4103-
self.btest_exit(test_file('pthread/call_sync_on_main_thread.c'), args=['-Oz', '-DPROXY_TO_PTHREAD=0', '--js-library', test_file('pthread/call_sync_on_main_thread.js'), '-sEXPORTED_FUNCTIONS=_main,_malloc'])
4104+
self.btest_exit(test_file('pthread/call_sync_on_main_thread.c'), args=['-Oz', '-DPROXY_TO_PTHREAD=0', '--js-library', test_file('pthread/call_sync_on_main_thread.js')])
41044105

41054106
# Test that it is possible to asynchronously call a JavaScript function on the main thread.
41064107
@requires_threads

test/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11595,6 +11595,7 @@ def test_deps_info(self):
1159511595
cmd.append('-sASYNCIFY')
1159611596
if function.startswith('emscripten_webgl_') or 'offscreencanvas' in function:
1159711597
cmd.append('-sOFFSCREENCANVAS_SUPPORT')
11598+
cmd.append('-sUSE_PTHREADS')
1159811599
if function.startswith('wgpu'):
1159911600
cmd.append('-sUSE_WEBGPU')
1160011601
if function.startswith('__cxa_'):

tools/deps_info.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
'emscripten_set_touchstart_callback_on_thread': ['malloc'],
138138
'emscripten_set_visibilitychange_callback_on_thread': ['malloc'],
139139
'emscripten_set_wheel_callback_on_thread': ['malloc'],
140-
'emscripten_webgl_create_context': ['malloc'],
141140
'emscripten_webgl_get_parameter_utf8': ['malloc'],
142141
'emscripten_webgl_get_program_info_log_utf8': ['malloc'],
143142
'emscripten_webgl_get_shader_info_log_utf8': ['malloc'],
@@ -206,16 +205,21 @@ def get_deps_info():
206205
_deps_info['__cxa_find_matching_catch_7'] = ['__cxa_can_catch', 'setTempRet0']
207206
_deps_info['__cxa_find_matching_catch_8'] = ['__cxa_can_catch', 'setTempRet0']
208207
_deps_info['__cxa_find_matching_catch_9'] = ['__cxa_can_catch', 'setTempRet0']
208+
if settings.USE_PTHREADS and settings.OFFSCREENCANVAS_SUPPORT:
209+
_deps_info['pthread_create'] = ['malloc']
209210
if settings.FILESYSTEM and settings.SYSCALLS_REQUIRE_FILESYSTEM:
210211
_deps_info['mmap'] = ['emscripten_builtin_memalign']
211-
if settings.USE_PTHREADS and settings.OFFSCREEN_FRAMEBUFFER:
212-
# When OFFSCREEN_FRAMEBUFFER is defined these functions are defined in native code,
213-
# otherwise they are defined in src/library_html5_webgl.js.
214-
_deps_info['emscripten_webgl_destroy_context'] = ['emscripten_webgl_make_context_current', 'emscripten_webgl_get_current_context']
215-
if settings.USE_PTHREADS and settings.OFFSCREENCANVAS_SUPPORT:
216-
_deps_info['emscripten_set_offscreencanvas_size_on_target_thread'] = ['emscripten_dispatch_to_thread_', 'malloc', 'free']
217-
_deps_info['emscripten_set_offscreencanvas_size_on_target_thread_js'] = ['malloc']
218212
if settings.USE_PTHREADS:
213+
_deps_info['glutCreateWindow'] = ['malloc']
214+
_deps_info['emscripten_webgl_create_context'] = ['malloc']
215+
_deps_info['emscripten_webgl_destroy_context'] = ['free']
219216
_deps_info['emscripten_set_canvas_element_size_calling_thread'] = ['emscripten_dispatch_to_thread_']
217+
if settings.OFFSCREEN_FRAMEBUFFER:
218+
# When OFFSCREEN_FRAMEBUFFER is defined these functions are defined in native code,
219+
# otherwise they are defined in src/library_html5_webgl.js.
220+
_deps_info['emscripten_webgl_destroy_context'] += ['emscripten_webgl_make_context_current', 'emscripten_webgl_get_current_context']
221+
if settings.OFFSCREENCANVAS_SUPPORT:
222+
_deps_info['emscripten_set_offscreencanvas_size_on_target_thread'] = ['emscripten_dispatch_to_thread_', 'malloc', 'free']
223+
_deps_info['emscripten_set_offscreencanvas_size_on_target_thread_js'] = ['malloc']
220224

221225
return _deps_info

0 commit comments

Comments
 (0)