Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: vm/dart_api_impl.cc

Issue 10782016: Enforce length/size limits for variable size heap object in order to (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/vm/vm.status ('k') | vm/dart_api_impl_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/dart.h" 10 #include "vm/dart.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 CURRENT_FUNC, #dart_handle, #type); \ 59 CURRENT_FUNC, #dart_handle, #type); \
60 } \ 60 } \
61 } while (0) 61 } while (0)
62 62
63 63
64 #define RETURN_NULL_ERROR(parameter) \ 64 #define RETURN_NULL_ERROR(parameter) \
65 return Api::NewError("%s expects argument '%s' to be non-null.", \ 65 return Api::NewError("%s expects argument '%s' to be non-null.", \
66 CURRENT_FUNC, #parameter); 66 CURRENT_FUNC, #parameter);
67 67
68 68
69 #define CHECK_LENGTH(length, max_elements) \
70 do { \
71 intptr_t len = (length); \
72 intptr_t max = (max_elements); \
73 if (len < 0 || len > max) { \
74 return Api::NewError( \
75 "%s expects argument '%s' to be in the range [0..%ld].", \
76 CURRENT_FUNC, #length, max); \
77 } \
78 } while (0)
79
69 // Takes a vm internal name and makes it suitable for external user. 80 // Takes a vm internal name and makes it suitable for external user.
70 // 81 //
71 // Examples: 82 // Examples:
72 // 83 //
73 // Internal getter and setter prefices are removed: 84 // Internal getter and setter prefices are removed:
74 // 85 //
75 // get:foo -> foo 86 // get:foo -> foo
76 // set:foo -> foo 87 // set:foo -> foo
77 // 88 //
78 // Private name mangling is removed, possibly twice: 89 // Private name mangling is removed, possibly twice:
(...skipping 1494 matching lines...) Expand 10 before | Expand all | Expand 10 after
1573 CURRENT_FUNC); 1584 CURRENT_FUNC);
1574 } 1585 }
1575 return Api::NewHandle(isolate, String::New(str)); 1586 return Api::NewHandle(isolate, String::New(str));
1576 } 1587 }
1577 1588
1578 1589
1579 DART_EXPORT Dart_Handle Dart_NewString8(const uint8_t* codepoints, 1590 DART_EXPORT Dart_Handle Dart_NewString8(const uint8_t* codepoints,
1580 intptr_t length) { 1591 intptr_t length) {
1581 Isolate* isolate = Isolate::Current(); 1592 Isolate* isolate = Isolate::Current();
1582 DARTSCOPE(isolate); 1593 DARTSCOPE(isolate);
1594 if (codepoints == NULL) {
1595 RETURN_NULL_ERROR(codepoints);
1596 }
1597 CHECK_LENGTH(length, String::kMaxElements);
1583 return Api::NewHandle(isolate, String::New(codepoints, length)); 1598 return Api::NewHandle(isolate, String::New(codepoints, length));
1584 } 1599 }
1585 1600
1586 1601
1587 DART_EXPORT Dart_Handle Dart_NewString16(const uint16_t* codepoints, 1602 DART_EXPORT Dart_Handle Dart_NewString16(const uint16_t* codepoints,
1588 intptr_t length) { 1603 intptr_t length) {
1589 Isolate* isolate = Isolate::Current(); 1604 Isolate* isolate = Isolate::Current();
1590 DARTSCOPE(isolate); 1605 DARTSCOPE(isolate);
1606 if (codepoints == NULL) {
1607 RETURN_NULL_ERROR(codepoints);
1608 }
1609 CHECK_LENGTH(length, String::kMaxElements);
1591 return Api::NewHandle(isolate, String::New(codepoints, length)); 1610 return Api::NewHandle(isolate, String::New(codepoints, length));
1592 } 1611 }
1593 1612
1594 1613
1595 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints, 1614 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints,
1596 intptr_t length) { 1615 intptr_t length) {
1597 Isolate* isolate = Isolate::Current(); 1616 Isolate* isolate = Isolate::Current();
1598 DARTSCOPE(isolate); 1617 DARTSCOPE(isolate);
1618 if (codepoints == NULL) {
1619 RETURN_NULL_ERROR(codepoints);
1620 }
1621 CHECK_LENGTH(length, String::kMaxElements);
1599 return Api::NewHandle(isolate, String::New(codepoints, length)); 1622 return Api::NewHandle(isolate, String::New(codepoints, length));
1600 } 1623 }
1601 1624
1602 1625
1603 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) { 1626 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) {
1604 return RawObject::IsExternalStringClassId(Api::ClassId(object)); 1627 return RawObject::IsExternalStringClassId(Api::ClassId(object));
1605 } 1628 }
1606 1629
1607 1630
1608 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object, 1631 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object,
(...skipping 19 matching lines...) Expand all
1628 1651
1629 DART_EXPORT Dart_Handle Dart_NewExternalString8(const uint8_t* codepoints, 1652 DART_EXPORT Dart_Handle Dart_NewExternalString8(const uint8_t* codepoints,
1630 intptr_t length, 1653 intptr_t length,
1631 void* peer, 1654 void* peer,
1632 Dart_PeerFinalizer callback) { 1655 Dart_PeerFinalizer callback) {
1633 Isolate* isolate = Isolate::Current(); 1656 Isolate* isolate = Isolate::Current();
1634 DARTSCOPE(isolate); 1657 DARTSCOPE(isolate);
1635 if (codepoints == NULL && length != 0) { 1658 if (codepoints == NULL && length != 0) {
1636 RETURN_NULL_ERROR(codepoints); 1659 RETURN_NULL_ERROR(codepoints);
1637 } 1660 }
1638 if (length < 0) { 1661 CHECK_LENGTH(length, String::kMaxElements);
1639 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1640 CURRENT_FUNC);
1641 }
1642 return Api::NewHandle( 1662 return Api::NewHandle(
1643 isolate, String::NewExternal(codepoints, length, peer, callback)); 1663 isolate, String::NewExternal(codepoints, length, peer, callback));
1644 } 1664 }
1645 1665
1646 1666
1647 DART_EXPORT Dart_Handle Dart_NewExternalString16(const uint16_t* codepoints, 1667 DART_EXPORT Dart_Handle Dart_NewExternalString16(const uint16_t* codepoints,
1648 intptr_t length, 1668 intptr_t length,
1649 void* peer, 1669 void* peer,
1650 Dart_PeerFinalizer callback) { 1670 Dart_PeerFinalizer callback) {
1651 Isolate* isolate = Isolate::Current(); 1671 Isolate* isolate = Isolate::Current();
1652 DARTSCOPE(isolate); 1672 DARTSCOPE(isolate);
1653 if (codepoints == NULL && length != 0) { 1673 if (codepoints == NULL && length != 0) {
1654 RETURN_NULL_ERROR(codepoints); 1674 RETURN_NULL_ERROR(codepoints);
1655 } 1675 }
1656 if (length < 0) { 1676 CHECK_LENGTH(length, String::kMaxElements);
1657 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1658 CURRENT_FUNC);
1659 }
1660 return Api::NewHandle( 1677 return Api::NewHandle(
1661 isolate, String::NewExternal(codepoints, length, peer, callback)); 1678 isolate, String::NewExternal(codepoints, length, peer, callback));
1662 } 1679 }
1663 1680
1664 1681
1665 DART_EXPORT Dart_Handle Dart_NewExternalString32(const uint32_t* codepoints, 1682 DART_EXPORT Dart_Handle Dart_NewExternalString32(const uint32_t* codepoints,
1666 intptr_t length, 1683 intptr_t length,
1667 void* peer, 1684 void* peer,
1668 Dart_PeerFinalizer callback) { 1685 Dart_PeerFinalizer callback) {
1669 Isolate* isolate = Isolate::Current(); 1686 Isolate* isolate = Isolate::Current();
1670 DARTSCOPE(isolate); 1687 DARTSCOPE(isolate);
1671 if (codepoints == NULL && length != 0) { 1688 if (codepoints == NULL && length != 0) {
1672 RETURN_NULL_ERROR(codepoints); 1689 RETURN_NULL_ERROR(codepoints);
1673 } 1690 }
1674 if (length < 0) { 1691 CHECK_LENGTH(length, String::kMaxElements);
1675 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1676 CURRENT_FUNC);
1677 }
1678 return Api::NewHandle( 1692 return Api::NewHandle(
1679 isolate, String::NewExternal(codepoints, length, peer, callback)); 1693 isolate, String::NewExternal(codepoints, length, peer, callback));
1680 } 1694 }
1681 1695
1682 1696
1683 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str, 1697 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str,
1684 uint8_t* codepoints, 1698 uint8_t* codepoints,
1685 intptr_t* length) { 1699 intptr_t* length) {
1686 Isolate* isolate = Isolate::Current(); 1700 Isolate* isolate = Isolate::Current();
1687 DARTSCOPE(isolate); 1701 DARTSCOPE(isolate);
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
1817 Isolate* isolate = Isolate::Current(); 1831 Isolate* isolate = Isolate::Current();
1818 DARTSCOPE(isolate); 1832 DARTSCOPE(isolate);
1819 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); 1833 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1820 return GetListInstance(isolate, obj) != Instance::null(); 1834 return GetListInstance(isolate, obj) != Instance::null();
1821 } 1835 }
1822 1836
1823 1837
1824 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length) { 1838 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length) {
1825 Isolate* isolate = Isolate::Current(); 1839 Isolate* isolate = Isolate::Current();
1826 DARTSCOPE(isolate); 1840 DARTSCOPE(isolate);
1841 CHECK_LENGTH(length, Array::kMaxElements);
1827 return Api::NewHandle(isolate, Array::New(length)); 1842 return Api::NewHandle(isolate, Array::New(length));
1828 } 1843 }
1829 1844
1830 1845
1831 #define GET_LIST_LENGTH(isolate, type, obj, len) \ 1846 #define GET_LIST_LENGTH(isolate, type, obj, len) \
1832 type& array = type::Handle(isolate); \ 1847 type& array = type::Handle(isolate); \
1833 array ^= obj.raw(); \ 1848 array ^= obj.raw(); \
1834 *len = array.Length(); \ 1849 *len = array.Length(); \
1835 return Api::Success(isolate); \ 1850 return Api::Success(isolate); \
1836 1851
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
2157 2172
2158 2173
2159 DART_EXPORT bool Dart_IsByteArray(Dart_Handle object) { 2174 DART_EXPORT bool Dart_IsByteArray(Dart_Handle object) {
2160 return RawObject::IsByteArrayClassId(Api::ClassId(object)); 2175 return RawObject::IsByteArrayClassId(Api::ClassId(object));
2161 } 2176 }
2162 2177
2163 2178
2164 DART_EXPORT Dart_Handle Dart_NewByteArray(intptr_t length) { 2179 DART_EXPORT Dart_Handle Dart_NewByteArray(intptr_t length) {
2165 Isolate* isolate = Isolate::Current(); 2180 Isolate* isolate = Isolate::Current();
2166 DARTSCOPE(isolate); 2181 DARTSCOPE(isolate);
2182 CHECK_LENGTH(length, Uint8Array::kMaxElements);
2167 return Api::NewHandle(isolate, Uint8Array::New(length)); 2183 return Api::NewHandle(isolate, Uint8Array::New(length));
2168 } 2184 }
2169 2185
2170 2186
2171 DART_EXPORT Dart_Handle Dart_NewExternalByteArray(uint8_t* data, 2187 DART_EXPORT Dart_Handle Dart_NewExternalByteArray(uint8_t* data,
2172 intptr_t length, 2188 intptr_t length,
2173 void* peer, 2189 void* peer,
2174 Dart_PeerFinalizer callback) { 2190 Dart_PeerFinalizer callback) {
2175 Isolate* isolate = Isolate::Current(); 2191 Isolate* isolate = Isolate::Current();
2176 DARTSCOPE(isolate); 2192 DARTSCOPE(isolate);
2177 if (data == NULL && length != 0) { 2193 if (data == NULL && length != 0) {
2178 RETURN_NULL_ERROR(data); 2194 RETURN_NULL_ERROR(data);
2179 } 2195 }
2180 if (length < 0) { 2196 CHECK_LENGTH(length, ExternalUint8Array::kMaxElements);
2181 return Api::NewError("%s expects argument 'length' to be greater than 0.",
2182 CURRENT_FUNC);
2183 }
2184 return Api::NewHandle( 2197 return Api::NewHandle(
2185 isolate, ExternalUint8Array::New(data, length, peer, callback)); 2198 isolate, ExternalUint8Array::New(data, length, peer, callback));
2186 } 2199 }
2187 2200
2188 2201
2189 DART_EXPORT Dart_Handle Dart_ExternalByteArrayGetPeer(Dart_Handle object, 2202 DART_EXPORT Dart_Handle Dart_ExternalByteArrayGetPeer(Dart_Handle object,
2190 void** peer) { 2203 void** peer) {
2191 Isolate* isolate = Isolate::Current(); 2204 Isolate* isolate = Isolate::Current();
2192 DARTSCOPE(isolate); 2205 DARTSCOPE(isolate);
2193 const ExternalUint8Array& array = 2206 const ExternalUint8Array& array =
(...skipping 1995 matching lines...) Expand 10 before | Expand all | Expand 10 after
4189 DART_EXPORT void Dart_InitPerfEventsSupport(Dart_FileWriterFunction function) { 4202 DART_EXPORT void Dart_InitPerfEventsSupport(Dart_FileWriterFunction function) {
4190 Dart::set_perf_events_writer(function); 4203 Dart::set_perf_events_writer(function);
4191 } 4204 }
4192 4205
4193 4206
4194 DART_EXPORT void Dart_InitFlowGraphPrinting(Dart_FileWriterFunction function) { 4207 DART_EXPORT void Dart_InitFlowGraphPrinting(Dart_FileWriterFunction function) {
4195 Dart::set_flow_graph_writer(function); 4208 Dart::set_flow_graph_writer(function);
4196 } 4209 }
4197 4210
4198 } // namespace dart 4211 } // namespace dart
OLDNEW
« no previous file with comments | « tests/vm/vm.status ('k') | vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698