Skip to content

Commit 1da3717

Browse files
committed
Update wasm files to account for new memory contracts
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 8176d9a commit 1da3717

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/wasmsamples/HelloWorld.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ int HelloWorld(char* msg)
4242
printf("contents of buffer after snprintf: %s\n", buf);
4343

4444
free(buf);
45+
free(msg); // Free the msg since we own it
4546

4647
return 0;
4748
}

src/wasmsamples/RunWasm.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int CalcFib(int n)
3636
}
3737
}
3838

39-
// This function receives an array of bytes followed by a length and then returns a pointer to a buffer where the first 4 bytes is the length followed by the data.
39+
// This function receives an array of bytes followed by a length and then returns a buffer where the first 4 bytes is the length followed by the data.
4040
__attribute__((export_name("ReceiveByteArray")))
4141
void* ReceiveByteArray(void* data, int length) {
4242

@@ -45,15 +45,17 @@ void* ReceiveByteArray(void* data, int length) {
4545
result += 4;
4646
memcpy(result, data, length);
4747
result -= 4;
48-
return result;
48+
free(data); // Free the guest parameter, which we own
49+
return result; // Transfer ownership of return value to host
4950
}
5051

5152
__attribute__((export_name("WasmPrintUsingHostPrint")))
5253
int WasmPrintUsingHostPrint(char* msg)
5354
{
54-
// Host Print now returns a flatbuffer buffer
55-
HostPrint(msg);
56-
return strlen(msg);
55+
HostPrint(msg); // Host borrows msg
56+
int len = strlen(msg);
57+
free(msg); // Free the param since we own
58+
return len;
5759
}
5860

5961
__attribute__((export_name("PrintHelloWorld")))
@@ -67,12 +69,13 @@ __attribute__((export_name("Print")))
6769
void Print(char* msg)
6870
{
6971
HostPrint(msg);
72+
free(msg); // Free the msg since we own it
7073
}
7174

7275
__attribute__((export_name("Echo")))
7376
char* Echo(char* msg)
7477
{
75-
return msg;
78+
return msg; // Transfer ownership to host
7679
}
7780

7881
__attribute__((export_name("ToUpper"), optnone))
@@ -96,6 +99,7 @@ __attribute__((export_name("PrintUpper"), optnone))
9699
void PrintUpper(char* msg)
97100
{
98101
HostPrint(ToUpper(msg));
102+
free(msg);
99103
}
100104

101105
__attribute__((export_name("KeepCPUBusy")))

0 commit comments

Comments
 (0)