Title: [204218] trunk/Source
Revision
204218
Author
[email protected]
Date
2016-08-05 20:48:10 -0700 (Fri, 05 Aug 2016)

Log Message

Add LEBDecoder and tests
https://bugs.webkit.org/show_bug.cgi?id=160625

Reviewed by Benjamin Poulain.

Source/_javascript_Core:

Adds a new target testWASM that is currently used to test the LEB decoder.
In the future, if we add more support for WASM we will put more tests
here.

* _javascript_Core.xcodeproj/project.pbxproj:
* testWASM.cpp: Added.
(CommandLine::CommandLine):
(printUsageStatement):
(CommandLine::parseArguments):
(runLEBTests):
(main):

Source/WTF:

Adds some LEB decoder functions to WTF. These are used in the
WASM spec to encode numbers in a semi-compressed format.

* WTF.xcodeproj/project.pbxproj:
* wtf/LEBDecoder.h: Added.
(decodeUInt32):
(decodeInt32):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (204217 => 204218)


--- trunk/Source/_javascript_Core/ChangeLog	2016-08-06 03:17:09 UTC (rev 204217)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-08-06 03:48:10 UTC (rev 204218)
@@ -1,5 +1,24 @@
 2016-08-05  Keith Miller  <[email protected]>
 
+        Add LEBDecoder and tests
+        https://bugs.webkit.org/show_bug.cgi?id=160625
+
+        Reviewed by Benjamin Poulain.
+
+        Adds a new target testWASM that is currently used to test the LEB decoder.
+        In the future, if we add more support for WASM we will put more tests
+        here.
+
+        * _javascript_Core.xcodeproj/project.pbxproj:
+        * testWASM.cpp: Added.
+        (CommandLine::CommandLine):
+        (printUsageStatement):
+        (CommandLine::parseArguments):
+        (runLEBTests):
+        (main):
+
+2016-08-05  Keith Miller  <[email protected]>
+
         32-bit JSC test failure: stress/instanceof-late-constant-folding.js
         https://bugs.webkit.org/show_bug.cgi?id=160620
 

Modified: trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj (204217 => 204218)


--- trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj	2016-08-06 03:17:09 UTC (rev 204217)
+++ trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj	2016-08-06 03:48:10 UTC (rev 204218)
@@ -25,6 +25,7 @@
 			buildPhases = (
 			);
 			dependencies = (
+				539EB0831D5560F400C82EF7 /* PBXTargetDependency */,
 				0F6183471C45F67A0072450B /* PBXTargetDependency */,
 				0F93275D1C20BF3A00CF6564 /* PBXTargetDependency */,
 				0FEC85B11BDB5D8F0080FF74 /* PBXTargetDependency */,
@@ -1182,6 +1183,9 @@
 		5370B4F51BF26202005C40FC /* AdaptiveInferredPropertyValueWatchpointBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5370B4F31BF25EA2005C40FC /* AdaptiveInferredPropertyValueWatchpointBase.cpp */; };
 		5370B4F61BF26205005C40FC /* AdaptiveInferredPropertyValueWatchpointBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 5370B4F41BF25EA2005C40FC /* AdaptiveInferredPropertyValueWatchpointBase.h */; };
 		53917E7B1B7906FA000EBD33 /* JSGenericTypedArrayViewPrototypeFunctions.h in Headers */ = {isa = PBXBuildFile; fileRef = 53917E7A1B7906E4000EBD33 /* JSGenericTypedArrayViewPrototypeFunctions.h */; };
