Jelajahi Sumber

String decoding for substrate

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 5 tahun lalu
induk
melakukan
66e1c98cb7
6 mengubah file dengan 138 tambahan dan 5 penghapusan
  1. 27 1
      src/emit/substrate.rs
  2. 1 1
      src/link.rs
  3. TEMPAT SAMPAH
      stdlib/stdlib.bc
  4. 43 1
      stdlib/stdlib.c
  5. 1 1
      tests/substrate.rs
  6. 66 1
      tests/substrate_strings/mod.rs

+ 27 - 1
src/emit/substrate.rs

@@ -475,7 +475,33 @@ impl SubstrateTarget {
 
 
                 to.into()
                 to.into()
             }
             }
-            resolver::Type::String | resolver::Type::DynamicBytes => unimplemented!(),
+            resolver::Type::String | resolver::Type::DynamicBytes => {
+                let from = contract.builder.build_alloca(
+                    contract.context.i8_type().ptr_type(AddressSpace::Generic),
+                    "from",
+                );
+
+                contract.builder.build_store(from, *data);
+
+                let v = contract
+                    .builder
+                    .build_call(
+                        contract.module.get_function("scale_decode_string").unwrap(),
+                        &[from.into()],
+                        "",
+                    )
+                    .try_as_basic_value()
+                    .left()
+                    .unwrap()
+                    .into_pointer_value();
+
+                *data = contract
+                    .builder
+                    .build_load(from, "data")
+                    .into_pointer_value();
+
+                v.into()
+            }
             resolver::Type::Undef => unreachable!(),
             resolver::Type::Undef => unreachable!(),
             resolver::Type::StorageRef(_) => unreachable!(),
             resolver::Type::StorageRef(_) => unreachable!(),
             resolver::Type::Ref(ty) => self.decode_ty(contract, function, ty, to, data),
             resolver::Type::Ref(ty) => self.decode_ty(contract, function, ty, to, data),

+ 1 - 1
src/link.rs

@@ -90,7 +90,7 @@ pub fn link(input: &[u8], target: &Target) -> Vec<u8> {
             Target::Substrate => imports.push(ImportEntry::new(
             Target::Substrate => imports.push(ImportEntry::new(
                 "env".into(),
                 "env".into(),
                 "memory".into(),
                 "memory".into(),
-                elements::External::Memory(elements::MemoryType::new(2, Some(2))),
+                elements::External::Memory(elements::MemoryType::new(16, Some(16))),
             )),
             )),
             Target::Sabre => exports.push(ExportEntry::new("memory".into(), Internal::Memory(0))),
             Target::Sabre => exports.push(ExportEntry::new("memory".into(), Internal::Memory(0))),
         }
         }

TEMPAT SAMPAH
stdlib/stdlib.bc


+ 43 - 1
stdlib/stdlib.c

@@ -466,7 +466,7 @@ uint8_t *compact_encode_u32(uint8_t *dest, uint32_t val)
 	{
 	{
 		*dest++ = val << 2;
 		*dest++ = val << 2;
 	}
 	}
-	else if (val < 4000)
+	else if (val < 0x4000)
 	{
 	{
 		*((uint16_t *)dest) = (val << 2) | 1;
 		*((uint16_t *)dest) = (val << 2) | 1;
 		dest += 2;
 		dest += 2;
@@ -498,4 +498,46 @@ uint8_t *scale_encode_string(uint8_t *dest, struct vector *s)
 	}
 	}
 
 
 	return data_dst;
 	return data_dst;
