Merge 541058a4b428e5e6cc0da645fc56c804d43c5d1b on remote branch

Change-Id: I531d01a2de4d0cb31e5bb06b80117880306b085a
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 2587a95..4f6a222 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
 {
   "git": {
-    "sha1": "7a48e0bb843e702832c1c3ac024c468dedf0023c"
+    "sha1": "61a60923255fe618589bf03ece46fff097b8cb43"
   }
 }
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 2b542c6..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,19 +22,28 @@
     - 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: 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: 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: sudo apt install -y protobuf-compiler
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
+    - 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;
@@ -43,18 +53,27 @@
     - 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: which cargo && cargo version && clang --version && openssl version
     - run: cargo xtask submodule
-    - run: cargo test --features "openssl-vendored" --all
+    - 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
@@ -62,7 +81,7 @@
     steps:
     - uses: actions/checkout@v2
     - run: rustup default nightly
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
+    - 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
@@ -76,7 +95,7 @@
     runs-on: macos-latest
     steps:
     - uses: actions/checkout@v2
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
+    - 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"
@@ -90,7 +109,7 @@
     steps:
     - uses: actions/checkout@v2
     - run: brew update && brew upgrade openssl@1.1
-    - run: which go && go version && which cargo && cargo version && clang --version && openssl version
+    - run: which cargo && cargo version && clang --version && openssl version
     - run: cargo xtask submodule
     - run: OPENSSL_ROOT_DIR="/usr/local/opt/openssl@1.1/" cargo test --features "openssl" --all
     - run: cargo test --features "openssl-vendored" --all
@@ -100,6 +119,7 @@
     runs-on: windows-latest
     env:
       LIBCLANG_PATH: 'C:\Program Files\LLVM\bin'
+      RUSTFLAGS: ""
     steps:
     - uses: actions/checkout@v2
     - run: choco install -y llvm
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 4ac52fb..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:
-      - cargo xtask submodule
-      - export GRPC_VERSION=1.38.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 cargo xtask bindgen && git diff --exit-code HEAD;
-        fi
-      - cargo xtask bindgen
-      - 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:
-      - cargo xtask submodule
-      - export GRPC_VERSION=1.38.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 94c112b..f532453 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 --run --device --features=protobuf,protobuf-codec.
 // 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: [
@@ -37,43 +39,3 @@
         "libprotobuf",
     ],
 }
-
-// dependent_library ["feature_list"]
-//   autocfg-1.0.1
-//   boringssl-src-0.3.0+688fc5c
-//   cc-1.0.68
-//   cfg-if-1.0.0
-//   cmake-0.1.45
-//   futures-0.3.15 "alloc,async-await,default,executor,futures-executor,std"
-//   futures-channel-0.3.15 "alloc,futures-sink,sink,std"
-//   futures-core-0.3.15 "alloc,std"
-//   futures-executor-0.3.15 "std"
-//   futures-io-0.3.15 "std"
-//   futures-macro-0.3.15
-//   futures-sink-0.3.15 "alloc,std"
-//   futures-task-0.3.15 "alloc,std"
-//   futures-util-0.3.15 "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.9.0+1.38.0
-//   instant-0.1.9
-//   libc-0.2.97 "default,std"
-//   libz-sys-1.1.3 "libc,static"
-//   lock_api-0.4.4
-//   log-0.4.14
-//   memchr-2.4.0 "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.27 "default,proc-macro"
-//   protobuf-2.24.1
-//   quote-1.0.9 "default,proc-macro"
-//   same-file-1.0.6
-//   scopeguard-1.1.0
-//   slab-0.4.3 "default,std"
-//   smallvec-1.6.1
-//   syn-1.0.73 "clone-impls,default,derive,full,parsing,printing,proc-macro,quote"
-//   unicode-xid-0.2.2 "default"
-//   walkdir-2.3.2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 57a34bf..aa28773 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+# 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)
diff --git a/Cargo.toml b/Cargo.toml
index a151b8a..fd9e71f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,7 +13,7 @@
 [package]
 edition = "2018"
 name = "grpcio"
-version = "0.9.0"
+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."
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 6fecf28..9755697 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
 [package]
 name = "grpcio"
-version = "0.9.0"
+version = "0.9.1"
 edition = "2018"
 authors = ["The TiKV Project Developers"]
 license = "Apache-2.0"
diff --git a/METADATA b/METADATA
index ad40b66..8384cab 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@
   }
   url {
     type: ARCHIVE
-    value: "https://static.crates.io/crates/grpcio/grpcio-0.9.0.crate"
+    value: "https://static.crates.io/crates/grpcio/grpcio-0.9.1.crate"
   }
-  version: "0.9.0"
+  version: "0.9.1"
   license_type: NOTICE
   last_upgrade_date {
     year: 2021
-    month: 6
-    day: 21
+    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/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 3674623..25174ea 100644
--- a/src/call/mod.rs
+++ b/src/call/mod.rs
@@ -717,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;
@@ -730,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);
@@ -781,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 58c63bf..e762889 100644
--- a/src/call/server.rs
+++ b/src/call/server.rs
@@ -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| {
diff --git a/src/channel.rs b/src/channel.rs
index 3b2e10c..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.9.0";
+    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 7c180a9..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;
 
@@ -67,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 4727331..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;
diff --git a/src/server.rs b/src/server.rs
index c8e28df..f2150a0 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -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,