Skip to content

Commit 012d785

Browse files
committed
Forward port changes from v0.6 release branch
Merge fixes from release-0.6 branch, such as memory corruption with erlang:make_tuple/2, emscripten tests and release step, advanced tracing build and improve valgrind CI step.
2 parents c60d126 + 426d9a1 commit 012d785

File tree

12 files changed

+69
-56
lines changed

12 files changed

+69
-56
lines changed

.github/workflows/build-and-test.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,37 +367,37 @@ jobs:
367367
working-directory: build
368368
run: |
369369
ulimit -c unlimited
370+
valgrind --error-exitcode=1 ./tests/test-erlang -s prime_smp
370371
./tests/test-erlang -s prime_smp
371-
valgrind ./tests/test-erlang -s prime_smp
372372
373373
- name: "Test: test-enif"
374374
working-directory: build
375375
run: |
376376
ulimit -c unlimited
377+
valgrind --error-exitcode=1 ./tests/test-enif
377378
./tests/test-enif
378-
valgrind ./tests/test-enif
379379
380380
- name: "Test: test-heap"
381381
working-directory: build
382382
run: |
383383
ulimit -c unlimited
384+
valgrind --error-exitcode=1 ./tests/test-heap
384385
./tests/test-heap
385-
valgrind ./tests/test-heap
386386
387387
- name: "Test: test-mailbox"
388388
working-directory: build
389389
run: |
390390
ulimit -c unlimited
391+
valgrind --error-exitcode=1 ./tests/test-mailbox
391392
./tests/test-mailbox
392-
valgrind ./tests/test-mailbox
393393
394394
- name: "Test: test-structs"
395395
timeout-minutes: 10
396396
working-directory: build
397397
run: |
398398
ulimit -c unlimited
399+
valgrind --error-exitcode=1 ./tests/test-structs
399400
./tests/test-structs
400-
valgrind ./tests/test-structs
401401
402402
- name: "Test: test_etest.avm"
403403
timeout-minutes: 5
@@ -412,24 +412,24 @@ jobs:
412412
working-directory: build
413413
run: |
414414
ulimit -c unlimited
415+
valgrind --error-exitcode=1 ./src/AtomVM ./tests/libs/estdlib/test_estdlib.avm
415416
./src/AtomVM ./tests/libs/estdlib/test_estdlib.avm
416-
valgrind ./src/AtomVM ./tests/libs/estdlib/test_estdlib.avm
417417
418418
- name: "Test: test_eavmlib.avm"
419419
timeout-minutes: 10
420420
working-directory: build
421421
run: |
422422
ulimit -c unlimited
423+
valgrind --error-exitcode=1 ./src/AtomVM ./tests/libs/eavmlib/test_eavmlib.avm
423424
./src/AtomVM ./tests/libs/eavmlib/test_eavmlib.avm
424-
valgrind ./src/AtomVM ./tests/libs/eavmlib/test_eavmlib.avm
425425
426426
- name: "Test: test_alisp.avm"
427427
timeout-minutes: 10
428428
working-directory: build
429429
run: |
430430
ulimit -c unlimited
431+
valgrind --error-exitcode=1 ./src/AtomVM ./tests/libs/alisp/test_alisp.avm
431432
./src/AtomVM ./tests/libs/alisp/test_alisp.avm
432-
valgrind ./src/AtomVM ./tests/libs/alisp/test_alisp.avm
433433
434434
- name: "Test: Tests.avm (Elixir)"
435435
timeout-minutes: 10
@@ -438,8 +438,8 @@ jobs:
438438
ulimit -c unlimited
439439
if command -v elixirc &> /dev/null
440440
then
441+
valgrind --error-exitcode=1 ./src/AtomVM ./tests/libs/exavmlib/Tests.avm
441442
./src/AtomVM ./tests/libs/exavmlib/Tests.avm
442-
valgrind ./src/AtomVM ./tests/libs/exavmlib/Tests.avm
443443
fi
444444
445445
- name: "Install and smoke test"

.github/workflows/build-libraries.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ jobs:
8989
timeout-minutes: 10
9090
working-directory: build
9191
run: |
92+
valgrind --error-exitcode=1 ./src/AtomVM ./tests/libs/estdlib/test_estdlib.avm
9293
./src/AtomVM ./tests/libs/estdlib/test_estdlib.avm
93-
valgrind ./src/AtomVM ./tests/libs/estdlib/test_estdlib.avm
9494
9595
- name: "Rename and write sha256sum"
9696
working-directory: build

