Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array and string trim_in_place allocation bounds check #2840

Merged
merged 1 commit into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/builtin/array.pony
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,17 @@ class Array[A] is Seq[A]
_alloc = if last == _size then _alloc - offset else size' end

_size = size'
_ptr = _ptr._offset(offset)

// if _alloc == 0 then we've trimmed all the memory originally allocated.
// if we do _ptr._offset, we will spill into memory not allocated/owned
// by this array and could potentially cause a segfault if we cross
// a pagemap boundary into a pagemap address that hasn't been allocated
// yet when `reserve` is called next.
if _alloc == 0 then
_ptr = Pointer[A]
else
_ptr = _ptr._offset(offset)
end

fun val trim(from: USize = 0, to: USize = -1): Array[A] val =>
"""
Expand Down
12 changes: 11 additions & 1 deletion packages/builtin/string.pony
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,17 @@ actor Main
_alloc = if last == _size then _alloc - offset else size' end

_size = size'
_ptr = _ptr._offset(offset)

// if _alloc == 0 then we've trimmed all the memory originally allocated.
// if we do _ptr._offset, we will spill into memory not allocated/owned
// by this string and could potentially cause a segfault if we cross
// a pagemap boundary into a pagemap address that hasn't been allocated
// yet when `reserve` is called next.
if _alloc == 0 then
_ptr = Pointer[U8]
else
_ptr = _ptr._offset(offset)
end

fun val trim(from: USize = 0, to: USize = -1): String val =>
"""
Expand Down
16 changes: 16 additions & 0 deletions packages/builtin_test/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ class iso _TestStringTrimInPlace is UnitTest
case(h, "", "0123456", 4, 1, 0)
case(h, "456", "0123456789".clone().>trim_in_place(1, 8), 3, 6, 3)
case(h, "456", "0123456789".trim(1, 8), 3, 6, 3)
case(h, "", "0123456789".clone().>trim_in_place(1, 8), 3, 3, 0)

fun case(
h: TestHelper,
Expand All @@ -512,10 +513,17 @@ class iso _TestStringTrimInPlace is UnitTest
space: USize = 0)
=>
let copy: String ref = orig.clone()
let pre_trim_pagemap = @ponyint_pagemap_get[Pointer[None]](copy.cpointer())
copy.trim_in_place(from, to)
h.assert_eq[String box](expected, copy)
h.assert_eq[USize](space, copy.space())
h.assert_eq[String box](expected, copy.clone()) // safe to clone
let post_trim_pagemap = @ponyint_pagemap_get[Pointer[None]](copy.cpointer())
if copy.space() == 0 then
h.assert_eq[USize](0, post_trim_pagemap.usize())
else
h.assert_eq[USize](pre_trim_pagemap.usize(), post_trim_pagemap.usize())
end

class iso _TestStringTrimInPlaceWithAppend is UnitTest
"""
Expand Down Expand Up @@ -1236,6 +1244,7 @@ class iso _TestArrayTrimInPlace is UnitTest
case(h, [4; 5; 6], [0; 1; 2; 3; 4; 5; 6], 4 where space = 4)
case(h, Array[U8], [0; 1; 2; 3; 4; 5; 6], 4, 4, 0)
case(h, Array[U8], [0; 1; 2; 3; 4; 5; 6], 4, 1, 0)
case(h, Array[U8], Array[U8].init(8, 1024), 1024)

fun case(
h: TestHelper,
Expand All @@ -1246,9 +1255,16 @@ class iso _TestArrayTrimInPlace is UnitTest
space: USize = 0)
=>
let copy: Array[U8] ref = orig.clone()
let pre_trim_pagemap = @ponyint_pagemap_get[Pointer[None]](copy.cpointer())
copy.trim_in_place(from, to)
h.assert_eq[USize](space, copy.space())
h.assert_array_eq[U8](expected, copy)
let post_trim_pagemap = @ponyint_pagemap_get[Pointer[None]](copy.cpointer())
if copy.space() == 0 then
h.assert_eq[USize](0, post_trim_pagemap.usize())
else
h.assert_eq[USize](pre_trim_pagemap.usize(), post_trim_pagemap.usize())
end

class iso _TestArrayTrimInPlaceWithAppend is UnitTest
"""
Expand Down