[C ++] how to ensure that an object is read-only during runtime

Source: Internet
Author: User

I believe many people have encountered a problem:CodeToo many objects. I don't know where to modify this object. There are actually two ways.

1. When debugging, you can set a data breakpoint.

GDB has a watch breakpoint. for example, GDB> watch * (int *) 0x12433. Remember to use an address if you want to monitor the data. Otherwise, the data breakpoint will be invalid after the scope is passed, in addition, if the value of monitoring can be expressed by built-in data types, there is a hardware breakpoint, otherwise the efficiency will be very low .....

2. During running, this article focuses on this.

Let's take a look at it. We all know that there are several exec segments, such as code segments and data segments. these segments have read and write attributes. For example, the code segment can only be read, and the stack segment can be read and written ~~. So we want to insert an object into a segment that cannot be written, such as The. Text Segment... (in fact, I have inserted it, but there will be a warning)

This exception is violent, and the reserved space is too small to fit.

Currently, the operating system uses the segment mode + paging mode to manage the memory. The segment mode does not work. In the paging mode, find a way to set the Memory Page attributes ~~.

Fortunately, there is a mprotect System Call in Linux. You can set the read/write attribute of the Memory Page during runtime. The only requirement is that the memory needs to be 4 K aligned, which wastes some time.

OK, let's take a look at mprotect's man page: http://linux.die.net/man/2/mprotect

Brief answer: int mprotect (const void * ADDR, size_t Len, int prot );

An address, memory length, and read/write attribute. The call result is returned. If the call is successful, 0 is returned. If the call fails, other numbers are returned.

By the way, let's take a look at the example in man page. There is a trick in it: Get a 4 K memory alignment ~~ Through (PTR + 4096-1 )&~ (4096-1) obtained

# Include <stdio. h> # include <stdlib. h> # include <errno. h> # include <sys/Mman. h> # include <limits. h>/* For pagesize */# ifndef pagesize # define pagesize 4096 # endifintmain (void) {char * P; char C;/* allocate a buffer; it will have the default protection of prot_read | prot_write. */P = malloc (1024 + PAGESIZE-1); If (! P) {perror ("couldn't malloc (1024)"); exit (errno);}/* align to a multiple of pagesize, assumed to be a power of two */P = (char *) (INT) P + PAGESIZE-1 )&~ (PAGESIZE-1); C = P [666];/* read; OK */P [666] = 42;/* write; OK * // * mark the buffer read-only. */If (mprotect (p, 1024, prot_read) {perror ("couldn't mprotect"); exit (errno);} c = P [666]; /* read; OK */P [666] = 42;/* write; program dies on SIGSEGV */exit (0 );}

At this point, we can ensure that an object cannot be written during running. If it is written, the core is dropped:-D.

PS:

I hope there will be similar system calls in windows and virtualprotect in windows. If you are interested, study it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.