1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
#!./perl -w
=pod
=head1 TEST FOR B::Assembler.pm AND B::Disassembler.pm
=head2 Description
The general idea is to test by assembling a choice set of assembler
instructions, then disassemble them, and check that we've completed the
round trip. Also, error checking of Assembler.pm is tested by feeding
it assorted errors.
Since Assembler.pm likes to assemble a file, we comply by writing a
text file. This file contains three sections:
testing operand categories
use each opcode
erronous assembler instructions
An "operand category" is identified by the suffix of the PUT_/GET_
subroutines as shown in the C<%Asmdata::insn_data> initialization, e.g.
opcode C<ldsv> has operand category C<svindex>:
insn_data{ldsv} = [1, \&PUT_svindex, "GET_svindex"];
Because Disassembler.pm also assumes input from a file, we write the
resulting object code to a file. And disassembled output is written to
yet another text file which is then compared to the original input.
(Erronous assembler instructions still generate code, but this is not
written to the object file; therefore disassembly bails out at the first
instruction in error.)
All files are kept in memory by using TIEHASH.
=head2 Caveats
An error where Assembler.pm and Disassembler.pm agree but Assembler.pm
generates invalid object code will not be detected.
Due to the way this test has been set up, failure of a single test
could cause all subsequent tests to fail as well: After an unexpected
assembler error no output is written, and disassembled lines will be
out of sync for all lines thereafter.
Not all possibilities for writing a valid operand value can be tested
because disassembly results in a uniform representation.
=head2 Maintenance
New opcodes are added automatically.
A new operand category will cause this program to die ("no operand list
for XXX"). The cure is to add suitable entries to C<%goodlist> and
C<%badlist>. (Since the data in Asmdata.pm is autogenerated, it may also
happen that the corresponding assembly or disassembly subroutine is
missing.) Note that an empty array as a C<%goodlist> entry means that
opcodes of the operand category do not take an operand (and therefore the
corresponding entry in C<%badlist> should have one). An C<undef> entry
in C<%badlist> means that any value is acceptable (and thus there is no
way to cause an error).
Set C<$dbg> to debug this test.
=cut
package VirtFile;
use strict;
# Note: This is NOT a general purpose package. It implements
# sequential text and binary file i/o in a rather simple form.
sub TIEHANDLE($;$){
my( $class, $data ) = @_;
my $obj = { data => defined( $data ) ? $data : '',
pos => 0 };
return bless( $obj, $class );
}
sub PRINT($@){
my( $self ) = shift;
$self->{data} .= join( '', @_ );
}
sub WRITE($$;$$){
my( $self, $buf, $len, $offset ) = @_;
unless( defined( $len ) ){
$len = length( $buf );
$offset = 0;
}
unless( defined( $offset ) ){
$offset = 0;
}
$self->{data} .= substr( $buf, $offset, $len );
return $len;
}
sub GETC($){
my( $self ) = @_;
return undef() if $self->{pos} >= length( $self->{data} );
return substr( $self->{data}, $self->{pos}++, 1 );
}
sub READLINE($){
my( $self ) = @_;
return undef() if $self->{pos} >= length( $self->{data} );
my $lfpos = index( $self->{data}, "\n", $self->{pos} );
if( $lfpos < 0 ){
$lfpos = length( $self->{data} );
}
my $pos = $self->{pos};
$self->{pos} = $lfpos + 1;
return substr( $self->{data}, $pos, $self->{pos} - $pos );
}
sub READ($@){
my $self = shift();
my $bufref = \$_[0];
my( undef, $len, $offset ) = @_;
if( $offset ){
die( "offset beyond end of buffer\n" )
if ! defined( $$bufref ) || $offset > length( $$bufref );
} else {
$$bufref = '';
$offset = 0;
}
my $remlen = length( $self->{data} ) - $self->{pos};
$len = $remlen if $remlen < $len;
return 0 unless $len;
substr( $$bufref, $offset, $len ) =
substr( $self->{data}, $self->{pos}, $len );
$self->{pos} += $len;
return $len;
}
sub TELL($){
my $self = shift();
return $self->{pos};
}
sub CLOSE($){
my( $self ) = @_;
$self->{pos} = 0;
}
1;
package main;
use strict;
use Test::More;
use Config qw(%Config);
BEGIN {
if (($Config{'extensions'} !~ /\bByteLoader\b/) ){
print "1..0 # Skip -- Perl configured without ByteLoader module\n";
exit 0;
}
}
use B::Asmdata qw( %insn_data );
use B::Assembler qw( &assemble_fh );
use B::Disassembler qw( &disassemble_fh &get_header );
my( %opsByType, @code2name );
my( $lineno, $dbg, $firstbadline, @descr );
$dbg = 0; # debug switch
# $SIG{__WARN__} handler to catch Assembler error messages
#
my $warnmsg;
sub catchwarn($){
$warnmsg = $_[0];
print "error: $warnmsg\n" if $dbg;
}
# Callback for writing assembled bytes. This is where we check
# that we do get an error.
#
sub putobj($){
if( ++$lineno >= $firstbadline ){
ok( $warnmsg && $warnmsg =~ /^\d+:\s/, $descr[$lineno] );
undef( $warnmsg );
} else {
my $l = syswrite( OBJ, $_[0] );
}
}
# Callback for writing a disassembled statement.
#
sub putdis(@){
my $line = join( ' ', @_ );
++$lineno;
print DIS "$line\n";
printf "%5d %s\n", $lineno, $line if $dbg;
}
# Generate assembler instructions from a hash of operand types: each
# existing entry contains a list of good or bad operand values. The
# corresponding opcodes can be found in %opsByType.
#
sub gen_type($$$){
my( $href, $descref, $text ) = @_;
for my $odt ( sort( keys( %opsByType ) ) ){
my $opcode = $opsByType{$odt}->[0];
my $sel = $odt;
$sel =~ s/^GET_//;
die( "no operand list for $sel\n" ) unless exists( $href->{$sel} );
if( defined( $href->{$sel} ) ){
if( @{$href->{$sel}} ){
for my $od ( @{$href->{$sel}} ){
++$lineno;
$descref->[$lineno] = "$text: $code2name[$opcode] $od";
print ASM "$code2name[$opcode] $od\n";
printf "%5d %s %s\n", $lineno, $code2name[$opcode], $od if $dbg;
}
} else {
++$lineno;
$descref->[$lineno] = "$text: $code2name[$opcode]";
print ASM "$code2name[$opcode]\n";
printf "%5d %s\n", $lineno, $code2name[$opcode] if $dbg;
}
}
}
}
# Interesting operand values
#
my %goodlist = (
comment_t => [ '"a comment"' ], # no \n
none => [],
svindex => [ 0x7fffffff, 0 ],
opindex => [ 0x7fffffff, 0 ],
pvindex => [ 0x7fffffff, 0 ],
U32 => [ 0xffffffff, 0 ],
U8 => [ 0xff, 0 ],
PV => [ '""', '"a string"', ],
I32 => [ -0x80000000, 0x7fffffff ],
IV64 => [ '0x000000000', '0x0ffffffff', '0x000000001' ], # disass formats 0x%09x
IV => $Config{ivsize} == 4 ?
[ -0x80000000, 0x7fffffff ] :
[ '0x000000000', '0x0ffffffff', '0x000000001' ],
NV => [ 1.23456789E3 ],
U16 => [ 0xffff, 0 ],
pvcontents => [],
strconst => [ '""', '"another string"' ], # no NUL
op_tr_array => [ join( ',', 256, 0..255 ) ],
PADOFFSET => undef,
long => undef,
);
# Erronous operand values
#
my %badlist = (
comment_t => [ '"multi-line\ncomment"' ], # no \n
none => [ '"spurious arg"' ],
svindex => [ 0xffffffff * 2, -1 ],
opindex => [ 0xffffffff * 2, -2 ],
pvindex => [ 0xffffffff * 2, -3 ],
U32 => [ 0xffffffff * 2, -4 ],
U16 => [ 0x5ffff, -5 ],
U8 => [ 0x6ff, -6 ],
PV => [ 'no quote"' ],
I32 => [ -0x80000001, 0x80000000 ],
IV64 => undef, # PUT_IV64 doesn't check - no integrity there
IV => $Config{ivsize} == 4 ?
[ -0x80000001, 0x80000000 ] : undef,
NV => undef, # PUT_NV accepts anything - it shouldn't, real-ly
pvcontents => [ '"spurious arg"' ],
strconst => [ 'no quote"', '"with NUL '."\0".' char"' ], # no NUL
op_tr_array => undef, # op_pv_tr is no longer exactly 256 shorts
PADOFFSET => undef,
long => undef,
);
# Determine all operand types from %Asmdata::insn_data
#
for my $opname ( keys( %insn_data ) ){
my ( $opcode, $put, $getname ) = @{$insn_data{$opname}};
push( @{$opsByType{$getname}}, $opcode );
$code2name[$opcode] = $opname;
}
# Write instruction(s) for correct operand values each operand type class
#
$lineno = 0;
tie( *ASM, 'VirtFile' );
gen_type( \%goodlist, \@descr, 'round trip' );
# Write one instruction for each opcode.
#
for my $opcode ( 0..$#code2name ){
next unless defined( $code2name[$opcode] );
my $sel = $insn_data{$code2name[$opcode]}->[2];
$sel =~ s/^GET_//;
die( "no operand list for $sel\n" ) unless exists( $goodlist{$sel} );
if( defined( $goodlist{$sel} ) ){
++$lineno;
if( @{$goodlist{$sel}} ){
my $od = $goodlist{$sel}[0];
$descr[$lineno] = "round trip: $code2name[$opcode] $od";
print ASM "$code2name[$opcode] $od\n";
printf "%5d %s %s\n", $lineno, $code2name[$opcode], $od if $dbg;
} else {
$descr[$lineno] = "round trip: $code2name[$opcode]";
print ASM "$code2name[$opcode]\n";
printf "%5d %s\n", $lineno, $code2name[$opcode] if $dbg;
}
}
}
# Write instruction(s) for incorrect operand values each operand type class
#
$firstbadline = $lineno + 1;
gen_type( \%badlist, \@descr, 'asm error' );
# invalid opcode is an odd-man-out ;-)
#
++$lineno;
$descr[$lineno] = "asm error: Gollum";
print ASM "Gollum\n";
printf "%5d %s\n", $lineno, 'Gollum' if $dbg;
close( ASM );
# Now that we have defined all of our tests: plan
#
plan( tests => $lineno );
print "firstbadline=$firstbadline\n" if $dbg;
# assemble (guard against warnings and death from assembly errors)
#
$SIG{'__WARN__'} = \&catchwarn;
$lineno = -1; # account for the assembly header
tie( *OBJ, 'VirtFile' );
eval { assemble_fh( \*ASM, \&putobj ); };
print "eval: $@" if $dbg;
close( ASM );
close( OBJ );
$SIG{'__WARN__'} = 'DEFAULT';
# disassemble
#
print "--- disassembling ---\n" if $dbg;
$lineno = 0;
tie( *DIS, 'VirtFile' );
disassemble_fh( \*OBJ, \&putdis );
close( OBJ );
close( DIS );
# get header (for debugging only)
#
if( $dbg ){
my( $magic, $archname, $blversion, $ivsize, $ptrsize, $byteorder ) =
get_header();
printf "Magic: 0x%08x\n", $magic;
print "Architecture: $archname\n";
print "Byteloader V: $blversion\n";
print "ivsize: $ivsize\n";
print "ptrsize: $ptrsize\n";
print "Byteorder: $byteorder\n";
}
# check by comparing files line by line
#
print "--- checking ---\n" if $dbg;
$lineno = 0;
my( $asmline, $disline );
while( defined( $asmline = <ASM> ) ){
$disline = <DIS>;
++$lineno;
last if $lineno eq $firstbadline; # bail out where errors begin
ok( $asmline eq $disline, $descr[$lineno] );
printf "%5d %s\n", $lineno, $asmline if $dbg;
}
close( ASM );
close( DIS );
__END__
|