Free Hero Mesh

Check-in [9f57c48591]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement stack overflow protection
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9f57c48591ae9d78e8265b93895ffcba2f7583fa
User & Date: user on 2018-06-24 02:12:44
Other Links: manifest | tags
Context
2018-07-02
21:24
Implement creating new objects and linking objects into the playfield (program execution is only started a bit and is not even nearly close to being written enough yet) check-in: d872cd754a user: user tags: trunk
2018-06-24
02:12
Implement stack overflow protection check-in: 9f57c48591 user: user tags: trunk
2018-06-22
07:17
Add exec.c (moving some things from main.c); add definitions for exec.c in heromesh.h; also add a macro tracing option. check-in: 9ffad27cd3 user: user tags: trunk
Changes

Modified class.c from [d86091200c] to [9452666c10].

635
636
637
638
639
640
641

642
643
644
645
646
647
648
}

static void nxttok(void) {
  if(pushback) {
    pushback=0;
    return;
  }

  again:
  nxttok1();
  if(tokent&TF_EOF) {
    if(inpstack) {
      InputStack s=*inpstack;
      free(inpstack);
      fclose(classfp);







>







635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
}

static void nxttok(void) {
  if(pushback) {
    pushback=0;
    return;
  }
  if(StackProtection()) fatal("Stack overflow\n");
  again:
  nxttok1();
  if(tokent&TF_EOF) {
    if(inpstack) {
      InputStack s=*inpstack;
      free(inpstack);
      fclose(classfp);

Modified exec.c from [7c6d309993] to [6979fc1925].

1
2
3
4
5
6

7
8
9
10
11
12
13
14
15
16
17
18








#if 0
gcc ${CFLAGS:--s -O2} -c exec.c `sdl-config --cflags`
exit
#endif

#include "SDL.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"

Uint32 generation_number;
Object*objects;
Uint32 nobjects;
Value globals[0x800];















>












>
>
>
>
>
>
>
>
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
#if 0
gcc ${CFLAGS:--s -O2} -c exec.c `sdl-config --cflags`
exit
#endif

#include "SDL.h"
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"

Uint32 generation_number;
Object*objects;
Uint32 nobjects;
Value globals[0x800];

static jmp_buf my_env;
static int my_error;

int execute_turn(int key) {
  if(setjmp(my_env)) return my_error;
  
  return 0;
}

Modified heromesh.h from [637f2cff6e] to [11a368eb64].

37
38
39
40
41
42
43



















44
45
46
47
48
49
50

extern sqlite3*userdb;
extern xrm_db*resourcedb;
extern const char*basefilename;
extern xrm_quark optionquery[16];
extern char main_options[128];
extern Uint8 message_trace[0x4100/8];




















unsigned char*read_lump(int sol,int lvl,long*sz,sqlite3_value**us);
void write_lump(int sol,int lvl,long sz,const unsigned char*data);
void set_cursor(int id);

#define FIL_SOLUTION 1
#define FIL_LEVEL 0







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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

extern sqlite3*userdb;
extern xrm_db*resourcedb;
extern const char*basefilename;
extern xrm_quark optionquery[16];
extern char main_options[128];
extern Uint8 message_trace[0x4100/8];

#ifdef __GNUC__
extern char stack_protect_mode;
extern void*stack_protect_mark;
extern void*stack_protect_low;
extern void*stack_protect_high;
#define StackProtection() (stack_protect_mode && ( \
  stack_protect_mode=='<' ? (__builtin_frame_address(0)<stack_protect_mark) : \
  stack_protect_mode=='>' ? (__builtin_frame_address(0)>stack_protect_mark) : \
  stack_protect_mode=='?' ? ({ \
    if(__builtin_frame_address(0)<stack_protect_low) stack_protect_low=__builtin_frame_address(0); \
    if(__builtin_frame_address(0)>stack_protect_high) stack_protect_high=__builtin_frame_address(0); \
    0; \
  }) : \
  stack_protect_mode=='!' ? 1 : \
0))
#else
#define StackProtection() 0
#endif

unsigned char*read_lump(int sol,int lvl,long*sz,sqlite3_value**us);
void write_lump(int sol,int lvl,long sz,const unsigned char*data);
void set_cursor(int id);

#define FIL_SOLUTION 1
#define FIL_LEVEL 0

Modified main.c from [8f25720cf2] to [d2ea69aa98].

39
40
41
42
43
44
45







46
47
48
49
50
51
52

sqlite3*userdb;
xrm_db*resourcedb;
const char*basefilename;
xrm_quark optionquery[16];
char main_options[128];
Uint8 message_trace[0x4100/8];








static const char*globalclassname;
static SDL_Cursor*cursor[77];
static FILE*levelfp;
static FILE*solutionfp;
static sqlite3_int64 leveluc,solutionuc;
static sqlite3_stmt*readusercachest;







>
>
>
>
>
>
>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

sqlite3*userdb;
xrm_db*resourcedb;
const char*basefilename;
xrm_quark optionquery[16];
char main_options[128];
Uint8 message_trace[0x4100/8];

#ifdef __GNUC__
char stack_protect_mode=0;
void*stack_protect_mark;
void*stack_protect_low;
void*stack_protect_high;
#endif

static const char*globalclassname;
static SDL_Cursor*cursor[77];
static FILE*levelfp;
static FILE*solutionfp;
static sqlite3_int64 leveluc,solutionuc;
static sqlite3_stmt*readusercachest;
559
560
561
562
563
564
565




















566
567
568
569
570
571
572
      txt[n++]=c;
    }
    if(n>=m) fatal("Too long SQL statement\n");
  }
  if(n) fatal("Unterminated SQL statement\n");
  free(txt);
}





















