From dc1bd8966a9f813de27dc2471267e70e8f0d7b16 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 8 Sep 2018 10:56:46 -0700 Subject: [PATCH 1/2] internal/abi/cwa: support the time_now call --- internal/abi/cwa/core.go | 6 ++++++ internal/abi/cwa/time.go | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 internal/abi/cwa/time.go diff --git a/internal/abi/cwa/core.go b/internal/abi/cwa/core.go index fef6e5b..2eb99bb 100644 --- a/internal/abi/cwa/core.go +++ b/internal/abi/cwa/core.go @@ -222,6 +222,12 @@ func (p *Process) ResolveFunc(module, field string) exec.FunctionImport { return 0 } + case "time_now": + return func(vm *exec.VirtualMachine) int64 { + p.SetVM(vm) + + return p.timeNow() + } default: log.Panicf("unknown import %s::%s", module, field) } diff --git a/internal/abi/cwa/time.go b/internal/abi/cwa/time.go new file mode 100644 index 0000000..87974b5 --- /dev/null +++ b/internal/abi/cwa/time.go @@ -0,0 +1,7 @@ +package cwa + +import "time" + +func (p *Process) timeNow() int64 { + return time.Now().UTC().Unix() +} From 16c98ee70dccf72246844969b1983d2a0b529cf4 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 8 Sep 2018 11:17:28 -0700 Subject: [PATCH 2/2] cwa: use time_now call, test it --- cwa/libcwa/Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++ cwa/libcwa/Cargo.toml | 1 + cwa/libcwa/src/lib.rs | 16 +++++++++ cwa/tests/Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++ cwa/tests/Cargo.toml | 1 + cwa/tests/src/main.rs | 12 +++++++ cwa/tests/src/time.rs | 19 +++++++++++ 7 files changed, 199 insertions(+) create mode 100644 cwa/tests/src/time.rs diff --git a/cwa/libcwa/Cargo.lock b/cwa/libcwa/Cargo.lock index 0dad63c..3ad9001 100644 --- a/cwa/libcwa/Cargo.lock +++ b/cwa/libcwa/Cargo.lock @@ -1,4 +1,79 @@ +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.43" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libcwa" version = "0.1.0" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +[metadata] +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" +"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" +"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" +"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cwa/libcwa/Cargo.toml b/cwa/libcwa/Cargo.toml index d9d0a70..320fd3d 100644 --- a/cwa/libcwa/Cargo.toml +++ b/cwa/libcwa/Cargo.toml @@ -9,3 +9,4 @@ repository = "https://github.com/Xe/olin" homepage = "https://github.com/Xe/olin" [dependencies] +chrono = "0.4" diff --git a/cwa/libcwa/src/lib.rs b/cwa/libcwa/src/lib.rs index 3a726b0..82da4cf 100644 --- a/cwa/libcwa/src/lib.rs +++ b/cwa/libcwa/src/lib.rs @@ -24,6 +24,8 @@ pub mod sys { pub fn resource_write(id: i32, data_ptr: *const u8, data_len: usize) -> i32; pub fn resource_close(id: i32); pub fn resource_flush(id: i32) -> i32; + + pub fn time_now() -> i64; } } @@ -267,3 +269,17 @@ impl Write for Resource { .unwrap_err()); } } + +pub mod time { + extern crate chrono; + + use time::chrono::TimeZone; + + pub fn now() -> chrono::DateTime { + return chrono::Utc.timestamp(ts(), 0); + } + + pub fn ts() -> i64 { + unsafe { ::sys::time_now() } + } +} diff --git a/cwa/tests/Cargo.lock b/cwa/tests/Cargo.lock index c4933df..e722032 100644 --- a/cwa/tests/Cargo.lock +++ b/cwa/tests/Cargo.lock @@ -1,7 +1,18 @@ +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cwa-tests" version = "0.1.0" dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "libcwa 0.1.0", ] @@ -11,9 +22,73 @@ name = "httparse" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libc" +version = "0.2.43" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libcwa" version = "0.1.0" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540" +"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" +"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" +"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cwa/tests/Cargo.toml b/cwa/tests/Cargo.toml index f49017a..cc27d63 100644 --- a/cwa/tests/Cargo.toml +++ b/cwa/tests/Cargo.toml @@ -6,3 +6,4 @@ authors = ["Christine Dodrill "] [dependencies] libcwa = { path = "../libcwa" } httparse = "1.3.2" +chrono = "0.4" diff --git a/cwa/tests/src/main.rs b/cwa/tests/src/main.rs index 9fa28fa..6514c30 100644 --- a/cwa/tests/src/main.rs +++ b/cwa/tests/src/main.rs @@ -8,6 +8,7 @@ extern crate libcwa; use libcwa::*; mod http; +mod time; #[no_mangle] pub extern "C" fn cwa_main() -> i32 { @@ -29,6 +30,17 @@ pub extern "C" fn cwa_main() -> i32 { Err(e) => result = e as i32, } + if result != 0 { + return result; + } + + log::info("time test"); + let result: i32; + match time::test() { + Ok(()) => result = 0, + Err(e) => result = e as i32, + } + result } diff --git a/cwa/tests/src/time.rs b/cwa/tests/src/time.rs new file mode 100644 index 0000000..c9e5dc6 --- /dev/null +++ b/cwa/tests/src/time.rs @@ -0,0 +1,19 @@ +extern crate chrono; +extern crate libcwa; + +use time::chrono::TimeZone; +use std::io; +use libcwa::*; + +pub fn test() -> Result<(), i32> { + let now: i64 = time::ts(); + let dt = chrono::Utc.timestamp(now, 0); + + log::info(&format!("ts: {}, dt: {}", now, dt.to_rfc3339())); + + let now = time::now(); + log::info(&format!("time::now(): {}", now.to_rfc3339())); + + log::info("time test passed"); + Ok(()) +}