forked from IntersectMBO/cardano-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return errors as string from validate_cbor
So that we don't panic in the function and we keep everything pure. Now we can properly hook into Hedgehog annotation system to provide better failures.
- Loading branch information
Showing
8 changed files
with
51 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
all: | ||
cargo build --release | ||
mv target/release/libcddl.a /usr/local/lib/libcddl.a | ||
cbindgen --crate cddl > cbits/libcddl.h | ||
@cat cbits/libcddl.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
{-# LANGUAGE ForeignFunctionInterface #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
|
||
module Codec.CBOR.Schema where | ||
|
||
import Prelude | ||
|
||
import Control.Exception (SomeException(..), Exception(..), catch) | ||
import Data.Text (Text) | ||
import qualified Data.Text as Text | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Base16 as Base16 | ||
import qualified Data.ByteString as BS | ||
import Foreign.C.String (CString, withCStringLen) | ||
import qualified Data.ByteString.Char8 as B8 | ||
import Foreign.Marshal.Alloc (free) | ||
import Foreign.C.String (CString, peekCString, withCStringLen) | ||
import System.IO.Unsafe (unsafePerformIO) | ||
|
||
data ValidationError = ValidationError | ||
{ cbor :: String -- | The erroneous CBOR as a base16-encoded text string | ||
, hint :: String -- | An (hopefully) helpful error message | ||
} deriving (Eq, Show) | ||
|
||
validate | ||
:: Text | ||
-- ^ A CDDL specification | ||
-> ByteString | ||
-- ^ Some CBOR value | ||
-> Either String () | ||
validate cddl cbor = unsafePerformIO $ do | ||
withCStringLen (Text.unpack cddl) $ \(cddl_ptr, cddl_len) -> | ||
BS.useAsCStringLen cbor $ \(cbor_ptr, cbor_len) -> | ||
( Right <$> validate_cbor | ||
cddl_ptr (fromIntegral cddl_len) | ||
cbor_ptr (fromIntegral cbor_len) | ||
) `catch` \(SomeException e) -> return (Left (displayException e)) | ||
-> Either ValidationError () | ||
validate cddl cbor = | ||
case result of | ||
[] -> | ||
Right () | ||
hint -> | ||
Left $ ValidationError | ||
{ hint | ||
, cbor = B8.unpack (Base16.encode cbor) | ||
} | ||
where | ||
result = unsafePerformIO $ do | ||
withCStringLen (Text.unpack cddl) $ \(cddl_ptr, cddl_len) -> | ||
BS.useAsCStringLen cbor $ \(cbor_ptr, cbor_len) -> do | ||
cstr <- validate_cbor | ||
cddl_ptr (fromIntegral cddl_len) | ||
cbor_ptr (fromIntegral cbor_len) | ||
peekCString cstr <* free cstr | ||
|
||
foreign import ccall "cbits/libcddl.h validate_cbor" | ||
validate_cbor | ||
:: CString | ||
-> Word | ||
-> CString | ||
-> Word | ||
-> IO () | ||
-> IO CString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
use cddl_cat::validate_cbor_bytes; | ||
use std::{slice, str}; | ||
use std::{ffi::CString, slice, str}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn validate_cbor( | ||
cddl_ptr: *const u8, | ||
cddl_len: usize, | ||
cbor_ptr: *const u8, | ||
cbor_len: usize, | ||
) -> () { | ||
) -> *mut i8 { | ||
let cddl = unsafe { slice::from_raw_parts(cddl_ptr, cddl_len) }; | ||
let cbor = unsafe { slice::from_raw_parts(cbor_ptr, cbor_len) }; | ||
match validate_cbor_bytes("rule", str::from_utf8(cddl).unwrap(), cbor) { | ||
Ok(_) => (), | ||
Err(e) => panic!("{e:#?}"), | ||
Ok(_) => CString::new("").unwrap(), | ||
Err(e) => CString::new(format!("{e:#?}")).unwrap(), | ||
} | ||
.into_raw() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters