Skip to content

Commit 473ba1d

Browse files
committed
More fixes for 4gb and 2gb high memory modes
1 parent 97053db commit 473ba1d

File tree

7 files changed

+83
-70
lines changed

7 files changed

+83
-70
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ jobs:
819819
browser_2gb.test_emscripten_animate_canvas_element_size_manual_css
820820
browser_2gb.test_fulles2_sdlproc
821821
browser_2gb.test_cubegeom*
822+
browser_2gb.test_html5_webgl_create_context*
822823
"
823824
test-browser-chrome-wasm64-4gb:
824825
executor: bionic
@@ -834,6 +835,7 @@ jobs:
834835
browser64_4gb.test_fetch*
835836
browser64_4gb.test_emscripten_animate_canvas_element_size_manual_css
836837
browser64_4gb.test_fulles2_sdlproc
838+
browser64_4gb.test_html5_webgl_create_context*
837839
"
838840
test-browser-firefox:
839841
executor: bionic

src/library_html5_webgl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ var LibraryHtml5WebGL = {
1414
var len = arr.length;
1515
var writeLength = dstLength < len ? dstLength : len;
1616
var heap = heapType ? HEAPF32 : HEAP32;
17+
dst = {{{ getHeapOffset('dst', 'float') }}};
1718
for (var i = 0; i < writeLength; ++i) {
18-
heap[(dst >> 2) + i] = arr[i];
19+
heap[dst + i] = arr[i];
1920
}
2021
return len;
2122
},

system/include/emscripten/html5_webgl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
extern "C" {
1515
#endif
1616

17-
typedef intptr_t EMSCRIPTEN_WEBGL_CONTEXT_HANDLE;
17+
typedef uintptr_t EMSCRIPTEN_WEBGL_CONTEXT_HANDLE;
1818

1919
typedef int EMSCRIPTEN_WEBGL_CONTEXT_PROXY_MODE;
2020
#define EMSCRIPTEN_WEBGL_CONTEXT_PROXY_DISALLOW 0

test/browser/emmalloc_memgrowth.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,30 @@
55
uint64_t nextAllocationSize = 16*1024*1024;
66
bool allocHasFailed = false;
77

8-
void grow_memory()
9-
{
10-
uint8_t *ptr = (uint8_t*)malloc((size_t)nextAllocationSize);
11-
EM_ASM({}, ptr); // Pass ptr out to confuse LLVM that it is used, so it won't optimize it away in -O1 and higher.
12-
size_t heapSize = emscripten_get_heap_size();
13-
printf("Allocated %zu: %d. Heap size: %zu\n", (size_t)nextAllocationSize, ptr ? 1 : 0, heapSize);
14-
if (ptr)
15-
{
16-
if (!allocHasFailed)
17-
{
18-
nextAllocationSize *= 2;
19-
// Make sure we don't overflow, and also exercise malloc(-1) to gracefully return 0 in ABORTING_MALLOC=0 mode.
20-
if (nextAllocationSize > 0xFFFFFFFFULL)
21-
nextAllocationSize = 0xFFFFFFFFULL;
22-
}
23-
}
24-
else
25-
{
26-
nextAllocationSize /= 2;
27-
allocHasFailed = true;
28-
}
8+
void grow_memory() {
9+
uint8_t *ptr = (uint8_t*)malloc((size_t)nextAllocationSize);
10+
EM_ASM({}, ptr); // Pass ptr out to confuse LLVM that it is used, so it won't optimize it away in -O1 and higher.
11+
size_t heapSize = emscripten_get_heap_size();
12+
printf("Allocated %zu: %d. Heap size: %zu\n", (size_t)nextAllocationSize, ptr ? 1 : 0, heapSize);
13+
if (ptr) {
14+
if (!allocHasFailed) {
15+
nextAllocationSize *= 2;
16+
// Make sure we don't overflow, and also exercise malloc(-1) to gracefully return 0 in ABORTING_MALLOC=0 mode.
17+
if (nextAllocationSize > 0xFFFFFFFFULL)
18+
nextAllocationSize = 0xFFFFFFFFULL;
19+
}
20+
} else {
21+
nextAllocationSize /= 2;
22+
allocHasFailed = true;
23+
}
2924
}
3025

31-
int main()
32-
{
33-
// Exhaust all available memory.
34-
for(int i = 0; i < 50; ++i)
35-
grow_memory();
36-
// If we get this far without crashing on OOM, we are ok!
37-
printf("Test finished!\n");
38-
#ifdef REPORT_RESULT
39-
REPORT_RESULT(0);
40-
#endif
26+
int main() {
27+
// Exhaust all available memory.
28+
for(int i = 0; i < 50; ++i) {
29+
grow_memory();
30+
}
31+
// If we get this far without crashing on OOM, we are ok!
32+
printf("Test finished!\n");
33+
return 0;
4134
}

test/browser/webgl_create_context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void loop() {
6060
assert(emscripten_webgl_get_current_context() == 0);
6161
context = emscripten_webgl_create_context("#canvas", &attrs);
6262

63-
assert(context > 0); // Must have received a valid context.
63+
assert(context != 0); // Must have received a valid context.
6464
res = emscripten_webgl_make_context_current(context);
6565
assert(res == EMSCRIPTEN_RESULT_SUCCESS);
6666
assert(emscripten_webgl_get_current_context() == context);
@@ -104,7 +104,7 @@ int main() {
104104

105105
assert(emscripten_webgl_get_current_context() == 0);
106106
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#customCanvas", &attrs);
107-
assert(context > 0); // Must have received a valid context.
107+
assert(context != 0); // Must have received a valid context.
108108
EMSCRIPTEN_RESULT res = emscripten_webgl_make_context_current(context);
109109
assert(res == EMSCRIPTEN_RESULT_SUCCESS);
110110
assert(emscripten_webgl_get_current_context() == context);

test/fs/test_idbfs_sync.c

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515

1616
int result = 1;
1717

18-
void success()
19-
{
18+
void success() {
2019
REPORT_RESULT(result);
2120
#ifdef FORCE_EXIT
2221
emscripten_force_exit(0);
2322
#endif
2423
}
2524

2625
void test() {
27-
2826
int fd;
2927
struct stat st;
30-
28+
3129
#if FIRST
3230

3331
// for each file, we first make sure it doesn't currently exist
@@ -49,22 +47,20 @@ void test() {
4947
fd = open("/working1/waka.txt", O_RDWR | O_CREAT, 0666);
5048
if (fd == -1)
5149
result = -5000 - errno;
52-
else
53-
{
50+
else {
5451
if (write(fd,"az",2) != 2)
5552
result = -6000 - errno;
5653
if (close(fd) != 0)
5754
result = -7000 - errno;
5855
}
59-
56+
6057
// a file whose contents are random-ish string set by the test_browser.py file
6158
if ((stat("/working1/moar.txt", &st) != -1) || (errno != ENOENT))
6259
result = -8000 - errno;
6360
fd = open("/working1/moar.txt", O_RDWR | O_CREAT, 0666);
6461
if (fd == -1)
6562
result = -9000 - errno;
66-
else
67-
{
63+
else {
6864
if (write(fd, SECRET, strlen(SECRET)) != strlen(SECRET))
6965
result = -10000 - errno;
7066
if (close(fd) != 0)
@@ -92,8 +88,7 @@ void test() {
9288
fd = open("/working1/waka.txt", O_RDONLY);
9389
if (fd == -1)
9490
result = -17000 - errno;
95-
else
96-
{
91+
else {
9792
char bf[4];
9893
int bytes_read = read(fd,&bf[0],sizeof(bf));
9994
if (bytes_read != 2)
@@ -105,19 +100,17 @@ void test() {
105100
if (unlink("/working1/waka.txt") != 0)
106101
result = -21000 - errno;
107102
}
108-
103+
109104
// does the random-ish file exist and does it contain SECRET?
110105
fd = open("/working1/moar.txt", O_RDONLY);
111-
if (fd == -1)
106+
if (fd == -1) {
112107
result = -22000 - errno;
113-
else
114-
{
108+
} else {
115109
char bf[256];
116110
int bytes_read = read(fd,&bf[0],sizeof(bf));
117-
if (bytes_read != strlen(SECRET))
111+
if (bytes_read != strlen(SECRET)) {
118112
result = -23000;
119-
else
120-
{
113+
} else {
121114
bf[strlen(SECRET)] = 0;
122115
if (strcmp(bf,SECRET) != 0)
123116
result = -24000;
@@ -129,14 +122,13 @@ void test() {
129122
}
130123

131124
// does the directory exist?
132-
if (stat("/working1/dir", &st) != 0)
125+
if (stat("/working1/dir", &st) != 0) {
133126
result = -27000 - errno;
134-
else
135-
{
127+
} else {
136128
if (!S_ISDIR(st.st_mode))
137129
result = -28000;
138-
if (rmdir("/working1/dir") != 0)
139-
result = -29000 - errno;
130+
if (rmdir("/working1/dir") != 0)
131+
result = -29000 - errno;
140132
}
141133

142134
#endif
@@ -164,20 +156,18 @@ void test() {
164156
ccall('success', 'v');
165157
});
166158
);
167-
168159
}
169160

170161
int main() {
171-
172162
EM_ASM(
173163
FS.mkdir('/working1');
174164
FS.mount(IDBFS, {}, '/working1');
175165

176166
#if !FIRST
177-
// syncfs(true, f) should not break on already-existing directories:
178-
FS.mkdir('/working1/dir');
167+
// syncfs(true, f) should not break on already-existing directories:
168+
FS.mkdir('/working1/dir');
179169
#endif
180-
170+
181171
// sync from persisted state into memory and then
182172
// run the 'test' function
183173
FS.syncfs(true, function (err) {
@@ -187,6 +177,5 @@ int main() {
187177
);
188178

189179
emscripten_exit_with_live_runtime();
190-
191180
return 0;
192181
}

test/test_browser.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ def setUp(self):
237237
def require_wasm2js(self):
238238
if self.is_wasm64():
239239
self.skipTest('wasm2js is not compatible with MEMORY64')
240+
if self.is_2gb() or self.is_4gb():
241+
self.skipTest('wasm2js does not support over 2gb of memory')
240242

241243
def require_jspi(self):
242244
if not is_chrome():
@@ -1525,7 +1527,9 @@ def test_idbstore_sync(self, asyncify):
15251527

15261528
def test_idbstore_sync_worker(self):
15271529
secret = str(time.time())
1528-
self.btest('test_idbstore_sync_worker.c', expected='0', args=['-lidbstore.js', f'-DSECRET="{secret}"', '-O3', '-g2', '--proxy-to-worker', '-sINITIAL_MEMORY=80MB', '-sASYNCIFY'])
1530+
if not self.is_2gb() and not self.is_4gb():
1531+
self.set_setting('INITIAL_MEMORY', '80mb')
1532+
self.btest('test_idbstore_sync_worker.c', expected='0', args=['-lidbstore.js', f'-DSECRET="{secret}"', '-O3', '-g2', '--proxy-to-worker', '-sASYNCIFY'])
15291533

15301534
def test_force_exit(self):
15311535
self.btest_exit('force_exit.c', assert_returncode=10)
@@ -1816,6 +1820,7 @@ def test_glgears_animation(self, filename):
18161820
self.run_browser('something.html', '/report_gl_result?true')
18171821

18181822
@requires_graphics_hardware
1823+
@no_4gb('assertion fails')
18191824
def test_fulles2_sdlproc(self):
18201825
self.btest_exit('full_es2_sdlproc.c', assert_returncode=1, args=['-sGL_TESTING', '-DHAVE_BUILTIN_SINCOS', '-sFULL_ES2', '-lGL', '-lSDL', '-lglut', '-sGL_ENABLE_GET_PROC_ADDRESS'])
18211826

@@ -1826,6 +1831,7 @@ def test_glgears_deriv(self):
18261831
assert 'gl-matrix' not in read_file('test.html'), 'Should not include glMatrix when not needed'
18271832

18281833
@requires_graphics_hardware
1834+
@no_4gb('fails to render')
18291835
def test_glbook(self):
18301836
self.emcc_args.append('-Wno-int-conversion')
18311837
self.emcc_args.append('-Wno-pointer-sign')
@@ -1859,6 +1865,7 @@ def book_path(path):
18591865
args=args)
18601866

18611867
@requires_graphics_hardware
1868+
@no_4gb('fails to render')
18621869
@parameterized({
18631870
'normal': (['-sFULL_ES2'],),
18641871
# Enabling FULL_ES3 also enables ES2 automatically
@@ -2017,6 +2024,7 @@ def test_gl_glteximage(self):
20172024
})
20182025
@requires_graphics_hardware
20192026
@requires_threads
2027+
@no_4gb('assertion failure')
20202028
def test_gl_textures(self, args):
20212029
self.btest_exit('gl_textures.cpp', args=['-lGL', '-g', '-sSTACK_SIZE=1MB'] + args)
20222030

@@ -2047,14 +2055,18 @@ def test_gl_renderers(self):
20472055
self.btest('gl_renderers.c', reference='gl_renderers.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
20482056

20492057
@requires_graphics_hardware
2058+
@no_2gb('render fails')
2059+
@no_4gb('render fails')
20502060
def test_gl_stride(self):
20512061
self.btest('gl_stride.c', reference='gl_stride.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
20522062

20532063
@requires_graphics_hardware
2064+
@no_4gb('assertion failure')
20542065
def test_gl_vertex_buffer_pre(self):
20552066
self.btest('gl_vertex_buffer_pre.c', reference='gl_vertex_buffer_pre.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
20562067

20572068
@requires_graphics_hardware
2069+
@no_4gb('assertion failure')
20582070
def test_gl_vertex_buffer(self):
20592071
self.btest('gl_vertex_buffer.c', reference='gl_vertex_buffer.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], reference_slack=1)
20602072

@@ -2310,6 +2322,7 @@ def test_tex_nonbyte(self):
23102322
self.btest('tex_nonbyte.c', reference='tex_nonbyte.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
23112323

23122324
@requires_graphics_hardware
2325+
@no_4gb('fails to render')
23132326
def test_float_tex(self):
23142327
self.btest('float_tex.cpp', reference='float_tex.png', args=['-lGL', '-lglut'])
23152328

@@ -5571,6 +5584,7 @@ def test_4gb(self):
55715584

55725585
# Tests that emmalloc supports up to 4GB Wasm heaps.
55735586
@no_firefox('no 4GB support yet')
5587+
@no_4gb('uses MAXIMUM_MEMORY')
55745588
def test_emmalloc_4gb(self):
55755589
# For now, keep this in browser as this suite runs serially, which
55765590
# means we don't compete for memory with anything else (and run it
@@ -5581,21 +5595,29 @@ def test_emmalloc_4gb(self):
55815595
# Test that it is possible to malloc() a huge 3GB memory block in 4GB mode using emmalloc.
55825596
# Also test emmalloc-memvalidate and emmalloc-memvalidate-verbose build configurations.
55835597
@no_firefox('no 4GB support yet')
5598+
@no_2gb('not enough space tp run in this mode')
55845599
@parameterized({
55855600
'': (['-sMALLOC=emmalloc'],),
55865601
'debug': (['-sMALLOC=emmalloc-debug'],),
55875602
'memvalidate': (['-sMALLOC=emmalloc-memvalidate'],),
55885603
'memvalidate_verbose': (['-sMALLOC=emmalloc-memvalidate-verbose'],),
55895604
})
55905605
def test_emmalloc_3gb(self, args):
5591-
self.btest_exit('alloc_3gb.c',
5592-
args=['-sMAXIMUM_MEMORY=4GB', '-sALLOW_MEMORY_GROWTH=1'] + args)
5606+
if self.is_4gb():
5607+
self.set_setting('MAXIMUM_MEMORY', '8GB')
5608+
else:
5609+
self.set_setting('MAXIMUM_MEMORY', '4GB')
5610+
self.btest_exit('alloc_3gb.c', args=['-sALLOW_MEMORY_GROWTH=1'] + args)
55935611

55945612
# Test that it is possible to malloc() a huge 3GB memory block in 4GB mode using dlmalloc.
55955613
@no_firefox('no 4GB support yet')
5614+
@no_2gb('not enough space tp run in this mode')
55965615
def test_dlmalloc_3gb(self):
5597-
self.btest_exit('alloc_3gb.c',
5598-
args=['-sMALLOC=dlmalloc', '-sMAXIMUM_MEMORY=4GB', '-sALLOW_MEMORY_GROWTH=1'])
5616+
if self.is_4gb():
5617+
self.set_setting('MAXIMUM_MEMORY', '8GB')
5618+
else:
5619+
self.set_setting('MAXIMUM_MEMORY', '4GB')
5620+
self.btest_exit('alloc_3gb.c', args=['-sMALLOC=dlmalloc', '-sALLOW_MEMORY_GROWTH=1'])
55995621

56005622
@no_wasm64()
56015623
@parameterized({
@@ -5649,7 +5671,9 @@ def test_wasmfs_opfs_errors(self):
56495671

56505672
@no_firefox('no 4GB support yet')
56515673
def test_emmalloc_memgrowth(self, *args):
5652-
self.btest('emmalloc_memgrowth.cpp', expected='0', args=['-sMALLOC=emmalloc', '-sALLOW_MEMORY_GROWTH=1', '-sABORTING_MALLOC=0', '-sASSERTIONS=2', '-sMINIMAL_RUNTIME=1', '-sMAXIMUM_MEMORY=4GB'])
5674+
if not self.is_4gb():
5675+
self.set_setting('MAXIMUM_MEMORY', '4GB')
5676+
self.btest_exit('emmalloc_memgrowth.cpp', args=['-sMALLOC=emmalloc', '-sALLOW_MEMORY_GROWTH=1', '-sABORTING_MALLOC=0', '-sASSERTIONS=2', '-sMINIMAL_RUNTIME=1'])
56535677

56545678
@no_firefox('no 4GB support yet')
56555679
@no_2gb('uses MAXIMUM_MEMORY')
@@ -5902,6 +5926,8 @@ def setUp(self):
59025926
self.set_setting('INITIAL_MEMORY', '4200mb')
59035927
self.set_setting('GLOBAL_BASE', '4gb')
59045928
self.emcc_args.append('-Wno-experimental')
5929+
# Without this we get a warning about GLOBAL_BASE being ignored when used with SIDE_MODULE
5930+
self.emcc_args.append('-Wno-unused-command-line-argument')
59055931
self.require_wasm64()
59065932

59075933

@@ -5920,3 +5946,5 @@ def setUp(self):
59205946
super().setUp()
59215947
self.set_setting('INITIAL_MEMORY', '2200mb')
59225948
self.set_setting('GLOBAL_BASE', '2gb')
5949+
# Without this we get a warning about GLOBAL_BASE being ignored when used with SIDE_MODULE
5950+
self.emcc_args.append('-Wno-unused-command-line-argument')

0 commit comments

Comments
 (0)