.github/workflows/wasm-build.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ jobs:
124124
ATOMVM_JS=AtomVM-node-${{ github.ref_name }}.js
125125
mv AtomVM.js "${ATOMVM_JS}"
126126
sha256sum "${ATOMVM_JS}" > "${ATOMVM_JS}.sha256"
127-
ATOMVM_WORKER_JS=AtomVM.worker-node-${{ github.ref_name }}.js
128-
mv AtomVM.worker.js "${ATOMVM_WORKER_JS}"
129-
sha256sum "${ATOMVM_WORKER_JS}" > "${ATOMVM_WORKER_JS}.sha256"
130127
ATOMVM_WASM=AtomVM-node-${{ github.ref_name }}.wasm
131128
mv AtomVM.wasm "${ATOMVM_WASM}"
132129
sha256sum "${ATOMVM_WASM}" > "${ATOMVM_WASM}.sha256"
@@ -140,8 +137,6 @@ jobs:
140137
files: |
141138
src/platforms/emscripten/build/src/AtomVM-node-${{ github.ref_name }}.js
142139
src/platforms/emscripten/build/src/AtomVM-node-${{ github.ref_name }}.js.sha256
143-
src/platforms/emscripten/build/src/AtomVM.worker-node-${{ github.ref_name }}.js
144-
src/platforms/emscripten/build/src/AtomVM.worker-node-${{ github.ref_name }}.js.sha256
145140
src/platforms/emscripten/build/src/AtomVM-node-${{ github.ref_name }}.wasm
146141
src/platforms/emscripten/build/src/AtomVM-node-${{ github.ref_name }}.wasm.sha256
147142
@@ -227,9 +222,6 @@ jobs:
227222
ATOMVM_JS=AtomVM-web-${{ github.ref_name }}.js
228223
mv AtomVM.js "${ATOMVM_JS}"
229224
sha256sum "${ATOMVM_JS}" > "${ATOMVM_JS}.sha256"
230-
ATOMVM_WORKER_JS=AtomVM.worker-web-${{ github.ref_name }}.js
231-
mv AtomVM.worker.js "${ATOMVM_WORKER_JS}"
232-
sha256sum "${ATOMVM_WORKER_JS}" > "${ATOMVM_WORKER_JS}.sha256"
233225
ATOMVM_WASM=AtomVM-web-${{ github.ref_name }}.wasm
234226
mv AtomVM.wasm "${ATOMVM_WASM}"
235227
sha256sum "${ATOMVM_WASM}" > "${ATOMVM_WASM}.sha256"
@@ -243,7 +235,5 @@ jobs:
243235
files: |
244236
src/platforms/emscripten/build/src/AtomVM-web-${{ github.ref_name }}.js
245237
src/platforms/emscripten/build/src/AtomVM-web-${{ github.ref_name }}.js.sha256
246-
src/platforms/emscripten/build/src/AtomVM.worker-web-${{ github.ref_name }}.js
247-
src/platforms/emscripten/build/src/AtomVM.worker-web-${{ github.ref_name }}.js.sha256
248238
src/platforms/emscripten/build/src/AtomVM-web-${{ github.ref_name }}.wasm
249239
src/platforms/emscripten/build/src/AtomVM-web-${{ github.ref_name }}.wasm.sha256

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ memory error
8585
- Fixed possible concurrency problems in ESP32 UART driver
8686
- Fixed concurrency and memory leak related to links and monitors
8787
- Fixed issues with parsing of line references for stack traces
88+
- Fixed memory corruption issue with `erlang:make_tuple/2`
8889

8990
### Changed
9091

examples/emscripten/run_script.erl

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
-export([start/0]).
2323

2424
start() ->
25-
CounterPid = spawn(fun() -> counter_loop() end),
26-
emscripten:run_script(<<"alert('hello from Erlang in main thread')">>, [main_thread]),
25+
{CounterPid, CounterMonitor} = spawn_opt(fun() -> counter_loop() end, [monitor]),
26+
emscripten:run_script(
27+
<<
28+
"console.log('hello from Erlang in main thread');"
29+
"alert('hello from Erlang in main thread')"
30+
>>,
31+
[main_thread]
32+
),
2733
CounterPid ! {self(), terminate},
2834
receive
2935
{CounterPid, CounterValue, DeltaMS} ->
@@ -40,15 +46,20 @@ start() ->
4046
]
4147
)
4248
end,
43-
emscripten:run_script(<<"alert('hello from Erlang in main thread async')">>, [
44-
main_thread, async
45-
]),
46-
emscripten:run_script(<<"alert('hello from Erlang in worker thread')">>),
49+
% Ensure process terminated
50+
receive
51+
{'DOWN', CounterMonitor, process, CounterPid, normal} -> ok
52+
end,
4753
emscripten:run_script(
48-
<<"window.document.getElementById('demo-not').style = 'display: none';">>, [
49-
main_thread
54+
<<
55+
"console.log('hello from Erlang in main thread async');"
56+
"alert('hello from Erlang in main thread async')"
57+
>>,
58+
[
59+
main_thread, async
5060
]
51-
).
61+
),
62+
emscripten:run_script(<<"console.log('hello from Erlang in worker thread')">>).
5263

5364
counter_loop() ->
5465
StartTimeMS = erlang:system_time(millisecond),

