Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
substrate
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Denis_P
substrate
Commits
a114df7d
Unverified
Commit
a114df7d
authored
6 years ago
by
Michael Müller
Browse files
Options
Downloads
Patches
Plain Diff
Revert me: Panic on double allocate/free
parent
925d3308
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/executor/src/heap.rs
+10
-0
10 additions, 0 deletions
core/executor/src/heap.rs
with
10 additions
and
0 deletions
core/executor/src/heap.rs
+
10
−
0
View file @
a114df7d
...
...
@@ -18,6 +18,7 @@
//! See more details at https://github.com/paritytech/substrate/issues/1615.
use
log
::
trace
;
use
std
::
collections
::
HashMap
;
// The pointers need to be aligned to 8 bytes.
const
ALIGNMENT
:
usize
=
8
;
...
...
@@ -31,6 +32,7 @@ const N: usize = 22;
const
MAX_POSSIBLE_ALLOCATION
:
usize
=
16777216
;
// 2^24 bytes
pub
struct
Heap
{
allocated_ptrs
:
HashMap
<
usize
,
bool
>
,
bumper
:
usize
,
heads
:
[
u32
;
N
],
heap
:
Vec
<
u8
>
,
...
...
@@ -60,6 +62,7 @@ impl Heap {
}
Heap
{
allocated_ptrs
:
HashMap
::
new
(),
bumper
:
0
,
heads
:
[
0
;
N
],
heap
:
vec!
[
0
;
heap_size
],
...
...
@@ -102,6 +105,9 @@ impl Heap {
self
.total_size
=
self
.total_size
+
item_size
+
8
;
trace!
(
target
:
"wasm-heap"
,
"Heap size is {} bytes after allocation"
,
self
.total_size
);
assert_eq!
(
self
.allocated_ptrs
.get
(
&
ptr
),
None
,
"Double allocate at {}"
,
ptr
);
self
.allocated_ptrs
.insert
(
ptr
,
true
);
(
self
.ptr_offset
+
ptr
)
as
u32
}
...
...
@@ -110,6 +116,8 @@ impl Heap {
let
mut
ptr
=
ptr
as
usize
;
ptr
-=
self
.ptr_offset
;
assert_ne!
(
self
.allocated_ptrs
.get
(
&
ptr
),
None
,
"Double free at {}"
,
ptr
);
let
list_index
=
self
.heap
[
ptr
-
8
]
as
usize
;
for
i
in
1
..
8
{
assert!
(
self
.heap
[
ptr
-
i
]
==
255
);
}
let
tail
=
self
.heads
[
list_index
];
...
...
@@ -117,6 +125,8 @@ impl Heap {
Heap
::
write_u32_into_le_bytes
(
tail
,
&
mut
self
.heap
[
ptr
-
8
..
ptr
-
4
]);
self
.allocated_ptrs
.remove
(
&
ptr
)
.unwrap
();
let
item_size
=
Heap
::
get_item_size_from_index
(
list_index
);
self
.total_size
=
self
.total_size
.checked_sub
(
item_size
+
8
)
.unwrap_or
(
0
);
trace!
(
target
:
"wasm-heap"
,
"Heap size is {} bytes after deallocation"
,
self
.total_size
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment