Skip to content

Instantly share code, notes, and snippets.

@pzol
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save pzol/8995990 to your computer and use it in GitHub Desktop.

Select an option

Save pzol/8995990 to your computer and use it in GitHub Desktop.
#[allow(ctypes, dead_code)];
#[feature(globs)];
use std::vec;
pub type UBool = u8;
pub type UProperty = int;
pub type UChar = u16;
pub type UChar32 = i32;
#[deriving(Eq)]
pub enum UErrorCode {
ZERO_ERROR = 0
}
pub static TRUE: u8 = 1u8;
pub static FALSE: u8 = 0u8;
pub static SENTINEL: UChar32 = -1;
pub mod icuio {
use super::*;
#[link(name = "icuio")]
extern "C" {
pub fn u_printf_u_52(patternSpecification: *UChar) -> i32;
}
}
pub mod icuuc {
use super::*;
use std::libc::{c_char };
#[link(name = "icuuc")]
extern "C" {
pub fn u_strToUpper_52(dest: *mut UChar, destCapacity: i32, src: *UChar, srcLength: i32, locale: *c_char, pErrorCode: *mut UErrorCode) -> i32;
pub fn u_strlen_52(s: *UChar) -> i32;
pub fn u_errorName_52(code: UErrorCode) -> *char;
pub fn u_strFromUTF8WithSub_52(dest: *mut UChar, destCapacity: i32, pDestLength: *mut i32, src: *c_char, subChar: UChar32, pNumSubstitutions: *i32, pErrorCode: *mut UErrorCode) -> *UChar;
}
}
// osx only
mod icucore {
use super::*;
use std::libc::{c_char };
#[link(name = "icucore")]
extern "C" {
pub fn u_strFromUTF8WithSub(dest: *mut UChar, destCapacity: i32, pDestLength: *mut i32, src: *c_char, subChar: UChar32, pNumSubstitutions: *i32, pErrorCode: *mut UErrorCode) -> *UChar;
}
}
#[test]
fn test(){
let s = "foobar";
let cap = s.char_len() * 2;
let mut buf: ~[UChar] = std::vec::from_elem(cap, 0u16);
// let pDestLength: *mut i32 = &mut 0;
// let pNumSubstitutions: *mut i32 = &mut 0;
// let pErrorCode: *mut UErrorCode = &mut ZERO_ERROR;
let mut pDestLength = 0;
let mut pNumSubstitutions = 0;
let mut pErrorCode = ZERO_ERROR;
unsafe {
let r = icucore::u_strFromUTF8WithSub(buf.as_mut_ptr(), buf.capacity() as i32,
&mut pDestLength, s.as_bytes().as_ptr() as *i8,
s.len() as i32, &SENTINEL, &mut pErrorCode);
println!("{} {} {:?}", pDestLength, pNumSubstitutions, pErrorCode);
println!("buf {:?}", buf);
}
}
}
➜ ustring.rs git:(master) ✗ valgrind ./ffi
==57628== Memcheck, a memory error detector
==57628== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==57628== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==57628== Command: ./ffi
==57628==
==57628== WARNING: Support on MacOS 10.8/10.9 is experimental and mostly broken.
==57628== WARNING: Expect incorrect results, assertions and crashes.
==57628== WARNING: In particular, Memcheck on 32-bit programs will fail to
==57628== WARNING: detect any errors associated with heap-allocated data.
==57628==
--57628-- ./ffi:
--57628-- dSYM directory is missing; consider using --dsymutil=yes
==57628== Conditional jump or move depends on uninitialised value(s)
==57628== at 0x7FFF5FC20817: strchr (in /usr/lib/dyld)
==57628== by 0x7FFF5FC1E98E: realpath$DARWIN_EXTSN (in /usr/lib/dyld)
==57628== by 0x7FFF5FC10D71: ImageLoaderMachO::getRPaths(ImageLoader::LinkContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >&) const (in /usr/lib/dyld)
==57628== by 0x7FFF5FC0DC00: ImageLoader::recursiveLoadLibraries(ImageLoader::LinkContext const&, bool, ImageLoader::RPathChain const&) (in /usr/lib/dyld)
==57628== by 0x7FFF5FC0D9D3: ImageLoader::link(ImageLoader::LinkContext const&, bool, bool, bool, ImageLoader::RPathChain const&) (in /usr/lib/dyld)
==57628== by 0x7FFF5FC04763: dyld::link(ImageLoader*, bool, bool, ImageLoader::RPathChain const&) (in /usr/lib/dyld)
==57628== by 0x7FFF5FC053AB: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==57628== by 0x7FFF5FC0127A: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==57628== by 0x7FFF5FC0105D: _dyld_start (in /usr/lib/dyld)
==57628==
==57628== Conditional jump or move depends on uninitialised value(s)
==57628== at 0x7FFF5FC20817: strchr (in /usr/lib/dyld)
==57628== by 0x7FFF5FC1E98E: realpath$DARWIN_EXTSN (in /usr/lib/dyld)
==57628== by 0x7FFF5FC10D71: ImageLoaderMachO::getRPaths(ImageLoader::LinkContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >&) const (in /usr/lib/dyld)
==57628== by 0x7FFF5FC0B6B9: dlopen (in /usr/lib/dyld)
==57628== by 0x1DC37EE: dlopen (in /usr/lib/system/libdyld.dylib)
==57628== by 0x20A86FD: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==57628== by 0x20A8414: map_images (in /usr/lib/libobjc.A.dylib)
==57628== by 0x7FFF5FC04533: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==57628== by 0x7FFF5FC0427B: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==57628== by 0x1DC38F0: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==57628== by 0x20A804C: _objc_init (in /usr/lib/libobjc.A.dylib)
==57628== by 0x1D8DFB6: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==57628==
==57628== Warning: noted but unhandled ioctl 0x20006601 with no size/direction hints
==57628== This could cause spurious value errors to appear.
==57628== See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper.
==57628== Warning: noted but unhandled ioctl 0x20006601 with no size/direction hints
==57628== This could cause spurious value errors to appear.
==57628== See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper.
==57628== Warning: noted but unhandled ioctl 0x20006601 with no size/direction hints
==57628== This could cause spurious value errors to appear.
==57628== See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper.
==57628== Warning: client switching stacks? SP change: 0x2508ad0 --> 0xb0100020
==57628== to suppress, use: --max-stackframe=2915005776 or greater
==57628== Warning: client switching stacks? SP change: 0xb00f7f50 --> 0x2508ad0
==57628== to suppress, use: --max-stackframe=2914972800 or greater
==57628== Warning: client switching stacks? SP change: 0x2508ad0 --> 0xb00f7f50
==57628== to suppress, use: --max-stackframe=2914972800 or greater
==57628== further instances of this message will not be shown.
running 1 test
test test ... ==57628== Thread 2:
==57628== Conditional jump or move depends on uninitialised value(s)
==57628== at 0x22167F: u_strFromUTF8WithSub (in /usr/lib/libicucore.A.dylib)
==57628== by 0x10000173C: test::ha8c9aa1956070247aW::v0.0 (in ./ffi)
==57628== by 0x1000E0B5F: task::__extensions__::build_start_wrapper::anon::anon::expr_fn::aF (in ./ffi)
==57628== by 0x1000BBB47: rt::task::__extensions__::run::anon::expr_fn::a1 (in ./ffi)
==57628== by 0x1000C324B: rust_try (in ./ffi)
==57628== by 0x1000E087D: task::__extensions__::build_start_wrapper::anon::expr_fn::ax (in ./ffi)
==57628==
0 0 ZERO_ERROR
==57628== Conditional jump or move depends on uninitialised value(s)
==57628== at 0x1000B6A41: repr::ReprVisitor::visit_ptr_inner::he19a8cc4f04659cbukao::v0.10.pre (in ./ffi)
==57628== by 0x1000B8A94: repr::ReprVisitor::write_vec_range::h39033b00df0aff48ukaq::v0.10.pre (in ./ffi)
==57628== by 0x100014785: repr::TyVisitor$ReprVisitor::visit_evec_uniq::haa963cdd4bc7bf43bgaC::v0.10.pre (in ./ffi)
==57628== by 0x100006B52: reflect::TyVisitor$MovePtrAdaptor::visit_evec_uniq::h7fb0f99119c0bfbf8pae::v0.0 (in ./ffi)
==57628== by 0x10000DC18: _$UP$$x5bu16$x5d::glue_visit::h69313895f389de97aT (in ./ffi)
==57628== by 0x10000D932: repr::write_repr::h01fa5450a1e57257aN::v0.0 (in ./ffi)
==57628== by 0x10000D77E: fmt::Poly$T::fmt::h8969dae8fade18022RaM::v0.0 (in ./ffi)
==57628== by 0x10000D6C8: fmt::secret_poly::h0ddc20e46ea0ab7daL::v0.0 (in ./ffi)
==57628== by 0x1000AD521: fmt::Formatter::run::hb697482cf65dfc4fTGaA::v0.10.pre (in ./ffi)
==57628== by 0x10006E340: fmt::writeln::h7dde24ed1e4c06a1aw::v0.10.pre (in ./ffi)
==57628== by 0x10006E21F: io::stdio::println_args::anon::expr_fn::aU (in ./ffi)
==57628== by 0x10006B0B4: io::stdio::with_task_stdout::h7855d72aec98a910av::v0.10.pre (in ./ffi)
==57628==
buf ~[0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16]
==57628== Conditional jump or move depends on uninitialised value(s)
==57628== at 0x1000512EA: task::TaskBuilder::spawn::h6bd026654befa2aeKEat::v0.10.pre (in ./ffi)
==57628== by 0x100207365: test::run_test::run_test_inner::anon::expr_fn::a4 (in ./ffi)
==57628== by 0x1000E0B5F: task::__extensions__::build_start_wrapper::anon::anon::expr_fn::aF (in ./ffi)
==57628== by 0x1000BBB47: rt::task::__extensions__::run::anon::expr_fn::a1 (in ./ffi)
==57628== by 0x1000C324B: rust_try (in ./ffi)
==57628== by 0x1000E087D: task::__extensions__::build_start_wrapper::anon::expr_fn::ax (in ./ffi)
==57628==
==57628== Conditional jump or move depends on uninitialised value(s)
==57628== at 0x1000512EA: task::TaskBuilder::spawn::h6bd026654befa2aeKEat::v0.10.pre (in ./ffi)
==57628== by 0x1000518B8: task::spawn::h861f8595da519b2daN::v0.10.pre (in ./ffi)
==57628== by 0x10020702A: test::run_test::run_test_inner::hcd5f34b98529d896aP::v0.10.pre (in ./ffi)
==57628== by 0x1001FD381: test::run_test::ha765ea71cb227453ab::v0.10.pre (in ./ffi)
==57628== by 0x1001E7F7F: test::run_tests_console::hd58303f2a68ae248ak::v0.10.pre (in ./ffi)
==57628== by 0x1001E43EA: test::test_main::h4ae0616292efc078aa::v0.10.pre (in ./ffi)
==57628== by 0x1001EE73F: test::test_main_static::h4d16c74672900b80al::v0.10.pre (in ./ffi)
==57628== by 0x10000DE07: __test::main::h852d6e7940eac85aa1::v0.0 (in ./ffi)
==57628== by 0x1000E0B5F: task::__extensions__::build_start_wrapper::anon::anon::expr_fn::aF (in ./ffi)
==57628== by 0x1000BBB47: rt::task::__extensions__::run::anon::expr_fn::a1 (in ./ffi)
==57628== by 0x1000C324B: rust_try (in ./ffi)
==57628== by 0x1000E087D: task::__extensions__::build_start_wrapper::anon::expr_fn::ax (in ./ffi)
==57628==
ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
==57628==
==57628== HEAP SUMMARY:
==57628== in use at exit: 25,254 bytes in 367 blocks
==57628== total heap usage: 2,148 allocs, 1,781 frees, 811,005 bytes allocated
==57628==
==57628== LEAK SUMMARY:
==57628== definitely lost: 0 bytes in 0 blocks
==57628== indirectly lost: 0 bytes in 0 blocks
==57628== possibly lost: 0 bytes in 0 blocks
==57628== still reachable: 8 bytes in 1 blocks
==57628== suppressed: 25,246 bytes in 366 blocks
==57628== Rerun with --leak-check=full to see details of leaked memory
==57628==
==57628== For counts of detected and suppressed errors, rerun with: -v
==57628== Use --track-origins=yes to see where uninitialised values come from
==57628== ERROR SUMMARY: 25 errors from 6 contexts (suppressed: 137 from 34)
@pzol

pzol commented Feb 14, 2014

Copy link
Copy Markdown
Author

Output

running 1 test
12u
0 0 ZERO_ERROR
~[0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]
test test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment