Skip to content

Avoid checks before _free calls. NFC #24416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.status, 'xhr.status', 'i16') }}}
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
onprogress?.(fetch, xhr, e);
if (ptr) {
_free(ptr);
}
_free(ptr);
};
xhr.onreadystatechange = (e) => {
// check if xhr was aborted by user and don't try to call back
Expand Down
11 changes: 3 additions & 8 deletions src/lib/libbrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ var LibraryBrowser = {

// To avoid creating worker parent->child chains, always proxies to execute on the main thread.
emscripten_create_worker__proxy: 'sync',
emscripten_create_worker__deps: ['$UTF8ToString', 'malloc', 'free'],
emscripten_create_worker__deps: ['$UTF8ToString', 'realloc'],
emscripten_create_worker: (url) => {
url = UTF8ToString(url);
var id = Browser.workers.length;
Expand All @@ -763,7 +763,6 @@ var LibraryBrowser = {
callbacks: [],
awaited: 0,
buffer: 0,
bufferSize: 0
};
info.worker.onmessage = function info_worker_onmessage(msg) {
if (ABORT) return;
Expand All @@ -781,11 +780,7 @@ var LibraryBrowser = {
var data = msg.data['data'];
if (data) {
if (!data.byteLength) data = new Uint8Array(data);
if (!info.buffer || info.bufferSize < data.length) {
if (info.buffer) _free(info.buffer);
info.bufferSize = data.length;
info.buffer = _malloc(data.length);
}
info.buffer = _realloc(info.buffer, data.length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does realloc do the equivalent check that the buffer size is < the desired length? (i.e. does it avoid the actual reallocation in the case where the existing buffer is big enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, thats exactly what it does

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in practice it also never shrinks, but I think that is fine for all these cases.

HEAPU8.set(data, info.buffer);
callbackInfo.func(info.buffer, data.length, callbackInfo.arg);
} else {
Expand All @@ -801,7 +796,7 @@ var LibraryBrowser = {
emscripten_destroy_worker: (id) => {
var info = Browser.workers[id];
info.worker.terminate();
if (info.buffer) _free(info.buffer);
_free(info.buffer);
Browser.workers[id] = null;
},

Expand Down
10 changes: 3 additions & 7 deletions src/lib/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1299,20 +1299,16 @@ addToLibrary({
// Mark as `noleakcheck` otherwise lsan will report the last returned string
// as a leak.
emscripten_run_script_string__noleakcheck: true,
emscripten_run_script_string__deps: ['$lengthBytesUTF8', '$stringToUTF8', 'malloc'],
emscripten_run_script_string__deps: ['$lengthBytesUTF8', '$stringToUTF8', 'realloc'],
emscripten_run_script_string: (ptr) => {
{{{ makeEval("var s = eval(UTF8ToString(ptr));") }}}
if (s == null) {
return 0;
}
s += '';
var me = _emscripten_run_script_string;
var len = lengthBytesUTF8(s);
if (!me.bufferSize || me.bufferSize < len+1) {
if (me.bufferSize) _free(me.buffer);
me.bufferSize = len+1;
me.buffer = _malloc(me.bufferSize);
}
me.bufferSize = lengthBytesUTF8(s) + 1;
me.buffer = _realloc(me.buffer ?? 0, me.bufferSize)
stringToUTF8(s, me.buffer, me.bufferSize);
return me.buffer;
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/libsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ var LibrarySDL = {

var info = SDL.surfaces[surf];
if (!info.usePageCanvas && info.canvas) SDL.canvasPool.push(info.canvas);
if (info.buffer) _free(info.buffer);
_free(info.buffer);
_free(info.pixelFormat);
_free(surf);
SDL.surfaces[surf] = null;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/libstack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ var LibraryStackTrace = {
} else {
name = wasmOffsetConverter.getName(pc);
}
if (_emscripten_pc_get_function.ret) _free(_emscripten_pc_get_function.ret);
_free(_emscripten_pc_get_function.ret ?? 0);
_emscripten_pc_get_function.ret = stringToNewUTF8(name);
return _emscripten_pc_get_function.ret;
#endif
Expand Down Expand Up @@ -311,7 +311,7 @@ var LibraryStackTrace = {
var result = convertPCtoSourceLocation(pc);
if (!result) return 0;

if (_emscripten_pc_get_file.ret) _free(_emscripten_pc_get_file.ret);
_free(_emscripten_pc_get_file.ret ?? 0);
_emscripten_pc_get_file.ret = stringToNewUTF8(result.file);
return _emscripten_pc_get_file.ret;
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/libwget.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var LibraryWget = {
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
if (onload) {{{ makeDynCall('vippi', 'onload') }}}(handle, userdata, buffer, byteArray.length);
if (free) _free(buffer);
_free(buffer);
} else {
onerrorjs();
}
Expand Down
8 changes: 2 additions & 6 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ run();
var workerResponded = false, workerCallbackId = -1;

(() => {
var messageBuffer = null, buffer = 0, bufferSize = 0;
var messageBuffer = null, buffer = 0;

function flushMessages() {
if (!messageBuffer) return;
Expand Down Expand Up @@ -396,11 +396,7 @@ var workerResponded = false, workerCallbackId = -1;
var data = msg.data['data'];
if (data) {
if (!data.byteLength) data = new Uint8Array(data);
if (!buffer || bufferSize < data.length) {
if (buffer) _free(buffer);
bufferSize = data.length;
buffer = _malloc(data.length);
}
buffer = _realloc(buffer, data.length);
HEAPU8.set(data, buffer);
}

Expand Down
1 change: 1 addition & 0 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ def create_pointer_conversion_wrappers(metadata):
'emscripten_builtin_calloc': 'ppp',
'wasmfs_create_node_backend': 'pp',
'malloc': 'pp',
'realloc': 'ppp',
'calloc': 'ppp',
'webidl_malloc': 'pp',
'memalign': 'ppp',
Expand Down
4 changes: 2 additions & 2 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,9 +1752,9 @@ def limit_incoming_module_api():
# need to be able to call these explicitly.
settings.REQUIRED_EXPORTS += ['__funcs_on_exit']

# The worker code in src/postamble.js depends on malloc/free being exported
# The worker code in src/postamble.js depends on realloc
if settings.BUILD_AS_WORKER:
settings.REQUIRED_EXPORTS += ['malloc', 'free']
settings.REQUIRED_EXPORTS += ['realloc']

if not settings.DISABLE_EXCEPTION_CATCHING:
settings.REQUIRED_EXPORTS += [
Expand Down