examples/emscripten/run_script.html

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,26 @@ <h1>Run script example</h1>
3030
<code>emscripten:run_script/2</code> to run Javascript from Erlang.
3131
</p>
3232

33-
<p>It displays three alerts:</p>
33+
<p>It runs four Javascript scripts:</p>
3434
<ul>
35-
<li>An alert in the main thread, synchronously</li>
36-
<li>An alert in the main thread, asynchronously</li>
37-
<li>An alert in the worker thread</li>
35+
<li>One script from the main thread, synchronously, that prints a line to the console and displays an alert</li>
36+
<li>One script from the main thread, asynchronously, that prints a line to the console and displays an alert</li>
37+
<li>One script from the worker thread that prints a line to the console</li>
38+
<li>One script that updates the DOM to display the time required for first script to run</li>
3839
</ul>
39-
<p>Second alert is likely to appear after third alert.</p>
4040

4141
<p>
4242
On non-SMP builds, it demonstrates the fact that Erlang continues to run
4343
when <code>emscripten:run_script/2</code> is called with
4444
<code>[main_thread]</code> as a counter is incremented while waiting for
45-
first alert to complete. The value is <span id="demo-counter">unset</span>
45+
first line to be printed. The value is <span id="demo-counter">unset</span>
4646
</p>
4747

4848
<p>
4949
Beyond the counter, this example also demonstrates manipulating DOM (in
5050
the main thread) using JS function.
5151
</p>
5252

53-
<p>
54-
Style of this text was <span id="demo-not">not</span> modified by Erlang.
55-
</p>
5653
<script>
5754
// Arguments are loaded using fetch API.
5855
// wasm_webserver serves under /build/ files in build subdirectory.

src/libAtomVM/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ enum ModuleLoadResult
160160
* @param module_atom module name atom string.
161161
* @param function_atom function name atom string.
162162
*/
163-
void module_get_imported_function_module_and_name(const Module *this_module, int index, AtomString *module_atom, AtomString *function_atom);
163+
void module_get_imported_function_module_and_name(const Module *this_module, int index, AtomString *module_atom, AtomString *function_atom, GlobalContext *glb);
164164
#endif
165165

166166
/**

src/libAtomVM/nifs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ static term nif_erlang_make_tuple_2(Context *ctx, int argc, term argv[])
17101710
RAISE_ERROR(BADARG_ATOM);
17111711
}
17121712

1713-
if (UNLIKELY(memory_ensure_free_opt(ctx, count_elem + 1, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
1713+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(count_elem), 1, argv + 1, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
17141714
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
17151715
}
17161716
term new_tuple = term_alloc_tuple(count_elem, &ctx->heap);

src/libAtomVM/opcodesswitch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,7 @@ static bool maybe_call_native(Context *ctx, AtomString module_name, AtomString f
16961696
if (UNLIKELY(ctx->trace_calls)) {
16971697
AtomString module_name;
16981698
AtomString function_name;
1699-
module_get_imported_function_module_and_name(mod, index, &module_name, &function_name);
1699+
module_get_imported_function_module_and_name(mod, index, &module_name, &function_name, ctx->global);
17001700
trace_apply(ctx, call_type, module_name, function_name, arity);
17011701
}
17021702
}

src/platforms/emscripten/tests/cypress/e2e/examples.spec.cy.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,38 @@ describe("emscripten hello world BEAM", () => {
5757

5858
describe("emscripten run script", () => {
5959
it("should display three alerts and update counter", () => {
60+
cy.visit("/run_script.html", {
61+
onBeforeLoad(win) {
62+
cy.stub(win.console, "log").as("consoleLog");
63+
},
64+
});
6065
const alert = cy.stub().as("alert");
6166
cy.on("window:alert", alert);
62-
cy.visit("/run_script.html");
6367
cy.get("#demo-counter").should("contain", "unset");
64-
cy.get("@alert").should(
68+
cy.get("@consoleLog").should(
6569
"have.been.calledWith",
6670
"hello from Erlang in main thread",
6771
);
6872
cy.get("@alert").should(
69-
"have.been.calledWithMatch",
70-
"hello from Erlang in worker thread",
73+
"have.been.calledWith",
74+
"hello from Erlang in main thread",
75+
);
76+
// window.console is not the console invoked by our code from worker
77+
// thread, or at least cypress stub doesn't work
78+
// cy.get("@consoleLog").should(
79+
// "have.been.calledWithMatch",
80+
// "hello from Erlang in worker thread",
81+
// );
82+
cy.get("@consoleLog").should(
83+
"have.been.calledWith",
84+
"hello from Erlang in main thread async",
7185
);
7286
cy.get("@alert").should(
7387
"have.been.calledWith",
7488
"hello from Erlang in main thread async",
7589
);
7690
cy.get("#demo-counter").should("contain", "ms");
91+
cy.get("@consoleLog").should("be.calledWith", "Return value: ok");
7792
});
7893
});
7994

0 commit comments

Comments
 (0)