+		539EB0791D55607000C82EF7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 51F0EB6105C86C6B00E6DF1B /* Foundation.framework */; };
+		539EB07A1D55607000C82EF7 /* _javascript_Core.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 932F5BD90822A1C700736975 /* _javascript_Core.framework */; };
+		539EB0811D55608A00C82EF7 /* testWASM.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 539EB0711D553DF800C82EF7 /* testWASM.cpp */; };
 		539FB8BA1C99DA7C00940FA1 /* JSArrayInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = 539FB8B91C99DA7C00940FA1 /* JSArrayInlines.h */; };
 		53F6BF6D1C3F060A00F41E5D /* InternalFunctionAllocationProfile.h in Headers */ = {isa = PBXBuildFile; fileRef = 53F6BF6C1C3F060A00F41E5D /* InternalFunctionAllocationProfile.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		53FA2AE11CF37F3F0022711D /* LLIntPrototypeLoadAdaptiveStructureWatchpoint.h in Headers */ = {isa = PBXBuildFile; fileRef = 53FA2AE01CF37F3F0022711D /* LLIntPrototypeLoadAdaptiveStructureWatchpoint.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -2152,6 +2156,13 @@
 			remoteGlobalIDString = 0F4680A914BA7FD900BFE272;
 			remoteInfo = "LLInt Offsets";
 		};
+		539EB0821D5560F400C82EF7 /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = 0867D690FE84028FC02AAC07 /* Project object */;
+			proxyType = 1;
+			remoteGlobalIDString = 539EB0751D55607000C82EF7;
+			remoteInfo = testWASM;
+		};
 		5D69E911152BE5470028D720 /* PBXContainerItemProxy */ = {
 			isa = PBXContainerItemProxy;
 			containerPortal = 0867D690FE84028FC02AAC07 /* Project object */;
@@ -3344,6 +3355,8 @@
 		53917E7A1B7906E4000EBD33 /* JSGenericTypedArrayViewPrototypeFunctions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSGenericTypedArrayViewPrototypeFunctions.h; sourceTree = "<group>"; };
 		53917E7C1B791106000EBD33 /* JSTypedArrayViewPrototype.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSTypedArrayViewPrototype.h; sourceTree = "<group>"; };
 		53917E831B791CB8000EBD33 /* TypedArrayPrototype.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode._javascript_; name = TypedArrayPrototype.js; path = builtins/TypedArrayPrototype.js; sourceTree = SOURCE_ROOT; };
+		539EB0711D553DF800C82EF7 /* testWASM.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = testWASM.cpp; sourceTree = "<group>"; };
+		539EB0801D55607000C82EF7 /* testWASM */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = testWASM; sourceTree = BUILT_PRODUCTS_DIR; };
 		539FB8B91C99DA7C00940FA1 /* JSArrayInlines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSArrayInlines.h; sourceTree = "<group>"; };
 		53F256E11B87E28000B4B768 /* JSTypedArrayViewPrototype.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSTypedArrayViewPrototype.cpp; sourceTree = "<group>"; };
 		53F6BF6C1C3F060A00F41E5D /* InternalFunctionAllocationProfile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InternalFunctionAllocationProfile.h; sourceTree = "<group>"; };
@@ -4454,6 +4467,15 @@
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
+		539EB0781D55607000C82EF7 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				539EB0791D55607000C82EF7 /* Foundation.framework in Frameworks */,
+				539EB07A1D55607000C82EF7 /* _javascript_Core.framework in Frameworks */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
 		651122FC14046A4C002B101D /* Frameworks */ = {
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
@@ -4501,6 +4523,7 @@
 				6511230514046A4C002B101D /* testRegExp */,
 				0F9327591C20BCBA00CF6564 /* dynbench */,
 				0F6183431C45F62A0072450B /* testair */,
+				539EB0801D55607000C82EF7 /* testWASM */,
 			);
 			name = Products;
 			sourceTree = "<group>";
@@ -5000,6 +5023,7 @@
 				FECB8B291D25CABB006F2463 /* testapi-function-overrides.js */,
 				86D22219167EF9440024C804 /* testapi.mm */,
 				651122E5140469BA002B101D /* testRegExp.cpp */,
+				539EB0711D553DF800C82EF7 /* testWASM.cpp */,
 			);
 			name = tests;
 			sourceTree = "<group>";
@@ -8305,6 +8329,22 @@
 			productReference = 14BD59BF0A3E8F9000BAF59C /* testapi */;
 			productType = "com.apple.product-type.tool";
 		};
+		539EB0751D55607000C82EF7 /* testWASM */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = 539EB07B1D55607000C82EF7 /* Build configuration list for PBXNativeTarget "testWASM" */;
+			buildPhases = (
+				539EB0761D55607000C82EF7 /* Sources */,
+				539EB0781D55607000C82EF7 /* Frameworks */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = testWASM;
+			productName = testapi;
+			productReference = 539EB0801D55607000C82EF7 /* testWASM */;
+			productType = "com.apple.product-type.tool";
+		};
 		651122F714046A4C002B101D /* testRegExp */ = {
 			isa = PBXNativeTarget;
 			buildConfigurationList = 6511230014046A4C002B101D /* Build configuration list for PBXNativeTarget "testRegExp" */;
@@ -8406,6 +8446,7 @@
 				5D6B2A47152B9E17005231DE /* Test Tools */,
 				0F93274E1C20BCBA00CF6564 /* dynbench */,
 				0F6183381C45F62A0072450B /* testair */,
+				539EB0751D55607000C82EF7 /* testWASM */,
 			);
 		};
 /* End PBXProject section */