+}
+
+struct vector *scale_decode_string(uint8_t **from)
+{
+	uint8_t *src = *from;
+	uint8_t upperbits = *src >> 2;
+	uint32_t size_array;
+
+	switch (*src & 0x03)
+	{
+	case 0:
+		size_array = upperbits;
+		src += 1;
+		break;
+	case 1:
+		size_array = *((uint16_t *)src) >> 2;
+		src += 2;
+		break;
+	case 2:
+		size_array = *((uint32_t *)src) >> 2;
+		src += 4;
+		break;
+	default:
+		// sizes of 2**30 (1GB) or larger are not allowed
+		__builtin_unreachable();
+	}
+
+	struct vector *v = __malloc(sizeof(*v) + size_array);
+
+	v->len = size_array;
+	v->size = size_array;
+
+	uint8_t *data = v->data;
+
+	while (size_array--)
+	{
+		*data++ = *src++;
+	}
+
+	*from = src;
+
+	return v;
 }
 }

+ 1 - 1
tests/substrate.rs

@@ -56,7 +56,7 @@ impl ContractStorage {
     #[allow(clippy::new_without_default)]
     #[allow(clippy::new_without_default)]
     pub fn new() -> Self {
     pub fn new() -> Self {
         ContractStorage {
         ContractStorage {
-            memory: MemoryInstance::alloc(Pages(2), Some(Pages(2))).unwrap(),
+            memory: MemoryInstance::alloc(Pages(16), Some(Pages(16))).unwrap(),
             scratch: Vec::new(),
             scratch: Vec::new(),
             store: HashMap::new(),
             store: HashMap::new(),
         }
         }

+ 66 - 1
tests/substrate_strings/mod.rs

@@ -1,5 +1,6 @@
-use parity_scale_codec::{Decode, Encode};
+use parity_scale_codec::Encode;
 use parity_scale_codec_derive::{Decode, Encode};
 use parity_scale_codec_derive::{Decode, Encode};
+use rand::Rng;
 
 
 use super::{build_solidity, first_error, no_errors};
 use super::{build_solidity, first_error, no_errors};
 use solang::{parse_and_resolve, Target};
 use solang::{parse_and_resolve, Target};
@@ -277,3 +278,67 @@ fn string_abi_encode() {
 
 
     assert_eq!(store.scratch, Ret3([ 120, 3, -127, 64], "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.".to_string(), true).encode());
     assert_eq!(store.scratch, Ret3([ 120, 3, -127, 64], "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.".to_string(), true).encode());
 }
 }
+
+#[test]
+fn string_abi_decode() {
+    #[derive(Debug, PartialEq, Encode, Decode)]
+    struct Val(String);
+
+    #[derive(Debug, PartialEq, Encode, Decode)]
+    struct ValB(Vec<u8>);
+
+    // we should try lengths: 0 to 63, 64 to 0x800
+    let (runtime, mut store) = build_solidity(
+        r##"
+        contract foo {
+            function test(string s) public returns (string){
+                return " " + s + " ";
+            }
+        }"##,
+    );
+
+    let moby_dick_first_para = "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.";
+
+    runtime.function(&mut store, "test", Val("foobar".to_string()).encode());
+    assert_eq!(store.scratch, Val(" foobar ".to_string()).encode());
+
+    runtime.function(
+        &mut store,
+        "test",
+        Val(moby_dick_first_para.to_string()).encode(),
+    );
+
+    assert_eq!(
+        store.scratch,
+        Val(format!(" {} ", moby_dick_first_para)).encode()
+    );
+
+    let mut rng = rand::thread_rng();
+
+    for len in 0x4000 - 10..0x4000 + 10 {
+        let mut s = Vec::new();
+
+        s.resize(len, 0);
+
+        rng.fill(&mut s[..]);
+
+        let (runtime, mut store) = build_solidity(
+            r##"
+            contract foo {
+                function test(bytes s) public returns (bytes){
+                    return hex"fe" + s;
+                }
+            }"##,
+        );
+
+        let arg = ValB(s.clone()).encode();
+
+        runtime.function(&mut store, "test", arg.clone());
+
+        s.insert(0, 0xfeu8);
+
+        let ret = ValB(s).encode();
+
+        assert_eq!(store.scratch, ret);
+    }
+}