Skip to content

Commit 946c850

Browse files
authored
Remove remaining uses of setErrNo and deprecate SUPPORT_ERRNO setting (#21074)
Aside from the `setErrNo` library function `SUPPORT_ERRNO` also controlled the building the malloc and sbrk. However, just setting errno to ENOMEM didn't seem to have any effect on the code size for any of our micro benchmarks. This may be because LTO can completely eliminate the write to `errno` when there are no readers, or it could be that errno usage exists in in even the smallest program already so removing from malloc alone makes no difference.
1 parent 9fe607b commit 946c850

18 files changed

+58
-60
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ See docs/process.md for more on how version tagging works.
5757
- C++ objects passed into embind's val via constructors, methods, and call
5858
function will not be automatically destroyed after the function call. This
5959
makes the behavior consistent for invocations.
60+
- The `SUPPORT_ERRNO` setting is now deprecated as it only controlled setting
61+
errno from JS library functions and emscripten no longer requires this.
62+
(#21074)
6063

6164
3.1.51 - 12/13/23
6265
-----------------

embuilder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
'libc++-noexcept',
4747
'libal',
4848
'libdlmalloc',
49-
'libdlmalloc-noerrno',
5049
'libdlmalloc-tracing',
5150
'libdlmalloc-debug',
5251
'libembind',

site/source/docs/tools_reference/settings_reference.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ options: 'quiet', 'warn', 'error'. If set to 'warn', Closure warnings are
315315
printed out to console. If set to 'error', Closure warnings are treated like
316316
errors, similar to -Werror compiler flag.
317317

318+
.. note:: This setting is deprecated
319+
318320
.. _ignore_closure_compiler_errors:
319321

320322
IGNORE_CLOSURE_COMPILER_ERRORS
@@ -1177,6 +1179,8 @@ EXTRA_EXPORTED_RUNTIME_METHODS
11771179

11781180
Deprecated, use EXPORTED_RUNTIME_METHODS instead.
11791181

1182+
.. note:: This setting is deprecated
1183+
11801184
.. _incoming_module_js_api:
11811185

11821186
INCOMING_MODULE_JS_API
@@ -2447,13 +2451,11 @@ Minimum supported value is 101900, which was released 2020-02-05.
24472451
SUPPORT_ERRNO
24482452
=============
24492453

2450-
Tracks whether we are building with errno support enabled. Set to 0
2451-
to disable compiling errno support in altogether. This saves a little
2452-
bit of generated code size in applications that do not care about
2453-
POSIX errno variable. Setting this to 0 also requires using --closure
2454-
for effective code size optimizations to take place.
2454+
Whether we support setting errno from JS library code.
24552455
In MINIMAL_RUNTIME builds, this option defaults to 0.
24562456

2457+
.. note:: This setting is deprecated
2458+
24572459
.. _minimal_runtime:
24582460

24592461
MINIMAL_RUNTIME

src/library.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ addToLibrary({
328328
updateMemoryViews();
329329
},
330330

331-
system__deps: ['$setErrNo'],
332-
system: (command) => {
331+
_emscripten_system: (command) => {
333332
#if ENVIRONMENT_MAY_BE_NODE
334333
if (ENVIRONMENT_IS_NODE) {
335334
if (!command) return 1; // shell is available
@@ -368,8 +367,7 @@ addToLibrary({
368367
// http://pubs.opengroup.org/onlinepubs/000095399/functions/system.html
369368
// Can't call external programs.
370369
if (!command) return 0; // no shell available
371-
setErrNo({{{ cDefs.ENOSYS }}});
372-
return -1;
370+
return -{{{ cDefs.ENOSYS }}};
373371
},
374372

375373
// ==========================================================================
@@ -1522,20 +1520,6 @@ addToLibrary({
15221520
{{{ cDefs.EOWNERDEAD }}}: 'Previous owner died',
15231521
{{{ cDefs.ESTRPIPE }}}: 'Streams pipe error',
15241522
},
1525-
#if SUPPORT_ERRNO
1526-
$setErrNo__deps: ['__errno_location'],
1527-
$setErrNo: (value) => {
1528-
{{{makeSetValue("___errno_location()", 0, 'value', 'i32') }}};
1529-
return value;
1530-
},
1531-
#else
1532-
$setErrNo: (value) => {
1533-
#if ASSERTIONS
1534-
err('failed to set errno from JS');
1535-
#endif
1536-
return 0;
1537-
},
1538-
#endif
15391523

15401524
#if PROXY_POSIX_SOCKETS == 0
15411525
// ==========================================================================
@@ -2138,11 +2122,10 @@ addToLibrary({
21382122
// nonblocking
21392123
// ==========================================================================
21402124
#if SOCKET_WEBRTC
2141-
$Sockets__deps: ['$setErrNo',
2125+
$Sockets__deps: [
21422126
() => 'var SocketIO = ' + read('../third_party/socket.io.js') + ';\n',
2143-
() => 'var Peer = ' + read('../third_party/wrtcp.js') + ';\n'],
2144-
#else
2145-
$Sockets__deps: ['$setErrNo'],
2127+
() => 'var Peer = ' + read('../third_party/wrtcp.js') + ';\n'
2128+
],
21462129
#endif
21472130
$Sockets: {
21482131
BUFFER_SIZE: 10*1024, // initial size

src/library_legacy.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,19 @@ addToLibrary({
7575

7676
$allocateUTF8: '$stringToNewUTF8',
7777
$allocateUTF8OnStack: '$stringToUTF8OnStack',
78+
79+
#if SUPPORT_ERRNO
80+
$setErrNo__deps: ['__errno_location'],
81+
$setErrNo: (value) => {
82+
{{{makeSetValue("___errno_location()", 0, 'value', 'i32') }}};
83+
return value;
84+
},
85+
#else
86+
$setErrNo: (value) => {
87+
#if ASSERTIONS
88+
err('failed to set errno from JS');
89+
#endif
90+
return 0;
91+
},
92+
#endif
7893
});

src/library_sigs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ sigs = {
331331
_emscripten_receive_on_main_thread_js__sig: 'dipip',
332332
_emscripten_runtime_keepalive_clear__sig: 'v',
333333
_emscripten_set_offscreencanvas_size__sig: 'ipii',
334+
_emscripten_system__sig: 'ip',
334335
_emscripten_thread_exit_joinable__sig: 'vp',
335336
_emscripten_thread_mailbox_await__sig: 'vp',
336337
_emscripten_thread_set_strongref__sig: 'vp',
@@ -1541,7 +1542,6 @@ sigs = {
15411542
strftime_l__sig: 'pppppp',
15421543
strptime__sig: 'pppp',
15431544
strptime_l__sig: 'ppppp',
1544-
system__sig: 'ip',
15451545
uuid_clear__sig: 'vp',
15461546
uuid_compare__sig: 'ipp',
15471547
uuid_copy__sig: 'vpp',

src/settings.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ var USE_CLOSURE_COMPILER = false;
274274
// printed out to console. If set to 'error', Closure warnings are treated like
275275
// errors, similar to -Werror compiler flag.
276276
// [link]
277+
// [deprecated]
277278
var CLOSURE_WARNINGS = 'quiet';
278279

279280
// Ignore closure warnings and errors (like on duplicate definitions)
@@ -922,6 +923,7 @@ var ASYNCIFY_EXPORTS = [];
922923
var EXPORTED_RUNTIME_METHODS = [];
923924

924925
// Deprecated, use EXPORTED_RUNTIME_METHODS instead.
926+
// [deprecated]
925927
var EXTRA_EXPORTED_RUNTIME_METHODS = [];
926928

927929
// A list of incoming values on the Module object in JS that we care about. If
@@ -1839,13 +1841,10 @@ var MIN_CHROME_VERSION = 85;
18391841
// Minimum supported value is 101900, which was released 2020-02-05.
18401842
var MIN_NODE_VERSION = 160000;
18411843

1842-
// Tracks whether we are building with errno support enabled. Set to 0
1843-
// to disable compiling errno support in altogether. This saves a little
1844-
// bit of generated code size in applications that do not care about
1845-
// POSIX errno variable. Setting this to 0 also requires using --closure
1846-
// for effective code size optimizations to take place.
1844+
// Whether we support setting errno from JS library code.
18471845
// In MINIMAL_RUNTIME builds, this option defaults to 0.
18481846
// [link]
1847+
// [deprecated]
18491848
var SUPPORT_ERRNO = true;
18501849

18511850
// If true, uses minimal sized runtime without POSIX features, Module,

system/lib/libc/emscripten_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ void llvm_eh_typeid_for(void* exn);
148148

149149
uint32_t _emscripten_lookup_name(const char *name);
150150

151+
int _emscripten_system(const char *command);
152+
151153
#ifdef __cplusplus
152154
}
153155
#endif

system/lib/libc/sbrk.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
#define __NEED_max_align_t
1313
#endif
1414

15-
#ifndef EMSCRIPTEN_NO_ERRNO
1615
#include <errno.h>
17-
#endif
1816
#include <limits.h>
1917
#include <stddef.h>
2018
#include <stdint.h>
@@ -31,12 +29,6 @@ void emscripten_memprof_sbrk_grow(intptr_t old, intptr_t new);
3129

3230
#include <emscripten/heap.h>
3331

34-
#ifndef EMSCRIPTEN_NO_ERRNO
35-
#define SET_ERRNO() { errno = ENOMEM; }
36-
#else
37-
#define SET_ERRNO()
38-
#endif
39-
4032
extern size_t __heap_base;
4133

4234
static uintptr_t sbrk_val = (uintptr_t)&__heap_base;
@@ -79,7 +71,7 @@ void *sbrk(intptr_t increment_) {
7971
// increase the WebAssembly Memory size, and abort if that fails.
8072
if ((increment > 0 && new_brk <= old_brk)
8173
|| (new_brk > emscripten_get_heap_size() && !emscripten_resize_heap(new_brk))) {
82-
SET_ERRNO();
74+
errno = ENOMEM;
8375
return (void*)-1;
8476
}
8577
#ifdef __EMSCRIPTEN_SHARED_MEMORY__

system/lib/libc/system.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <unistd.h>
2+
#include <stdlib.h>
3+
#include <errno.h>
4+
#include "pthread_impl.h"
5+
#include "emscripten_internal.h"
6+
7+
int system(const char *cmd)
8+
{
9+
return __syscall_ret(_emscripten_system(cmd));
10+
}

test/minimal_webgl/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ if (EMSCRIPTEN)
7171
# Optimization flag to optimize aggressively for size. (other options -Os, -O3, -O2, -O1, -O0)
7272
append_linker_flags("-Oz")
7373

74-
# Reduce code size: We do not need libc errno field support in our build output.
75-
append_linker_flags("-sSUPPORT_ERRNO=0")
76-
7774
# Reduce code size: We do not need native POSIX filesystem emulation support (Emscripten FS/MEMFS)
7875
append_linker_flags("-sFILESYSTEM=0")
7976
endif()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8422
1+
8420
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7251
1+
7250
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
57121
1+
57107

test/test_other.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10596,7 +10596,6 @@ def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False)
1059610596
'-sTEXTDECODER=2',
1059710597
'-sABORTING_MALLOC=0',
1059810598
'-sALLOW_MEMORY_GROWTH=0',
10599-
'-sSUPPORT_ERRNO=0',
1060010599
'-sDECLARE_ASM_MODULE_EXPORTS',
1060110600
'-sMALLOC=emmalloc',
1060210601
'-sGL_EMULATE_GLES_VERSION_STRING_FORMAT=0',
@@ -11921,7 +11920,7 @@ def test_default_to_cxx(self):
1192111920
'minimal': (['-sMINIMAL_RUNTIME', '-sSUPPORT_ERRNO'],),
1192211921
})
1192311922
def test_support_errno(self, args):
11924-
self.emcc_args += args + ['-sEXPORTED_FUNCTIONS=_main,___errno_location']
11923+
self.emcc_args += args + ['-sEXPORTED_FUNCTIONS=_main,___errno_location', '-Wno-deprecated']
1192511924

1192611925
self.do_other_test('test_support_errno.c')
1192711926
size_default = os.path.getsize('test_support_errno.js')

tools/link.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ def phase_linker_setup(options, state, newargs):
729729

730730
final_suffix = get_file_suffix(target)
731731

732+
if 'SUPPORT_ERRNO' in user_settings:
733+
diagnostics.warning('deprecated', 'SUPPORT_ERRNO is deprecated since emscripten no longer uses the setErrNo library function')
734+
732735
if settings.EXTRA_EXPORTED_RUNTIME_METHODS:
733736
diagnostics.warning('deprecated', 'EXTRA_EXPORTED_RUNTIME_METHODS is deprecated, please use EXPORTED_RUNTIME_METHODS instead')
734737
settings.EXPORTED_RUNTIME_METHODS += settings.EXTRA_EXPORTED_RUNTIME_METHODS
@@ -879,7 +882,6 @@ def phase_linker_setup(options, state, newargs):
879882
# It is unlikely that developers targeting "native web" APIs with MINIMAL_RUNTIME need
880883
# errno support by default.
881884
if settings.MINIMAL_RUNTIME:
882-
default_setting('SUPPORT_ERRNO', 0)
883885
# Require explicit -lfoo.js flags to link with JS libraries.
884886
default_setting('AUTO_JS_LIBRARIES', 0)
885887
# When using MINIMAL_RUNTIME, symbols should only be exported if requested.

tools/maint/update_settings_docs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'compile+link': 'Applicable during both linking and compilation',
5151
'compile': 'Only applicable during compilation',
5252
'experimental': 'This is an experimental setting',
53+
'deprecated': 'This setting is deprecated',
5354
}
5455

5556
output_file = path_from_root('site/source/docs/tools_reference/settings_reference.rst')

tools/system_libs.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ def get_files(self):
12671267
'sigtimedwait.c',
12681268
'wasi-helpers.c',
12691269
'sbrk.c',
1270+
'system.c',
12701271
])
12711272

12721273
if settings.RELOCATABLE:
@@ -1666,7 +1667,6 @@ def __init__(self, **kwargs):
16661667
if self.malloc not in ('dlmalloc', 'emmalloc', 'emmalloc-debug', 'emmalloc-memvalidate', 'emmalloc-verbose', 'emmalloc-memvalidate-verbose', 'mimalloc', 'none'):
16671668
raise Exception('malloc must be one of "emmalloc[-debug|-memvalidate][-verbose]", "dlmalloc" or "none", see settings.js')
16681669

1669-
self.use_errno = kwargs.pop('use_errno')
16701670
self.is_tracing = kwargs.pop('is_tracing')
16711671
self.memvalidate = kwargs.pop('memvalidate')
16721672
self.verbose = kwargs.pop('verbose')
@@ -1691,8 +1691,6 @@ def get_cflags(self):
16911691
cflags += ['-UNDEBUG', '-DDLMALLOC_DEBUG']
16921692
else:
16931693
cflags += ['-DNDEBUG']
1694-
if not self.use_errno:
1695-
cflags += ['-DMALLOC_FAILURE_ACTION=', '-DEMSCRIPTEN_NO_ERRNO']
16961694
if self.is_tracing:
16971695
cflags += ['--tracing']
16981696
return cflags
@@ -1704,9 +1702,6 @@ def get_base_name(self):
17041702
name = super().get_base_name()
17051703
if self.is_debug and not self.memvalidate and not self.verbose:
17061704
name += '-debug'
1707-
if not self.use_errno:
1708-
# emmalloc doesn't actually use errno, but it's easier to build it again
1709-
name += '-noerrno'
17101705
if self.is_tracing:
17111706
name += '-tracing'
17121707
return name
@@ -1716,14 +1711,13 @@ def can_use(self):
17161711

17171712
@classmethod
17181713
def vary_on(cls):
1719-
return super().vary_on() + ['is_debug', 'use_errno', 'is_tracing', 'memvalidate', 'verbose']
1714+
return super().vary_on() + ['is_debug', 'is_tracing', 'memvalidate', 'verbose']
17201715

17211716
@classmethod
17221717
def get_default_variation(cls, **kwargs):
17231718
return super().get_default_variation(
17241719
malloc=settings.MALLOC,
17251720
is_debug=settings.ASSERTIONS >= 2,
1726-
use_errno=settings.SUPPORT_ERRNO,
17271721
is_tracing=settings.EMSCRIPTEN_TRACING,
17281722
memvalidate='memvalidate' in settings.MALLOC,
17291723
verbose='verbose' in settings.MALLOC,

0 commit comments

Comments
 (0)