Skip to content

Commit

Permalink
Merge branch 'main' into fork-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK authored Jan 24, 2025
2 parents 194e8ca + 34013d5 commit 732b6e5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:

env:
BUILDER_VERSION: v0.9.72
BUILDER_VERSION: v0.9.74
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
BUILDER_SOURCE: releases
PACKAGE_NAME: aws-c-common
Expand All @@ -28,4 +28,4 @@ jobs:
run: |
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
chmod a+x builder
./builder build -p ${{ env.PACKAGE_NAME }} --compiler=gcc-9 --coverage --coverage-exclude=source/external/
./builder build -p ${{ env.PACKAGE_NAME }} --compiler=gcc --coverage --coverage-exclude=source/external/
10 changes: 5 additions & 5 deletions source/arch/intel/asm/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ uint64_t aws_run_xgetbv(uint32_t xcr) {
/* NOTE: we could have used the _xgetbv() intrinsic in <immintrin.h>, but it's missing from GCC < 9.0:
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71659 */

/* xgetbv writes high and low of 64bit value to EAX:EDX */
uint32_t eax;
uint32_t edx;
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
return (((uint64_t)eax) << 32) | edx;
/* xgetbv writes high and low of 64bit value to EDX:EAX */
uint32_t xcrhigh;
uint32_t xcrlow;
__asm__ __volatile__("xgetbv" : "=a"(xcrlow), "=d"(xcrhigh) : "c"(xcr));
return (((uint64_t)xcrhigh) << 32) | xcrlow;
}
7 changes: 5 additions & 2 deletions source/arch/intel/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ static void s_cache_cpu_features(void) {
return;
}
aws_run_cpuid(0x7, 0x0, abcd);
s_cpu_features[AWS_CPU_FEATURE_BMI2] = abcd[1] & (1 << 8); /* bmi2 = EBX[bit 8] */
s_cpu_features[AWS_CPU_FEATURE_VPCLMULQDQ] = abcd[2] & (1 << 10); /* vpclmulqdq = ECX[bit 10] */
s_cpu_features[AWS_CPU_FEATURE_BMI2] = abcd[1] & (1 << 8); /* bmi2 = EBX[bit 8] */

/* NOTE: It SHOULD be impossible for a CPU to support AVX2 without supporting AVX.
* But we've received crash reports where the AVX2 feature check passed
Expand All @@ -90,6 +89,10 @@ static void s_cache_cpu_features(void) {
* We don't know for sure what was up with those machines, but this extra
* check should stop them from running our AVX/AVX2 code paths. */
if (feature_avx) {
if (avx_usable) {
s_cpu_features[AWS_CPU_FEATURE_AVX2] = abcd[1] & (1 << 5); /* AVX2 = EBX[bit 5] */
s_cpu_features[AWS_CPU_FEATURE_VPCLMULQDQ] = abcd[2] & (1 << 10); /* vpclmulqdq = ECX[bit 10] */
}
if (avx512_usable) {
s_cpu_features[AWS_CPU_FEATURE_AVX512] = abcd[1] & (1 << 16); /* AVX-512 Foundation = EBX[bit 16] */
}
Expand Down
45 changes: 29 additions & 16 deletions source/host_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ static bool s_is_ipv6_char(uint8_t value) {
return aws_isxdigit(value) || value == ':';
}

static bool s_ends_with(struct aws_byte_cursor cur, uint8_t ch) {
return cur.len > 0 && cur.ptr[cur.len - 1] == ch;
}

bool aws_host_utils_is_ipv4(struct aws_byte_cursor host) {
if (host.len > AWS_IPV4_STR_LEN - 1) {
return false;
Expand Down Expand Up @@ -68,6 +64,8 @@ static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_
* ipv6 can be embedded in url, in which case % must be uri encoded as %25.
* Implementation is fairly trivial and just iterates through the string
* keeping track of the spec above.
* Note: there is no single rfc for IPv6 address - base format defined in RFC 5952,
* zoneId and uri extensions defined in RFC 6874 and RFC 3986
*/
bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
if (host.len == 0) {
Expand All @@ -79,24 +77,39 @@ bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
bool is_split = aws_byte_cursor_next_split(&host, '%', &substr);
AWS_ASSERT(is_split); /* function is guaranteed to return at least one split */

if (!is_split || substr.len == 0 || s_ends_with(substr, ':') ||
!aws_byte_cursor_satisfies_pred(&substr, s_is_ipv6_char)) {
if (!is_split || substr.len < 2 || substr.len > 39 || !aws_byte_cursor_satisfies_pred(&substr, s_is_ipv6_char)) {
return false;
}

if ((substr.ptr[0] == ':' && substr.ptr[1] != ':') || /* no single colon at start */
(substr.ptr[substr.len - 1] == ':' && substr.ptr[substr.len - 2] != ':')) { /* no single colon at end */
return false;
}

uint8_t group_count = 0;
uint8_t group_count = 1; /* string itself is the first group and then every new : we encounter is new group */
uint8_t digit_count = 0;
bool has_double_colon = false;
struct aws_byte_cursor group = {0};
while (aws_byte_cursor_next_split(&substr, ':', &group)) {
++group_count;

if (group_count > 8 || /* too many groups */
group.len > 4 || /* too many chars in group */
(has_double_colon && group.len == 0 && group_count > 2)) { /* only one double colon allowed */
return false;
for (size_t i = 0; i < substr.len; ++i) {
if (substr.ptr[i] == ':') {
++group_count;
digit_count = 0;

if (i > 0 && substr.ptr[i - 1] == ':') {
if (has_double_colon) { /* one double colon max */
return false;
}
has_double_colon = true;
--group_count; /* avoid double counting groups */
}
} else {
++digit_count;
}

has_double_colon = has_double_colon || group.len == 0;
if (digit_count > 4 || /* too many digits in group */
group_count > 8) { /* too many groups */
return false;
}
}

/* second split is optional zone part */
Expand All @@ -110,5 +123,5 @@ bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
}
}

return has_double_colon ? group_count < 7 : group_count == 8;
return has_double_colon ? group_count <= 8 : group_count == 8;
}
25 changes: 23 additions & 2 deletions tests/host_util_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,45 @@ static int s_test_is_ipv6(struct aws_allocator *allocator, void *ctx) {
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001::"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("1:2:3:4:5:6:7::"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::1:2:3:4:5:6:7"), false));

ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:DB8:85A3::8A2E:370:7334"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:DB8:85A3::8A2E:370:7334"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::ffff"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:0:0:8a2e:370:7334"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("0:0:0:0:0:0:0:0"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8::"), false));

ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001::"), false));
ASSERT_FALSE(
aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false));
ASSERT_FALSE(
aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str(":2001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false));
ASSERT_FALSE(
aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001::8a2e::8745"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::2001:0db8:0000:0000:8a2e:0370:7334"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("1:2:3:4:5:6:7:8::"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::1:2:3:4:5:6:7:8"), false));

ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%24en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%25en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0]"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25"), true));

ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:0000:0000:8a2e:0370:7334:1"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:0000"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:85a3:0000:0000:8a2e:0370:7334:"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("g001:0db8:85a3:0000:0000:8a2e:0370:7334"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8::85a3::1"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str(":2001:db8:85a3:0000:0000:8a2e:0370:7334"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("0:0:0:0:0:0:0:0"), false));

return AWS_OP_SUCCESS;
}

0 comments on commit 732b6e5

Please sign in to comment.