2011-04-06 17:40:01 +00:00
|
|
|
# This file is part of Metasm, the Ruby assembly manipulation suite
|
|
|
|
# Copyright (C) 2006-2009 Yoann GUILLOT
|
|
|
|
#
|
|
|
|
# Licence is LGPL, see LICENCE in the top-level directory
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
require 'metasm'
|
|
|
|
|
|
|
|
class TestDynldr < Test::Unit::TestCase
|
2013-08-30 21:28:33 +00:00
|
|
|
def cp
|
|
|
|
@cp ||= Metasm::Ia32.new.new_cparser
|
|
|
|
end
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def test_parse_c
|
|
|
|
assert_nothing_raised {
|
|
|
|
cp.parse("static int i_1=42; __stdcall __int64 foo_1(const char*);")
|
|
|
|
}
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_kind_of(Metasm::C::Function, cp.toplevel.symbol['foo_1'].type)
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_raise(Metasm::ParseError) { cp.parse("static extern int fu;") }
|
|
|
|
cp.readtok until cp.eos?
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_nothing_raised { cp.parse("const volatile volatile const char const * const * blarg_0;") }
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_nothing_raised { cp.parse("void *ptr = &ptr;") }
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_raise(Metasm::ParseError) { cp.parse("void *ptr = ptr;") }
|
|
|
|
cp.readtok until cp.eos?
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_nothing_raised { cp.parse("struct { int sz; } bla = { .sz = sizeof(bla) };") }
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_raise(Metasm::ParseError) { cp.parse("signed unsigned int fu;") }
|
|
|
|
cp.readtok until cp.eos?
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_raise(Metasm::ParseError) { cp.parse("long long long fu;") }
|
|
|
|
cp.readtok until cp.eos?
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_raise(Metasm::ParseError) { cp.parse <<EOS }
|
2011-04-06 17:40:01 +00:00
|
|
|
asm <<EOA
|
|
|
|
foo
|
|
|
|
EOA
|
|
|
|
EOS
|
2013-08-30 21:28:33 +00:00
|
|
|
cp.readtok until cp.eos?
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_nothing_raised { cp.parse <<EOS }
|
2011-04-06 17:40:01 +00:00
|
|
|
asm <<-EOA
|
|
|
|
foo
|
|
|
|
EOA
|
|
|
|
EOS
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2011-04-06 17:40:01 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def test_struct
|
|
|
|
cp.parse <<EOS
|
2011-04-06 17:40:01 +00:00
|
|
|
struct foo_2 {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int32 a;
|
|
|
|
__int8 b;
|
|
|
|
__int32 c;
|
|
|
|
__int8 d;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct foo_3 {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int8 a;
|
|
|
|
__int8 b;
|
|
|
|
__int16 c;
|
|
|
|
__int32 d;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct foo_4 {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int32 a;
|
|
|
|
__int8 b;
|
|
|
|
__int32 c;
|
|
|
|
__int8 d;
|
2011-04-06 17:40:01 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
union foo_5 {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int64;
|
|
|
|
struct {
|
|
|
|
__int8;
|
|
|
|
__int8*;
|
|
|
|
};
|
|
|
|
struct {
|
|
|
|
__int16[8];
|
|
|
|
};
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
EOS
|
2013-08-30 21:28:33 +00:00
|
|
|
assert_equal(16, cp.sizeof(cp.toplevel.struct['foo_2']))
|
|
|
|
assert_equal(8, cp.toplevel.struct['foo_2'].offsetof(cp, 'c'))
|
|
|
|
assert_equal(8, cp.sizeof(cp.toplevel.struct['foo_3']))
|
|
|
|
assert_equal(10, cp.sizeof(cp.toplevel.struct['foo_4']))
|
|
|
|
assert_equal(4, cp.toplevel.struct['foo_4'].offsetof(cp, 'b'))
|
|
|
|
assert_equal(5, cp.toplevel.struct['foo_4'].offsetof(cp, 'c'))
|
|
|
|
assert_equal(16, cp.sizeof(cp.toplevel.struct['foo_5']))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bitfields
|
|
|
|
cp.parse <<EOS
|
2011-04-06 17:40:01 +00:00
|
|
|
struct foo_bits {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int32 f0:4;
|
|
|
|
__int32 :0;
|
|
|
|
__int32 f1:4;
|
|
|
|
__int32 f2:4;
|
|
|
|
__int8 f3;
|
|
|
|
__int32 f4:4;
|
|
|
|
__int32 f5:30;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct foo_n_bits {
|
2013-08-30 21:28:33 +00:00
|
|
|
struct foo_bits;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct foo2_bits {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int64 f0:30;
|
|
|
|
__int64 f1:30;
|
|
|
|
__int64 f2:30;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct foo3_bits {
|
2013-08-30 21:28:33 +00:00
|
|
|
__int16 f0:8;
|
|
|
|
__int16 f1:4;
|
|
|
|
__int16 f2:1;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
EOS
|
2013-08-30 21:28:33 +00:00
|
|
|
st = cp.toplevel.struct['foo_bits']
|
|
|
|
assert_equal(20, cp.sizeof(st))
|
|
|
|
assert_equal([0, 4], st.bitoffsetof(cp, 'f1'))
|
|
|
|
assert_equal([4, 4], st.bitoffsetof(cp, 'f2'))
|
|
|
|
assert_equal(8, st.offsetof(cp, 'f3'))
|
|
|
|
assert_equal(12, st.offsetof(cp, 'f4'))
|
|
|
|
assert_equal([0, 30], st.bitoffsetof(cp, 'f5'))
|
|
|
|
|
|
|
|
st = cp.toplevel.struct['foo_n_bits']
|
|
|
|
assert_equal(20, cp.sizeof(st))
|
|
|
|
assert_equal([0, 4], st.bitoffsetof(cp, 'f1'))
|
|
|
|
assert_equal([4, 4], st.bitoffsetof(cp, 'f2'))
|
|
|
|
assert_equal(8, st.offsetof(cp, 'f3'))
|
|
|
|
assert_equal(12, st.offsetof(cp, 'f4'))
|
|
|
|
assert_equal([0, 30], st.bitoffsetof(cp, 'f5'))
|
|
|
|
|
|
|
|
st = cp.toplevel.struct['foo2_bits']
|
|
|
|
assert_equal([0, 30], st.bitoffsetof(cp, 'f0'))
|
|
|
|
assert_equal([30, 30], st.bitoffsetof(cp, 'f1'))
|
|
|
|
assert_equal([0, 30], st.bitoffsetof(cp, 'f2'))
|
|
|
|
|
|
|
|
st = cp.toplevel.struct['foo3_bits']
|
|
|
|
assert_equal(2, cp.sizeof(st))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_allocstruct
|
2011-04-06 17:40:01 +00:00
|
|
|
cp.parse <<EOS
|
|
|
|
struct foo_outer {
|
2013-08-30 21:28:33 +00:00
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
} inner;
|
2011-04-06 17:40:01 +00:00
|
|
|
};
|
|
|
|
EOS
|
2013-08-30 21:28:33 +00:00
|
|
|
s = cp.alloc_c_struct('foo_outer', :i => :size)
|
|
|
|
assert_equal(12, s.length)
|
|
|
|
assert_equal(12, s.i)
|
|
|
|
assert_raise(RuntimeError) { s.l = 42 }
|
|
|
|
assert_nothing_raised { s.j = 0x12345678 }
|
|
|
|
assert_nothing_raised { s.inner.k = 0x3333_3333 }
|
|
|
|
assert_equal(4, s.inner.stroff)
|
|
|
|
assert_equal("0C0000007856341233333333", s.str.unpack('H*')[0].upcase)
|
|
|
|
end
|
2011-04-06 17:40:01 +00:00
|
|
|
end
|