Debian Codegraph @

prboom2/src/g_overflow.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 *
31 *---------------------------------------------------------------------
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdint.h>
41
42 #include "g_overflow.h"
43
44 #include "doomstat.h"
45 #include "lprintf.h"
46 #include "m_misc.h"
47 #include "w_wad.h"
48 #include "e6y.h"
49
50 #include "dsda/args.h"
51
52 int overflows_enabled = true;
53
54 overrun_param_t overflows[OVERFLOW_MAX];
55 const char *overflow_cfgname[OVERFLOW_MAX] =
56 {
57 "overrun_spechit_emulate",
58 "overrun_reject_emulate",
59 "overrun_intercept_emulate",
60 "overrun_playeringame_emulate",
61 "overrun_donut_emulate",
62 "overrun_missedbackside_emulate"
63 };
64
65 void ResetOverruns(void)
66 {
67 overrun_list_t overflow;
68 for (overflow = 0; overflow < OVERFLOW_MAX; ++overflow)
69 {
70 overflows[overflow].happened = false;
71 }
72 }
73
74 static void ShowOverflowWarning(overrun_list_t overflow, int fatal, const char *params, ...)
75 {
76 overflows[overflow].happened = true;
77
78 if (overflows[overflow].warn && !overflows[overflow].promted)
79 {
80 va_list argptr;
81 char buffer[1024];
82
83 static const char *name[OVERFLOW_MAX] = {
84 "SPECHIT", "REJECT", "INTERCEPT", "PLYERINGAME", "DONUT", "MISSEDBACKSIDE"};
85
86 static const char str1[] =
87 "Too big or not supported %s overflow has been detected. "
88 "Desync or crash can occur soon "
89 "or during playback with the vanilla engine in case you're recording demo.%s%s";
90
91 static const char str2[] =
92 "%s overflow has been detected.%s%s";
93
94 static const char str3[] =
95 "%s overflow has been detected. "
96 "The option responsible for emulation of this overflow is switched off "
97 "hence desync or crash can occur soon "
98 "or during playback with the vanilla engine in case you're recording demo.%s%s";
99
100 overflows[overflow].promted = true;
101
102 snprintf(buffer, sizeof(buffer),
103 (fatal ? str1 : (EMULATE(overflow) ? str2 : str3)),
104 name[overflow],
105 "\nYou can change PrBoom behaviour for this overflow through in-game menu.",
106 params);
107
108 va_start(argptr, params);
109 I_vWarning(buffer, argptr);
110 va_end(argptr);
111 }
112 }
113
114 // e6y
115 //
116 // Intercepts Overrun emulation
117 // See more information on:
118 // doomworld.com/vb/doom-speed-demos/35214-spechits-reject-and-intercepts-overflow-lists
119 //
120 // Thanks to Simon Howard (fraggle) for refactor the intercepts
121 // overrun code so that it should work properly on big endian machines
122 // as well as little endian machines.
123
124 // Overwrite a specific memory location with a value.
125 static void InterceptsMemoryOverrun(int location, int value)
126 {
127 int i, offset;
128 int index;
129 void *addr;
130 void *addr2;
131
132 i = 0;
133 offset = 0;
134
135 // Search down the array until we find the right entry
136
137 while (intercepts_overrun[i].len != 0)
138 {
139 if (offset + intercepts_overrun[i].len > location)
140 {
141 addr = intercepts_overrun[i].addr;
142 addr2 = intercepts_overrun[i].addr2;
143
144 // Write the value to the memory location.
145 // 16-bit and 32-bit values are written differently.
146
147 if (addr != NULL)
148 {
149 // 2 shorts
150 if (intercepts_overrun[i].addr2)
151 {
152 *((short *) addr) = value & 0xffff;
153 *((short *) addr2) = (value >> 16) & 0xffff;
154 }
155 else
156 {
157 index = (location - offset) / 4;
158 ((int *) addr)[index] = value;
159 }
160 }
161
162 break;
163 }
164
165 offset += intercepts_overrun[i].len;
166 ++i;
167 }
168 }
169
170 void InterceptsOverrun(int num_intercepts, intercept_t *intercept)
171 {
172 void P_MustRebuildBlockmap(void);
173
174 if (!hexen && num_intercepts > MAXINTERCEPTS_ORIGINAL && demo_compatibility && PROCESS(OVERFLOW_INTERCEPT))
175 {
176 ShowOverflowWarning(OVERFLOW_INTERCEPT, false, "");
177
178 if (EMULATE(OVERFLOW_INTERCEPT))
179 {
180 int location = (num_intercepts - MAXINTERCEPTS_ORIGINAL - 1) * 12;
181
182 // Overwrite memory that is overwritten in Vanilla Doom, using
183 // the values from the intercept structure.
184 //
185 // Note: the ->d.{thing,line} member should really have its
186 // address translated into the correct address value for
187 // Vanilla Doom.
188
189 InterceptsMemoryOverrun(location, intercept->frac);
190 InterceptsMemoryOverrun(location + 4, intercept->isaline);
191 InterceptsMemoryOverrun(location + 8, (int)(intptr_t) intercept->d.thing);
192
193 P_MustRebuildBlockmap();
194 }
195 }
196 }
197
198 // e6y
199 // playeringame overrun emulation
200 // it detects and emulates overflows on vex6d.wad\bug_wald(toke).lmp, etc.
201 // http://www.doom2.net/doom2/research/runningbody.zip
202
203 int PlayeringameOverrun(const mapthing_t* mthing)
204 {
205 if (mthing->type == 0 && PROCESS(OVERFLOW_PLAYERINGAME))
206 {
207 // playeringame[-1] == players[3].didsecret
208 ShowOverflowWarning(OVERFLOW_PLAYERINGAME, (players + 3)->didsecret, "");
209
210 if (EMULATE(OVERFLOW_PLAYERINGAME))
211 {
212 return true;
213 }
214 }
215 return false;
216 }
217
218 //
219 // spechit overrun emulation
220 //
221
222 unsigned int spechit_baseaddr = 0;
223
224 // e6y
225 // Code to emulate the behavior of Vanilla Doom when encountering an overrun
226 // of the spechit array.
227 // No more desyncs on compet-n\hr.wad\hr18*.lmp, all strain.wad\map07 demos etc.
228 // http://www.doomworld.com/vb/showthread.php?s=&threadid=35214
229 // See more information on:
230 // doomworld.com/vb/doom-speed-demos/35214-spechits-reject-and-intercepts-overflow-lists
231 void SpechitOverrun(spechit_overrun_param_t *params)
232 {
233 int numspechit = *(params->numspechit);
234
235 if (!hexen && demo_compatibility && numspechit > 8)
236 {
237 line_t **spechit = *(params->spechit);
238
239 ShowOverflowWarning(OVERFLOW_SPECHIT,
240 numspechit >
241 (compatibility_level == dosdoom_compatibility ||
242 compatibility_level == tasdoom_compatibility ? 10 : 14),
243 "\n\nThe list of LineID leading to overrun:\n%d, %d, %d, %d, %d, %d, %d, %d, %d.",
244 spechit[0]->iLineID, spechit[1]->iLineID, spechit[2]->iLineID,
245 spechit[3]->iLineID, spechit[4]->iLineID, spechit[5]->iLineID,
246 spechit[6]->iLineID, spechit[7]->iLineID, spechit[8]->iLineID);
247
248 if (EMULATE(OVERFLOW_SPECHIT))
249 {
250 unsigned int addr;
251
252 if (spechit_baseaddr == 0)
253 {
254 dsda_arg_t *arg;
255
256 // This is the first time we have had an overrun. Work out
257 // what base address we are going to use.
258 // Allow a spechit value to be specified on the command line.
259
260 //
261 // Use the specified magic value when emulating spechit overruns.
262 //
263
264 arg = dsda_Arg(dsda_arg_spechit);
265 if (arg->found)
266 {
267 spechit_baseaddr = (unsigned int) arg->value.v_int;
268 }
269 else
270 {
271 spechit_baseaddr = DEFAULT_SPECHIT_MAGIC;
272 }
273 }
274
275 // Calculate address used in doom2.exe
276
277 addr = spechit_baseaddr + (params->line - lines) * 0x3E;
278
279 if (compatibility_level == dosdoom_compatibility || compatibility_level == tasdoom_compatibility)
280 {
281 // There are no more desyncs in the following dosdoom demos:
282 // flsofdth.wad\fod3uv.lmp - http://www.doomworld.com/sda/flsofdth.htm
283 // hr.wad\hf181430.lmp - http://www.doomworld.com/tas/hf181430.zip
284 // hr.wad\hr181329.lmp - http://www.doomworld.com/tas/hr181329.zip
285 // icarus.wad\ic09uv.lmp - http://competn.doom2.net/pub/sda/i-o/icuvlmps.zip
286
287 switch(numspechit)
288 {
289 case 9:
290 *(params->tmfloorz) = addr;
291 break;
292 case 10:
293 *(params->tmceilingz) = addr;
294 break;
295
296 default:
297 fprintf(stderr, "SpechitOverrun: Warning: unable to emulate"
298 "an overrun where numspechit=%i\n",
299 numspechit);
300 break;
301 }
302 }
303 else
304 {
305 switch(numspechit)
306 {
307 case 9:
308 case 10:
309 case 11:
310 case 12:
311 params->tmbbox[numspechit-9] = addr;
312 break;
313 case 13:
314 *(params->nofit) = addr;
315 break;
316 case 14:
317 *(params->crushchange) = addr;
318
319 // In vanilla doom, this field is interpreted as a boolean,
320 // but it now stores the crushing damage itself.
321 // Since any nonzero value should yield doom's damage,
322 // we can set this explicitly.
323 if (*params->crushchange)
324 *params->crushchange = 10;
325
326 break;
327
328 default:
329 lprintf(LO_ERROR, "SpechitOverrun: Warning: unable to emulate"
330 " an overrun where numspechit=%i\n",
331 numspechit);
332 break;
333 }
334 }
335 }
336 }
337 }
338
339 //
340 // reject overrun emulation
341 //
342
343 // padding the reject table if it is too short
344 // totallines must be the number returned by P_GroupLines()
345 // an underflow will be padded with zeroes, or a doom.exe z_zone header
346 //
347 // e6y
348 // reject overrun emulation code
349 // It's emulated successfully if the size of overflow no more than 16 bytes.
350 // No more desync on teeth-32.wad\teeth-32.lmp.
351 // http://www.doomworld.com/vb/showthread.php?s=&threadid=35214
352
353 void RejectOverrun(unsigned int length, const byte **rejectmatrix, int totallines)
354 {
355 unsigned int required;
356 byte *newreject;
357 unsigned char pad;
358
359 required = (numsectors * numsectors + 7) / 8;
360
361 if (length < required)
362 {
363 // allocate a new block and copy the reject table into it; zero the rest
364 newreject = Z_MallocLevel(required);
365 *rejectmatrix = length ? memmove(newreject, *rejectmatrix, length) : newreject;
366
367 // e6y
368 // PrBoom 2.2.5 and 2.2.6 padded a short REJECT with 0xff
369 // This command line switch is needed for all potential demos
370 // recorded with these versions of PrBoom on maps with too short REJECT
371 // I don't think there are any demos that will need it but yes that seems sensible
372 pad = prboom_comp[PC_REJECT_PAD_WITH_FF].state ? 0xff : 0;
373
374 memset(newreject + length, pad, required - length);
375
376 if (!hexen && demo_compatibility && PROCESS(OVERFLOW_REJECT))
377 {
378 ShowOverflowWarning(OVERFLOW_REJECT, (required - length > 16) || (length%4 != 0), "");
379
380 if (EMULATE(OVERFLOW_REJECT))
381 {
382 // merged in RejectOverrunAddInt(), and the 4 calls to it, here
383 unsigned int rejectpad[4] = {
384 0, // size, will be filled in using totallines
385 0, // part of the header of a doom.exe z_zone block
386 50, // DOOM_CONST_PU_LEVEL
387 0x1d4a11 // DOOM_CONST_ZONEID
388 };
389 unsigned int i, pad = 0, *src = rejectpad;
390 byte *dest = newreject + length;
391
392 rejectpad[0] = ((totallines*4+3)&~3)+24; // doom.exe zone header size
393
394 // copy at most 16 bytes from rejectpad
395 // emulating a 32-bit, little-endian architecture (can't memmove)
396 for (i = 0; i < (unsigned int)(required - length) && i < 16; i++) { // 16 hard-coded
397 if (!(i&3)) // get the next 4 bytes to copy when i=0,4,8,12
398 pad = *src++;
399 *dest++ = pad & 0xff; // store lowest-significant byte
400 pad >>= 8; // rotate the next byte down
401 }
402 }
403 }
404
405 if (length)
406 lprintf(LO_WARN, "P_LoadReject: REJECT too short (%u<%u) - padded\n", length, required);
407 }
408 }
409
410 //
411 // Read Access Violation emulation.
412 //
413
414 // C:\>debug
415 // -d 0:0
416 //
417 // DOS 6.22:
418 // 0000:0000 (57 92 19 00) F4 06 70 00-(16 00)
419 // DOS 7.1:
420 // 0000:0000 (9E 0F C9 00) 65 04 70 00-(16 00)
421 // Win98:
422 // 0000:0000 (9E 0F C9 00) 65 04 70 00-(16 00)
423 // DOSBox under XP:
424 // 0000:0000 (00 00 00 F1) ?? ?? ?? 00-(07 00)
425
426 #define DOS_MEM_DUMP_SIZE 10
427
428 unsigned char mem_dump_dos622[DOS_MEM_DUMP_SIZE] = {
429 0x57, 0x92, 0x19, 0x00, 0xF4, 0x06, 0x70, 0x00, 0x16, 0x00};
430 unsigned char mem_dump_win98[DOS_MEM_DUMP_SIZE] = {
431 0x9E, 0x0F, 0xC9, 0x00, 0x65, 0x04, 0x70, 0x00, 0x16, 0x00};
432 unsigned char mem_dump_dosbox[DOS_MEM_DUMP_SIZE] = {
433 0x00, 0x00, 0x00, 0xF1, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00};
434
435 unsigned char *dos_mem_dump = mem_dump_dos622;
436
437 static int GetMemoryValue(unsigned int offset, void *value, int size)
438 {
439 static int firsttime = true;
440
441 if (firsttime)
442 {
443 int i, val;
444 dsda_arg_t *arg;
445
446 firsttime = false;
447 i = 0;
448
449 arg = dsda_Arg(dsda_arg_setmem);
450 if (arg->found)
451 {
452 if (!strcasecmp(arg->value.v_string_array[0], "dos622"))
453 dos_mem_dump = mem_dump_dos622;
454 if (!strcasecmp(arg->value.v_string_array[0], "dos71"))
455 dos_mem_dump = mem_dump_win98;
456 else if (!strcasecmp(arg->value.v_string_array[0], "dosbox"))
457 dos_mem_dump = mem_dump_dosbox;
458 else
459 {
460 while (i < DOS_MEM_DUMP_SIZE)
461 {
462 M_StrToInt(arg->value.v_string_array[i], &val);
463 dos_mem_dump[i++] = (unsigned char)val;
464 }
465 }
466 }
467 }
468
469 if (value)
470 {
471 switch (size)
472 {
473 case 1:
474 *((unsigned char*)value) = *((unsigned char*)(&dos_mem_dump[offset]));
475 return true;
476 case 2:
477 *((unsigned short*)value) = *((unsigned short*)(&dos_mem_dump[offset]));
478 return true;
479 case 4:
480 *((unsigned int*)value) = *((unsigned int*)(&dos_mem_dump[offset]));
481 return true;
482 }
483 }
484
485 return false;
486 }
487
488 //
489 // donut overrun emulation (linedef action #9)
490 //
491
492 #define DONUT_FLOORPIC_DEFAULT 0x16
493 int DonutOverrun(fixed_t *pfloorheight, short *pfloorpic)
494 {
495 if (demo_compatibility && PROCESS(OVERFLOW_DONUT))
496 {
497 ShowOverflowWarning(OVERFLOW_DONUT, 0, "");
498
499 if (EMULATE(OVERFLOW_DONUT))
500 {
501 if (pfloorheight && pfloorpic)
502 {
503 GetMemoryValue(0, pfloorheight, 4);
504 GetMemoryValue(8, pfloorpic, 2);
505
506 // bounds-check floorpic
507 if ((*pfloorpic) <= 0 || (*pfloorpic) >= numflats)
508 {
509 *pfloorpic = MIN(numflats - 1, DONUT_FLOORPIC_DEFAULT);
510 }
511
512 return true;
513 }
514 }
515 }
516
517 return false;
518 }
519
520
521 int MissedBackSideOverrun(line_t *line)
522 {
523 if (demo_compatibility)
524 {
525 if (line)
526 {
527 ShowOverflowWarning(OVERFLOW_MISSEDBACKSIDE, 0,
528 "\n\nLinedef %d has two-sided flag set, but no second sidedef",
529 line->iLineID);
530 }
531 else
532 {
533 ShowOverflowWarning(OVERFLOW_MISSEDBACKSIDE, 0, "");
534 }
535 }
536
537 return false;
538 }
539
540 //
541 // GetSectorAtNullAddress
542 //
543 sector_t* GetSectorAtNullAddress(void)
544 {
545 static int null_sector_is_initialized = false;
546 static sector_t null_sector;
547
548 if (demo_compatibility && EMULATE(OVERFLOW_MISSEDBACKSIDE))
549 {
550 if (!null_sector_is_initialized)
551 {
552 memset(&null_sector, 0, sizeof(null_sector));
553 null_sector.flags = NULL_SECTOR;
554 GetMemoryValue(0, &null_sector.floorheight, 4);
555 GetMemoryValue(4, &null_sector.ceilingheight, 4);
556 null_sector_is_initialized = true;
557 }
558
559 return &null_sector;
560 }
561
562 return 0;
563 }