@@ -8675,6 +8716,14 @@
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
+		539EB0761D55607000C82EF7 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				539EB0811D55608A00C82EF7 /* testWASM.cpp in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
 		651122FA14046A4C002B101D /* Sources */ = {
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
@@ -9518,6 +9567,11 @@
 			target = 0F4680A914BA7FD900BFE272 /* LLInt Offsets */;
 			targetProxy = 0FF922D214F46B2F0041A24E /* PBXContainerItemProxy */;
 		};
+		539EB0831D5560F400C82EF7 /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			target = 539EB0751D55607000C82EF7 /* testWASM */;
+			targetProxy = 539EB0821D5560F400C82EF7 /* PBXContainerItemProxy */;
+		};
 		5D69E912152BE5470028D720 /* PBXTargetDependency */ = {
 			isa = PBXTargetDependency;
 			target = 932F5BDA0822A1C700736975 /* jsc */;
@@ -9853,6 +9907,38 @@
 			};
 			name = Production;
 		};
+		539EB07C1D55607000C82EF7 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			baseConfigurationReference = BC021BF2136900C300FC5467 /* ToolExecutable.xcconfig */;
+			buildSettings = {
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Debug;
+		};
+		539EB07D1D55607000C82EF7 /* Release */ = {
+			isa = XCBuildConfiguration;
+			baseConfigurationReference = BC021BF2136900C300FC5467 /* ToolExecutable.xcconfig */;
+			buildSettings = {
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Release;
+		};
+		539EB07E1D55607000C82EF7 /* Profiling */ = {
+			isa = XCBuildConfiguration;
+			baseConfigurationReference = BC021BF2136900C300FC5467 /* ToolExecutable.xcconfig */;
+			buildSettings = {
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Profiling;
+		};
+		539EB07F1D55607000C82EF7 /* Production */ = {
+			isa = XCBuildConfiguration;
+			baseConfigurationReference = BC021BF2136900C300FC5467 /* ToolExecutable.xcconfig */;
+			buildSettings = {
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Production;
+		};
 		5D6B2A48152B9E17005231DE /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
@@ -10144,6 +10230,17 @@
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationName = Production;
 		};
+		539EB07B1D55607000C82EF7 /* Build configuration list for PBXNativeTarget "testWASM" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				539EB07C1D55607000C82EF7 /* Debug */,
+				539EB07D1D55607000C82EF7 /* Release */,
+				539EB07E1D55607000C82EF7 /* Profiling */,
+				539EB07F1D55607000C82EF7 /* Production */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Production;
+		};
 		5D6B2A4C152B9E17005231DE /* Build configuration list for PBXAggregateTarget "Test Tools" */ = {
 			isa = XCConfigurationList;
 			buildConfigurations = (

Added: trunk/Source/_javascript_Core/testWASM.cpp (0 => 204218)


--- trunk/Source/_javascript_Core/testWASM.cpp	                        (rev 0)
+++ trunk/Source/_javascript_Core/testWASM.cpp	2016-08-06 03:48:10 UTC (rev 204218)
@@ -0,0 +1,186 @@
+/*
+ * Copyright (C) 2016 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "JSString.h"
+#include <wtf/DataLog.h>
+#include <wtf/LEBDecoder.h>
+
+class CommandLine {
+public:
+    CommandLine(int argc, char** argv)
+    {
+        parseArguments(argc, argv);
+    }
+
+    Vector<String> m_arguments;
+    bool m_runLEBTests { false };
+
+    void parseArguments(int, char**);
+};
+
+static NO_RETURN void printUsageStatement(bool help = false)
+{
+    fprintf(stderr, "Usage: testWASM [options]\n");
+    fprintf(stderr, "  -h|--help  Prints this help message\n");
+    fprintf(stderr, "  -l|--leb   Runs the LEB decoder tests\n");
+    fprintf(stderr, "\n");
+
+    exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+void CommandLine::parseArguments(int argc, char** argv)
+{
+    int i = 1;
+
+    for (; i < argc; ++i) {
+        const char* arg = argv[i];
+        if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
+            printUsageStatement(true);
+
+        if (!strcmp(arg, "-l") || !strcmp(arg, "--leb"))
+            m_runLEBTests = true;
+    }
+
+    for (; i < argc; ++i)
+        m_arguments.append(argv[i]);
+
+}
+
+#define FOR_EACH_UNSIGNED_LEB_TEST(macro) \
+    /* Simple tests that use all the bits in the array */ \
+    macro(({ 0x07 }), 0, true, 0x7, 1) \
+    macro(({ 0x77 }), 0, true, 0x77, 1) \
+    macro(({ 0x80, 0x07 }), 0, true, 0x380, 2) \
+    macro(({ 0x89, 0x12 }), 0, true, 0x909, 2) \
+    macro(({ 0xf3, 0x85, 0x02 }), 0, true, 0x82f3, 3) \
+    macro(({ 0xf3, 0x85, 0xff, 0x74 }), 0, true, 0xe9fc2f3, 4) \
+    macro(({ 0xf3, 0x85, 0xff, 0xf4, 0x7f }), 0, true, 0xfe9fc2f3, 5) \
+    /* Test with extra trailing numbers */ \
+    macro(({ 0x07, 0x80 }), 0, true, 0x7, 1) \
+    macro(({ 0x07, 0x75 }), 0, true, 0x7, 1) \
+    macro(({ 0xf3, 0x85, 0xff, 0x74, 0x43 }), 0, true, 0xe9fc2f3, 4) \
+    macro(({ 0xf3, 0x85, 0xff, 0x74, 0x80 }), 0, true, 0xe9fc2f3, 4) \
+    /* Test with preceeding numbers */ \
+    macro(({ 0xf3, 0x07 }), 1, true, 0x7, 2) \
+    macro(({ 0x03, 0x07 }), 1, true, 0x7, 2) \
+    macro(({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77 }), 5, true, 0x77, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77 }), 5, true, 0x77, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02 }), 3, true, 0x82f3, 6) \
+    /* Test in the middle */ \
+    macro(({ 0xf3, 0x07, 0x89 }), 1, true, 0x7, 2) \
+    macro(({ 0x03, 0x07, 0x23 }), 1, true, 0x7, 2) \
+    macro(({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77, 0x43 }), 5, true, 0x77, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77, 0xf9 }), 5, true, 0x77, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02, 0xa4 }), 3, true, 0x82f3, 6) \
+    /* Test decode too long */ \
+    macro(({ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}), 0, false, 0x0, 0) \
+    macro(({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff}), 1, false, 0x0, 0) \
+    macro(({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff}), 0, false, 0x0, 0) \
+    /* Test decode off end of array */ \
+    macro(({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff}), 2, false, 0x0, 0) \
+
+
+#define TEST_UNSIGNED_LEB_DECODE(init, startOffset, expectedStatus, expectedResult, expectedOffset) \
+    { \
+        Vector<uint8_t> vector = Vector<uint8_t> init; \
+        size_t offset = startOffset; \
+        uint32_t result; \
+        bool status = decodeUInt32(vector, offset, result); \
+        RELEASE_ASSERT(status == expectedStatus); \
+        if (expectedStatus) { \
+            RELEASE_ASSERT(result == expectedResult); \
+            RELEASE_ASSERT(offset == expectedOffset); \
+        } \
+    };
+
+
+#define FOR_EACH_SIGNED_LEB_TEST(macro) \
+    /* Simple tests that use all the bits in the array */ \
+    macro(({ 0x07 }), 0, true, 0x7, 1) \
+    macro(({ 0x77 }), 0, true, -0x9, 1) \
+    macro(({ 0x80, 0x07 }), 0, true, 0x380, 2) \
+    macro(({ 0x89, 0x12 }), 0, true, 0x909, 2) \
+    macro(({ 0xf3, 0x85, 0x02 }), 0, true, 0x82f3, 3) \
+    macro(({ 0xf3, 0x85, 0xff, 0x74 }), 0, true, 0xfe9fc2f3, 4) \
+    macro(({ 0xf3, 0x85, 0xff, 0xf4, 0x7f }), 0, true, 0xfe9fc2f3, 5) \
+    /* Test with extra trailing numbers */ \
+    macro(({ 0x07, 0x80 }), 0, true, 0x7, 1) \
+    macro(({ 0x07, 0x75 }), 0, true, 0x7, 1) \
+    macro(({ 0xf3, 0x85, 0xff, 0x74, 0x43 }), 0, true, 0xfe9fc2f3, 4) \
+    macro(({ 0xf3, 0x85, 0xff, 0x74, 0x80 }), 0, true, 0xfe9fc2f3, 4) \
+    /* Test with preceeding numbers */ \
+    macro(({ 0xf3, 0x07 }), 1, true, 0x7, 2) \
+    macro(({ 0x03, 0x07 }), 1, true, 0x7, 2) \
+    macro(({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77 }), 5, true, -0x9, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77 }), 5, true, -0x9, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02 }), 3, true, 0x82f3, 6) \
+    /* Test in the middle */ \
+    macro(({ 0xf3, 0x07, 0x89 }), 1, true, 0x7, 2) \
+    macro(({ 0x03, 0x07, 0x23 }), 1, true, 0x7, 2) \
+    macro(({ 0xf2, 0x53, 0x43, 0x67, 0x79, 0x77, 0x43 }), 5, true, -0x9, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf7, 0x84, 0x77, 0xf9 }), 5, true, -0x9, 6) \
+    macro(({ 0xf2, 0x53, 0x43, 0xf3, 0x85, 0x02, 0xa4 }), 3, true, 0x82f3, 6) \
+    /* Test decode too long */ \
+    macro(({ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}), 0, false, 0x0, 0) \
+    macro(({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff}), 1, false, 0x0, 0) \
+    macro(({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff}), 0, false, 0x0, 0) \
+    /* Test decode off end of array */ \
+    macro(({ 0x80, 0x80, 0xab, 0x8a, 0x9a, 0xa3, 0xff}), 2, false, 0x0, 0) \
+
+
+#define TEST_SIGNED_LEB_DECODE(init, startOffset, expectedStatus, expectedResult, expectedOffset) \
+    { \
+        Vector<uint8_t> vector = Vector<uint8_t> init; \
+        size_t offset = startOffset; \
+        int32_t result; \
+        bool status = decodeInt32(vector, offset, result); \
+        RELEASE_ASSERT(status == expectedStatus); \
+        if (expectedStatus) { \
+            int32_t expected = expectedResult; \
+            RELEASE_ASSERT(result == expected); \
+            RELEASE_ASSERT(offset == expectedOffset); \
+        } \
+    };
+
+
+static void runLEBTests()
+{
+    FOR_EACH_UNSIGNED_LEB_TEST(TEST_UNSIGNED_LEB_DECODE)
+    FOR_EACH_SIGNED_LEB_TEST(TEST_SIGNED_LEB_DECODE)
+}
+
+
+int main(int argc, char** argv)
+{
+    CommandLine options(argc, argv);
+
+    if (options.m_runLEBTests)
+        runLEBTests();
+
+    return EXIT_SUCCESS;
+}
+

Modified: trunk/Source/WTF/ChangeLog (204217 => 204218)


--- trunk/Source/WTF/ChangeLog	2016-08-06 03:17:09 UTC (rev 204217)
+++ trunk/Source/WTF/ChangeLog	2016-08-06 03:48:10 UTC (rev 204218)
@@ -1,5 +1,20 @@
 2016-08-05  Keith Miller  <[email protected]>
 
+        Add LEBDecoder and tests
+        https://bugs.webkit.org/show_bug.cgi?id=160625
+
+        Reviewed by Benjamin Poulain.
+
+        Adds some LEB decoder functions to WTF. These are used in the
+        WASM spec to encode numbers in a semi-compressed format.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/LEBDecoder.h: Added.
+        (decodeUInt32):
+        (decodeInt32):
+
+2016-08-05  Keith Miller  <[email protected]>
+
         Delete out of date WASM code.
         https://bugs.webkit.org/show_bug.cgi?id=160603
 

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (204217 => 204218)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2016-08-06 03:17:09 UTC (rev 204217)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2016-08-06 03:48:10 UTC (rev 204218)
@@ -114,6 +114,7 @@
 		515F794F1CFC9F4A00CCED93 /* CrossThreadCopier.h in Headers */ = {isa = PBXBuildFile; fileRef = 515F794C1CFC9F4A00CCED93 /* CrossThreadCopier.h */; };
 		515F79501CFC9F4A00CCED93 /* CrossThreadTask.h in Headers */ = {isa = PBXBuildFile; fileRef = 515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */; };
 		515F79561CFD3A6900CCED93 /* CrossThreadQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */; };