int main(int argc,char**argv) {
  int optind=1;
  while(argc>optind && argv[optind][0]=='-') {
    int i;
    const char*s=argv[optind++];
    if(s[1]=='-' && !s[2]) break;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
      txt[n++]=c;
    }
    if(n>=m) fatal("Too long SQL statement\n");
  }
  if(n) fatal("Unterminated SQL statement\n");
  free(txt);
}

#ifdef __GNUC__
static void test_stack_protection(void) {
  fprintf(stderr,"Stack protection final values: %p %p %p\n",stack_protect_mark,stack_protect_low,stack_protect_high);
}

static void set_stack_protection(void) {
  const char*v;
  optionquery[1]=Q_stackProtection;
  v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
  if(!v || !*v) return;
  stack_protect_mode=*v;
  if(*v=='?') {
    fprintf(stderr,"Stack protection test mode: %p\n",stack_protect_mark);
    stack_protect_low=stack_protect_high=stack_protect_mark;
    atexit(test_stack_protection);
  }
  if(v[1]) stack_protect_mark=((char*)stack_protect_mark)+strtoll(v+1,0,0);
}
#endif

int main(int argc,char**argv) {
  int optind=1;
  while(argc>optind && argv[optind][0]=='-') {
    int i;
    const char*s=argv[optind++];
    if(s[1]=='-' && !s[2]) break;
581
582
583
584
585
586
587







588
589
590
591
592
593
594
595
596
597
598
599
600
601
  basefilename=argv[optind++];
  if(argc>optind && argv[1][0]=='=') {
    globalclassname=argv[optind++]+1;
  } else if(find_globalclassname()) {
    globalclassname=strrchr(basefilename,'/');
    globalclassname=globalclassname?globalclassname+1:basefilename;
  }







  if(main_options['c']) {
    load_classes();
    return 0;
  }
  load_options();
  if(argc>optind) read_options(argc-optind,argv+optind);
  *optionquery=xrm_make_quark(globalclassname,0)?:xrm_anyq;
  init_sql();
  load_key_bindings();
  init_screen();
  load_pictures();
  if(main_options['T']) {
    test_mode();
    return 0;







>
>
>
>
>
>
>




<
<
<







608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625



626
627
628
629
630
631
632
  basefilename=argv[optind++];
  if(argc>optind && argv[1][0]=='=') {
    globalclassname=argv[optind++]+1;
  } else if(find_globalclassname()) {
    globalclassname=strrchr(basefilename,'/');
    globalclassname=globalclassname?globalclassname+1:basefilename;
  }
  if(!main_options['c']) load_options();
  if(argc>optind) read_options(argc-optind,argv+optind);
  *optionquery=xrm_make_quark(globalclassname,0)?:xrm_anyq;
#ifdef __GNUC__
  stack_protect_mark=__builtin_frame_address(0);
  set_stack_protection();
#endif
  if(main_options['c']) {
    load_classes();
    return 0;
  }



  init_sql();
  load_key_bindings();
  init_screen();
  load_pictures();
  if(main_options['T']) {
    test_mode();
    return 0;

Modified quarks from [28bc2772ba] to [289fcbcd17].

205
206
207
208
209
210
211

212
sqlSmallAllocations
sqlCoveringIndexScan
sqlPowerSafe

! Miscellaneous
level
tracePrefix









>

205
206
207
208
209
210
211
212
213
sqlSmallAllocations
sqlCoveringIndexScan
sqlPowerSafe

! Miscellaneous
level
tracePrefix
stackProtection

Modified quarks.h from [1c8a2f470a] to [e59ed81496].

170
171
172
173
174
175
176

177
178
179
180
181
182
183
#define Q_sqlExtensions 171
#define Q_sqlMemStatus 172
#define Q_sqlSmallAllocations 173
#define Q_sqlCoveringIndexScan 174
#define Q_sqlPowerSafe 175
#define Q_level 176
#define Q_tracePrefix 177

static const char*const global_quarks[]={
  "screenWidth",
  "screenHeight",
  "palette",
  "popupColors",
  "imageSize",
  "altImage",







>







170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#define Q_sqlExtensions 171
#define Q_sqlMemStatus 172
#define Q_sqlSmallAllocations 173
#define Q_sqlCoveringIndexScan 174
#define Q_sqlPowerSafe 175
#define Q_level 176
#define Q_tracePrefix 177
#define Q_stackProtection 178
static const char*const global_quarks[]={
  "screenWidth",
  "screenHeight",
  "palette",
  "popupColors",
  "imageSize",
  "altImage",
347
348
349
350
351
352
353

354
355
356
357
358
359
360
  "sqlExtensions",
  "sqlMemStatus",
  "sqlSmallAllocations",
  "sqlCoveringIndexScan",
  "sqlPowerSafe",
  "level",
  "tracePrefix",

0};
#ifdef HEROMESH_BINDINGS
static const SDLKey quark_to_key[Q_undo+1-Q_backspace]={
SDLK_BACKSPACE,
SDLK_TAB,
SDLK_CLEAR,
SDLK_RETURN,







>







348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  "sqlExtensions",
  "sqlMemStatus",
  "sqlSmallAllocations",
  "sqlCoveringIndexScan",
  "sqlPowerSafe",
  "level",
  "tracePrefix",
  "stackProtection",
0};
#ifdef HEROMESH_BINDINGS
static const SDLKey quark_to_key[Q_undo+1-Q_backspace]={
SDLK_BACKSPACE,
SDLK_TAB,
SDLK_CLEAR,
SDLK_RETURN,