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

Side by Side Diff: runtime/vm/dart_api_impl_test.cc

Issue 957483002: Verify acquired typed data: allow detection of use-after-free. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 5 years, 9 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 | « runtime/vm/dart_api_impl.cc ('k') | no next file » | 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 "bin/builtin.h" 5 #include "bin/builtin.h"
6 #include "include/dart_api.h" 6 #include "include/dart_api.h"
7 #include "include/dart_debugger_api.h" 7 #include "include/dart_debugger_api.h"
8 #include "include/dart_mirrors_api.h" 8 #include "include/dart_mirrors_api.h"
9 #include "include/dart_native_api.h" 9 #include "include/dart_native_api.h"
10 #include "platform/assert.h" 10 #include "platform/assert.h"
11 #include "platform/json.h" 11 #include "platform/json.h"
12 #include "platform/utils.h" 12 #include "platform/utils.h"
13 #include "vm/class_finalizer.h" 13 #include "vm/class_finalizer.h"
14 #include "vm/dart_api_impl.h" 14 #include "vm/dart_api_impl.h"
15 #include "vm/dart_api_state.h" 15 #include "vm/dart_api_state.h"
16 #include "vm/lockers.h" 16 #include "vm/lockers.h"
17 #include "vm/unit_test.h" 17 #include "vm/unit_test.h"
18 #include "vm/verifier.h" 18 #include "vm/verifier.h"
19 19
20 namespace dart { 20 namespace dart {
21 21
22 DECLARE_FLAG(bool, enable_type_checks); 22 DECLARE_FLAG(bool, enable_type_checks);
23 DECLARE_FLAG(bool, verify_acquired_data);
23 24
24 TEST_CASE(ErrorHandleBasics) { 25 TEST_CASE(ErrorHandleBasics) {
25 const char* kScriptChars = 26 const char* kScriptChars =
26 "void testMain() {\n" 27 "void testMain() {\n"
27 " throw new Exception(\"bad news\");\n" 28 " throw new Exception(\"bad news\");\n"
28 "}\n"; 29 "}\n";
29 30
30 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 31 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
31 32
32 Dart_Handle instance = Dart_True(); 33 Dart_Handle instance = Dart_True();
(...skipping 1701 matching lines...) Expand 10 before | Expand all | Expand 10 after
1734 result = Dart_Invoke(lib, NewString("main"), 0, NULL); 1735 result = Dart_Invoke(lib, NewString("main"), 0, NULL);
1735 EXPECT_VALID(result); 1736 EXPECT_VALID(result);
1736 1737
1737 for (intptr_t i = 0; i < kExtLength; i+=2) { 1738 for (intptr_t i = 0; i < kExtLength; i+=2) {
1738 EXPECT_EQ(0x24, data[i]); 1739 EXPECT_EQ(0x24, data[i]);
1739 EXPECT_EQ(0x28, data[i+1]); 1740 EXPECT_EQ(0x28, data[i+1]);
1740 } 1741 }
1741 } 1742 }
1742 1743
1743 1744
1744 TEST_CASE(TypedDataDirectAccess) { 1745 static void TestTypedDataDirectAccess() {
1745 Dart_Handle str = Dart_NewStringFromCString("junk"); 1746 Dart_Handle str = Dart_NewStringFromCString("junk");
1746 Dart_Handle byte_array = Dart_NewTypedData(Dart_TypedData_kUint8, 10); 1747 Dart_Handle byte_array = Dart_NewTypedData(Dart_TypedData_kUint8, 10);
1747 EXPECT_VALID(byte_array); 1748 EXPECT_VALID(byte_array);
1748 Dart_Handle result; 1749 Dart_Handle result;
1749 result = Dart_TypedDataAcquireData(byte_array, NULL, NULL, NULL); 1750 result = Dart_TypedDataAcquireData(byte_array, NULL, NULL, NULL);
1750 EXPECT_ERROR(result, "Dart_TypedDataAcquireData expects argument 'type'" 1751 EXPECT_ERROR(result, "Dart_TypedDataAcquireData expects argument 'type'"
1751 " to be non-null."); 1752 " to be non-null.");
1752 Dart_TypedData_Type type; 1753 Dart_TypedData_Type type;
1753 result = Dart_TypedDataAcquireData(byte_array, &type, NULL, NULL); 1754 result = Dart_TypedDataAcquireData(byte_array, &type, NULL, NULL);
1754 EXPECT_ERROR(result, "Dart_TypedDataAcquireData expects argument 'data'" 1755 EXPECT_ERROR(result, "Dart_TypedDataAcquireData expects argument 'data'"
(...skipping 12 matching lines...) Expand all
1767 1768
1768 result = Dart_TypedDataReleaseData(Dart_Null()); 1769 result = Dart_TypedDataReleaseData(Dart_Null());
1769 EXPECT_ERROR(result, "Dart_TypedDataReleaseData expects argument 'object'" 1770 EXPECT_ERROR(result, "Dart_TypedDataReleaseData expects argument 'object'"
1770 " to be non-null."); 1771 " to be non-null.");
1771 result = Dart_TypedDataReleaseData(str); 1772 result = Dart_TypedDataReleaseData(str);
1772 EXPECT_ERROR(result, "Dart_TypedDataReleaseData expects argument 'object'" 1773 EXPECT_ERROR(result, "Dart_TypedDataReleaseData expects argument 'object'"
1773 " to be of type 'TypedData'."); 1774 " to be of type 'TypedData'.");
1774 } 1775 }
1775 1776
1776 1777
1778 TEST_CASE(TypedDataDirectAccessUnverified) {
1779 FLAG_verify_acquired_data = false;
1780 TestTypedDataDirectAccess();
1781 }
1782
1783
1784 TEST_CASE(TypedDataDirectAccessVerified) {
1785 FLAG_verify_acquired_data = true;
1786 TestTypedDataDirectAccess();
1787 }
1788
1789
1777 static void TestDirectAccess(Dart_Handle lib, 1790 static void TestDirectAccess(Dart_Handle lib,
1778 Dart_Handle array, 1791 Dart_Handle array,
1779 Dart_TypedData_Type expected_type) { 1792 Dart_TypedData_Type expected_type) {
1780 Dart_Handle result; 1793 Dart_Handle result;
1781 1794
1782 // Invoke the dart function that sets initial values. 1795 // Invoke the dart function that sets initial values.
1783 Dart_Handle dart_args[1]; 1796 Dart_Handle dart_args[1];
1784 dart_args[0] = array; 1797 dart_args[0] = array;
1785 result = Dart_Invoke(lib, NewString("setMain"), 1, dart_args); 1798 result = Dart_Invoke(lib, NewString("setMain"), 1, dart_args);
1786 EXPECT_VALID(result); 1799 EXPECT_VALID(result);
(...skipping 21 matching lines...) Expand all
1808 // Release direct accesss to the typed data object. 1821 // Release direct accesss to the typed data object.
1809 result = Dart_TypedDataReleaseData(array); 1822 result = Dart_TypedDataReleaseData(array);
1810 EXPECT_VALID(result); 1823 EXPECT_VALID(result);
1811 1824
1812 // Invoke the dart function in order to check the modified values. 1825 // Invoke the dart function in order to check the modified values.
1813 result = Dart_Invoke(lib, NewString("testMain"), 1, dart_args); 1826 result = Dart_Invoke(lib, NewString("testMain"), 1, dart_args);
1814 EXPECT_VALID(result); 1827 EXPECT_VALID(result);
1815 } 1828 }
1816 1829
1817 1830
1818 TEST_CASE(TypedDataDirectAccess1) { 1831 static void TestTypedDataDirectAccess1() {
1819 const char* kScriptChars = 1832 const char* kScriptChars =
1820 "import 'dart:typed_data';\n" 1833 "import 'dart:typed_data';\n"
1821 "class Expect {\n" 1834 "class Expect {\n"
1822 " static equals(a, b) {\n" 1835 " static equals(a, b) {\n"
1823 " if (a != b) {\n" 1836 " if (a != b) {\n"
1824 " throw new Exception('not equal. expected: $a, got: $b');\n" 1837 " throw new Exception('not equal. expected: $a, got: $b');\n"
1825 " }\n" 1838 " }\n"
1826 " }\n" 1839 " }\n"
1827 "}\n" 1840 "}\n"
1828 "void setMain(var a) {" 1841 "void setMain(var a) {"
(...skipping 24 matching lines...) Expand all
1853 uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 1866 uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1854 intptr_t data_length = ARRAY_SIZE(data); 1867 intptr_t data_length = ARRAY_SIZE(data);
1855 Dart_Handle ext_list_access_test_obj; 1868 Dart_Handle ext_list_access_test_obj;
1856 ext_list_access_test_obj = Dart_NewExternalTypedData(Dart_TypedData_kUint8, 1869 ext_list_access_test_obj = Dart_NewExternalTypedData(Dart_TypedData_kUint8,
1857 data, data_length); 1870 data, data_length);
1858 EXPECT_VALID(ext_list_access_test_obj); 1871 EXPECT_VALID(ext_list_access_test_obj);
1859 TestDirectAccess(lib, ext_list_access_test_obj, Dart_TypedData_kUint8); 1872 TestDirectAccess(lib, ext_list_access_test_obj, Dart_TypedData_kUint8);
1860 } 1873 }
1861 1874
1862 1875
1863 TEST_CASE(TypedDataViewDirectAccess) { 1876 TEST_CASE(TypedDataDirectAccess1Unverified) {
1877 FLAG_verify_acquired_data = false;
1878 TestTypedDataDirectAccess1();
1879 }
1880
1881
1882 TEST_CASE(TypedDataDirectAccess1Verified) {
1883 FLAG_verify_acquired_data = true;
1884 TestTypedDataDirectAccess1();
1885 }
1886
1887
1888 static void TestTypedDataViewDirectAccess() {
1864 const char* kScriptChars = 1889 const char* kScriptChars =
1865 "import 'dart:typed_data';\n" 1890 "import 'dart:typed_data';\n"
1866 "class Expect {\n" 1891 "class Expect {\n"
1867 " static equals(a, b) {\n" 1892 " static equals(a, b) {\n"
1868 " if (a != b) {\n" 1893 " if (a != b) {\n"
1869 " throw 'not equal. expected: $a, got: $b';\n" 1894 " throw 'not equal. expected: $a, got: $b';\n"
1870 " }\n" 1895 " }\n"
1871 " }\n" 1896 " }\n"
1872 "}\n" 1897 "}\n"
1873 "void setMain(var list) {" 1898 "void setMain(var list) {"
(...skipping 18 matching lines...) Expand all
1892 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1917 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1893 1918
1894 // Test with a typed data view object. 1919 // Test with a typed data view object.
1895 Dart_Handle list_access_test_obj; 1920 Dart_Handle list_access_test_obj;
1896 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL); 1921 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL);
1897 EXPECT_VALID(list_access_test_obj); 1922 EXPECT_VALID(list_access_test_obj);
1898 TestDirectAccess(lib, list_access_test_obj, Dart_TypedData_kInt8); 1923 TestDirectAccess(lib, list_access_test_obj, Dart_TypedData_kInt8);
1899 } 1924 }
1900 1925
1901 1926
1902 TEST_CASE(ByteDataDirectAccess) { 1927 TEST_CASE(TypedDataViewDirectAccessUnverified) {
1928 FLAG_verify_acquired_data = false;
1929 TestTypedDataViewDirectAccess();
1930 }
1931
1932
1933 TEST_CASE(TypedDataViewDirectAccessVerified) {
1934 FLAG_verify_acquired_data = true;
1935 TestTypedDataViewDirectAccess();
1936 }
1937
1938
1939 static void TestByteDataDirectAccess() {
1903 const char* kScriptChars = 1940 const char* kScriptChars =
1904 "import 'dart:typed_data';\n" 1941 "import 'dart:typed_data';\n"
1905 "class Expect {\n" 1942 "class Expect {\n"
1906 " static equals(a, b) {\n" 1943 " static equals(a, b) {\n"
1907 " if (a != b) {\n" 1944 " if (a != b) {\n"
1908 " throw 'not equal. expected: $a, got: $b';\n" 1945 " throw 'not equal. expected: $a, got: $b';\n"
1909 " }\n" 1946 " }\n"
1910 " }\n" 1947 " }\n"
1911 "}\n" 1948 "}\n"
1912 "void setMain(var list) {" 1949 "void setMain(var list) {"
(...skipping 18 matching lines...) Expand all
1931 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1968 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1932 1969
1933 // Test with a typed data view object. 1970 // Test with a typed data view object.
1934 Dart_Handle list_access_test_obj; 1971 Dart_Handle list_access_test_obj;
1935 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL); 1972 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL);
1936 EXPECT_VALID(list_access_test_obj); 1973 EXPECT_VALID(list_access_test_obj);
1937 TestDirectAccess(lib, list_access_test_obj, Dart_TypedData_kByteData); 1974 TestDirectAccess(lib, list_access_test_obj, Dart_TypedData_kByteData);
1938 } 1975 }
1939 1976
1940 1977
1978 TEST_CASE(ByteDataDirectAccessUnverified) {
1979 FLAG_verify_acquired_data = false;
1980 TestByteDataDirectAccess();
1981 }
1982
1983
1984 TEST_CASE(ByteDataDirectAccessVerified) {
1985 FLAG_verify_acquired_data = true;
1986 TestByteDataDirectAccess();
1987 }
1988
1989
1941 static void ExternalTypedDataAccessTests(Dart_Handle obj, 1990 static void ExternalTypedDataAccessTests(Dart_Handle obj,
1942 Dart_TypedData_Type expected_type, 1991 Dart_TypedData_Type expected_type,
1943 uint8_t data[], 1992 uint8_t data[],
1944 intptr_t data_length) { 1993 intptr_t data_length) {
1945 EXPECT_VALID(obj); 1994 EXPECT_VALID(obj);
1946 EXPECT_EQ(expected_type, Dart_GetTypeOfExternalTypedData(obj)); 1995 EXPECT_EQ(expected_type, Dart_GetTypeOfExternalTypedData(obj));
1947 EXPECT(Dart_IsList(obj)); 1996 EXPECT(Dart_IsList(obj));
1948 1997
1949 void* raw_data = NULL; 1998 void* raw_data = NULL;
1950 intptr_t len; 1999 intptr_t len;
(...skipping 6922 matching lines...) Expand 10 before | Expand all | Expand 10 after
8873 result = Dart_Invoke(lib, 8922 result = Dart_Invoke(lib,
8874 NewString("testView16"), 8923 NewString("testView16"),
8875 1, 8924 1,
8876 dart_args); 8925 dart_args);
8877 EXPECT_VALID(result); 8926 EXPECT_VALID(result);
8878 EXPECT(Dart_IsString(result)); 8927 EXPECT(Dart_IsString(result));
8879 } 8928 }
8880 } 8929 }
8881 8930
8882 } // namespace dart 8931 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698