+		539EB0631D55284200C82EF7 /* LEBDecoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 539EB0621D55284200C82EF7 /* LEBDecoder.h */; };
 		553071CA1C40427200384898 /* TinyLRUCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 553071C91C40427200384898 /* TinyLRUCache.h */; };
 		5C7C88D41D0A3A0A009D2F6D /* UniqueRef.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C7C88D31D0A3A0A009D2F6D /* UniqueRef.h */; };
 		70A993FE1AD7151300FA615B /* SymbolRegistry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 70A993FC1AD7151300FA615B /* SymbolRegistry.cpp */; };
@@ -450,6 +451,7 @@
 		515F794C1CFC9F4A00CCED93 /* CrossThreadCopier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadCopier.h; sourceTree = "<group>"; };
 		515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadTask.h; sourceTree = "<group>"; };
 		515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadQueue.h; sourceTree = "<group>"; };
+		539EB0621D55284200C82EF7 /* LEBDecoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LEBDecoder.h; sourceTree = "<group>"; };
 		553071C91C40427200384898 /* TinyLRUCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TinyLRUCache.h; sourceTree = "<group>"; };
 		5C7C88D31D0A3A0A009D2F6D /* UniqueRef.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UniqueRef.h; sourceTree = "<group>"; };
 		5D247B6214689B8600E78B76 /* libWTF.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libWTF.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -901,6 +903,7 @@
 				A70DA0821799F04D00529A9B /* Insertion.h */,
 				7CDD7FF7186D291E007433CD /* IteratorAdaptors.h */,
 				7CDD7FF9186D2A54007433CD /* IteratorRange.h */,
