Skip to content

Commit b2afed6

Browse files
committed
Take alignment into consideration during malloc
1 parent 3d78163 commit b2afed6

File tree

7 files changed

+21
-20
lines changed

7 files changed

+21
-20
lines changed

crates/cli-support/src/js/binding.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -813,12 +813,14 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
813813
let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?;
814814
let malloc = js.cx.export_name_of(*malloc);
815815
let i = js.tmp();
816+
let align = std::cmp::max(kind.size(), 4);
816817
js.prelude(&format!(
817-
"const ptr{i} = {f}({0}, wasm.{malloc});",
818+
"const ptr{i} = {f}({0}, wasm.{malloc}, {align});",
818819
val,
819820
i = i,
820821
f = func,
821822
malloc = malloc,
823+
align = align,
822824
));
823825
js.prelude(&format!("const len{} = WASM_VECTOR_LEN;", i));
824826
js.push(format!("ptr{}", i));
@@ -922,7 +924,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
922924
let malloc = js.cx.export_name_of(*malloc);
923925
let val = js.pop();
924926
js.prelude(&format!(
925-
"var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc});",
927+
"var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc}, 4);",
926928
val,
927929
i = i,
928930
f = func,
@@ -940,7 +942,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
940942
let malloc = js.cx.export_name_of(*malloc);
941943
let i = js.tmp();
942944
js.prelude(&format!(
943-
"var ptr{i} = {f}({val}, wasm.{malloc});",
945+
"var ptr{i} = {f}({val}, wasm.{malloc}, 4);",
944946
val = val,
945947
i = i,
946948
f = func,

crates/cli-support/src/js/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1259,14 +1259,14 @@ impl<'a> Context<'a> {
12591259
"\
12601260
if (realloc === undefined) {{
12611261
const buf = cachedTextEncoder.encode(arg);
1262-
const ptr = malloc(buf.length) >>> 0;
1262+
const ptr = malloc(buf.length, 4) >>> 0;
12631263
{mem}().subarray(ptr, ptr + buf.length).set(buf);
12641264
WASM_VECTOR_LEN = buf.length;
12651265
return ptr;
12661266
}}
12671267
12681268
let len = arg.length;
1269-
let ptr = malloc(len) >>> 0;
1269+
let ptr = malloc(len, 4) >>> 0;
12701270
12711271
const mem = {mem}();
12721272
@@ -1366,7 +1366,7 @@ impl<'a> Context<'a> {
13661366
self.global(&format!(
13671367
"
13681368
function {}(array, malloc) {{
1369-
const ptr = malloc(array.length * 4) >>> 0;
1369+
const ptr = malloc(array.length * 4, 4) >>> 0;
13701370
const mem = {}();
13711371
for (let i = 0; i < array.length; i++) {{
13721372
mem[ptr / 4 + i] = {}(array[i]);
@@ -1383,7 +1383,7 @@ impl<'a> Context<'a> {
13831383
self.global(&format!(
13841384
"
13851385
function {}(array, malloc) {{
1386-
const ptr = malloc(array.length * 4) >>> 0;
1386+
const ptr = malloc(array.length * 4, 4) >>> 0;
13871387
const mem = {}();
13881388
for (let i = 0; i < array.length; i++) {{
13891389
mem[ptr / 4 + i] = addHeapObject(array[i]);
@@ -1415,8 +1415,8 @@ impl<'a> Context<'a> {
14151415
self.expose_wasm_vector_len();
14161416
self.global(&format!(
14171417
"
1418-
function {}(arg, malloc) {{
1419-
const ptr = malloc(arg.length * {size}) >>> 0;
1418+
function {}(arg, malloc, align) {{
1419+
const ptr = malloc(arg.length * {size}, align) >>> 0;
14201420
{}().set(arg, ptr / {size});
14211421
WASM_VECTOR_LEN = arg.length;
14221422
return ptr;

crates/cli/tests/reference/string-arg.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ function passStringToWasm0(arg, malloc, realloc) {
4747

4848
if (realloc === undefined) {
4949
const buf = cachedTextEncoder.encode(arg);
50-
const ptr = malloc(buf.length) >>> 0;
50+
const ptr = malloc(buf.length, 4) >>> 0;
5151
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
5252
WASM_VECTOR_LEN = buf.length;
5353
return ptr;
5454
}
5555

5656
let len = arg.length;
57-
let ptr = malloc(len) >>> 0;
57+
let ptr = malloc(len, 4) >>> 0;
5858

5959
const mem = getUint8Memory0();
6060

crates/cli/tests/reference/string-arg.wat

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(module
2-
(type (;0;) (func (param i32) (result i32)))
3-
(type (;1;) (func (param i32 i32)))
2+
(type (;0;) (func (param i32 i32)))
3+
(type (;1;) (func (param i32 i32) (result i32)))
44
(type (;2;) (func (param i32 i32 i32) (result i32)))
55
(func $__wbindgen_realloc (;0;) (type 2) (param i32 i32 i32) (result i32))
6-
(func $__wbindgen_malloc (;1;) (type 0) (param i32) (result i32))
7-
(func $foo (;2;) (type 1) (param i32 i32))
6+
(func $__wbindgen_malloc (;1;) (type 1) (param i32 i32) (result i32))
7+
(func $foo (;2;) (type 0) (param i32 i32))
88
(memory (;0;) 17)
99
(export "memory" (memory 0))
1010
(export "foo" (func $foo))

crates/threads-xform/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ fn inject_start(
344344
// we give ourselves a stack and we update our stack
345345
// pointer as the default stack pointer is surely wrong for us.
346346
|body| {
347-
// local = malloc(stack.size) [aka base]
347+
// local = malloc(stack.size, align) [aka base]
348348
with_temp_stack(body, memory, stack, |body| {
349349
body.i32_const(stack.size as i32)
350+
.i32_const(4)
350351
.call(malloc)
351352
.local_tee(local);
352353
});
@@ -368,7 +369,6 @@ fn inject_start(
368369
// Afterwards we need to initialize our thread-local state.
369370
body.i32_const(tls.size as i32)
370371
.i32_const(tls.align as i32)
371-
.drop() // TODO: need to actually respect alignment
372372
.call(malloc)
373373
.global_set(tls.base)
374374
.global_get(tls.base)

guide/src/contributing/design/exporting-rust.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import * as wasm from './foo_bg';
3333
function passStringToWasm(arg) {
3434
const buf = new TextEncoder('utf-8').encode(arg);
3535
const len = buf.length;
36-
const ptr = wasm.__wbindgen_malloc(len);
36+
const ptr = wasm.__wbindgen_malloc(len, 4);
3737
let array = new Uint8Array(wasm.memory.buffer);
3838
array.set(buf, ptr);
3939
return [ptr, len];

src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1569,8 +1569,7 @@ pub mod __rt {
15691569
use std::mem;
15701570

15711571
#[no_mangle]
1572-
pub extern "C" fn __wbindgen_malloc(size: usize) -> *mut u8 {
1573-
let align = mem::align_of::<usize>();
1572+
pub extern "C" fn __wbindgen_malloc(size: usize, align: usize) -> *mut u8 {
15741573
if let Ok(layout) = Layout::from_size_align(size, align) {
15751574
unsafe {
15761575
if layout.size() > 0 {

0 commit comments

Comments
 (0)