Snap for 8564071 from b7a6f5938fe6c35af3cd9c0967485af224d3f873 to mainline-extservices-release

Change-Id: I4e46af549ccc75199d1f78ec0d7b8aa68238230e
diff --git a/.cargo/config b/.cargo/config
new file mode 100644
index 0000000..d8c2032
--- /dev/null
+++ b/.cargo/config
@@ -0,0 +1,2 @@
+[alias]
+xtask = "run --manifest-path ./xtask/Cargo.toml --"
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 05823ce..4f6a222 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
 {
   "git": {
-    "sha1": "b35467b1bfe58eedf4e77025432074d361be591f"
+    "sha1": "61a60923255fe618589bf03ece46fff097b8cb43"
   }
 }
diff --git a/.clang-format b/.clang-format
index b641a64..7460950 100644
--- a/.clang-format
+++ b/.clang-format
@@ -3,6 +3,7 @@
 BasedOnStyle: Google
 DerivePointerAlignment: false
 PointerAlignment: Left
+IncludeBlocks: Preserve
 ---
 Language: ObjC
 BasedOnStyle: Google
diff --git a/.clang-tidy b/.clang-tidy
index 752b25e..8714bad 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -1,37 +1,122 @@
 ---
-# Disable abseil-no-namespace: https://bugs.llvm.org/show_bug.cgi?id=47947
+# Note on checks are disabled on purpose
+#
+# - abseil-no-namespace
+#   https://bugs.llvm.org/show_bug.cgi?id=47947
+#
+# - bugprone-reserved-identifier
+#   Some macros need to be defined for portability purpose; e.g. _BSD_SOURCE.
+#
+# - google-upgrade-googletest-case
+#   This requires googletest 1.10 which is higher than ones installed on many linux distributions.
+#
+# - modernize-redundant-void-arg
+#   Some source should be strictly C99 and func(void) should be used.
+#
+# Note on checks which will be enabled in future. These are good to have but
+# it's not activated yet due to the existing issues with the checks.
+# Once those issues are clear, these checks can be enabled later.
+#
+# - bugprone-branch-clone
+# - bugprone-infinite-loop
+# - bugprone-narrowing-conversions
+# - bugprone-not-null-terminated-result
+# - bugprone-signed-char-misuse
+# - bugprone-sizeof-expression
+# - bugprone-too-small-loop-variable
+# - clang-diagnostic-deprecated-declarations
+# - clang-diagnostic-unused-function
+# - google-readability-avoid-underscore-in-googletest-name
+# - google-runtime-int
+# - google-runtime-references
+# - modernize-avoid-bind
+# - modernize-deprecated-headers
+# - modernize-loop-convert
+# - modernize-pass-by-value
+# - modernize-raw-string-literal
+# - modernize-return-braced-init-list
+# - modernize-use-auto
+# - modernize-use-default-member-init
+# - modernize-use-emplace
+# - modernize-use-equals-default
+# - modernize-use-equals-delete
+# - modernize-use-using
+# - performance-no-automatic-move
+# - performance-unnecessary-copy-initialization
+# - performance-unnecessary-value-param
+# - readability-else-after-return
+# - readability-implicit-bool-conversion
+# - readability-redundant-declaration
+# - readability-static-definition-in-anonymous-namespace
+#
 Checks: '-*,
   abseil-*,
   -abseil-no-namespace,
   bugprone-*,
+  -bugprone-branch-clone,
+  -bugprone-infinite-loop,
   -bugprone-narrowing-conversions,
+  -bugprone-not-null-terminated-result,
+  -bugprone-reserved-identifier,
+  -bugprone-signed-char-misuse,
+  -bugprone-sizeof-expression,
   -bugprone-too-small-loop-variable,
-  performance-*,
-  -performance-unnecessary-copy-initialization,
-  -performance-unnecessary-value-param,
   google-*,
+  -google-readability-avoid-underscore-in-googletest-name,
   -google-runtime-int,
   -google-runtime-references,
+  -google-upgrade-googletest-case,
+  performance-*,
+  -performance-no-automatic-move,
+  -performance-unnecessary-copy-initialization,
+  -performance-unnecessary-value-param,
+  clang-diagnostic-deprecated-register,
+  clang-diagnostic-expansion-to-defined,
+  clang-diagnostic-ignored-attributes,
+  clang-diagnostic-non-pod-varargs,
+  clang-diagnostic-shadow-field,
+  clang-diagnostic-shift-sign-overflow,
+  clang-diagnostic-tautological-undefined-compare,
+  clang-diagnostic-thread-safety*,
+  clang-diagnostic-undefined-bool-conversion,
+  clang-diagnostic-unreachable-code,
+  clang-diagnostic-unreachable-code-loop-increment,
+  clang-diagnostic-unused-const-variable,
+  clang-diagnostic-unused-lambda-capture,
+  clang-diagnostic-unused-local-typedef,
+  clang-diagnostic-unused-private-field,
+  clang-diagnostic-user-defined-warnings,
   misc-definitions-in-headers,
   misc-static-assert,
   misc-unconventional-assign-operator,
   misc-uniqueptr-reset-release,
   misc-unused-alias-decls,
   misc-unused-using-decls,
+  modernize-make-shared,
   modernize-make-unique,
-  -modernize-redundant-void-arg,
   modernize-replace-auto-ptr,
+  modernize-replace-random-shuffle,
   modernize-shrink-to-fit,
+  modernize-unary-static-assert,
   modernize-use-bool-literals,
+  modernize-use-noexcept,
   modernize-use-nullptr,
   modernize-use-override,
+  modernize-use-transparent-functors,
+  readability-const-return-type,
   readability-container-size-empty,
+  readability-delete-null-pointer,
   readability-deleted-default,
   readability-function-size,
   readability-inconsistent-declaration-parameter-name,
+  readability-misleading-indentation,
+  readability-misplaced-array-index,
   readability-redundant-control-flow,
+  readability-redundant-function-ptr-dereference,
   readability-redundant-smartptr-get,
-  readability-string-compare'
+  readability-simplify-boolean-expr,
+  readability-string-compare,
+  readability-uniqueptr-delete-release'
 WarningsAsErrors: '*'
 CheckOptions:
   - key:    readability-function-size.StatementThreshold
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 3731f9e..f25093f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -10,7 +10,8 @@
 
 env:
   RUST_BACKTRACE: 1
-  RUSTFLAGS: "--deny=warnings"
+  # Some of the bindgen tests generate "deref-nullptr" warnings, see https://github.com/rust-lang/rust-bindgen/issues/1651
+  RUSTFLAGS: "--deny=warnings --allow deref-nullptr"
   TEST_BIND: 1
 
 jobs:
@@ -21,38 +22,58 @@
     - uses: actions/checkout@v2
     - run: sudo apt-get install -y clang-tidy-9
     - run: sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-9 100
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version && which cmake && cmake --version
-    - run: scripts/reset-submodule.cmd
+    - run: which cargo && cargo version && clang --version && openssl version && which cmake && cmake --version
+    - run: cargo xtask submodule
     - run: cargo fmt --all -- --check
     - run: cargo clippy --all -- -D clippy::all && cargo clippy --all --no-default-features --features prost-codec -- -D clippy::all
-    - run: scripts/lint-grpc-sys.sh && git diff-index --quiet HEAD
+    - run: cargo xtask clang-lint && git diff-index --quiet HEAD
 
   Linux-Stable:
-    name: Linux-Stable
-    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        include:
+          - host: ubuntu-latest
+            profile: 
+            suffix:
+          - host: ARM64
+            profile: --release
+            suffix: -Arm64
+    name: Linux-Stable${{ matrix.suffix }}
+    runs-on: ${{ matrix.host }}
     steps:
     - uses: actions/checkout@v2
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
-    - run: scripts/reset-submodule.cmd
-    - run: env TEST_BIND=0 scripts/generate-bindings.sh && git diff --exit-code HEAD
-    - run: scripts/generate-bindings.sh
+    - run: sudo apt install -y protobuf-compiler
+    - run: which cargo && cargo version && clang --version && openssl version
+    - run: cargo xtask submodule
+    - run: env TEST_BIND=0 cargo xtask bindgen && git diff --exit-code HEAD
+    - run: cargo xtask codegen && git diff --exit-code HEAD;
+    - run: cargo xtask bindgen
     - run: cargo build --no-default-features
     - run: cargo build --no-default-features --features protobuf-codec
     - run: cargo build --no-default-features --features prost-codec
     - run: cd proto && cargo build --no-default-features --features prost-codec
     - run: cargo build
-    - run: cargo test --all
+    - run: cargo test --all ${{ matrix.profile }}
 
   Linux-Stable-openssl:
-    name: Linux-Stable-openssl
-    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        include:
+          - host: ubuntu-latest
+            profile: 
+            suffix:
+          - host: ARM64
+            profile: --release
+            suffix: -Arm64
+    name: Linux-Stable-openssl${{ matrix.suffix }}
+    runs-on: ${{ matrix.host }}
     steps:
     - uses: actions/checkout@v2
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
-    - run: scripts/reset-submodule.cmd
-    - run: cargo test --features "openssl-vendored" --all
+    - run: which cargo && cargo version && clang --version && openssl version
+    - run: cargo xtask submodule
+    - run: cargo test --features "openssl-vendored" --all ${{ matrix.profile }}
     - run: cargo clean
-    - run: cargo test --features "openssl" --all
+    - run: cargo test --features "openssl" --all ${{ matrix.profile }}
 
   Linux-Nightly:
     name: Linux-Nightly
@@ -60,8 +81,8 @@
     steps:
     - uses: actions/checkout@v2
     - run: rustup default nightly
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
-    - run: scripts/reset-submodule.cmd
+    - run: which cargo && cargo version && clang --version && openssl version
+    - run: cargo xtask submodule
     - run: cargo build --no-default-features
     - run: cargo build --no-default-features --features protobuf-codec
     - run: cargo build --no-default-features --features prost-codec
@@ -74,8 +95,8 @@
     runs-on: macos-latest
     steps:
     - uses: actions/checkout@v2
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
-    - run: scripts/reset-submodule.cmd
+    - run: which cargo && cargo version && clang --version && openssl version
+    - run: cargo xtask submodule
     - run: cargo build --no-default-features --features use-bindgen
     - run: cargo build --no-default-features --features "protobuf-codec use-bindgen"
     - run: cargo build --no-default-features --features "prost-codec use-bindgen"
@@ -88,8 +109,8 @@
     steps:
     - uses: actions/checkout@v2
     - run: brew update && brew upgrade [email protected]
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
-    - run: scripts/reset-submodule.cmd
+    - run: which cargo && cargo version && clang --version && openssl version
+    - run: cargo xtask submodule
     - run: OPENSSL_ROOT_DIR="/usr/local/opt/[email protected]/" cargo test --features "openssl" --all
     - run: cargo test --features "openssl-vendored" --all
 
@@ -98,12 +119,13 @@
     runs-on: windows-latest
     env:
       LIBCLANG_PATH: 'C:\Program Files\LLVM\bin'
+      RUSTFLAGS: ""
     steps:
     - uses: actions/checkout@v2
     - run: choco install -y llvm
     - run: refreshenv
     - run: go version ; cargo version ; cmake --version 
-    - run: scripts/reset-submodule.cmd
+    - run: cargo xtask submodule
     - run: cargo build
     - run: cargo test --all
   
@@ -112,7 +134,7 @@
     runs-on: ubuntu-latest
     steps:
     - uses: actions/checkout@v2
-    - run: scripts/reset-submodule.cmd
+    - run: cargo xtask submodule
     - run: cd grpc-sys && cargo publish --dry-run
     - name: Check generated package size
       run: |
diff --git a/.gitmodules b/.gitmodules
deleted file mode 100644
index 3ed0061..0000000
--- a/.gitmodules
+++ /dev/null
@@ -1,4 +0,0 @@
-[submodule "grpc-sys/grpc"]
-	path = grpc-sys/grpc
-	url = https://github.com/pingcap/grpc.git
-	branch = rs-release
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 9cc0821..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,59 +0,0 @@
-# Travis is only used to test ARM64 Linux
-
-dist: focal
-sudo: true
-language: rust
-git:
-  submodules: false
-
-rust: stable
-
-env:
-  global:
-    - RUST_BACKTRACE=1
-    # absl deadlock detection performs poorly on arm, so we build it release
-    # mode to skip the check. Enabling debug-assertions to get safer test
-    # results.
-    - RUSTFLAGS="--deny=warnings -C debug-assertions"
-    - TEST_BIND=1
-
-addons:
-  apt:
-    update: true
-    packages:
-      - libunwind-dev
-
-jobs:
-  include:
-  - os: linux
-    arch: arm64-graviton2
-    vm: virt
-    before_script:
-      - scripts/reset-submodule.cmd
-      - export GRPC_VERSION=1.35.0
-      - export PATH="$PATH:$HOME/.cache/bin:$HOME/.cargo/bin"
-      - which cmake && cmake --version && openssl version
-      - eval "$(gimme stable)"
-    script:
-      - if [[ $TRAVIS_OS_NAME == "linux" ]] && [[ $TRAVIS_RUST_VERSION == "stable" ]]; then
-          rustup component add rustfmt && cargo fmt --all -- --check;
-          env TEST_BIND=0 scripts/generate-bindings.sh && git diff --exit-code HEAD;
-        fi
-      - ./scripts/generate-bindings.sh
-      - cargo build --no-default-features
-      - cargo build --no-default-features --features protobuf-codec
-      - cargo build --no-default-features --features prost-codec
-      - cargo build
-      - travis_wait 40 cargo test --release --all
-  - os: linux
-    arch: arm64-graviton2
-    vm: virt
-    before_script:
-      - scripts/reset-submodule.cmd
-      - export GRPC_VERSION=1.35.0
-      - export PATH="$PATH:$HOME/.cache/bin:$HOME/.cargo/bin"
-      - sudo apt-get update && sudo apt-get -y install libssl-dev
-      - which cmake && cmake --version && openssl version
-      - eval "$(gimme stable)"
-    script:
-      - travis_wait 40 cargo test --release --features "openssl-vendored" --all
diff --git a/Android.bp b/Android.bp
index 44ad1a2..8ee540c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,4 +1,4 @@
-// This file is generated by cargo2android.py --run --device --dependencies --features=protobuf,protobuf-codec.
+// This file is generated by cargo2android.py --config cargo2android.json.
 // Do not modify this file as changes will be overridden on upgrade.
 
 package {
@@ -22,6 +22,8 @@
     name: "libgrpcio",
     host_supported: true,
     crate_name: "grpcio",
+    cargo_env_compat: true,
+    cargo_pkg_version: "0.9.1",
     srcs: ["src/lib.rs"],
     edition: "2018",
     features: [
@@ -36,43 +38,9 @@
         "libparking_lot",
         "libprotobuf",
     ],
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.bluetooth",
+    ],
+    min_sdk_version: "29",
 }
-
-// dependent_library ["feature_list"]
-//   boringssl-src-0.2.0
-//   cc-1.0.67
-//   cfg-if-1.0.0
-//   cmake-0.1.45
-//   futures-0.3.13 "alloc,async-await,default,executor,futures-executor,std"
-//   futures-channel-0.3.13 "alloc,futures-sink,sink,std"
-//   futures-core-0.3.13 "alloc,std"
-//   futures-executor-0.3.13 "std"
-//   futures-io-0.3.13 "std"
-//   futures-macro-0.3.13
-//   futures-sink-0.3.13 "alloc,std"
-//   futures-task-0.3.13 "alloc,std"
-//   futures-util-0.3.13 "alloc,async-await,async-await-macro,channel,futures-channel,futures-io,futures-macro,futures-sink,io,memchr,proc-macro-hack,proc-macro-nested,sink,slab,std"
-//   grpcio-sys-0.8.1
-//   instant-0.1.9
-//   libc-0.2.92 "default,std"
-//   libz-sys-1.1.2 "default,libc,static,stock-zlib"
-//   lock_api-0.4.2
-//   log-0.4.14
-//   memchr-2.3.4 "default,std"
-//   parking_lot-0.11.1 "default"
-//   parking_lot_core-0.8.3
-//   pin-project-lite-0.2.6
-//   pin-utils-0.1.0
-//   pkg-config-0.3.19
-//   proc-macro-hack-0.5.19
-//   proc-macro-nested-0.1.7
-//   proc-macro2-1.0.26 "default,proc-macro"
-//   protobuf-2.22.1
-//   quote-1.0.9 "default,proc-macro"
-//   same-file-1.0.6
-//   scopeguard-1.1.0
-//   slab-0.4.2
-//   smallvec-1.6.1
-//   syn-1.0.68 "clone-impls,default,derive,full,parsing,printing,proc-macro,quote"
-//   unicode-xid-0.2.1 "default"
-//   walkdir-2.3.2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f928b24..aa28773 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,20 @@
-# 0.8.2 - 2012-03-10
+# 0.9.1 - 2021-09-18
+
+- Make boringssl-src optional (#537)
+- codec: avoid overflowing error (#495)
+
+# 0.9.0 - 2021-05-24
+
+- Support rich error (#514)
+- Provide default service implementations (#521)
+- Support abstract UDS (#523)
+- Use default-features=false on libz-sys to allow for zlib-ng (#525)
+- Update grpc to 1.38.0 (#526)
+
+# 0.8.2 - 2021-03-10
 
 - Fix send requirement in connectivity APIs (#516)
+- Add default health service implemetations (#518)
 
 # 0.8.1 - 2021-03-05
 
diff --git a/Cargo.toml b/Cargo.toml
index c8ccf28..fd9e71f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,7 +13,7 @@
 [package]
 edition = "2018"
 name = "grpcio"
-version = "0.8.2"
+version = "0.9.1"
 authors = ["The TiKV Project Developers"]
 autoexamples = false
 description = "The rust language implementation of gRPC, base on the gRPC c core library."
@@ -26,8 +26,6 @@
 repository = "https://github.com/tikv/grpc-rs"
 [package.metadata.docs.rs]
 all-features = true
-[profile.release]
-debug = true
 [dependencies.bytes]
 version = "1.0"
 optional = true
@@ -36,7 +34,7 @@
 version = "0.3"
 
 [dependencies.grpcio-sys]
-version = "0.8"
+version = "0.9"
 default-features = false
 
 [dependencies.libc]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 8938f97..9755697 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
 [package]
 name = "grpcio"
-version = "0.8.2"
+version = "0.9.1"
 edition = "2018"
 authors = ["The TiKV Project Developers"]
 license = "Apache-2.0"
@@ -17,7 +17,7 @@
 all-features = true
 
 [dependencies]
-grpcio-sys = { path = "grpc-sys", version = "0.8", default-features = false }
+grpcio-sys = { path = "grpc-sys", version = "0.9", default-features = false }
 libc = "0.2"
 futures = "0.3"
 protobuf = { version = "2.0", optional = true }
@@ -27,7 +27,17 @@
 parking_lot = "0.11"
 
 [workspace]
-members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"]
+members = [
+    "proto",
+    "benchmark",
+    "compiler",
+    "health",
+    "interop",
+    "tests-and-examples",
+]
+# Don't include it in workspace to make it possible to use different version of
+# rust-protobuf.
+exclude = ["xtask"]
 
 [features]
 default = ["protobuf-codec", "secure", "use-bindgen"]
@@ -39,11 +49,8 @@
 no-omit-frame-pointer = ["grpcio-sys/no-omit-frame-pointer"]
 use-bindgen = ["grpcio-sys/use-bindgen"]
 
-[profile.release]
-debug = true
-
 [badges]
 travis-ci = { repository = "tikv/grpc-rs" }
 
 [patch.crates-io]
-grpcio-compiler = { path = "compiler", version = "0.8.0", default-features = false }
+grpcio-compiler = { path = "compiler", version = "0.9.0", default-features = false }
diff --git a/METADATA b/METADATA
index a81517a..8384cab 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@
   }
   url {
     type: ARCHIVE
-    value: "https://static.crates.io/crates/grpcio/grpcio-0.8.2.crate"
+    value: "https://static.crates.io/crates/grpcio/grpcio-0.9.1.crate"
   }
-  version: "0.8.2"
+  version: "0.9.1"
   license_type: NOTICE
   last_upgrade_date {
     year: 2021
-    month: 4
-    day: 1
+    month: 9
+    day: 22
   }
 }
diff --git a/README.android b/README.android
deleted file mode 100644
index ef72ffa..0000000
--- a/README.android
+++ /dev/null
@@ -1,2 +0,0 @@
-The patch in patches/env.diff needs to be updated with crate version update to match
-the new crate version.
diff --git a/README.md b/README.md
index d4d384a..ca9e9da 100644
--- a/README.md
+++ b/README.md
@@ -44,7 +44,7 @@
 ## Build
 
 ```
-$ ./scripts/reset-submodule.cmd # if you just cloned the repository
+$ cargo xtask submodule # if you just cloned the repository
 $ cargo build
 ```
 
@@ -142,5 +142,5 @@
 If the content in grpc-sys/grpc is updated, you may need to regenerate bindings:
 
 ```
-$ ./scripts/generate-bindings.sh
+$ cargo xtask bindgen
 ```
diff --git a/cargo2android.json b/cargo2android.json
new file mode 100644
index 0000000..001bc3e
--- /dev/null
+++ b/cargo2android.json
@@ -0,0 +1,10 @@
+{
+  "apex-available": [
+    "//apex_available:platform",
+    "com.android.bluetooth"
+  ],
+  "device": true,
+  "features": "protobuf,protobuf-codec",
+  "min-sdk-version": "29",
+  "run": true
+}
diff --git a/scripts/format-grpc-sys b/scripts/format-grpc-sys
deleted file mode 100755
index d97445a..0000000
--- a/scripts/format-grpc-sys
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/usr/bin/env bash
-clang-format-5.0 -i grpc-sys/grpc_wrap.cc
diff --git a/scripts/generate-bindings.sh b/scripts/generate-bindings.sh
deleted file mode 100755
index 81a043e..0000000
--- a/scripts/generate-bindings.sh
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/bash
-
-# NOTE: 
-# This script is only used when you want to generate bindings yourself.
-# The generated bindings will overwrite grpc-sys/bindings/*
-
-if [ "$ARCH" == "" ]; then
-    ARCH=`uname -p`
-fi
-export UPDATE_BIND=1
-cargo build -p grpcio-sys --target ${ARCH}-unknown-linux-gnu
-rustfmt grpc-sys/bindings/*
-if [ "$(uname -s)" == "Linux" ]; then
-  sed -i '/^pub type .*= ::std::os::raw::.*/d' grpc-sys/bindings/*
-fi
diff --git a/scripts/lint-grpc-sys.sh b/scripts/lint-grpc-sys.sh
deleted file mode 100755
index a3fc7e0..0000000
--- a/scripts/lint-grpc-sys.sh
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/usr/bin/env bash
-clang-tidy grpc-sys/grpc_wrap.cc -- -Igrpc-sys/grpc/include -x c++ -std=c++11
diff --git a/scripts/reset-submodule.cmd b/scripts/reset-submodule.cmd
deleted file mode 100755
index bac6a35..0000000
--- a/scripts/reset-submodule.cmd
+++ /dev/null
@@ -1,9 +0,0 @@
-git submodule update --init grpc-sys/grpc
-cd grpc-sys/grpc
-git submodule update --init third_party/cares/cares
-git submodule update --init third_party/abseil-cpp
-git submodule update --init third_party/re2
-rm -rf third_party/boringssl-with-bazel/*
-cd third_party/zlib
-git clean -df
-git reset --hard
diff --git a/src/call/client.rs b/src/call/client.rs
index d1047bf..e89862a 100644
--- a/src/call/client.rs
+++ b/src/call/client.rs
@@ -110,7 +110,7 @@
     ) -> Result<ClientUnaryReceiver<Resp>> {
         let call = channel.create_call(method, &opt)?;
         let mut payload = GrpcSlice::default();
-        (method.req_ser())(req, &mut payload);
+        (method.req_ser())(req, &mut payload)?;
         let cq_f = check_run(BatchType::CheckRead, |ctx, tag| unsafe {
             grpc_sys::grpcwrap_call_start_unary(
                 call.call,
@@ -163,7 +163,7 @@
     ) -> Result<ClientSStreamReceiver<Resp>> {
         let call = channel.create_call(method, &opt)?;
         let mut payload = GrpcSlice::default();
-        (method.req_ser())(req, &mut payload);
+        (method.req_ser())(req, &mut payload)?;
         let cq_f = check_run(BatchType::Finish, |ctx, tag| unsafe {
             grpc_sys::grpcwrap_call_start_server_streaming(
                 call.call,
diff --git a/src/call/mod.rs b/src/call/mod.rs
index 7f1582f..25174ea 100644
--- a/src/call/mod.rs
+++ b/src/call/mod.rs
@@ -8,8 +8,8 @@
 use std::sync::Arc;
 use std::{ptr, slice};
 
-use crate::cq::CompletionQueue;
 use crate::grpc_sys::{self, grpc_call, grpc_call_error, grpcwrap_batch_context};
+use crate::{cq::CompletionQueue, Metadata, MetadataBuilder};
 use futures::future::Future;
 use futures::ready;
 use futures::task::{Context, Poll};
@@ -156,10 +156,15 @@
 #[derive(Debug, Clone)]
 pub struct RpcStatus {
     /// gRPC status code. `Ok` indicates success, all other values indicate an error.
-    pub status: RpcStatusCode,
+    code: RpcStatusCode,
 
-    /// Optional detail string.
-    pub details: Option<String>,
+    /// error message.
+    message: String,
+
+    /// Additional details for rich error model.
+    ///
+    /// See also https://grpc.io/docs/guides/error/#richer-error-model.
+    details: Vec<u8>,
 }
 
 impl Display for RpcStatus {
@@ -170,16 +175,54 @@
 
 impl RpcStatus {
     /// Create a new [`RpcStatus`].
-    pub fn new<T: Into<RpcStatusCode>>(code: T, details: Option<String>) -> RpcStatus {
+    pub fn new<T: Into<RpcStatusCode>>(code: T) -> RpcStatus {
+        RpcStatus::with_message(code, String::new())
+    }
+
+    /// Create a new [`RpcStatus`] with given message.
+    pub fn with_message<T: Into<RpcStatusCode>>(code: T, message: String) -> RpcStatus {
+        RpcStatus::with_details(code, message, vec![])
+    }
+
+    /// Create a new [`RpcStats`] with code, message and details.
+    ///
+    /// If using rich error model, `details` should be binary message that sets `code` and
+    /// `message` to the same value. Or you can use `into` method to do automatical
+    /// transformation if using `grpcio_proto::google::rpc::Status`.
+    pub fn with_details<T: Into<RpcStatusCode>>(
+        code: T,
+        message: String,
+        details: Vec<u8>,
+    ) -> RpcStatus {
         RpcStatus {
-            status: code.into(),
+            code: code.into(),
+            message,
             details,
         }
     }
 
     /// Create a new [`RpcStatus`] that status code is Ok.
     pub fn ok() -> RpcStatus {
-        RpcStatus::new(RpcStatusCode::OK, None)
+        RpcStatus::new(RpcStatusCode::OK)
+    }
+
+    /// Return the instance's error code.
+    #[inline]
+    pub fn code(&self) -> RpcStatusCode {
+        self.code
+    }
+
+    /// Return the instance's error message.
+    #[inline]
+    pub fn message(&self) -> &str {
+        &self.message
+    }
+
+    /// Return the (binary) error details.
+    ///
+    /// Usually it contains a serialized `google.rpc.Status` proto.
+    pub fn details(&self) -> &[u8] {
+        &self.details
     }
 }
 
@@ -216,21 +259,26 @@
             grpc_sys::grpcwrap_batch_context_recv_status_on_client_status(self.ctx)
         });
 
-        let details = if status == RpcStatusCode::OK {
-            None
+        if status == RpcStatusCode::OK {
+            RpcStatus::ok()
         } else {
             unsafe {
-                let mut details_len = 0;
+                let mut msg_len = 0;
                 let details_ptr = grpc_sys::grpcwrap_batch_context_recv_status_on_client_details(
                     self.ctx,
-                    &mut details_len,
+                    &mut msg_len,
                 );
-                let details_slice = slice::from_raw_parts(details_ptr as *const _, details_len);
-                Some(String::from_utf8_lossy(details_slice).into_owned())
+                let msg_slice = slice::from_raw_parts(details_ptr as *const _, msg_len);
+                let message = String::from_utf8_lossy(msg_slice).into_owned();
+                let m_ptr =
+                    grpc_sys::grpcwrap_batch_context_recv_status_on_client_trailing_metadata(
+                        self.ctx,
+                    );
+                let metadata = &*(m_ptr as *const Metadata);
+                let details = metadata.search_binary_error_details().to_vec();
+                RpcStatus::with_details(status, message, details)
             }
-        };
-
-        RpcStatus::new(status, details)
+        }
     }
 
     /// Fetch the response bytes of the rpc call.
@@ -352,22 +400,31 @@
         let _cq_ref = self.cq.borrow()?;
         let send_empty_metadata = if send_empty_metadata { 1 } else { 0 };
         let f = check_run(BatchType::Finish, |ctx, tag| unsafe {
-            let details_ptr = status
-                .details
-                .as_ref()
-                .map_or_else(ptr::null, |s| s.as_ptr() as _);
-            let details_len = status.details.as_ref().map_or(0, String::len);
+            let (msg_ptr, msg_len) = if status.code() == RpcStatusCode::OK {
+                (ptr::null(), 0)
+            } else {
+                (status.message.as_ptr(), status.message.len())
+            };
             let payload_p = match payload {
                 Some(p) => p.as_mut_ptr(),
                 None => ptr::null_mut(),
             };
+            let mut trailing_metadata = if status.details.is_empty() {
+                None
+            } else {
+                let mut builder = MetadataBuilder::new();
+                builder.set_binary_error_details(&status.details);
+                Some(builder.build())
+            };
             grpc_sys::grpcwrap_call_send_status_from_server(
                 self.call,
                 ctx,
-                status.status.into(),
-                details_ptr,
-                details_len,
-                ptr::null_mut(),
+                status.code().into(),
+                msg_ptr as _,
+                msg_len,
+                trailing_metadata
+                    .as_mut()
+                    .map_or_else(ptr::null_mut, |m| m as *mut _ as _),
                 send_empty_metadata,
                 payload_p,
                 write_flags,
@@ -390,17 +447,17 @@
         let (batch_ptr, tag_ptr) = box_batch_tag(tag);
 
         let code = unsafe {
-            let details_ptr = status
-                .details
-                .as_ref()
-                .map_or_else(ptr::null, |s| s.as_ptr() as _);
-            let details_len = status.details.as_ref().map_or(0, String::len);
+            let (msg_ptr, msg_len) = if status.code() == RpcStatusCode::OK {
+                (ptr::null(), 0)
+            } else {
+                (status.message.as_ptr(), status.message.len())
+            };
             grpc_sys::grpcwrap_call_send_status_from_server(
                 call_ptr,
                 batch_ptr,
-                status.status.into(),
-                details_ptr,
-                details_len,
+                status.code().into(),
+                msg_ptr as _,
+                msg_len,
                 ptr::null_mut(),
                 1,
                 ptr::null_mut(),
@@ -660,7 +717,7 @@
         // temporary fix: buffer hint with send meta will not send out any metadata.
         // note: only the first message can enter this code block.
         if self.send_metadata {
-            ser(t, &mut self.buffer);
+            ser(t, &mut self.buffer)?;
             self.buf_flags = Some(flags);
             self.start_send_buffer_message(false, call)?;
             self.send_metadata = false;
@@ -673,7 +730,7 @@
             self.start_send_buffer_message(true, call)?;
         }
 
-        ser(t, &mut self.buffer);
+        ser(t, &mut self.buffer)?;
         let hint = flags.get_buffer_hint();
         self.last_buf_hint &= hint;
         self.buf_flags = Some(flags);
@@ -724,7 +781,7 @@
         // `start_send` is supposed to be called after `poll_ready` returns ready.
         assert!(self.batch_f.is_none());
 
-        let mut flags = self.buf_flags.clone().unwrap();
+        let mut flags = self.buf_flags.unwrap();
         flags = flags.buffer_hint(buffer_hint);
         let write_f = call.call(|c| {
             c.call
diff --git a/src/call/server.rs b/src/call/server.rs
index 0fed656..e762889 100644
--- a/src/call/server.rs
+++ b/src/call/server.rs
@@ -252,7 +252,7 @@
             return execute(self.request, cq, reader, handler, checker);
         }
 
-        let status = RpcStatus::new(RpcStatusCode::INTERNAL, Some("No payload".to_owned()));
+        let status = RpcStatus::with_message(RpcStatusCode::INTERNAL, "No payload".to_owned());
         self.request.call(cq.clone()).abort(&status)
     }
 }
@@ -358,11 +358,20 @@
             }
 
             fn complete(mut self, status: RpcStatus, t: Option<T>) -> $rt {
-                let mut data = t.as_ref().map(|t| {
-                    let mut buf = GrpcSlice::default();
-                    (self.ser)(t, &mut buf);
-                    buf
-                });
+                let mut data = match t {
+                    Some(t) => {
+                        let mut buf = GrpcSlice::default();
+                        if let Err(e) = (self.ser)(&t, &mut buf) {
+                            return $rt {
+                                call: self.call.take().unwrap(),
+                                cq_f: None,
+                                err: Some(e),
+                            };
+                        }
+                        Some(buf)
+                    }
+                    None => None,
+                };
 
                 let write_flags = self.write_flags;
                 let res = self.call.as_mut().unwrap().call(|c| {
@@ -703,9 +712,9 @@
     let request = match de(payload) {
         Ok(f) => f,
         Err(e) => {
-            let status = RpcStatus::new(
+            let status = RpcStatus::with_message(
                 RpcStatusCode::INTERNAL,
-                Some(format!("Failed to deserialize response message: {:?}", e)),
+                format!("Failed to deserialize response message: {:?}", e),
             );
             call.abort(&status);
             return;
@@ -749,9 +758,9 @@
     let request = match de(payload) {
         Ok(t) => t,
         Err(e) => {
-            let status = RpcStatus::new(
+            let status = RpcStatus::with_message(
                 RpcStatusCode::INTERNAL,
-                Some(format!("Failed to deserialize response message: {:?}", e)),
+                format!("Failed to deserialize response message: {:?}", e),
             );
             call.abort(&status);
             return;
@@ -786,7 +795,7 @@
     let ctx = ctx;
     let mut call = ctx.call(cq);
     accept_call!(call);
-    call.abort(&RpcStatus::new(RpcStatusCode::UNIMPLEMENTED, None))
+    call.abort(&RpcStatus::new(RpcStatusCode::UNIMPLEMENTED))
 }
 
 // Helper function to call handler.
diff --git a/src/channel.rs b/src/channel.rs
index c8c67b1..c017f13 100644
--- a/src/channel.rs
+++ b/src/channel.rs
@@ -31,7 +31,7 @@
 
 /// Ref: http://www.grpc.io/docs/guides/wire.html#user-agents
 fn format_user_agent_string(agent: &str) -> CString {
-    let version = "0.8.2";
+    let version = "0.9.1";
     let trimed_agent = agent.trim();
     let val = if trimed_agent.is_empty() {
         format!("grpc-rust/{}", version)
@@ -395,6 +395,7 @@
 
     /// Build `ChannelArgs` from the current configuration.
     #[allow(clippy::useless_conversion)]
+    #[allow(clippy::cmp_owned)]
     pub fn build_args(&self) -> ChannelArgs {
         let args = unsafe { grpc_sys::grpcwrap_channel_args_create(self.options.len()) };
         for (i, (k, v)) in self.options.iter().enumerate() {
diff --git a/src/codec.rs b/src/codec.rs
index e0214b7..35dfb2e 100644
--- a/src/codec.rs
+++ b/src/codec.rs
@@ -5,7 +5,11 @@
 use crate::error::Result;
 
 pub type DeserializeFn<T> = fn(MessageReader) -> Result<T>;
-pub type SerializeFn<T> = fn(&T, &mut GrpcSlice);
+pub type SerializeFn<T> = fn(&T, &mut GrpcSlice) -> Result<()>;
+
+/// According to https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md, grpc uses
+/// a four bytes to describe the length of a message, so it should not exceed u32::MAX.
+pub const MAX_MESSAGE_SIZE: usize = std::u32::MAX as usize;
 
 /// Defines how to serialize and deserialize between the specialized type and byte slice.
 pub struct Marshaller<T> {
@@ -29,18 +33,25 @@
 pub mod pb_codec {
     use protobuf::{CodedInputStream, CodedOutputStream, Message};
 
-    use super::MessageReader;
+    use super::{MessageReader, MAX_MESSAGE_SIZE};
     use crate::buf::GrpcSlice;
-    use crate::error::Result;
+    use crate::error::{Error, Result};
 
     #[inline]
-    pub fn ser<T: Message>(t: &T, buf: &mut GrpcSlice) {
-        let cap = t.compute_size();
-        unsafe {
-            let bytes = buf.realloc(cap as usize);
-            let raw_bytes = &mut *(bytes as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]);
-            let mut s = CodedOutputStream::bytes(raw_bytes);
-            t.write_to_with_cached_sizes(&mut s).unwrap();
+    pub fn ser<T: Message>(t: &T, buf: &mut GrpcSlice) -> Result<()> {
+        let cap = t.compute_size() as usize;
+        // FIXME: This is not a practical fix until stepancheg/rust-protobuf#530 is fixed.
+        if cap <= MAX_MESSAGE_SIZE {
+            unsafe {
+                let bytes = buf.realloc(cap);
+                let raw_bytes = &mut *(bytes as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]);
+                let mut s = CodedOutputStream::bytes(raw_bytes);
+                t.write_to_with_cached_sizes(&mut s).map_err(Into::into)
+            }
+        } else {
+            Err(Error::Codec(
+                format!("message is too large: {} > {}", cap, MAX_MESSAGE_SIZE).into(),
+            ))
         }
     }
 
@@ -57,19 +68,25 @@
 pub mod pr_codec {
     use prost::Message;
 
-    use super::MessageReader;
+    use super::{MessageReader, MAX_MESSAGE_SIZE};
     use crate::buf::GrpcSlice;
-    use crate::error::Result;
+    use crate::error::{Error, Result};
 
     #[inline]
-    pub fn ser<M: Message>(msg: &M, buf: &mut GrpcSlice) {
+    pub fn ser<M: Message>(msg: &M, buf: &mut GrpcSlice) -> Result<()> {
         let size = msg.encoded_len();
-        unsafe {
-            let bytes = buf.realloc(size);
-            let mut b = &mut *(bytes as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]);
-            msg.encode(&mut b)
-                .expect("Writing message to buffer failed");
-            debug_assert!(b.is_empty());
+        if size <= MAX_MESSAGE_SIZE {
+            unsafe {
+                let bytes = buf.realloc(size);
+                let mut b = &mut *(bytes as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]);
+                msg.encode(&mut b)?;
+                debug_assert!(b.is_empty());
+            }
+            Ok(())
+        } else {
+            Err(Error::Codec(
+                format!("message is too large: {} > {}", size, MAX_MESSAGE_SIZE).into(),
+            ))
         }
     }
 
diff --git a/src/error.rs b/src/error.rs
index f12ffa4..2d65eb2 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,8 +5,6 @@
 use crate::call::RpcStatus;
 use crate::grpc_sys::grpc_call_error;
 
-#[cfg(feature = "prost-codec")]
-use prost::DecodeError;
 #[cfg(feature = "protobuf-codec")]
 use protobuf::ProtobufError;
 
@@ -38,10 +36,13 @@
 impl fmt::Display for Error {
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
         match self {
-            Error::RpcFailure(RpcStatus { status, details }) => match details {
-                Some(details) => write!(fmt, "RpcFailure: {} {}", status, details),
-                None => write!(fmt, "RpcFailure: {}", status),
-            },
+            Error::RpcFailure(s) => {
+                if s.message().is_empty() {
+                    write!(fmt, "RpcFailure: {}", s.code())
+                } else {
+                    write!(fmt, "RpcFailure: {} {}", s.code(), s.message())
+                }
+            }
             other_error => write!(fmt, "{:?}", other_error),
         }
     }
@@ -64,8 +65,15 @@
 }
 
 #[cfg(feature = "prost-codec")]
-impl From<DecodeError> for Error {
-    fn from(e: DecodeError) -> Error {
+impl From<prost::DecodeError> for Error {
+    fn from(e: prost::DecodeError) -> Error {
+        Error::Codec(Box::new(e))
+    }
+}
+
+#[cfg(feature = "prost-codec")]
+impl From<prost::EncodeError> for Error {
+    fn from(e: prost::EncodeError) -> Error {
         Error::Codec(Box::new(e))
     }
 }
diff --git a/src/lib.rs b/src/lib.rs
index 2bb0c11..fd147af 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -66,7 +66,7 @@
 pub use crate::codec::pr_codec::{de as pr_de, ser as pr_ser};
 
 pub use crate::auth_context::{AuthContext, AuthProperty, AuthPropertyIter};
-pub use crate::codec::Marshaller;
+pub use crate::codec::{Marshaller, MAX_MESSAGE_SIZE};
 pub use crate::env::{EnvBuilder, Environment};
 pub use crate::error::{Error, Result};
 pub use crate::log_util::redirect_log;
@@ -80,3 +80,24 @@
 pub use crate::server::{
     CheckResult, Server, ServerBuilder, ServerChecker, Service, ServiceBuilder, ShutdownFuture,
 };
+
+/// A shortcut for implementing a service method by returning `UNIMPLEMENTED` status code.
+///
+/// Compiler will provide a default implementations for all methods to invoke this macro, so
+/// you usually won't call it directly. If you really need to, just call it like:
+/// ```ignored
+/// fn method(&self, ctx: grpcio::RpcContext, req: Request, resp: UnarySink<Response>) {
+///     unimplemented_call!(ctx, resp);
+/// }
+/// ```
+#[macro_export]
+macro_rules! unimplemented_call {
+    ($ctx:ident, $sink:ident) => {{
+        let f = async move {
+            let _ = $sink
+                .fail($crate::RpcStatus::new($crate::RpcStatusCode::UNIMPLEMENTED))
+                .await;
+        };
+        $ctx.spawn(f)
+    }};
+}
diff --git a/src/metadata.rs b/src/metadata.rs
index 893f6e2..caaebc8 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -7,6 +7,8 @@
 
 use crate::error::{Error, Result};
 
+const BINARY_ERROR_DETAILS_KEY: &str = "grpc-status-details-bin";
+
 fn normalize_key(key: &str, binary: bool) -> Result<Cow<'_, str>> {
     if key.is_empty() {
         return Err(Error::InvalidMetadata(
@@ -107,6 +109,13 @@
         Ok(self.add_metadata(&key, value))
     }
 
+    /// Set binary error details to support rich error model.
+    ///
+    /// See also https://grpc.io/docs/guides/error/#richer-error-model.
+    pub(crate) fn set_binary_error_details(&mut self, value: &[u8]) -> &mut MetadataBuilder {
+        self.add_metadata(BINARY_ERROR_DETAILS_KEY, value)
+    }
+
     /// Create `Metadata` with configured entries.
     pub fn build(mut self) -> Metadata {
         unsafe {
@@ -214,6 +223,16 @@
             metadata: p,
         })
     }
+
+    /// Search for binary error details.
+    pub(crate) fn search_binary_error_details(&self) -> &[u8] {
+        for (k, v) in self.iter() {
+            if k == BINARY_ERROR_DETAILS_KEY {
+                return v;
+            }
+        }
+        &[]
+    }
 }
 
 impl Clone for Metadata {
diff --git a/src/server.rs b/src/server.rs
index a612b13..f2150a0 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -68,7 +68,7 @@
 /// Given a host and port, creates a string of the form "host:port" or
 /// "[host]:port", depending on whether the host is an IPv6 literal.
 fn join_host_port(host: &str, port: u16) -> String {
-    if host.starts_with("unix:") {
+    if host.starts_with("unix:") | host.starts_with("unix-abstract:") {
         format!("{}\0", host)
     } else if let Ok(ip) = host.parse::<IpAddr>() {
         format!("{}\0", SocketAddr::new(ip, port))
@@ -88,6 +88,8 @@
         pub host: String,
         pub port: u16,
         cred: Option<ServerCredentials>,
+        // Double allocation to get around C call.
+        #[allow(clippy::redundant_allocation)]
         _fetcher: Option<Box<Box<dyn ServerCredentialsFetcher + Send + Sync>>>,
     }
 
@@ -102,6 +104,7 @@
             }
         }
 
+        #[allow(clippy::redundant_allocation)]
         pub fn with_cred(
             host: String,
             port: u16,
diff --git a/src/task/promise.rs b/src/task/promise.rs
index 50add06..2d826d4 100644
--- a/src/task/promise.rs
+++ b/src/task/promise.rs
@@ -57,7 +57,7 @@
             let mut guard = self.inner.lock();
             if succeed {
                 let status = self.ctx.rpc_status();
-                if status.status == RpcStatusCode::OK {
+                if status.code() == RpcStatusCode::OK {
                     guard.set_result(Ok(None))
                 } else {
                     guard.set_result(Err(Error::RpcFailure(status)))
@@ -73,7 +73,7 @@
         let task = {
             let mut guard = self.inner.lock();
             let status = self.ctx.rpc_status();
-            if status.status == RpcStatusCode::OK {
+            if status.code() == RpcStatusCode::OK {
                 guard.set_result(Ok(self.ctx.recv_message()))
             } else {
                 guard.set_result(Err(Error::RpcFailure(status)))