+				539EB0621D55284200C82EF7 /* LEBDecoder.h */,
 				A70DA0831799F04D00529A9B /* ListDump.h */,
 				A8A472C1151A825A004123FF /* ListHashSet.h */,
 				0FE164681B6FFC9600400E7C /* Lock.cpp */,
@@ -1215,6 +1218,7 @@
 				A8A47385151A825B004123FF /* ASCIICType.h in Headers */,
 				5C7C88D41D0A3A0A009D2F6D /* UniqueRef.h in Headers */,
 				A8A47434151A825B004123FF /* ASCIIFastPath.h in Headers */,
+				539EB0631D55284200C82EF7 /* LEBDecoder.h in Headers */,
 				A8A47387151A825B004123FF /* Assertions.h in Headers */,
 				A8A47388151A825B004123FF /* Atomics.h in Headers */,
 				A8A47436151A825B004123FF /* AtomicString.h in Headers */,

Added: trunk/Source/WTF/wtf/LEBDecoder.h (0 => 204218)


--- trunk/Source/WTF/wtf/LEBDecoder.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/LEBDecoder.h	2016-08-06 03:48:10 UTC (rev 204218)
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2016 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "Compiler.h"
+#include "Vector.h"
+#include <algorithm>
+
+#include "DataLog.h"
+
+// This file contains a bunch of helper functions for decoding LEB numbers.
+// See https://en.wikipedia.org/wiki/LEB128 for more information about the
+// LEB format.
+
+const size_t maxLEBByteLength = 5;
+
+inline bool WARN_UNUSED_RETURN decodeUInt32(const Vector<uint8_t>& bytes, size_t& offset, uint32_t& result)
+{
+    ASSERT(bytes.size() > offset);
+    result = 0;
+    unsigned shift = 0;
+    size_t last = std::min(maxLEBByteLength, bytes.size() - offset - 1);
+    for (unsigned i = 0; true; ++i) {
+        uint8_t byte = bytes[offset++];
+        result |= (byte & 0x7f) << shift;
+        shift += 7;
+        if (!(byte & 0x80))
+            return true;
+        if (i == last)
+            return false;
+    }
+    RELEASE_ASSERT_NOT_REACHED();
+    return true;
+}
+
+inline bool WARN_UNUSED_RETURN decodeInt32(const Vector<uint8_t>& bytes, size_t& offset, int32_t& result)
+{
+    ASSERT(bytes.size() > offset);
+    result = 0;
+    unsigned shift = 0;
+    size_t last = std::min(maxLEBByteLength, bytes.size() - offset - 1);
+    uint8_t byte;
+    for (unsigned i = 0; true; ++i) {
+        byte = bytes[offset++];
+        result |= (byte & 0x7f) << shift;
+        shift += 7;
+        if (!(byte & 0x80))
+            break;
+        if (i == last)
+            return false;
+    }
+
+    if (shift < 32 && (byte & 0x40))
+        result |= ((-1u) << shift);
+    return true;
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to