From c9354d88530fea3485fe2e802eadcbc161759691 Mon Sep 17 00:00:00 2001 From: Taiki Date: Sat, 26 Jan 2019 15:04:04 +0100 Subject: [PATCH 1/3] Extend the capabilities of convert_ruby_to_v8 to convert more types, and to work better when the encoding of strings isn't UTF-8. --- ext/mini_racer_extension/extconf.rb | 5 + .../mini_racer_extension.cc | 208 +++++--- ext/mini_racer_extension/simdutf8check.h | 463 ++++++++++++++++++ test/mini_racer_test.rb | 62 ++- 4 files changed, 672 insertions(+), 66 deletions(-) create mode 100644 ext/mini_racer_extension/simdutf8check.h diff --git a/ext/mini_racer_extension/extconf.rb b/ext/mini_racer_extension/extconf.rb index 48196387..beee3729 100644 --- a/ext/mini_racer_extension/extconf.rb +++ b/ext/mini_racer_extension/extconf.rb @@ -11,6 +11,11 @@ $CPPFLAGS += " -fPIC" unless $CPPFLAGS.split.include? "-rdynamic" or IS_DARWIN $CPPFLAGS += " -std=c++0x" $CPPFLAGS += " -fpermissive" +if enable_config('avx2') + $CPPFLAGS += " -mavx2" +else + $CPPFLAGS += " -mssse3" +end $CPPFLAGS += " -Wno-reserved-user-defined-literal" if IS_DARWIN diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index 836f3d3f..bc71ec6c 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -9,6 +9,7 @@ #include #include #include +#include "simdutf8check.h" using namespace v8; @@ -453,7 +454,84 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local::New(isolate, value)); } -static Local convert_ruby_to_v8(Isolate* isolate, VALUE value) { +static VALUE encode_as_utf8(VALUE string) +{ + return rb_funcall(string, rb_intern("encode"), 1, rb_str_new2("UTF-8")); +} + +#ifdef __AVX2__ +static bool (*best_utf8_validate_func(void))(const char *, size_t) +{ + __builtin_cpu_init(); + if (__builtin_cpu_supports("avx2")) { + return validate_utf8_fast_avx; + } else { + return validate_utf8_fast; + } +} +#endif + +static inline Local convert_ruby_str_to_v8( + HandleScope& scope, Isolate *isolate, VALUE value) +{ + static const rb_encoding *utf8_enc = rb_utf8_encoding(); + static const rb_encoding *ascii8bit_enc = rb_ascii8bit_encoding(); + static const rb_encoding *usascii_enc = rb_usascii_encoding(); + static const rb_encoding *latin1_enc = rb_enc_find("ISO-8859-1"); + assert(latin1_enc != nullptr); +#ifndef __AVX2__ +# define validate_utf8 validate_utf8_fast +#else + static const (*validate_utf8)(const char *, size_t) = + best_utf8_validate_func(); +#endif + + rb_encoding *enc = rb_enc_get(value); + char *str = RSTRING_PTR(value); + long len = RSTRING_LEN(value); + if (len < 0 || len > INT_MAX) { + return Null(isolate); + } + bool is_valid_utf8 = enc == utf8_enc && + validate_utf8(str, static_cast(len)); + + MaybeLocal v8str; + int int_len = static_cast(len); + if (is_valid_utf8) { +convert_from_utf8: + v8str = String::NewFromUtf8( + isolate, str, NewStringType::kNormal, int_len); + } else if (enc == utf8_enc || enc == ascii8bit_enc || + enc == usascii_enc || enc == latin1_enc || + rb_funcall(value, rb_intern("valid_encoding?"), 0) == Qfalse) { +treat_as_latin1: + // if ASCII, it could be that the string is invalid + // ignore that possibility (effectively treat it as latin1) + v8str = String::NewFromOneByte( + isolate, reinterpret_cast(str), + NewStringType::kNormal, int_len); + } else { + int state; + VALUE result = rb_protect(encode_as_utf8, value, &state); + + //Ran into an exception! + if (state) { + rb_set_errinfo(Qnil); + goto treat_as_latin1; + } else if (rb_enc_get(result) != utf8_enc) { + // conversion did not result in UTF-8. Odd! + goto treat_as_latin1; + } else { + str = RSTRING_PTR(result); + int_len = RSTRING_LEN(result); + goto convert_from_utf8; + } + } + return v8str.ToLocalChecked(); +} + +static Local convert_ruby_to_v8(Isolate* isolate, VALUE value) +{ EscapableHandleScope scope(isolate); Local array; @@ -466,67 +544,85 @@ static Local convert_ruby_to_v8(Isolate* isolate, VALUE value) { VALUE klass; switch (TYPE(value)) { - case T_FIXNUM: - fixnum = NUM2LONG(value); - if (fixnum > INT_MAX) + case T_FIXNUM: { - return scope.Escape(Number::New(isolate, (double)fixnum)); + fixnum = NUM2LONG(value); + if (fixnum > INT_MAX) + { + return scope.Escape(Number::New(isolate, (double)fixnum)); + } + return scope.Escape(Integer::New(isolate, (int)fixnum)); } - return scope.Escape(Integer::New(isolate, (int)fixnum)); - case T_FLOAT: - return scope.Escape(Number::New(isolate, NUM2DBL(value))); - case T_STRING: - return scope.Escape(String::NewFromUtf8(isolate, RSTRING_PTR(value), NewStringType::kNormal, (int)RSTRING_LEN(value)).ToLocalChecked()); - case T_NIL: - return scope.Escape(Null(isolate)); - case T_TRUE: - return scope.Escape(True(isolate)); - case T_FALSE: - return scope.Escape(False(isolate)); - case T_ARRAY: - length = RARRAY_LEN(value); - array = Array::New(isolate, (int)length); - for(i=0; iSet(i, convert_ruby_to_v8(isolate, rb_ary_entry(value, i))); - } - return scope.Escape(array); - case T_HASH: - object = Object::New(isolate); - hash_as_array = rb_funcall(value, rb_intern("to_a"), 0); - length = RARRAY_LEN(hash_as_array); - for(i=0; iSet(convert_ruby_to_v8(isolate, rb_ary_entry(pair, 0)), - convert_ruby_to_v8(isolate, rb_ary_entry(pair, 1))); - } - return scope.Escape(object); - case T_SYMBOL: - value = rb_funcall(value, rb_intern("to_s"), 0); - return scope.Escape(String::NewFromUtf8(isolate, RSTRING_PTR(value), NewStringType::kNormal, (int)RSTRING_LEN(value)).ToLocalChecked()); - case T_DATA: - klass = rb_funcall(value, rb_intern("class"), 0); - if (klass == rb_cTime || klass == rb_cDateTime) + case T_FLOAT: + return scope.Escape(Number::New(isolate, NUM2DBL(value))); + case T_STRING: + return scope.Escape(convert_ruby_str_to_v8(scope, isolate, value)); + case T_NIL: + return scope.Escape(Null(isolate)); + case T_TRUE: + return scope.Escape(True(isolate)); + case T_FALSE: + return scope.Escape(False(isolate)); + case T_ARRAY: + { + length = RARRAY_LEN(value); + array = Array::New(isolate, (int)length); + for(i=0; iSet(i, convert_ruby_to_v8(isolate, rb_ary_entry(value, i))); + } + return scope.Escape(array); + } + case T_HASH: + { + object = Object::New(isolate); + hash_as_array = rb_funcall(value, rb_intern("to_a"), 0); + length = RARRAY_LEN(hash_as_array); + for(i=0; iSet(convert_ruby_to_v8(isolate, rb_ary_entry(pair, 0)), + convert_ruby_to_v8(isolate, rb_ary_entry(pair, 1))); + } + return scope.Escape(object); + } + case T_SYMBOL: { - if (klass == rb_cDateTime) + value = rb_funcall(value, rb_intern("to_s"), 0); + return scope.Escape(convert_ruby_str_to_v8(scope, isolate, value)); + } + case T_DATA: + { + klass = rb_funcall(value, rb_intern("class"), 0); + if (klass == rb_cTime || klass == rb_cDateTime) { - value = rb_funcall(value, rb_intern("to_time"), 0); + if (klass == rb_cDateTime) + { + value = rb_funcall(value, rb_intern("to_time"), 0); + } + value = rb_funcall(value, rb_intern("to_f"), 0); + return scope.Escape(Date::New(isolate, NUM2DBL(value) * 1000)); + } + // break intentionally missing + } + case T_OBJECT: + case T_CLASS: + case T_ICLASS: + case T_MODULE: + case T_REGEXP: + case T_MATCH: + case T_STRUCT: + case T_BIGNUM: + case T_FILE: + case T_UNDEF: + case T_NODE: + default: + { + if (rb_respond_to(value, rb_intern("to_s"))) { + // TODO: if this throws we're screwed + value = rb_funcall(value, rb_intern("to_s"), 0); + return scope.Escape(convert_ruby_str_to_v8(scope, isolate, value)); } - value = rb_funcall(value, rb_intern("to_f"), 0); - return scope.Escape(Date::New(isolate, NUM2DBL(value) * 1000)); + return scope.Escape(String::NewFromUtf8(isolate, "Undefined Conversion")); } - case T_OBJECT: - case T_CLASS: - case T_ICLASS: - case T_MODULE: - case T_REGEXP: - case T_MATCH: - case T_STRUCT: - case T_BIGNUM: - case T_FILE: - case T_UNDEF: - case T_NODE: - default: - return scope.Escape(String::NewFromUtf8(isolate, "Undefined Conversion")); } } diff --git a/ext/mini_racer_extension/simdutf8check.h b/ext/mini_racer_extension/simdutf8check.h new file mode 100644 index 00000000..9954b2ec --- /dev/null +++ b/ext/mini_racer_extension/simdutf8check.h @@ -0,0 +1,463 @@ + +#ifndef SIMDUTF8CHECK_H +#define SIMDUTF8CHECK_H +#include +#include +#include +#include +#include +/* + * legal utf-8 byte sequence + * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94 + * + * Code Points 1st 2s 3s 4s + * U+0000..U+007F 00..7F + * U+0080..U+07FF C2..DF 80..BF + * U+0800..U+0FFF E0 A0..BF 80..BF + * U+1000..U+CFFF E1..EC 80..BF 80..BF + * U+D000..U+D7FF ED 80..9F 80..BF + * U+E000..U+FFFF EE..EF 80..BF 80..BF + * U+10000..U+3FFFF F0 90..BF 80..BF 80..BF + * U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF + * U+100000..U+10FFFF F4 80..8F 80..BF 80..BF + * + */ + +// all byte values must be no larger than 0xF4 +static inline void checkSmallerThan0xF4(__m128i current_bytes, + __m128i *has_error) { + // unsigned, saturates to 0 below max + *has_error = _mm_or_si128(*has_error, + _mm_subs_epu8(current_bytes, _mm_set1_epi8(0xF4))); +} + +static inline __m128i continuationLengths(__m128i high_nibbles) { + return _mm_shuffle_epi8( + _mm_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4), // 1111, next should be 0 (not checked here) + high_nibbles); +} + +static inline __m128i carryContinuations(__m128i initial_lengths, + __m128i previous_carries) { + + __m128i right1 = + _mm_subs_epu8(_mm_alignr_epi8(initial_lengths, previous_carries, 16 - 1), + _mm_set1_epi8(1)); + __m128i sum = _mm_add_epi8(initial_lengths, right1); + + __m128i right2 = _mm_subs_epu8(_mm_alignr_epi8(sum, previous_carries, 16 - 2), + _mm_set1_epi8(2)); + return _mm_add_epi8(sum, right2); +} + +static inline void checkContinuations(__m128i initial_lengths, __m128i carries, + __m128i *has_error) { + + // overlap || underlap + // carry > length && length > 0 || !(carry > length) && !(length > 0) + // (carries > length) == (lengths > 0) + __m128i overunder = + _mm_cmpeq_epi8(_mm_cmpgt_epi8(carries, initial_lengths), + _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128())); + + *has_error = _mm_or_si128(*has_error, overunder); +} + +// when 0xED is found, next byte must be no larger than 0x9F +// when 0xF4 is found, next byte must be no larger than 0x8F +// next byte must be continuation, ie sign bit is set, so signed < is ok +static inline void checkFirstContinuationMax(__m128i current_bytes, + __m128i off1_current_bytes, + __m128i *has_error) { + __m128i maskED = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xED)); + __m128i maskF4 = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xF4)); + + __m128i badfollowED = + _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x9F)), maskED); + __m128i badfollowF4 = + _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x8F)), maskF4); + + *has_error = _mm_or_si128(*has_error, _mm_or_si128(badfollowED, badfollowF4)); +} + +// map off1_hibits => error condition +// hibits off1 cur +// C => < C2 && true +// E => < E1 && < A0 +// F => < F1 && < 90 +// else false && false +static inline void checkOverlong(__m128i current_bytes, + __m128i off1_current_bytes, __m128i hibits, + __m128i previous_hibits, __m128i *has_error) { + __m128i off1_hibits = _mm_alignr_epi8(hibits, previous_hibits, 16 - 1); + __m128i initial_mins = _mm_shuffle_epi8( + _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1), + off1_hibits); + + __m128i initial_under = _mm_cmpgt_epi8(initial_mins, off1_current_bytes); + + __m128i second_mins = _mm_shuffle_epi8( + _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90), + off1_hibits); + __m128i second_under = _mm_cmpgt_epi8(second_mins, current_bytes); + *has_error = + _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under)); +} + +struct processed_utf_bytes { + __m128i rawbytes; + __m128i high_nibbles; + __m128i carried_continuations; +}; + +static inline void count_nibbles(__m128i bytes, + struct processed_utf_bytes *answer) { + answer->rawbytes = bytes; + answer->high_nibbles = + _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F)); +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct processed_utf_bytes +checkUTF8Bytes(__m128i current_bytes, struct processed_utf_bytes *previous, + __m128i *has_error) { + struct processed_utf_bytes pb; + count_nibbles(current_bytes, &pb); + + checkSmallerThan0xF4(current_bytes, has_error); + + __m128i initial_lengths = continuationLengths(pb.high_nibbles); + + pb.carried_continuations = + carryContinuations(initial_lengths, previous->carried_continuations); + + checkContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m128i off1_current_bytes = + _mm_alignr_epi8(pb.rawbytes, previous->rawbytes, 16 - 1); + checkFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + checkOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +// alternative to _mm_testz_si128(x, x) (which requires SSE 4.1) +static inline bool is_all_zeros(__m128i xmm) { + return _mm_movemask_epi8(_mm_cmpeq_epi8(xmm, _mm_setzero_si128())) == 0xFFFF; +} + +static bool validate_utf8_fast(const char *src, size_t len) { + size_t i = 0; + __m128i has_error = _mm_setzero_si128(); + struct processed_utf_bytes previous = {.rawbytes = _mm_setzero_si128(), + .high_nibbles = _mm_setzero_si128(), + .carried_continuations = + _mm_setzero_si128()}; + if (len >= 16) { + for (; i <= len - 16; i += 16) { + __m128i current_bytes = _mm_loadu_si128((const __m128i *)(src + i)); + previous = checkUTF8Bytes(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[16]; + memset(buffer, 0, 16); + memcpy(buffer, src + i, len - i); + __m128i current_bytes = _mm_loadu_si128((const __m128i *)(buffer)); + previous = checkUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = + _mm_or_si128(_mm_cmpgt_epi8(previous.carried_continuations, + _mm_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 1)), + has_error); + } + + return is_all_zeros(has_error); +} + +#ifdef __AVX2__ + +/*****************************/ +static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b) { + return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15); +} + +static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b) { + return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14); +} + +// all byte values must be no larger than 0xF4 +static inline void avxcheckSmallerThan0xF4(__m256i current_bytes, + __m256i *has_error) { + // unsigned, saturates to 0 below max + *has_error = _mm256_or_si256( + *has_error, _mm256_subs_epu8(current_bytes, _mm256_set1_epi8(0xF4))); +} + +static inline __m256i avxcontinuationLengths(__m256i high_nibbles) { + return _mm256_shuffle_epi8( + _mm256_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4, // 1111, next should be 0 (not checked here) + 1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4 // 1111, next should be 0 (not checked here) + ), + high_nibbles); +} + +static inline __m256i avxcarryContinuations(__m256i initial_lengths, + __m256i previous_carries) { + + __m256i right1 = _mm256_subs_epu8( + push_last_byte_of_a_to_b(previous_carries, initial_lengths), + _mm256_set1_epi8(1)); + __m256i sum = _mm256_add_epi8(initial_lengths, right1); + + __m256i right2 = _mm256_subs_epu8( + push_last_2bytes_of_a_to_b(previous_carries, sum), _mm256_set1_epi8(2)); + return _mm256_add_epi8(sum, right2); +} + +static inline void avxcheckContinuations(__m256i initial_lengths, + __m256i carries, __m256i *has_error) { + + // overlap || underlap + // carry > length && length > 0 || !(carry > length) && !(length > 0) + // (carries > length) == (lengths > 0) + __m256i overunder = _mm256_cmpeq_epi8( + _mm256_cmpgt_epi8(carries, initial_lengths), + _mm256_cmpgt_epi8(initial_lengths, _mm256_setzero_si256())); + + *has_error = _mm256_or_si256(*has_error, overunder); +} + +// when 0xED is found, next byte must be no larger than 0x9F +// when 0xF4 is found, next byte must be no larger than 0x8F +// next byte must be continuation, ie sign bit is set, so signed < is ok +static inline void avxcheckFirstContinuationMax(__m256i current_bytes, + __m256i off1_current_bytes, + __m256i *has_error) { + __m256i maskED = + _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xED)); + __m256i maskF4 = + _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xF4)); + + __m256i badfollowED = _mm256_and_si256( + _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x9F)), maskED); + __m256i badfollowF4 = _mm256_and_si256( + _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x8F)), maskF4); + + *has_error = + _mm256_or_si256(*has_error, _mm256_or_si256(badfollowED, badfollowF4)); +} + +// map off1_hibits => error condition +// hibits off1 cur +// C => < C2 && true +// E => < E1 && < A0 +// F => < F1 && < 90 +// else false && false +static inline void avxcheckOverlong(__m256i current_bytes, + __m256i off1_current_bytes, __m256i hibits, + __m256i previous_hibits, + __m256i *has_error) { + __m256i off1_hibits = push_last_byte_of_a_to_b(previous_hibits, hibits); + __m256i initial_mins = _mm256_shuffle_epi8( + _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1), + off1_hibits); + + __m256i initial_under = _mm256_cmpgt_epi8(initial_mins, off1_current_bytes); + + __m256i second_mins = _mm256_shuffle_epi8( + _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90), + off1_hibits); + __m256i second_under = _mm256_cmpgt_epi8(second_mins, current_bytes); + *has_error = _mm256_or_si256(*has_error, + _mm256_and_si256(initial_under, second_under)); +} + +struct avx_processed_utf_bytes { + __m256i rawbytes; + __m256i high_nibbles; + __m256i carried_continuations; +}; + +static inline void avx_count_nibbles(__m256i bytes, + struct avx_processed_utf_bytes *answer) { + answer->rawbytes = bytes; + answer->high_nibbles = + _mm256_and_si256(_mm256_srli_epi16(bytes, 4), _mm256_set1_epi8(0x0F)); +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct avx_processed_utf_bytes +avxcheckUTF8Bytes(__m256i current_bytes, + struct avx_processed_utf_bytes *previous, + __m256i *has_error) { + struct avx_processed_utf_bytes pb; + avx_count_nibbles(current_bytes, &pb); + + avxcheckSmallerThan0xF4(current_bytes, has_error); + + __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles); + + pb.carried_continuations = + avxcarryContinuations(initial_lengths, previous->carried_continuations); + + avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m256i off1_current_bytes = + push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes); + avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct avx_processed_utf_bytes +avxcheckUTF8Bytes_asciipath(__m256i current_bytes, + struct avx_processed_utf_bytes *previous, + __m256i *has_error) { + if (_mm256_testz_si256(current_bytes, + _mm256_set1_epi8(0x80))) { // fast ascii path + *has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous->carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + *has_error); + return *previous; + } + + struct avx_processed_utf_bytes pb; + avx_count_nibbles(current_bytes, &pb); + + avxcheckSmallerThan0xF4(current_bytes, has_error); + + __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles); + + pb.carried_continuations = + avxcarryContinuations(initial_lengths, previous->carried_continuations); + + avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m256i off1_current_bytes = + push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes); + avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +static bool validate_utf8_fast_avx_asciipath(const char *src, size_t len) { + size_t i = 0; + __m256i has_error = _mm256_setzero_si256(); + struct avx_processed_utf_bytes previous = { + .rawbytes = _mm256_setzero_si256(), + .high_nibbles = _mm256_setzero_si256(), + .carried_continuations = _mm256_setzero_si256()}; + if (len >= 32) { + for (; i <= len - 32; i += 32) { + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i)); + previous = + avxcheckUTF8Bytes_asciipath(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[32]; + memset(buffer, 0, 32); + memcpy(buffer, src + i, len - i); + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous.carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm256_testz_si256(has_error, has_error); +} + +static bool validate_utf8_fast_avx(const char *src, size_t len) { + size_t i = 0; + __m256i has_error = _mm256_setzero_si256(); + struct avx_processed_utf_bytes previous = { + .rawbytes = _mm256_setzero_si256(), + .high_nibbles = _mm256_setzero_si256(), + .carried_continuations = _mm256_setzero_si256()}; + if (len >= 32) { + for (; i <= len - 32; i += 32) { + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[32]; + memset(buffer, 0, 32); + memcpy(buffer, src + i, len - i); + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous.carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm256_testz_si256(has_error, has_error); +} + +#endif // __AVX2__ +#endif diff --git a/test/mini_racer_test.rb b/test/mini_racer_test.rb index 06558108..fd1c3b91 100644 --- a/test/mini_racer_test.rb +++ b/test/mini_racer_test.rb @@ -274,20 +274,62 @@ def test_return_int_max assert_equal(test_num, context.eval("test()")) end - def test_return_unknown + def test_non_utf8_argument context = MiniRacer::Context.new - test_unknown = Date.new # hits T_DATA in convert_ruby_to_v8 - context.attach("test", proc{test_unknown}) - assert_equal("Undefined Conversion", context.eval("test()")) + context.eval('function test(arg) { return "saw " + arg; }') - # clean up and start up a new context - context = nil - GC.start + bad_utf8 = "\x80" + if bad_utf8.encoding != Encoding::UTF_8 + bad_utf8 = bad_utf8.force_encoding('UTF-8') + end + + assert_equal bad_utf8.encoding, Encoding::UTF_8 + assert_equal("saw \u0080", context.call('test', bad_utf8)) + assert_equal("saw \u0080", context.call('test', "\x80".force_encoding('ASCII-8BIT'))) + assert_equal("saw \u0080", context.call('test', "\x80".force_encoding('ISO-8859-1'))) + end + + def test_conversion_to_utf8 + context = MiniRacer::Context.new + context.eval('function test(arg) { return "saw " + arg; }') + + assert_equal("saw real string", context.call("test", "real string")) + + euro = "\x80".force_encoding(Encoding::WINDOWS_1252) # euro! + assert_equal("saw \u20AC", context.call('test', euro)) + end + + def test_failed_conversion_to_utf8 + context = MiniRacer::Context.new + context.eval('function test(arg) { return "saw " + arg; }') + + # euro, but ruby doesn't do the conversion + euro = "\x80".force_encoding(Encoding::WINDOWS_1258) + begin + euro.encode(Encoding::UTF_8) + skip 'Expected ruby not to know how to do this conversion' + rescue Encoding::ConverterNotFoundError + # expected + end + + # in which case we treat it as latin1 + assert_equal("saw \u0080", context.call('test', euro)) + end + def test_non_utf_argument_deep context = MiniRacer::Context.new - test_unknown = Date.new # hits T_DATA in convert_ruby_to_v8 - context.attach("test", proc{test_unknown}) - assert_equal("Undefined Conversion", context.eval("test()")) + context.eval('function test(arg) { return "saw " + Object.keys(arg[0]) + " => " + Object.values(arg[0]); }') + + assert_equal "saw \u0081 => \u0080", context.call("test", [{"\x81" => ["\x80".force_encoding('ASCII-8BIT')]}]) + end + + def test_io_object_arg + # to_s should be called on this + context = MiniRacer::Context.new + context.eval('function test(arg) { return "saw " + arg; }') + + output = context.call("test", IO.new(1)) + assert_match /\Asaw # Date: Sun, 10 Feb 2019 10:57:59 +0100 Subject: [PATCH 2/3] Add a forgotten header --- ext/mini_racer_extension/mini_racer_extension.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index bc71ec6c..7c83d0dd 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -1,4 +1,5 @@ #include +#include #include #include #include From 70d2a56b8984127934bbd4d4b427f002feb658cc Mon Sep 17 00:00:00 2001 From: Gustavo Lopes Date: Tue, 12 Feb 2019 12:35:36 +0000 Subject: [PATCH 3/3] Comply with the terms of the Apache license --- LICENSE.txt | 212 +++++++++++++++++++++++ ext/mini_racer_extension/simdutf8check.h | 9 + 2 files changed, 221 insertions(+) diff --git a/LICENSE.txt b/LICENSE.txt index 109b204f..cb1fdb1d 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -19,3 +19,215 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +The file ext/mini_racer_extension/simdutf8check.h, Apache License 2.0, whose +copyright holders are the contributors to +https://github.com/lemire/fastvalidate-utf-8 (Daniel Lemire, Kendall Willets +and Zach Bjornson). The license is reproduced below: + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +NOTE: This software may be used as part of software released under the +GNU General Public License (GPL) version 2. diff --git a/ext/mini_racer_extension/simdutf8check.h b/ext/mini_racer_extension/simdutf8check.h index 9954b2ec..71528c77 100644 --- a/ext/mini_racer_extension/simdutf8check.h +++ b/ext/mini_racer_extension/simdutf8check.h @@ -1,3 +1,12 @@ +/* + * This file has been modified. The function is_all_zeros was added and + * replaces a _mm_testz_si128 call. This was done so that the program can run + * on older processors. + * + * Do not remove this notice, as it is required by the terms of the Apache + * license. + */ + #ifndef SIMDUTF8CHECK_H #define SIMDUTF8CHECK_H